func handleServices(confIni *ini.File, p *proxy.IsolationProxy) { confSection, err := confIni.GetSection("services") check(err) for _, service := range confSection.Keys() { if !isRunningLocally(service.Name(), service.String(), p.Registry) { go func(service *ini.Key) { check(p.Handle(service.Name(), service.String())) }(service) } else { // If the service should be running locally on the same port, don't proxy it services.L.Warnf("Not handling %s on %s: should be running locally", service.Name(), service.String()) } } }
func loadServiceRegistry(confIni *ini.File) proxy.LocalRegistry { reg := make(proxy.LocalRegistry) addService := func(name string, endpoints ...string) { for _, addr := range endpoints { endpoint := &proxy.Endpoint{ Service: name, Host: addr, } endpoint.TestActive() reg.Add(name, endpoint) } } confSection, err := confIni.GetSection("backends") check(err) for _, service := range confSection.Keys() { addService(service.Name(), service.Strings(",")...) } return reg }
func getSingleHostname(file string, section string) string { var ini_cfg *ini.File loginfo := "getSingleHostname" hostname := "" ini_cfg, err := ini.Load(file) if err != nil || ini_cfg == nil { fmt.Printf("[ERROR] %s() - %s.\n", loginfo, err) return hostname } items, err := ini_cfg.GetSection(section) if err != nil { fmt.Printf("[ERROR] %s() from %s %s.\n", loginfo, file, err) os.Exit(retFailed) } keystrs := items.KeyStrings() if len(keystrs) <= 0 { fmt.Printf("[ERROR] %s() get single hostname from %s failed.", loginfo, file) os.Exit(retFailed) } for _, v := range keystrs { v = strings.TrimSpace(v) if v != "" { tokens := strings.Split(v, " ") if len(tokens) == 0 { continue } host := tokens[0] if strings.HasPrefix(host, "(") && strings.HasSuffix(host, ")") { srp := strings.NewReplacer("(", "", ")", "") host = srp.Replace(host) } else if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") { srp := strings.NewReplacer("[", "", "]", "") host = srp.Replace(host) } tokens = strings.Split(host, " ") if len(tokens) == 0 { continue } hostname = tokens[0] // Three cases to check: // 0. A hostname that contains a range pesudo-code and a port // 1. A hostname that contains just a port if strings.Count(hostname, ":") > 1 { // Possible an IPv6 address, or maybe a host line with multiple ranges // IPv6 with Port XXX:XXX::XXX.port // FQDN foo.example.com if strings.Count(hostname, ".") == 1 { hostname = hostname[0:strings.LastIndex(hostname, ".")] } } else if (strings.Count(hostname, "[") > 0 && strings.Count(hostname, "]") > 0 && (strings.LastIndex(hostname, "]") < strings.LastIndex(hostname, ":"))) || ((strings.Count(hostname, "]") <= 0) && (strings.Count(hostname, ":") > 0)) { hostname = hostname[0:strings.LastIndex(hostname, ":")] } } if hostname != "" { break } } return hostname }