Coder Social home page Coder Social logo

elysia-ip's Introduction

elysia-ip

badge

Get the Client Ip Address in Elysia.

It supports Cloudflare, Fastly and other runtimes as well.

Installation

Requires Bun v1.0.4 or above.

bun a elysia-ip

Documentation

Introduction

This plugin let's you get client's ip address in Elysia.js

It supports Multiple runtimes (Cloudflare, Fastly Edge, etc.)

Usage

import { Elysia } from "elysia";
import { ip } from "elysia-ip";

new Elysia().use(ip()).get("/", ({ ip }) => ip).listen(3000);

How does it work?

It relies on headers for runtimes other than Bun. Cloudflare and other providers send back specific headers, containing the IP address. For example CF-Connecting-IP for Cloudflare and Fastly-Client-IP for Fastly.

We also add support for X-Forwarded-For header (de-facto standard header) and other various headers.

Priority list:

  1. User specified
  2. X-Forwarded-For (de-facto standard header)
  3. CF-Connecting-IP (Cloudflare)
  4. Fastly-Client-IP (Fastly)
  5. X-Real-IP (Apache)
  6. X-Client-IP (Nginx)
  7. X-Cluster-Client-IP (GCP)
  8. X-Forwarded (RFC 7239)
  9. Forwarded-For (RFC 7239)
  10. Forwarded (RFC 7239)
  11. appengine-user-ip (GCP)
  12. true-client-ip (Akamai and Cloudflare)
  13. cf-pseudo-ipv4 (Cloudflare)

You can even specify your own header or list of headers if you want to as following

import { Elysia } from "elysia";
import { ip } from "elysia-ip";

new Elysia().use(ip({ checkHeaders: ["X-Forwarded-For", "X-Real-IP"] })).get("/", ({ ip }) => ip).listen(3000);

or

import { Elysia } from "elysia";
import { ip } from "elysia-ip";

new Elysia().use(ip({ checkHeaders: "X-Forwarded-For" })).get("/", ({ ip }) => ip).listen(3000);

For Bun runtime, We use server.requestIP introduced in Bun v1.0.4

License

MIT

Author

Copyright (c) 2023 Gaurish Sethia, All Rights Reserved.

elysia-ip's People

Contributors

caamillo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.