Skip to content

Commit 9a7e5f4

Browse files
committed
net: don't extra bind for Tor if binds are restricted
If only `-bind=addr:port` is given (without `-bind=...=onion`) then we would bind to `addr:port` _and_ to `127.0.0.1:8334` in addition which may be unexpected, assuming the semantic of `-bind=addr:port` is "bind _only_ to `addr:port`". Change the above to not do the additional bind: if only `-bind=addr:port` is given (without `-bind=...=onion`) then bind to `addr:port` (only). If we are creating a Tor hidden service then use `addr:port` as target (same behavior as before bitcoin/bitcoin#19991). This allows disabling binding on the onion port. Fixes bitcoin/bitcoin#22726
1 parent 2f6dca4 commit 9a7e5f4

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/init.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
18901890
CService onion_service_target;
18911891
if (!connOptions.onion_binds.empty()) {
18921892
onion_service_target = connOptions.onion_binds.front();
1893+
} else if (!connOptions.vBinds.empty()) {
1894+
onion_service_target = connOptions.vBinds.front();
18931895
} else {
18941896
onion_service_target = DefaultOnionServiceTarget();
18951897
connOptions.onion_binds.push_back(onion_service_target);

test/functional/feature_bind_extra.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def set_test_params(self):
2727
# Avoid any -bind= on the command line. Force the framework to avoid
2828
# adding -bind=127.0.0.1.
2929
self.bind_to_localhost_only = False
30-
self.num_nodes = 2
30+
self.num_nodes = 3
3131

3232
def skip_test_if_missing_module(self):
3333
# Due to OS-specific network stats queries, we only run on Linux.
@@ -60,14 +60,21 @@ def setup_network(self):
6060
)
6161
port += 2
6262

63+
# Node2, no -bind=...=onion, thus no extra port for Tor target.
64+
self.expected.append(
65+
[
66+
[f"-bind=127.0.0.1:{port}"],
67+
[(loopback_ipv4, port)]
68+
],
69+
)
70+
port += 1
71+
6372
self.extra_args = list(map(lambda e: e[0], self.expected))
64-
self.add_nodes(self.num_nodes, self.extra_args)
65-
# Don't start the nodes, as some of them would collide trying to bind on the same port.
73+
self.setup_nodes()
6674

6775
def run_test(self):
68-
for i in range(len(self.expected)):
69-
self.log.info(f"Starting node {i} with {self.expected[i][0]}")
70-
self.start_node(i)
76+
for i, (args, expected_services) in enumerate(self.expected):
77+
self.log.info(f"Checking listening ports of node {i} with {args}")
7178
pid = self.nodes[i].process.pid
7279
binds = set(get_bind_addrs(pid))
7380
# Remove IPv6 addresses because on some CI environments "::1" is not configured
@@ -78,9 +85,7 @@ def run_test(self):
7885
binds = set(filter(lambda e: len(e[0]) != ipv6_addr_len_bytes, binds))
7986
# Remove RPC ports. They are not relevant for this test.
8087
binds = set(filter(lambda e: e[1] != rpc_port(i), binds))
81-
assert_equal(binds, set(self.expected[i][1]))
82-
self.stop_node(i)
83-
self.log.info(f"Stopped node {i}")
88+
assert_equal(binds, set(expected_services))
8489

8590
if __name__ == '__main__':
8691
BindExtraTest().main()

0 commit comments

Comments
 (0)