Skip to content

Commit

Permalink
Print startup error message to stderr.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Jan 24, 2013
1 parent 88cbccd commit eb2046f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
19 changes: 11 additions & 8 deletions cmd/shadowsocks-local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
"errors"
"flag"
"fmt"
ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
"io"
"log"
Expand Down Expand Up @@ -322,28 +323,30 @@ func main() {
config, err := ss.ParseConfig(configFile)
if err != nil {
config = &cmdConfig
if os.IsNotExist(err) {
log.Println("config file not found, using all options from command line")
} else {
log.Fatal("error reading config file: %v\n", err)
if !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "error reading %s: %v\n", configFile, err)
os.Exit(1)
}
} else {
ss.UpdateConfig(config, &cmdConfig)
}
if err = ss.SetDefaultCipher(config.Method); err != nil {
log.Fatal(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

if len(config.ServerPassword) == 0 {
if !enoughOptions(config) {
log.Fatal("must specify server address, password and both server/local port")
fmt.Fprintln(os.Stderr, "must specify server address, password and both server/local port")
os.Exit(1)
}
} else {
if config.Password != "" || config.ServerPort != 0 || config.GetServerArray() != nil {
log.Println("given server_password, ignore server, server_port and password option:", config)
fmt.Fprintln(os.Stderr, "given server_password, ignore server, server_port and password option:", config)
}
if config.LocalPort == 0 {
log.Fatal("must specify local port")
fmt.Fprintln(os.Stderr, "must specify local port")
os.Exit(1)
}
}

Expand Down
14 changes: 7 additions & 7 deletions cmd/shadowsocks-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
"errors"
"flag"
"fmt"
ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
"io"
"log"
Expand Down Expand Up @@ -279,14 +280,14 @@ func enoughOptions(config *ss.Config) bool {
func unifyPortPassword(config *ss.Config) (err error) {
if len(config.PortPassword) == 0 { // this handles both nil PortPassword and empty one
if !enoughOptions(config) {
log.Println("must specify both port and password")
fmt.Fprintln(os.Stderr, "must specify both port and password")
return errors.New("not enough options")
}
port := strconv.Itoa(config.ServerPort)
config.PortPassword = map[string]string{port: config.Password}
} else {
if config.Password != "" || config.ServerPort != 0 {
log.Println("given port_password, ignore server_port and password option")
fmt.Fprintln(os.Stderr, "given port_password, ignore server_port and password option")
}
}
return
Expand Down Expand Up @@ -323,10 +324,8 @@ func main() {
var err error
config, err = ss.ParseConfig(configFile)
if err != nil {
if os.IsNotExist(err) {
log.Println("config file not found, using all options from command line")
} else {
log.Printf("error reading %s: %v\n", configFile, err)
if !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "error reading %s: %v\n", configFile, err)
os.Exit(1)
}
config = &cmdConfig
Expand All @@ -337,7 +336,8 @@ func main() {
os.Exit(1)
}
if err = ss.SetDefaultCipher(config.Method); err != nil {
log.Fatal(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if core > 0 {
runtime.GOMAXPROCS(core)
Expand Down
2 changes: 1 addition & 1 deletion sample-config/shadowsocks
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ do_start() {
echo $PID > $PID_FILE
sleep 0.3
if ! check_running; then
echo "start failed, log file $LOG_FILE may provide some help"
echo "start failed"
return 1
fi
echo "shadowsocks running with PID $PID"
Expand Down

0 comments on commit eb2046f

Please sign in to comment.