Add support for socks5 proxy with stratum
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
diff --git a/util.c b/util.c
index 6c5d9c0..558b42b 100644
--- a/util.c
+++ b/util.c
@@ -1783,6 +1783,63 @@ static bool http_negotiate(struct pool *pool, int sockd, bool http0)
return true;
}
+static bool socks5_negotiate(struct pool *pool, int sockd)
+{
+ unsigned char atyp, uclen;
+ unsigned short port;
+ char buf[515];
+ int i, len;
+
+ buf[0] = 0x05;
+ buf[1] = 0x01;
+ buf[2] = 0x00;
+ send(sockd, buf, 3, 0);
+ if (recv_byte(sockd) != 0x05 || recv_byte(sockd) != buf[2]) {
+ applog(LOG_WARNING, "Bad response from %s:%s SOCKS5 server",
+ pool->sockaddr_proxy_url, pool->sockaddr_proxy_port );
+ return false;
+ }
+
+ buf[0] = 0x05;
+ buf[1] = 0x01;
+ buf[2] = 0x00;
+ buf[3] = 0x03;
+ len = (strlen(pool->sockaddr_url));
+ if (len > 255)
+ len = 255;
+ uclen = len;
+ buf[4] = (uclen & 0xff);
+ memcpy(buf + 5, pool->sockaddr_url, len);
+ port = atoi(pool->stratum_port);
+ buf[5 + len] = (port >> 8);
+ buf[6 + len] = (port & 0xff);
+ send(sockd, buf, (7 + len), 0);
+ if (recv_byte(sockd) != 0x05 || recv_byte(sockd) != 0x00) {
+ applog(LOG_WARNING, "Bad response from %s:%s SOCKS5 server",
+ pool->sockaddr_proxy_url, pool->sockaddr_proxy_port );
+ return false;
+ }
+
+ recv_byte(sockd);
+ atyp = recv_byte(sockd);
+ if (atyp == 0x01) {
+ for (i = 0; i < 4; i++)
+ recv_byte(sockd);
+ } else if (atyp == 0x03) {
+ len = recv_byte(sockd);
+ for (i = 0; i < len; i++)
+ recv_byte(sockd);
+ } else {
+ applog(LOG_WARNING, "Bad response from %s:%s SOCKS5 server",
+ pool->sockaddr_proxy_url, pool->sockaddr_proxy_port );
+ return false;
+ }
+ for (i = 0; i < 2; i++)
+ recv_byte(sockd);
+
+ return true;
+}
+
static bool setup_stratum_socket(struct pool *pool)
{
struct addrinfo servinfobase, *servinfo, *hints, pbase, *p;
@@ -1854,6 +1911,10 @@ static bool setup_stratum_socket(struct pool *pool)
if (!http_negotiate(pool, sockd, false))
return false;
break;
+ case CURLPROXY_SOCKS5:
+ if (!socks5_negotiate(pool, sockd))
+ return false;
+ break;
default:
applog(LOG_WARNING, "Unsupported proxy type for %s:%s",
pool->sockaddr_proxy_url, pool->sockaddr_proxy_port);