Hello everyone and welcome to this new episode of Inside Bitcoin Code.
A special welcome to the new people that recently subscribed to this newsletter, I hope that you will enjoy what I write. A small suggestion for you: if you know nothing about coding, start reading the posts from Learn2Code to get some basic knowledge before diving in the real deal.
A huge thanks to each of the 145 subscribers of this newsletter and to those who decided to contribute with some sats to this project. It means the world to me!
Today we are going to continue our trip inside the initialization of Bitcoin Core with proxy setting for outgoing network traffic.
Let’s start!
Proxy
First of all, what is a proxy1? A proxy can be seen as middleman that routes your internet traffic through another server. Basically, it acts as an intermediary between a client requesting a resource to a server, and the server providing the resource to the client. This allows a user to gain privacy or to bypass restrictions on a network.
At the beginning, different Proxy
objects are declared, one for each available network type:
Proxy ipv4_proxy;
Proxy ipv6_proxy;
Proxy onion_proxy;
Proxy name_proxy;
Proxy cjdns_proxy;
It is possible to set:
IPv42: Regular internet connection on the v4 standard.
IPv63: Regular internet connection on the v6 standard.
Onion: For anonymous connection on the Tor4 network.
Name: For DNS lookups.
Cjdns5: For connection on a distributed network.
By default, the proxy setting is removed, which is the same as passing the argument as -noproxy
, -proxy=0
or -proxy="".
After declaration, a for
loop iterates through all the inputs coming from the -proxy
argument. The GetArgs
function returns a string
, which gets stored inside param_value
. This iteration goes on until there is no more data in the -proxy
list.
for (const std::string& param_value : args.GetArgs("-proxy"))
Each param_value
will specify a network address to a proxy server and, if needed, the type of network to which it applies. If no network is specified, it is applied to all network types. Here is an example:
127.0.0.1:9050=ipv4
Network string
Once the input is in the param_value
string, the = character is looked for in it. This is done to later separate the address from the type of network.
const auto eq_pos{param_value.rfind('=')};
const std::string proxy_str{param_value.substr(0, eq_pos)};
eq_pos
represents the position in the string at which the character is found. This variable is used to extrapolate the address string from param_value
using the substr
function. This function takes a starting point, 0
in our case, and a length (eq_pos
) and stores the result in proxy_str
.
Then, the code checks whether or not the network type was set in the input argument, and, in case, stores it in net_str
string. To do so, the code checks that the eq_pos
variable is different than npos
, which is a member of the string
class that literally says “end of the string”. In other words, it means that the =
character was not found in the param_value string.
std::string net_str;
if (eq_pos != std::string::npos) {
if (eq_pos + 1 == param_value.length()) {
return InitError(strprintf(_("Invalid -proxy address or hostname, ends with '=': '%s'"), param_value));
}
net_str = ToLower(param_value.substr(eq_pos + 1)); // e.g. 127.0.0.1:9050=ipv4 -> ipv4
}
In case the if
clause returns true
, another check is made to be sure something has been defined after =
. If true
, an error is returned. This input, for example, would return an error:
127.0.0.1:9050=
In case everything is setup correctly, the network type is saved into the net_str
variable.
Let’s keep in touch:
Check out my writings on btc++ insider edition
Try my new app Sats Tracker, an expense tracker app for people living in the Bitcoin standard.
Zap me a coffee: tuma@wallet.yakihonne.comx