socks5-client.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. *
  24. * Socks5 Client -related helpers
  25. */
  26. #include "private-lib-core.h"
  27. int
  28. lws_set_socks(struct lws_vhost *vhost, const char *socks)
  29. {
  30. char *p_at, *p_colon;
  31. char user[96];
  32. char password[96];
  33. if (!socks)
  34. return -1;
  35. vhost->socks_user[0] = '\0';
  36. vhost->socks_password[0] = '\0';
  37. p_at = strrchr(socks, '@');
  38. if (p_at) { /* auth is around */
  39. if ((unsigned int)(p_at - socks) > (sizeof(user)
  40. + sizeof(password) - 2)) {
  41. lwsl_err("Socks auth too long\n");
  42. goto bail;
  43. }
  44. p_colon = strchr(socks, ':');
  45. if (p_colon) {
  46. if ((unsigned int)(p_colon - socks) > (sizeof(user)
  47. - 1) ) {
  48. lwsl_err("Socks user too long\n");
  49. goto bail;
  50. }
  51. if ((unsigned int)(p_at - p_colon) > (sizeof(password)
  52. - 1) ) {
  53. lwsl_err("Socks password too long\n");
  54. goto bail;
  55. }
  56. lws_strncpy(vhost->socks_user, socks,
  57. p_colon - socks + 1);
  58. lws_strncpy(vhost->socks_password, p_colon + 1,
  59. p_at - (p_colon + 1) + 1);
  60. }
  61. lwsl_info(" Socks auth, user: %s, password: %s\n",
  62. vhost->socks_user, vhost->socks_password );
  63. socks = p_at + 1;
  64. }
  65. lws_strncpy(vhost->socks_proxy_address, socks,
  66. sizeof(vhost->socks_proxy_address));
  67. p_colon = strchr(vhost->socks_proxy_address, ':');
  68. if (!p_colon && !vhost->socks_proxy_port) {
  69. lwsl_err("socks_proxy needs to be address:port\n");
  70. return -1;
  71. } else {
  72. if (p_colon) {
  73. *p_colon = '\0';
  74. vhost->socks_proxy_port = atoi(p_colon + 1);
  75. }
  76. }
  77. lwsl_debug("%s: Connections via Socks5 %s:%u\n", __func__,
  78. vhost->socks_proxy_address, vhost->socks_proxy_port);
  79. return 0;
  80. bail:
  81. return -1;
  82. }
  83. int
  84. lws_socks5c_generate_msg(struct lws *wsi, enum socks_msg_type type,
  85. ssize_t *msg_len)
  86. {
  87. struct lws_context *context = wsi->a.context;
  88. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  89. uint8_t *p = pt->serv_buf, *end = &p[context->pt_serv_buf_size];
  90. ssize_t n, passwd_len;
  91. short net_num;
  92. char *cp;
  93. switch (type) {
  94. case SOCKS_MSG_GREETING:
  95. if (lws_ptr_diff(end, p) < 4)
  96. return 1;
  97. /* socks version, version 5 only */
  98. *p++ = SOCKS_VERSION_5;
  99. /* number of methods */
  100. *p++ = 2;
  101. /* username password method */
  102. *p++ = SOCKS_AUTH_USERNAME_PASSWORD;
  103. /* no authentication method */
  104. *p++ = SOCKS_AUTH_NO_AUTH;
  105. break;
  106. case SOCKS_MSG_USERNAME_PASSWORD:
  107. n = strlen(wsi->a.vhost->socks_user);
  108. passwd_len = strlen(wsi->a.vhost->socks_password);
  109. if (n > 254 || passwd_len > 254)
  110. return 1;
  111. if (lws_ptr_diff(end, p) < 3 + n + passwd_len)
  112. return 1;
  113. /* the subnegotiation version */
  114. *p++ = SOCKS_SUBNEGOTIATION_VERSION_1;
  115. /* length of the user name */
  116. *p++ = n;
  117. /* user name */
  118. memcpy(p, wsi->a.vhost->socks_user, n);
  119. p += n;
  120. /* length of the password */
  121. *p++ = passwd_len;
  122. /* password */
  123. memcpy(p, wsi->a.vhost->socks_password, passwd_len);
  124. p += passwd_len;
  125. break;
  126. case SOCKS_MSG_CONNECT:
  127. n = strlen(wsi->stash->cis[CIS_ADDRESS]);
  128. if (n > 254 || lws_ptr_diff(end, p) < 5 + n + 2)
  129. return 1;
  130. cp = (char *)&net_num;
  131. /* socks version */
  132. *p++ = SOCKS_VERSION_5;
  133. /* socks command */
  134. *p++ = SOCKS_COMMAND_CONNECT;
  135. /* reserved */
  136. *p++ = 0;
  137. /* address type */
  138. *p++ = SOCKS_ATYP_DOMAINNAME;
  139. /* length of ---> */
  140. *p++ = n;
  141. /* the address we tell SOCKS proxy to connect to */
  142. memcpy(p, wsi->stash->cis[CIS_ADDRESS], n);
  143. p += n;
  144. net_num = htons(wsi->c_port);
  145. /* the port we tell SOCKS proxy to connect to */
  146. *p++ = cp[0];
  147. *p++ = cp[1];
  148. break;
  149. default:
  150. return 1;
  151. }
  152. *msg_len = lws_ptr_diff(p, pt->serv_buf);
  153. return 0;
  154. }
  155. int
  156. lws_socks5c_ads_server(struct lws_vhost *vh,
  157. const struct lws_context_creation_info *info)
  158. {
  159. /* socks proxy */
  160. if (info->socks_proxy_address) {
  161. /* override for backwards compatibility */
  162. if (info->socks_proxy_port)
  163. vh->socks_proxy_port = info->socks_proxy_port;
  164. lws_set_socks(vh, info->socks_proxy_address);
  165. return 0;
  166. }
  167. #ifdef LWS_HAVE_GETENV
  168. {
  169. char *p = getenv("socks_proxy");
  170. if (p && strlen(p) > 0 && strlen(p) < 95)
  171. lws_set_socks(vh, p);
  172. }
  173. #endif
  174. return 0;
  175. }
  176. /*
  177. * Returns 0 = nothing for caller to do, 1 = return wsi, -1 = goto failed
  178. */
  179. int
  180. lws_socks5c_greet(struct lws *wsi, const char **pcce)
  181. {
  182. struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
  183. ssize_t plen;
  184. int n;
  185. /* socks proxy */
  186. if (!wsi->a.vhost->socks_proxy_port)
  187. return 0;
  188. if (lws_socks5c_generate_msg(wsi, SOCKS_MSG_GREETING, &plen)) {
  189. *pcce = "socks msg too large";
  190. return -1;
  191. }
  192. // lwsl_hexdump_notice(pt->serv_buf, plen);
  193. n = send(wsi->desc.sockfd, (char *)pt->serv_buf, plen,
  194. MSG_NOSIGNAL);
  195. if (n < 0) {
  196. lwsl_debug("ERROR writing socks greeting\n");
  197. *pcce = "socks write failed";
  198. return -1;
  199. }
  200. lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_SOCKS_GREETING_REPLY,
  201. wsi->a.context->timeout_secs);
  202. lwsi_set_state(wsi, LRS_WAITING_SOCKS_GREETING_REPLY);
  203. return 1;
  204. }
  205. int
  206. lws_socks5c_handle_state(struct lws *wsi, struct lws_pollfd *pollfd,
  207. const char **pcce)
  208. {
  209. struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
  210. int conn_mode = 0, pending_timeout = 0;
  211. ssize_t len;
  212. int n;
  213. /* handle proxy hung up on us */
  214. if (pollfd->revents & LWS_POLLHUP) {
  215. lwsl_warn("SOCKS connection %p (fd=%d) dead\n",
  216. (void *)wsi, pollfd->fd);
  217. *pcce = "socks conn dead";
  218. return LW5CHS_RET_BAIL3;
  219. }
  220. n = recv(wsi->desc.sockfd, pt->serv_buf,
  221. wsi->a.context->pt_serv_buf_size, 0);
  222. if (n < 0) {
  223. if (LWS_ERRNO == LWS_EAGAIN) {
  224. lwsl_debug("SOCKS read EAGAIN, retrying\n");
  225. return LW5CHS_RET_RET0;
  226. }
  227. lwsl_err("ERROR reading from SOCKS socket\n");
  228. *pcce = "socks recv fail";
  229. return LW5CHS_RET_BAIL3;
  230. }
  231. // lwsl_hexdump_warn(pt->serv_buf, n);
  232. switch (lwsi_state(wsi)) {
  233. case LRS_WAITING_SOCKS_GREETING_REPLY:
  234. if (pt->serv_buf[0] != SOCKS_VERSION_5)
  235. goto socks_reply_fail;
  236. if (pt->serv_buf[1] == SOCKS_AUTH_NO_AUTH) {
  237. lwsl_client("SOCKS GR: No Auth Method\n");
  238. if (lws_socks5c_generate_msg(wsi, SOCKS_MSG_CONNECT,
  239. &len)) {
  240. lwsl_err("%s: failed to generate connect msg\n",
  241. __func__);
  242. goto socks_send_msg_fail;
  243. }
  244. conn_mode = LRS_WAITING_SOCKS_CONNECT_REPLY;
  245. pending_timeout =
  246. PENDING_TIMEOUT_AWAITING_SOCKS_CONNECT_REPLY;
  247. goto socks_send;
  248. }
  249. if (pt->serv_buf[1] == SOCKS_AUTH_USERNAME_PASSWORD) {
  250. lwsl_client("SOCKS GR: User/Pw Method\n");
  251. if (lws_socks5c_generate_msg(wsi,
  252. SOCKS_MSG_USERNAME_PASSWORD,
  253. &len))
  254. goto socks_send_msg_fail;
  255. conn_mode = LRS_WAITING_SOCKS_AUTH_REPLY;
  256. pending_timeout =
  257. PENDING_TIMEOUT_AWAITING_SOCKS_AUTH_REPLY;
  258. goto socks_send;
  259. }
  260. goto socks_reply_fail;
  261. case LRS_WAITING_SOCKS_AUTH_REPLY:
  262. if (pt->serv_buf[0] != SOCKS_SUBNEGOTIATION_VERSION_1 ||
  263. pt->serv_buf[1] !=
  264. SOCKS_SUBNEGOTIATION_STATUS_SUCCESS)
  265. goto socks_reply_fail;
  266. lwsl_client("SOCKS password OK, sending connect\n");
  267. if (lws_socks5c_generate_msg(wsi, SOCKS_MSG_CONNECT, &len)) {
  268. socks_send_msg_fail:
  269. *pcce = "socks gen msg fail";
  270. return LW5CHS_RET_BAIL3;
  271. }
  272. conn_mode = LRS_WAITING_SOCKS_CONNECT_REPLY;
  273. pending_timeout =
  274. PENDING_TIMEOUT_AWAITING_SOCKS_CONNECT_REPLY;
  275. socks_send:
  276. // lwsl_hexdump_notice(pt->serv_buf, len);
  277. n = send(wsi->desc.sockfd, (char *)pt->serv_buf, len,
  278. MSG_NOSIGNAL);
  279. if (n < 0) {
  280. lwsl_debug("ERROR writing to socks proxy\n");
  281. *pcce = "socks write fail";
  282. return LW5CHS_RET_BAIL3;
  283. }
  284. lws_set_timeout(wsi, pending_timeout,
  285. wsi->a.context->timeout_secs);
  286. lwsi_set_state(wsi, conn_mode);
  287. break;
  288. socks_reply_fail:
  289. lwsl_err("%s: socks reply: v%d, err %d\n", __func__,
  290. pt->serv_buf[0], pt->serv_buf[1]);
  291. *pcce = "socks reply fail";
  292. return LW5CHS_RET_BAIL3;
  293. case LRS_WAITING_SOCKS_CONNECT_REPLY:
  294. if (pt->serv_buf[0] != SOCKS_VERSION_5 ||
  295. pt->serv_buf[1] != SOCKS_REQUEST_REPLY_SUCCESS)
  296. goto socks_reply_fail;
  297. lwsl_client("%s: socks connect OK\n", __func__);
  298. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  299. if (lwsi_role_http(wsi) &&
  300. lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
  301. wsi->a.vhost->socks_proxy_address)) {
  302. *pcce = "socks connect fail";
  303. return LW5CHS_RET_BAIL3;
  304. }
  305. #endif
  306. wsi->c_port = wsi->a.vhost->socks_proxy_port;
  307. /* clear his proxy connection timeout */
  308. lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
  309. return LW5CHS_RET_STARTHS;
  310. default:
  311. break;
  312. }
  313. return LW5CHS_RET_NOTHING;
  314. }