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.

687 lines
20 KiB

  1. #include <sys/socket.h>
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <arpa/inet.h>
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <locale.h>
  8. #include <netdb.h>
  9. #include <netinet/in.h>
  10. #include <netinet/tcp.h>
  11. #include <pthread.h>
  12. #include <signal.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <strings.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include <assert.h>
  20. #include "utils.h"
  21. #include "server.h"
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #ifndef EAGAIN
  26. #define EAGAIN EWOULDBLOCK
  27. #endif
  28. #ifndef EWOULDBLOCK
  29. #define EWOULDBLOCK EAGAIN
  30. #endif
  31. #define min(a,b) (((a)<(b))?(a):(b))
  32. static int verbose = 0;
  33. int setnonblocking(int fd) {
  34. int flags;
  35. if (-1 ==(flags = fcntl(fd, F_GETFL, 0)))
  36. flags = 0;
  37. return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  38. }
  39. int create_and_bind(const char *host, const char *port) {
  40. struct addrinfo hints;
  41. struct addrinfo *result, *rp;
  42. int s, listen_sock;
  43. memset(&hints, 0, sizeof(struct addrinfo));
  44. hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
  45. hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
  46. s = getaddrinfo(host, port, &hints, &result);
  47. if (s != 0) {
  48. LOGE("getaddrinfo: %s\n", gai_strerror(s));
  49. return -1;
  50. }
  51. for (rp = result; rp != NULL; rp = rp->ai_next) {
  52. listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  53. if (listen_sock == -1)
  54. continue;
  55. int opt = 1;
  56. int err = setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  57. if (err) {
  58. perror("setsocket");
  59. }
  60. s = bind(listen_sock, rp->ai_addr, rp->ai_addrlen);
  61. if (s == 0) {
  62. /* We managed to bind successfully! */
  63. break;
  64. } else {
  65. perror("bind");
  66. }
  67. close(listen_sock);
  68. }
  69. if (rp == NULL) {
  70. LOGE("Could not bind\n");
  71. return -1;
  72. }
  73. freeaddrinfo(result);
  74. return listen_sock;
  75. }
  76. struct remote *connect_to_remote(char *remote_host, char *remote_port, struct timeval timeout) {
  77. struct addrinfo hints, *res;
  78. int sockfd;
  79. memset(&hints, 0, sizeof hints);
  80. hints.ai_family = AF_UNSPEC;
  81. hints.ai_socktype = SOCK_STREAM;
  82. int err = getaddrinfo(remote_host, remote_port, &hints, &res);
  83. if (err) {
  84. perror("getaddrinfo");
  85. return NULL;
  86. }
  87. // initilize remote socks
  88. sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  89. if (sockfd < 0) {
  90. perror("socket");
  91. close(sockfd);
  92. freeaddrinfo(res);
  93. return NULL;
  94. }
  95. // setup remote socks
  96. err = setsockopt(sockfd, SOL_SOCKET,
  97. SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
  98. if (err) perror("setsockopt");
  99. err = setsockopt(sockfd, SOL_SOCKET,
  100. SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
  101. if (err) perror("setsockopt");
  102. setnonblocking(sockfd);
  103. struct remote *remote = new_remote(sockfd, timeout);
  104. connect(sockfd, res->ai_addr, res->ai_addrlen);
  105. // release addrinfo
  106. freeaddrinfo(res);
  107. return remote;
  108. }
  109. static void server_recv_cb (EV_P_ ev_io *w, int revents) {
  110. struct server_ctx *server_recv_ctx = (struct server_ctx *)w;
  111. struct server *server = server_recv_ctx->server;
  112. struct remote *remote = NULL;
  113. char *buf = server->buf;
  114. int *buf_len = &server->buf_len;
  115. if (server->stage == 5) {
  116. remote = server->remote;
  117. buf = remote->buf;
  118. buf_len = &remote->buf_len;
  119. }
  120. ssize_t r = recv(server->fd, buf, BUF_SIZE, 0);
  121. if (r == 0) {
  122. // connection closed
  123. *buf_len = 0;
  124. close_and_free_remote(EV_A_ remote);
  125. close_and_free_server(EV_A_ server);
  126. return;
  127. } else if(r < 0) {
  128. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  129. // no data
  130. // continue to wait for recv
  131. return;
  132. } else {
  133. perror("server recv");
  134. close_and_free_remote(EV_A_ remote);
  135. close_and_free_server(EV_A_ server);
  136. return;
  137. }
  138. }
  139. decrypt_ctx(buf, r, server->d_ctx);
  140. // handshake and transmit data
  141. if (server->stage == 5) {
  142. int w = send(remote->fd, remote->buf, r, 0);
  143. if(w == -1) {
  144. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  145. // no data, wait for send
  146. remote->buf_len = r;
  147. ev_io_stop(EV_A_ &server_recv_ctx->io);
  148. ev_io_start(EV_A_ &remote->send_ctx->io);
  149. return;
  150. } else {
  151. perror("server_recv_send");
  152. close_and_free_remote(EV_A_ remote);
  153. close_and_free_server(EV_A_ server);
  154. return;
  155. }
  156. } else if(w < r) {
  157. char *pt = remote->buf;
  158. char *et = pt + r;
  159. while (pt + w < et) {
  160. *pt = *(pt + w);
  161. pt++;
  162. }
  163. remote->buf_len = r - w;
  164. assert(remote->buf_len >= 0);
  165. ev_io_stop(EV_A_ &server_recv_ctx->io);
  166. ev_io_start(EV_A_ &remote->send_ctx->io);
  167. return;
  168. }
  169. } else if (server->stage == 0) {
  170. /*
  171. * Shadowsocks Protocol:
  172. *
  173. * +------+----------+----------+
  174. * | ATYP | DST.ADDR | DST.PORT |
  175. * +------+----------+----------+
  176. * | 1 | Variable | 2 |
  177. * +------+----------+----------+
  178. */
  179. int offset = 0;
  180. char atyp = server->buf[offset++];
  181. char host[256];
  182. memset(host, 0, 256);
  183. int port = 0;
  184. // get remote addr and port
  185. if (atyp == 1) {
  186. // IP V4
  187. size_t in_addr_len = sizeof(struct in_addr);
  188. char *a = inet_ntoa(*(struct in_addr*)(server->buf + offset));
  189. memcpy(host, a, strlen(a));
  190. offset += in_addr_len;
  191. } else if (atyp == 3) {
  192. // Domain name
  193. uint8_t name_len = *(uint8_t *)(server->buf + offset);
  194. memcpy(host, server->buf + offset + 1, name_len);
  195. offset += name_len + 1;
  196. } else {
  197. LOGE("unsupported addrtype: %d\n", atyp);
  198. close_and_free_remote(EV_A_ remote);
  199. close_and_free_server(EV_A_ server);
  200. return;
  201. }
  202. port += *(uint8_t *)(server->buf + offset++) << 8;
  203. port += *(uint8_t *)(server->buf + offset);
  204. if (verbose) {
  205. LOGD("connect to: %s:%s\n", host, itoa(port));
  206. }
  207. struct remote *remote = connect_to_remote(host, itoa(port), server->timeout);
  208. if (remote == NULL) {
  209. close_and_free_server(EV_A_ server);
  210. return;
  211. }
  212. // listen to remote connected event
  213. ev_io_stop(EV_A_ &server->recv_ctx->io);
  214. ev_io_start(EV_A_ &remote->send_ctx->io);
  215. ev_timer_start(EV_A_ &remote->send_ctx->watcher);
  216. remote->server = server;
  217. server->remote = remote;
  218. server->stage = 5;
  219. }
  220. }
  221. static void server_send_cb (EV_P_ ev_io *w, int revents) {
  222. struct server_ctx *server_send_ctx = (struct server_ctx *)w;
  223. struct server *server = server_send_ctx->server;
  224. struct remote *remote = server->remote;
  225. if (server->buf_len == 0) {
  226. // close and free
  227. close_and_free_remote(EV_A_ remote);
  228. close_and_free_server(EV_A_ server);
  229. return;
  230. } else {
  231. // has data to send
  232. ssize_t r = send(server->fd, server->buf,
  233. server->buf_len, 0);
  234. if (r < 0) {
  235. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  236. perror("server_send_send");
  237. close_and_free_remote(EV_A_ remote);
  238. close_and_free_server(EV_A_ server);
  239. }
  240. return;
  241. }
  242. if (r < server->buf_len) {
  243. // partly sent, move memory, wait for the next time to send
  244. char *pt = server->buf;
  245. char *et = pt + server->buf_len;
  246. while (pt + r < et) {
  247. *pt = *(pt + r);
  248. pt++;
  249. }
  250. server->buf_len -= r;
  251. assert(server->buf_len >= 0);
  252. return;
  253. } else {
  254. // all sent out, wait for reading
  255. ev_io_stop(EV_A_ &server_send_ctx->io);
  256. if (remote != NULL) {
  257. ev_io_start(EV_A_ &remote->recv_ctx->io);
  258. } else {
  259. close_and_free_remote(EV_A_ remote);
  260. close_and_free_server(EV_A_ server);
  261. return;
  262. }
  263. }
  264. }
  265. }
  266. static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents) {
  267. struct remote_ctx *remote_ctx = (struct remote_ctx *) (((void*)watcher)
  268. - sizeof(ev_io));
  269. struct remote *remote = remote_ctx->remote;
  270. struct server *server = remote->server;
  271. LOGE("remote timeout\n");
  272. ev_timer_stop(EV_A_ watcher);
  273. if (server == NULL) {
  274. close_and_free_remote(EV_A_ remote);
  275. return;
  276. }
  277. close_and_free_remote(EV_A_ remote);
  278. close_and_free_server(EV_A_ server);
  279. }
  280. static void remote_recv_cb (EV_P_ ev_io *w, int revents) {
  281. struct remote_ctx *remote_recv_ctx = (struct remote_ctx *)w;
  282. struct remote *remote = remote_recv_ctx->remote;
  283. struct server *server = remote->server;
  284. if (server == NULL) {
  285. close_and_free_remote(EV_A_ remote);
  286. return;
  287. }
  288. while (1) {
  289. ssize_t r = recv(remote->fd, server->buf, BUF_SIZE, 0);
  290. if (r == 0) {
  291. // connection closed
  292. server->buf_len = 0;
  293. close_and_free_remote(EV_A_ remote);
  294. close_and_free_server(EV_A_ server);
  295. return;
  296. } else if(r < 0) {
  297. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  298. // no data
  299. // continue to wait for recv
  300. break;
  301. } else {
  302. perror("remote recv");
  303. close_and_free_remote(EV_A_ remote);
  304. close_and_free_server(EV_A_ server);
  305. return;
  306. }
  307. }
  308. encrypt_ctx(server->buf, r, server->e_ctx);
  309. int w = send(server->fd, server->buf, r, 0);
  310. if(w == -1) {
  311. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  312. // no data, wait for send
  313. server->buf_len = r;
  314. ev_io_stop(EV_A_ &remote_recv_ctx->io);
  315. ev_io_start(EV_A_ &server->send_ctx->io);
  316. break;
  317. } else {
  318. perror("remote_recv_send");
  319. close_and_free_remote(EV_A_ remote);
  320. close_and_free_server(EV_A_ server);
  321. return;
  322. }
  323. } else if(w < r) {
  324. char *pt = server->buf;
  325. char *et = pt + r;
  326. while (pt + w < et) {
  327. *pt = *(pt + w);
  328. pt++;
  329. }
  330. server->buf_len = r - w;
  331. assert(server->buf_len >= 0);
  332. ev_io_stop(EV_A_ &remote_recv_ctx->io);
  333. ev_io_start(EV_A_ &server->send_ctx->io);
  334. break;
  335. }
  336. }
  337. }
  338. static void remote_send_cb (EV_P_ ev_io *w, int revents) {
  339. struct remote_ctx *remote_send_ctx = (struct remote_ctx *)w;
  340. struct remote *remote = remote_send_ctx->remote;
  341. struct server *server = remote->server;
  342. if (!remote_send_ctx->connected) {
  343. struct sockaddr_storage addr;
  344. socklen_t len = sizeof addr;
  345. int r = getpeername(remote->fd, (struct sockaddr*)&addr, &len);
  346. if (r == 0) {
  347. remote_send_ctx->connected = 1;
  348. ev_io_stop(EV_A_ &remote_send_ctx->io);
  349. ev_timer_stop(EV_A_ &remote_send_ctx->watcher);
  350. ev_io_start(EV_A_ &server->recv_ctx->io);
  351. ev_io_start(EV_A_ &remote->recv_ctx->io);
  352. return;
  353. } else {
  354. perror("getpeername");
  355. // not connected
  356. close_and_free_remote(EV_A_ remote);
  357. close_and_free_server(EV_A_ server);
  358. return;
  359. }
  360. } else {
  361. if (remote->buf_len == 0) {
  362. // close and free
  363. close_and_free_remote(EV_A_ remote);
  364. close_and_free_server(EV_A_ server);
  365. return;
  366. } else {
  367. // has data to send
  368. ssize_t r = send(remote->fd, remote->buf,
  369. remote->buf_len, 0);
  370. if (r < 0) {
  371. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  372. perror("remote_send_send");
  373. // close and free
  374. close_and_free_remote(EV_A_ remote);
  375. close_and_free_server(EV_A_ server);
  376. }
  377. return;
  378. }
  379. if (r < remote->buf_len) {
  380. // partly sent, move memory, wait for the next time to send
  381. char *pt = remote->buf;
  382. char *et = pt + remote->buf_len;
  383. while (pt + r < et) {
  384. *pt = *(pt + r);
  385. pt++;
  386. }
  387. remote->buf_len -= r;
  388. assert(remote->buf_len >= 0);
  389. return;
  390. } else {
  391. // all sent out, wait for reading
  392. ev_io_stop(EV_A_ &remote_send_ctx->io);
  393. if (server != NULL) {
  394. ev_io_start(EV_A_ &server->recv_ctx->io);
  395. } else {
  396. close_and_free_remote(EV_A_ remote);
  397. close_and_free_server(EV_A_ server);
  398. return;
  399. }
  400. }
  401. }
  402. }
  403. }
  404. struct remote* new_remote(int fd, struct timeval timeout) {
  405. struct remote *remote;
  406. remote = malloc(sizeof(struct remote));
  407. remote->recv_ctx = malloc(sizeof(struct remote_ctx));
  408. remote->send_ctx = malloc(sizeof(struct remote_ctx));
  409. remote->fd = fd;
  410. ev_io_init(&remote->recv_ctx->io, remote_recv_cb, fd, EV_READ);
  411. ev_io_init(&remote->send_ctx->io, remote_send_cb, fd, EV_WRITE);
  412. ev_timer_init(&remote->send_ctx->watcher, remote_timeout_cb, timeout.tv_sec, 0);
  413. remote->recv_ctx->remote = remote;
  414. remote->recv_ctx->connected = 0;
  415. remote->send_ctx->remote = remote;
  416. remote->send_ctx->connected = 0;
  417. remote->buf_len = 0;
  418. return remote;
  419. }
  420. void free_remote(struct remote *remote) {
  421. if (remote != NULL) {
  422. if (remote->server != NULL) {
  423. remote->server->remote = NULL;
  424. }
  425. free(remote->recv_ctx);
  426. free(remote->send_ctx);
  427. free(remote);
  428. }
  429. }
  430. void close_and_free_remote(EV_P_ struct remote *remote) {
  431. if (remote != NULL) {
  432. ev_timer_stop(EV_A_ &remote->send_ctx->watcher);
  433. ev_io_stop(EV_A_ &remote->send_ctx->io);
  434. ev_io_stop(EV_A_ &remote->recv_ctx->io);
  435. close(remote->fd);
  436. free_remote(remote);
  437. }
  438. }
  439. struct server* new_server(int fd) {
  440. struct server *server;
  441. server = malloc(sizeof(struct server));
  442. server->recv_ctx = malloc(sizeof(struct server_ctx));
  443. server->send_ctx = malloc(sizeof(struct server_ctx));
  444. server->fd = fd;
  445. ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ);
  446. ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE);
  447. server->recv_ctx->server = server;
  448. server->recv_ctx->connected = 0;
  449. server->send_ctx->server = server;
  450. server->send_ctx->connected = 0;
  451. server->stage = 0;
  452. if (enc_conf.method == RC4) {
  453. server->e_ctx = malloc(sizeof(struct rc4_state));
  454. server->d_ctx = malloc(sizeof(struct rc4_state));
  455. enc_ctx_init(server->e_ctx, 1);
  456. enc_ctx_init(server->d_ctx, 0);
  457. } else {
  458. server->e_ctx = NULL;
  459. server->d_ctx = NULL;
  460. }
  461. server->buf_len = 0;
  462. return server;
  463. }
  464. void free_server(struct server *server) {
  465. if (server != NULL) {
  466. if (server->remote != NULL) {
  467. server->remote->server = NULL;
  468. }
  469. if (enc_conf.method == RC4) {
  470. free(server->e_ctx);
  471. free(server->d_ctx);
  472. }
  473. free(server->recv_ctx);
  474. free(server->send_ctx);
  475. free(server);
  476. }
  477. }
  478. void close_and_free_server(EV_P_ struct server *server) {
  479. if (server != NULL) {
  480. ev_io_stop(EV_A_ &server->send_ctx->io);
  481. ev_io_stop(EV_A_ &server->recv_ctx->io);
  482. close(server->fd);
  483. free_server(server);
  484. }
  485. }
  486. static void accept_cb (EV_P_ ev_io *w, int revents) {
  487. struct listen_ctx *listener = (struct listen_ctx *)w;
  488. int serverfd = accept(listener->fd, NULL, NULL);
  489. if (serverfd == -1) {
  490. perror("accept");
  491. return;
  492. }
  493. setnonblocking(serverfd);
  494. if (verbose) {
  495. LOGD("Accept a connection.\n");
  496. }
  497. struct server *server = new_server(serverfd);
  498. server->timeout = listener->timeout;
  499. ev_io_start(EV_A_ &server->recv_ctx->io);
  500. }
  501. int main (int argc, char **argv) {
  502. int i, c;
  503. int pid_flags = 0;
  504. char *password = NULL;
  505. char *timeout = NULL;
  506. char *method = NULL;
  507. char *pid_path = NULL;
  508. char *conf_path = NULL;
  509. int server_num = 0;
  510. char *server_host[MAX_REMOTE_NUM];
  511. char *server_port = NULL;
  512. opterr = 0;
  513. while ((c = getopt (argc, argv, "f:s:p:l:k:t:m:c:v")) != -1) {
  514. switch (c) {
  515. case 's':
  516. server_host[server_num++] = optarg;
  517. break;
  518. case 'p':
  519. server_port = optarg;
  520. break;
  521. case 'k':
  522. password = optarg;
  523. break;
  524. case 'f':
  525. pid_flags = 1;
  526. pid_path = optarg;
  527. break;
  528. case 't':
  529. timeout = optarg;
  530. break;
  531. case 'm':
  532. method = optarg;
  533. break;
  534. case 'c':
  535. conf_path = optarg;
  536. break;
  537. case 'v':
  538. verbose = 1;
  539. }
  540. }
  541. if (opterr) {
  542. usage();
  543. exit(EXIT_FAILURE);
  544. }
  545. if (conf_path != NULL) {
  546. jconf_t *conf = read_jconf(conf_path);
  547. if (server_num == 0) {
  548. server_num = conf->remote_num;
  549. for (i = 0; i < server_num; i++) {
  550. server_host[i] = conf->remote_host[i];
  551. }
  552. }
  553. if (server_port == NULL) server_port = conf->remote_port;
  554. if (password == NULL) password = conf->password;
  555. if (method == NULL) method = conf->method;
  556. if (timeout == NULL) timeout = conf->timeout;
  557. }
  558. if (server_num == 0 || server_port == NULL || password == NULL) {
  559. usage();
  560. exit(EXIT_FAILURE);
  561. }
  562. if (timeout == NULL) timeout = "60";
  563. if (pid_flags) {
  564. demonize(pid_path);
  565. }
  566. // ignore SIGPIPE
  567. signal(SIGPIPE, SIG_IGN);
  568. // Setup keys
  569. LOGD("calculating ciphers...\n");
  570. enc_conf_init(password, method);
  571. // Inilitialize ev loop
  572. struct ev_loop *loop = ev_default_loop(0);
  573. if (!loop) {
  574. FATAL("ev_loop error.\n");
  575. }
  576. // bind to each interface
  577. while (server_num > 0) {
  578. int index = --server_num;
  579. const char* host = server_host[index];
  580. // Bind to port
  581. int listenfd;
  582. listenfd = create_and_bind(host, server_port);
  583. if (listenfd < 0) {
  584. FATAL("bind() error..\n");
  585. }
  586. if (listen(listenfd, SOMAXCONN) == -1) {
  587. FATAL("listen() error.\n");
  588. }
  589. setnonblocking(listenfd);
  590. LOGD("server listening at port %s.\n", server_port);
  591. // Setup proxy context
  592. struct listen_ctx listen_ctx;
  593. struct timeval time;
  594. time.tv_sec = timeout;
  595. time.tv_usec = 0;
  596. listen_ctx.timeout = time;
  597. listen_ctx.fd = listenfd;
  598. ev_io_init (&listen_ctx.io, accept_cb, listenfd, EV_READ);
  599. ev_io_start (loop, &listen_ctx.io);
  600. }
  601. // start ev loop
  602. ev_run (loop, 0);
  603. return 0;
  604. }