|
| 1 | +export type ProxyStats = { |
| 2 | + totalConnections: number; |
| 3 | + activeConnections: number; |
| 4 | + totalBytesReceived: number; |
| 5 | + totalBytesSent: number; |
| 6 | + totalCommandsReceived: number; |
| 7 | + totalCommandsSent: number; |
| 8 | +}; |
| 9 | + |
| 10 | +export type SendResult = { |
| 11 | + success: boolean; |
| 12 | + connectionId: string; |
| 13 | + error?: string; |
| 14 | +}; |
| 15 | + |
| 16 | +export type ProxyConfig = { |
| 17 | + listenPort: number; |
| 18 | + listenHost?: string; |
| 19 | + targetHost: string; |
| 20 | + targetPort: number; |
| 21 | + timeout?: number; |
| 22 | + enableLogging?: boolean; |
| 23 | +}; |
| 24 | + |
| 25 | +export default class ProxyController { |
| 26 | + constructor(private url: string) { } |
| 27 | + |
| 28 | + private fetchJson(path: string, options?: RequestInit): Promise<unknown> { |
| 29 | + return fetch(`${this.url}${path}`, options).then(res => res.json()); |
| 30 | + } |
| 31 | + |
| 32 | + getStats(): Promise<Record<string, ProxyStats>> { |
| 33 | + return this.fetchJson('/stats') as Promise<Record<string, ProxyStats>>; |
| 34 | + } |
| 35 | + |
| 36 | + getConnections(): Promise<Record<string, readonly string[]>> { |
| 37 | + return this.fetchJson('/connections') as Promise<Record<string, readonly string[]>>; |
| 38 | + } |
| 39 | + |
| 40 | + getNodes(): Promise<{ ids: string[] }> { |
| 41 | + return this.fetchJson('/nodes') as Promise<{ ids: string[] }>; |
| 42 | + } |
| 43 | + |
| 44 | + createNode(config: Partial<ProxyConfig>): Promise<{ success: boolean; cfg: ProxyConfig }> { |
| 45 | + return this.fetchJson('/nodes', { |
| 46 | + method: 'POST', |
| 47 | + headers: { 'Content-Type': 'application/json' }, |
| 48 | + body: JSON.stringify(config) |
| 49 | + }) as Promise<{ success: boolean; cfg: ProxyConfig }>; |
| 50 | + } |
| 51 | + |
| 52 | + deleteNode(nodeId: string): Promise<{ success: boolean }> { |
| 53 | + return this.fetchJson(`/nodes/${nodeId}`, { |
| 54 | + method: 'DELETE' |
| 55 | + }) as Promise<{ success: boolean }>; |
| 56 | + } |
| 57 | + |
| 58 | + sendToClient(connectionId: string, data: string, encoding: 'base64' | 'raw' = 'base64'): Promise<SendResult> { |
| 59 | + return this.fetchJson(`/send-to-client/${connectionId}?encoding=${encoding}`, { |
| 60 | + method: 'POST', |
| 61 | + headers: { 'Content-Type': 'text/plain' }, |
| 62 | + body: data |
| 63 | + }) as Promise<SendResult>; |
| 64 | + } |
| 65 | + |
| 66 | + sendToClients(connectionIds: string[], data: string, encoding: 'base64' | 'raw' = 'base64'): Promise<{ results: SendResult[] }> { |
| 67 | + return this.fetchJson(`/send-to-clients?connectionIds=${connectionIds.join(',')}&encoding=${encoding}`, { |
| 68 | + method: 'POST', |
| 69 | + headers: { 'Content-Type': 'text/plain' }, |
| 70 | + body: data |
| 71 | + }) as Promise<{ results: SendResult[] }>; |
| 72 | + } |
| 73 | + |
| 74 | + sendToAllClients(data: string, encoding: 'base64' | 'raw' = 'base64'): Promise<{ results: SendResult[] }> { |
| 75 | + return this.fetchJson(`/send-to-all-clients?encoding=${encoding}`, { |
| 76 | + method: 'POST', |
| 77 | + headers: { 'Content-Type': 'text/plain' }, |
| 78 | + body: data |
| 79 | + }) as Promise<{ results: SendResult[] }>; |
| 80 | + } |
| 81 | + |
| 82 | + closeConnection(connectionId: string): Promise<{ success: boolean; connectionId: string }> { |
| 83 | + return this.fetchJson(`/connections/${connectionId}`, { |
| 84 | + method: 'DELETE' |
| 85 | + }) as Promise<{ success: boolean; connectionId: string }>; |
| 86 | + } |
| 87 | + |
| 88 | + createScenario(responses: string[], encoding: 'base64' | 'raw' = 'base64'): Promise<{ success: boolean; totalResponses: number }> { |
| 89 | + return this.fetchJson('/scenarios', { |
| 90 | + method: 'POST', |
| 91 | + headers: { 'Content-Type': 'application/json' }, |
| 92 | + body: JSON.stringify({ responses, encoding }) |
| 93 | + }) as Promise<{ success: boolean; totalResponses: number }>; |
| 94 | + } |
| 95 | + |
| 96 | + addInterceptor(name: string, match: string, response: string, encoding: 'base64' | 'raw' = 'base64'): Promise<{ success: boolean; name: string }> { |
| 97 | + return this.fetchJson('/interceptors', { |
| 98 | + method: 'POST', |
| 99 | + headers: { 'Content-Type': 'application/json' }, |
| 100 | + body: JSON.stringify({ name, match, response, encoding }) |
| 101 | + }) as Promise<{ success: boolean; name: string }>; |
| 102 | + } |
| 103 | +} |
0 commit comments