chore: Format code again.
This commit is contained in:
parent
5f1df9ca88
commit
3143800c92
5 changed files with 255 additions and 171 deletions
|
@ -9,7 +9,12 @@ import type { ConnectedClientExt, PassyFireBackendProvider } from "./index.js";
|
|||
// This code sucks because this protocol sucks BUUUT it works, and I don't wanna reinvent
|
||||
// the gosh darn wheel for (almost) no reason
|
||||
|
||||
function authenticateSocket(instance: PassyFireBackendProvider, ws: WebSocket, message: string, state: ConnectedClientExt): Boolean {
|
||||
function authenticateSocket(
|
||||
instance: PassyFireBackendProvider,
|
||||
ws: WebSocket,
|
||||
message: string,
|
||||
state: ConnectedClientExt,
|
||||
): Boolean {
|
||||
if (!message.startsWith("Accept: ")) {
|
||||
ws.send("400 Bad Request");
|
||||
return false;
|
||||
|
@ -25,13 +30,13 @@ function authenticateSocket(instance: PassyFireBackendProvider, ws: WebSocket, m
|
|||
for (const proxy of instance.proxies) {
|
||||
for (const username of Object.keys(proxy.userConfig)) {
|
||||
const currentToken = proxy.userConfig[username];
|
||||
|
||||
|
||||
if (token == currentToken) {
|
||||
state.connectionDetails = proxy;
|
||||
state.username = username;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (state.connectionDetails && state.username) {
|
||||
ws.send("AcceptResponse Bearer: true");
|
||||
|
@ -44,7 +49,11 @@ function authenticateSocket(instance: PassyFireBackendProvider, ws: WebSocket, m
|
|||
return false;
|
||||
}
|
||||
|
||||
export function requestHandler(instance: PassyFireBackendProvider, ws: WebSocket, req: FastifyRequest) {
|
||||
export function requestHandler(
|
||||
instance: PassyFireBackendProvider,
|
||||
ws: WebSocket,
|
||||
req: FastifyRequest,
|
||||
) {
|
||||
let state: "authentication" | "data" = "authentication";
|
||||
let socket: dgram.Socket | net.Socket | undefined;
|
||||
|
||||
|
@ -52,25 +61,31 @@ export function requestHandler(instance: PassyFireBackendProvider, ws: WebSocket
|
|||
let connectedClient: ConnectedClientExt = {};
|
||||
|
||||
ws.on("close", () => {
|
||||
instance.clients.splice(instance.clients.indexOf(connectedClient as ConnectedClientExt), 1);
|
||||
instance.clients.splice(
|
||||
instance.clients.indexOf(connectedClient as ConnectedClientExt),
|
||||
1,
|
||||
);
|
||||
});
|
||||
|
||||
ws.on("message", (rawData: ArrayBuffer) => {
|
||||
if (state == "authentication") {
|
||||
const data = rawData.toString();
|
||||
|
||||
|
||||
if (authenticateSocket(instance, ws, data, connectedClient)) {
|
||||
ws.send("AcceptResponse Bearer: true");
|
||||
|
||||
connectedClient.ip = req.ip;
|
||||
connectedClient.port = req.socket.remotePort ?? -1;
|
||||
|
||||
|
||||
instance.clients.push(connectedClient);
|
||||
|
||||
|
||||
if (connectedClient.connectionDetails.protocol == "tcp") {
|
||||
socket = new net.Socket();
|
||||
|
||||
socket.connect(connectedClient.connectionDetails.sourcePort, connectedClient.connectionDetails.sourceIP);
|
||||
socket.connect(
|
||||
connectedClient.connectionDetails.sourcePort,
|
||||
connectedClient.connectionDetails.sourceIP,
|
||||
);
|
||||
|
||||
socket.on("connect", () => {
|
||||
state = "data";
|
||||
|
@ -79,7 +94,7 @@ export function requestHandler(instance: PassyFireBackendProvider, ws: WebSocket
|
|||
ws.send("InitProxy: Connected");
|
||||
});
|
||||
|
||||
socket.on("data", (data) => {
|
||||
socket.on("data", data => {
|
||||
ws.send(data);
|
||||
});
|
||||
} else if (connectedClient.connectionDetails.protocol == "udp") {
|
||||
|
@ -90,7 +105,11 @@ export function requestHandler(instance: PassyFireBackendProvider, ws: WebSocket
|
|||
ws.send("InitProxy: Connected");
|
||||
|
||||
socket.on("message", (data, rinfo) => {
|
||||
if (rinfo.address != connectedClient.connectionDetails.sourceIP || rinfo.port != connectedClient.connectionDetails.sourcePort) return;
|
||||
if (
|
||||
rinfo.address != connectedClient.connectionDetails.sourceIP ||
|
||||
rinfo.port != connectedClient.connectionDetails.sourcePort
|
||||
)
|
||||
return;
|
||||
ws.send(data);
|
||||
});
|
||||
}
|
||||
|
@ -98,17 +117,24 @@ export function requestHandler(instance: PassyFireBackendProvider, ws: WebSocket
|
|||
} else if (state == "data") {
|
||||
if (socket instanceof dgram.Socket) {
|
||||
const array = new Uint8Array(rawData);
|
||||
|
||||
socket.send(array, connectedClient.connectionDetails.sourcePort, connectedClient.connectionDetails.sourceIP, (err) => {
|
||||
if (err) throw err;
|
||||
});
|
||||
|
||||
socket.send(
|
||||
array,
|
||||
connectedClient.connectionDetails.sourcePort,
|
||||
connectedClient.connectionDetails.sourceIP,
|
||||
err => {
|
||||
if (err) throw err;
|
||||
},
|
||||
);
|
||||
} else if (socket instanceof net.Socket) {
|
||||
const array = new Uint8Array(rawData);
|
||||
|
||||
socket.write(array);
|
||||
}
|
||||
} else {
|
||||
throw new Error(`Whooops, our WebSocket reached an unsupported state: '${state}'`);
|
||||
throw new Error(
|
||||
`Whooops, our WebSocket reached an unsupported state: '${state}'`,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue