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

Enable the user to select the autotor extern onion port #3170

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 27 additions & 4 deletions common/wireaddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ void towire_wireaddr_internal(u8 **pptr, const struct wireaddr_internal *addr)
sizeof(addr->u.sockname));
return;
case ADDR_INTERNAL_AUTOTOR:
towire_wireaddr(pptr, &addr->u.torservice);
towire_wireaddr(pptr, &addr->u.torservice.torservice_address);
towire_u16(pptr, addr->u.torservice.port);
return;
case ADDR_INTERNAL_ALLPROTO:
towire_u16(pptr, addr->u.port);
Expand Down Expand Up @@ -113,7 +114,9 @@ bool fromwire_wireaddr_internal(const u8 **cursor, size_t *max,
addr->u.port = fromwire_u16(cursor, max);
return *cursor != NULL;
case ADDR_INTERNAL_AUTOTOR:
return fromwire_wireaddr(cursor, max, &addr->u.torservice);
fromwire_wireaddr(cursor, max, &addr->u.torservice.torservice_address);
addr->u.torservice.port = fromwire_u16(cursor, max);
return *cursor != NULL;
case ADDR_INTERNAL_WIREADDR:
return fromwire_wireaddr(cursor, max, &addr->u.wireaddr);
case ADDR_INTERNAL_FORPROXY:
Expand Down Expand Up @@ -204,7 +207,7 @@ char *fmt_wireaddr_internal(const tal_t *ctx,
a->u.unresolved.name, a->u.unresolved.port);
case ADDR_INTERNAL_AUTOTOR:
return tal_fmt(ctx, "autotor:%s",
fmt_wireaddr(tmpctx, &a->u.torservice));
fmt_wireaddr(tmpctx, &a->u.torservice.torservice_address));
}
abort();
}
Expand Down Expand Up @@ -448,12 +451,32 @@ bool parse_wireaddr_internal(const char *arg, struct wireaddr_internal *addr,
return true;
}

/* 'statictor:' is a special prefix meaning talk to Tor to create
* an onion address from a blob. */
if (strstarts(arg, "autotor:") &&
(strstr(arg, ":torport:"))) {
addr->itype = ADDR_INTERNAL_AUTOTOR;
char *temp = tal_fmt(tmpctx , "%.6s", strstr(arg, ":torport:") + strlen(":torport:"));
addr->u.torservice.port = atoi(temp);
if (strlen(temp) == 0) {
if (err_msg)
*err_msg = "port string too short";
return false;
}
temp = tal_fmt(tmpctx, "%s", arg + strlen("autotor:"));
*(strstr(temp, ":torport:")) = '\0';
return parse_wireaddr(temp,
&addr->u.torservice.torservice_address, 9051,
dns_ok ? NULL : &needed_dns,
err_msg);
}
/* 'autotor:' is a special prefix meaning talk to Tor to create
* an onion address. */
if (strstarts(arg, "autotor:")) {
addr->itype = ADDR_INTERNAL_AUTOTOR;
addr->u.torservice.port = DEFAULT_PORT;
return parse_wireaddr(arg + strlen("autotor:"),
&addr->u.torservice, 9051,
&addr->u.torservice.torservice_address, 9051,
dns_ok ? NULL : &needed_dns,
err_msg);
}
Expand Down
5 changes: 4 additions & 1 deletion common/wireaddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ struct wireaddr_internal {
/* ADDR_INTERNAL_ALLPROTO */
u16 port;
/* ADDR_INTERNAL_AUTOTOR */
struct wireaddr torservice;
struct torservice {
struct wireaddr torservice_address;
u16 port;
} torservice;
/* ADDR_INTERNAL_FORPROXY */
struct unresolved {
char name[256];
Expand Down
4 changes: 2 additions & 2 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1102,15 +1102,15 @@ static struct wireaddr_internal *setup_listeners(const tal_t *ctx,
continue;
if (!(proposed_listen_announce[i] & ADDR_ANNOUNCE)) {
tor_autoservice(tmpctx,
&proposed_wireaddr[i].u.torservice,
&proposed_wireaddr[i],
tor_password,
binding,
daemon->use_v3_autotor);
continue;
};
add_announcable(announcable,
tor_autoservice(tmpctx,
&proposed_wireaddr[i].u.torservice,
&proposed_wireaddr[i],
tor_password,
binding,
daemon->use_v3_autotor));
Expand Down
17 changes: 9 additions & 8 deletions connectd/tor_autoservice.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ static void discard_remaining_response(struct rbuf *rbuf)
static struct wireaddr *make_onion(const tal_t *ctx,
struct rbuf *rbuf,
const struct wireaddr *local,
bool use_v3_autotor)
bool use_v3_autotor,
u16 port)
{
char *line;
struct wireaddr *onion;
Expand Down Expand Up @@ -102,12 +103,12 @@ static struct wireaddr *make_onion(const tal_t *ctx,
tor_send_cmd(rbuf,
tal_fmt(tmpctx, "ADD_ONION NEW:RSA1024 Port=%d,%s Flags=DiscardPK,Detach",
/* FIXME: We *could* allow user to set Tor port */
DEFAULT_PORT, fmt_wireaddr(tmpctx, local)));
port, fmt_wireaddr(tmpctx, local)));
} else {
tor_send_cmd(rbuf,
tal_fmt(tmpctx, "ADD_ONION NEW:ED25519-V3 Port=%d,%s Flags=DiscardPK,Detach",
/* FIXME: We *could* allow user to set Tor port */
DEFAULT_PORT, fmt_wireaddr(tmpctx, local)));
port, fmt_wireaddr(tmpctx, local)));
}

