|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Use [ -n "$TRAVIS" ] to test for running on Travis-CI. |
| 4 | + |
| 5 | +# Run in the scripts directory. |
| 6 | +cd "$( dirname "${BASH_SOURCE[0]}" )" |
| 7 | + |
| 8 | +PLUGIN="v2ray-plugin" |
| 9 | +SERVER_PORT="4433" |
| 10 | +LOCAL_PORT="1080" |
| 11 | +SOCKS="127.0.0.1:1080" |
| 12 | +HTTP_PORT="8080" |
| 13 | + |
| 14 | +if [[ -z "${GOPATH}" ]]; then |
| 15 | + GOPATH=~/go |
| 16 | +fi |
| 17 | + |
| 18 | +wait_server() { |
| 19 | + local port |
| 20 | + port=$1 |
| 21 | + for i in {1..20}; do |
| 22 | + # sleep first because this maybe called immediately after server start |
| 23 | + sleep 0.1 |
| 24 | + nc -z -w 4 127.0.0.1 $port && break |
| 25 | + done |
| 26 | +} |
| 27 | + |
| 28 | +start_http_server() { |
| 29 | + go build http.go |
| 30 | + ./http $HTTP_PORT & |
| 31 | + wait_server $HTTP_PORT |
| 32 | + http_pid=$! |
| 33 | +} |
| 34 | + |
| 35 | +stop_http_server() { |
| 36 | + kill -SIGTERM $http_pid |
| 37 | +} |
| 38 | + |
| 39 | +test_get() { |
| 40 | + local url |
| 41 | + local target |
| 42 | + local code |
| 43 | + url=$1 |
| 44 | + target=$2 |
| 45 | + code=$3 |
| 46 | + |
| 47 | + if [[ -z $code ]]; then |
| 48 | + code="200" |
| 49 | + fi |
| 50 | + |
| 51 | + # -s silent to disable progress meter, but enable --show-error |
| 52 | + # -i to include http header |
| 53 | + # -L to follow redirect so we should always get HTTP 200 |
| 54 | + cont=`curl -m 5 --socks5 $SOCKS -s --show-error -i -L $url 2>&1` |
| 55 | + ok=`echo $cont | grep -E -o "HTTP/1\.1 +$code"` |
| 56 | + html=`echo $cont | grep -E -o -i "$target"` |
| 57 | + if [[ -z $ok || -z $html ]] ; then |
| 58 | + echo "GET $url FAILED!!!" |
| 59 | + echo "$ok" |
| 60 | + echo "$html" |
| 61 | + echo $cont |
| 62 | + return 1 |
| 63 | + fi |
| 64 | + return 0 |
| 65 | +} |
| 66 | + |
| 67 | +test_shadowsocks() { |
| 68 | + local url |
| 69 | + local method |
| 70 | + local server_pid |
| 71 | + local local_pid |
| 72 | + local client_option |
| 73 | + local server_option |
| 74 | + local msg |
| 75 | + local expectation |
| 76 | + url=$1 |
| 77 | + method=$2 |
| 78 | + msg="Testing HTTP/GET $url $method w/o plugin" |
| 79 | + expectation=$3 |
| 80 | + |
| 81 | + if [ $# -eq 4 ]; then |
| 82 | + server_option="-plugin $PLUGIN -plugin-opts server;" |
| 83 | + client_option="-plugin $PLUGIN" |
| 84 | + msg="Testing HTTP/GET $url $method with plugin" |
| 85 | + fi |
| 86 | + echo "============================================================" |
| 87 | + echo $msg |
| 88 | + |
| 89 | + go-shadowsocks2 -s "ss://$method:your-password@:$SERVER_PORT" -verbose $server_option & |
| 90 | + server_pid=$! |
| 91 | + wait_server $SERVER_PORT |
| 92 | + |
| 93 | + go-shadowsocks2 -c "ss://$method:[email protected]:$SERVER_PORT" \ |
| 94 | + -verbose -socks :$LOCAL_PORT -u $client_option & |
| 95 | + local_pid=$! |
| 96 | + wait_server $LOCAL_PORT |
| 97 | + |
| 98 | + for i in {1..3}; do |
| 99 | + test_get $url "go-shadowsocks2" |
| 100 | + if ! [ $? -eq $expectation ]; then |
| 101 | + echo -e "STATUS:\033[31m FAIL \033[0m" |
| 102 | + kill -SIGTERM $server_pid |
| 103 | + kill -SIGTERM $local_pid |
| 104 | + stop_http_server |
| 105 | + exit 1 |
| 106 | + fi |
| 107 | + done |
| 108 | + echo -e "STATUS:\033[32m PASS \033[0m" |
| 109 | + kill -SIGTERM $server_pid |
| 110 | + kill -SIGTERM $local_pid |
| 111 | + sleep 0.1 |
| 112 | +} |
| 113 | + |
| 114 | +test_methods() { |
| 115 | + local url |
| 116 | + url=http://127.0.0.1:$HTTP_PORT/README.md |
| 117 | + |
| 118 | + test_shadowsocks $url AEAD_AES_128_GCM 0 |
| 119 | + test_shadowsocks $url AEAD_AES_192_GCM 0 |
| 120 | + test_shadowsocks $url AEAD_AES_256_GCM 0 |
| 121 | + test_shadowsocks $url AEAD_CHACHA20_POLY1305 0 |
| 122 | + test_shadowsocks $url AES-128-CFB 0 |
| 123 | + test_shadowsocks $url AES-128-CTR 0 |
| 124 | + test_shadowsocks $url AES-192-CFB 0 |
| 125 | + test_shadowsocks $url AES-192-CTR 0 |
| 126 | + test_shadowsocks $url AES-256-CFB 0 |
| 127 | + test_shadowsocks $url AES-256-CTR 0 |
| 128 | + test_shadowsocks $url CHACHA20-IETF 0 |
| 129 | + test_shadowsocks $url XCHACHA20 0 |
| 130 | +} |
| 131 | + |
| 132 | +test_plugin() { |
| 133 | + local url |
| 134 | + url=http://127.0.0.1:$HTTP_PORT/README.md |
| 135 | + |
| 136 | + test_shadowsocks $url AEAD_AES_128_GCM 0 plugin |
| 137 | + mv $GOPATH/bin/$PLUGIN ./ |
| 138 | + test_shadowsocks $url AES-128-CFB 0 plugin |
| 139 | + mv ./$PLUGIN $GOPATH/bin/ |
| 140 | + |
| 141 | + # chmod -x ./$PLUGIN |
| 142 | + # test_shadowsocks $url AEAD_AES_128_GCM 1 plugin |
| 143 | + # mv ./$PLUGIN $GOPATH/bin/ |
| 144 | + # test_shadowsocks $url AES-128-CFB 1 plugin |
| 145 | + # chmod +x $GOPATH/bin/$PLUGIN |
| 146 | +} |
| 147 | + |
| 148 | +start_http_server |
| 149 | + |
| 150 | +test_methods |
| 151 | + |
| 152 | +test_plugin |
| 153 | + |
| 154 | +stop_http_server |
0 commit comments