gaurishhs / elysia-ip Goto Github PK
View Code? Open in Web Editor NEWGet Client IP Address in Elysia
License: MIT License
Get Client IP Address in Elysia
License: MIT License
Does this plugin have a function to return only the IPv4 address without the ffff prefix?
{
"name": "study-app",
"version": "1.0.50",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "bun run --watch src/index.ts",
"start": "NODE_ENV=production bun run src/index.ts"
},
"dependencies": {
"@elysiajs/bearer": "^1.0.2",
"@elysiajs/cors": "^1.0.2",
"@elysiajs/jwt": "^1.0.2",
"@elysiajs/static": "^1.0.2",
"@elysiajs/swagger": "^0.8.5",
"@types/pluralize": "^0.0.33",
"elysia": "^1.0.10",
"mongoose": "^8.2.1",
"pluralize": "^8.0.0",
"tencentcloud-sdk-nodejs-sms": "^4.0.789",
"uuid": "^9.0.1"
},
"devDependencies": {
"@types/uuid": "^9.0.8",
"bun-types": "latest"
},
"module": "src/index.js"
}
I don't know what is causing the issue.
Duplicate when use multiple time.
Fix by using new Elysia() and name it using Elysia({ name: "elysia-ip" })
Planing add option injectServer like
https://github.com/rayriffy/elysia-rate-limit/blob/main/example/muliInstanceInjected.ts
for fix issue like rayriffy/elysia-rate-limit#29
Remove throw error it make app crash
Line 49 in f46952e
Fixed in Fork
https://github.com/armada45-pixel/elysia-ip/blob/main/src/index.ts
Exactly what the title says, use IP headers (eg. X-Forwarded-For
) over server.requestIP
if the IP headers exist.
Description:
I'm experiencing an issue with the elysia-ip when specifying headers for IP retrieval. Despite setting cf-connecting-ip as the header to check, the middleware is still returning the IP from the x-forwarded-for header.
Node.js Version: v20.11.0
Elysia Version: v1.0.20
elysia-ip Version: v1.0.5
bun Version: v1.0.26
import { Elysia } from "elysia";
import { ip } from "elysia-ip";
const app = new Elysia()
.use(ip({ headersOnly: true, checkHeaders: ["cf-connecting-ip"] })).get(
"/test",
({ ip, request }) => {
console.log("ip", ip);
console.log("headers", JSON.stringify(request.headers, null, 2));
return "777";
},
).listen(3000);
console.log(
`๐ฆ Elysia is running at ${app.server?.hostname}:${app.server?.port}`,
);
Run the following curl command to test
curl http://localhost:3000/test \
-H 'cf-connecting-ip: 192.168.1.1' \
-H 'x-forwarded-for: 192.168.2.2'
Observe that the console output shows the IP from x-forwarded-for instead of cf-connecting-ip.
Console Output:
ip 192.168.2.2
headers {
"cf-connecting-ip": "192.168.1.1",
"x-forwarded-for": "192.168.2.2"
}
Hi. First of all, thank you so much for this repo; it saved me a headache I was not interested in dealing with!
I'm only just getting started building out the app I'm working on, but I'm seeing this when I run the dev server and ping it from another device on my local network:
{
address: "::ffff:192.168.1.154",
family: "IPv6",
port: 56144,
}
I know the client IPv4 address is 192.168.1.154; I'm not sure why it's showing up here as IPv6 though.
false
in checkHeaders param it will make condition true but will error type error
Default value
elysia-ip/src/services/getip.ts
Line 7 in bd53e7f
elysia-ip/src/services/getip.ts
Line 15 in bd53e7f
IP returns null
. There are breaking changes with Elysia@v1. See #513.
const app = new Elysia()
.use(ip())
.get('/', ({ ip }) => {
console.log("ip", ip); // logs "ip null"
return "Hello"
})
Property 'ip' does not exist on type '{ body: { token: string; user: string; }; query: Record<string, string | undefined>; params: never; headers: Record<string, string | undefined>; cookie: Record<string, Cookie<any>>; set: { ...; }; path: string; request: Request; store: {}; }'.
.post("/api/UniUser/getUsers", ({ body, ip, path }) => {
if (main.debug) logger(`audit> ${ip.address} requested ${path}`, 4);
UniUser.getUsers(body.user, body.token)
}, {
body: t.Object({
user: t.String(),
token: t.String()
})
})
red underline at ip
in ({ body, ip, path })
Hi! I'm running into an issue while following the basic implementation guide from the docs:
import { Elysia } from "elysia";
import { ip } from "elysia-ip";
new Elysia().use(ip()).get("/", ({ ip }) => ip).listen(3000);
On version ^0.0.7
, TypeScript throws a Property 'ip' does not exist on type
error for the { ip }
reference.
This error disappears when I downgrade to version ^0.0.6
, where ip
is correctly typed as
string | SocketAddress | null | undefined
My environment is using the latest Bun version (v1.0.27
), with all other dependencies up to date, including TypeScript (^5.3.3
) and Elysia (^0.8.17
or latest
).
I have attached screenshots to illustrate the issue (0.0.7
left, and 0.0.6
right), but please let me know if there are any issues accessing them or if further details are required.
The removed "types": "./src/index.ts",
from root level of package.json in 0.0.6 now raise an error.
After installing the plugin and using it like use(ip())
,
the typescript engine is complaining that in route handling .get("/id/:id", ({ params: { id } }) => id)
, id
has type any
.
The same goes for other params as well: .onError(({ code, error }) => { return new Response(error.toString()); });
, code
and error
have type any
My Elysia version is 0.8.8 and I am using version elysia-ip 0.0.5
Just added this package and attempted to use it as shown in the README.
I get this error from typescript:
No overload matches this call.
The last overload gave the following error.
Argument of type '(app: Elysia<"", false, { decorator: {}; store: {}; derive: {}; resolve: {}; }, { type: {}; error: {}; }, { schema: {}; macro: {}; }, {}, { derive: {}; resolve: {}; schema: {}; }, { derive: {}; resolve: {}; schema: {}; }>) => Elysia<...>' is not assignable to parameter of type 'Promise<{ default: AnyElysia; }>'.ts(2769)
index.d.ts(777, 5): The last overload is declared here.
(alias) ip(userOptions?: Partial<Options> | undefined): (app: Elysia<"", false, {
decorator: {};
store: {};
derive: {};
resolve: {};
}, {
type: {};
error: {};
}, {
schema: {};
macro: {};
}, {}, {
...;
}, {
...;
}>) => Elysia<...>
Usage:
import { ip } from "elysia-ip"; const app = new Elysia().use(ip()).get("/", () => ({ message: "Hello Elysia" }));
package json:
"elysia": "latest",
It seems like the IP isn't included on the request object for a POST request.
{
request: Request (0 KB) {
method: "POST",
url: "http://localhost:3000/get-quote",
headers: Headers {
"host": "localhost:3000",
"connection": "keep-alive",
"content-length": "138",
"content-type": "multipart/form-data; boundary=----WebKitFormBoundarycWgoyItEDfYxlrDK",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"accept": "*/*",
"origin": "http://localhost:3000",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
"referer": "http://localhost:3000/",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en-GB;q=0.9,en;q=0.8,fr-CA;q=0.7,fr;q=0.6",
"sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
"x-alpine-request": "true",
"sec-ch-ua-mobile": "?0",
"x-alpine-target": "quote",
"sec-ch-ua-platform": "\"macOS\"",
"sec-fetch-site": "same-origin",
}
},
store: {},
qi: -1,
path: "/get-quote",
set: {
headers: {},
status: 200,
},
headers: {
host: "localhost:3000",
connection: "keep-alive",
"content-length": "138",
"content-type": "multipart/form-data; boundary=----WebKitFormBoundarycWgoyItEDfYxlrDK",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
accept: "*/*",
origin: "http://localhost:3000",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
referer: "http://localhost:3000/",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en-GB;q=0.9,en;q=0.8,fr-CA;q=0.7,fr;q=0.6",
"sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
"x-alpine-request": "true",
"sec-ch-ua-mobile": "?0",
"x-alpine-target": "quote",
"sec-ch-ua-platform": "\"macOS\"",
"sec-fetch-site": "same-origin",
},
query: {},
body: {
testing: "",
},
ip: null,
html: [Function: html],
stream: [Function: stream],
}
Is this something that's possible to do?
elysia-ip version: 1.0.0
So you get this error whenever you write something sane like this (e.g. to separate route definitions).
const app = new Elysia()
.use(cors())
.use(login)
.listen(envs.PORT);
const login = new Elysia()
.use(ip())
.post('/login', async ({ body, ip }) => {
console.log(ip);
...
});
export default login;
.ws('/api/nodeData', {
message(ws) {
setInterval(() => {
ws.send(nodeData)
}, 1000)
}
})
Where should i add it to?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.