func newProxy(proto, ip, port string) (proxy.Proxy, error) {
	var (
		rAddr net.Addr
		lAddr net.Addr
		err   error
	)
	switch proto {
	case "tcp", "tcp6", "tcp4":
		rAddr, err = net.ResolveTCPAddr(proto, fmt.Sprintf("%s:%s", ip, port))
		if err != nil {
			return nil, err
		}
		lAddr, err = net.ResolveTCPAddr(proto, fmt.Sprintf("0.0.0.0:%s", port))
		if err != nil {
			return nil, err
		}
	case "udp", "udp4", "udp6":
		rAddr, err = net.ResolveUDPAddr(proto, fmt.Sprintf("%s:%s", ip, port))
		if err != nil {
			return nil, err
		}
		lAddr, err = net.ResolveUDPAddr(proto, fmt.Sprintf("0.0.0.0:%s", port))
		if err != nil {
			return nil, err
		}
	default:
		return nil, fmt.Errorf("Unsupported proto: %s", proto)
	}
	return proxy.NewProxy(lAddr, rAddr)
}
Ejemplo n.º 2
0
func main() {
	newAddr := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8080}
	originAddr := &net.TCPAddr{IP: net.IPv4(125, 221, 232, 253), Port: 80}
	proxy, err := proxy.NewProxy(newAddr, originAddr)
	if err != nil {
		log.Fatalln(err)
	}
	log.Println("run", originAddr, newAddr)
	proxy.Run()
}
Ejemplo n.º 3
0
// execProxy is the reexec function that is registered to start the userland proxies
func execProxy() {
	host, container := parseHostContainerAddrs()

	p, err := proxy.NewProxy(host, container)
	if err != nil {
		log.Fatal(err)
	}

	go handleStopSignals(p)

	// Run will block until the proxy stops
	p.Run()
}
Ejemplo n.º 4
0
// execProxy is the reexec function that is registered to start the userland proxies
func execProxy() {
	host, container := parseHostContainerAddrs()

	p, err := proxy.NewProxy(host, container)
	if err != nil {
		os.Stdout.WriteString("1\n")
		fmt.Fprint(os.Stderr, err)
		os.Exit(1)
	}
	go handleStopSignals(p)
	os.Stdout.WriteString("0\n")

	// Run will block until the proxy stops
	p.Run()
}
Ejemplo n.º 5
0
// execProxy is the reexec function that is registered to start the userland proxies
func execProxy() {
	f := os.NewFile(3, "signal-parent")
	host, container := parseHostContainerAddrs()

	p, err := proxy.NewProxy(host, container)
	if err != nil {
		fmt.Fprintf(f, "1\n%s", err)
		f.Close()
		os.Exit(1)
	}
	go handleStopSignals(p)
	fmt.Fprint(f, "0\n")
	f.Close()

	// Run will block until the proxy stops
	p.Run()
}
Ejemplo n.º 6
0
//proxyFactory creates docker proxy implementations
func proxyFactory(protocol string, frontend ProxyAddress, backends ...ProxyAddress) (Proxy, error) {

	if len(backends) == 0 {
		return nil, errors.New("default proxy only requies one backend")
	}
	if len(backends) > 1 {
		return nil, errors.New("default proxy only supports one backend")
	}

	backendIP := net.ParseIP(backends[0].IP)
	if backendIP == nil {
		return nil, fmt.Errorf("not a valid IP format: %v", backendIP)
	}

	frontendIP := net.ParseIP(frontend.IP)
	if frontendIP == nil {
		return nil, fmt.Errorf("not a valid IP format: %v", frontendIP)
	}

	var frontendAddr, backendAddr net.Addr
	switch strings.Trim(strings.ToLower(protocol), " ") {
	case commons.TCP:
		frontendAddr = &net.TCPAddr{IP: frontendIP, Port: int(frontend.Port)}
		backendAddr = &net.TCPAddr{IP: backendIP, Port: int(backends[0].Port)}

	case commons.UDP:
		frontendAddr = &net.UDPAddr{IP: frontendIP, Port: int(frontend.Port)}
		backendAddr = &net.UDPAddr{IP: backendIP, Port: int(backends[0].Port)}

	default:
		return nil, fmt.Errorf("unsupported protocol %v", protocol)

	}

	proxy, err := proxy.NewProxy(frontendAddr, backendAddr)
	if err != nil {
		return nil, err
	}
	return &proxyWrapper{proxy}, nil
}