|
| 1 | +import * as websocket from "websocket"; |
| 2 | + |
| 3 | +import CloseCode from "./CloseCode"; |
| 4 | +import { maxPayload } from "./Config"; |
| 5 | + |
| 6 | +export default class Connection { |
| 7 | + private slavePeer?: websocket.connection; |
| 8 | + |
| 9 | + constructor(private masterPeer: websocket.connection) { |
| 10 | + this.masterPeer.on("message", this.onMasterMessage); |
| 11 | + this.masterPeer.on("close", this.onMasterClose); |
| 12 | + } |
| 13 | + |
| 14 | + public isHalfOpen(): boolean { |
| 15 | + return !this.slavePeer; |
| 16 | + } |
| 17 | + |
| 18 | + public connectPeer(slavePeer: websocket.connection): void { |
| 19 | + this.slavePeer = slavePeer; |
| 20 | + this.slavePeer.on("message", this.onSlaveMessage); |
| 21 | + this.slavePeer.on("close", this.onSlaveClose); |
| 22 | + this.sendHostHello(); |
| 23 | + } |
| 24 | + |
| 25 | + public close(code: CloseCode): void { |
| 26 | + this.masterPeer.close(code); |
| 27 | + if (this.slavePeer) { |
| 28 | + this.slavePeer.close(code); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private sendHostHello(): void { |
| 33 | + this.masterPeer.send( |
| 34 | + JSON.stringify({ |
| 35 | + maxPayload, |
| 36 | + relayEvent: "connection", |
| 37 | + }) |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + private forwardMessage( |
| 42 | + destination: websocket.connection | undefined, |
| 43 | + data: websocket.Message |
| 44 | + ) { |
| 45 | + let payload; |
| 46 | + if (data.type === "utf8") { |
| 47 | + payload = data.utf8Data!; |
| 48 | + } else if (data.type === "binary") { |
| 49 | + payload = data.binaryData!; |
| 50 | + } else { |
| 51 | + console.error(`Invalid payload type: ${(data as any).type}`); |
| 52 | + this.close(CloseCode.PolicyViolation); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (destination) { |
| 57 | + destination.send(payload); |
| 58 | + } else { |
| 59 | + this.close(CloseCode.PolicyViolation); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private onMasterMessage = (data: websocket.Message) => { |
| 64 | + this.forwardMessage(this.slavePeer, data); |
| 65 | + }; |
| 66 | + |
| 67 | + private onSlaveMessage = (data: websocket.Message) => |
| 68 | + this.forwardMessage(this.masterPeer, data); |
| 69 | + |
| 70 | + private forwardClose( |
| 71 | + destination: websocket.connection | undefined, |
| 72 | + code: number, |
| 73 | + message: string |
| 74 | + ) { |
| 75 | + if (destination) { |
| 76 | + const skipCloseFrame = |
| 77 | + code === websocket.connection.CLOSE_REASON_ABNORMAL; |
| 78 | + if (skipCloseFrame) { |
| 79 | + destination.drop(code, message, true); |
| 80 | + } else { |
| 81 | + destination.close(code, message); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private onMasterClose = (code: number, message: string) => { |
| 87 | + this.forwardClose(this.slavePeer, code, message); |
| 88 | + }; |
| 89 | + |
| 90 | + private onSlaveClose = (code: number, message: string) => { |
| 91 | + this.forwardClose(this.masterPeer, code, message); |
| 92 | + }; |
| 93 | +} |
0 commit comments