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.

725 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 (remote == NULL) {
  229. LOGE("invalid server.");
  230. close_and_free_server(EV_A_ server);
  231. return;
  232. }
  233. if (server->buf_len == 0) {
  234. // close and free
  235. close_and_free_remote(EV_A_ remote);
  236. close_and_free_server(EV_A_ server);
  237. return;
  238. } else {
  239. // has data to send
  240. ssize_t s = send(server->fd, server->buf,
  241. server->buf_len, 0);
  242. if (s < 0) {
  243. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  244. perror("server_send_send");
  245. close_and_free_remote(EV_A_ remote);
  246. close_and_free_server(EV_A_ server);
  247. }
  248. return;
  249. } else if (s < server->buf_len) {
  250. // partly sent, move memory, wait for the next time to send
  251. char *pt = server->buf;
  252. char *et = pt + server->buf_len;
  253. while (pt + s < et) {
  254. *pt = *(pt + s);
  255. pt++;
  256. }
  257. server->buf_len -= s;
  258. assert(server->buf_len >= 0);
  259. return;
  260. } else {
  261. // all sent out, wait for reading
  262. server->buf_len = 0;
  263. ev_io_stop(EV_A_ &server_send_ctx->io);
  264. if (remote != NULL) {
  265. ev_io_start(EV_A_ &remote->recv_ctx->io);
  266. return;
  267. } else {
  268. LOGE("invalid remote.");
  269. close_and_free_remote(EV_A_ remote);
  270. close_and_free_server(EV_A_ server);
  271. return;
  272. }
  273. }
  274. }
  275. }
  276. static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents) {
  277. struct remote_ctx *remote_ctx = (struct remote_ctx *) (((void*)watcher)
  278. - sizeof(ev_io));
  279. struct remote *remote = remote_ctx->remote;
  280. struct server *server = remote->server;
  281. LOGE("remote timeout");
  282. ev_timer_stop(EV_A_ watcher);
  283. close_and_free_remote(EV_A_ remote);
  284. close_and_free_server(EV_A_ server);
  285. }
  286. static void remote_recv_cb (EV_P_ ev_io *w, int revents) {
  287. struct remote_ctx *remote_recv_ctx = (struct remote_ctx *)w;
  288. struct remote *remote = remote_recv_ctx->remote;
  289. struct server *server = remote->server;
  290. if (server == NULL) {
  291. LOGE("invalid server.");
  292. close_and_free_remote(EV_A_ remote);
  293. return;
  294. }
  295. ssize_t r = recv(remote->fd, server->buf, BUF_SIZE, 0);
  296. if (verbose) {
  297. LOGD("remote recv: %d byte", (int)r);
  298. }
  299. if (r == 0) {
  300. // connection closed
  301. close_and_free_remote(EV_A_ remote);
  302. if (server != NULL) {
  303. ev_io_start(EV_A_ &server->send_ctx->io);
  304. }
  305. return;
  306. } else if (r < 0) {
  307. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  308. // no data
  309. // continue to wait for recv
  310. return;
  311. } else {
  312. perror("remote recv");
  313. close_and_free_remote(EV_A_ remote);
  314. close_and_free_server(EV_A_ server);
  315. return;
  316. }
  317. }
  318. encrypt_ctx(server->buf, r, server->e_ctx);
  319. int s = send(server->fd, server->buf, r, 0);
  320. if (s == -1) {
  321. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  322. // no data, wait for send
  323. server->buf_len = r;
  324. ev_io_stop(EV_A_ &remote_recv_ctx->io);
  325. ev_io_start(EV_A_ &server->send_ctx->io);
  326. } else {
  327. perror("remote_recv_send");
  328. close_and_free_remote(EV_A_ remote);
  329. close_and_free_server(EV_A_ server);
  330. }
  331. return;
  332. } else if (s < r) {
  333. char *pt = server->buf;
  334. char *et = pt + r;
  335. while (pt + s < et) {
  336. *pt = *(pt + s);
  337. pt++;
  338. }
  339. server->buf_len = r - s;
  340. assert(server->buf_len >= 0);
  341. ev_io_stop(EV_A_ &remote_recv_ctx->io);
  342. ev_io_start(EV_A_ &server->send_ctx->io);
  343. return;
  344. }
  345. }
  346. static void remote_send_cb (EV_P_ ev_io *w, int revents) {
  347. struct remote_ctx *remote_send_ctx = (struct remote_ctx *)w;
  348. struct remote *remote = remote_send_ctx->remote;
  349. struct server *server = remote->server;
  350. if (server == NULL) {
  351. LOGE("invalid server.");
  352. close_and_free_remote(EV_A_ remote);
  353. return;
  354. }
  355. if (!remote_send_ctx->connected) {
  356. struct sockaddr_storage addr;
  357. socklen_t len = sizeof addr;
  358. int r = getpeername(remote->fd, (struct sockaddr*)&addr, &len);
  359. if (r == 0) {
  360. if (verbose) {
  361. LOGD("remote connected.");
  362. }
  363. remote_send_ctx->connected = 1;
  364. ev_timer_stop(EV_A_ &remote_send_ctx->watcher);
  365. ev_io_start(EV_A_ &remote->recv_ctx->io);
  366. server->stage = 5;
  367. if (remote->buf_len == 0) {
  368. ev_io_stop(EV_A_ &remote_send_ctx->io);
  369. ev_io_start(EV_A_ &server->recv_ctx->io);
  370. return;
  371. }
  372. } else {
  373. perror("getpeername");
  374. // not connected
  375. close_and_free_remote(EV_A_ remote);
  376. close_and_free_server(EV_A_ server);
  377. return;
  378. }
  379. }
  380. if (remote->buf_len == 0) {
  381. // close and free
  382. close_and_free_remote(EV_A_ remote);
  383. close_and_free_server(EV_A_ server);
  384. return;
  385. } else {
  386. // has data to send
  387. ssize_t s = send(remote->fd, remote->buf,
  388. remote->buf_len, 0);
  389. if (s < 0) {
  390. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  391. perror("remote_send_send");
  392. // close and free
  393. close_and_free_remote(EV_A_ remote);
  394. close_and_free_server(EV_A_ server);
  395. }
  396. return;
  397. } else if (s < remote->buf_len) {
  398. // partly sent, move memory, wait for the next time to send
  399. char *pt = remote->buf;
  400. char *et = pt + remote->buf_len;
  401. while (pt + s < et) {
  402. *pt = *(pt + s);
  403. pt++;
  404. }
  405. remote->buf_len -= s;
  406. assert(remote->buf_len >= 0);
  407. return;
  408. } else {
  409. // all sent out, wait for reading
  410. remote->buf_len = 0;
  411. ev_io_stop(EV_A_ &remote_send_ctx->io);
  412. if (server != NULL) {
  413. ev_io_start(EV_A_ &server->recv_ctx->io);
  414. } else {
  415. LOGE("invalid server.");
  416. close_and_free_remote(EV_A_ remote);
  417. close_and_free_server(EV_A_ server);
  418. }
  419. return;
  420. }
  421. }
  422. }
  423. struct remote* new_remote(int fd, int timeout) {
  424. remote_conn++;
  425. struct remote *remote;
  426. remote = malloc(sizeof(struct remote));
  427. remote->recv_ctx = malloc(sizeof(struct remote_ctx));
  428. remote->send_ctx = malloc(sizeof(struct remote_ctx));
  429. remote->fd = fd;
  430. ev_io_init(&remote->recv_ctx->io, remote_recv_cb, fd, EV_READ);
  431. ev_io_init(&remote->send_ctx->io, remote_send_cb, fd, EV_WRITE);
  432. ev_timer_init(&remote->send_ctx->watcher, remote_timeout_cb, timeout, 0);
  433. remote->recv_ctx->remote = remote;
  434. remote->recv_ctx->connected = 0;
  435. remote->send_ctx->remote = remote;
  436. remote->send_ctx->connected = 0;
  437. remote->buf_len = 0;
  438. remote->server = NULL;
  439. return remote;
  440. }
  441. void free_remote(struct remote *remote) {
  442. remote_conn--;
  443. if (remote != NULL) {
  444. if (remote->server != NULL) {
  445. remote->server->remote = NULL;
  446. }
  447. free(remote->recv_ctx);
  448. free(remote->send_ctx);
  449. free(remote);
  450. }
  451. }
  452. void close_and_free_remote(EV_P_ struct remote *remote) {
  453. if (remote != NULL) {
  454. ev_timer_stop(EV_A_ &remote->send_ctx->watcher);
  455. ev_io_stop(EV_A_ &remote->send_ctx->io);
  456. ev_io_stop(EV_A_ &remote->recv_ctx->io);
  457. close(remote->fd);
  458. free_remote(remote);
  459. }
  460. if (verbose) {
  461. LOGD("current remote connection: %d", remote_conn);
  462. }
  463. }
  464. struct server* new_server(int fd) {
  465. server_conn++;
  466. struct server *server;
  467. server = malloc(sizeof(struct server));
  468. server->recv_ctx = malloc(sizeof(struct server_ctx));
  469. server->send_ctx = malloc(sizeof(struct server_ctx));
  470. server->fd = fd;
  471. ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ);
  472. ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE);
  473. server->recv_ctx->server = server;
  474. server->recv_ctx->connected = 0;
  475. server->send_ctx->server = server;
  476. server->send_ctx->connected = 0;
  477. server->stage = 0;
  478. if (enc_conf.method == RC4) {
  479. server->e_ctx = malloc(sizeof(struct rc4_state));
  480. server->d_ctx = malloc(sizeof(struct rc4_state));
  481. enc_ctx_init(server->e_ctx, 1);
  482. enc_ctx_init(server->d_ctx, 0);
  483. } else {
  484. server->e_ctx = NULL;
  485. server->d_ctx = NULL;
  486. }
  487. server->buf_len = 0;
  488. server->remote = NULL;
  489. return server;
  490. }
  491. void free_server(struct server *server) {
  492. server_conn--;
  493. if (server != NULL) {
  494. if (server->remote != NULL) {
  495. server->remote->server = NULL;
  496. }
  497. if (enc_conf.method == RC4) {
  498. free(server->e_ctx);
  499. free(server->d_ctx);
  500. }
  501. free(server->recv_ctx);
  502. free(server->send_ctx);
  503. free(server);
  504. }
  505. }
  506. void close_and_free_server(EV_P_ struct server *server) {
  507. if (server != NULL) {
  508. ev_io_stop(EV_A_ &server->send_ctx->io);
  509. ev_io_stop(EV_A_ &server->recv_ctx->io);
  510. close(server->fd);
  511. free_server(server);
  512. }
  513. if (verbose) {
  514. LOGD("current server connection: %d", server_conn);
  515. }
  516. }
  517. static void accept_cb (EV_P_ ev_io *w, int revents) {
  518. struct listen_ctx *listener = (struct listen_ctx *)w;
  519. int serverfd = accept(listener->fd, NULL, NULL);
  520. if (serverfd == -1) {
  521. perror("accept");
  522. return;
  523. }
  524. setnonblocking(serverfd);
  525. if (verbose) {
  526. LOGD("accept a connection.");
  527. }
  528. struct server *server = new_server(serverfd);
  529. server->timeout = listener->timeout;
  530. ev_io_start(EV_A_ &server->recv_ctx->io);
  531. }
  532. int main (int argc, char **argv) {
  533. int i, c;
  534. int pid_flags = 0;
  535. char *password = NULL;
  536. char *timeout = NULL;
  537. char *method = NULL;
  538. char *pid_path = NULL;
  539. char *conf_path = NULL;
  540. int server_num = 0;
  541. char *server_host[MAX_REMOTE_NUM];
  542. char *server_port = NULL;
  543. opterr = 0;
  544. while ((c = getopt (argc, argv, "f:s:p:l:k:t:m:c:v")) != -1) {
  545. switch (c) {
  546. case 's':
  547. server_host[server_num++] = optarg;
  548. break;
  549. case 'p':
  550. server_port = optarg;
  551. break;
  552. case 'k':
  553. password = optarg;
  554. break;
  555. case 'f':
  556. pid_flags = 1;
  557. pid_path = optarg;
  558. break;
  559. case 't':
  560. timeout = optarg;
  561. break;
  562. case 'm':
  563. method = optarg;
  564. break;
  565. case 'c':
  566. conf_path = optarg;
  567. break;
  568. case 'v':
  569. verbose = 1;
  570. }
  571. }
  572. if (opterr) {
  573. usage();
  574. exit(EXIT_FAILURE);
  575. }
  576. if (conf_path != NULL) {
  577. jconf_t *conf = read_jconf(conf_path);
  578. if (server_num == 0) {
  579. server_num = conf->remote_num;
  580. for (i = 0; i < server_num; i++) {
  581. server_host[i] = conf->remote_host[i];
  582. }
  583. }
  584. if (server_port == NULL) server_port = conf->remote_port;
  585. if (password == NULL) password = conf->password;
  586. if (method == NULL) method = conf->method;
  587. if (timeout == NULL) timeout = conf->timeout;
  588. }
  589. if (server_num == 0 || server_port == NULL || password == NULL) {
  590. usage();
  591. exit(EXIT_FAILURE);
  592. }
  593. if (timeout == NULL) timeout = "60";
  594. if (pid_flags) {
  595. demonize(pid_path);
  596. }
  597. // ignore SIGPIPE
  598. signal(SIGPIPE, SIG_IGN);
  599. // Setup keys
  600. LOGD("calculating ciphers...");
  601. enc_conf_init(password, method);
  602. // Inilitialize ev loop
  603. struct ev_loop *loop = EV_DEFAULT;
  604. // bind to each interface
  605. while (server_num > 0) {
  606. int index = --server_num;
  607. const char* host = server_host[index];
  608. // Bind to port
  609. int listenfd;
  610. listenfd = create_and_bind(host, server_port);
  611. if (listenfd < 0) {
  612. FATAL("bind() error..");
  613. }
  614. if (listen(listenfd, SOMAXCONN) == -1) {
  615. FATAL("listen() error.");
  616. }
  617. setnonblocking(listenfd);
  618. LOGD("server listening at port %s.", server_port);
  619. // Setup proxy context
  620. struct listen_ctx listen_ctx;
  621. listen_ctx.timeout = timeout;
  622. listen_ctx.fd = listenfd;
  623. ev_io_init (&listen_ctx.io, accept_cb, listenfd, EV_READ);
  624. ev_io_start (loop, &listen_ctx.io);
  625. }
  626. // start ev loop
  627. ev_run (loop, 0);
  628. return 0;
  629. }