Skip to content

Commit

Permalink
ParseConfig return err instead of exit.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Dec 16, 2012
1 parent b3a5269 commit 30a888a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
5 changes: 4 additions & 1 deletion cmd/shadowsocks-local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ func main() {
flag.StringVar(&configFile, "c", "config.json", "specify config file")
flag.Parse()

config := ss.ParseConfig(configFile)
config, err := ss.ParseConfig(configFile)
if err != nil {
return
}
debug = ss.Debug
run(strconv.Itoa(config.LocalPort), config.Password,
config.Server+":"+strconv.Itoa(config.ServerPort))
Expand Down
5 changes: 4 additions & 1 deletion cmd/shadowsocks-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ func main() {
flag.StringVar(&configFile, "c", "config.json", "specify config file")
flag.Parse()

config := ss.ParseConfig(configFile)
config, err := ss.ParseConfig(configFile)
if err != nil {
return
}
debug = ss.Debug
if len(config.PortPassword) == 0 {
run(strconv.Itoa(config.ServerPort), config.Password)
Expand Down
19 changes: 12 additions & 7 deletions shadowsocks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,26 @@ type Config struct {

var readTimeout time.Duration

func ParseConfig(path string) *Config {
func ParseConfig(path string) (config *Config, err error) {
file, err := os.Open(path) // For read access.
if err != nil {
log.Fatal("error opening config file:", err)
log.Println("error opening config file:", err)
return
}
defer file.Close()

data, err := ioutil.ReadAll(file)
if err != nil {
log.Fatalln("error reading config:", err)
log.Println("error reading config:", err)
return
}
var config Config
if err = json.Unmarshal(data, &config); err != nil {
log.Fatalln("can not parse config:", err)

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

0 comments on commit 30a888a

Please sign in to comment.