func initTableCache(config *ss.Config) { var exists bool var err error if !config.CacheEncTable { goto emptyCache } exists, err = ss.IsFileExists(tableCacheFile) if exists { // load table cache from file f, err := os.Open(tableCacheFile) if err != nil { log.Println("error opening table cache:", err) goto emptyCache } defer f.Close() dec := gob.NewDecoder(bufio.NewReader(f)) if err = dec.Decode(&table.cache); err != nil { log.Println("error loading table cache:", err) goto emptyCache } debug.Println("table cache loaded from disk") return } if err != nil { log.Println("table cache:", err) } emptyCache: debug.Println("creating empty table cache") table.cache = map[string]*ss.EncryptTable{} }
func main() { log.SetOutput(os.Stdout) var configFile, cmdServer string var cmdConfig ss.Config var printVer bool flag.BoolVar(&printVer, "version", false, "print version") flag.StringVar(&configFile, "c", "config.json", "specify config file") flag.StringVar(&cmdServer, "s", "", "server address") flag.StringVar(&cmdConfig.Password, "k", "", "password") flag.IntVar(&cmdConfig.ServerPort, "p", 0, "server port") flag.IntVar(&cmdConfig.LocalPort, "l", 0, "local socks5 proxy port") flag.StringVar(&cmdConfig.Method, "m", "", "encryption method, use empty string or rc4") flag.BoolVar((*bool)(&debug), "d", false, "print debug message") flag.Parse() if printVer { ss.PrintVersion() os.Exit(0) } cmdConfig.Server = cmdServer ss.SetDebug(debug) exists, err := ss.IsFileExists(configFile) // If no config file in current directory, try search it in the binary directory // Note there's no portable way to detect the binary directory. binDir := path.Dir(os.Args[0]) if (!exists || err != nil) && binDir != "" && binDir != "." { oldConfig := configFile configFile = path.Join(binDir, "config.json") log.Printf("%s not found, try config file %s\n", oldConfig, configFile) } config, err := ss.ParseConfig(configFile) if err != nil { config = &cmdConfig if !os.IsNotExist(err) { fmt.Fprintf(os.Stderr, "error reading %s: %v\n", configFile, err) os.Exit(1) } } else { ss.UpdateConfig(config, &cmdConfig) } if len(config.ServerPassword) == 0 { if !enoughOptions(config) { 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 { fmt.Fprintln(os.Stderr, "given server_password, ignore server, server_port and password option:", config) } if config.LocalPort == 0 { fmt.Fprintln(os.Stderr, "must specify local port") os.Exit(1) } } parseServerConfig(config) run(strconv.Itoa(config.LocalPort)) }