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.

839 lines
22 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. /*
  2. * server.c - Provide shadowsocks service
  3. *
  4. * Copyright (C) 2013 - 2015, 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 shadowsocks-libev; see the file COPYING. If not, see
  20. * <http://www.gnu.org/licenses/>.
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <fcntl.h>
  28. #include <locale.h>
  29. #include <signal.h>
  30. #include <string.h>
  31. #include <strings.h>
  32. #include <time.h>
  33. #include <unistd.h>
  34. #include <getopt.h>
  35. #include <math.h>
  36. #include <ctype.h>
  37. #include <limits.h>
  38. #ifndef __MINGW32__
  39. #include <netdb.h>
  40. #include <errno.h>
  41. #include <arpa/inet.h>
  42. #include <netdb.h>
  43. #include <netinet/in.h>
  44. #include <netinet/tcp.h>
  45. #include <pthread.h>
  46. #include <sys/un.h>
  47. #include <sys/socket.h>
  48. #include <pwd.h>
  49. #endif
  50. #include <libcork/core.h>
  51. #ifdef __MINGW32__
  52. #include "win32.h"
  53. #endif
  54. #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H) && defined(__linux__)
  55. #include <net/if.h>
  56. #include <sys/ioctl.h>
  57. #define SET_INTERFACE
  58. #endif
  59. #include "json.h"
  60. #include "utils.h"
  61. #include "manager.h"
  62. #ifndef BUF_SIZE
  63. #define BUF_SIZE 65535
  64. #endif
  65. int verbose = 0;
  66. char *executable = "ss-server";
  67. char working_dir[PATH_MAX];
  68. static struct cork_hash_table *server_table;
  69. #ifndef __MINGW32__
  70. static int setnonblocking(int fd)
  71. {
  72. int flags;
  73. if (-1 == (flags = fcntl(fd, F_GETFL, 0))) {
  74. flags = 0;
  75. }
  76. return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  77. }
  78. #endif
  79. static void build_config(char *prefix, struct server *server)
  80. {
  81. char path[PATH_MAX];
  82. snprintf(path, PATH_MAX, "%s/.shadowsocks_%s.conf", prefix, server->port);
  83. FILE *f = fopen(path, "w+");
  84. if (f == NULL) {
  85. if (verbose) {
  86. LOGE("unable to open config file");
  87. }
  88. return;
  89. }
  90. fprintf(f, "{\n");
  91. fprintf(f, "\"server_port\":\"%s\",\n", server->port);
  92. fprintf(f, "\"password\":\"%s\",\n", server->password);
  93. fprintf(f, "}\n");
  94. fclose(f);
  95. }
  96. static char *construct_command_line(struct manager_ctx *manager, struct server *server)
  97. {
  98. static char cmd[BUF_SIZE];
  99. int i;
  100. build_config(working_dir, server);
  101. memset(cmd, 0, BUF_SIZE);
  102. snprintf(cmd, BUF_SIZE,
  103. "%s -m %s --manager-address %s -f %s/.shadowsocks_%s.pid -c %s/.shadowsocks_%s.conf",
  104. executable, manager->method, manager->manager_address,
  105. working_dir, server->port, working_dir, server->port);
  106. if (manager->acl != NULL) {
  107. int len = strlen(cmd);
  108. snprintf(cmd + len, BUF_SIZE - len, " --acl %s", manager->acl);
  109. }
  110. if (manager->timeout != NULL) {
  111. int len = strlen(cmd);
  112. snprintf(cmd + len, BUF_SIZE - len, " -t %s", manager->timeout);
  113. }
  114. if (manager->user != NULL) {
  115. int len = strlen(cmd);
  116. snprintf(cmd + len, BUF_SIZE - len, " -a %s", manager->user);
  117. }
  118. if (manager->verbose) {
  119. int len = strlen(cmd);
  120. snprintf(cmd + len, BUF_SIZE - len, " -v");
  121. }
  122. if (manager->mode == UDP_ONLY) {
  123. int len = strlen(cmd);
  124. snprintf(cmd + len, BUF_SIZE - len, " -U");
  125. }
  126. if (manager->mode == TCP_AND_UDP) {
  127. int len = strlen(cmd);
  128. snprintf(cmd + len, BUF_SIZE - len, " -u");
  129. }
  130. if (manager->mode == TCP_AND_UDP) {
  131. int len = strlen(cmd);
  132. snprintf(cmd + len, BUF_SIZE - len, " -u");
  133. }
  134. if (manager->auth) {
  135. int len = strlen(cmd);
  136. snprintf(cmd + len, BUF_SIZE - len, " -A");
  137. }
  138. if (manager->fast_open) {
  139. int len = strlen(cmd);
  140. snprintf(cmd + len, BUF_SIZE - len, " --fast_open");
  141. }
  142. for (i = 0; i < manager->nameserver_num; i++) {
  143. int len = strlen(cmd);
  144. snprintf(cmd + len, BUF_SIZE - len, " -d %s", manager->nameservers[i]);
  145. }
  146. for (i = 0; i < manager->host_num; i++) {
  147. int len = strlen(cmd);
  148. snprintf(cmd + len, BUF_SIZE - len, " -s %s", manager->hosts[i]);
  149. }
  150. if (verbose) {
  151. LOGI("cmd: %s", cmd);
  152. }
  153. return cmd;
  154. }
  155. static char *get_data(char *buf, int len)
  156. {
  157. char *data;
  158. int pos = 0;
  159. while (buf[pos] != '{' && pos < len)
  160. pos++;
  161. if (pos == len) {
  162. return NULL;
  163. }
  164. data = buf + pos - 1;
  165. return data;
  166. }
  167. static char *get_action(char *buf, int len)
  168. {
  169. char *action;
  170. int pos = 0;
  171. while (isspace((unsigned char)buf[pos]) && pos < len)
  172. pos++;
  173. if (pos == len) {
  174. return NULL;
  175. }
  176. action = buf + pos;
  177. while ((!isspace((unsigned char)buf[pos]) && buf[pos] != ':') && pos < len)
  178. pos++;
  179. buf[pos] = '\0';
  180. return action;
  181. }
  182. static struct server *get_server(char *buf, int len)
  183. {
  184. char *data = get_data(buf, len);
  185. char error_buf[512];
  186. struct server *server = (struct server *)malloc(sizeof(struct server));
  187. if (data == NULL) {
  188. LOGE("No data found");
  189. return NULL;
  190. }
  191. memset(server, 0, sizeof(struct server));
  192. json_settings settings = { 0 };
  193. json_value *obj = json_parse_ex(&settings, data, strlen(data), error_buf);
  194. if (obj == NULL) {
  195. LOGE("%s", error_buf);
  196. return NULL;
  197. }
  198. if (obj->type == json_object) {
  199. int i = 0;
  200. for (i = 0; i < obj->u.object.length; i++) {
  201. char *name = obj->u.object.values[i].name;
  202. json_value *value = obj->u.object.values[i].value;
  203. if (strcmp(name, "server_port") == 0) {
  204. if (value->type == json_string) {
  205. strncpy(server->port, value->u.string.ptr, 8);
  206. } else if (value->type == json_integer) {
  207. snprintf(server->port, 8, "%" PRIu64 "", value->u.integer);
  208. }
  209. } else if (strcmp(name, "password") == 0) {
  210. if (value->type == json_string) {
  211. strncpy(server->password, value->u.string.ptr, 128);
  212. }
  213. } else {
  214. LOGE("invalid data: %s", data);
  215. json_value_free(obj);
  216. return NULL;
  217. }
  218. }
  219. }
  220. json_value_free(obj);
  221. return server;
  222. }
  223. static int parse_traffic(char *buf, int len, char *port, uint64_t *traffic)
  224. {
  225. char *data = get_data(buf, len);
  226. char error_buf[512];
  227. json_settings settings = { 0 };
  228. if (data == NULL) {
  229. LOGE("No data found");
  230. return -1;
  231. }
  232. json_value *obj = json_parse_ex(&settings, data, strlen(data), error_buf);
  233. if (obj == NULL) {
  234. LOGE("%s", error_buf);
  235. return -1;
  236. }
  237. if (obj->type == json_object) {
  238. int i = 0;
  239. for (i = 0; i < obj->u.object.length; i++) {
  240. char *name = obj->u.object.values[i].name;
  241. json_value *value = obj->u.object.values[i].value;
  242. if (value->type == json_integer) {
  243. strncpy(port, name, 8);
  244. *traffic = value->u.integer;
  245. }
  246. }
  247. }
  248. json_value_free(obj);
  249. return 0;
  250. }
  251. static void add_server(struct manager_ctx *manager, struct server *server)
  252. {
  253. bool new = false;
  254. cork_hash_table_put(server_table, (void *)server->port, (void *)server, &new, NULL, NULL);
  255. char *cmd = construct_command_line(manager, server);
  256. if (system(cmd) == -1) {
  257. ERROR("add_server_system");
  258. }
  259. }
  260. static void stop_server(char *prefix, char *port)
  261. {
  262. char path[PATH_MAX];
  263. int pid;
  264. snprintf(path, PATH_MAX, "%s/.shadowsocks_%s.pid", prefix, port);
  265. FILE *f = fopen(path, "r");
  266. if (f == NULL) {
  267. if (verbose) {
  268. LOGE("unable to open pid file");
  269. }
  270. return;
  271. }
  272. if (fscanf(f, "%d", &pid) != EOF) {
  273. kill(pid, SIGTERM);
  274. }
  275. fclose(f);
  276. }
  277. static void remove_server(char *prefix, char *port)
  278. {
  279. char *old_port = NULL;
  280. struct server *old_server = NULL;
  281. cork_hash_table_delete(server_table, (void *)port, (void **)&old_port, (void **)&old_server);
  282. if (old_server != NULL) {
  283. free(old_server);
  284. }
  285. stop_server(prefix, port);
  286. }
  287. static void update_stat(char *port, uint64_t traffic)
  288. {
  289. void *ret = cork_hash_table_get(server_table, (void *)port);
  290. if (ret != NULL) {
  291. struct server *server = (struct server *)ret;
  292. server->traffic = traffic;
  293. }
  294. }
  295. static void manager_recv_cb(EV_P_ ev_io *w, int revents)
  296. {
  297. struct manager_ctx *manager = (struct manager_ctx *)w;
  298. socklen_t len;
  299. size_t r;
  300. struct sockaddr_un claddr;
  301. char buf[BUF_SIZE];
  302. memset(buf, 0, BUF_SIZE);
  303. len = sizeof(struct sockaddr_un);
  304. r = recvfrom(manager->fd, buf, BUF_SIZE, 0, (struct sockaddr *)&claddr, &len);
  305. if (r == -1) {
  306. ERROR("manager_recvfrom");
  307. return;
  308. }
  309. if (r > BUF_SIZE / 2) {
  310. LOGE("too large request: %d", (int)r);
  311. return;
  312. }
  313. char *action = get_action(buf, r);
  314. if (strcmp(action, "add") == 0) {
  315. struct server *server = get_server(buf, r);
  316. if (server == NULL || server->port[0] == 0 || server->password[0] == 0) {
  317. LOGE("invalid command: %s:%s", buf, get_data(buf, r));
  318. if (server != NULL) {
  319. free(server);
  320. }
  321. goto ERROR_MSG;
  322. }
  323. remove_server(working_dir, server->port);
  324. add_server(manager, server);
  325. char msg[3] = "ok";
  326. if (sendto(manager->fd, msg, 3, 0, (struct sockaddr *)&claddr, len) != 3) {
  327. ERROR("add_sendto");
  328. }
  329. } else if (strcmp(action, "remove") == 0) {
  330. struct server *server = get_server(buf, r);
  331. if (server == NULL || server->port[0] == 0) {
  332. LOGE("invalid command: %s:%s", buf, get_data(buf, r));
  333. if (server != NULL) {
  334. free(server);
  335. }
  336. goto ERROR_MSG;
  337. }
  338. remove_server(working_dir, server->port);
  339. free(server);
  340. char msg[3] = "ok";
  341. if (sendto(manager->fd, msg, 3, 0, (struct sockaddr *)&claddr, len) != 3) {
  342. ERROR("remove_sendto");
  343. }
  344. } else if (strcmp(action, "stat") == 0) {
  345. char port[8];
  346. uint64_t traffic = 0;
  347. if (parse_traffic(buf, r, port, &traffic) == -1) {
  348. LOGE("invalid command: %s:%s", buf, get_data(buf, r));
  349. return;
  350. }
  351. update_stat(port, traffic);
  352. } else if (strcmp(action, "ping") == 0) {
  353. struct cork_hash_table_entry *entry;
  354. struct cork_hash_table_iterator server_iter;
  355. char buf[BUF_SIZE];
  356. memset(buf, 0, BUF_SIZE);
  357. sprintf(buf, "stat: {");
  358. cork_hash_table_iterator_init(server_table, &server_iter);
  359. while ((entry = cork_hash_table_iterator_next(&server_iter)) != NULL) {
  360. struct server *server = (struct server *)entry->value;
  361. size_t pos = strlen(buf);
  362. if (pos > BUF_SIZE / 2) {
  363. buf[pos - 1] = '}';
  364. if (sendto(manager->fd, buf, pos + 1, 0, (struct sockaddr *)&claddr, len)
  365. != pos + 1) {
  366. ERROR("ping_sendto");
  367. }
  368. memset(buf, 0, BUF_SIZE);
  369. } else {
  370. sprintf(buf + pos, "\"%s\":%" PRIu64 ",", server->port, server->traffic);
  371. }
  372. }
  373. size_t pos = strlen(buf);
  374. if (pos > 7) {
  375. buf[pos - 1] = '}';
  376. } else {
  377. buf[pos] = '}';
  378. }
  379. if (sendto(manager->fd, buf, pos + 1, 0, (struct sockaddr *)&claddr, len)
  380. != pos + 1) {
  381. ERROR("ping_sendto");
  382. }
  383. }
  384. return;
  385. ERROR_MSG:
  386. strcpy(buf, "err");
  387. if (sendto(manager->fd, buf, 4, 0, (struct sockaddr *)&claddr, len) != 4) {
  388. ERROR("error_sendto");
  389. }
  390. }
  391. static void signal_cb(EV_P_ ev_signal *w, int revents)
  392. {
  393. if (revents & EV_SIGNAL) {
  394. switch (w->signum) {
  395. case SIGINT:
  396. case SIGTERM:
  397. ev_unloop(EV_A_ EVUNLOOP_ALL);
  398. }
  399. }
  400. }
  401. int create_server_socket(const char *host, const char *port)
  402. {
  403. struct addrinfo hints;
  404. struct addrinfo *result, *rp, *ipv4v6bindall;
  405. int s, server_sock;
  406. memset(&hints, 0, sizeof(struct addrinfo));
  407. hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
  408. hints.ai_socktype = SOCK_DGRAM; /* We want a UDP socket */
  409. hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; /* For wildcard IP address */
  410. hints.ai_protocol = IPPROTO_UDP;
  411. s = getaddrinfo(host, port, &hints, &result);
  412. if (s != 0) {
  413. LOGE("getaddrinfo: %s", gai_strerror(s));
  414. return -1;
  415. }
  416. rp = result;
  417. /*
  418. * On Linux, with net.ipv6.bindv6only = 0 (the default), getaddrinfo(NULL) with
  419. * AI_PASSIVE returns 0.0.0.0 and :: (in this order). AI_PASSIVE was meant to
  420. * return a list of addresses to listen on, but it is impossible to listen on
  421. * 0.0.0.0 and :: at the same time, if :: implies dualstack mode.
  422. */
  423. if (!host) {
  424. ipv4v6bindall = result;
  425. /* Loop over all address infos found until a IPV6 address is found. */
  426. while (ipv4v6bindall) {
  427. if (ipv4v6bindall->ai_family == AF_INET6) {
  428. rp = ipv4v6bindall; /* Take first IPV6 address available */
  429. break;
  430. }
  431. ipv4v6bindall = ipv4v6bindall->ai_next; /* Get next address info, if any */
  432. }
  433. }
  434. for (/*rp = result*/; rp != NULL; rp = rp->ai_next) {
  435. server_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  436. if (server_sock == -1) {
  437. continue;
  438. }
  439. if (rp->ai_family == AF_INET6) {
  440. int ipv6only = host ? 1 : 0;
  441. setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, sizeof(ipv6only));
  442. }
  443. int opt = 1;
  444. setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  445. s = bind(server_sock, rp->ai_addr, rp->ai_addrlen);
  446. if (s == 0) {
  447. /* We managed to bind successfully! */
  448. break;
  449. } else {
  450. ERROR("bind");
  451. }
  452. close(server_sock);
  453. }
  454. if (rp == NULL) {
  455. LOGE("cannot bind");
  456. return -1;
  457. }
  458. freeaddrinfo(result);
  459. return server_sock;
  460. }
  461. int main(int argc, char **argv)
  462. {
  463. int i, c;
  464. int pid_flags = 0;
  465. char *acl = NULL;
  466. char *user = NULL;
  467. char *password = NULL;
  468. char *timeout = NULL;
  469. char *method = NULL;
  470. char *pid_path = NULL;
  471. char *conf_path = NULL;
  472. char *iface = NULL;
  473. char *manager_address = NULL;
  474. int auth = 0;
  475. int fast_open = 0;
  476. int mode = TCP_ONLY;
  477. int server_num = 0;
  478. char *server_host[MAX_REMOTE_NUM];
  479. char *nameservers[MAX_DNS_NUM + 1];
  480. int nameserver_num = 0;
  481. jconf_t *conf = NULL;
  482. int option_index = 0;
  483. static struct option long_options[] = {
  484. { "fast-open", no_argument, 0, 0 },
  485. { "acl", required_argument, 0, 0 },
  486. { "manager-address", required_argument, 0, 0 },
  487. { "executable", required_argument, 0, 0 },
  488. { 0, 0, 0, 0 }
  489. };
  490. opterr = 0;
  491. USE_TTY();
  492. while ((c = getopt_long(argc, argv, "f:s:l:k:t:m:c:i:d:a:uUvA",
  493. long_options, &option_index)) != -1)
  494. switch (c) {
  495. case 0:
  496. if (option_index == 0) {
  497. fast_open = 1;
  498. } else if (option_index == 1) {
  499. acl = optarg;
  500. } else if (option_index == 2) {
  501. manager_address = optarg;
  502. } else if (option_index == 3) {
  503. executable = optarg;
  504. }
  505. break;
  506. case 's':
  507. if (server_num < MAX_REMOTE_NUM) {
  508. server_host[server_num++] = optarg;
  509. }
  510. break;
  511. case 'k':
  512. password = optarg;
  513. break;
  514. case 'f':
  515. pid_flags = 1;
  516. pid_path = optarg;
  517. break;
  518. case 't':
  519. timeout = optarg;
  520. break;
  521. case 'm':
  522. method = optarg;
  523. break;
  524. case 'c':
  525. conf_path = optarg;
  526. break;
  527. case 'i':
  528. iface = optarg;
  529. break;
  530. case 'd':
  531. if (nameserver_num < MAX_DNS_NUM) {
  532. nameservers[nameserver_num++] = optarg;
  533. }
  534. break;
  535. case 'a':
  536. user = optarg;
  537. break;
  538. case 'u':
  539. mode = TCP_AND_UDP;
  540. break;
  541. case 'U':
  542. mode = UDP_ONLY;
  543. break;
  544. case 'v':
  545. verbose = 1;
  546. break;
  547. case 'A':
  548. auth = 1;
  549. break;
  550. }
  551. if (opterr) {
  552. usage();
  553. exit(EXIT_FAILURE);
  554. }
  555. if (conf_path != NULL) {
  556. conf = read_jconf(conf_path);
  557. if (server_num == 0) {
  558. server_num = conf->remote_num;
  559. for (i = 0; i < server_num; i++)
  560. server_host[i] = conf->remote_addr[i].host;
  561. }
  562. if (password == NULL) {
  563. password = conf->password;
  564. }
  565. if (method == NULL) {
  566. method = conf->method;
  567. }
  568. if (timeout == NULL) {
  569. timeout = conf->timeout;
  570. }
  571. #ifdef TCP_FASTOPEN
  572. if (fast_open == 0) {
  573. fast_open = conf->fast_open;
  574. }
  575. #endif
  576. if (conf->nameserver != NULL) {
  577. nameservers[nameserver_num++] = conf->nameserver;
  578. }
  579. if (auth == 0) {
  580. auth = conf->auth;
  581. }
  582. }
  583. if (server_num == 0) {
  584. server_host[server_num++] = "0.0.0.0";
  585. }
  586. if (method == NULL) {
  587. method = "table";
  588. }
  589. if (timeout == NULL) {
  590. timeout = "60";
  591. }
  592. if (pid_flags) {
  593. USE_SYSLOG(argv[0]);
  594. daemonize(pid_path);
  595. }
  596. if (server_num == 0 || manager_address == NULL) {
  597. usage();
  598. exit(EXIT_FAILURE);
  599. }
  600. if (fast_open == 1) {
  601. #ifdef TCP_FASTOPEN
  602. LOGI("using tcp fast open");
  603. #else
  604. LOGE("tcp fast open is not supported by this environment");
  605. #endif
  606. }
  607. if (auth) {
  608. LOGI("onetime authentication enabled");
  609. }
  610. #ifdef __MINGW32__
  611. winsock_init();
  612. #else
  613. // ignore SIGPIPE
  614. signal(SIGPIPE, SIG_IGN);
  615. signal(SIGCHLD, SIG_IGN);
  616. signal(SIGABRT, SIG_IGN);
  617. #endif
  618. struct ev_signal sigint_watcher;
  619. struct ev_signal sigterm_watcher;
  620. ev_signal_init(&sigint_watcher, signal_cb, SIGINT);
  621. ev_signal_init(&sigterm_watcher, signal_cb, SIGTERM);
  622. ev_signal_start(EV_DEFAULT, &sigint_watcher);
  623. ev_signal_start(EV_DEFAULT, &sigterm_watcher);
  624. struct manager_ctx manager;
  625. memset(&manager, 0, sizeof(struct manager_ctx));
  626. manager.fast_open = fast_open;
  627. manager.verbose = verbose;
  628. manager.mode = mode;
  629. manager.auth = auth;
  630. manager.password = password;
  631. manager.timeout = timeout;
  632. manager.method = method;
  633. manager.iface = iface;
  634. manager.acl = acl;
  635. manager.user = user;
  636. manager.manager_address = manager_address;
  637. manager.hosts = server_host;
  638. manager.host_num = server_num;
  639. manager.nameservers = nameservers;
  640. manager.nameserver_num = nameserver_num;
  641. // inilitialize ev loop
  642. struct ev_loop *loop = EV_DEFAULT;
  643. // setuid
  644. if (user != NULL) {
  645. run_as(user);
  646. }
  647. struct passwd *pw = getpwuid(getuid());
  648. const char *homedir = pw->pw_dir;
  649. snprintf(working_dir, PATH_MAX, "%s/.shadowsocks", homedir);
  650. int err = mkdir(working_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  651. if (err != 0 && errno != EEXIST) {
  652. ERROR("mkdir");
  653. FATAL("unable to create working directory");
  654. }
  655. server_table = cork_string_hash_table_new(MAX_PORT_NUM, 0);
  656. if (conf != NULL) {
  657. for (i = 0; i < conf->port_password_num; i++) {
  658. struct server *server = (struct server *)malloc(sizeof(struct server));
  659. strncpy(server->port, conf->port_password[i].port, 8);
  660. strncpy(server->password, conf->port_password[i].password, 128);
  661. add_server(&manager, server);
  662. }
  663. }
  664. int sfd;
  665. ss_addr_t ip_addr = { .host = NULL, .port = NULL };
  666. parse_addr(manager_address, &ip_addr);
  667. if (ip_addr.host == NULL || ip_addr.port == NULL) {
  668. struct sockaddr_un svaddr;
  669. sfd = socket(AF_UNIX, SOCK_DGRAM, 0); /* Create server socket */
  670. if (sfd == -1) {
  671. FATAL("socket");
  672. }
  673. setnonblocking(sfd);
  674. if (remove(manager_address) == -1 && errno != ENOENT) {
  675. ERROR("bind");
  676. exit(EXIT_FAILURE);
  677. }
  678. memset(&svaddr, 0, sizeof(struct sockaddr_un));
  679. svaddr.sun_family = AF_UNIX;
  680. strncpy(svaddr.sun_path, manager_address, sizeof(svaddr.sun_path) - 1);
  681. if (bind(sfd, (struct sockaddr *)&svaddr, sizeof(struct sockaddr_un)) == -1) {
  682. ERROR("bind");
  683. exit(EXIT_FAILURE);
  684. }
  685. } else {
  686. sfd = create_server_socket(ip_addr.host, ip_addr.port);
  687. if (sfd == -1) {
  688. FATAL("socket");
  689. }
  690. }
  691. manager.fd = sfd;
  692. ev_io_init(&manager.io, manager_recv_cb, manager.fd, EV_READ);
  693. ev_io_start(loop, &manager.io);
  694. // start ev loop
  695. ev_run(loop, 0);
  696. if (verbose) {
  697. LOGI("closed gracefully");
  698. }
  699. // Clean up
  700. struct cork_hash_table_entry *entry;
  701. struct cork_hash_table_iterator server_iter;
  702. cork_hash_table_iterator_init(server_table, &server_iter);
  703. while ((entry = cork_hash_table_iterator_next(&server_iter)) != NULL) {
  704. struct server *server = (struct server *)entry->value;
  705. stop_server(working_dir, server->port);
  706. }
  707. #ifdef __MINGW32__
  708. winsock_cleanup();
  709. #endif
  710. ev_signal_stop(EV_DEFAULT, &sigint_watcher);
  711. ev_signal_stop(EV_DEFAULT, &sigterm_watcher);
  712. return 0;
  713. }