|
|
|
@ -122,6 +122,60 @@ struct proxy_instance {
|
|
|
|
|
|
|
|
|
|
typedef struct proxy_instance proxy_instance_t; |
|
|
|
|
|
|
|
|
|
static bool server_alive(ckpool_t *ckp, server_instance_t *si, bool pinging) |
|
|
|
|
{ |
|
|
|
|
char *userpass = NULL; |
|
|
|
|
bool ret = false; |
|
|
|
|
connsock_t *cs; |
|
|
|
|
gbtbase_t *gbt; |
|
|
|
|
|
|
|
|
|
cs = &si->cs; |
|
|
|
|
if (!extract_sockaddr(si->url, &cs->url, &cs->port)) { |
|
|
|
|
LOGWARNING("Failed to extract address from %s", si->url); |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
userpass = strdup(si->auth); |
|
|
|
|
realloc_strcat(&userpass, ":"); |
|
|
|
|
realloc_strcat(&userpass, si->pass); |
|
|
|
|
cs->auth = http_base64(userpass); |
|
|
|
|
dealloc(userpass); |
|
|
|
|
if (!cs->auth) { |
|
|
|
|
LOGWARNING("Failed to create base64 auth from %s", userpass); |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cs->fd = connect_socket(cs->url, cs->port); |
|
|
|
|
if (cs->fd < 0) { |
|
|
|
|
if (!pinging) |
|
|
|
|
LOGWARNING("Failed to connect socket to %s:%s !", cs->url, cs->port); |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Test we can connect, authorise and get a block template */ |
|
|
|
|
gbt = ckzalloc(sizeof(gbtbase_t)); |
|
|
|
|
si->data = gbt; |
|
|
|
|
if (!gen_gbtbase(cs, gbt)) { |
|
|
|
|
if (!pinging) { |
|
|
|
|
LOGINFO("Failed to get test block template from %s:%s!", |
|
|
|
|
cs->url, cs->port); |
|
|
|
|
} |
|
|
|
|
goto out_close; |
|
|
|
|
} |
|
|
|
|
clear_gbtbase(gbt); |
|
|
|
|
if (!validate_address(cs, ckp->btcaddress)) { |
|
|
|
|
LOGWARNING("Invalid btcaddress: %s !", ckp->btcaddress); |
|
|
|
|
goto out_close; |
|
|
|
|
} |
|
|
|
|
ret = true; |
|
|
|
|
out_close: |
|
|
|
|
if (!ret) |
|
|
|
|
close(cs->fd); |
|
|
|
|
else |
|
|
|
|
keep_sockalive(cs->fd); |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Find the highest priority server alive and return it */ |
|
|
|
|
static server_instance_t *live_server(ckpool_t *ckp) |
|
|
|
|
{ |
|
|
|
|
server_instance_t *alive = NULL; |
|
|
|
@ -134,49 +188,12 @@ retry:
|
|
|
|
|
goto out; |
|
|
|
|
|
|
|
|
|
for (i = 0; i < ckp->btcds; i++) { |
|
|
|
|
server_instance_t *si; |
|
|
|
|
char *userpass = NULL; |
|
|
|
|
gbtbase_t *gbt; |
|
|
|
|
|
|
|
|
|
si = ckp->servers[i]; |
|
|
|
|
cs = &si->cs; |
|
|
|
|
if (!extract_sockaddr(si->url, &cs->url, &cs->port)) { |
|
|
|
|
LOGWARNING("Failed to extract address from %s", si->url); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
userpass = strdup(si->auth); |
|
|
|
|
realloc_strcat(&userpass, ":"); |
|
|
|
|
realloc_strcat(&userpass, si->pass); |
|
|
|
|
cs->auth = http_base64(userpass); |
|
|
|
|
dealloc(userpass); |
|
|
|
|
if (!cs->auth) { |
|
|
|
|
LOGWARNING("Failed to create base64 auth from %s", userpass); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cs->fd = connect_socket(cs->url, cs->port); |
|
|
|
|
if (cs->fd < 0) { |
|
|
|
|
LOGWARNING("Failed to connect socket to %s:%s !", cs->url, cs->port); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
server_instance_t *si = ckp->servers[i]; |
|
|
|
|
|
|
|
|
|
keep_sockalive(cs->fd); |
|
|
|
|
|
|
|
|
|
/* Test we can connect, authorise and get a block template */ |
|
|
|
|
gbt = ckzalloc(sizeof(gbtbase_t)); |
|
|
|
|
si->data = gbt; |
|
|
|
|
if (!gen_gbtbase(cs, gbt)) { |
|
|
|
|
LOGINFO("Failed to get test block template from %s:%s!", |
|
|
|
|
cs->url, cs->port); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
clear_gbtbase(gbt); |
|
|
|
|
if (!validate_address(cs, ckp->btcaddress)) { |
|
|
|
|
LOGWARNING("Invalid btcaddress: %s !", ckp->btcaddress); |
|
|
|
|
continue; |
|
|
|
|
if (server_alive(ckp, si, false)) { |
|
|
|
|
alive = si; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
alive = si; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
if (!alive) { |
|
|
|
|
LOGWARNING("CRITICAL: No bitcoinds active!"); |
|
|
|
@ -1253,6 +1270,46 @@ static void passthrough_add_send(proxy_instance_t *proxi, const char *msg)
|
|
|
|
|
ckmsgq_add(proxi->passsends, pm); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static bool proxy_alive(ckpool_t *ckp, server_instance_t *si, proxy_instance_t *proxi, |
|
|
|
|
connsock_t *cs, bool pinging) |
|
|
|
|
{ |
|
|
|
|
if (!extract_sockaddr(si->url, &cs->url, &cs->port)) { |
|
|
|
|
LOGWARNING("Failed to extract address from %s", si->url); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
if (!connect_proxy(cs)) { |
|
|
|
|
if (!pinging) { |
|
|
|
|
LOGINFO("Failed to connect to %s:%s in proxy_mode!", |
|
|
|
|
cs->url, cs->port); |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
if (ckp->passthrough) { |
|
|
|
|
if (!passthrough_stratum(cs, proxi)) { |
|
|
|
|
LOGWARNING("Failed initial passthrough to %s:%s !", |
|
|
|
|
cs->url, cs->port); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
/* Test we can connect, authorise and get stratum information */ |
|
|
|
|
if (!subscribe_stratum(cs, proxi)) { |
|
|
|
|
if (!pinging) { |
|
|
|
|
LOGINFO("Failed initial subscribe to %s:%s !", |
|
|
|
|
cs->url, cs->port); |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
if (!auth_stratum(cs, proxi)) { |
|
|
|
|
if (!pinging) { |
|
|
|
|
LOGWARNING("Failed initial authorise to %s:%s with %s:%s !", |
|
|
|
|
cs->url, cs->port, si->auth, si->pass); |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Cycle through the available proxies and find the first alive one */ |
|
|
|
|
static proxy_instance_t *live_proxy(ckpool_t *ckp) |
|
|
|
|
{ |
|
|
|
@ -1272,37 +1329,10 @@ retry:
|
|
|
|
|
si = ckp->servers[i]; |
|
|
|
|
proxi = si->data; |
|
|
|
|
cs = proxi->cs; |
|
|
|
|
if (!extract_sockaddr(si->url, &cs->url, &cs->port)) { |
|
|
|
|
LOGWARNING("Failed to extract address from %s", si->url); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
if (!connect_proxy(cs)) { |
|
|
|
|
LOGINFO("Failed to connect to %s:%s in proxy_mode!", |
|
|
|
|
cs->url, cs->port); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
if (ckp->passthrough) { |
|
|
|
|
if (!passthrough_stratum(cs, proxi)) { |
|
|
|
|
LOGWARNING("Failed initial passthrough to %s:%s !", |
|
|
|
|
cs->url, cs->port); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
if (proxy_alive(ckp, si, proxi, cs, false)) { |
|
|
|
|
alive = proxi; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
/* Test we can connect, authorise and get stratum information */ |
|
|
|
|
if (!subscribe_stratum(cs, proxi)) { |
|
|
|
|
LOGINFO("Failed initial subscribe to %s:%s !", |
|
|
|
|
cs->url, cs->port); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
if (!auth_stratum(cs, proxi)) { |
|
|
|
|
LOGWARNING("Failed initial authorise to %s:%s with %s:%s !", |
|
|
|
|
cs->url, cs->port, si->auth, si->pass); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
alive = proxi; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
if (!alive) { |
|
|
|
|
send_proc(ckp->stratifier, "dropall"); |
|
|
|
|