Compare commits
327 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
87f91d3e62 | 10 years ago |
|
|
938a3246ba | 10 years ago |
|
|
bdee67b856 | 10 years ago |
|
|
e048e224d0 | 10 years ago |
|
|
d45ba8de03 | 10 years ago |
|
|
c3a9612919 | 10 years ago |
|
|
ce25bbf650 | 10 years ago |
|
|
0b94af0b7d | 10 years ago |
|
|
2a6fbc87d4 | 10 years ago |
|
|
ac58020595 | 10 years ago |
|
|
9533044bd8 | 10 years ago |
|
|
c7fb9bcb5e | 10 years ago |
|
|
e5824be145 | 10 years ago |
|
|
13ffdac2b7 | 10 years ago |
|
|
2eaca2faea | 10 years ago |
|
|
cfb8944003 | 10 years ago |
|
|
685b62532e | 10 years ago |
|
|
a0ee80a5ad | 10 years ago |
|
|
b8afd216d6 | 10 years ago |
|
|
5345c66abe | 10 years ago |
|
|
249b7fd73f | 10 years ago |
|
|
45e27f9142 | 10 years ago |
|
|
c48190fe7a | 10 years ago |
|
|
d1f64de6b4 | 10 years ago |
|
|
1183d567c9 | 10 years ago |
|
|
293a937501 | 10 years ago |
|
|
e8f6d87bb1 | 10 years ago |
|
|
574a10a743 | 10 years ago |
|
|
ada4eab628 | 10 years ago |
|
|
e7db6d4248 | 10 years ago |
|
|
a407bcc568 | 10 years ago |
|
|
52041387f9 | 10 years ago |
|
|
933e75b9a7 | 10 years ago |
|
|
a02e995a8a | 10 years ago |
|
|
2637c8d1c7 | 10 years ago |
|
|
88017d9643 | 10 years ago |
|
|
e81f8db0b8 | 10 years ago |
|
|
1fe6e89c07 | 10 years ago |
|
|
1221a1f3f6 | 10 years ago |
|
|
3d4356e8b4 | 10 years ago |
|
|
6c34c4116f | 10 years ago |
|
|
6ee4eb3184 | 10 years ago |
|
|
5e485eb2be | 10 years ago |
|
|
b3fcee662a | 10 years ago |
|
|
e1aa68029d | 10 years ago |
|
|
1e2445061f | 10 years ago |
|
|
efea3e08cc | 10 years ago |
|
|
f989fd4d37 | 10 years ago |
|
|
a26019f9e6 | 10 years ago |
|
|
816d2f6298 | 10 years ago |
|
|
3ce6e6f714 | 10 years ago |
|
|
b1aade8640 | 10 years ago |
|
|
d47905f01d | 10 years ago |
|
|
f3808a306f | 10 years ago |
|
|
e15030cc6e | 10 years ago |
|
|
292865f625 | 10 years ago |
|
|
f14461ea64 | 10 years ago |
|
|
8da3bdc511 | 10 years ago |
|
|
25e9c131d4 | 10 years ago |
|
|
9b314af84b | 10 years ago |
|
|
4a291ff335 | 10 years ago |
|
|
ca41f18f42 | 10 years ago |
|
|
82df4524d1 | 10 years ago |
|
|
1121434a5f | 10 years ago |
|
|
f0d6a5b41e | 10 years ago |
|
|
0213e78847 | 10 years ago |
|
|
a440a726f6 | 10 years ago |
|
|
3525d3d86a | 10 years ago |
|
|
2c4600cf0e | 10 years ago |
|
|
b922d70a87 | 10 years ago |
|
|
0ed10c73c9 | 10 years ago |
|
|
a6162496ad | 10 years ago |
|
|
0a7d71a393 | 10 years ago |
|
|
c3484b3375 | 10 years ago |
|
|
9c80dee9da | 10 years ago |
|
|
7bfafa7f3d | 10 years ago |
|
|
794121f111 | 10 years ago |
|
|
32957158b9 | 10 years ago |
|
|
9538afebff | 10 years ago |
|
|
3f6f16e4d6 | 10 years ago |
|
|
e2bae5874c | 10 years ago |
|
|
3fd0fbf1d0 | 10 years ago |
|
|
8e73f50ff5 | 10 years ago |
|
|
6db1445d2a | 10 years ago |
|
|
5709f7d901 | 10 years ago |
|
|
68d0c8e260 | 10 years ago |
|
|
1dbc5e780d | 10 years ago |
|
|
4fdd9ea0ab | 10 years ago |
|
|
d8dda5042f | 10 years ago |
|
|
6255a1d321 | 10 years ago |
|
|
783267dba0 | 10 years ago |
|
|
b812ab10bf | 11 years ago |
|
|
e45d5d308e | 11 years ago |
|
|
cd6dab188c | 11 years ago |
|
|
45584dc4f5 | 11 years ago |
|
|
50b11fff02 | 11 years ago |
|
|
2621e5e564 | 11 years ago |
|
|
050270a725 | 11 years ago |
|
|
f5434bdbd0 | 11 years ago |
|
|
187f0de25a | 11 years ago |
|
|
bd43069bcf | 11 years ago |
|
|
f58d76da47 | 11 years ago |
|
|
4b2adda440 | 11 years ago |
|
|
b344bc236b | 11 years ago |
|
|
9b14c019bb | 11 years ago |
|
|
ad625cd899 | 11 years ago |
|
|
7259b211c6 | 11 years ago |
|
|
3912ecba8e | 11 years ago |
|
|
d2b6c34bdf | 11 years ago |
|
|
3d1d38aa0f | 11 years ago |
|
|
3d56b040cf | 11 years ago |
|
|
acb3eb2910 | 11 years ago |
|
|
44598a070d | 11 years ago |
|
|
8031337d65 | 11 years ago |
|
|
044e1539e9 | 11 years ago |
|
|
268f6e23b4 | 11 years ago |
|
|
9e64d1837c | 11 years ago |
|
|
97785aae2e | 11 years ago |
|
|
8556fd3c18 | 11 years ago |
|
|
2f8517190c | 11 years ago |
|
|
5c1b90b774 | 11 years ago |
|
|
8cbca008c6 | 11 years ago |
|
|
72aab7701f | 11 years ago |
|
|
d653c9bdb3 | 11 years ago |
|
|
9e288c9ac0 | 11 years ago |
|
|
8dac9faa28 | 11 years ago |
|
|
825aece219 | 11 years ago |
|
|
4af9472986 | 11 years ago |
|
|
7f26f43f88 | 11 years ago |
|
|
c7864acc37 | 11 years ago |
|
|
51bd3fcea0 | 11 years ago |
|
|
569aaa5248 | 11 years ago |
|
|
4d43e12139 | 11 years ago |
|
|
6932548d8e | 11 years ago |
|
|
91576d1e08 | 11 years ago |
|
|
82ffa15a56 | 11 years ago |
|
|
9eefd27152 | 11 years ago |
|
|
7ccee594d5 | 11 years ago |
|
|
ce189ecd85 | 11 years ago |
|
|
3c64e849b0 | 11 years ago |
|
|
b3ab5d7c4c | 11 years ago |
|
|
8b35508b6f | 11 years ago |
|
|
2e65becc44 | 11 years ago |
|
|
c5ca6bd80a | 11 years ago |
|
|
979a51318c | 11 years ago |
|
|
5dbeac99de | 11 years ago |
|
|
4e10ff9063 | 11 years ago |
|
|
760a5308dd | 11 years ago |
|
|
cb866dc8fe | 11 years ago |
|
|
be922ba7c2 | 11 years ago |
|
|
6906c7db04 | 11 years ago |
|
|
f91e04b0bd | 11 years ago |
|
|
7e8472332a | 11 years ago |
|
|
4a02d19597 | 11 years ago |
|
|
b71af8f4df | 11 years ago |
|
|
b8cf491212 | 11 years ago |
|
|
d47b0d47ea | 11 years ago |
|
|
4e43a4932d | 11 years ago |
|
|
bb916eb705 | 11 years ago |
|
|
691aa0970a | 11 years ago |
|
|
0e26f9b513 | 11 years ago |
|
|
6486f02fbe | 11 years ago |
|
|
b8feaacbbb | 11 years ago |
|
|
9f4f42eefd | 11 years ago |
|
|
d6640b2e81 | 11 years ago |
|
|
95bc6e4a2f | 11 years ago |
|
|
962a05f137 | 11 years ago |
|
|
14edb58281 | 11 years ago |
|
|
cb86c34c3b | 11 years ago |
|
|
77abc7d1bc | 11 years ago |
|
|
41e92e277e | 11 years ago |
|
|
16eb3e3448 | 11 years ago |
|
|
b9aefc144b | 11 years ago |
|
|
27d2214337 | 11 years ago |
|
|
397990534a | 11 years ago |
|
|
187e266b26 | 11 years ago |
|
|
f5cc946796 | 11 years ago |
|
|
8b7db6d996 | 11 years ago |
|
|
a49b21ee2c | 11 years ago |
|
|
81b0597591 | 11 years ago |
|
|
e76939981a | 11 years ago |
|
|
6576359c1c | 11 years ago |
|
|
90ae1fdc40 | 11 years ago |
|
|
876bbbe5d8 | 11 years ago |
|
|
e56c5e4b90 | 11 years ago |
|
|
519c84719f | 11 years ago |
|
|
42a05d22d6 | 11 years ago |
|
|
b151bf815f | 11 years ago |
|
|
ddcf7f796f | 11 years ago |
|
|
3dec2377bb | 11 years ago |
|
|
202f0f6142 | 11 years ago |
|
|
80604a9421 | 11 years ago |
|
|
68a77ddc70 | 11 years ago |
|
|
459b0c7004 | 11 years ago |
|
|
0c4b00a8af | 11 years ago |
|
|
8fb3cc3394 | 11 years ago |
|
|
e889f953ff | 11 years ago |
|
|
430b98c982 | 11 years ago |
|
|
7300205add | 11 years ago |
|
|
443c4f35c2 | 11 years ago |
|
|
13579f1ca4 | 11 years ago |
|
|
aff97d4ce8 | 11 years ago |
|
|
aa92a59405 | 11 years ago |
|
|
e61ab27e59 | 11 years ago |
|
|
c83341da9b | 11 years ago |
|
|
7f480248f8 | 11 years ago |
|
|
96495e7dd9 | 11 years ago |
|
|
b5da4bc86a | 11 years ago |
|
|
4894925cac | 11 years ago |
|
|
409f3b053d | 11 years ago |
|
|
4b97412569 | 11 years ago |
|
|
57433bccb9 | 11 years ago |
|
|
0504fb43cf | 11 years ago |
|
|
3930cfc726 | 11 years ago |
|
|
84275b6815 | 11 years ago |
|
|
19c84cac17 | 11 years ago |
|
|
e513faae64 | 11 years ago |
|
|
737b7d9890 | 11 years ago |
|
|
8826629741 | 11 years ago |
|
|
125c3e88fd | 11 years ago |
|
|
2a03747e76 | 11 years ago |
|
|
b778a60502 | 11 years ago |
|
|
f45d57950b | 11 years ago |
|
|
b4d1d6b5f4 | 11 years ago |
|
|
877f0470c5 | 11 years ago |
|
|
04133d806c | 11 years ago |
|
|
24a3769809 | 11 years ago |
|
|
5e46aa0b47 | 11 years ago |
|
|
b606e19c0b | 11 years ago |
|
|
bc419c4dc2 | 11 years ago |
|
|
b713690521 | 11 years ago |
|
|
cb17c7b3b5 | 11 years ago |
|
|
032027c3ff | 11 years ago |
|
|
0f7a0c5628 | 11 years ago |
|
|
a96e2cbb63 | 11 years ago |
|
|
52391b9214 | 11 years ago |
|
|
60e86c27c4 | 11 years ago |
|
|
208f1462c3 | 11 years ago |
|
|
7d2b66766c | 11 years ago |
|
|
fcc7a4cf9b | 11 years ago |
|
|
72001556b5 | 11 years ago |
|
|
82b5980422 | 11 years ago |
|
|
fe4b35fb0a | 11 years ago |
|
|
c02fcbf746 | 11 years ago |
|
|
d9dc3ae0b5 | 11 years ago |
|
|
c0d1d666a4 | 11 years ago |
|
|
e6d7a12afe | 11 years ago |
|
|
011415f710 | 11 years ago |
|
|
8ee4cd513a | 11 years ago |
|
|
5137377580 | 11 years ago |
|
|
f5fbf579e2 | 11 years ago |
|
|
33d907cc08 | 11 years ago |
|
|
b31a8ea3f3 | 11 years ago |
|
|
c8423e360d | 11 years ago |
|
|
6ee8035735 | 11 years ago |
|
|
59105f6e64 | 11 years ago |
|
|
83c8b50ea2 | 11 years ago |
|
|
39dffa220f | 11 years ago |
|
|
0ba76b666d | 11 years ago |
|
|
126afea524 | 11 years ago |
|
|
3f66ec443f | 11 years ago |
|
|
041a5c1046 | 11 years ago |
|
|
79260ff4ab | 11 years ago |
|
|
eec312cc4c | 11 years ago |
|
|
6be40dbf1e | 11 years ago |
|
|
4551d5c268 | 11 years ago |
|
|
612c1e2ab3 | 11 years ago |
|
|
8988926ec7 | 11 years ago |
|
|
6d963cea44 | 11 years ago |
|
|
c73127d858 | 11 years ago |
|
|
36dc8f9463 | 11 years ago |
|
|
a597e61093 | 11 years ago |
|
|
463ce07426 | 11 years ago |
|
|
e9f005c926 | 11 years ago |
|
|
96a9a25eaa | 11 years ago |
|
|
74972875ff | 11 years ago |
|
|
f9cdf2090f | 11 years ago |
|
|
e060fd5b4d | 11 years ago |
|
|
93c94fbe3c | 11 years ago |
|
|
f9e8af6358 | 11 years ago |
|
|
bbbb9995d8 | 11 years ago |
|
|
680c128c72 | 11 years ago |
|
|
6f8fc5b77f | 11 years ago |
|
|
3e5cbb955e | 11 years ago |
|
|
71338a7385 | 11 years ago |
|
|
2e3114f40f | 11 years ago |
|
|
9df46398d6 | 11 years ago |
|
|
a723c2f05e | 11 years ago |
|
|
ac19b91719 | 11 years ago |
|
|
b6961974ba | 11 years ago |
|
|
5a5b897578 | 11 years ago |
|
|
401da7815f | 11 years ago |
|
|
55d10ace01 | 11 years ago |
|
|
56af51b43c | 11 years ago |
|
|
5249fd528c | 11 years ago |
|
|
61959945c3 | 11 years ago |
|
|
928c26e4f2 | 11 years ago |
|
|
9defba2d36 | 11 years ago |
|
|
da46866828 | 11 years ago |
|
|
b32cc8f703 | 11 years ago |
|
|
661fbbfb8b | 11 years ago |
|
|
a3344d9f1c | 11 years ago |
|
|
e5e3102591 | 11 years ago |
|
|
6e5744829c | 11 years ago |
|
|
a9ea55c396 | 11 years ago |
|
|
469d9f7bfa | 12 years ago |
|
|
69d369268a | 12 years ago |
|
|
f78f5b56e3 | 12 years ago |
|
|
298e8a06c9 | 12 years ago |
|
|
a142b957a7 | 12 years ago |
|
|
3af426b13b | 12 years ago |
|
|
543a5903a1 | 12 years ago |
|
|
ad588af75f | 12 years ago |
|
|
d4ca0d281c | 12 years ago |
|
|
28318b5c1a | 12 years ago |
|
|
1f530ff8f1 | 12 years ago |
|
|
17122c299c | 12 years ago |
|
|
4b52f83376 | 12 years ago |
|
|
07dc98f60d | 12 years ago |
|
|
99db626225 | 12 years ago |
|
|
9b7dab692b | 12 years ago |
|
|
565812ae15 | 12 years ago |
|
|
616a0c74bd | 12 years ago |
|
|
1e71fba852 | 12 years ago |
|
|
7c2fe9fd56 | 12 years ago |
|
|
c27f6283a2 | 12 years ago |
|
|
f2efed9608 | 12 years ago |
@ -0,0 +1,32 @@
|
|||||||
|
FROM alpine
|
||||||
|
|
||||||
|
ENV SERVER_ADDR 0.0.0.0
|
||||||
|
ENV SERVER_PORT 51348
|
||||||
|
ENV PASSWORD=
|
||||||
|
ENV METHOD aes-256-cfb
|
||||||
|
ENV PROTOCOL origin
|
||||||
|
ENV OBFS http_simple
|
||||||
|
ENV TIMEOUT 300
|
||||||
|
ENV DNS_ADDR 8.8.8.8
|
||||||
|
ENV DNS_ADDR_2 8.8.4.4
|
||||||
|
|
||||||
|
|
||||||
|
RUN apk update \
|
||||||
|
&& apk add python \
|
||||||
|
libsodium \
|
||||||
|
unzip \
|
||||||
|
wget \
|
||||||
|
&& rm -rf /var/cache/apk/*
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
RUN wget --no-check-certificate https://github.com/breakwa11/shadowsocks/archive/manyuser.zip -O /tmp/manyuser.zip \
|
||||||
|
&& unzip -d /tmp /tmp/manyuser.zip \
|
||||||
|
&& mv /tmp/shadowsocks-manyuser/shadowsocks ~/shadowsocks \
|
||||||
|
&& rm -rf /tmp/*
|
||||||
|
|
||||||
|
|
||||||
|
WORKDIR ~/shadowsocks
|
||||||
|
|
||||||
|
|
||||||
|
CMD python ~/shadowsocks/server.py -p $SERVER_PORT -k $PASSWORD -m $METHOD -O $PROTOCOL -o $OBFS
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"server": "0.0.0.0",
|
||||||
|
"server_ipv6": "::",
|
||||||
|
"server_port": 8388,
|
||||||
|
"local_address": "127.0.0.1",
|
||||||
|
"local_port": 1080,
|
||||||
|
"password": "m",
|
||||||
|
"timeout": 120,
|
||||||
|
"udp_timeout": 60,
|
||||||
|
"method": "aes-256-cfb",
|
||||||
|
"protocol": "auth_sha1_v4_compatible",
|
||||||
|
"protocol_param": "",
|
||||||
|
"obfs": "tls1.2_ticket_auth_compatible",
|
||||||
|
"obfs_param": "",
|
||||||
|
"dns_ipv6": false,
|
||||||
|
"connect_verbose_info": 0,
|
||||||
|
"redirect": "",
|
||||||
|
"fast_open": false
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
chmod +x *.sh
|
||||||
|
chmod +x shadowsocks/*.sh
|
||||||
|
cp -n apiconfig.py userapiconfig.py
|
||||||
|
cp -n config.json user-config.json
|
||||||
|
cp -n mysql.json usermysql.json
|
||||||
|
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
cd `dirname $0`
|
||||||
|
eval $(ps -ef | grep "[0-9] python server\\.py m" | awk '{print "kill "$2}')
|
||||||
|
ulimit -n 512000
|
||||||
|
nohup python server.py m>> ssserver.log 2>&1 &
|
||||||
|
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"host": "127.0.0.1",
|
||||||
|
"port": 3306,
|
||||||
|
"user": "ss",
|
||||||
|
"password": "pass",
|
||||||
|
"db": "sspanel",
|
||||||
|
"node_id": 0,
|
||||||
|
"transfer_mul": 1.0,
|
||||||
|
"ssl_enable": 0,
|
||||||
|
"ssl_ca": "",
|
||||||
|
"ssl_cert": "",
|
||||||
|
"ssl_key": ""
|
||||||
|
}
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
cd `dirname $0`
|
||||||
|
eval $(ps -ef | grep "[0-9] python server\\.py m" | awk '{print "kill "$2}')
|
||||||
|
ulimit -n 512000
|
||||||
|
nohup python server.py m>> /dev/null 2>&1 &
|
||||||
|
|
||||||
@ -0,0 +1,65 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# Copyright 2015 breakwall
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
|
import os
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import inspect
|
||||||
|
os.chdir(os.path.dirname(os.path.realpath(inspect.getfile(inspect.currentframe()))))
|
||||||
|
|
||||||
|
import server_pool
|
||||||
|
import db_transfer
|
||||||
|
from shadowsocks import shell
|
||||||
|
from configloader import load_config, get_config
|
||||||
|
|
||||||
|
class MainThread(threading.Thread):
|
||||||
|
def __init__(self, obj):
|
||||||
|
threading.Thread.__init__(self)
|
||||||
|
self.obj = obj
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self.obj.thread_db(self.obj)
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self.obj.thread_db_stop()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
shell.check_python()
|
||||||
|
if False:
|
||||||
|
db_transfer.DbTransfer.thread_db()
|
||||||
|
else:
|
||||||
|
if get_config().API_INTERFACE == 'mudbjson':
|
||||||
|
thread = MainThread(db_transfer.MuJsonTransfer)
|
||||||
|
elif get_config().API_INTERFACE == 'sspanelv2':
|
||||||
|
thread = MainThread(db_transfer.DbTransfer)
|
||||||
|
else:
|
||||||
|
thread = MainThread(db_transfer.Dbv3Transfer)
|
||||||
|
thread.start()
|
||||||
|
try:
|
||||||
|
while thread.is_alive():
|
||||||
|
time.sleep(10)
|
||||||
|
except (KeyboardInterrupt, IOError, OSError) as e:
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
thread.stop()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
||||||
@ -0,0 +1,135 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Copyright (c) 2014 clowwindy
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
from __future__ import absolute_import, division, print_function, \
|
||||||
|
with_statement
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from ctypes import CDLL, c_char_p, c_int, c_ulonglong, byref, \
|
||||||
|
create_string_buffer, c_void_p
|
||||||
|
|
||||||
|
__all__ = ['ciphers']
|
||||||
|
|
||||||
|
libsodium = None
|
||||||
|
loaded = False
|
||||||
|
|
||||||
|
buf_size = 2048
|
||||||
|
|
||||||
|
# for salsa20 and chacha20
|
||||||
|
BLOCK_SIZE = 64
|
||||||
|
|
||||||
|
|
||||||
|
def load_libsodium():
|
||||||
|
global loaded, libsodium, buf
|
||||||
|
|
||||||
|
from ctypes.util import find_library
|
||||||
|
for p in ('sodium',):
|
||||||
|
libsodium_path = find_library(p)
|
||||||
|
if libsodium_path:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise Exception('libsodium not found')
|
||||||
|
logging.info('loading libsodium from %s', libsodium_path)
|
||||||
|
libsodium = CDLL(libsodium_path)
|
||||||
|
libsodium.sodium_init.restype = c_int
|
||||||
|
libsodium.crypto_stream_salsa20_xor_ic.restype = c_int
|
||||||
|
libsodium.crypto_stream_salsa20_xor_ic.argtypes = (c_void_p, c_char_p,
|
||||||
|
c_ulonglong,
|
||||||
|
c_char_p, c_ulonglong,
|
||||||
|
c_char_p)
|
||||||
|
libsodium.crypto_stream_chacha20_xor_ic.restype = c_int
|
||||||
|
libsodium.crypto_stream_chacha20_xor_ic.argtypes = (c_void_p, c_char_p,
|
||||||
|
c_ulonglong,
|
||||||
|
c_char_p, c_ulonglong,
|
||||||
|
c_char_p)
|
||||||
|
|
||||||
|
libsodium.sodium_init()
|
||||||
|
|
||||||
|
buf = create_string_buffer(buf_size)
|
||||||
|
loaded = True
|
||||||
|
|
||||||
|
|
||||||
|
class Salsa20Crypto(object):
|
||||||
|
def __init__(self, cipher_name, key, iv, op):
|
||||||
|
if not loaded:
|
||||||
|
load_libsodium()
|
||||||
|
self.key = key
|
||||||
|
self.iv = iv
|
||||||
|
self.key_ptr = c_char_p(key)
|
||||||
|
self.iv_ptr = c_char_p(iv)
|
||||||
|
if cipher_name == b'salsa20':
|
||||||
|
self.cipher = libsodium.crypto_stream_salsa20_xor_ic
|
||||||
|
elif cipher_name == b'chacha20':
|
||||||
|
self.cipher = libsodium.crypto_stream_chacha20_xor_ic
|
||||||
|
else:
|
||||||
|
raise Exception('Unknown cipher')
|
||||||
|
# byte counter, not block counter
|
||||||
|
self.counter = 0
|
||||||
|
|
||||||
|
def update(self, data):
|
||||||
|
global buf_size, buf
|
||||||
|
l = len(data)
|
||||||
|
|
||||||
|
# we can only prepend some padding to make the encryption align to
|
||||||
|
# blocks
|
||||||
|
padding = self.counter % BLOCK_SIZE
|
||||||
|
if buf_size < padding + l:
|
||||||
|
buf_size = (padding + l) * 2
|
||||||
|
buf = create_string_buffer(buf_size)
|
||||||
|
|
||||||
|
if padding:
|
||||||
|
data = (b'\0' * padding) + data
|
||||||
|
self.cipher(byref(buf), c_char_p(data), padding + l,
|
||||||
|
self.iv_ptr, int(self.counter / BLOCK_SIZE), self.key_ptr)
|
||||||
|
self.counter += l
|
||||||
|
# buf is copied to a str object when we access buf.raw
|
||||||
|
# strip off the padding
|
||||||
|
return buf.raw[padding:padding + l]
|
||||||
|
|
||||||
|
|
||||||
|
ciphers = {
|
||||||
|
b'salsa20': (32, 8, Salsa20Crypto),
|
||||||
|
b'chacha20': (32, 8, Salsa20Crypto),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_salsa20():
|
||||||
|
from shadowsocks.crypto import util
|
||||||
|
|
||||||
|
cipher = Salsa20Crypto(b'salsa20', b'k' * 32, b'i' * 16, 1)
|
||||||
|
decipher = Salsa20Crypto(b'salsa20', b'k' * 32, b'i' * 16, 0)
|
||||||
|
|
||||||
|
util.run_cipher(cipher, decipher)
|
||||||
|
|
||||||
|
|
||||||
|
def test_chacha20():
|
||||||
|
from shadowsocks.crypto import util
|
||||||
|
|
||||||
|
cipher = Salsa20Crypto(b'chacha20', b'k' * 32, b'i' * 16, 1)
|
||||||
|
decipher = Salsa20Crypto(b'chacha20', b'k' * 32, b'i' * 16, 0)
|
||||||
|
|
||||||
|
util.run_cipher(cipher, decipher)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_chacha20()
|
||||||
|
test_salsa20()
|
||||||
@ -0,0 +1,188 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Copyright (c) 2014 clowwindy
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
from __future__ import absolute_import, division, print_function, \
|
||||||
|
with_statement
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from ctypes import CDLL, c_char_p, c_int, c_long, byref,\
|
||||||
|
create_string_buffer, c_void_p
|
||||||
|
|
||||||
|
__all__ = ['ciphers']
|
||||||
|
|
||||||
|
libcrypto = None
|
||||||
|
loaded = False
|
||||||
|
|
||||||
|
buf_size = 2048
|
||||||
|
|
||||||
|
|
||||||
|
def load_openssl():
|
||||||
|
global loaded, libcrypto, buf
|
||||||
|
|
||||||
|
from ctypes.util import find_library
|
||||||
|
for p in ('crypto', 'eay32', 'libeay32'):
|
||||||
|
libcrypto_path = find_library(p)
|
||||||
|
if libcrypto_path:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise Exception('libcrypto(OpenSSL) not found')
|
||||||
|
logging.info('loading libcrypto from %s', libcrypto_path)
|
||||||
|
libcrypto = CDLL(libcrypto_path)
|
||||||
|
libcrypto.EVP_get_cipherbyname.restype = c_void_p
|
||||||
|
libcrypto.EVP_CIPHER_CTX_new.restype = c_void_p
|
||||||
|
|
||||||
|
libcrypto.EVP_CipherInit_ex.argtypes = (c_void_p, c_void_p, c_char_p,
|
||||||
|
c_char_p, c_char_p, c_int)
|
||||||
|
|
||||||
|
libcrypto.EVP_CipherUpdate.argtypes = (c_void_p, c_void_p, c_void_p,
|
||||||
|
c_char_p, c_int)
|
||||||
|
|
||||||
|
libcrypto.EVP_CIPHER_CTX_cleanup.argtypes = (c_void_p,)
|
||||||
|
libcrypto.EVP_CIPHER_CTX_free.argtypes = (c_void_p,)
|
||||||
|
if hasattr(libcrypto, 'OpenSSL_add_all_ciphers'):
|
||||||
|
libcrypto.OpenSSL_add_all_ciphers()
|
||||||
|
|
||||||
|
buf = create_string_buffer(buf_size)
|
||||||
|
loaded = True
|
||||||
|
|
||||||
|
|
||||||
|
def load_cipher(cipher_name):
|
||||||
|
func_name = b'EVP_' + cipher_name.replace(b'-', b'_')
|
||||||
|
if bytes != str:
|
||||||
|
func_name = str(func_name, 'utf-8')
|
||||||
|
cipher = getattr(libcrypto, func_name, None)
|
||||||
|
if cipher:
|
||||||
|
cipher.restype = c_void_p
|
||||||
|
return cipher()
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
class CtypesCrypto(object):
|
||||||
|
def __init__(self, cipher_name, key, iv, op):
|
||||||
|
if not loaded:
|
||||||
|
load_openssl()
|
||||||
|
self._ctx = None
|
||||||
|
cipher = libcrypto.EVP_get_cipherbyname(cipher_name)
|
||||||
|
if not cipher:
|
||||||
|
cipher = load_cipher(cipher_name)
|
||||||
|
if not cipher:
|
||||||
|
raise Exception('cipher %s not found in libcrypto' % cipher_name)
|
||||||
|
key_ptr = c_char_p(key)
|
||||||
|
iv_ptr = c_char_p(iv)
|
||||||
|
self._ctx = libcrypto.EVP_CIPHER_CTX_new()
|
||||||
|
if not self._ctx:
|
||||||
|
raise Exception('can not create cipher context')
|
||||||
|
r = libcrypto.EVP_CipherInit_ex(self._ctx, cipher, None,
|
||||||
|
key_ptr, iv_ptr, c_int(op))
|
||||||
|
if not r:
|
||||||
|
self.clean()
|
||||||
|
raise Exception('can not initialize cipher context')
|
||||||
|
|
||||||
|
def update(self, data):
|
||||||
|
global buf_size, buf
|
||||||
|
cipher_out_len = c_long(0)
|
||||||
|
l = len(data)
|
||||||
|
if buf_size < l:
|
||||||
|
buf_size = l * 2
|
||||||
|
buf = create_string_buffer(buf_size)
|
||||||
|
libcrypto.EVP_CipherUpdate(self._ctx, byref(buf),
|
||||||
|
byref(cipher_out_len), c_char_p(data), l)
|
||||||
|
# buf is copied to a str object when we access buf.raw
|
||||||
|
return buf.raw[:cipher_out_len.value]
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
self.clean()
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
if self._ctx:
|
||||||
|
libcrypto.EVP_CIPHER_CTX_cleanup(self._ctx)
|
||||||
|
libcrypto.EVP_CIPHER_CTX_free(self._ctx)
|
||||||
|
|
||||||
|
|
||||||
|
ciphers = {
|
||||||
|
b'aes-128-cfb': (16, 16, CtypesCrypto),
|
||||||
|
b'aes-192-cfb': (24, 16, CtypesCrypto),
|
||||||
|
b'aes-256-cfb': (32, 16, CtypesCrypto),
|
||||||
|
b'aes-128-ofb': (16, 16, CtypesCrypto),
|
||||||
|
b'aes-192-ofb': (24, 16, CtypesCrypto),
|
||||||
|
b'aes-256-ofb': (32, 16, CtypesCrypto),
|
||||||
|
b'aes-128-ctr': (16, 16, CtypesCrypto),
|
||||||
|
b'aes-192-ctr': (24, 16, CtypesCrypto),
|
||||||
|
b'aes-256-ctr': (32, 16, CtypesCrypto),
|
||||||
|
b'aes-128-cfb8': (16, 16, CtypesCrypto),
|
||||||
|
b'aes-192-cfb8': (24, 16, CtypesCrypto),
|
||||||
|
b'aes-256-cfb8': (32, 16, CtypesCrypto),
|
||||||
|
b'aes-128-cfb1': (16, 16, CtypesCrypto),
|
||||||
|
b'aes-192-cfb1': (24, 16, CtypesCrypto),
|
||||||
|
b'aes-256-cfb1': (32, 16, CtypesCrypto),
|
||||||
|
b'bf-cfb': (16, 8, CtypesCrypto),
|
||||||
|
b'camellia-128-cfb': (16, 16, CtypesCrypto),
|
||||||
|
b'camellia-192-cfb': (24, 16, CtypesCrypto),
|
||||||
|
b'camellia-256-cfb': (32, 16, CtypesCrypto),
|
||||||
|
b'cast5-cfb': (16, 8, CtypesCrypto),
|
||||||
|
b'des-cfb': (8, 8, CtypesCrypto),
|
||||||
|
b'idea-cfb': (16, 8, CtypesCrypto),
|
||||||
|
b'rc2-cfb': (16, 8, CtypesCrypto),
|
||||||
|
b'rc4': (16, 0, CtypesCrypto),
|
||||||
|
b'seed-cfb': (16, 16, CtypesCrypto),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def run_method(method):
|
||||||
|
from shadowsocks.crypto import util
|
||||||
|
|
||||||
|
cipher = CtypesCrypto(method, b'k' * 32, b'i' * 16, 1)
|
||||||
|
decipher = CtypesCrypto(method, b'k' * 32, b'i' * 16, 0)
|
||||||
|
|
||||||
|
util.run_cipher(cipher, decipher)
|
||||||
|
|
||||||
|
|
||||||
|
def test_aes_128_cfb():
|
||||||
|
run_method(b'aes-128-cfb')
|
||||||
|
|
||||||
|
|
||||||
|
def test_aes_256_cfb():
|
||||||
|
run_method(b'aes-256-cfb')
|
||||||
|
|
||||||
|
|
||||||
|
def test_aes_128_cfb8():
|
||||||
|
run_method(b'aes-128-cfb8')
|
||||||
|
|
||||||
|
|
||||||
|
def test_aes_256_ofb():
|
||||||
|
run_method(b'aes-256-ofb')
|
||||||
|
|
||||||
|
|
||||||
|
def test_aes_256_ctr():
|
||||||
|
run_method(b'aes-256-ctr')
|
||||||
|
|
||||||
|
|
||||||
|
def test_bf_cfb():
|
||||||
|
run_method(b'bf-cfb')
|
||||||
|
|
||||||
|
|
||||||
|
def test_rc4():
|
||||||
|
run_method(b'rc4')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_aes_128_cfb()
|
||||||
@ -0,0 +1,51 @@
|
|||||||
|
from __future__ import absolute_import, division, print_function, \
|
||||||
|
with_statement
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../'))
|
||||||
|
|
||||||
|
|
||||||
|
from shadowsocks.crypto import rc4_md5
|
||||||
|
from shadowsocks.crypto import openssl
|
||||||
|
from shadowsocks.crypto import sodium
|
||||||
|
from shadowsocks.crypto import table
|
||||||
|
|
||||||
|
def run(func):
|
||||||
|
try:
|
||||||
|
func()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def run_n(func, name):
|
||||||
|
try:
|
||||||
|
func(name)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print("\n""rc4_md5")
|
||||||
|
rc4_md5.test()
|
||||||
|
print("\n""aes-256-cfb")
|
||||||
|
openssl.test_aes_256_cfb()
|
||||||
|
print("\n""aes-128-cfb")
|
||||||
|
openssl.test_aes_128_cfb()
|
||||||
|
print("\n""bf-cfb")
|
||||||
|
run(openssl.test_bf_cfb)
|
||||||
|
print("\n""camellia-128-cfb")
|
||||||
|
run_n(openssl.run_method, "camellia-128-cfb")
|
||||||
|
print("\n""cast5-cfb")
|
||||||
|
run_n(openssl.run_method, "cast5-cfb")
|
||||||
|
print("\n""idea-cfb")
|
||||||
|
run_n(openssl.run_method, "idea-cfb")
|
||||||
|
print("\n""seed-cfb")
|
||||||
|
run_n(openssl.run_method, "seed-cfb")
|
||||||
|
print("\n""salsa20")
|
||||||
|
run(sodium.test_salsa20)
|
||||||
|
print("\n""chacha20")
|
||||||
|
run(sodium.test_chacha20)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
cd `dirname $0`
|
||||||
|
eval $(ps -ef | grep "[0-9] python server\\.py a" | awk '{print "kill "$2}')
|
||||||
|
ulimit -n 4096
|
||||||
|
nohup python server.py a >> ssserver.log 2>&1 &
|
||||||
|
|
||||||
@ -0,0 +1,291 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# Copyright 2015 clowwindy
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
from __future__ import absolute_import, division, print_function, \
|
||||||
|
with_statement
|
||||||
|
|
||||||
|
import errno
|
||||||
|
import traceback
|
||||||
|
import socket
|
||||||
|
import logging
|
||||||
|
import json
|
||||||
|
import collections
|
||||||
|
|
||||||
|
from shadowsocks import common, eventloop, tcprelay, udprelay, asyncdns, shell
|
||||||
|
|
||||||
|
|
||||||
|
BUF_SIZE = 1506
|
||||||
|
STAT_SEND_LIMIT = 50
|
||||||
|
|
||||||
|
|
||||||
|
class Manager(object):
|
||||||
|
|
||||||
|
def __init__(self, config):
|
||||||
|
self._config = config
|
||||||
|
self._relays = {} # (tcprelay, udprelay)
|
||||||
|
self._loop = eventloop.EventLoop()
|
||||||
|
self._dns_resolver = asyncdns.DNSResolver()
|
||||||
|
self._dns_resolver.add_to_loop(self._loop)
|
||||||
|
|
||||||
|
self._statistics = collections.defaultdict(int)
|
||||||
|
self._control_client_addr = None
|
||||||
|
try:
|
||||||
|
manager_address = common.to_str(config['manager_address'])
|
||||||
|
if ':' in manager_address:
|
||||||
|
addr = manager_address.rsplit(':', 1)
|
||||||
|
addr = addr[0], int(addr[1])
|
||||||
|
addrs = socket.getaddrinfo(addr[0], addr[1])
|
||||||
|
if addrs:
|
||||||
|
family = addrs[0][0]
|
||||||
|
else:
|
||||||
|
logging.error('invalid address: %s', manager_address)
|
||||||
|
exit(1)
|
||||||
|
else:
|
||||||
|
addr = manager_address
|
||||||
|
family = socket.AF_UNIX
|
||||||
|
self._control_socket = socket.socket(family,
|
||||||
|
socket.SOCK_DGRAM)
|
||||||
|
self._control_socket.bind(addr)
|
||||||
|
self._control_socket.setblocking(False)
|
||||||
|
except (OSError, IOError) as e:
|
||||||
|
logging.error(e)
|
||||||
|
logging.error('can not bind to manager address')
|
||||||
|
exit(1)
|
||||||
|
self._loop.add(self._control_socket,
|
||||||
|
eventloop.POLL_IN, self)
|
||||||
|
self._loop.add_periodic(self.handle_periodic)
|
||||||
|
|
||||||
|
port_password = config['port_password']
|
||||||
|
del config['port_password']
|
||||||
|
for port, password in port_password.items():
|
||||||
|
a_config = config.copy()
|
||||||
|
a_config['server_port'] = int(port)
|
||||||
|
a_config['password'] = password
|
||||||
|
self.add_port(a_config)
|
||||||
|
|
||||||
|
def add_port(self, config):
|
||||||
|
port = int(config['server_port'])
|
||||||
|
servers = self._relays.get(port, None)
|
||||||
|
if servers:
|
||||||
|
logging.error("server already exists at %s:%d" % (config['server'],
|
||||||
|
port))
|
||||||
|
return
|
||||||
|
logging.info("adding server at %s:%d" % (config['server'], port))
|
||||||
|
t = tcprelay.TCPRelay(config, self._dns_resolver, False,
|
||||||
|
stat_callback=self.stat_callback)
|
||||||
|
u = udprelay.UDPRelay(config, self._dns_resolver, False,
|
||||||
|
stat_callback=self.stat_callback)
|
||||||
|
t.add_to_loop(self._loop)
|
||||||
|
u.add_to_loop(self._loop)
|
||||||
|
self._relays[port] = (t, u)
|
||||||
|
|
||||||
|
def remove_port(self, config):
|
||||||
|
port = int(config['server_port'])
|
||||||
|
servers = self._relays.get(port, None)
|
||||||
|
if servers:
|
||||||
|
logging.info("removing server at %s:%d" % (config['server'], port))
|
||||||
|
t, u = servers
|
||||||
|
t.close(next_tick=False)
|
||||||
|
u.close(next_tick=False)
|
||||||
|
del self._relays[port]
|
||||||
|
else:
|
||||||
|
logging.error("server not exist at %s:%d" % (config['server'],
|
||||||
|
port))
|
||||||
|
|
||||||
|
def handle_event(self, sock, fd, event):
|
||||||
|
if sock == self._control_socket and event == eventloop.POLL_IN:
|
||||||
|
data, self._control_client_addr = sock.recvfrom(BUF_SIZE)
|
||||||
|
parsed = self._parse_command(data)
|
||||||
|
if parsed:
|
||||||
|
command, config = parsed
|
||||||
|
a_config = self._config.copy()
|
||||||
|
if config:
|
||||||
|
# let the command override the configuration file
|
||||||
|
a_config.update(config)
|
||||||
|
if 'server_port' not in a_config:
|
||||||
|
logging.error('can not find server_port in config')
|
||||||
|
else:
|
||||||
|
if command == 'add':
|
||||||
|
self.add_port(a_config)
|
||||||
|
self._send_control_data(b'ok')
|
||||||
|
elif command == 'remove':
|
||||||
|
self.remove_port(a_config)
|
||||||
|
self._send_control_data(b'ok')
|
||||||
|
elif command == 'ping':
|
||||||
|
self._send_control_data(b'pong')
|
||||||
|
else:
|
||||||
|
logging.error('unknown command %s', command)
|
||||||
|
|
||||||
|
def _parse_command(self, data):
|
||||||
|
# commands:
|
||||||
|
# add: {"server_port": 8000, "password": "foobar"}
|
||||||
|
# remove: {"server_port": 8000"}
|
||||||
|
data = common.to_str(data)
|
||||||
|
parts = data.split(':', 1)
|
||||||
|
if len(parts) < 2:
|
||||||
|
return data, None
|
||||||
|
command, config_json = parts
|
||||||
|
try:
|
||||||
|
config = shell.parse_json_in_str(config_json)
|
||||||
|
return command, config
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(e)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def stat_callback(self, port, data_len):
|
||||||
|
self._statistics[port] += data_len
|
||||||
|
|
||||||
|
def handle_periodic(self):
|
||||||
|
r = {}
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
def send_data(data_dict):
|
||||||
|
if data_dict:
|
||||||
|
# use compact JSON format (without space)
|
||||||
|
data = common.to_bytes(json.dumps(data_dict,
|
||||||
|
separators=(',', ':')))
|
||||||
|
self._send_control_data(b'stat: ' + data)
|
||||||
|
|
||||||
|
for k, v in self._statistics.items():
|
||||||
|
r[k] = v
|
||||||
|
i += 1
|
||||||
|
# split the data into segments that fit in UDP packets
|
||||||
|
if i >= STAT_SEND_LIMIT:
|
||||||
|
send_data(r)
|
||||||
|
r.clear()
|
||||||
|
i = 0
|
||||||
|
if len(r) > 0 :
|
||||||
|
send_data(r)
|
||||||
|
self._statistics.clear()
|
||||||
|
|
||||||
|
def _send_control_data(self, data):
|
||||||
|
if self._control_client_addr:
|
||||||
|
try:
|
||||||
|
self._control_socket.sendto(data, self._control_client_addr)
|
||||||
|
except (socket.error, OSError, IOError) as e:
|
||||||
|
error_no = eventloop.errno_from_exception(e)
|
||||||
|
if error_no in (errno.EAGAIN, errno.EINPROGRESS,
|
||||||
|
errno.EWOULDBLOCK):
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
shell.print_exception(e)
|
||||||
|
if self._config['verbose']:
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self._loop.run()
|
||||||
|
|
||||||
|
|
||||||
|
def run(config):
|
||||||
|
Manager(config).run()
|
||||||
|
|
||||||
|
|
||||||
|
def test():
|
||||||
|
import time
|
||||||
|
import threading
|
||||||
|
import struct
|
||||||
|
from shadowsocks import encrypt
|
||||||
|
|
||||||
|
logging.basicConfig(level=5,
|
||||||
|
format='%(asctime)s %(levelname)-8s %(message)s',
|
||||||
|
datefmt='%Y-%m-%d %H:%M:%S')
|
||||||
|
enc = []
|
||||||
|
eventloop.TIMEOUT_PRECISION = 1
|
||||||
|
|
||||||
|
def run_server():
|
||||||
|
config = shell.get_config(True)
|
||||||
|
config = config.copy()
|
||||||
|
a_config = {
|
||||||
|
'server': '127.0.0.1',
|
||||||
|
'local_port': 1081,
|
||||||
|
'port_password': {
|
||||||
|
'8381': 'foobar1',
|
||||||
|
'8382': 'foobar2'
|
||||||
|
},
|
||||||
|
'method': 'aes-256-cfb',
|
||||||
|
'manager_address': '127.0.0.1:6001',
|
||||||
|
'timeout': 60,
|
||||||
|
'fast_open': False,
|
||||||
|
'verbose': 2
|
||||||
|
}
|
||||||
|
config.update(a_config)
|
||||||
|
manager = Manager(config)
|
||||||
|
enc.append(manager)
|
||||||
|
manager.run()
|
||||||
|
|
||||||
|
t = threading.Thread(target=run_server)
|
||||||
|
t.start()
|
||||||
|
time.sleep(1)
|
||||||
|
manager = enc[0]
|
||||||
|
cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
cli.connect(('127.0.0.1', 6001))
|
||||||
|
|
||||||
|
# test add and remove
|
||||||
|
time.sleep(1)
|
||||||
|
cli.send(b'add: {"server_port":7001, "password":"asdfadsfasdf"}')
|
||||||
|
time.sleep(1)
|
||||||
|
assert 7001 in manager._relays
|
||||||
|
data, addr = cli.recvfrom(1506)
|
||||||
|
assert b'ok' in data
|
||||||
|
|
||||||
|
cli.send(b'remove: {"server_port":8381}')
|
||||||
|
time.sleep(1)
|
||||||
|
assert 8381 not in manager._relays
|
||||||
|
data, addr = cli.recvfrom(1506)
|
||||||
|
assert b'ok' in data
|
||||||
|
logging.info('add and remove test passed')
|
||||||
|
|
||||||
|
# test statistics for TCP
|
||||||
|
header = common.pack_addr(b'google.com') + struct.pack('>H', 80)
|
||||||
|
data = encrypt.encrypt_all(b'asdfadsfasdf', 'aes-256-cfb', 1,
|
||||||
|
header + b'GET /\r\n\r\n')
|
||||||
|
tcp_cli = socket.socket()
|
||||||
|
tcp_cli.connect(('127.0.0.1', 7001))
|
||||||
|
tcp_cli.send(data)
|
||||||
|
tcp_cli.recv(4096)
|
||||||
|
tcp_cli.close()
|
||||||
|
|
||||||
|
data, addr = cli.recvfrom(1506)
|
||||||
|
data = common.to_str(data)
|
||||||
|
assert data.startswith('stat: ')
|
||||||
|
data = data.split('stat:')[1]
|
||||||
|
stats = shell.parse_json_in_str(data)
|
||||||
|
assert '7001' in stats
|
||||||
|
logging.info('TCP statistics test passed')
|
||||||
|
|
||||||
|
# test statistics for UDP
|
||||||
|
header = common.pack_addr(b'127.0.0.1') + struct.pack('>H', 80)
|
||||||
|
data = encrypt.encrypt_all(b'foobar2', 'aes-256-cfb', 1,
|
||||||
|
header + b'test')
|
||||||
|
udp_cli = socket.socket(type=socket.SOCK_DGRAM)
|
||||||
|
udp_cli.sendto(data, ('127.0.0.1', 8382))
|
||||||
|
tcp_cli.close()
|
||||||
|
|
||||||
|
data, addr = cli.recvfrom(1506)
|
||||||
|
data = common.to_str(data)
|
||||||
|
assert data.startswith('stat: ')
|
||||||
|
data = data.split('stat:')[1]
|
||||||
|
stats = json.loads(data)
|
||||||
|
assert '8382' in stats
|
||||||
|
logging.info('UDP statistics test passed')
|
||||||
|
|
||||||
|
manager._loop.stop()
|
||||||
|
t.join()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test()
|
||||||
@ -0,0 +1,104 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
#
|
||||||
|
# Copyright 2015-2015 breakwa11
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
from __future__ import absolute_import, division, print_function, \
|
||||||
|
with_statement
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import hashlib
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from shadowsocks import common
|
||||||
|
from shadowsocks.obfsplugin import plain, http_simple, obfs_tls, verify, auth
|
||||||
|
|
||||||
|
|
||||||
|
method_supported = {}
|
||||||
|
method_supported.update(plain.obfs_map)
|
||||||
|
method_supported.update(http_simple.obfs_map)
|
||||||
|
method_supported.update(obfs_tls.obfs_map)
|
||||||
|
method_supported.update(verify.obfs_map)
|
||||||
|
method_supported.update(auth.obfs_map)
|
||||||
|
|
||||||
|
class server_info(object):
|
||||||
|
def __init__(self, data):
|
||||||
|
self.data = data
|
||||||
|
|
||||||
|
class obfs(object):
|
||||||
|
def __init__(self, method):
|
||||||
|
method = common.to_str(method)
|
||||||
|
self.method = method
|
||||||
|
self._method_info = self.get_method_info(method)
|
||||||
|
if self._method_info:
|
||||||
|
self.obfs = self.get_obfs(method)
|
||||||
|
else:
|
||||||
|
raise Exception('obfs plugin [%s] not supported' % method)
|
||||||
|
|
||||||
|
def init_data(self):
|
||||||
|
return self.obfs.init_data()
|
||||||
|
|
||||||
|
def set_server_info(self, server_info):
|
||||||
|
return self.obfs.set_server_info(server_info)
|
||||||
|
|
||||||
|
def get_method_info(self, method):
|
||||||
|
method = method.lower()
|
||||||
|
m = method_supported.get(method)
|
||||||
|
return m
|
||||||
|
|
||||||
|
def get_obfs(self, method):
|
||||||
|
m = self._method_info
|
||||||
|
return m[0](method)
|
||||||
|
|
||||||
|
def client_pre_encrypt(self, buf):
|
||||||
|
return self.obfs.client_pre_encrypt(buf)
|
||||||
|
|
||||||
|
def client_encode(self, buf):
|
||||||
|
return self.obfs.client_encode(buf)
|
||||||
|
|
||||||
|
def client_decode(self, buf):
|
||||||
|
return self.obfs.client_decode(buf)
|
||||||
|
|
||||||
|
def client_post_decrypt(self, buf):
|
||||||
|
return self.obfs.client_post_decrypt(buf)
|
||||||
|
|
||||||
|
def server_pre_encrypt(self, buf):
|
||||||
|
return self.obfs.server_pre_encrypt(buf)
|
||||||
|
|
||||||
|
def server_encode(self, buf):
|
||||||
|
return self.obfs.server_encode(buf)
|
||||||
|
|
||||||
|
def server_decode(self, buf):
|
||||||
|
return self.obfs.server_decode(buf)
|
||||||
|
|
||||||
|
def server_post_decrypt(self, buf):
|
||||||
|
return self.obfs.server_post_decrypt(buf)
|
||||||
|
|
||||||
|
def client_udp_pre_encrypt(self, buf):
|
||||||
|
return self.obfs.client_udp_pre_encrypt(buf)
|
||||||
|
|
||||||
|
def client_udp_post_decrypt(self, buf):
|
||||||
|
return self.obfs.client_udp_post_decrypt(buf)
|
||||||
|
|
||||||
|
def server_udp_pre_encrypt(self, buf):
|
||||||
|
return self.obfs.server_udp_pre_encrypt(buf)
|
||||||
|
|
||||||
|
def server_udp_post_decrypt(self, buf):
|
||||||
|
return self.obfs.server_udp_post_decrypt(buf)
|
||||||
|
|
||||||
|
def dispose(self):
|
||||||
|
self.obfs.dispose()
|
||||||
|
del self.obfs
|
||||||
|
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
#
|
||||||
|
# Copyright 2015 clowwindy
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
from __future__ import absolute_import, division, print_function, \
|
||||||
|
with_statement
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,307 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
#
|
||||||
|
# Copyright 2015-2015 breakwa11
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
from __future__ import absolute_import, division, print_function, \
|
||||||
|
with_statement
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import hashlib
|
||||||
|
import logging
|
||||||
|
import binascii
|
||||||
|
import struct
|
||||||
|
import base64
|
||||||
|
import datetime
|
||||||
|
import random
|
||||||
|
|
||||||
|
from shadowsocks import common
|
||||||
|
from shadowsocks.obfsplugin import plain
|
||||||
|
from shadowsocks.common import to_bytes, to_str, ord, chr
|
||||||
|
|
||||||
|
def create_http_simple_obfs(method):
|
||||||
|
return http_simple(method)
|
||||||
|
|
||||||
|
def create_http_post_obfs(method):
|
||||||
|
return http_post(method)
|
||||||
|
|
||||||
|
def create_random_head_obfs(method):
|
||||||
|
return random_head(method)
|
||||||
|
|
||||||
|
obfs_map = {
|
||||||
|
'http_simple': (create_http_simple_obfs,),
|
||||||
|
'http_simple_compatible': (create_http_simple_obfs,),
|
||||||
|
'http_post': (create_http_post_obfs,),
|
||||||
|
'http_post_compatible': (create_http_post_obfs,),
|
||||||
|
'random_head': (create_random_head_obfs,),
|
||||||
|
'random_head_compatible': (create_random_head_obfs,),
|
||||||
|
}
|
||||||
|
|
||||||
|
def match_begin(str1, str2):
|
||||||
|
if len(str1) >= len(str2):
|
||||||
|
if str1[:len(str2)] == str2:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
class http_simple(plain.plain):
|
||||||
|
def __init__(self, method):
|
||||||
|
self.method = method
|
||||||
|
self.has_sent_header = False
|
||||||
|
self.has_recv_header = False
|
||||||
|
self.host = None
|
||||||
|
self.port = 0
|
||||||
|
self.recv_buffer = b''
|
||||||
|
self.user_agent = [b"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0",
|
||||||
|
b"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:40.0) Gecko/20100101 Firefox/44.0",
|
||||||
|
b"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
|
||||||
|
b"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Ubuntu/11.10 Chromium/27.0.1453.93 Chrome/27.0.1453.93 Safari/537.36",
|
||||||
|
b"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0",
|
||||||
|
b"Mozilla/5.0 (compatible; WOW64; MSIE 10.0; Windows NT 6.2)",
|
||||||
|
b"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27",
|
||||||
|
b"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; Trident/7.0; .NET4.0E; .NET4.0C)",
|
||||||
|
b"Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko",
|
||||||
|
b"Mozilla/5.0 (Linux; Android 4.4; Nexus 5 Build/BuildID) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36",
|
||||||
|
b"Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3",
|
||||||
|
b"Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3"]
|
||||||
|
|
||||||
|
def encode_head(self, buf):
|
||||||
|
hexstr = binascii.hexlify(buf)
|
||||||
|
chs = []
|
||||||
|
for i in range(0, len(hexstr), 2):
|
||||||
|
chs.append(b"%" + hexstr[i:i+2])
|
||||||
|
return b''.join(chs)
|
||||||
|
|
||||||
|
def client_encode(self, buf):
|
||||||
|
if self.has_sent_header:
|
||||||
|
return buf
|
||||||
|
head_size = len(self.server_info.iv) + self.server_info.head_len
|
||||||
|
if len(buf) - head_size > 64:
|
||||||
|
headlen = head_size + random.randint(0, 64)
|
||||||
|
else:
|
||||||
|
headlen = len(buf)
|
||||||
|
headdata = buf[:headlen]
|
||||||
|
buf = buf[headlen:]
|
||||||
|
port = b''
|
||||||
|
if self.server_info.port != 80:
|
||||||
|
port = b':' + to_bytes(str(self.server_info.port))
|
||||||
|
body = None
|
||||||
|
hosts = (self.server_info.obfs_param or self.server_info.host)
|
||||||
|
pos = hosts.find("#")
|
||||||
|
if pos >= 0:
|
||||||
|
body = hosts[pos + 1:].replace("\n", "\r\n")
|
||||||
|
body = body.replace("\\n", "\r\n")
|
||||||
|
hosts = hosts[:pos]
|
||||||
|
hosts = hosts.split(',')
|
||||||
|
host = random.choice(hosts)
|
||||||
|
http_head = b"GET /" + self.encode_head(headdata) + b" HTTP/1.1\r\n"
|
||||||
|
http_head += b"Host: " + to_bytes(host) + port + b"\r\n"
|
||||||
|
if body:
|
||||||
|
http_head += body + "\r\n\r\n"
|
||||||
|
else:
|
||||||
|
http_head += b"User-Agent: " + random.choice(self.user_agent) + b"\r\n"
|
||||||
|
http_head += b"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-US,en;q=0.8\r\nAccept-Encoding: gzip, deflate\r\nDNT: 1\r\nConnection: keep-alive\r\n\r\n"
|
||||||
|
self.has_sent_header = True
|
||||||
|
return http_head + buf
|
||||||
|
|
||||||
|
def client_decode(self, buf):
|
||||||
|
if self.has_recv_header:
|
||||||
|
return (buf, False)
|
||||||
|
pos = buf.find(b'\r\n\r\n')
|
||||||
|
if pos >= 0:
|
||||||
|
self.has_recv_header = True
|
||||||
|
return (buf[pos + 4:], False)
|
||||||
|
else:
|
||||||
|
return (b'', False)
|
||||||
|
|
||||||
|
def server_encode(self, buf):
|
||||||
|
if self.has_sent_header:
|
||||||
|
return buf
|
||||||
|
|
||||||
|
header = b'HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Encoding: gzip\r\nContent-Type: text/html\r\nDate: '
|
||||||
|
header += to_bytes(datetime.datetime.now().strftime('%a, %d %b %Y %H:%M:%S GMT'))
|
||||||
|
header += b'\r\nServer: nginx\r\nVary: Accept-Encoding\r\n\r\n'
|
||||||
|
self.has_sent_header = True
|
||||||
|
return header + buf
|
||||||
|
|
||||||
|
def get_data_from_http_header(self, buf):
|
||||||
|
ret_buf = b''
|
||||||
|
lines = buf.split(b'\r\n')
|
||||||
|
if lines and len(lines) > 4:
|
||||||
|
hex_items = lines[0].split(b'%')
|
||||||
|
if hex_items and len(hex_items) > 1:
|
||||||
|
for index in range(1, len(hex_items)):
|
||||||
|
if len(hex_items[index]) < 2:
|
||||||
|
ret_buf += binascii.unhexlify('0' + hex_items[index])
|
||||||
|
break
|
||||||
|
elif len(hex_items[index]) > 2:
|
||||||
|
ret_buf += binascii.unhexlify(hex_items[index][:2])
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
ret_buf += binascii.unhexlify(hex_items[index])
|
||||||
|
return ret_buf
|
||||||
|
return b''
|
||||||
|
|
||||||
|
def get_host_from_http_header(self, buf):
|
||||||
|
ret_buf = b''
|
||||||
|
lines = buf.split(b'\r\n')
|
||||||
|
if lines and len(lines) > 4:
|
||||||
|
for line in lines:
|
||||||
|
if match_begin(line, b"Host: "):
|
||||||
|
return line[6:]
|
||||||
|
|
||||||
|
def not_match_return(self, buf):
|
||||||
|
self.has_sent_header = True
|
||||||
|
self.has_recv_header = True
|
||||||
|
if self.method == 'http_simple':
|
||||||
|
return (b'E'*2048, False, False)
|
||||||
|
return (buf, True, False)
|
||||||
|
|
||||||
|
def server_decode(self, buf):
|
||||||
|
if self.has_recv_header:
|
||||||
|
return (buf, True, False)
|
||||||
|
|
||||||
|
self.recv_buffer += buf
|
||||||
|
buf = self.recv_buffer
|
||||||
|
if len(buf) > 10:
|
||||||
|
if match_begin(buf, b'GET ') or match_begin(buf, b'POST '):
|
||||||
|
if len(buf) > 65536:
|
||||||
|
self.recv_buffer = None
|
||||||
|
logging.warn('http_simple: over size')
|
||||||
|
return self.not_match_return(buf)
|
||||||
|
else: #not http header, run on original protocol
|
||||||
|
self.recv_buffer = None
|
||||||
|
logging.debug('http_simple: not match begin')
|
||||||
|
return self.not_match_return(buf)
|
||||||
|
else:
|
||||||
|
return (b'', True, False)
|
||||||
|
|
||||||
|
if b'\r\n\r\n' in buf:
|
||||||
|
datas = buf.split(b'\r\n\r\n', 1)
|
||||||
|
ret_buf = self.get_data_from_http_header(buf)
|
||||||
|
host = self.get_host_from_http_header(buf)
|
||||||
|
if host and self.server_info.obfs_param:
|
||||||
|
pos = host.find(":")
|
||||||
|
if pos >= 0:
|
||||||
|
host = host[:pos]
|
||||||
|
hosts = self.server_info.obfs_param.split(',')
|
||||||
|
if host not in hosts:
|
||||||
|
return self.not_match_return(buf)
|
||||||
|
if len(datas) > 1:
|
||||||
|
ret_buf += datas[1]
|
||||||
|
if len(ret_buf) >= 7:
|
||||||
|
self.has_recv_header = True
|
||||||
|
return (ret_buf, True, False)
|
||||||
|
return self.not_match_return(buf)
|
||||||
|
else:
|
||||||
|
return (b'', True, False)
|
||||||
|
|
||||||
|
class http_post(http_simple):
|
||||||
|
def __init__(self, method):
|
||||||
|
super(http_post, self).__init__(method)
|
||||||
|
|
||||||
|
def boundary(self):
|
||||||
|
return b''.join([random.choice(b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") for i in range(32)])
|
||||||
|
|
||||||
|
def client_encode(self, buf):
|
||||||
|
if self.has_sent_header:
|
||||||
|
return buf
|
||||||
|
head_size = len(self.server_info.iv) + self.server_info.head_len
|
||||||
|
if len(buf) - head_size > 64:
|
||||||
|
headlen = head_size + random.randint(0, 64)
|
||||||
|
else:
|
||||||
|
headlen = len(buf)
|
||||||
|
headdata = buf[:headlen]
|
||||||
|
buf = buf[headlen:]
|
||||||
|
port = b''
|
||||||
|
if self.server_info.port != 80:
|
||||||
|
port = b':' + to_bytes(str(self.server_info.port))
|
||||||
|
body = None
|
||||||
|
hosts = (self.server_info.obfs_param or self.server_info.host)
|
||||||
|
pos = hosts.find("#")
|
||||||
|
if pos >= 0:
|
||||||
|
body = hosts[pos + 1:].replace("\\n", "\r\n")
|
||||||
|
hosts = hosts[:pos]
|
||||||
|
hosts = hosts.split(',')
|
||||||
|
host = random.choice(hosts)
|
||||||
|
http_head = b"POST /" + self.encode_head(headdata) + b" HTTP/1.1\r\n"
|
||||||
|
http_head += b"Host: " + to_bytes(host) + port + b"\r\n"
|
||||||
|
if body:
|
||||||
|
http_head += body + "\r\n\r\n"
|
||||||
|
else:
|
||||||
|
http_head += b"User-Agent: " + random.choice(self.user_agent) + b"\r\n"
|
||||||
|
http_head += b"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-US,en;q=0.8\r\nAccept-Encoding: gzip, deflate\r\n"
|
||||||
|
http_head += b"Content-Type: multipart/form-data; boundary=" + self.boundary() + b"\r\nDNT: 1\r\n"
|
||||||
|
http_head += "Connection: keep-alive\r\n\r\n"
|
||||||
|
self.has_sent_header = True
|
||||||
|
return http_head + buf
|
||||||
|
|
||||||
|
def not_match_return(self, buf):
|
||||||
|
self.has_sent_header = True
|
||||||
|
self.has_recv_header = True
|
||||||
|
if self.method == 'http_post':
|
||||||
|
return (b'E'*2048, False, False)
|
||||||
|
return (buf, True, False)
|
||||||
|
|
||||||
|
class random_head(plain.plain):
|
||||||
|
def __init__(self, method):
|
||||||
|
self.method = method
|
||||||
|
self.has_sent_header = False
|
||||||
|
self.has_recv_header = False
|
||||||
|
self.raw_trans_sent = False
|
||||||
|
self.raw_trans_recv = False
|
||||||
|
self.send_buffer = b''
|
||||||
|
|
||||||
|
def client_encode(self, buf):
|
||||||
|
if self.raw_trans_sent:
|
||||||
|
return buf
|
||||||
|
self.send_buffer += buf
|
||||||
|
if not self.has_sent_header:
|
||||||
|
self.has_sent_header = True
|
||||||
|
data = os.urandom(common.ord(os.urandom(1)[0]) % 96 + 4)
|
||||||
|
crc = (0xffffffff - binascii.crc32(data)) & 0xffffffff
|
||||||
|
return data + struct.pack('<I', crc)
|
||||||
|
if self.raw_trans_recv:
|
||||||
|
ret = self.send_buffer
|
||||||
|
self.send_buffer = b''
|
||||||
|
self.raw_trans_sent = True
|
||||||
|
return ret
|
||||||
|
return b''
|
||||||
|
|
||||||
|
def client_decode(self, buf):
|
||||||
|
if self.raw_trans_recv:
|
||||||
|
return (buf, False)
|
||||||
|
self.raw_trans_recv = True
|
||||||
|
return (b'', True)
|
||||||
|
|
||||||
|
def server_encode(self, buf):
|
||||||
|
if self.has_sent_header:
|
||||||
|
return buf
|
||||||
|
self.has_sent_header = True
|
||||||
|
return os.urandom(common.ord(os.urandom(1)[0]) % 96 + 4)
|
||||||
|
|
||||||
|
def server_decode(self, buf):
|
||||||
|
if self.has_recv_header:
|
||||||
|
return (buf, True, False)
|
||||||
|
|
||||||
|
self.has_recv_header = True
|
||||||
|
crc = binascii.crc32(buf) & 0xffffffff
|
||||||
|
if crc != 0xffffffff:
|
||||||
|
self.has_sent_header = True
|
||||||
|
if self.method == 'random_head':
|
||||||
|
return (b'E'*2048, False, False)
|
||||||
|
return (buf, True, False)
|
||||||
|
# (buffer_to_recv, is_need_decrypt, is_need_to_encode_and_send_back)
|
||||||
|
return (b'', False, True)
|
||||||
|
|
||||||
@ -0,0 +1,270 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
#
|
||||||
|
# Copyright 2015-2015 breakwa11
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
from __future__ import absolute_import, division, print_function, \
|
||||||
|
with_statement
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import hashlib
|
||||||
|
import logging
|
||||||
|
import binascii
|
||||||
|
import struct
|
||||||
|
import base64
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
import hmac
|
||||||
|
import hashlib
|
||||||
|
import string
|
||||||
|
|
||||||
|
from shadowsocks import common
|
||||||
|
from shadowsocks.obfsplugin import plain
|
||||||
|
from shadowsocks.common import to_bytes, to_str, ord
|
||||||
|
from shadowsocks import lru_cache
|
||||||
|
|
||||||
|
def create_tls_ticket_auth_obfs(method):
|
||||||
|
return tls_ticket_auth(method)
|
||||||
|
|
||||||
|
obfs_map = {
|
||||||
|
'tls1.2_ticket_auth': (create_tls_ticket_auth_obfs,),
|
||||||
|
'tls1.2_ticket_auth_compatible': (create_tls_ticket_auth_obfs,),
|
||||||
|
}
|
||||||
|
|
||||||
|
def match_begin(str1, str2):
|
||||||
|
if len(str1) >= len(str2):
|
||||||
|
if str1[:len(str2)] == str2:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
class obfs_auth_data(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.client_data = lru_cache.LRUCache(60 * 5)
|
||||||
|
self.client_id = os.urandom(32)
|
||||||
|
self.startup_time = int(time.time() - 60 * 30) & 0xFFFFFFFF
|
||||||
|
|
||||||
|
class tls_ticket_auth(plain.plain):
|
||||||
|
def __init__(self, method):
|
||||||
|
self.method = method
|
||||||
|
self.handshake_status = 0
|
||||||
|
self.send_buffer = b''
|
||||||
|
self.recv_buffer = b''
|
||||||
|
self.client_id = b''
|
||||||
|
self.max_time_dif = 0 # time dif (second) setting
|
||||||
|
self.tls_version = b'\x03\x03'
|
||||||
|
|
||||||
|
def init_data(self):
|
||||||
|
return obfs_auth_data()
|
||||||
|
|
||||||
|
def sni(self, url):
|
||||||
|
url = common.to_bytes(url)
|
||||||
|
data = b"\x00" + struct.pack('>H', len(url)) + url
|
||||||
|
data = b"\x00\x00" + struct.pack('>H', len(data) + 2) + struct.pack('>H', len(data)) + data
|
||||||
|
return data
|
||||||
|
|
||||||
|
def pack_auth_data(self, client_id):
|
||||||
|
utc_time = int(time.time()) & 0xFFFFFFFF
|
||||||
|
data = struct.pack('>I', utc_time) + os.urandom(18)
|
||||||
|
data += hmac.new(self.server_info.key + client_id, data, hashlib.sha1).digest()[:10]
|
||||||
|
return data
|
||||||
|
|
||||||
|
def client_encode(self, buf):
|
||||||
|
if self.handshake_status == -1:
|
||||||
|
return buf
|
||||||
|
if self.handshake_status == 8:
|
||||||
|
ret = b''
|
||||||
|
while len(buf) > 2048:
|
||||||
|
size = min(struct.unpack('>H', os.urandom(2))[0] % 4096 + 100, len(buf))
|
||||||
|
ret += b"\x17" + self.tls_version + struct.pack('>H', size) + buf[:size]
|
||||||
|
buf = buf[size:]
|
||||||
|
if len(buf) > 0:
|
||||||
|
ret += b"\x17" + self.tls_version + struct.pack('>H', len(buf)) + buf
|
||||||
|
return ret
|
||||||
|
self.send_buffer += b"\x17" + self.tls_version + struct.pack('>H', len(buf)) + buf
|
||||||
|
if self.handshake_status == 0:
|
||||||
|
self.handshake_status = 1
|
||||||
|
data = self.tls_version + self.pack_auth_data(self.server_info.data.client_id) + b"\x20" + self.server_info.data.client_id + binascii.unhexlify(b"001cc02bc02fcca9cca8cc14cc13c00ac014c009c013009c0035002f000a" + b"0100")
|
||||||
|
ext = binascii.unhexlify(b"ff01000100")
|
||||||
|
host = self.server_info.obfs_param or self.server_info.host
|
||||||
|
if host and host[-1] in string.digits:
|
||||||
|
host = ''
|
||||||
|
hosts = host.split(',')
|
||||||
|
host = random.choice(hosts)
|
||||||
|
ext += self.sni(host)
|
||||||
|
ext += b"\x00\x17\x00\x00"
|
||||||
|
ext += b"\x00\x23\x00\xd0" + os.urandom(208) # ticket
|
||||||
|
ext += binascii.unhexlify(b"000d001600140601060305010503040104030301030302010203")
|
||||||
|
ext += binascii.unhexlify(b"000500050100000000")
|
||||||
|
ext += binascii.unhexlify(b"00120000")
|
||||||
|
ext += binascii.unhexlify(b"75500000")
|
||||||
|
ext += binascii.unhexlify(b"000b00020100")
|
||||||
|
ext += binascii.unhexlify(b"000a0006000400170018")
|
||||||
|
data += struct.pack('>H', len(ext)) + ext
|
||||||
|
data = b"\x01\x00" + struct.pack('>H', len(data)) + data
|
||||||
|
data = b"\x16\x03\x01" + struct.pack('>H', len(data)) + data
|
||||||
|
return data
|
||||||
|
elif self.handshake_status == 1 and len(buf) == 0:
|
||||||
|
data = b"\x14" + self.tls_version + b"\x00\x01\x01" #ChangeCipherSpec
|
||||||
|
data += b"\x16" + self.tls_version + b"\x00\x20" + os.urandom(22) #Finished
|
||||||
|
data += hmac.new(self.server_info.key + self.server_info.data.client_id, data, hashlib.sha1).digest()[:10]
|
||||||
|
ret = data + self.send_buffer
|
||||||
|
self.send_buffer = b''
|
||||||
|
self.handshake_status = 8
|
||||||
|
return ret
|
||||||
|
return b''
|
||||||
|
|
||||||
|
def client_decode(self, buf):
|
||||||
|
if self.handshake_status == -1:
|
||||||
|
return (buf, False)
|
||||||
|
|
||||||
|
if self.handshake_status == 8:
|
||||||
|
ret = b''
|
||||||
|
self.recv_buffer += buf
|
||||||
|
while len(self.recv_buffer) > 5:
|
||||||
|
if ord(self.recv_buffer[0]) != 0x17:
|
||||||
|
logging.info("data = %s" % (binascii.hexlify(self.recv_buffer)))
|
||||||
|
raise Exception('server_decode appdata error')
|
||||||
|
size = struct.unpack('>H', self.recv_buffer[3:5])[0]
|
||||||
|
if len(self.recv_buffer) < size + 5:
|
||||||
|
break
|
||||||
|
buf = self.recv_buffer[5:size+5]
|
||||||
|
ret += buf
|
||||||
|
self.recv_buffer = self.recv_buffer[size+5:]
|
||||||
|
return (ret, False)
|
||||||
|
|
||||||
|
if len(buf) < 11 + 32 + 1 + 32:
|
||||||
|
raise Exception('client_decode data error')
|
||||||
|
verify = buf[11:33]
|
||||||
|
if hmac.new(self.server_info.key + self.server_info.data.client_id, verify, hashlib.sha1).digest()[:10] != buf[33:43]:
|
||||||
|
raise Exception('client_decode data error')
|
||||||
|
return (b'', True)
|
||||||
|
|
||||||
|
def server_encode(self, buf):
|
||||||
|
if self.handshake_status == -1:
|
||||||
|
return buf
|
||||||
|
if self.handshake_status == 8:
|
||||||
|
ret = b''
|
||||||
|
while len(buf) > 2048:
|
||||||
|
size = min(struct.unpack('>H', os.urandom(2))[0] % 4096 + 100, len(buf))
|
||||||
|
ret += b"\x17" + self.tls_version + struct.pack('>H', size) + buf[:size]
|
||||||
|
buf = buf[size:]
|
||||||
|
if len(buf) > 0:
|
||||||
|
ret += b"\x17" + self.tls_version + struct.pack('>H', len(buf)) + buf
|
||||||
|
return ret
|
||||||
|
self.handshake_status = 3
|
||||||
|
data = self.tls_version + self.pack_auth_data(self.client_id) + b"\x20" + self.client_id + binascii.unhexlify(b"c02f000005ff01000100")
|
||||||
|
data = b"\x02\x00" + struct.pack('>H', len(data)) + data #server hello
|
||||||
|
data = b"\x16\x03\x03" + struct.pack('>H', len(data)) + data
|
||||||
|
data += b"\x14" + self.tls_version + b"\x00\x01\x01" #ChangeCipherSpec
|
||||||
|
data += b"\x16" + self.tls_version + b"\x00\x20" + os.urandom(22) #Finished
|
||||||
|
data += hmac.new(self.server_info.key + self.client_id, data, hashlib.sha1).digest()[:10]
|
||||||
|
return data
|
||||||
|
|
||||||
|
def decode_error_return(self, buf):
|
||||||
|
self.handshake_status = -1
|
||||||
|
if self.method == 'tls1.2_ticket_auth':
|
||||||
|
return (b'E'*2048, False, False)
|
||||||
|
return (buf, True, False)
|
||||||
|
|
||||||
|
def server_decode(self, buf):
|
||||||
|
if self.handshake_status == -1:
|
||||||
|
return (buf, True, False)
|
||||||
|
|
||||||
|
if self.handshake_status == 8:
|
||||||
|
ret = b''
|
||||||
|
self.recv_buffer += buf
|
||||||
|
while len(self.recv_buffer) > 5:
|
||||||
|
if ord(self.recv_buffer[0]) != 0x17 or ord(self.recv_buffer[1]) != 0x3 or ord(self.recv_buffer[2]) != 0x3:
|
||||||
|
logging.info("data = %s" % (binascii.hexlify(self.recv_buffer)))
|
||||||
|
raise Exception('server_decode appdata error')
|
||||||
|
size = struct.unpack('>H', self.recv_buffer[3:5])[0]
|
||||||
|
if len(self.recv_buffer) < size + 5:
|
||||||
|
break
|
||||||
|
ret += self.recv_buffer[5:size+5]
|
||||||
|
self.recv_buffer = self.recv_buffer[size+5:]
|
||||||
|
return (ret, True, False)
|
||||||
|
|
||||||
|
if self.handshake_status == 3:
|
||||||
|
verify = buf
|
||||||
|
verify_len = 43 - 10
|
||||||
|
if len(buf) < 43:
|
||||||
|
raise Exception('server_decode data error')
|
||||||
|
if not match_begin(buf, b"\x14" + self.tls_version + b"\x00\x01\x01"): #ChangeCipherSpec
|
||||||
|
raise Exception('server_decode data error')
|
||||||
|
buf = buf[6:]
|
||||||
|
if not match_begin(buf, b"\x16" + self.tls_version + b"\x00\x20"): #Finished
|
||||||
|
raise Exception('server_decode data error')
|
||||||
|
if hmac.new(self.server_info.key + self.client_id, verify[:verify_len], hashlib.sha1).digest()[:10] != verify[verify_len:verify_len+10]:
|
||||||
|
raise Exception('server_decode data error')
|
||||||
|
if len(buf) < 37:
|
||||||
|
raise Exception('server_decode data error')
|
||||||
|
self.recv_buffer = buf[37:]
|
||||||
|
self.handshake_status = 8
|
||||||
|
return self.server_decode(b'')
|
||||||
|
|
||||||
|
#raise Exception("handshake data = %s" % (binascii.hexlify(buf)))
|
||||||
|
self.handshake_status = 2
|
||||||
|
ogn_buf = buf
|
||||||
|
if not match_begin(buf, b'\x16\x03\x01'):
|
||||||
|
return self.decode_error_return(ogn_buf)
|
||||||
|
buf = buf[3:]
|
||||||
|
if struct.unpack('>H', buf[:2])[0] != len(buf) - 2:
|
||||||
|
logging.info("tls_auth wrong tls head size")
|
||||||
|
return self.decode_error_return(ogn_buf)
|
||||||
|
buf = buf[2:]
|
||||||
|
if not match_begin(buf, b'\x01\x00'): #client hello
|
||||||
|
logging.info("tls_auth not client hello message")
|
||||||
|
return self.decode_error_return(ogn_buf)
|
||||||
|
buf = buf[2:]
|
||||||
|
if struct.unpack('>H', buf[:2])[0] != len(buf) - 2:
|
||||||
|
logging.info("tls_auth wrong message size")
|
||||||
|
return self.decode_error_return(ogn_buf)
|
||||||
|
buf = buf[2:]
|
||||||
|
if not match_begin(buf, self.tls_version):
|
||||||
|
logging.info("tls_auth wrong tls version")
|
||||||
|
return self.decode_error_return(ogn_buf)
|
||||||
|
buf = buf[2:]
|
||||||
|
verifyid = buf[:32]
|
||||||
|
buf = buf[32:]
|
||||||
|
sessionid_len = ord(buf[0])
|
||||||
|
if sessionid_len < 32:
|
||||||
|
logging.info("tls_auth wrong sessionid_len")
|
||||||
|
return self.decode_error_return(ogn_buf)
|
||||||
|
sessionid = buf[1:sessionid_len + 1]
|
||||||
|
buf = buf[sessionid_len+1:]
|
||||||
|
self.client_id = sessionid
|
||||||
|
sha1 = hmac.new(self.server_info.key + sessionid, verifyid[:22], hashlib.sha1).digest()[:10]
|
||||||
|
utc_time = struct.unpack('>I', verifyid[:4])[0]
|
||||||
|
time_dif = common.int32((int(time.time()) & 0xffffffff) - utc_time)
|
||||||
|
if self.server_info.obfs_param:
|
||||||
|
try:
|
||||||
|
self.max_time_dif = int(self.server_info.obfs_param)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
if self.max_time_dif > 0 and (time_dif < -self.max_time_dif or time_dif > self.max_time_dif \
|
||||||
|
or common.int32(utc_time - self.server_info.data.startup_time) < -self.max_time_dif / 2):
|
||||||
|
logging.info("tls_auth wrong time")
|
||||||
|
return self.decode_error_return(ogn_buf)
|
||||||
|
if sha1 != verifyid[22:]:
|
||||||
|
logging.info("tls_auth wrong sha1")
|
||||||
|
return self.decode_error_return(ogn_buf)
|
||||||
|
if self.server_info.data.client_data.get(verifyid[:22]):
|
||||||
|
logging.info("replay attack detect, id = %s" % (binascii.hexlify(verifyid)))
|
||||||
|
return self.decode_error_return(ogn_buf)
|
||||||
|
self.server_info.data.client_data.sweep()
|
||||||
|
self.server_info.data.client_data[verifyid[:22]] = sessionid
|
||||||
|
# (buffer_to_recv, is_need_decrypt, is_need_to_encode_and_send_back)
|
||||||
|
return (b'', False, True)
|
||||||
|
|
||||||
@ -0,0 +1,98 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
#
|
||||||
|
# Copyright 2015-2015 breakwa11
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
from __future__ import absolute_import, division, print_function, \
|
||||||
|
with_statement
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import hashlib
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from shadowsocks.common import ord
|
||||||
|
|
||||||
|
def create_obfs(method):
|
||||||
|
return plain(method)
|
||||||
|
|
||||||
|
obfs_map = {
|
||||||
|
'plain': (create_obfs,),
|
||||||
|
'origin': (create_obfs,),
|
||||||
|
}
|
||||||
|
|
||||||
|
class plain(object):
|
||||||
|
def __init__(self, method):
|
||||||
|
self.method = method
|
||||||
|
self.server_info = None
|
||||||
|
|
||||||
|
def init_data(self):
|
||||||
|
return b''
|
||||||
|
|
||||||
|
def set_server_info(self, server_info):
|
||||||
|
self.server_info = server_info
|
||||||
|
|
||||||
|
def client_pre_encrypt(self, buf):
|
||||||
|
return buf
|
||||||
|
|
||||||
|
def client_encode(self, buf):
|
||||||
|
return buf
|
||||||
|
|
||||||
|
def client_decode(self, buf):
|
||||||
|
# (buffer_to_recv, is_need_to_encode_and_send_back)
|
||||||
|
return (buf, False)
|
||||||
|
|
||||||
|
def client_post_decrypt(self, buf):
|
||||||
|
return buf
|
||||||
|
|
||||||
|
def server_pre_encrypt(self, buf):
|
||||||
|
return buf
|
||||||
|
|
||||||
|
def server_encode(self, buf):
|
||||||
|
return buf
|
||||||
|
|
||||||
|
def server_decode(self, buf):
|
||||||
|
# (buffer_to_recv, is_need_decrypt, is_need_to_encode_and_send_back)
|
||||||
|
return (buf, True, False)
|
||||||
|
|
||||||
|
def server_post_decrypt(self, buf):
|
||||||
|
return (buf, False)
|
||||||
|
|
||||||
|
def client_udp_pre_encrypt(self, buf):
|
||||||
|
return buf
|
||||||
|
|
||||||
|
def client_udp_post_decrypt(self, buf):
|
||||||
|
return buf
|
||||||
|
|
||||||
|
def server_udp_pre_encrypt(self, buf):
|
||||||
|
return buf
|
||||||
|
|
||||||
|
def server_udp_post_decrypt(self, buf):
|
||||||
|
return buf
|
||||||
|
|
||||||
|
def dispose(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_head_size(self, buf, def_value):
|
||||||
|
if len(buf) < 2:
|
||||||
|
return def_value
|
||||||
|
head_type = ord(buf[0]) & 0x7
|
||||||
|
if head_type == 1:
|
||||||
|
return 7
|
||||||
|
if head_type == 4:
|
||||||
|
return 19
|
||||||
|
if head_type == 3:
|
||||||
|
return 4 + ord(buf[1])
|
||||||
|
return def_value
|
||||||
|
|
||||||
@ -0,0 +1,360 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
#
|
||||||
|
# Copyright 2015-2015 breakwa11
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
from __future__ import absolute_import, division, print_function, \
|
||||||
|
with_statement
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import hashlib
|
||||||
|
import logging
|
||||||
|
import binascii
|
||||||
|
import base64
|
||||||
|
import time
|
||||||
|
import datetime
|
||||||
|
import random
|
||||||
|
import struct
|
||||||
|
import zlib
|
||||||
|
import hmac
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
import shadowsocks
|
||||||
|
from shadowsocks import common
|
||||||
|
from shadowsocks.obfsplugin import plain
|
||||||
|
from shadowsocks.common import to_bytes, to_str, ord, chr
|
||||||
|
|
||||||
|
def create_verify_obfs(method):
|
||||||
|
return verify_simple(method)
|
||||||
|
|
||||||
|
def create_verify_deflate(method):
|
||||||
|
return verify_deflate(method)
|
||||||
|
|
||||||
|
def create_verify_sha1(method):
|
||||||
|
return verify_sha1(method)
|
||||||
|
|
||||||
|
def create_auth_obfs(method):
|
||||||
|
return auth_simple(method)
|
||||||
|
|
||||||
|
obfs_map = {
|
||||||
|
'verify_simple': (create_verify_obfs,),
|
||||||
|
'verify_deflate': (create_verify_deflate,),
|
||||||
|
'verify_sha1': (create_verify_sha1,),
|
||||||
|
'verify_sha1_compatible': (create_verify_sha1,),
|
||||||
|
}
|
||||||
|
|
||||||
|
def match_begin(str1, str2):
|
||||||
|
if len(str1) >= len(str2):
|
||||||
|
if str1[:len(str2)] == str2:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
class obfs_verify_data(object):
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class verify_base(plain.plain):
|
||||||
|
def __init__(self, method):
|
||||||
|
super(verify_base, self).__init__(method)
|
||||||
|
self.method = method
|
||||||
|
|
||||||
|
def init_data(self):
|
||||||
|
return obfs_verify_data()
|
||||||
|
|
||||||
|
def set_server_info(self, server_info):
|
||||||
|
self.server_info = server_info
|
||||||
|
|
||||||
|
def client_encode(self, buf):
|
||||||
|
return buf
|
||||||
|
|
||||||
|
def client_decode(self, buf):
|
||||||
|
return (buf, False)
|
||||||
|
|
||||||
|
def server_encode(self, buf):
|
||||||
|
return buf
|
||||||
|
|
||||||
|
def server_decode(self, buf):
|
||||||
|
return (buf, True, False)
|
||||||
|
|
||||||
|
class verify_simple(verify_base):
|
||||||
|
def __init__(self, method):
|
||||||
|
super(verify_simple, self).__init__(method)
|
||||||
|
self.recv_buf = b''
|
||||||
|
self.unit_len = 8100
|
||||||
|
self.decrypt_packet_num = 0
|
||||||
|
self.raw_trans = False
|
||||||
|
|
||||||
|
def pack_data(self, buf):
|
||||||
|
if len(buf) == 0:
|
||||||
|
return b''
|
||||||
|
rnd_data = os.urandom(common.ord(os.urandom(1)[0]) % 16)
|
||||||
|
data = common.chr(len(rnd_data) + 1) + rnd_data + buf
|
||||||
|
data = struct.pack('>H', len(data) + 6) + data
|
||||||
|
crc = (0xffffffff - binascii.crc32(data)) & 0xffffffff
|
||||||
|
data += struct.pack('<I', crc)
|
||||||
|
return data
|
||||||
|
|
||||||
|
def client_pre_encrypt(self, buf):
|
||||||
|
ret = b''
|
||||||
|
while len(buf) > self.unit_len:
|
||||||
|
ret += self.pack_data(buf[:self.unit_len])
|
||||||
|
buf = buf[self.unit_len:]
|
||||||
|
ret += self.pack_data(buf)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def client_post_decrypt(self, buf):
|
||||||
|
if self.raw_trans:
|
||||||
|
return buf
|
||||||
|
self.recv_buf += buf
|
||||||
|
out_buf = b''
|
||||||
|
while len(self.recv_buf) > 2:
|
||||||
|
length = struct.unpack('>H', self.recv_buf[:2])[0]
|
||||||
|
if length >= 8192 or length < 7:
|
||||||
|
self.raw_trans = True
|
||||||
|
self.recv_buf = b''
|
||||||
|
raise Exception('client_post_decrypt data error')
|
||||||
|
if length > len(self.recv_buf):
|
||||||
|
break
|
||||||
|
|
||||||
|
if (binascii.crc32(self.recv_buf[:length]) & 0xffffffff) != 0xffffffff:
|
||||||
|
self.raw_trans = True
|
||||||
|
self.recv_buf = b''
|
||||||
|
raise Exception('client_post_decrypt data uncorrect CRC32')
|
||||||
|
|
||||||
|
pos = common.ord(self.recv_buf[2]) + 2
|
||||||
|
out_buf += self.recv_buf[pos:length - 4]
|
||||||
|
self.recv_buf = self.recv_buf[length:]
|
||||||
|
|
||||||
|
if out_buf:
|
||||||
|
self.decrypt_packet_num += 1
|
||||||
|
return out_buf
|
||||||
|
|
||||||
|
def server_pre_encrypt(self, buf):
|
||||||
|
ret = b''
|
||||||
|
while len(buf) > self.unit_len:
|
||||||
|
ret += self.pack_data(buf[:self.unit_len])
|
||||||
|
buf = buf[self.unit_len:]
|
||||||
|
ret += self.pack_data(buf)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def server_post_decrypt(self, buf):
|
||||||
|
if self.raw_trans:
|
||||||
|
return (buf, False)
|
||||||
|
self.recv_buf += buf
|
||||||
|
out_buf = b''
|
||||||
|
while len(self.recv_buf) > 2:
|
||||||
|
length = struct.unpack('>H', self.recv_buf[:2])[0]
|
||||||
|
if length >= 8192 or length < 7:
|
||||||
|
self.raw_trans = True
|
||||||
|
self.recv_buf = b''
|
||||||
|
if self.decrypt_packet_num == 0:
|
||||||
|
return (b'E'*2048, False)
|
||||||
|
else:
|
||||||
|
raise Exception('server_post_decrype data error')
|
||||||
|
if length > len(self.recv_buf):
|
||||||
|
break
|
||||||
|
|
||||||
|
if (binascii.crc32(self.recv_buf[:length]) & 0xffffffff) != 0xffffffff:
|
||||||
|
self.raw_trans = True
|
||||||
|
self.recv_buf = b''
|
||||||
|
if self.decrypt_packet_num == 0:
|
||||||
|
return (b'E'*2048, False)
|
||||||
|
else:
|
||||||
|
raise Exception('server_post_decrype data uncorrect CRC32')
|
||||||
|
|
||||||
|
pos = common.ord(self.recv_buf[2]) + 2
|
||||||
|
out_buf += self.recv_buf[pos:length - 4]
|
||||||
|
self.recv_buf = self.recv_buf[length:]
|
||||||
|
|
||||||
|
if out_buf:
|
||||||
|
self.decrypt_packet_num += 1
|
||||||
|
return (out_buf, False)
|
||||||
|
|
||||||
|
class verify_deflate(verify_base):
|
||||||
|
def __init__(self, method):
|
||||||
|
super(verify_deflate, self).__init__(method)
|
||||||
|
self.recv_buf = b''
|
||||||
|
self.unit_len = 32700
|
||||||
|
self.decrypt_packet_num = 0
|
||||||
|
self.raw_trans = False
|
||||||
|
|
||||||
|
def pack_data(self, buf):
|
||||||
|
if len(buf) == 0:
|
||||||
|
return b''
|
||||||
|
data = zlib.compress(buf)
|
||||||
|
data = struct.pack('>H', len(data)) + data[2:]
|
||||||
|
return data
|
||||||
|
|
||||||
|
def client_pre_encrypt(self, buf):
|
||||||
|
ret = b''
|
||||||
|
while len(buf) > self.unit_len:
|
||||||
|
ret += self.pack_data(buf[:self.unit_len])
|
||||||
|
buf = buf[self.unit_len:]
|
||||||
|
ret += self.pack_data(buf)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def client_post_decrypt(self, buf):
|
||||||
|
if self.raw_trans:
|
||||||
|
return buf
|
||||||
|
self.recv_buf += buf
|
||||||
|
out_buf = b''
|
||||||
|
while len(self.recv_buf) > 2:
|
||||||
|
length = struct.unpack('>H', self.recv_buf[:2])[0]
|
||||||
|
if length >= 32768 or length < 6:
|
||||||
|
self.raw_trans = True
|
||||||
|
self.recv_buf = b''
|
||||||
|
raise Exception('client_post_decrypt data error')
|
||||||
|
if length > len(self.recv_buf):
|
||||||
|
break
|
||||||
|
|
||||||
|
out_buf += zlib.decompress(b'x\x9c' + self.recv_buf[2:length])
|
||||||
|
self.recv_buf = self.recv_buf[length:]
|
||||||
|
|
||||||
|
if out_buf:
|
||||||
|
self.decrypt_packet_num += 1
|
||||||
|
return out_buf
|
||||||
|
|
||||||
|
def server_pre_encrypt(self, buf):
|
||||||
|
ret = b''
|
||||||
|
while len(buf) > self.unit_len:
|
||||||
|
ret += self.pack_data(buf[:self.unit_len])
|
||||||
|
buf = buf[self.unit_len:]
|
||||||
|
ret += self.pack_data(buf)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def server_post_decrypt(self, buf):
|
||||||
|
if self.raw_trans:
|
||||||
|
return (buf, False)
|
||||||
|
self.recv_buf += buf
|
||||||
|
out_buf = b''
|
||||||
|
while len(self.recv_buf) > 2:
|
||||||
|
length = struct.unpack('>H', self.recv_buf[:2])[0]
|
||||||
|
if length >= 32768 or length < 6:
|
||||||
|
self.raw_trans = True
|
||||||
|
self.recv_buf = b''
|
||||||
|
if self.decrypt_packet_num == 0:
|
||||||
|
return (b'E'*2048, False)
|
||||||
|
else:
|
||||||
|
raise Exception('server_post_decrype data error')
|
||||||
|
if length > len(self.recv_buf):
|
||||||
|
break
|
||||||
|
|
||||||
|
out_buf += zlib.decompress(b'\x78\x9c' + self.recv_buf[2:length])
|
||||||
|
self.recv_buf = self.recv_buf[length:]
|
||||||
|
|
||||||
|
if out_buf:
|
||||||
|
self.decrypt_packet_num += 1
|
||||||
|
return (out_buf, False)
|
||||||
|
|
||||||
|
class verify_sha1(verify_base):
|
||||||
|
def __init__(self, method):
|
||||||
|
super(verify_sha1, self).__init__(method)
|
||||||
|
self.recv_buf = b''
|
||||||
|
self.unit_len = 8100
|
||||||
|
self.raw_trans = False
|
||||||
|
self.pack_id = 0
|
||||||
|
self.recv_id = 0
|
||||||
|
self.has_sent_header = False
|
||||||
|
self.has_recv_header = False
|
||||||
|
|
||||||
|
def pack_data(self, buf):
|
||||||
|
if len(buf) == 0:
|
||||||
|
return b''
|
||||||
|
sha1data = hmac.new(self.server_info.iv + struct.pack('>I', self.pack_id), buf, hashlib.sha1).digest()
|
||||||
|
data = struct.pack('>H', len(buf)) + sha1data[:10] + buf
|
||||||
|
self.pack_id += 1
|
||||||
|
return data
|
||||||
|
|
||||||
|
def pack_auth_data(self, buf):
|
||||||
|
data = chr(ord(buf[0]) | 0x10) + buf[1:]
|
||||||
|
data += hmac.new(self.server_info.iv + self.server_info.key, data, hashlib.sha1).digest()[:10]
|
||||||
|
return data
|
||||||
|
|
||||||
|
def client_pre_encrypt(self, buf):
|
||||||
|
ret = b''
|
||||||
|
if not self.has_sent_header:
|
||||||
|
datalen = self.get_head_size(buf, 30)
|
||||||
|
ret += self.pack_auth_data(buf[:datalen])
|
||||||
|
buf = buf[datalen:]
|
||||||
|
self.has_sent_header = True
|
||||||
|
while len(buf) > self.unit_len:
|
||||||
|
ret += self.pack_data(buf[:self.unit_len])
|
||||||
|
buf = buf[self.unit_len:]
|
||||||
|
ret += self.pack_data(buf)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def client_post_decrypt(self, buf):
|
||||||
|
return buf
|
||||||
|
|
||||||
|
def server_pre_encrypt(self, buf):
|
||||||
|
return buf
|
||||||
|
|
||||||
|
def not_match_return(self, buf):
|
||||||
|
self.raw_trans = True
|
||||||
|
if self.method == 'verify_sha1':
|
||||||
|
return (b'E'*2048, False)
|
||||||
|
return (buf, False)
|
||||||
|
|
||||||
|
def server_post_decrypt(self, buf):
|
||||||
|
if self.raw_trans:
|
||||||
|
return (buf, False)
|
||||||
|
self.recv_buf += buf
|
||||||
|
out_buf = b''
|
||||||
|
if not self.has_recv_header:
|
||||||
|
if len(self.recv_buf) < 2:
|
||||||
|
return (b'', False)
|
||||||
|
if (ord(self.recv_buf[0]) & 0x10) != 0x10:
|
||||||
|
return self.not_match_return(self.recv_buf)
|
||||||
|
head_size = self.get_head_size(self.recv_buf, 65536)
|
||||||
|
if len(self.recv_buf) < head_size + 10:
|
||||||
|
return self.not_match_return(self.recv_buf)
|
||||||
|
sha1data = hmac.new(self.server_info.recv_iv + self.server_info.key, self.recv_buf[:head_size], hashlib.sha1).digest()[:10]
|
||||||
|
if sha1data != self.recv_buf[head_size:head_size + 10]:
|
||||||
|
logging.error('server_post_decrype data uncorrect auth HMAC-SHA1')
|
||||||
|
return self.not_match_return(self.recv_buf)
|
||||||
|
out_buf = to_bytes(chr(ord(self.recv_buf[0]) & 0xEF)) + self.recv_buf[1:head_size]
|
||||||
|
self.recv_buf = self.recv_buf[head_size + 10:]
|
||||||
|
self.has_recv_header = True
|
||||||
|
while len(self.recv_buf) > 2:
|
||||||
|
length = struct.unpack('>H', self.recv_buf[:2])[0] + 12
|
||||||
|
if length > len(self.recv_buf):
|
||||||
|
break
|
||||||
|
|
||||||
|
data = self.recv_buf[12:length]
|
||||||
|
sha1data = hmac.new(self.server_info.recv_iv + struct.pack('>I', self.recv_id), data, hashlib.sha1).digest()[:10]
|
||||||
|
if sha1data != self.recv_buf[2:12]:
|
||||||
|
raise Exception('server_post_decrype data uncorrect chunk HMAC-SHA1')
|
||||||
|
|
||||||
|
self.recv_id = (self.recv_id + 1) & 0xFFFFFFFF
|
||||||
|
out_buf += data
|
||||||
|
self.recv_buf = self.recv_buf[length:]
|
||||||
|
|
||||||
|
return (out_buf, False)
|
||||||
|
|
||||||
|
def client_udp_pre_encrypt(self, buf):
|
||||||
|
ret = self.pack_auth_data(buf)
|
||||||
|
return chr(ord(buf[0]) | 0x10) + buf[1:]
|
||||||
|
|
||||||
|
def server_udp_post_decrypt(self, buf):
|
||||||
|
if buf and ((ord(buf[0]) & 0x10) == 0x10):
|
||||||
|
if len(buf) <= 11:
|
||||||
|
return b''
|
||||||
|
sha1data = hmac.new(self.server_info.recv_iv + self.server_info.key, buf[:-10], hashlib.sha1).digest()[:10]
|
||||||
|
if sha1data != buf[-10:]:
|
||||||
|
return b''
|
||||||
|
return to_bytes(chr(ord(buf[0]) & 0xEF)) + buf[1:-10]
|
||||||
|
else:
|
||||||
|
return buf
|
||||||
|
|
||||||
@ -0,0 +1,214 @@
|
|||||||
|
import collections
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
### OrderedDict
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
class OrderedDict(dict):
|
||||||
|
'Dictionary that remembers insertion order'
|
||||||
|
# An inherited dict maps keys to values.
|
||||||
|
# The inherited dict provides __getitem__, __len__, __contains__, and get.
|
||||||
|
# The remaining methods are order-aware.
|
||||||
|
# Big-O running times for all methods are the same as regular dictionaries.
|
||||||
|
|
||||||
|
# The internal self.__map dict maps keys to links in a doubly linked list.
|
||||||
|
# The circular doubly linked list starts and ends with a sentinel element.
|
||||||
|
# The sentinel element never gets deleted (this simplifies the algorithm).
|
||||||
|
# Each link is stored as a list of length three: [PREV, NEXT, KEY].
|
||||||
|
|
||||||
|
def __init__(*args, **kwds):
|
||||||
|
'''Initialize an ordered dictionary. The signature is the same as
|
||||||
|
regular dictionaries, but keyword arguments are not recommended because
|
||||||
|
their insertion order is arbitrary.
|
||||||
|
|
||||||
|
'''
|
||||||
|
if not args:
|
||||||
|
raise TypeError("descriptor '__init__' of 'OrderedDict' object "
|
||||||
|
"needs an argument")
|
||||||
|
self = args[0]
|
||||||
|
args = args[1:]
|
||||||
|
if len(args) > 1:
|
||||||
|
raise TypeError('expected at most 1 arguments, got %d' % len(args))
|
||||||
|
try:
|
||||||
|
self.__root
|
||||||
|
except AttributeError:
|
||||||
|
self.__root = root = [] # sentinel node
|
||||||
|
root[:] = [root, root, None]
|
||||||
|
self.__map = {}
|
||||||
|
self.__update(*args, **kwds)
|
||||||
|
|
||||||
|
def __setitem__(self, key, value, dict_setitem=dict.__setitem__):
|
||||||
|
'od.__setitem__(i, y) <==> od[i]=y'
|
||||||
|
# Setting a new item creates a new link at the end of the linked list,
|
||||||
|
# and the inherited dictionary is updated with the new key/value pair.
|
||||||
|
if key not in self:
|
||||||
|
root = self.__root
|
||||||
|
last = root[0]
|
||||||
|
last[1] = root[0] = self.__map[key] = [last, root, key]
|
||||||
|
return dict_setitem(self, key, value)
|
||||||
|
|
||||||
|
def __delitem__(self, key, dict_delitem=dict.__delitem__):
|
||||||
|
'od.__delitem__(y) <==> del od[y]'
|
||||||
|
# Deleting an existing item uses self.__map to find the link which gets
|
||||||
|
# removed by updating the links in the predecessor and successor nodes.
|
||||||
|
dict_delitem(self, key)
|
||||||
|
link_prev, link_next, _ = self.__map.pop(key)
|
||||||
|
link_prev[1] = link_next # update link_prev[NEXT]
|
||||||
|
link_next[0] = link_prev # update link_next[PREV]
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
'od.__iter__() <==> iter(od)'
|
||||||
|
# Traverse the linked list in order.
|
||||||
|
root = self.__root
|
||||||
|
curr = root[1] # start at the first node
|
||||||
|
while curr is not root:
|
||||||
|
yield curr[2] # yield the curr[KEY]
|
||||||
|
curr = curr[1] # move to next node
|
||||||
|
|
||||||
|
def __reversed__(self):
|
||||||
|
'od.__reversed__() <==> reversed(od)'
|
||||||
|
# Traverse the linked list in reverse order.
|
||||||
|
root = self.__root
|
||||||
|
curr = root[0] # start at the last node
|
||||||
|
while curr is not root:
|
||||||
|
yield curr[2] # yield the curr[KEY]
|
||||||
|
curr = curr[0] # move to previous node
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
'od.clear() -> None. Remove all items from od.'
|
||||||
|
root = self.__root
|
||||||
|
root[:] = [root, root, None]
|
||||||
|
self.__map.clear()
|
||||||
|
dict.clear(self)
|
||||||
|
|
||||||
|
# -- the following methods do not depend on the internal structure --
|
||||||
|
|
||||||
|
def keys(self):
|
||||||
|
'od.keys() -> list of keys in od'
|
||||||
|
return list(self)
|
||||||
|
|
||||||
|
def values(self):
|
||||||
|
'od.values() -> list of values in od'
|
||||||
|
return [self[key] for key in self]
|
||||||
|
|
||||||
|
def items(self):
|
||||||
|
'od.items() -> list of (key, value) pairs in od'
|
||||||
|
return [(key, self[key]) for key in self]
|
||||||
|
|
||||||
|
def iterkeys(self):
|
||||||
|
'od.iterkeys() -> an iterator over the keys in od'
|
||||||
|
return iter(self)
|
||||||
|
|
||||||
|
def itervalues(self):
|
||||||
|
'od.itervalues -> an iterator over the values in od'
|
||||||
|
for k in self:
|
||||||
|
yield self[k]
|
||||||
|
|
||||||
|
def iteritems(self):
|
||||||
|
'od.iteritems -> an iterator over the (key, value) pairs in od'
|
||||||
|
for k in self:
|
||||||
|
yield (k, self[k])
|
||||||
|
|
||||||
|
update = collections.MutableMapping.update
|
||||||
|
|
||||||
|
__update = update # let subclasses override update without breaking __init__
|
||||||
|
|
||||||
|
__marker = object()
|
||||||
|
|
||||||
|
def pop(self, key, default=__marker):
|
||||||
|
'''od.pop(k[,d]) -> v, remove specified key and return the corresponding
|
||||||
|
value. If key is not found, d is returned if given, otherwise KeyError
|
||||||
|
is raised.
|
||||||
|
|
||||||
|
'''
|
||||||
|
if key in self:
|
||||||
|
result = self[key]
|
||||||
|
del self[key]
|
||||||
|
return result
|
||||||
|
if default is self.__marker:
|
||||||
|
raise KeyError(key)
|
||||||
|
return default
|
||||||
|
|
||||||
|
def setdefault(self, key, default=None):
|
||||||
|
'od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od'
|
||||||
|
if key in self:
|
||||||
|
return self[key]
|
||||||
|
self[key] = default
|
||||||
|
return default
|
||||||
|
|
||||||
|
def popitem(self, last=True):
|
||||||
|
'''od.popitem() -> (k, v), return and remove a (key, value) pair.
|
||||||
|
Pairs are returned in LIFO order if last is true or FIFO order if false.
|
||||||
|
|
||||||
|
'''
|
||||||
|
if not self:
|
||||||
|
raise KeyError('dictionary is empty')
|
||||||
|
key = next(reversed(self) if last else iter(self))
|
||||||
|
value = self.pop(key)
|
||||||
|
return key, value
|
||||||
|
|
||||||
|
def __repr__(self, _repr_running={}):
|
||||||
|
'od.__repr__() <==> repr(od)'
|
||||||
|
call_key = id(self), _get_ident()
|
||||||
|
if call_key in _repr_running:
|
||||||
|
return '...'
|
||||||
|
_repr_running[call_key] = 1
|
||||||
|
try:
|
||||||
|
if not self:
|
||||||
|
return '%s()' % (self.__class__.__name__,)
|
||||||
|
return '%s(%r)' % (self.__class__.__name__, self.items())
|
||||||
|
finally:
|
||||||
|
del _repr_running[call_key]
|
||||||
|
|
||||||
|
def __reduce__(self):
|
||||||
|
'Return state information for pickling'
|
||||||
|
items = [[k, self[k]] for k in self]
|
||||||
|
inst_dict = vars(self).copy()
|
||||||
|
for k in vars(OrderedDict()):
|
||||||
|
inst_dict.pop(k, None)
|
||||||
|
if inst_dict:
|
||||||
|
return (self.__class__, (items,), inst_dict)
|
||||||
|
return self.__class__, (items,)
|
||||||
|
|
||||||
|
def copy(self):
|
||||||
|
'od.copy() -> a shallow copy of od'
|
||||||
|
return self.__class__(self)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def fromkeys(cls, iterable, value=None):
|
||||||
|
'''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.
|
||||||
|
If not specified, the value defaults to None.
|
||||||
|
|
||||||
|
'''
|
||||||
|
self = cls()
|
||||||
|
for key in iterable:
|
||||||
|
self[key] = value
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
'''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive
|
||||||
|
while comparison to a regular mapping is order-insensitive.
|
||||||
|
|
||||||
|
'''
|
||||||
|
if isinstance(other, OrderedDict):
|
||||||
|
return dict.__eq__(self, other) and all(_imap(_eq, self, other))
|
||||||
|
return dict.__eq__(self, other)
|
||||||
|
|
||||||
|
def __ne__(self, other):
|
||||||
|
'od.__ne__(y) <==> od!=y'
|
||||||
|
return not self == other
|
||||||
|
|
||||||
|
# -- the following methods support python 3.x style dictionary views --
|
||||||
|
|
||||||
|
def viewkeys(self):
|
||||||
|
"od.viewkeys() -> a set-like object providing a view on od's keys"
|
||||||
|
return KeysView(self)
|
||||||
|
|
||||||
|
def viewvalues(self):
|
||||||
|
"od.viewvalues() -> an object providing a view on od's values"
|
||||||
|
return ValuesView(self)
|
||||||
|
|
||||||
|
def viewitems(self):
|
||||||
|
"od.viewitems() -> a set-like object providing a view on od's items"
|
||||||
|
return ItemsView(self)
|
||||||
|
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
cd `dirname $0`
|
||||||
|
eval $(ps -ef | grep "[0-9] python server\\.py a" | awk '{print "kill "$2}')
|
||||||
|
ulimit -n 4096
|
||||||
|
nohup python server.py a >> /dev/null 2>&1 &
|
||||||
|
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
eval $(ps -ef | grep "[0-9] python server\\.py a" | awk '{print "kill "$2}')
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
tail -f ssserver.log
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,20 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# Copyright 2016 breakwa11
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
def version():
|
||||||
|
return '2.9.7 2016-11-22'
|
||||||
|
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
eval $(ps -ef | grep "[0-9] python server\\.py m" | awk '{print "kill "$2}')
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
def getKeys(key_list):
|
||||||
|
return key_list
|
||||||
|
#return key_list + ['plan'] # append the column name 'plan'
|
||||||
|
|
||||||
|
def isTurnOn(row):
|
||||||
|
return True
|
||||||
|
#return row['plan'] == 'B' # then judge here
|
||||||
|
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
import socket
|
||||||
|
import socks
|
||||||
|
|
||||||
|
|
||||||
|
SERVER_IP = '127.0.0.1'
|
||||||
|
SERVER_PORT = 1081
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
# Test 1: same source port IPv4
|
||||||
|
sock_out = socks.socksocket(socket.AF_INET, socket.SOCK_DGRAM,
|
||||||
|
socket.SOL_UDP)
|
||||||
|
sock_out.set_proxy(socks.SOCKS5, SERVER_IP, SERVER_PORT)
|
||||||
|
sock_out.bind(('127.0.0.1', 9000))
|
||||||
|
|
||||||
|
sock_in1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
|
||||||
|
socket.SOL_UDP)
|
||||||
|
sock_in2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
|
||||||
|
socket.SOL_UDP)
|
||||||
|
|
||||||
|
sock_in1.bind(('127.0.0.1', 9001))
|
||||||
|
sock_in2.bind(('127.0.0.1', 9002))
|
||||||
|
|
||||||
|
sock_out.sendto(b'data', ('127.0.0.1', 9001))
|
||||||
|
result1 = sock_in1.recvfrom(8)
|
||||||
|
|
||||||
|
sock_out.sendto(b'data', ('127.0.0.1', 9002))
|
||||||
|
result2 = sock_in2.recvfrom(8)
|
||||||
|
|
||||||
|
sock_out.close()
|
||||||
|
sock_in1.close()
|
||||||
|
sock_in2.close()
|
||||||
|
|
||||||
|
# make sure they're from the same source port
|
||||||
|
assert result1 == result2
|
||||||
|
|
||||||
|
# Test 2: same source port IPv6
|
||||||
|
# try again from the same port but IPv6
|
||||||
|
sock_out = socks.socksocket(socket.AF_INET, socket.SOCK_DGRAM,
|
||||||
|
socket.SOL_UDP)
|
||||||
|
sock_out.set_proxy(socks.SOCKS5, SERVER_IP, SERVER_PORT)
|
||||||
|
sock_out.bind(('127.0.0.1', 9000))
|
||||||
|
|
||||||
|
sock_in1 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM,
|
||||||
|
socket.SOL_UDP)
|
||||||
|
sock_in2 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM,
|
||||||
|
socket.SOL_UDP)
|
||||||
|
|
||||||
|
sock_in1.bind(('::1', 9001))
|
||||||
|
sock_in2.bind(('::1', 9002))
|
||||||
|
|
||||||
|
sock_out.sendto(b'data', ('::1', 9001))
|
||||||
|
result1 = sock_in1.recvfrom(8)
|
||||||
|
|
||||||
|
sock_out.sendto(b'data', ('::1', 9002))
|
||||||
|
result2 = sock_in2.recvfrom(8)
|
||||||
|
|
||||||
|
sock_out.close()
|
||||||
|
sock_in1.close()
|
||||||
|
sock_in2.close()
|
||||||
|
|
||||||
|
# make sure they're from the same source port
|
||||||
|
assert result1 == result2
|
||||||
|
|
||||||
|
# Test 3: different source ports IPv6
|
||||||
|
sock_out = socks.socksocket(socket.AF_INET, socket.SOCK_DGRAM,
|
||||||
|
socket.SOL_UDP)
|
||||||
|
sock_out.set_proxy(socks.SOCKS5, SERVER_IP, SERVER_PORT)
|
||||||
|
sock_out.bind(('127.0.0.1', 9003))
|
||||||
|
|
||||||
|
sock_in1 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM,
|
||||||
|
socket.SOL_UDP)
|
||||||
|
sock_in1.bind(('::1', 9001))
|
||||||
|
sock_out.sendto(b'data', ('::1', 9001))
|
||||||
|
result3 = sock_in1.recvfrom(8)
|
||||||
|
|
||||||
|
# make sure they're from different source ports
|
||||||
|
assert result1 != result3
|
||||||
|
|
||||||
|
sock_out.close()
|
||||||
|
sock_in1.close()
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
PYTHON="coverage run -p"
|
||||||
|
|
||||||
|
mkdir -p tmp
|
||||||
|
|
||||||
|
$PYTHON shadowsocks/local.py -c tests/aes.json -v &
|
||||||
|
LOCAL=$!
|
||||||
|
|
||||||
|
$PYTHON shadowsocks/server.py -c tests/aes.json --forbidden-ip "" -v &
|
||||||
|
SERVER=$!
|
||||||
|
|
||||||
|
sleep 3
|
||||||
|
|
||||||
|
python tests/test_udp_src.py
|
||||||
|
r=$?
|
||||||
|
|
||||||
|
kill -s SIGINT $LOCAL
|
||||||
|
kill -s SIGINT $SERVER
|
||||||
|
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
exit $r
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
[Definition]
|
||||||
|
|
||||||
|
_daemon = shadowsocks
|
||||||
|
|
||||||
|
failregex = ^\s+ERROR\s+can not parse header when handling connection from <HOST>:\d+$
|
||||||
Loading…
Reference in New Issue