websocket forward proxy server

i am trying to set up a custom WebSocket server. This server will act as an intermediary, forwarding all messages it receives to another WebSocket server
async function websocket(url, final) {
let resp = await fetch(url, {
headers: {
Upgrade: 'websocket',
"NDCDEVICEID": await getDeviceId(),
"NDC-MSG-SIG": await getSignature(final)
},
});

console.log(resp);

let ws = resp.webSocket;
if (!ws) {
throw new Error("server didn't accept WebSocket");
}

ws.accept();


}

export default {
async fetch(request, env, ctx) {
const upgradeHeader = request.headers.get('Upgrade');
if (upgradeHeader && upgradeHeader.toLowerCase() === 'websocket') {
const [client, server] = Object.values(new WebSocketPair());
const device_id = await getDeviceId();
const timestamp = new Date().getTime();
const final = `${device_id}|${timestamp}`;
const url = `http://ws1.aminoapps.com/?signbody=${final.replace('|', '%7C')}`;

console.log(url);

websocket(url, final);

server.accept();
server.addEventListener('message', async (event) => {

console.log('Client:', event.data);

server.send(`Echo: ${event.data}`);
});

return new Response(null, {
status: 101,
webSocket: server
});
}
async function websocket(url, final) {
let resp = await fetch(url, {
headers: {
Upgrade: 'websocket',
"NDCDEVICEID": await getDeviceId(),
"NDC-MSG-SIG": await getSignature(final)
},
});

console.log(resp);

let ws = resp.webSocket;
if (!ws) {
throw new Error("server didn't accept WebSocket");
}

ws.accept();


}

export default {
async fetch(request, env, ctx) {
const upgradeHeader = request.headers.get('Upgrade');
if (upgradeHeader && upgradeHeader.toLowerCase() === 'websocket') {
const [client, server] = Object.values(new WebSocketPair());
const device_id = await getDeviceId();
const timestamp = new Date().getTime();
const final = `${device_id}|${timestamp}`;
const url = `http://ws1.aminoapps.com/?signbody=${final.replace('|', '%7C')}`;

console.log(url);

websocket(url, final);

server.accept();
server.addEventListener('message', async (event) => {

console.log('Client:', event.data);

server.send(`Echo: ${event.data}`);
});

return new Response(null, {
status: 101,
webSocket: server
});
}
i am still new to cloudflare worker so let me know where i am wrong
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server