func Start(home string, monitor InternalEventMonitor) error { clientConf := home + "/client.json" hostsConf := home + "/hosts.json" confdata, err := helper.ReadWithoutComment(clientConf, "//") if nil != err { //log.Println(err) return err } err = json.Unmarshal(confdata, &GConf) if nil != err { fmt.Printf("Failed to unmarshal json:%s to config for reason:%v", string(confdata), err) return err } err = hosts.Init(hostsConf) if nil != err { log.Printf("Failed to init local hosts with reason:%v.", err) } event.SetDefaultSecretKey(GConf.Encrypt.Method, GConf.Encrypt.Key) proxyHome = home GConf.init() for _, conf := range GConf.Channel { conf.Type = strings.ToUpper(conf.Type) if t, ok := proxyTypeTable[conf.Type]; !ok { log.Printf("[ERROR]No registe proxy channel type for %s", conf.Type) continue } else { v := reflect.New(t) p := v.Interface().(Proxy) if !conf.Enable { continue } if 0 == conf.ConnsPerServer { conf.ConnsPerServer = 1 } err = p.Init(conf) if nil != err { log.Printf("Proxy channel(%s):%s init failed with reason:%v", conf.Type, conf.Name, err) } else { log.Printf("Proxy channel(%s):%s init success", conf.Type, conf.Name) proxyTable[conf.Name] = p } } } logger.InitLogger(GConf.Log) log.Printf("Starting GSnova %s.", local.Version) go initDNS() go startAdminServer() startLocalServers() return nil }
func init() { key := flag.String("key", "", "Crypto key setting") listen := flag.String("listen", "", "Server listen address") logging := flag.String("log", "stdout", "Server log setting, , split by ','") auth := flag.String("auth", "*", "Auth user setting, split by ','") dps := flag.String("dps", "", "Candidate dynamic ports") ndp := flag.Uint("ndp", 0, "Max dynamic ports") conf := flag.String("conf", "server.json", "Server config file") flag.Parse() if _, err := os.Stat(*conf); os.IsNotExist(err) { if len(*key) == 0 || len(*listen) == 0 { flag.PrintDefaults() return } dpstrs := strings.Split(*dps, ",") for _, s := range dpstrs { i, err := strconv.Atoi(s) if nil == err && i > 1024 && i < 65535 { ServerConf.CandidateDynamicPort = append(ServerConf.CandidateDynamicPort, i) } } ServerConf.Log = strings.Split(*logging, ",") ServerConf.Auth = strings.Split(*auth, ",") ServerConf.Listen = *listen ServerConf.Encrypt.Key = *key ServerConf.MaxDynamicPort = int(*ndp) } else { data, err := helper.ReadWithoutComment(*conf, "//") //data, err := ioutil.ReadFile(file) if nil == err { err = json.Unmarshal(data, &ServerConf) } if nil != err { log.Fatalf("Failed to load server config:%s for reason:%v", *conf, err) return } } logger.InitLogger(ServerConf.Log) log.Printf("Load server conf success.") log.Printf("ServerConf:%v", &ServerConf) event.SetDefaultSecretKey(ServerConf.Encrypt.Method, ServerConf.Encrypt.Key) }