| 133 | } |
| 134 | |
| 135 | async addOnion(port: number, host?: string, privateKey?: string | null): Promise<OnionResult> { |
| 136 | const key = privateKey ?? 'NEW:BEST' |
| 137 | const portSpec = host !== undefined ? `${port},${host}:${port}` : `${port}` |
| 138 | const response = await this.sendCommand(`ADD_ONION ${key} Port=${portSpec}`) |
| 139 | |
| 140 | const result: OnionResult = {} |
| 141 | for (const line of response.split('\r\n')) { |
| 142 | const m = line.match(/^250[-\s](\w+)=(.+)$/) |
| 143 | if (m) { (result as Record<string, string>)[m[1]] = m[2] } |
| 144 | } |
| 145 | if (result.ServiceID) { result.ServiceID += '.onion' } |
| 146 | if (!result.PrivateKey && privateKey) { result.PrivateKey = privateKey } |
| 147 | return result |
| 148 | } |
| 149 | |
| 150 | async quit(): Promise<void> { |
| 151 | await this.sendCommand('QUIT').catch(() => undefined) |