Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Mar 25, 2013
2 parents 6e34d27 + 76d5be3 commit 1254da5
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 33 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
0.6.1 (2013-03-15)
0.6.3 (not released)
* Support IPv6 server

0.6.2 (2013-03-15)
* Connect to multiple servers in the order specified in config
* More information in server error log to help error diagnosing

Expand Down
18 changes: 13 additions & 5 deletions cmd/shadowsocks-local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func getRequest(conn net.Conn) (rawaddr []byte, host string, err error) {
host = addrIp.String()
}
port := binary.BigEndian.Uint16(buf[reqLen-2 : reqLen])
host += ":" + strconv.Itoa(int(port))
host = net.JoinHostPort(host, strconv.Itoa(int(port)))
}

return
Expand All @@ -156,6 +156,14 @@ var servers struct {
}

func parseServerConfig(config *ss.Config) {
hasPort := func(s string) bool {
_, port, err := net.SplitHostPort(s)
if err != nil {
return false
}
return port != ""
}

if len(config.ServerPassword) == 0 {
// only one encryption table
cipher, err := ss.NewCipher(config.Method, config.Password)
Expand All @@ -168,11 +176,11 @@ func parseServerConfig(config *ss.Config) {
servers.srvCipher = make([]*ServerCipher, n)

for i, s := range srvArr {
if ss.HasPort(s) {
if hasPort(s) {
log.Println("ignore server_port option for server", s)
servers.srvCipher[i] = &ServerCipher{s, cipher}
} else {
servers.srvCipher[i] = &ServerCipher{s + ":" + srvPort, cipher}
servers.srvCipher[i] = &ServerCipher{net.JoinHostPort(s, srvPort), cipher}
}
}
} else {
Expand All @@ -192,8 +200,8 @@ func parseServerConfig(config *ss.Config) {
if len(serverInfo) == 3 {
encmethod = serverInfo[2]
}
if !ss.HasPort(server) {
log.Fatalf("no port for server %s, please specify port in the form of %s:port\n", server, server)
if !hasPort(server) {
log.Fatalf("no port for server %s\n", server)
}
cipher, ok := cipherCache[passwd]
if !ok {
Expand Down
55 changes: 42 additions & 13 deletions script/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,38 @@ LOCAL_PORT="1090"
SOCKS="127.0.0.1:$LOCAL_PORT"

test_get() {
local url
url=$1
target=$2
code=$3

if [[ -z $code ]]; then
code="200"
fi

# get 5 times
for i in {1..2}; do
# -s silent to disable progress meter, but enable --show-error
# -i to include http header
# -L to follow redirect so we should always get HTTP 200
cont=`curl --socks5 $SOCKS -s --show-error -i -L $url 2>&1`
ok=`echo $cont | grep -E -o "HTTP/1\.1 +$code"`
html=`echo $cont | grep -E -o -i "$target"`
if [[ -z $ok || -z $html ]] ; then
echo "=============================="
echo "GET $url FAILED!!!"
echo "$ok"
echo "$html"
echo $cont
echo "=============================="
return 1
fi
sleep 0.3
done
return 0
}

test_shadowsocks() {
local url
local method
local server_pid
Expand All @@ -18,27 +50,24 @@ test_get() {
local_pid=$!

# wait server and client finish startup
sleep 0.5
sleep 1

# get 5 times
for i in {1..5}; do
if ! curl -s --socks5 $SOCKS $url >/dev/null 2>&1; then
echo "=============================="
echo "GET $url $method FAILED!!!"
echo "=============================="
kill -SIGINT $server_pid
kill -SIGINT $local_pid
exit 1
if ! test_get $url "<html"; then
kill -SIGTERM $server_pid
kill -SIGTERM $local_pid
return 1
fi
done
echo "=============================="
echo "GET $url $method passed"
echo "=============================="
kill -SIGINT $server_pid
kill -SIGINT $local_pid
sleep 0.5
kill -SIGTERM $server_pid
kill -SIGTERM $local_pid
sleep 1
}

test_get baidu.com
test_get baidu.com rc4
test_shadowsocks baidu.com
test_shadowsocks baidu.com rc4

8 changes: 3 additions & 5 deletions shadowsocks/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"net"
"strconv"
"strings"
)

type Conn struct {
Expand All @@ -19,12 +18,11 @@ func NewConn(cn net.Conn, cipher Cipher) *Conn {
}

func RawAddr(addr string) (buf []byte, err error) {
arr := strings.Split(addr, ":")
if len(arr) != 2 {
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
return nil, errors.New(
fmt.Sprintf("shadowsocks: malformed address %s", addr))
fmt.Sprintf("shadowsocks: address error %s %v", addr, err))
}
host, portStr := arr[0], arr[1]
port, err := strconv.Atoi(portStr)
if err != nil {
return nil, errors.New(
Expand Down
2 changes: 2 additions & 0 deletions shadowsocks/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ func TestRC4Cipher(t *testing.T) {
t.Error("Should get error for empty key creating rc4 cipher")
}
cipher, err = NewCipher("rc4", "Alibaba")
ciphercopy := cipher.Copy()
if err != nil {
t.Error("Should not error creating rc4 cipher with key Alibaba")
}
if _, ok := cipher.(*RC4Cipher); !ok {
t.Error("Should get rc4 cipher")
} else {
testCiphter(t, cipher, "RC4Cipher")
testCiphter(t, ciphercopy, "RC4Cipher copy")
}
}
9 changes: 0 additions & 9 deletions shadowsocks/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,3 @@ func IsFileExists(path string) (bool, error) {
}
return false, err
}

func HasPort(s string) bool {
for i := len(s) - 1; i > 0; i-- {
if s[i] == ':' {
return true
}
}
return false
}

0 comments on commit 1254da5

Please sign in to comment.