Implement X-Forwarded-For

This commit is contained in:
Hri7566 2024-02-01 12:36:00 -05:00
parent 3ef51d525d
commit c9dccd0f38
3 changed files with 14 additions and 5 deletions

2
public

@ -1 +1 @@
Subproject commit 0f21e390667589f7acfef4d82a8f270a830822ee Subproject commit 6d894d19cd7dbe09f9fdf65cd954b916326c4437

View File

@ -56,11 +56,11 @@ export class Socket extends EventEmitter {
private cursorPos: Vector2<CursorValue> = { x: 200, y: 100 }; private cursorPos: Vector2<CursorValue> = { x: 200, y: 100 };
constructor( constructor(
private ws: ServerWebSocket<unknown>, private ws: ServerWebSocket<{ ip: string }>,
public socketID: string public socketID: string
) { ) {
super(); super();
this.ip = ws.remoteAddress; // Participant ID this.ip = ws.data.ip;
// User ID // User ID
this._id = createUserID(this.getIP()); this._id = createUserID(this.getIP());

View File

@ -31,11 +31,20 @@ async function getIndex() {
return response; return response;
} }
export const app = Bun.serve({ export const app = Bun.serve<{ ip: string }>({
port: env.PORT, port: env.PORT,
hostname: "0.0.0.0", hostname: "0.0.0.0",
fetch: (req, server) => { fetch: (req, server) => {
if (server.upgrade(req)) { const reqip = server.requestIP(req);
if (!reqip) return;
if (
server.upgrade(req, {
data: {
ip: req.headers.get("X-Forwarded-For") || reqip.address
}
})
) {
return; return;
} else { } else {
const url = new URL(req.url).pathname; const url = new URL(req.url).pathname;