func updatePasswd() { log.Println("updating password") newconfig, err := ss.ParseConfig(configFile) if err != nil { log.Printf("error parsing config file %s to update password: %v\n", configFile, err) return } oldconfig := config config = newconfig if err = unifyPortPassword(config); err != nil { return } for port, psw_limit := range config.PortPasswordLimit { passwdManager.updatePortPasswd(port, psw_limit[0], psw_limit[1]) if oldconfig.PortPasswordLimit != nil { delete(oldconfig.PortPasswordLimit, port) } } // port password still left in the old config should be closed for port, _ := range oldconfig.PortPasswordLimit { log.Printf("closing port %s as it's deleted\n", port) passwdManager.del(port) } log.Println("password updated") }
func main() { log.SetOutput(os.Stdout) var cmdConfig ss.Config var printVer bool var core int flag.BoolVar(&printVer, "version", false, "print version") flag.StringVar(&configFile, "c", "config.json", "specify config file") flag.StringVar(&cmdConfig.Password, "k", "", "password") flag.IntVar(&cmdConfig.ServerPort, "p", 0, "server port") flag.IntVar(&cmdConfig.Timeout, "t", 60, "connection timeout (in seconds)") flag.StringVar(&cmdConfig.Method, "m", "", "encryption method, default: aes-256-cfb") flag.IntVar(&core, "core", 0, "maximum number of CPU cores to use, default is determinied by Go runtime") flag.BoolVar((*bool)(&debug), "d", false, "print debug message") flag.BoolVar(&udp, "u", false, "UDP Relay") flag.Parse() if printVer { ss.PrintVersion() os.Exit(0) } ss.SetDebug(debug) var err error config, err = ss.ParseConfig(configFile) if err != nil { if !os.IsNotExist(err) { fmt.Fprintf(os.Stderr, "error reading %s: %v\n", configFile, err) os.Exit(1) } config = &cmdConfig } else { ss.UpdateConfig(config, &cmdConfig) } if config.Method == "" { config.Method = "aes-128-cfb" } if err = ss.CheckCipherMethod(config.Method); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } if err = unifyPortPassword(config); err != nil { os.Exit(1) } if core > 0 { runtime.GOMAXPROCS(core) } if udp { ss.InitNAT() } //read from file flowData, err = flow.ParseConfig("flowInfo.json") if err != nil { now := time.Now().Format("2006-01-02 15:04:05") flowData = &flow.Info{now, map[string][]string{}} flow.SaveConfig("flowInfo.json", flowData) } for port, pass_limit := range config.PortPasswordLimit { go run(port, pass_limit[0], pass_limit[1]) if udp { go runUDP(port, pass_limit[0], pass_limit[1]) } } //url http.HandleFunc("/", status) //设置访问的路由 http.Handle("/css/", http.FileServer(http.Dir("static"))) http.Handle("/js/", http.FileServer(http.Dir("static"))) err = http.ListenAndServe(":9000", nil) //设置监听的端口 if err != nil { log.Fatal("ListenAndServe: ", err) } waitSignal() }