package config import ( "encoding/json" "fmt" "log" "regexp" "strings" "time" "gopkg.in/yaml.v2" ) type Proxy struct { Auth string To string } func parseOneProxyCSV(s string) (string, Proxy) { p := Proxy{} key := "" l := strings.Split(s, ",") if len(l) > 0 { key = l[0] } if len(l) > 1 { p.To = l[1] } return key, p } func GetAuth() (string, string, bool) { user := conf.Get("user").GetString() pass := conf.Get("pass").GetString() return user, pass, user != "" && pass != "" } func GetTrim() string { return conf.Get("trim").GetString() } func GetPort() string { port := conf.Get("p").GetInt() return ":" + fmt.Sprint(port) } func GetAltPort() string { port := conf.Get("ap").GetInt() return ":" + fmt.Sprint(port) } func GetRate() (int, int) { rate := conf.Get("r").GetInt() burst := conf.Get("b").GetInt() log.Println("rate/burst:", rate, burst) return rate, burst } func GetRoutes() map[string]Proxy { s := conf.Get("proxy2").GetString() var dict map[string]string if err := yaml.Unmarshal([]byte(s), &dict); err == nil && len(s) > 0 { pattern := regexp.MustCompile(`(([^:]*):)?([a-z0-9]*:.*)`) result := map[string]Proxy{} for k, v := range dict { submatches := pattern.FindAllStringSubmatch(v, -1) log.Printf("%+v", submatches) result[k] = Proxy{ Auth: submatches[0][2], To: submatches[0][3], } } return result } return getRoutesCSV() } func getRoutesCSV() map[string]Proxy { list := conf.Get("proxy2").GetString() definitions := strings.Split(list, ",,") routes := make(map[string]Proxy) for _, definition := range definitions { k, v := parseOneProxyCSV(definition) routes[k] = v } return routes } func GetSSL() (string, string, bool) { crt := conf.Get("crt").GetString() key := conf.Get("key").GetString() return crt, key, crt != "" && key != "" } func GetTCP() (string, bool) { tcp := conf.Get("tcp").GetString() return tcp, tcp != "" } func GetTimeout() time.Duration { timeout := conf.Get("timeout").GetDuration() return timeout } func GetCORS(key string) bool { cors := conf.GetString("cors") var m map[string]bool if err := json.Unmarshal([]byte(cors), &m); err != nil { return false } _, ok := m[key] return ok } func GetNoPath(key string) bool { nopath := conf.GetString("nopath") var m map[string]bool if err := json.Unmarshal([]byte(nopath), &m); err != nil { return false } _, ok := m[key] return ok } func GetCompression() bool { return conf.GetBool("compression") }