while ((line = tor_response_line(rbuf)) != NULL) {
Expand All @@ -122,10 +123,10 @@ static struct wireaddr *make_onion(const tal_t *ctx,

name = tal_fmt(tmpctx, "%s.onion", line);
onion = tal(ctx, struct wireaddr);
if (!parse_wireaddr(name, onion, DEFAULT_PORT, false, NULL))
if (!parse_wireaddr(name, onion, local->port, false, NULL))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Tor gave bad onion name '%s'", name);
status_info("New autotor service onion address: \"%s:%d\"", name, DEFAULT_PORT);
status_info("New autotor service onion address: \"%s:%d\" bound to extern-port:%d", name, local->port, port);
discard_remaining_response(rbuf);
return onion;
}
Expand Down Expand Up @@ -226,7 +227,7 @@ find_local_address(const struct wireaddr_internal *bindings)
}

struct wireaddr *tor_autoservice(const tal_t *ctx,
const struct wireaddr *tor_serviceaddr,
const struct wireaddr_internal *tor_serviceaddr,
const char *tor_password,
const struct wireaddr_internal *bindings,
const bool use_v3_autotor)
Expand All @@ -239,7 +240,7 @@ struct wireaddr *tor_autoservice(const tal_t *ctx,
char *buffer;

laddr = find_local_address(bindings);
ai_tor = wireaddr_to_addrinfo(tmpctx, tor_serviceaddr);
ai_tor = wireaddr_to_addrinfo(tmpctx, &tor_serviceaddr->u.torservice.torservice_address);

fd = socket(ai_tor->ai_family, SOCK_STREAM, 0);
if (fd < 0)
Expand All @@ -252,7 +253,7 @@ struct wireaddr *tor_autoservice(const tal_t *ctx,
rbuf_init(&rbuf, fd, buffer, tal_count(buffer), buf_resize);

negotiate_auth(&rbuf, tor_password);
onion = make_onion(ctx, &rbuf, laddr, use_v3_autotor);
onion = make_onion(ctx, &rbuf, laddr, use_v3_autotor, tor_serviceaddr->u.torservice.port);

/*on the other hand we can stay connected until ln finish to keep onion alive and then vanish */
//because when we run with Detach flag as we now do every start of LN creates a new addr while the old
Expand Down
2 changes: 1 addition & 1 deletion connectd/tor_autoservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stdlib.h>

struct wireaddr *tor_autoservice(const tal_t *ctx,
const struct wireaddr *tor_serviceaddr,
const struct wireaddr_internal *tor_serviceaddr,
const char *tor_password,
const struct wireaddr_internal *bindings,
const bool use_v3_autotor);
Expand Down
2 changes: 1 addition & 1 deletion lightningd/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ void json_add_address_internal(struct json_stream *response,
case ADDR_INTERNAL_AUTOTOR:
json_object_start(response, fieldname);
json_add_string(response, "type", "Tor generated address");
json_add_address(response, "service", &addr->u.torservice);
json_add_address(response, "service", &addr->u.torservice.torservice_address);
json_object_end(response);
return;
case ADDR_INTERNAL_FORPROXY:
Expand Down