func SetMessageDataWithDesc(lang, langDesc string, data []byte) error { message, err := goconfig.LoadFromData(data) if err == nil { message.BlockMode = false lc := new(locale) lc.lang = lang lc.langDesc = langDesc lc.message = message if locales.Add(lc) == false { return fmt.Errorf("Lang %s alread exist", lang) } } return err }
func main() { // 读取证书文件 rootPEM, err := ioutil.ReadFile("cert.pem") if err != nil { log.Println("读取 cert.pem 出错:", err, "请检查文件是否存在") return } roots := x509.NewCertPool() ok := roots.AppendCertsFromPEM(rootPEM) if !ok { log.Println("证书分析失败,请检查证书文件是否正确") return } // 加载配置文件 cfg, err := goconfig.LoadConfigFile("client.ini") if err != nil { log.Println("配置文件加载失败,自动重置配置文件:", err) cfg, err = goconfig.LoadFromData([]byte{}) if err != nil { log.Println(err) return } } var ( port, ok1 = cfg.MustValueSet("client", "port", "7071") key, ok2 = cfg.MustValueSet("client", "key", "eGauUecvzS05U5DIsxAN4n2hadmRTZGBqNd2zsCkrvwEBbqoITj36mAMk4Unw6Pr") serverHost, ok3 = cfg.MustValueSet("server", "host", "localhost") serverPort, ok4 = cfg.MustValueSet("server", "port", "8081") ) // 如果缺少配置则保存为默认配置 if ok1 || ok2 || ok3 || ok4 { err = goconfig.SaveConfigFile(cfg, "client.ini") if err != nil { log.Println("配置文件保存失败:", err) } } ln, err := net.Listen("tcp", ":"+port) if err != nil { log.Println(err) return } defer ln.Close() log.Println("|>>>>>>>>>>>>>>>|<<<<<<<<<<<<<<<|") log.Println("程序版本:" + VERSION) log.Println("代理端口:" + port) log.Println("Key:" + key) log.Println("服务器地址:" + serverHost + ":" + serverPort) log.Println("|>>>>>>>>>>>>>>>|<<<<<<<<<<<<<<<|") s := &serve{ serverHost: serverHost, serverPort: serverPort, key: key, conf: &tls.Config{ RootCAs: roots, }, } // 登录 if err = s.updateSession(); err != nil { log.Println("与服务器连接失败:", err) return } log.Println("登录成功,服务器连接完毕") go s.updateSessionLoop() for { conn, err := ln.Accept() if err != nil { log.Println(err) continue } go s.handleConnection(conn) } }
// NewConfigContext initializes configuration context. // NOTE: do not print any log except error. func NewConfigContext() { workDir, err := WorkDir() if err != nil { log.Fatal("Fail to get work directory: %v", err) } data, err := bin.Asset("conf/app.ini") if err != nil { log.Fatal("Fail to read 'conf/app.ini': %v", err) } Cfg, err = goconfig.LoadFromData(data) if err != nil { log.Fatal("Fail to parse 'conf/app.ini': %v", err) } CustomPath = os.Getenv("GOGS_CUSTOM") if len(CustomPath) == 0 { CustomPath = path.Join(workDir, "custom") } cfgPath := path.Join(CustomPath, "conf/app.ini") if com.IsFile(cfgPath) { if err = Cfg.AppendFiles(cfgPath); err != nil { log.Fatal("Fail to load custom 'conf/app.ini': %v", err) } } else { log.Warn("No custom 'conf/app.ini' found") } AppName = Cfg.MustValue("", "APP_NAME", "Gogs: Go Git Service") AppLogo = Cfg.MustValue("", "APP_LOGO", "img/favicon.png") AppUrl = Cfg.MustValue("server", "ROOT_URL", "http://localhost:3000") Protocol = HTTP if Cfg.MustValue("server", "PROTOCOL") == "https" { Protocol = HTTPS CertFile = Cfg.MustValue("server", "CERT_FILE") KeyFile = Cfg.MustValue("server", "KEY_FILE") } Domain = Cfg.MustValue("server", "DOMAIN", "localhost") HttpAddr = Cfg.MustValue("server", "HTTP_ADDR", "0.0.0.0") HttpPort = Cfg.MustValue("server", "HTTP_PORT", "3000") SshPort = Cfg.MustInt("server", "SSH_PORT", 22) OfflineMode = Cfg.MustBool("server", "OFFLINE_MODE") DisableRouterLog = Cfg.MustBool("server", "DISABLE_ROUTER_LOG") StaticRootPath = Cfg.MustValue("server", "STATIC_ROOT_PATH", workDir) LogRootPath = Cfg.MustValue("log", "ROOT_PATH", path.Join(workDir, "log")) InstallLock = Cfg.MustBool("security", "INSTALL_LOCK") SecretKey = Cfg.MustValue("security", "SECRET_KEY") LogInRememberDays = Cfg.MustInt("security", "LOGIN_REMEMBER_DAYS") CookieUserName = Cfg.MustValue("security", "COOKIE_USERNAME") CookieRememberName = Cfg.MustValue("security", "COOKIE_REMEMBER_NAME") RunUser = Cfg.MustValue("", "RUN_USER") curUser := os.Getenv("USER") if len(curUser) == 0 { curUser = os.Getenv("USERNAME") } // Does not check run user when the install lock is off. if InstallLock && RunUser != curUser { log.Fatal("Expect user(%s) but current user is: %s", RunUser, curUser) } // Determine and create root git reposiroty path. homeDir, err := com.HomeDir() if err != nil { log.Fatal("Fail to get home directory: %v", err) } RepoRootPath = Cfg.MustValue("repository", "ROOT", filepath.Join(homeDir, "gogs-repositories")) if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil { log.Fatal("Fail to create repository root path(%s): %v", RepoRootPath, err) } ScriptType = Cfg.MustValue("repository", "SCRIPT_TYPE", "bash") PictureService = Cfg.MustValueRange("picture", "SERVICE", "server", []string{"server"}) DisableGravatar = Cfg.MustBool("picture", "DISABLE_GRAVATAR") }
func main() { // 读取配置文件 cfg, err := goconfig.LoadConfigFile("server.ini") if err != nil { log.Println("配置文件加载失败,自动重置配置文件", err) cfg, err = goconfig.LoadFromData([]byte{}) if err != nil { log.Println(err) return } } var ( key, ok1 = cfg.MustValueSet("client", "key", "eGauUecvzS05U5DIsxAN4n2hadmRTZGBqNd2zsCkrvwEBbqoITj36mAMk4Unw6Pr") port, ok2 = cfg.MustValueSet("server", "port", "8081") ) // 如果缺少配置则保存为默认配置 if ok1 || ok2 { err = goconfig.SaveConfigFile(cfg, "server.ini") if err != nil { log.Println("配置文件保存失败:", err) } } // 读取公私钥 cer, err := tls.LoadX509KeyPair("cert.pem", "key.pem") if err != nil { log.Println(err) return } // 监听端口 ln, err := tls.Listen("tcp", ":"+port, &tls.Config{ Certificates: []tls.Certificate{cer}, }) if err != nil { log.Println(err) return } defer ln.Close() s := &serve{ key: key, clients: make(map[string]uint), } // 加载完成后输出配置信息 log.Println("|>>>>>>>>>>>>>>>|<<<<<<<<<<<<<<<|") log.Println("程序版本:" + version) log.Println("监听端口:" + port) log.Println("Key:" + key) log.Println("|>>>>>>>>>>>>>>>|<<<<<<<<<<<<<<<|") for { conn, err := ln.Accept() if err != nil { log.Println(err) continue } go s.handleConnection(conn) } }