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.

665 lines
23 KiB

9 years ago
12 years ago
5 years ago
9 years ago
11 years ago
11 years ago
11 years ago
4 years ago
9 years ago
9 years ago
12 years ago
7 years ago
11 years ago
12 years ago
7 years ago
7 years ago
7 years ago
7 years ago
4 years ago
9 years ago
8 years ago
4 years ago
11 years ago
12 years ago
12 years ago
11 years ago
12 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
8 years ago
11 years ago
9 years ago
9 years ago
12 years ago
10 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
12 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
5 years ago
5 years ago
11 years ago
11 years ago
11 years ago
7 years ago
6 years ago
11 years ago
7 years ago
  1. # shadowsocks-libev
  2. [![Build Status](https://travis-ci.com/shadowsocks/shadowsocks-libev.svg?branch=master)](https://travis-ci.com/shadowsocks/shadowsocks-libev) [![Snap Status](https://build.snapcraft.io/badge/shadowsocks/shadowsocks-libev.svg)](https://build.snapcraft.io/user/shadowsocks/shadowsocks-libev)
  3. ## Intro
  4. [Shadowsocks-libev](https://shadowsocks.org) is a lightweight secured SOCKS5
  5. proxy for embedded devices and low-end boxes.
  6. It is a port of [Shadowsocks](https://github.com/shadowsocks/shadowsocks)
  7. created by [@clowwindy](https://github.com/clowwindy), and maintained by
  8. [@madeye](https://github.com/madeye) and [@linusyang](https://github.com/linusyang).
  9. Current version: 3.3.5 | [Changelog](debian/changelog)
  10. ## Features
  11. Shadowsocks-libev is written in pure C and depends on [libev](http://software.schmorp.de/pkg/libev.html). It's designed
  12. to be a lightweight implementation of shadowsocks protocol, in order to keep the resource usage as low as possible.
  13. For a full list of feature comparison between different versions of shadowsocks,
  14. refer to the [Wiki page](https://github.com/shadowsocks/shadowsocks/wiki/Feature-Comparison-across-Different-Versions).
  15. ## Quick Start
  16. Snap is the recommended way to install the latest binaries.
  17. ### Install snap core
  18. https://snapcraft.io/core
  19. ### Install from snapcraft.io
  20. Stable channel:
  21. ```bash
  22. sudo snap install shadowsocks-libev
  23. ```
  24. Edge channel:
  25. ```bash
  26. sudo snap install shadowsocks-libev --edge
  27. ```
  28. ## Installation
  29. ### Distribution-specific guide
  30. - [Debian & Ubuntu](#debian--ubuntu)
  31. + [Install from repository](#install-from-repository-not-recommended)
  32. + [Build deb package from source](#build-deb-package-from-source)
  33. + [Configure and start the service](#configure-and-start-the-service)
  34. - [Fedora & RHEL](#fedora--rhel)
  35. + [Build from source with centos](#build-from-source-with-centos)
  36. - [Archlinux & Manjaro](#archlinux--manjaro)
  37. - [NixOS](#nixos)
  38. - [Nix](#nix)
  39. - [Directly build and install on UNIX-like system](#linux)
  40. - [FreeBSD](#freebsd)
  41. + [Install](#install)
  42. + [Configuration](#configuration)
  43. + [Run](#run)
  44. + [Run as client](#run-as-client)
  45. - [OpenWRT](#openwrt)
  46. - [OS X](#os-x)
  47. - [Windows (MinGW)](#windows-mingw)
  48. - [Docker](#docker)
  49. * * *
  50. ### Pre-build configure guide
  51. For a complete list of available configure-time option,
  52. try `configure --help`.
  53. ### Debian & Ubuntu
  54. #### Install from repository (not recommended)
  55. Shadowsocks-libev is available in the official repository for following distributions:
  56. * Debian 8 or higher, including oldoldstable (jessie), old stable (stretch), stable (buster), testing (bullseye) and unstable (sid)
  57. * Ubuntu 16.10 or higher
  58. ```bash
  59. sudo apt update
  60. sudo apt install shadowsocks-libev
  61. ```
  62. #### Build deb package from source
  63. Supported distributions:
  64. * Debian 8, 9 or higher
  65. * Ubuntu 14.04 LTS, 16.04 LTS, 16.10 or higher
  66. You can build shadowsocks-libev and all its dependencies by script:
  67. ```bash
  68. mkdir -p ~/build-area/
  69. cp ./scripts/build_deb.sh ~/build-area/
  70. cd ~/build-area
  71. ./build_deb.sh
  72. ```
  73. For older systems, building `.deb` packages is not supported.
  74. Please try to build and install directly from source. See the [Linux](#linux) section below.
  75. **Note for Debian 8 (Jessie) users to build their own deb packages**:
  76. We strongly encourage you to install shadowsocks-libev from `jessie-backports-sloppy`. If you insist on building from source, you will need to manually install libsodium from `jessie-backports-sloppy`, **NOT** libsodium in main repository.
  77. For more info about backports, you can refer [Debian Backports](https://backports.debian.org).
  78. ``` bash
  79. cd shadowsocks-libev
  80. sudo sh -c 'printf "deb http://deb.debian.org/debian jessie-backports main" > /etc/apt/sources.list.d/jessie-backports.list'
  81. sudo sh -c 'printf "deb http://deb.debian.org/debian jessie-backports-sloppy main" >> /etc/apt/sources.list.d/jessie-backports.list'
  82. sudo apt-get install --no-install-recommends devscripts equivs
  83. mk-build-deps --root-cmd sudo --install --tool "apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y"
  84. ./autogen.sh && dpkg-buildpackage -b -us -uc
  85. cd ..
  86. sudo dpkg -i shadowsocks-libev*.deb
  87. ```
  88. **Note for Debian 9 (Stretch) users to build their own deb packages**:
  89. We strongly encourage you to install shadowsocks-libev from `stretch-backports`. If you insist on building from source, you will need to manually install libsodium from `stretch-backports`, **NOT** libsodium in main repository.
  90. For more info about backports, you can refer [Debian Backports](https://backports.debian.org).
  91. ``` bash
  92. cd shadowsocks-libev
  93. sudo sh -c 'printf "deb http://deb.debian.org/debian stretch-backports main" > /etc/apt/sources.list.d/stretch-backports.list'
  94. sudo apt-get install --no-install-recommends devscripts equivs
  95. mk-build-deps --root-cmd sudo --install --tool "apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y"
  96. ./autogen.sh && dpkg-buildpackage -b -us -uc
  97. cd ..
  98. sudo dpkg -i shadowsocks-libev*.deb
  99. ```
  100. #### Configure and start the service
  101. ```
  102. # Edit the configuration file
  103. sudo vim /etc/shadowsocks-libev/config.json
  104. # Edit the default configuration for debian
  105. sudo vim /etc/default/shadowsocks-libev
  106. # Start the service
  107. sudo /etc/init.d/shadowsocks-libev start # for sysvinit, or
  108. sudo systemctl start shadowsocks-libev # for systemd
  109. ```
  110. ### Fedora & RHEL
  111. Supported distributions:
  112. * Recent Fedora versions (until EOL)
  113. * RHEL 6, 7 and derivatives (including CentOS, Scientific Linux)
  114. #### Build from source with centos
  115. If you are using CentOS 7, you need to install these prerequirements to build from source code:
  116. ```bash
  117. yum install epel-release -y
  118. yum install gcc gettext autoconf libtool automake make pcre-devel asciidoc xmlto c-ares-devel libev-devel libsodium-devel mbedtls-devel -y
  119. ```
  120. ### Archlinux & Manjaro
  121. ```bash
  122. sudo pacman -S shadowsocks-libev
  123. ```
  124. Please refer to downstream [PKGBUILD](https://projects.archlinux.org/svntogit/community.git/tree/trunk?h=packages/shadowsocks-libev)
  125. script for extra modifications and distribution-specific bugs.
  126. ### NixOS
  127. ```bash
  128. nix-env -iA nixos.shadowsocks-libev
  129. ```
  130. ### Nix
  131. ```bash
  132. nix-env -iA nixpkgs.shadowsocks-libev
  133. ```
  134. ### Linux
  135. In general, you need the following build dependencies:
  136. * autotools (autoconf, automake, libtool)
  137. * gettext
  138. * pkg-config
  139. * libmbedtls
  140. * libsodium
  141. * libpcre3 (old pcre library)
  142. * libev
  143. * libc-ares
  144. * asciidoc (for documentation only)
  145. * xmlto (for documentation only)
  146. Notes: Fedora 26 libsodium version >= 1.0.12, so you can install via dnf install libsodium instead build from source.
  147. If your system is too old to provide libmbedtls and libsodium (later than **v1.0.8**), you will need to either install those libraries manually or upgrade your system.
  148. If your system provides with those libraries, you **should not** install them from source. You should jump this section and install them from distribution repository instead.
  149. For some of the distributions, you might install build dependencies like this:
  150. ```bash
  151. # Installation of basic build dependencies
  152. ## Debian / Ubuntu
  153. sudo apt-get install --no-install-recommends gettext build-essential autoconf libtool libpcre3-dev asciidoc xmlto libev-dev libc-ares-dev automake libmbedtls-dev libsodium-dev
  154. ## CentOS / Fedora / RHEL
  155. sudo yum install gettext gcc autoconf libtool automake make asciidoc xmlto c-ares-devel libev-devel
  156. ## Arch
  157. sudo pacman -S gettext gcc autoconf libtool automake make asciidoc xmlto c-ares libev
  158. # Installation of libsodium
  159. export LIBSODIUM_VER=1.0.16
  160. wget https://download.libsodium.org/libsodium/releases/libsodium-$LIBSODIUM_VER.tar.gz
  161. tar xvf libsodium-$LIBSODIUM_VER.tar.gz
  162. pushd libsodium-$LIBSODIUM_VER
  163. ./configure --prefix=/usr && make
  164. sudo make install
  165. popd
  166. sudo ldconfig
  167. # Installation of MbedTLS
  168. export MBEDTLS_VER=2.6.0
  169. wget https://tls.mbed.org/download/mbedtls-$MBEDTLS_VER-gpl.tgz
  170. tar xvf mbedtls-$MBEDTLS_VER-gpl.tgz
  171. pushd mbedtls-$MBEDTLS_VER
  172. make SHARED=1 CFLAGS="-O2 -fPIC"
  173. sudo make DESTDIR=/usr install
  174. popd
  175. sudo ldconfig
  176. # Start building
  177. ./autogen.sh && ./configure && make
  178. sudo make install
  179. ```
  180. You may need to manually install missing softwares.
  181. ### FreeBSD
  182. #### Install
  183. Shadowsocks-libev is available in FreeBSD Ports Collection. You can install it in either way, `pkg` or `ports`.
  184. **pkg (recommended)**
  185. ```bash
  186. pkg install shadowsocks-libev
  187. ```
  188. **ports**
  189. ```bash
  190. cd /usr/ports/net/shadowsocks-libev
  191. make install
  192. ```
  193. #### Configuration
  194. Edit your `config.json` file. By default, it's located in `/usr/local/etc/shadowsocks-libev`.
  195. To enable shadowsocks-libev, add the following rc variable to your `/etc/rc.conf` file:
  196. ```
  197. shadowsocks_libev_enable="YES"
  198. ```
  199. #### Run
  200. Start the Shadowsocks server:
  201. ```bash
  202. service shadowsocks_libev start
  203. ```
  204. #### Run as client
  205. By default, shadowsocks-libev is running as a server in FreeBSD. If you would like to start shadowsocks-libev in client mode, you can modify the rc script (`/usr/local/etc/rc.d/shadowsocks_libev`) manually.
  206. ```
  207. # modify the following line from "ss-server" to "ss-local"
  208. command="/usr/local/bin/ss-local"
  209. ```
  210. Note that is simply a workaround, each time you upgrade the port your changes will be overwritten by the new version.
  211. ### OpenWRT
  212. The OpenWRT project is maintained here:
  213. [openwrt-shadowsocks](https://github.com/shadowsocks/openwrt-shadowsocks).
  214. ### OS X
  215. For OS X, use [Homebrew](http://brew.sh) to install or build.
  216. Install Homebrew:
  217. ```bash
  218. ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  219. ```
  220. Install shadowsocks-libev:
  221. ```bash
  222. brew install shadowsocks-libev
  223. ```
  224. ### Windows (MinGW)
  225. To build Windows native binaries, the recommended method is to use Docker:
  226. * On Windows: double-click `make.bat` in `docker\mingw`
  227. * On Unix-like system:
  228. cd shadowsocks-libev/docker/mingw
  229. make
  230. A tarball with 32-bit and 64-bit binaries will be generated in the same directory.
  231. You could also manually use MinGW-w64 compilers to build in Unix-like shell (MSYS2/Cygwin), or cross-compile on Unix-like systems (Linux/MacOS). Please refer to build scripts in `docker/mingw`.
  232. Currently you need to use a patched libev library for MinGW:
  233. * https://github.com/shadowsocks/libev/archive/mingw.zip
  234. Notice that TCP Fast Open (TFO) is only available on **Windows 10**, **1607** or later version (precisely, build >= 14393). If you are using **1709** (build 16299) or later version, you also need to run the following command in PowerShell/Command Prompt **as Administrator** and **reboot** to use TFO properly:
  235. netsh int tcp set global fastopenfallback=disabled
  236. ### Docker
  237. As you expect, simply pull the image and run.
  238. ```
  239. docker pull shadowsocks/shadowsocks-libev
  240. docker run -e PASSWORD=<password> -p<server-port>:8388 -p<server-port>:8388/udp -d shadowsocks/shadowsocks-libev
  241. ```
  242. More information about the image can be found [here](docker/alpine/README.md).
  243. ## Usage
  244. For a detailed and complete list of all supported arguments,
  245. you may refer to the man pages of the applications, respectively.
  246. ss-[local|redir|server|tunnel|manager]
  247. -s <server_host> Host name or IP address of your remote server.
  248. -p <server_port> Port number of your remote server.
  249. -l <local_port> Port number of your local server.
  250. -k <password> Password of your remote server.
  251. -m <encrypt_method> Encrypt method: rc4-md5,
  252. aes-128-gcm, aes-192-gcm, aes-256-gcm,
  253. aes-128-cfb, aes-192-cfb, aes-256-cfb,
  254. aes-128-ctr, aes-192-ctr, aes-256-ctr,
  255. camellia-128-cfb, camellia-192-cfb,
  256. camellia-256-cfb, bf-cfb,
  257. chacha20-ietf-poly1305,
  258. xchacha20-ietf-poly1305,
  259. salsa20, chacha20 and chacha20-ietf.
  260. The default cipher is chacha20-ietf-poly1305.
  261. [-a <user>] Run as another user.
  262. [-f <pid_file>] The file path to store pid.
  263. [-t <timeout>] Socket timeout in seconds.
  264. [-c <config_file>] The path to config file.
  265. [-n <number>] Max number of open files.
  266. [-i <interface>] Network interface to bind.
  267. (not available in redir mode)
  268. [-b <local_address>] Local address to bind.
  269. For servers: Specify the local address to use
  270. while this server is making outbound
  271. connections to remote servers on behalf of the
  272. clients.
  273. For clients: Specify the local address to use
  274. while this client is making outbound
  275. connections to the server.
  276. [-u] Enable UDP relay.
  277. (TPROXY is required in redir mode)
  278. [-U] Enable UDP relay and disable TCP relay.
  279. (not available in local mode)
  280. [-T] Use tproxy instead of redirect. (for tcp)
  281. (only available in redir mode)
  282. [-L <addr>:<port>] Destination server address and port
  283. for local port forwarding.
  284. (only available in tunnel mode)
  285. [-6] Resolve hostname to IPv6 address first.
  286. [-d <addr>] Name servers for internal DNS resolver.
  287. (only available in server mode)
  288. [--reuse-port] Enable port reuse.
  289. [--fast-open] Enable TCP fast open.
  290. with Linux kernel > 3.7.0.
  291. (only available in local and server mode)
  292. [--acl <acl_file>] Path to ACL (Access Control List).
  293. (only available in local and server mode)
  294. [--manager-address <addr>] UNIX domain socket address.
  295. (only available in server and manager mode)
  296. [--mtu <MTU>] MTU of your network interface.
  297. [--mptcp] Enable Multipath TCP on MPTCP Kernel.
  298. [--no-delay] Enable TCP_NODELAY.
  299. [--executable <path>] Path to the executable of ss-server.
  300. (only available in manager mode)
  301. [-D <path>] Path to the working directory of ss-manager.
  302. (only available in manager mode)
  303. [--key <key_in_base64>] Key of your remote server.
  304. [--plugin <name>] Enable SIP003 plugin. (Experimental)
  305. [--plugin-opts <options>] Set SIP003 plugin options. (Experimental)
  306. [-v] Verbose mode.
  307. ## Transparent proxy
  308. The latest shadowsocks-libev has provided a *redir* mode. You can configure your Linux-based box or router to proxy all TCP traffic transparently, which is handy if you use an OpenWRT-powered router.
  309. # Create new chain
  310. iptables -t nat -N SHADOWSOCKS
  311. iptables -t mangle -N SHADOWSOCKS
  312. # Ignore your shadowsocks server's addresses
  313. # It's very IMPORTANT, just be careful.
  314. iptables -t nat -A SHADOWSOCKS -d 123.123.123.123 -j RETURN
  315. # Ignore LANs and any other addresses you'd like to bypass the proxy
  316. # See Wikipedia and RFC5735 for full list of reserved networks.
  317. # See ashi009/bestroutetb for a highly optimized CHN route list.
  318. iptables -t nat -A SHADOWSOCKS -d 0.0.0.0/8 -j RETURN
  319. iptables -t nat -A SHADOWSOCKS -d 10.0.0.0/8 -j RETURN
  320. iptables -t nat -A SHADOWSOCKS -d 127.0.0.0/8 -j RETURN
  321. iptables -t nat -A SHADOWSOCKS -d 169.254.0.0/16 -j RETURN
  322. iptables -t nat -A SHADOWSOCKS -d 172.16.0.0/12 -j RETURN
  323. iptables -t nat -A SHADOWSOCKS -d 192.168.0.0/16 -j RETURN
  324. iptables -t nat -A SHADOWSOCKS -d 224.0.0.0/4 -j RETURN
  325. iptables -t nat -A SHADOWSOCKS -d 240.0.0.0/4 -j RETURN
  326. # Anything else should be redirected to shadowsocks's local port
  327. iptables -t nat -A SHADOWSOCKS -p tcp -j REDIRECT --to-ports 12345
  328. # Add any UDP rules
  329. ip route add local default dev lo table 100
  330. ip rule add fwmark 1 lookup 100
  331. iptables -t mangle -A SHADOWSOCKS -p udp --dport 53 -j TPROXY --on-port 12345 --tproxy-mark 0x01/0x01
  332. # Apply the rules
  333. iptables -t nat -A PREROUTING -p tcp -j SHADOWSOCKS
  334. iptables -t mangle -A PREROUTING -j SHADOWSOCKS
  335. # Start the shadowsocks-redir
  336. ss-redir -u -c /etc/config/shadowsocks.json -f /var/run/shadowsocks.pid
  337. ## Transparent proxy (pure tproxy)
  338. Executing this script on the linux host can proxy all outgoing traffic of this machine (except the traffic sent to the reserved address). Other hosts under the same LAN can also change their default gateway to the ip of this linux host (at the same time change the dns server to 1.1.1.1 or 8.8.8.8, etc.) to proxy their outgoing traffic.
  339. > Of course, the ipv6 proxy is similar, just change `iptables` to `ip6tables`, `ip` to `ip -6`, `127.0.0.1` to `::1`, and other details.
  340. ```shell
  341. #!/bin/bash
  342. start_ssredir() {
  343. # please modify MyIP, MyPort, etc.
  344. (ss-redir -s MyIP -p MyPort -m MyMethod -k MyPasswd -b 127.0.0.1 -l 60080 --no-delay -u -T -v </dev/null &>>/var/log/ss-redir.log &)
  345. }
  346. stop_ssredir() {
  347. kill -9 $(pidof ss-redir) &>/dev/null
  348. }
  349. start_iptables() {
  350. ##################### SSREDIR #####################
  351. iptables -t mangle -N SSREDIR
  352. # connection-mark -> packet-mark
  353. iptables -t mangle -A SSREDIR -j CONNMARK --restore-mark
  354. iptables -t mangle -A SSREDIR -m mark --mark 0x2333 -j RETURN
  355. # please modify MyIP, MyPort, etc.
  356. # ignore traffic sent to ss-server
  357. iptables -t mangle -A SSREDIR -p tcp -d MyIP --dport MyPort -j RETURN
  358. iptables -t mangle -A SSREDIR -p udp -d MyIP --dport MyPort -j RETURN
  359. # ignore traffic sent to reserved addresses
  360. iptables -t mangle -A SSREDIR -d 0.0.0.0/8 -j RETURN
  361. iptables -t mangle -A SSREDIR -d 10.0.0.0/8 -j RETURN
  362. iptables -t mangle -A SSREDIR -d 100.64.0.0/10 -j RETURN
  363. iptables -t mangle -A SSREDIR -d 127.0.0.0/8 -j RETURN
  364. iptables -t mangle -A SSREDIR -d 169.254.0.0/16 -j RETURN
  365. iptables -t mangle -A SSREDIR -d 172.16.0.0/12 -j RETURN
  366. iptables -t mangle -A SSREDIR -d 192.0.0.0/24 -j RETURN
  367. iptables -t mangle -A SSREDIR -d 192.0.2.0/24 -j RETURN
  368. iptables -t mangle -A SSREDIR -d 192.88.99.0/24 -j RETURN
  369. iptables -t mangle -A SSREDIR -d 192.168.0.0/16 -j RETURN
  370. iptables -t mangle -A SSREDIR -d 198.18.0.0/15 -j RETURN
  371. iptables -t mangle -A SSREDIR -d 198.51.100.0/24 -j RETURN
  372. iptables -t mangle -A SSREDIR -d 203.0.113.0/24 -j RETURN
  373. iptables -t mangle -A SSREDIR -d 224.0.0.0/4 -j RETURN
  374. iptables -t mangle -A SSREDIR -d 240.0.0.0/4 -j RETURN
  375. iptables -t mangle -A SSREDIR -d 255.255.255.255/32 -j RETURN
  376. # mark the first packet of the connection
  377. iptables -t mangle -A SSREDIR -p tcp --syn -j MARK --set-mark 0x2333
  378. iptables -t mangle -A SSREDIR -p udp -m conntrack --ctstate NEW -j MARK --set-mark 0x2333
  379. # packet-mark -> connection-mark
  380. iptables -t mangle -A SSREDIR -j CONNMARK --save-mark
  381. ##################### OUTPUT #####################
  382. # proxy the outgoing traffic from this machine
  383. iptables -t mangle -A OUTPUT -p tcp -m addrtype --src-type LOCAL ! --dst-type LOCAL -j SSREDIR
  384. iptables -t mangle -A OUTPUT -p udp -m addrtype --src-type LOCAL ! --dst-type LOCAL -j SSREDIR
  385. ##################### PREROUTING #####################
  386. # proxy traffic passing through this machine (other->other)
  387. iptables -t mangle -A PREROUTING -p tcp -m addrtype ! --src-type LOCAL ! --dst-type LOCAL -j SSREDIR
  388. iptables -t mangle -A PREROUTING -p udp -m addrtype ! --src-type LOCAL ! --dst-type LOCAL -j SSREDIR
  389. # hand over the marked package to TPROXY for processing
  390. iptables -t mangle -A PREROUTING -p tcp -m mark --mark 0x2333 -j TPROXY --on-ip 127.0.0.1 --on-port 60080
  391. iptables -t mangle -A PREROUTING -p udp -m mark --mark 0x2333 -j TPROXY --on-ip 127.0.0.1 --on-port 60080
  392. }
  393. stop_iptables() {
  394. ##################### PREROUTING #####################
  395. iptables -t mangle -D PREROUTING -p tcp -m mark --mark 0x2333 -j TPROXY --on-ip 127.0.0.1 --on-port 60080 &>/dev/null
  396. iptables -t mangle -D PREROUTING -p udp -m mark --mark 0x2333 -j TPROXY --on-ip 127.0.0.1 --on-port 60080 &>/dev/null
  397. iptables -t mangle -D PREROUTING -p tcp -m addrtype ! --src-type LOCAL ! --dst-type LOCAL -j SSREDIR &>/dev/null
  398. iptables -t mangle -D PREROUTING -p udp -m addrtype ! --src-type LOCAL ! --dst-type LOCAL -j SSREDIR &>/dev/null
  399. ##################### OUTPUT #####################
  400. iptables -t mangle -D OUTPUT -p tcp -m addrtype --src-type LOCAL ! --dst-type LOCAL -j SSREDIR &>/dev/null
  401. iptables -t mangle -D OUTPUT -p udp -m addrtype --src-type LOCAL ! --dst-type LOCAL -j SSREDIR &>/dev/null
  402. ##################### SSREDIR #####################
  403. iptables -t mangle -F SSREDIR &>/dev/null
  404. iptables -t mangle -X SSREDIR &>/dev/null
  405. }
  406. start_iproute2() {
  407. ip route add local default dev lo table 100
  408. ip rule add fwmark 0x2333 table 100
  409. }
  410. stop_iproute2() {
  411. ip rule del table 100 &>/dev/null
  412. ip route flush table 100 &>/dev/null
  413. }
  414. start_resolvconf() {
  415. # or nameserver 8.8.8.8, etc.
  416. echo "nameserver 1.1.1.1" >/etc/resolv.conf
  417. }
  418. stop_resolvconf() {
  419. echo "nameserver 114.114.114.114" >/etc/resolv.conf
  420. }
  421. start() {
  422. echo "start ..."
  423. start_ssredir
  424. start_iptables
  425. start_iproute2
  426. start_resolvconf
  427. echo "start end"
  428. }
  429. stop() {
  430. echo "stop ..."
  431. stop_resolvconf
  432. stop_iproute2
  433. stop_iptables
  434. stop_ssredir
  435. echo "stop end"
  436. }
  437. restart() {
  438. stop
  439. sleep 1
  440. start
  441. }
  442. main() {
  443. if [ $# -eq 0 ]; then
  444. echo "usage: $0 start|stop|restart ..."
  445. return 1
  446. fi
  447. for funcname in "$@"; do
  448. if [ "$(type -t $funcname)" != 'function' ]; then
  449. echo "'$funcname' not a shell function"
  450. return 1
  451. fi
  452. done
  453. for funcname in "$@"; do
  454. $funcname
  455. done
  456. return 0
  457. }
  458. main "$@"
  459. ```
  460. ## Security Tips
  461. For any public server, to avoid users accessing localhost of your server, please add `--acl acl/server_block_local.acl` to the command line.
  462. Although shadowsocks-libev can handle thousands of concurrent connections nicely, we still recommend
  463. setting up your server's firewall rules to limit connections from each user:
  464. # Up to 32 connections are enough for normal usage
  465. iptables -A INPUT -p tcp --syn --dport ${SHADOWSOCKS_PORT} -m connlimit --connlimit-above 32 -j REJECT --reject-with tcp-reset
  466. ## License
  467. ```
  468. Copyright: 2013-2015, Clow Windy <clowwindy42@gmail.com>
  469. 2013-2018, Max Lv <max.c.lv@gmail.com>
  470. 2014, Linus Yang <linusyang@gmail.com>
  471. This program is free software: you can redistribute it and/or modify
  472. it under the terms of the GNU General Public License as published by
  473. the Free Software Foundation, either version 3 of the License, or
  474. (at your option) any later version.
  475. This program is distributed in the hope that it will be useful,
  476. but WITHOUT ANY WARRANTY; without even the implied warranty of
  477. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  478. GNU General Public License for more details.
  479. You should have received a copy of the GNU General Public License
  480. along with this program. If not, see <http://www.gnu.org/licenses/>.
  481. ```