Skip to content

Commit

Permalink
Allow no config file and specify all options on cmd line.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Dec 16, 2012
1 parent e395333 commit 8b5aa2c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
21 changes: 18 additions & 3 deletions cmd/shadowsocks-local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"log"
"net"
"os"
"strconv"
)

Expand Down Expand Up @@ -178,7 +179,7 @@ func run(port, password, server string) {
log.Fatal(err)
}
encTbl := ss.GetTable(password)
log.Printf("starting local socks5 server at port %v, remote shadowsocks server %s...\n", port, server)
log.Printf("starting local socks5 server at port %v, remote shadowsocks server %s ...\n", port, server)
for {
conn, err := ln.Accept()
if err != nil {
Expand All @@ -189,6 +190,11 @@ func run(port, password, server string) {
}
}

func enoughOptions(config *ss.Config) bool {
return config.Server != "" && config.ServerPort != 0 &&
config.LocalPort != 0 && config.Password != ""
}

func main() {
var configFile string
var cmdConfig ss.Config
Expand All @@ -204,9 +210,18 @@ func main() {

config, err := ss.ParseConfig(configFile)
if err != nil {
return
enough := enoughOptions(&cmdConfig)
if !(enough && os.IsNotExist(err)) {
log.Printf("error reading %s: %v\n", configFile, err)
}
if !enough {
return
}
log.Println("using all options from command line")
config = &cmdConfig
} else {
ss.UpdateConfig(config, &cmdConfig)
}
ss.UpdateConfig(config, &cmdConfig)
ss.SetDebug(debug)

run(strconv.Itoa(config.LocalPort), config.Password,
Expand Down
18 changes: 16 additions & 2 deletions cmd/shadowsocks-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"log"
"net"
"os"
"strconv"
"sync/atomic"
"time"
Expand Down Expand Up @@ -150,6 +151,10 @@ func run(port, password string) {
}
}

func enoughOptions(config *ss.Config) bool {
return config.ServerPort != 0 && config.Password != ""
}

func main() {
var configFile string
var cmdConfig ss.Config
Expand All @@ -164,9 +169,18 @@ func main() {

config, err := ss.ParseConfig(configFile)
if err != nil {
return
enough := enoughOptions(&cmdConfig)
if !(enough && os.IsNotExist(err)) {
log.Printf("error reading %s: %v\n", configFile, err)
}
if !enough {
return
}
log.Println("using all options from command line")
config = &cmdConfig
} else {
ss.UpdateConfig(config, &cmdConfig)
}
ss.UpdateConfig(config, &cmdConfig)
ss.SetDebug(debug)

if len(config.PortPassword) == 0 {
Expand Down
4 changes: 0 additions & 4 deletions shadowsocks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package shadowsocks
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"reflect"
"time"
Expand All @@ -30,20 +29,17 @@ var readTimeout time.Duration
func ParseConfig(path string) (config *Config, err error) {
file, err := os.Open(path) // For read access.
if err != nil {
log.Println("error opening config file:", err)
return
}
defer file.Close()

data, err := ioutil.ReadAll(file)
if err != nil {
log.Println("error reading config:", err)
return
}

config = &Config{}
if err = json.Unmarshal(data, config); err != nil {
log.Println("can not parse config:", err)
return nil, err
}
readTimeout = time.Duration(config.Timeout) * time.Second
Expand Down

0 comments on commit 8b5aa2c

Please sign in to comment.