Skip to content

Commit

Permalink
fixup! torcontrol: Create also a v3 ed25519-V3 service address.
Browse files Browse the repository at this point in the history
  • Loading branch information
Saibato committed Jul 23, 2020
1 parent e003843 commit b20ba82
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions src/torcontrol.cpp
Expand Up @@ -48,8 +48,8 @@ static const float RECONNECT_TIMEOUT_EXP = 1.5;
static const int MAX_LINE_LENGTH = 100000;

enum Tor_proto_version {
TOR_NET_V2,
TOR_NET_V3
TOR_NET_V2,
TOR_NET_V3
};

/****** Low-level TorControlConnection ********/
Expand Down Expand Up @@ -469,7 +469,6 @@ TorController::TorController(struct event_base* _base, const std::string& _targe
std::bind(&TorController::disconnected_cb, this, std::placeholders::_1) )) {
LogPrintf("tor: Initiating connection to Tor control port %s failed\n", _target);
}

}

TorController::~TorController()
Expand All @@ -486,7 +485,7 @@ TorController::~TorController()
void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlReply& reply)
{
if (reply.code == 250) {
LogPrint(BCLog::TOR, "tor: ADD_ONION %s successful\n", tor_version == TOR_NET_V2?"V2":"V3");
LogPrint(BCLog::TOR, "tor: ADD_ONION %s successful\n", tor_version == TOR_NET_V2 ? "V2" : "V3");
for (const std::string &s : reply.lines) {
std::map<std::string,std::string> m = ParseTorReplyMapping(s);
std::map<std::string,std::string>::iterator i;
Expand All @@ -496,15 +495,17 @@ void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlRe
private_key = i->second;
}
if (service_id.empty()) {
LogPrintf("tor: Error parsing ADD_ONION %s parameters:\n", tor_version == TOR_NET_V2?"V2":"V3");
LogPrintf("tor: Error parsing ADD_ONION %s parameters:\n", tor_version == TOR_NET_V2 ? "V2" : "V3");
for (const std::string &s : reply.lines) {
LogPrintf(" %s\n", SanitizeString(s));
}

} else {
service = LookupNumeric(std::string(service_id+".onion"), Params().GetDefaultPort());
if (service_id.length() > 16) tor_version = TOR_NET_V3;
else tor_version = TOR_NET_V2;
service = LookupNumeric(std::string(service_id + ".onion"), Params().GetDefaultPort());
if (service_id.length() > 16)
tor_version = TOR_NET_V3;
else
tor_version = TOR_NET_V2;
LogPrintf("tor: Got service ID %s, created %s.onion, advertising service %s\n", service_id, service_id, service.ToString());
if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) {
LogPrint(BCLog::TOR, "tor: Write cached service private key to %s\n", GetPrivateKeyFile().string());
Expand All @@ -515,28 +516,28 @@ void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlRe
// ... onion requested - keep connection open

if (reply.code == 510) { // 510 Unrecognized command
LogPrintf("tor: Add onion failed with unrecognized command (You probably need to upgrade Tor)\n");
LogPrintf("tor: Add onion failed with unrecognized command (You probably need to upgrade Tor)\n");
}
}
} else {
LogPrintf("tor: Add onion failed; error code %d\n", reply.code);
}
// Tor might deprecate v2-onions so we try to create from now on also v3 onions
// Now we try to create the v3-service
if (tor_version == TOR_NET_V2) {
tor_version = TOR_NET_V3;
private_key = "";
std::pair<bool,std::string> pkf = ReadBinaryFile(GetPrivateKeyFile());
if (pkf.first) {
LogPrint(BCLog::TOR, "tor: Reading cached private key from %s\n", GetPrivateKeyFile().string());
private_key = pkf.second;
}
if (private_key.empty()) // No private key, generate one
private_key= "NEW:ED25519-V3"; // Explicitly request ED25519-V3
// Request v3-hidden service, redirect port.
// Note that the 'virtual' port is always the default port to avoid decloaking nodes using other ports.
conn.Command(strprintf("ADD_ONION %s Port=%i,127.0.0.1:%i", private_key, Params().GetDefaultPort(), GetListenPort()),
std::bind(&TorController::add_onion_cb, this, std::placeholders::_1, std::placeholders::_2));
// Tor might deprecate v2-onions so we try to create from now on also v3 onions
// Now we try to create the v3-service
if (tor_version == TOR_NET_V2) {
tor_version = TOR_NET_V3;
private_key = "";
std::pair<bool, std::string> pkf = ReadBinaryFile(GetPrivateKeyFile());
if (pkf.first) {
LogPrint(BCLog::TOR, "tor: Reading cached private key from %s\n", GetPrivateKeyFile().string());
private_key = pkf.second;
}
if (private_key.empty()) // No private key, generate one
private_key = "NEW:ED25519-V3"; // Explicitly request ED25519-V3
// Request v3-hidden service, redirect port.
// Note that the 'virtual' port is always the default port to avoid decloaking nodes using other ports.
conn.Command(strprintf("ADD_ONION %s Port=%i,127.0.0.1:%i", private_key, Params().GetDefaultPort(), GetListenPort()),
std::bind(&TorController::add_onion_cb, this, std::placeholders::_1, std::placeholders::_2));
}
}

Expand All @@ -545,7 +546,7 @@ void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply&
if (reply.code == 250) {
LogPrint(BCLog::TOR, "tor: Authentication successful\n");

// Now that we know Tor is running setup the proxy for onion addresses
// Now that we know Tor is running, set up the proxy for onion addresses
// if -onion isn't set to something else.
if (gArgs.GetArg("-onion", "") == "") {
CService resolved(LookupNumeric("127.0.0.1", 9050));
Expand All @@ -557,7 +558,7 @@ void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply&
// Finaly we create the v2-service
tor_version = TOR_NET_V2;
private_key = "";
std::pair<bool,std::string> pkf = ReadBinaryFile(GetPrivateKeyFile());
std::pair<bool, std::string> pkf = ReadBinaryFile(GetPrivateKeyFile());
if (pkf.first) {
LogPrint(BCLog::TOR, "tor: Reading cached private key from %s\n", GetPrivateKeyFile().string());
private_key = pkf.second;
Expand Down Expand Up @@ -804,5 +805,4 @@ void StopTorControl()
event_base_free(gBase);
gBase = nullptr;
}

}

0 comments on commit b20ba82

Please sign in to comment.