Esempio n. 1
0
func httpsBalance(bind string, backends BA.Backends) {
	if httpsOptions.certFile == "" || httpsOptions.keyFile == "" {
		log.Fatalln("specify both -cert and -key")
	}

	log.Println("using https balancing")

	proxy := &Proxy{
		&httputil.ReverseProxy{Director: func(req *http.Request) {
			backend := backends.Choose()
			if backend == nil {
				log.Printf("no backend for client %s", req.RemoteAddr)
				panic(NoBackend{})
			}
			req.URL.Scheme = "http"
			req.Header.Add("X-Forwarded-Proto", "https")
			req.URL.Host = backend.String()
			req.Header.Add(XRealIP, RealIP(req))
		}},
	}
	log.Printf("listening on %s, balancing %d backends", bind, backends.Len())
	err := http.ListenAndServeTLS(bind, httpsOptions.certFile, httpsOptions.keyFile, proxy)
	if err != nil {
		log.Fatalf("failed to bind: %s", err)
	}
}
Esempio n. 2
0
func httpBalance(bind string, backends BA.Backends) {
	log.Println("using http balancing")
	proxy := &httputil.ReverseProxy{Director: func(req *http.Request) {
		req.URL.Host = backends.Choose()
	}}
	log.Printf("listening on %s, balancing %d backends", bind, backends.Len())
	err := http.ListenAndServe(bind, proxy)
	if err != nil {
		log.Fatalf("failed to bind: %s", err)
	}
}
Esempio n. 3
0
func httpBalance(bind string, backends BA.Backends) error {
	log.Println("using http balancing")
	proxy := &Proxy{
		&httputil.ReverseProxy{Director: func(req *http.Request) {
			backend := backends.Choose()
			if backend == nil {
				log.Printf("no backend for client %s", req.RemoteAddr)
				panic(NoBackend{})
			}
			req.URL.Scheme = "http"
			req.URL.Host = backend.String()
			req.Header.Add(XRealIP, RealIP(req))
		}},
	}
	log.Printf("listening on %s, balancing %d backends", bind, backends.Len())
	return http.ListenAndServe(bind, proxy)
}
Esempio n. 4
0
func tcpBalance(bind string, backends BA.Backends) {
	log.Println("using tcp balancing")
	ln, err := net.Listen("tcp", bind)
	if err != nil {
		log.Fatalf("failed to bind: %s", err)
	}

	log.Printf("listening on %s, balancing %d backends", bind, backends.Len())

	for {
		conn, err := ln.Accept()
		if err != nil {
			log.Printf("failed to accept: %s", err)
			continue
		}
		go handleConnection(conn, backends.Choose())
	}
}