diff --git a/cli/ss-server/config.json b/cli/ss-server/config.json index 96fb883..63169a5 100644 --- a/cli/ss-server/config.json +++ b/cli/ss-server/config.json @@ -2,5 +2,6 @@ "server": "::", "server_port": 8080, "method": "2022-blake3-aes-128-gcm", - "key": "psk" + "key": "psk", + "log_level": "warn" } \ No newline at end of file diff --git a/cli/ss-server/main.go b/cli/ss-server/main.go index eff9339..c092704 100644 --- a/cli/ss-server/main.go +++ b/cli/ss-server/main.go @@ -9,7 +9,6 @@ import ( "net/netip" "os" "os/signal" - "strings" "syscall" "github.com/sagernet/sing" @@ -41,54 +40,55 @@ type flags struct { Password string `json:"password"` Key string `json:"key"` Method string `json:"method"` - Verbose bool `json:"verbose"` - ConfigFile string + LogLevel string `json:"log_level"` } +var configPath string + func main() { - f := new(flags) - command := &cobra.Command{ - Use: "ss-local", - Short: "shadowsocks client", + Use: "ss-server [-c config.json]", + Short: "shadowsocks server", Version: sing.VersionStr, - Run: func(cmd *cobra.Command, args []string) { - run(cmd, f) - }, + Run: run, } - - command.Flags().StringVarP(&f.Server, "server", "s", "", "Set the server’s hostname or IP.") - command.Flags().Uint16VarP(&f.ServerPort, "server-port", "p", 0, "Set the server’s port number.") - command.Flags().StringVarP(&f.Bind, "local-address", "b", "", "Set the local address.") - command.Flags().Uint16VarP(&f.LocalPort, "local-port", "l", 0, "Set the local port number.") - command.Flags().StringVar(&f.Key, "key", "", "Set the key directly. The key should be encoded with URL-safe Base64.") - command.Flags().StringVarP(&f.Password, "password", "k", "", "Set the password. The server and the client should use the same password.") - - var supportedCiphers []string - supportedCiphers = append(supportedCiphers, shadowsocks.MethodNone) - supportedCiphers = append(supportedCiphers, shadowaead.List...) - supportedCiphers = append(supportedCiphers, shadowaead_2022.List...) - - command.Flags().StringVarP(&f.Method, "encrypt-method", "m", "", "Set the cipher.\n\nSupported ciphers:\n\n"+strings.Join(supportedCiphers, "\n")) - command.Flags().StringVarP(&f.ConfigFile, "config", "c", "", "Use a configuration file.") - command.Flags().BoolVarP(&f.Verbose, "verbose", "v", false, "Enable verbose mode.") - + command.Flags().StringVarP(&configPath, "config", "c", "", "set a configuration file") err := command.Execute() if err != nil { logrus.Fatal(err) } } -func run(cmd *cobra.Command, f *flags) { +func run(cmd *cobra.Command, args []string) { + if configPath == "" { + configPath = "config.json" + } + + configFile, err := ioutil.ReadFile(configPath) + if err != nil { + logrus.Fatal(E.Cause(err, "read config file")) + } + + f := new(flags) + err = json.Unmarshal(configFile, f) + if err != nil { + logrus.Fatal(E.Cause(err, "parse config file")) + } + + if f.LogLevel != "" { + level, err := logrus.ParseLevel(f.LogLevel) + if err != nil { + logrus.Fatal("unknown log level ", f.LogLevel) + } + logrus.SetLevel(level) + } + s, err := newServer(f) if err != nil { logrus.Fatal(err) } - err = s.tcpIn.Start() - if err != nil { - logrus.Fatal(err) - } - err = s.udpIn.Start() + + err = s.Start() if err != nil { logrus.Fatal(err) } @@ -98,8 +98,7 @@ func run(cmd *cobra.Command, f *flags) { signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM) <-osSignals - s.tcpIn.Close() - s.udpIn.Close() + s.Close() } type server struct { @@ -126,46 +125,6 @@ func (s *server) Close() error { func newServer(f *flags) (*server, error) { s := new(server) - if f.ConfigFile != "" { - configFile, err := ioutil.ReadFile(f.ConfigFile) - if err != nil { - return nil, E.Cause(err, "read config file") - } - flagsNew := new(flags) - err = json.Unmarshal(configFile, flagsNew) - if err != nil { - return nil, E.Cause(err, "decode config file") - } - if flagsNew.Server != "" && f.Server == "" { - f.Server = flagsNew.Server - } - if flagsNew.ServerPort != 0 && f.ServerPort == 0 { - f.ServerPort = flagsNew.ServerPort - } - if flagsNew.Bind != "" && f.Bind == "" { - f.Bind = flagsNew.Bind - } - if flagsNew.LocalPort != 0 && f.LocalPort == 0 { - f.LocalPort = flagsNew.LocalPort - } - if flagsNew.Password != "" && f.Password == "" { - f.Password = flagsNew.Password - } - if flagsNew.Key != "" && f.Key == "" { - f.Key = flagsNew.Key - } - if flagsNew.Method != "" && f.Method == "" { - f.Method = flagsNew.Method - } - if flagsNew.Verbose { - f.Verbose = true - } - } - - if f.Verbose { - logrus.SetLevel(logrus.TraceLevel) - } - if f.Server == "" { return nil, E.New("missing server address") } else if f.ServerPort == 0 {