|
| 1 | +import { EventEmitter } from "node:stream"; |
| 2 | +import { conn_regex, ip_regex, location_regex, index_regex } from "./regex"; |
| 3 | +import type { OutputHandler, Tunnel } from "./tunnel.js"; |
| 4 | +import type { Connection } from "./types.js"; |
| 5 | + |
| 6 | +export class ConnectionHandler { |
| 7 | + private connections: (Connection | undefined)[] = []; |
| 8 | + |
| 9 | + constructor(tunnel: Tunnel) { |
| 10 | + tunnel.addHandler(this.connected_handler.bind(this)); |
| 11 | + tunnel.addHandler(this.disconnected_handler.bind(this)); |
| 12 | + } |
| 13 | + |
| 14 | + private connected_handler: OutputHandler = (output, tunnel) => { |
| 15 | + // Registered tunnel connection connIndex=0 connection=4db5ec6e-4076-45c5-8752-745071bc2567 event=0 ip=198.41.200.193 location=tpe01 protocol=quic |
| 16 | + const conn_match = output.match(conn_regex); |
| 17 | + const ip_match = output.match(ip_regex); |
| 18 | + const location_match = output.match(location_regex); |
| 19 | + const index_match = output.match(index_regex); |
| 20 | + |
| 21 | + if (conn_match && ip_match && location_match && index_match) { |
| 22 | + const connection = { |
| 23 | + id: conn_match[1], |
| 24 | + ip: ip_match[1], |
| 25 | + location: location_match[1], |
| 26 | + }; |
| 27 | + this.connections[Number(index_match[1])] = connection; |
| 28 | + tunnel.emit("connected", connection); |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + private disconnected_handler: OutputHandler = (output, tunnel) => { |
| 33 | + // Connection terminated error="connection with edge closed" connIndex=1 |
| 34 | + const index_match = output.includes("terminated") ? output.match(index_regex) : null; |
| 35 | + if (index_match) { |
| 36 | + const index = Number(index_match[1]); |
| 37 | + if (this.connections[index]) { |
| 38 | + tunnel.emit("disconnected", this.connections[index]); |
| 39 | + this.connections[index] = undefined; |
| 40 | + } |
| 41 | + } |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +export class TryCloudflareHandler { |
| 46 | + constructor(tunnel: Tunnel) { |
| 47 | + tunnel.addHandler(this.url_handler.bind(this)); |
| 48 | + } |
| 49 | + |
| 50 | + private url_handler: OutputHandler = (output, tunnel) => { |
| 51 | + // https://xxxxxxxxxx.trycloudflare.com |
| 52 | + const url_match = output.match(/https:\/\/([a-z0-9-]+)\.trycloudflare\.com/); |
| 53 | + if (url_match) { |
| 54 | + tunnel.emit("url", url_match[0]); |
| 55 | + } |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +export interface ConfigHandlerEvents<T> { |
| 60 | + config: (config: { config: T; version: number }) => void; |
| 61 | + error: (error: Error) => void; |
| 62 | +} |
| 63 | + |
| 64 | +export interface TunnelConfig { |
| 65 | + ingress: Record<string, string>[]; |
| 66 | + warp_routing: { enabled: boolean }; |
| 67 | +} |
| 68 | + |
| 69 | +export class ConfigHandler<T = TunnelConfig> extends EventEmitter { |
| 70 | + constructor(tunnel: Tunnel) { |
| 71 | + super(); |
| 72 | + tunnel.addHandler(this.config_handler.bind(this)); |
| 73 | + } |
| 74 | + |
| 75 | + private config_handler: OutputHandler = (output, tunnel) => { |
| 76 | + // Updated to new configuration config="{\"ingress\":[{\"hostname\":\"host.mydomain.com\", \"service\":\"http://localhost:1234\"}, {\"service\":\"http_status:404\"}], \"warp-routing\":{\"enabled\":false}}" version=1 |
| 77 | + const config_match = output.match(/\bconfig="(.+?)" version=(\d+)/); |
| 78 | + |
| 79 | + if (config_match) { |
| 80 | + try { |
| 81 | + // Parse the escaped JSON string |
| 82 | + const config_str = config_match[1].replace(/\\"/g, '"'); |
| 83 | + const config: T = JSON.parse(config_str); |
| 84 | + const version = parseInt(config_match[2], 10); |
| 85 | + |
| 86 | + this.emit("config", { |
| 87 | + config, |
| 88 | + version, |
| 89 | + }); |
| 90 | + |
| 91 | + if ( |
| 92 | + config && |
| 93 | + typeof config === "object" && |
| 94 | + "ingress" in config && |
| 95 | + Array.isArray(config.ingress) |
| 96 | + ) { |
| 97 | + for (const ingress of config.ingress) { |
| 98 | + if ("hostname" in ingress) { |
| 99 | + tunnel.emit("url", ingress.hostname); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } catch (error) { |
| 104 | + this.emit("error", new Error(`Failed to parse config: ${error}`)); |
| 105 | + } |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + public on<E extends keyof ConfigHandlerEvents<T>>( |
| 110 | + event: E, |
| 111 | + listener: ConfigHandlerEvents<T>[E], |
| 112 | + ): this { |
| 113 | + return super.on(event, listener); |
| 114 | + } |
| 115 | + public once<E extends keyof ConfigHandlerEvents<T>>( |
| 116 | + event: E, |
| 117 | + listener: ConfigHandlerEvents<T>[E], |
| 118 | + ): this { |
| 119 | + return super.once(event, listener); |
| 120 | + } |
| 121 | + public off<E extends keyof ConfigHandlerEvents<T>>( |
| 122 | + event: E, |
| 123 | + listener: ConfigHandlerEvents<T>[E], |
| 124 | + ): this { |
| 125 | + return super.off(event, listener); |
| 126 | + } |
| 127 | + public emit<E extends keyof ConfigHandlerEvents<T>>( |
| 128 | + event: E, |
| 129 | + ...args: Parameters<ConfigHandlerEvents<T>[E]> |
| 130 | + ): boolean { |
| 131 | + return super.emit(event, ...args); |
| 132 | + } |
| 133 | +} |
0 commit comments