// DialerFromEnvironment takes in a "direct" *net.Dialer and returns a // proxy.Dialer which will route the connections through the proxy using the // given dialer. func DialerFromEnvironment(direct *net.Dialer) (proxy.Dialer, error) { allProxy := GetProxyEnv("all_proxy") if len(allProxy) == 0 { return direct, nil } proxyURL, err := url.Parse(allProxy) if err != nil { return direct, err } proxyFromURL, err := proxy.FromURL(proxyURL, direct) if err != nil { return direct, err } noProxy := GetProxyEnv("no_proxy") if len(noProxy) == 0 { return proxyFromURL, nil } perHost := proxy.NewPerHost(proxyFromURL, direct) perHost.AddFromString(noProxy) return perHost, nil }
func main() { listenAddr := flag.String("listen", ":8080", "Listening address and port for HTTP relay proxy.") blockedAddrs := flag.String("block", "", "Comma-separated list of blocked host names, zone names, ip addresses and CIDR addresses.") flag.Parse() // Prepare proxy dialer var dialer proxy.Dialer = proxy.Direct if *blockedAddrs != "" { // Prepare dialer for blocked addresses perHostDialer := proxy.NewPerHost(dialer, &httpRelay.NopDialer{}) perHostDialer.AddFromString(*blockedAddrs) dialer = perHostDialer } // Start HTTP proxy server log.Println("HTTP proxy server started on", *listenAddr) log.Println(http.ListenAndServe(*listenAddr, &httpRelay.HTTPProxyHandler{Dialer: dialer})) }
func main() { socksAddr := flag.String("socks", "localhost:8000", "Address and port of SOCKS5 proxy server.") listenAddr := flag.String("listen", ":8080", "Listening address and port for HTTP relay proxy.") blockedAddrs := flag.String("block", "", "Comma-separated list of blocked host names, zone names, ip addresses and CIDR addresses.") flag.Parse() // Prepare proxy relay with target SOCKS proxy dialer, err := proxy.SOCKS5("tcp", *socksAddr, nil, proxy.Direct) if err != nil { log.Println("Failed to create proxy definition:", err.Error()) return } if *blockedAddrs != "" { // Prepare dialer for blocked addresses perHostDialer := proxy.NewPerHost(dialer, &httpRelay.NopDialer{}) perHostDialer.AddFromString(*blockedAddrs) dialer = perHostDialer } // Start HTTP proxy server log.Println("HTTP proxy relay server started on", *listenAddr, "relaying to SOCKS proxy", *socksAddr) log.Println(http.ListenAndServe(*listenAddr, &httpRelay.HTTPProxyHandler{Dialer: dialer})) }
// This is a rip off of proxy.FromEnvironment with a custom forward dialer func getDialer(forward proxy.Dialer) proxy.Dialer { allProxy := os.Getenv("all_proxy") if len(allProxy) == 0 { return forward } proxyURL, err := url.Parse(allProxy) if err != nil { return forward } prxy, err := proxy.FromURL(proxyURL, forward) if err != nil { return forward } noProxy := os.Getenv("no_proxy") if len(noProxy) == 0 { return prxy } perHost := proxy.NewPerHost(prxy, forward) perHost.AddFromString(noProxy) return perHost }