func initHosts() error { m := multiconfig.NewWithPath("conf/hosts.toml") err := m.Load(Cfg) if err != nil { return err } return nil }
func newRedisService() { configPath := getUserConfigFile("redis.toml") m := multiconfig.NewWithPath(configPath) err := m.Load(Cfg) if err != nil { panic(err) } }
func appConfig() { configPath := getUserConfigFile("app.toml") m := multiconfig.NewWithPath(configPath) err := m.Load(Cfg) if err != nil { panic(err) } }
func main() { printVer := flag.Bool("version", false, "print version") config_path := flag.String("config", "config.toml", "配置文件路径") flag.String("addr", ":5050", "绑定地址") flag.Parse() if *printVer { fmt.Println("TcpRoute2 version", version) return } config_dir := filepath.Dir(*config_path) m := multiconfig.NewWithPath(*config_path) serverConfig := new(ServerConfig) m.MustLoad(serverConfig) if len(serverConfig.PreHttpPorts) == 0 && len(serverConfig.PreHttpsPorts) == 0 { log.Printf("未配置是否启用 客户端dns解析纠正功能,默认将在发现浏览器进行了dns本地解析时强制改为为代理服务器进行dns解析。") serverConfig.PreHttpPorts = []int{80} serverConfig.PreHttpsPorts = []int{443} } preHttpPorts = serverConfig.PreHttpPorts preHttpsPorts = serverConfig.PreHttpsPorts if err := netchan.HostsDns.Config(&netchan.DnschanHostsConfig{BashPath: config_dir, Hostss: serverConfig.Hosts, CheckInterval: 1 * time.Minute, }); err != nil { panic(err) } // 创建 tcpping 上层代理 upStream := NewTcppingUpStream() for _, up := range serverConfig.UpStreams { if up.Name == "" { up.Name = up.ProxyUrl } if err := upStream.AddUpStream(up.Name, up.ProxyUrl, up.DnsResolve, up.Credit, time.Duration(up.Sleep)*time.Millisecond, time.Duration(up.CorrectDelay)*time.Millisecond); err != nil { panic(err) } } // 服务器监听 srv := NewServer(serverConfig.Addr, upStream) // TODO: DNS 配置 // TODO: 各端口需要的安全级别 srv.ListAndServe() }
func newLogService() { f := "logs.toml" configPath := getUserConfigFile(f) m := multiconfig.NewWithPath(configPath) err := m.Load(Cfg) if err != nil { panic(err) } log.Info("k \n %v", Cfg.Logs) }
func main() { m := multiconfig.NewWithPath("config.toml") // supports TOML and JSON // Get an empty struct for your configuration serverConf := new(Server) // Populated the serverConf struct m.MustLoad(serverConf) // Check for error fmt.Println("After Loading: ") fmt.Printf("%+v\n", serverConf) if serverConf.Enabled { fmt.Println("Enabled field is set to true") } else { fmt.Println("Enabled field is set to false") } }
func initConfig(configFile string) error { m := multiconfig.NewWithPath(configFile) mcfg := new(MainConfig) m.MustLoad(mcfg) Config = &redsnif.SniffConfig{} Config.Device = mcfg.Network.Device Config.Snaplen = int32(mcfg.Network.Snaplen) Config.Timeout = time.Duration(time.Millisecond * time.Duration(mcfg.Network.Timeout)) Config.Promiscuous = mcfg.Network.Promiscuous Config.UseZeroCopy = mcfg.Network.UseZeroCopy Config.Host = mcfg.Redis.Host Config.Port = mcfg.Redis.Port Config.MaxBufSize = mcfg.Redis.MaxBufSize Config.AzConfig = &redsnif.AnalyzeConfig{ ReadHitAnalyze: mcfg.Analyze.ReadHitAnalyze, SaveCmdTypes: mcfg.Analyze.SaveCmdTypes, SaveDetail: mcfg.Analyze.SaveDetail, } return nil }
func main() { m := multiconfig.NewWithPath("config.toml") err := m.Load(conf) if err != nil { log.Fatal(err) } if !conf.Debug { gin.SetMode(gin.ReleaseMode) } db, err := sqlx.Open(conf.DBType, conf.DBConn) if err != nil { log.Fatal(err) } defer db.Close() reportd := Reportd{db: db} router := gin.Default() router.LoadHTMLGlob("templates/*") router.GET("/", reportd.getIndex) router.GET("/json", reportd.getIndexJson) router.Run(conf.Listen) }
func newLogService() { f := "logs.toml" configPath := getUserConfigFile(f) m := multiconfig.NewWithPath(configPath) err := m.Load(Cfg) if err != nil { panic(err) } for _, conf := range Cfg.Logs { if !conf.ENABLE { continue } level, ok := logLevels[conf.LEVEL] if !ok { log.Fatal(4, "Unknown log level: %s", conf.LEVEL) } str := "" switch conf.MODE { case "console": str = fmt.Sprintf(`{"level":%s}`, level) case "file": str = fmt.Sprintf( `{"level":%s,"filename":"%s","rotate":%v,"maxlines":%d,"maxsize":%d,"daily":%v,"maxdays":%d}`, level, conf.FILE_NAME, conf.LOG_ROTATE, conf.MAX_LINES, 1<<uint(conf.MAX_SIZE_SHIFT), conf.DAILY_ROTATE, conf.MAX_DAYS, ) case "conn": str = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":"%s","addr":"%s"}`, level, conf.RECONNECT_ON_MSG, conf.RECONNECT, conf.PROTOCOL, conf.ADDR) case "smtp": tos, err := json.Marshal(conf.RECEIVERS) if err != nil { log.Error(4, "json.Marshal(conf.RECEIVERS) err %v", err) continue } str = fmt.Sprintf(`{"level":%s,"username":"******","password":"******","host":"%s","sendTos":%s,"subject":"%s"}`, level, conf.USER, conf.PASSWD, conf.HOST, tos, conf.SUBJECT) case "database": str = fmt.Sprintf(`{"level":%s,"driver":"%s","conn":"%s"}`, level, conf.DRIVER, conf.CONN) default: continue } log.Info(str) log.NewLogger(conf.BUFFER_LEN, conf.MODE, str) log.Info("Log Mode: %s(%s)", conf.MODE, conf.LEVEL) } }
//read config file func readConfig() { m := multiconfig.NewWithPath("config.toml") // supports TOML and JSON // Populated the serverConf struct m.MustLoad(config) // Check for error }
func main() { printVer := flag.Bool("version", false, "print version") config_path := flag.String("config", "config.toml", "配置文件路径") flag.String("addr", ":5050", "绑定地址") flag.Parse() if *printVer { fmt.Println("TcpRoute2 version", version) return } config_dir := filepath.Dir(*config_path) m := multiconfig.NewWithPath(*config_path) serverConfig := new(ServerConfig) m.MustLoad(serverConfig) if len(serverConfig.PreHttpPorts) == 0 && len(serverConfig.PreHttpsPorts) == 0 { log.Printf("未配置是否启用 客户端dns解析纠正功能,默认将在发现浏览器进行了dns本地解析时强制改为为代理服务器进行dns解析。") serverConfig.PreHttpPorts = []int{80} serverConfig.PreHttpsPorts = []int{443} } preHttpPorts = serverConfig.PreHttpPorts preHttpsPorts = serverConfig.PreHttpsPorts if err := netchan.HostsDns.Config(&netchan.DnschanHostsConfig{BashPath: config_dir, Hostss: serverConfig.Hosts, CheckInterval: 1 * time.Minute, }); err != nil { log.Print(err) return } // 获得线路列表 configDialClients := ConfigDialClients{ UpStreams: serverConfig.UpStreams, BasePath: config_dir, } dialClients, err := NewDialClients(&configDialClients) if err != nil { log.Print(err) return } // 创建 tcpping 上层代理 upStream := NewTcppingUpStream(dialClients) // 服务器监听 srv := NewServer(serverConfig.Addr, upStream) // TODO: DNS 配置 // TODO: 各端口需要的安全级别 if err := srv.ListAndServe(); err != nil { log.Print(err) return } }
func main() { // Load the configuration m := multiconfig.NewWithPath(os.Getenv("config")) cfg := &dns.Config{} m.MustLoad(cfg) // Parse the log level lvl, err := log.LvlFromString(cfg.LogLevel) if err != nil { panic(err) } // Create a new logger l := log.New() l.SetHandler( log.LvlFilterHandler(lvl, log.StdoutHandler), ) // Start the initialization il := l.New("module", "init") il.Info("Starting up the application") // Parse the Redis connection string url := redisurl.Parse(cfg.Redis) // Create an options list opts := []redis.DialOption{} if url.Database != 0 { opts = append(opts, redis.DialDatabase(url.Database)) } if url.Password != "" { opts = append(opts, redis.DialPassword(url.Password)) } // Verify the DNS setup results, err := net.LookupNS(cfg.Domain) if err != nil { il.Warn("Unable to look up the NS of domain", "domain", cfg.Domain, "error", err.Error()) } if len(results) > 0 { found := false for _, record := range results { if record.Host == cfg.Hostname { found = true } } if !found { existing := []string{} for _, record := range results { existing = append(existing, record.Host) } il.Warn("Invalid NS records for the domain", "domain", cfg.Domain, "records", existing) } } else { il.Warn("No NS records found for domain", "domain", cfg.Domain, "error", err.Error()) } // Dial the server rc, err := redis.Dial("tcp", url.Host+":"+strconv.Itoa(url.Port), opts...) if err != nil { il.Crit("Unable to connect to the Redis server", "error", err.Error()) return } // Initialize the DNS server ds := &dns.DNS{ Config: cfg, Redis: rc, } if err := ds.ListenAndServe(); err != nil { il.Crit("Error while listening to the specified port", "error", err.Error()) } }