func parseAddrs(addrs string) ([]string, []string) { adrs := strings.Split(addrs, ",") parsedIPv4 := []string{} parsedIPv6 := []string{} if addrs != "" { for _, adr := range adrs { ip := net.ParseIP(adr) if ip != nil { if ip.To4() != nil { parsedIPv4 = append(parsedIPv4, adr) } else { parsedIPv6 = append(parsedIPv6, adr) } } else { //Not a valid single IP, could it be a CIDR? parsedIP, net, err := net.ParseCIDR(adr) if err == nil { if parsedIP.To4() != nil { parsedIPv4 = append(parsedIPv4, net.String()) } else { parsedIPv6 = append(parsedIPv6, net.String()) } } else { log.Fatalf("Incorrectly specified target IP or CIDR:", adr) } } } } return parsedIPv4, parsedIPv6 }
func (m *Muxy) LoadPlugins() { // Load Configuration var err error var confLoader *plugo.ConfigLoader c := &PluginConfig{} if m.config.ConfigFile != "" { confLoader = &plugo.ConfigLoader{} err = confLoader.LoadFromFile(m.config.ConfigFile, &c) if err != nil { log.Fatalf("Unable to read configuration file: %s", err.Error()) } } else { log.Fatal("No config file provided") } log.SetLevel(log.LogLevel(c.LogLevel)) // Load all plugins m.middlewares = make([]Middleware, len(c.Middleware)) plugins := plugo.LoadPluginsWithConfig(confLoader, c.Middleware) for i, p := range plugins { log.Info("Loading plugin \t" + log.Colorize(log.YELLOW, c.Middleware[i].Name)) m.middlewares[i] = p.(Middleware) } m.proxies = make([]Proxy, len(c.Proxy)) plugins = plugo.LoadPluginsWithConfig(confLoader, c.Proxy) for i, p := range plugins { log.Info("Loading proxy \t" + log.Colorize(log.YELLOW, c.Proxy[i].Name)) m.proxies[i] = p.(Proxy) m.proxies[i].Setup(m.middlewares) } }
func parseLoss(loss string) float64 { val := loss if strings.Contains(loss, "%") { val = loss[:len(loss)-1] } l, err := strconv.ParseFloat(val, 64) if err != nil { log.Fatalf("Incorrectly specified packet loss:", loss) } return l }
func ParsePorts(ports string) []string { prts := strings.Split(ports, ",") parsed := []string{} if ports != "" { for _, prt := range prts { if strings.Contains(prt, ":") { if validRange(prt) { parsed = append(parsed, prt) } else { log.Fatalf("Incorrectly specified port range:", prt) } } else { //Isn't a range, check if just a single port if validPort(prt) { parsed = append(parsed, prt) } else { log.Fatalf("Incorrectly specified port:", prt) } } } } return parsed }
func parseProtos(protos string) []string { ptcs := strings.Split(protos, ",") parsed := []string{} if protos != "" { for _, ptc := range ptcs { p := strings.ToLower(ptc) if p == "udp" || p == "tcp" || p == "icmp" { parsed = append(parsed, p) } else { log.Fatalf("Incorrectly specified protocol:", p) } } } return parsed }
func check(err error) { if err != nil { log.Fatalf("Error setting up TCP Proxy: %s", err.Error()) } }