Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve connectd to try non-tor connection first and filter duplicates #4731

Merged
merged 3 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ struct daemon {
struct addrinfo *proxyaddr;

/* They can tell us we must use proxy even for non-Tor addresses. */
bool use_proxy_always;
bool always_use_proxy;

/* There are DNS seeds we can use to look up node addresses as a last
* resort, but doing so leaks our address so can be disabled. */
Expand Down Expand Up @@ -214,8 +214,9 @@ static bool broken_resolver(struct daemon *daemon)
const char *hostname = "nxdomain-test.doesntexist";
int err;

/* If they told us to never do DNS queries, don't even do this one and also not if we just say that we don't */
if (!daemon->use_dns || daemon->use_proxy_always) {
/* If they told us to never do DNS queries, don't even do this one and
* also not if we just say that we don't */
if (!daemon->use_dns || daemon->always_use_proxy) {
daemon->broken_resolver_response = NULL;
return false;
}
Expand Down Expand Up @@ -820,7 +821,7 @@ static struct io_plan *conn_proxy_init(struct io_conn *conn,
static void try_connect_one_addr(struct connecting *connect)
{
int fd, af;
bool use_proxy = connect->daemon->use_proxy_always;
bool use_proxy = connect->daemon->always_use_proxy;
const struct wireaddr_internal *addr = &connect->addrs[connect->addrnum];
struct io_conn *conn;

Expand Down Expand Up @@ -1311,7 +1312,7 @@ static struct io_plan *connect_init(struct io_conn *conn,
&daemon->id,
&proposed_wireaddr,
&proposed_listen_announce,
&proxyaddr, &daemon->use_proxy_always,
&proxyaddr, &daemon->always_use_proxy,
&daemon->dev_allow_localhost, &daemon->use_dns,
&tor_password,
&daemon->use_v3_autotor,
Expand Down Expand Up @@ -1459,9 +1460,18 @@ static void add_seed_addrs(struct wireaddr_internal **addrs,
}
}

static bool wireaddr_int_equals_wireaddr(struct wireaddr_internal *addr_a,
struct wireaddr *addr_b)
{
if (!addr_a || !addr_b)
return false;
return wireaddr_eq(&addr_a->u.wireaddr, addr_b);
}

/*~ This asks gossipd for any addresses advertized by the node. */
static void add_gossip_addrs(struct wireaddr_internal **addrs,
const struct node_id *id)
const struct node_id *id,
struct wireaddr_internal *addrhint)
{
u8 *msg;
struct wireaddr *normal_addrs;
Expand All @@ -1483,6 +1493,24 @@ static void add_gossip_addrs(struct wireaddr_internal **addrs,

/* Wrap each one in a wireaddr_internal and add to addrs. */
for (size_t i = 0; i < tal_count(normal_addrs); i++) {
/* add TOR addresses in a second loop */
m-schmoock marked this conversation as resolved.
Show resolved Hide resolved
if (normal_addrs[i].type == ADDR_TYPE_TOR_V2 ||
normal_addrs[i].type == ADDR_TYPE_TOR_V3)
continue;
if (wireaddr_int_equals_wireaddr(addrhint, &normal_addrs[i]))
continue;
struct wireaddr_internal addr;
addr.itype = ADDR_INTERNAL_WIREADDR;
addr.u.wireaddr = normal_addrs[i];
tal_arr_expand(addrs, addr);
}
/* so connectd prefers direct connections if possible. */
for (size_t i = 0; i < tal_count(normal_addrs); i++) {
if (normal_addrs[i].type != ADDR_TYPE_TOR_V2 &&
normal_addrs[i].type != ADDR_TYPE_TOR_V3)
continue;
if (wireaddr_int_equals_wireaddr(addrhint, &normal_addrs[i]))
continue;
struct wireaddr_internal addr;
addr.itype = ADDR_INTERNAL_WIREADDR;
addr.u.wireaddr = normal_addrs[i];
Expand All @@ -1500,7 +1528,7 @@ static void try_connect_peer(struct daemon *daemon,
struct wireaddr_internal *addrhint)
{
struct wireaddr_internal *addrs;
bool use_proxy = daemon->use_proxy_always;
bool use_proxy = daemon->always_use_proxy;
struct connecting *connect;

/* Already done? May happen with timer. */
Expand All @@ -1524,10 +1552,11 @@ static void try_connect_peer(struct daemon *daemon,
addrs = tal_arr(tmpctx, struct wireaddr_internal, 0);

/* They can supply an optional address for the connect RPC */
/* We add this first so its tried first by connectd */
if (addrhint)
tal_arr_expand(&addrs, *addrhint);

add_gossip_addrs(&addrs, id);
add_gossip_addrs(&addrs, id, addrhint);

if (tal_count(addrs) == 0) {
/* Don't resolve via DNS seed if we're supposed to use proxy. */
Expand Down
2 changes: 1 addition & 1 deletion doc/PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ simple JSON object containing the options:
"port": 9050
},
"torv3-enabled": true,
"use_proxy_always": false
"always_use_proxy": false
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions lightningd/connect_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static struct command_result *json_connect(struct command *cmd,
}
addr = tal(cmd, struct wireaddr_internal);
if (!parse_wireaddr_internal(name, addr, *port, false,
!cmd->ld->use_proxy_always
!cmd->ld->always_use_proxy
&& !cmd->ld->pure_tor_setup,
true, deprecated_apis,
&err_msg)) {
Expand Down Expand Up @@ -390,7 +390,7 @@ int connectd_init(struct lightningd *ld)
&ld->id,
wireaddrs,
listen_announce,
ld->proxyaddr, ld->use_proxy_always || ld->pure_tor_setup,
ld->proxyaddr, ld->always_use_proxy || ld->pure_tor_setup,
IFDEV(ld->dev_allow_localhost, false), ld->config.use_dns,
ld->tor_service_password ? ld->tor_service_password : "",
ld->config.use_v3_autotor,
Expand Down
2 changes: 1 addition & 1 deletion lightningd/lightningd.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ static struct lightningd *new_lightningd(const tal_t *ctx)
ld->topology = new_topology(ld, ld->log);
ld->daemon_parent_fd = -1;
ld->proxyaddr = NULL;
ld->use_proxy_always = false;
ld->always_use_proxy = false;
ld->pure_tor_setup = false;
ld->tor_service_password = NULL;

Expand Down
2 changes: 1 addition & 1 deletion lightningd/lightningd.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ struct lightningd {

/* tor support */
struct wireaddr *proxyaddr;
bool use_proxy_always;
bool always_use_proxy;
char *tor_service_password;
bool pure_tor_setup;

Expand Down
10 changes: 5 additions & 5 deletions lightningd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ static char *opt_add_addr_withtype(const char *arg,
tal_arr_expand(&ld->proposed_listen_announce, ala);
if (!parse_wireaddr_internal(arg, &wi,
ld->portnum,
wildcard_ok, !ld->use_proxy_always, false,
wildcard_ok, !ld->always_use_proxy, false,
deprecated_apis, &err_msg)) {
return tal_fmt(NULL, "Unable to parse address '%s': %s", arg, err_msg);
}
Expand Down Expand Up @@ -406,7 +406,7 @@ static char *opt_add_proxy_addr(const char *arg, struct lightningd *ld)
ld->proxyaddr = tal_arr(ld, struct wireaddr, 1);

if (!parse_wireaddr(arg, ld->proxyaddr, 9050,
ld->use_proxy_always ? &needed_dns : NULL,
ld->always_use_proxy ? &needed_dns : NULL,
NULL)) {
return tal_fmt(NULL, "Unable to parse Tor proxy address '%s' %s",
arg, needed_dns ? " (needed dns)" : "");
Expand Down Expand Up @@ -787,7 +787,7 @@ static void check_config(struct lightningd *ld)
if (ld->config.anchor_confirms == 0)
fatal("anchor-confirms must be greater than zero");

if (ld->use_proxy_always && !ld->proxyaddr)
if (ld->always_use_proxy && !ld->proxyaddr)
fatal("--always-use-proxy needs --proxy");

if (ld->daemon_parent_fd != -1 && !ld->logfile)
Expand Down Expand Up @@ -922,7 +922,7 @@ static void register_opts(struct lightningd *ld)
/* Early, as it suppresses DNS lookups from cmdline too. */
opt_register_early_arg("--always-use-proxy",
opt_set_bool_arg, opt_show_bool,
&ld->use_proxy_always, "Use the proxy always");
&ld->always_use_proxy, "Use the proxy always");

/* This immediately makes is a daemon. */
opt_register_early_noarg("--daemon", opt_start_daemon, ld,
Expand Down Expand Up @@ -1267,7 +1267,7 @@ void handle_opts(struct lightningd *ld, int argc, char *argv[])
if (argc != 1)
errx(1, "no arguments accepted");

/* We keep a separate variable rather than overriding use_proxy_always,
/* We keep a separate variable rather than overriding always_use_proxy,
* so listconfigs shows the correct thing. */
if (tal_count(ld->proposed_wireaddr) != 0
&& all_tor_addresses(ld->proposed_wireaddr)) {
Expand Down
5 changes: 4 additions & 1 deletion lightningd/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,10 @@ plugin_populate_init_request(struct plugin *plugin, struct jsonrpc_request *req)
if (ld->proxyaddr) {
json_add_address(req->stream, "proxy", ld->proxyaddr);
json_add_bool(req->stream, "torv3-enabled", ld->config.use_v3_autotor);
json_add_bool(req->stream, "use_proxy_always", ld->use_proxy_always);
m-schmoock marked this conversation as resolved.
Show resolved Hide resolved
json_add_bool(req->stream, "always_use_proxy", ld->always_use_proxy);
if (deprecated_apis)
json_add_bool(req->stream, "use_proxy_always",
ld->always_use_proxy);
}
json_object_start(req->stream, "feature_set");
for (enum feature_place fp = 0; fp < NUM_FEATURE_PLACE; fp++) {
Expand Down