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.

712 lines
20 KiB

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