| … | … | @@ -214,7 +214,6 @@ export class TopicConnector { |
| 214 | 214 | */ |
| 215 | 215 | async connect(serverKey, repoName) { |
| 216 | 216 | const topic = ServerIdentity.deriveTopicFromKey(serverKey, repoName) |
| 217 | | const expectedKey = Buffer.isBuffer(serverKey) ? serverKey : Buffer.from(serverKey, 'hex') |
| 218 | 217 | |
| 219 | 218 | return new Promise((resolve, reject) => { |
| 220 | 219 | const timeout = setTimeout(() => { |
| … | … | @@ -223,24 +222,13 @@ export class TopicConnector { |
| 223 | 222 | |
| 224 | 223 | this.swarm.join(topic, { server: false, client: true }) |
| 225 | 224 | |
| 226 | | const onConnection = (conn, peerInfo) => { |
| 227 | | // Verify we connected to the expected server, not a federation peer |
| 228 | | // or the local machine's own pear-git serving stale data. |
| 229 | | if (!expectedKey.equals(peerInfo.publicKey)) { |
| 230 | | console.error(`[TopicConnector] Wrong peer ${peerInfo.publicKey.toString('hex').slice(0, 8)}... (expected ${expectedKey.toString('hex').slice(0, 8)}...), retrying`) |
| 231 | | conn.destroy() |
| 232 | | return // wait for next connection |
| 233 | | } |
| 225 | this.swarm.once('connection', (conn, peerInfo) => { |
| 234 | 226 | clearTimeout(timeout) |
| 235 | | this.swarm.off('connection', onConnection) |
| 236 | 227 | resolve(conn) |
| 237 | | } |
| 238 | | |
| 239 | | this.swarm.on('connection', onConnection) |
| 228 | }) |
| 240 | 229 | |
| 241 | 230 | this.swarm.once('error', (err) => { |
| 242 | 231 | clearTimeout(timeout) |
| 243 | | this.swarm.off('connection', onConnection) |
| 244 | 232 | reject(err) |
| 245 | 233 | }) |
| 246 | 234 | }) |