You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

885 lines
23 KiB

10 years ago
10 years ago
10 years ago
  1. /*
  2. * tunnel.c - Setup a local port forwarding through remote shadowsocks server
  3. *
  4. * Copyright (C) 2013 - 2014, Max Lv <max.c.lv@gmail.com>
  5. *
  6. * This file is part of the shadowsocks-libev.
  7. *
  8. * shadowsocks-libev is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * shadowsocks-libev is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with pdnsd; see the file COPYING. If not, see
  20. * <http://www.gnu.org/licenses/>.
  21. */
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <fcntl.h>
  25. #include <locale.h>
  26. #include <signal.h>
  27. #include <string.h>
  28. #include <strings.h>
  29. #include <unistd.h>
  30. #ifndef __MINGW32__
  31. #include <errno.h>
  32. #include <arpa/inet.h>
  33. #include <netdb.h>
  34. #include <netinet/in.h>
  35. #include <netinet/tcp.h>
  36. #include <pthread.h>
  37. #endif
  38. #ifdef HAVE_CONFIG_H
  39. #include "config.h"
  40. #endif
  41. #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H) && defined(__linux__)
  42. #include <net/if.h>
  43. #include <sys/ioctl.h>
  44. #define SET_INTERFACE
  45. #endif
  46. #ifdef __MINGW32__
  47. #include "win32.h"
  48. #endif
  49. #include "utils.h"
  50. #include "tunnel.h"
  51. #ifndef EAGAIN
  52. #define EAGAIN EWOULDBLOCK
  53. #endif
  54. #ifndef EWOULDBLOCK
  55. #define EWOULDBLOCK EAGAIN
  56. #endif
  57. #ifndef BUF_SIZE
  58. #define BUF_SIZE 2048
  59. #endif
  60. int verbose = 0;
  61. int udprelay = 0;
  62. #ifndef __MINGW32__
  63. static int setnonblocking(int fd)
  64. {
  65. int flags;
  66. if (-1 ==(flags = fcntl(fd, F_GETFL, 0)))
  67. flags = 0;
  68. return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  69. }
  70. #endif
  71. #ifdef SET_INTERFACE
  72. int setinterface(int socket_fd, const char* interface_name)
  73. {
  74. struct ifreq interface;
  75. memset(&interface, 0, sizeof(interface));
  76. strncpy(interface.ifr_name, interface_name, IFNAMSIZ);
  77. int res = setsockopt(socket_fd, SOL_SOCKET, SO_BINDTODEVICE, &interface, sizeof(struct ifreq));
  78. return res;
  79. }
  80. #endif
  81. int create_and_bind(const char *addr, const char *port)
  82. {
  83. struct addrinfo hints;
  84. struct addrinfo *result, *rp;
  85. int s, listen_sock;
  86. memset(&hints, 0, sizeof(struct addrinfo));
  87. hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
  88. hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
  89. s = getaddrinfo(addr, port, &hints, &result);
  90. if (s != 0)
  91. {
  92. LOGD("getaddrinfo: %s", gai_strerror(s));
  93. return -1;
  94. }
  95. for (rp = result; rp != NULL; rp = rp->ai_next)
  96. {
  97. listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  98. if (listen_sock == -1)
  99. continue;
  100. int opt = 1;
  101. setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  102. setsockopt(listen_sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
  103. #ifdef SO_NOSIGPIPE
  104. setsockopt(listen_sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
  105. #endif
  106. s = bind(listen_sock, rp->ai_addr, rp->ai_addrlen);
  107. if (s == 0)
  108. {
  109. /* We managed to bind successfully! */
  110. break;
  111. }
  112. else
  113. {
  114. ERROR("bind");
  115. }
  116. close(listen_sock);
  117. }
  118. if (rp == NULL)
  119. {
  120. LOGE("Could not bind");
  121. return -1;
  122. }
  123. freeaddrinfo(result);
  124. return listen_sock;
  125. }
  126. static void server_recv_cb (EV_P_ ev_io *w, int revents)
  127. {
  128. struct server_ctx *server_recv_ctx = (struct server_ctx *)w;
  129. struct server *server = server_recv_ctx->server;
  130. struct remote *remote = server->remote;
  131. if (remote == NULL)
  132. {
  133. close_and_free_server(EV_A_ server);
  134. return;
  135. }
  136. ssize_t r = recv(server->fd, remote->buf, BUF_SIZE, 0);
  137. if (r == 0)
  138. {
  139. // connection closed
  140. remote->buf_len = 0;
  141. remote->buf_idx = 0;
  142. close_and_free_server(EV_A_ server);
  143. if (remote != NULL)
  144. {
  145. ev_io_start(EV_A_ &remote->send_ctx->io);
  146. }
  147. return;
  148. }
  149. else if(r < 0)
  150. {
  151. if (errno == EAGAIN || errno == EWOULDBLOCK)
  152. {
  153. // no data
  154. // continue to wait for recv
  155. return;
  156. }
  157. else
  158. {
  159. ERROR("server recv");
  160. close_and_free_remote(EV_A_ remote);
  161. close_and_free_server(EV_A_ server);
  162. return;
  163. }
  164. }
  165. remote->buf = ss_encrypt(BUF_SIZE, remote->buf, &r, server->e_ctx);
  166. if (remote->buf == NULL)
  167. {
  168. LOGE("invalid password or cipher");
  169. close_and_free_remote(EV_A_ remote);
  170. close_and_free_server(EV_A_ server);
  171. return;
  172. }
  173. int s = send(remote->fd, remote->buf, r, 0);
  174. if(s == -1)
  175. {
  176. if (errno == EAGAIN || errno == EWOULDBLOCK)
  177. {
  178. // no data, wait for send
  179. remote->buf_len = r;
  180. remote->buf_idx = 0;
  181. ev_io_stop(EV_A_ &server_recv_ctx->io);
  182. ev_io_start(EV_A_ &remote->send_ctx->io);
  183. return;
  184. }
  185. else
  186. {
  187. ERROR("send");
  188. close_and_free_remote(EV_A_ remote);
  189. close_and_free_server(EV_A_ server);
  190. return;
  191. }
  192. }
  193. else if(s < r)
  194. {
  195. remote->buf_len = r - s;
  196. remote->buf_idx = s;
  197. ev_io_stop(EV_A_ &server_recv_ctx->io);
  198. ev_io_start(EV_A_ &remote->send_ctx->io);
  199. return;
  200. }
  201. }
  202. static void server_send_cb (EV_P_ ev_io *w, int revents)
  203. {
  204. struct server_ctx *server_send_ctx = (struct server_ctx *)w;
  205. struct server *server = server_send_ctx->server;
  206. struct remote *remote = server->remote;
  207. if (server->buf_len == 0)
  208. {
  209. // close and free
  210. close_and_free_remote(EV_A_ remote);
  211. close_and_free_server(EV_A_ server);
  212. return;
  213. }
  214. else
  215. {
  216. // has data to send
  217. ssize_t s = send(server->fd, server->buf + server->buf_idx,
  218. server->buf_len, 0);
  219. if (s < 0)
  220. {
  221. if (errno != EAGAIN && errno != EWOULDBLOCK)
  222. {
  223. ERROR("send");
  224. close_and_free_remote(EV_A_ remote);
  225. close_and_free_server(EV_A_ server);
  226. }
  227. return;
  228. }
  229. else if (s < server->buf_len)
  230. {
  231. // partly sent, move memory, wait for the next time to send
  232. server->buf_len -= s;
  233. server->buf_idx += s;
  234. return;
  235. }
  236. else
  237. {
  238. // all sent out, wait for reading
  239. server->buf_len = 0;
  240. server->buf_idx = 0;
  241. ev_io_stop(EV_A_ &server_send_ctx->io);
  242. if (remote != NULL)
  243. {
  244. ev_io_start(EV_A_ &remote->recv_ctx->io);
  245. }
  246. else
  247. {
  248. close_and_free_remote(EV_A_ remote);
  249. close_and_free_server(EV_A_ server);
  250. return;
  251. }
  252. }
  253. }
  254. }
  255. static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
  256. {
  257. struct remote_ctx *remote_ctx = (struct remote_ctx *) (((void*)watcher)
  258. - sizeof(ev_io));
  259. struct remote *remote = remote_ctx->remote;
  260. struct server *server = remote->server;
  261. LOGD("remote timeout");
  262. ev_timer_stop(EV_A_ watcher);
  263. if (server == NULL)
  264. {
  265. close_and_free_remote(EV_A_ remote);
  266. return;
  267. }
  268. close_and_free_remote(EV_A_ remote);
  269. close_and_free_server(EV_A_ server);
  270. }
  271. static void remote_recv_cb (EV_P_ ev_io *w, int revents)
  272. {
  273. struct remote_ctx *remote_recv_ctx = (struct remote_ctx *)w;
  274. struct remote *remote = remote_recv_ctx->remote;
  275. struct server *server = remote->server;
  276. if (server == NULL)
  277. {
  278. close_and_free_remote(EV_A_ remote);
  279. return;
  280. }
  281. ssize_t r = recv(remote->fd, server->buf, BUF_SIZE, 0);
  282. if (r == 0)
  283. {
  284. // connection closed
  285. server->buf_len = 0;
  286. server->buf_idx = 0;
  287. close_and_free_remote(EV_A_ remote);
  288. if (server != NULL)
  289. {
  290. ev_io_start(EV_A_ &server->send_ctx->io);
  291. }
  292. return;
  293. }
  294. else if(r < 0)
  295. {
  296. if (errno == EAGAIN || errno == EWOULDBLOCK)
  297. {
  298. // no data
  299. // continue to wait for recv
  300. return;
  301. }
  302. else
  303. {
  304. ERROR("remote recv");
  305. close_and_free_remote(EV_A_ remote);
  306. close_and_free_server(EV_A_ server);
  307. return;
  308. }
  309. }
  310. server->buf = ss_decrypt(BUF_SIZE, server->buf, &r, server->d_ctx);
  311. if (server->buf == NULL)
  312. {
  313. LOGE("invalid password or cipher");
  314. close_and_free_remote(EV_A_ remote);
  315. close_and_free_server(EV_A_ server);
  316. return;
  317. }
  318. int s = send(server->fd, server->buf, r, 0);
  319. if (s == -1)
  320. {
  321. if (errno == EAGAIN || errno == EWOULDBLOCK)
  322. {
  323. // no data, wait for send
  324. server->buf_len = r;
  325. server->buf_idx = 0;
  326. ev_io_stop(EV_A_ &remote_recv_ctx->io);
  327. ev_io_start(EV_A_ &server->send_ctx->io);
  328. return;
  329. }
  330. else
  331. {
  332. ERROR("send");
  333. close_and_free_remote(EV_A_ remote);
  334. close_and_free_server(EV_A_ server);
  335. return;
  336. }
  337. }
  338. else if (s < r)
  339. {
  340. server->buf_len = r - s;
  341. server->buf_idx = s;
  342. ev_io_stop(EV_A_ &remote_recv_ctx->io);
  343. ev_io_start(EV_A_ &server->send_ctx->io);
  344. return;
  345. }
  346. }
  347. static void remote_send_cb (EV_P_ ev_io *w, int revents)
  348. {
  349. struct remote_ctx *remote_send_ctx = (struct remote_ctx *)w;
  350. struct remote *remote = remote_send_ctx->remote;
  351. struct server *server = remote->server;
  352. if (!remote_send_ctx->connected)
  353. {
  354. struct sockaddr_storage addr;
  355. socklen_t len = sizeof addr;
  356. int r = getpeername(remote->fd, (struct sockaddr*)&addr, &len);
  357. if (r == 0)
  358. {
  359. remote_send_ctx->connected = 1;
  360. ev_io_stop(EV_A_ &remote_send_ctx->io);
  361. ev_timer_stop(EV_A_ &remote_send_ctx->watcher);
  362. // send destaddr
  363. char *ss_addr_to_send = malloc(BUF_SIZE);
  364. ssize_t addr_len = 0;
  365. ss_addr_t *sa = &server->destaddr;
  366. int host_len = strlen(sa->host);
  367. uint16_t port = htons(atoi(sa->port));
  368. // treat as domain
  369. ss_addr_to_send[addr_len++] = 3;
  370. ss_addr_to_send[addr_len++] = host_len;
  371. memcpy(ss_addr_to_send + addr_len, sa->host, host_len);
  372. addr_len += host_len;
  373. memcpy(ss_addr_to_send + addr_len, &port, 2);
  374. addr_len += 2;
  375. ss_addr_to_send = ss_encrypt(BUF_SIZE, ss_addr_to_send, &addr_len, server->e_ctx);
  376. if (ss_addr_to_send == NULL)
  377. {
  378. LOGE("invalid password or cipher");
  379. close_and_free_remote(EV_A_ remote);
  380. close_and_free_server(EV_A_ server);
  381. return;
  382. }
  383. int s = send(remote->fd, ss_addr_to_send, addr_len, 0);
  384. free(ss_addr_to_send);
  385. if (s < addr_len)
  386. {
  387. LOGE("failed to send remote addr.");
  388. close_and_free_remote(EV_A_ remote);
  389. close_and_free_server(EV_A_ server);
  390. }
  391. ev_io_start(EV_A_ &remote->recv_ctx->io);
  392. ev_io_start(EV_A_ &server->recv_ctx->io);
  393. return;
  394. }
  395. else
  396. {
  397. ERROR("getpeername");
  398. // not connected
  399. close_and_free_remote(EV_A_ remote);
  400. close_and_free_server(EV_A_ server);
  401. return;
  402. }
  403. }
  404. else
  405. {
  406. if (remote->buf_len == 0)
  407. {
  408. // close and free
  409. close_and_free_remote(EV_A_ remote);
  410. close_and_free_server(EV_A_ server);
  411. return;
  412. }
  413. else
  414. {
  415. // has data to send
  416. ssize_t s = send(remote->fd, remote->buf + remote->buf_idx,
  417. remote->buf_len, 0);
  418. if (s < 0)
  419. {
  420. if (errno != EAGAIN && errno != EWOULDBLOCK)
  421. {
  422. ERROR("send");
  423. // close and free
  424. close_and_free_remote(EV_A_ remote);
  425. close_and_free_server(EV_A_ server);
  426. }
  427. return;
  428. }
  429. else if (s < remote->buf_len)
  430. {
  431. // partly sent, move memory, wait for the next time to send
  432. remote->buf_len -= s;
  433. remote->buf_idx += s;
  434. return;
  435. }
  436. else
  437. {
  438. // all sent out, wait for reading
  439. remote->buf_len = 0;
  440. remote->buf_idx = 0;
  441. ev_io_stop(EV_A_ &remote_send_ctx->io);
  442. if (server != NULL)
  443. {
  444. ev_io_start(EV_A_ &server->recv_ctx->io);
  445. }
  446. else
  447. {
  448. close_and_free_remote(EV_A_ remote);
  449. close_and_free_server(EV_A_ server);
  450. return;
  451. }
  452. }
  453. }
  454. }
  455. }
  456. struct remote* new_remote(int fd, int timeout)
  457. {
  458. struct remote *remote;
  459. remote = malloc(sizeof(struct remote));
  460. remote->buf = malloc(BUF_SIZE);
  461. remote->recv_ctx = malloc(sizeof(struct remote_ctx));
  462. remote->send_ctx = malloc(sizeof(struct remote_ctx));
  463. remote->fd = fd;
  464. ev_io_init(&remote->recv_ctx->io, remote_recv_cb, fd, EV_READ);
  465. ev_io_init(&remote->send_ctx->io, remote_send_cb, fd, EV_WRITE);
  466. ev_timer_init(&remote->send_ctx->watcher, remote_timeout_cb, timeout, 0);
  467. remote->recv_ctx->remote = remote;
  468. remote->recv_ctx->connected = 0;
  469. remote->send_ctx->remote = remote;
  470. remote->send_ctx->connected = 0;
  471. remote->buf_len = 0;
  472. remote->buf_idx = 0;
  473. return remote;
  474. }
  475. static void free_remote(struct remote *remote)
  476. {
  477. if (remote != NULL)
  478. {
  479. if (remote->server != NULL)
  480. {
  481. remote->server->remote = NULL;
  482. }
  483. if (remote->buf)
  484. {
  485. free(remote->buf);
  486. }
  487. free(remote->recv_ctx);
  488. free(remote->send_ctx);
  489. free(remote);
  490. }
  491. }
  492. static void close_and_free_remote(EV_P_ struct remote *remote)
  493. {
  494. if (remote != NULL)
  495. {
  496. ev_timer_stop(EV_A_ &remote->send_ctx->watcher);
  497. ev_io_stop(EV_A_ &remote->send_ctx->io);
  498. ev_io_stop(EV_A_ &remote->recv_ctx->io);
  499. close(remote->fd);
  500. free_remote(remote);
  501. }
  502. }
  503. struct server* new_server(int fd, int method)
  504. {
  505. struct server *server;
  506. server = malloc(sizeof(struct server));
  507. server->buf = malloc(BUF_SIZE);
  508. server->recv_ctx = malloc(sizeof(struct server_ctx));
  509. server->send_ctx = malloc(sizeof(struct server_ctx));
  510. server->fd = fd;
  511. ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ);
  512. ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE);
  513. server->recv_ctx->server = server;
  514. server->recv_ctx->connected = 0;
  515. server->send_ctx->server = server;
  516. server->send_ctx->connected = 0;
  517. if (method)
  518. {
  519. server->e_ctx = malloc(sizeof(struct enc_ctx));
  520. server->d_ctx = malloc(sizeof(struct enc_ctx));
  521. enc_ctx_init(method, server->e_ctx, 1);
  522. enc_ctx_init(method, server->d_ctx, 0);
  523. }
  524. else
  525. {
  526. server->e_ctx = NULL;
  527. server->d_ctx = NULL;
  528. }
  529. server->buf_len = 0;
  530. server->buf_idx = 0;
  531. return server;
  532. }
  533. static void free_server(struct server *server)
  534. {
  535. if (server != NULL)
  536. {
  537. if (server->remote != NULL)
  538. {
  539. server->remote->server = NULL;
  540. }
  541. if (server->e_ctx != NULL)
  542. {
  543. cipher_context_release(&server->e_ctx->evp);
  544. free(server->e_ctx);
  545. }
  546. if (server->d_ctx != NULL)
  547. {
  548. cipher_context_release(&server->d_ctx->evp);
  549. free(server->d_ctx);
  550. }
  551. if (server->buf)
  552. {
  553. free(server->buf);
  554. }
  555. free(server->recv_ctx);
  556. free(server->send_ctx);
  557. free(server);
  558. }
  559. }
  560. static void close_and_free_server(EV_P_ struct server *server)
  561. {
  562. if (server != NULL)
  563. {
  564. ev_io_stop(EV_A_ &server->send_ctx->io);
  565. ev_io_stop(EV_A_ &server->recv_ctx->io);
  566. close(server->fd);
  567. free_server(server);
  568. }
  569. }
  570. static void accept_cb (EV_P_ ev_io *w, int revents)
  571. {
  572. struct listen_ctx *listener = (struct listen_ctx *)w;
  573. int serverfd = accept(listener->fd, NULL, NULL);
  574. if (serverfd == -1)
  575. {
  576. ERROR("accept");
  577. return;
  578. }
  579. setnonblocking(serverfd);
  580. int opt = 1;
  581. setsockopt(serverfd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
  582. #ifdef SO_NOSIGPIPE
  583. setsockopt(serverfd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
  584. #endif
  585. struct addrinfo hints, *res;
  586. int sockfd;
  587. memset(&hints, 0, sizeof hints);
  588. hints.ai_family = AF_UNSPEC;
  589. hints.ai_socktype = SOCK_STREAM;
  590. int index = rand() % listener->remote_num;
  591. if (verbose)
  592. {
  593. LOGD("connect to %s:%s", listener->remote_addr[index].host, listener->remote_addr[index].port);
  594. }
  595. int err = getaddrinfo(listener->remote_addr[index].host, listener->remote_addr[index].port, &hints, &res);
  596. if (err)
  597. {
  598. ERROR("getaddrinfo");
  599. return;
  600. }
  601. sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  602. if (sockfd < 0)
  603. {
  604. ERROR("socket");
  605. freeaddrinfo(res);
  606. return;
  607. }
  608. setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
  609. #ifdef SO_NOSIGPIPE
  610. setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
  611. #endif
  612. // Setup
  613. setnonblocking(sockfd);
  614. #ifdef SET_INTERFACE
  615. if (listener->iface) setinterface(sockfd, listener->iface);
  616. #endif
  617. struct server *server = new_server(serverfd, listener->method);
  618. struct remote *remote = new_remote(sockfd, listener->timeout);
  619. server->destaddr = listener->tunnel_addr;
  620. server->remote = remote;
  621. remote->server = server;
  622. connect(sockfd, res->ai_addr, res->ai_addrlen);
  623. freeaddrinfo(res);
  624. // listen to remote connected event
  625. ev_io_start(EV_A_ &remote->send_ctx->io);
  626. ev_timer_start(EV_A_ &remote->send_ctx->watcher);
  627. }
  628. int main (int argc, char **argv)
  629. {
  630. int i, c;
  631. int pid_flags = 0;
  632. char *user = NULL;
  633. char *local_port = NULL;
  634. char *local_addr = NULL;
  635. char *password = NULL;
  636. char *timeout = NULL;
  637. char *method = NULL;
  638. char *pid_path = NULL;
  639. char *conf_path = NULL;
  640. char *iface = NULL;
  641. int remote_num = 0;
  642. ss_addr_t remote_addr[MAX_REMOTE_NUM];
  643. char *remote_port = NULL;
  644. ss_addr_t tunnel_addr = {.host = NULL, .port = NULL};
  645. char *tunnel_addr_str = NULL;
  646. opterr = 0;
  647. while ((c = getopt (argc, argv, "f:s:p:l:k:t:m:i:c:b:L:a:uv")) != -1)
  648. {
  649. switch (c)
  650. {
  651. case 's':
  652. remote_addr[remote_num].host = optarg;
  653. remote_addr[remote_num++].port = NULL;
  654. break;
  655. case 'p':
  656. remote_port = optarg;
  657. break;
  658. case 'l':
  659. local_port = optarg;
  660. break;
  661. case 'k':
  662. password = optarg;
  663. break;
  664. case 'f':
  665. pid_flags = 1;
  666. pid_path = optarg;
  667. break;
  668. case 't':
  669. timeout = optarg;
  670. break;
  671. case 'm':
  672. method = optarg;
  673. break;
  674. case 'c':
  675. conf_path = optarg;
  676. break;
  677. case 'i':
  678. iface = optarg;
  679. break;
  680. case 'b':
  681. local_addr = optarg;
  682. break;
  683. case 'u':
  684. udprelay = 1;
  685. break;
  686. case 'L':
  687. tunnel_addr_str = optarg;
  688. break;
  689. case 'a':
  690. user = optarg;
  691. break;
  692. case 'v':
  693. verbose = 1;
  694. break;
  695. }
  696. }
  697. if (opterr)
  698. {
  699. usage();
  700. exit(EXIT_FAILURE);
  701. }
  702. if (conf_path != NULL)
  703. {
  704. jconf_t *conf = read_jconf(conf_path);
  705. if (remote_num == 0)
  706. {
  707. remote_num = conf->remote_num;
  708. for (i = 0; i < remote_num; i++)
  709. {
  710. remote_addr[i] = conf->remote_addr[i];
  711. }
  712. }
  713. if (remote_port == NULL) remote_port = conf->remote_port;
  714. if (local_addr == NULL) local_addr = conf->local_addr;
  715. if (local_port == NULL) local_port = conf->local_port;
  716. if (password == NULL) password = conf->password;
  717. if (method == NULL) method = conf->method;
  718. if (timeout == NULL) timeout = conf->timeout;
  719. }
  720. if (remote_num == 0 || remote_port == NULL || tunnel_addr_str == NULL ||
  721. local_port == NULL || password == NULL)
  722. {
  723. usage();
  724. exit(EXIT_FAILURE);
  725. }
  726. if (timeout == NULL) timeout = "10";
  727. if (local_addr == NULL) local_addr = "0.0.0.0";
  728. if (pid_flags)
  729. {
  730. USE_SYSLOG(argv[0]);
  731. daemonize(pid_path);
  732. }
  733. // parse tunnel addr
  734. parse_addr(tunnel_addr_str, &tunnel_addr);
  735. #ifdef __MINGW32__
  736. winsock_init();
  737. #else
  738. // ignore SIGPIPE
  739. signal(SIGPIPE, SIG_IGN);
  740. signal(SIGABRT, SIG_IGN);
  741. #endif
  742. // Setup keys
  743. LOGD("initialize ciphers... %s", method);
  744. int m = enc_init(password, method);
  745. // Setup socket
  746. int listenfd;
  747. listenfd = create_and_bind(local_addr, local_port);
  748. if (listenfd < 0)
  749. {
  750. FATAL("bind() error..");
  751. }
  752. if (listen(listenfd, SOMAXCONN) == -1)
  753. {
  754. FATAL("listen() error.");
  755. }
  756. setnonblocking(listenfd);
  757. LOGD("server listening at port %s.", local_port);
  758. // Setup proxy context
  759. struct listen_ctx listen_ctx;
  760. listen_ctx.tunnel_addr = tunnel_addr;
  761. listen_ctx.remote_num = remote_num;
  762. listen_ctx.remote_addr = malloc(sizeof(ss_addr_t) * remote_num);
  763. while (remote_num > 0)
  764. {
  765. int index = --remote_num;
  766. if (remote_addr[index].port == NULL) remote_addr[index].port = remote_port;
  767. listen_ctx.remote_addr[index] = remote_addr[index];
  768. }
  769. listen_ctx.timeout = atoi(timeout);
  770. listen_ctx.fd = listenfd;
  771. listen_ctx.iface = iface;
  772. listen_ctx.method = m;
  773. struct ev_loop *loop = ev_default_loop(0);
  774. if (!loop)
  775. {
  776. FATAL("ev_loop error.");
  777. }
  778. ev_io_init (&listen_ctx.io, accept_cb, listenfd, EV_READ);
  779. ev_io_start (loop, &listen_ctx.io);
  780. // Setup UDP
  781. if (udprelay)
  782. {
  783. LOGD("udprelay enabled.");
  784. udprelay_init(local_addr, local_port, remote_addr[0].host, remote_addr[0].port,
  785. tunnel_addr, m, listen_ctx.timeout, iface);
  786. }
  787. // setuid
  788. if (user != NULL)
  789. run_as(user);
  790. ev_run (loop, 0);
  791. #ifdef __MINGW32__
  792. winsock_cleanup();
  793. #endif
  794. return 0;
  795. }