github.com/database64128/shadowsocks-go@v1.7.0/cmd/shadowsocks-go/main.go (about) 1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "os" 7 "os/signal" 8 "syscall" 9 10 "github.com/database64128/shadowsocks-go/jsonhelper" 11 "github.com/database64128/shadowsocks-go/logging" 12 "github.com/database64128/shadowsocks-go/service" 13 "go.uber.org/zap" 14 "go.uber.org/zap/zapcore" 15 ) 16 17 var ( 18 testConf = flag.Bool("testConf", false, "Test the configuration file without starting the services") 19 confPath = flag.String("confPath", "", "Path to JSON configuration file") 20 zapConf = flag.String("zapConf", "", "Preset name or path to JSON configuration file for building the zap logger.\nAvailable presets: console (default), systemd, production, development") 21 logLevel = flag.String("logLevel", "", "Override the logger configuration's log level.\nAvailable levels: debug, info, warn, error, dpanic, panic, fatal") 22 ) 23 24 func main() { 25 flag.Parse() 26 27 if *confPath == "" { 28 fmt.Println("Missing -confPath <path>.") 29 flag.Usage() 30 os.Exit(1) 31 } 32 33 var ( 34 zc zap.Config 35 sc service.Config 36 ) 37 38 switch *zapConf { 39 case "console", "": 40 zc = logging.NewProductionConsoleConfig(false) 41 case "systemd": 42 zc = logging.NewProductionConsoleConfig(true) 43 case "production": 44 zc = zap.NewProductionConfig() 45 case "development": 46 zc = zap.NewDevelopmentConfig() 47 default: 48 if err := jsonhelper.LoadAndDecodeDisallowUnknownFields(*zapConf, &zc); err != nil { 49 fmt.Println(err) 50 os.Exit(1) 51 } 52 } 53 54 if *logLevel != "" { 55 l, err := zapcore.ParseLevel(*logLevel) 56 if err != nil { 57 fmt.Println(err) 58 os.Exit(1) 59 } 60 zc.Level.SetLevel(l) 61 } 62 63 logger, err := zc.Build() 64 if err != nil { 65 fmt.Println(err) 66 os.Exit(1) 67 } 68 defer logger.Sync() 69 70 if err = jsonhelper.LoadAndDecodeDisallowUnknownFields(*confPath, &sc); err != nil { 71 logger.Fatal("Failed to load config", 72 zap.Stringp("confPath", confPath), 73 zap.Error(err), 74 ) 75 } 76 77 m, err := sc.Manager(logger) 78 if err != nil { 79 logger.Fatal("Failed to create service manager", 80 zap.Stringp("confPath", confPath), 81 zap.Error(err), 82 ) 83 } 84 defer m.Close() 85 86 if *testConf { 87 logger.Info("Config test OK", zap.Stringp("confPath", confPath)) 88 return 89 } 90 91 if err = m.Start(); err != nil { 92 logger.Fatal("Failed to start services", 93 zap.Stringp("confPath", confPath), 94 zap.Error(err), 95 ) 96 } 97 98 sigCh := make(chan os.Signal, 1) 99 signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) 100 sig := <-sigCh 101 logger.Info("Received exit signal", zap.Stringer("signal", sig)) 102 103 m.Stop() 104 }