Skip to content

Commit

Permalink
feat(p2p): add tor config option
Browse files Browse the repository at this point in the history
  • Loading branch information
engwarrior committed Feb 13, 2020
1 parent c037f66 commit f5e76f2
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bin/xud
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ const { argv } = require('yargs')
type: 'number',
alias: 'p',
},
'p2p.tor': {
describe: 'Allow connections to tor nodes',
type: 'boolean',
default: undefined,
alias: 't',
},
'raiden.disable': {
describe: 'Disable raiden integration',
type: 'boolean',
Expand Down
1 change: 1 addition & 0 deletions lib/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Config {
this.p2p = {
listen: true,
discover: true,
tor: false,
discoverminutes: 60 * 12, // 12 hours
detectexternalip: false,
port: this.getDefaultP2pPort(),
Expand Down
1 change: 1 addition & 0 deletions lib/grpc/getGrpcError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const getGrpcError = (err: any) => {
case serviceErrorCodes.INVALID_ARGUMENT:
case p2pErrorCodes.ATTEMPTED_CONNECTION_TO_SELF:
case p2pErrorCodes.UNEXPECTED_NODE_PUB_KEY:
case p2pErrorCodes.NODE_TOR_ADDRESS:
case orderErrorCodes.MIN_QUANTITY_VIOLATED:
case orderErrorCodes.QUANTITY_DOES_NOT_MATCH:
case orderErrorCodes.EXCEEDING_LIMIT:
Expand Down
5 changes: 5 additions & 0 deletions lib/p2p/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ class Pool extends EventEmitter {
throw errors.ATTEMPTED_CONNECTION_TO_SELF;
}

// check if we allow connections to tor addresses
if (!this.config.tor && address.host.indexOf('.onion') !== -1) {
throw errors.NODE_TOR_ADDRESS(nodePubKey, address);
}

if (this.nodes.isBanned(nodePubKey)) {
throw errors.NODE_IS_BANNED(nodePubKey);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/p2p/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const errorCodes = {
FRAMER_INCOMPATIBLE_MSG_ORIGIN_NETWORK: codesPrefix.concat('.22'),
FRAMER_INVALID_MSG_LENGTH: codesPrefix.concat('.23'),
POOL_CLOSED: codesPrefix.concat('.24'),
NODE_TOR_ADDRESS: codesPrefix.concat('.25'),
};

const errors = {
Expand All @@ -41,6 +42,10 @@ const errors = {
message: `node (${nodePubKey}) is not connected`,
code: errorCodes.NOT_CONNECTED,
}),
NODE_TOR_ADDRESS: (nodePubKey: string, address: Address) => ({
message: `can't connect to node (${nodePubKey}) at tor address ${addressUtils.toString(address)} because tor is disabled`,
code: errorCodes.NODE_TOR_ADDRESS,
}),
UNEXPECTED_NODE_PUB_KEY: (nodePubKey: string, expectedNodePubKey: string, address: string) => ({
message: `node at ${address} sent pub key ${nodePubKey}, expected ${expectedNodePubKey}`,
code: errorCodes.UNEXPECTED_NODE_PUB_KEY,
Expand Down
2 changes: 2 additions & 0 deletions lib/p2p/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export type PoolConfig = {
/** Which port to listen on. If 0, a random unused port will be used. */
port: number;

/** Whether to allow connections to tor nodes. */
tor: boolean;
/**
* An array of IP addresses or host names which can be used to connect to this server.
* It will be advertised with peers for them to try to connect to the server in the future.
Expand Down
8 changes: 8 additions & 0 deletions test/integration/Pool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import Logger, { Level } from '../../lib/Logger';
import NodeKey from '../../lib/nodekey/NodeKey';
import Peer from '../../lib/p2p/Peer';
import Pool from '../../lib/p2p/Pool';
import errors from '../../lib/p2p/errors';
import { Address } from '../../lib/p2p/types';
import addressUtils from '../../lib/utils/addressUtils';

chai.use(chaiAsPromised);

Expand Down Expand Up @@ -88,6 +90,12 @@ describe('P2P Pool Tests', async () => {
expect(nodeReputationPromise.banned).to.be.true;
});

it('should throw error when connecting to tor node with tor disabled', async () => {
const address = addressUtils.fromString('3g2upl4pq6kufc4m.onion');
const addPromise = pool.addOutbound(address, nodeKeyOne.pubKey, false, false);
await expect(addPromise).to.be.rejectedWith(errors.NODE_TOR_ADDRESS(nodeKeyOne.pubKey, address).message);
});

it('should unban a peer', async () => {
const unbanPromise = pool.unbanNode(nodeKeyOne.pubKey, false);
expect(unbanPromise).to.be.fulfilled;
Expand Down

0 comments on commit f5e76f2

Please sign in to comment.