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.

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