Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket support #17

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 72 additions & 5 deletions message-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,83 @@ import hostnameAssigned from "./src/message-handlers/hostname-assigned.js";
import forwardedRequest from "./src/message-handlers/forwarded-request.js";
import hostnameAlreadyTaken from "./src/message-handlers/hostname-already-taken.js";
import invalidSubscription from "./src/message-handlers/invalid-subscription.js";
import HostipWebSocket from "./src/websocket/host-ip-websocket.js";

/**
* Websocket message handlers for different message types
* Like app.ts for express, but with handlers for different message types instead of URLs
*/
const messageHandlers = {
hostnameAssigned,
forwardedRequest,
hostnameAlreadyTaken,
invalidSubscription
hostnameAssigned,
forwardedRequest,
hostnameAlreadyTaken,
invalidSubscription,
WebSocketCloseMessage,
WebSocketOpenMessage,
WebSocketHostMessage,
WebSocketClientMessage,
}
export { messageHandlers }

export { messageHandlers };
function WebSocketCloseMessage(message, websocket, options) {
if (!websocket.sockets) return console.log('WebSocketCloseMessage impossible')

const { socketId, code, data } = message
const found = websocket.sockets.get(socketId)
if (found.readyState === 1) found?.close(code === 1006 ? 1001 : code, data)
}

function WebSocketOpenMessage(forwardedRequestMessage, websocket, options) {
const port = options.port
const { socketId, url, headers } = forwardedRequestMessage
delete headers['sec-websocket-key']
delete headers['sec-websocket-extensions']

// Create end to end tunnel
if (!websocket.sockets) websocket.sockets = new Map()
const datatunnel = new HostipWebSocket('ws://127.0.0.1:' + port + url, {
// headers: { cookie: headers.cookie },
})
websocket.sockets.set(socketId, datatunnel)

// Forward messages from client to server
// console.log('WebSocketOpenMessage.ready', datatunnel.readyState)
datatunnel.on('open', () => {
// console.log('WebSocketOpenMessage.open', datatunnel.readyState)
})
datatunnel.on('message', (data) => {
const message = { type: 'WebSocketClientMessage', socketId, data }
websocket.sendMessage(message)
})
datatunnel.on('close', () => {
const close = { type: 'WebSocketCloseMessage', socketId }
websocket.sendMessage(close)
})
datatunnel.on('error', (e) => {
console.log('datatunnel.error', socketId, e)
})
}

async function WebSocketHostMessage(
forwardedRequestMessage,
websocket,
options
) {
// console.log('WebSocketHostMessage')
if (!websocket.sockets) {
// console.log('no sockets, wait')
await new Promise((resolve) => setTimeout(resolve, 1000))
}

if (!websocket.sockets) return // console.log('bummer')

const { socketId, data } = forwardedRequestMessage
const datatunnel = websocket.sockets.get(socketId)
// console.log('datatunnel', datatunnel.readyState)
if (!datatunnel) return // console.log('no datatunnel')
datatunnel.send(data)
}

function WebSocketClientMessage() {
console.log('WebSocketClientMessage impossible?')
}
Loading