Skip to content

Commit

Permalink
Add server_password option to specify multiple servers.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Dec 24, 2012
1 parent d9ec69c commit 815f893
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
20 changes: 16 additions & 4 deletions shadowsocks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,32 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"reflect"
"time"
)

type Config struct {
Server interface{} `json:"server"`
ServerPort int `json:"server_port"`
LocalPort int `json:"local_port"`
Password string `json:"password"`
Server interface{} `json:"server"`
ServerPort int `json:"server_port"`
LocalPort int `json:"local_port"`
Password string `json:"password"`

// following options are only used by server
PortPassword map[string]string `json:"port_password"`
Timeout int `json:"timeout"`
CacheEncTable bool `json:"cache_enctable"`

// following options are only used by client
ServerPassword map[string]string `json:"server_password"`
}

var readTimeout time.Duration

func (config *Config) GetServerArray() []string {
// Specifying multiple servers in the "server" options is deprecated.
// But for backward compatiblity, keep this.
if config.Server == nil {
return nil
}
Expand All @@ -38,6 +46,10 @@ func (config *Config) GetServerArray() []string {
}
arr, ok := config.Server.([]interface{})
if ok {
if len(arr) > 1 {
log.Println("Multiple servers in \"server\" option is deprecated. " +
"Please use \"server_password\" instead.")
}
serverArr := make([]string, len(arr), len(arr))
for i, s := range arr {
serverArr[i], ok = s.(string)
Expand Down
12 changes: 12 additions & 0 deletions shadowsocks/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ func TestDeprecatedClientMultiServerArray(t *testing.T) {
}
}

func TestClientMultiServerArray(t *testing.T) {
config, err := ParseConfig("testdata/client-multi-server.json")
if err != nil {
t.Error("error parsing client-multi-server.json:", err)
}

if config.ServerPassword["127.0.0.1:8388"] != "foobar" ||
config.ServerPassword["127.0.1.1:8389"] != "barfoo" {
t.Error("server_password parse error")
}
}

func TestParseConfigEmpty(t *testing.T) {
// make sure we will not crash
config, err := ParseConfig("testdata/noserver.json")
Expand Down
7 changes: 7 additions & 0 deletions shadowsocks/testdata/client-multi-server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"local_port":1081,
"server_password": {
"127.0.0.1:8388": "foobar",
"127.0.1.1:8389": "barfoo"
}
}

0 comments on commit 815f893

Please sign in to comment.