github.com/psiphon-labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/upstreamproxy/README.md (about)

     1  `upstreamproxy` Package
     2  =======================
     3  
     4  This provides upstream proxy support by extending golang.org/x/net/proxy package.
     5  
     6  Currently supported protocols:
     7  * SOCKS4 via `socks4a` URI scheme
     8  * SOCKS5 via `socks5` URI scheme
     9  * HTTP 'CONNECT' with Basic, Digest and NTLM Authentication via `http` URI scheme
    10  
    11  # Usage
    12  
    13  Note: `NewProxyDialFunc` returns `ForwardDialFunc` if `ProxyURIString` is empty
    14  
    15  ```
    16  /* 
    17     Proxy URI examples:
    18     "http://proxyhost:8080"
    19     "socks5://user:password@proxyhost:1080"
    20     "http://NTDOMAIN\NTUser:password@proxyhost:3375"
    21  */
    22  
    23  //Plain HTTP transport via HTTP proxy
    24  func doAuthenticatedHTTP() {
    25  	proxyUrl, err := url.Parse("http://user:password@172.16.1.1:8080")
    26  	transport := &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
    27  	customHeader := http.Header{"Test" :{"test"}}
    28  
    29  	authHttpTransport, err := upstreamproxy.NewProxyAuthTransport(transport, customHeader)
    30  	if err != nil {
    31  		fmt.Println("Error: ", err)
    32  		return
    33  	}
    34  	r, err := http.NewRequest("GET", "http://www.reddit.com", bytes.NewReader(data))
    35  	if err != nil {
    36  		fmt.Println("Error: ", err)
    37  		return
    38  	}
    39  	resp, err := authHttpTransport.RoundTrip(r)
    40  	if err != nil {
    41  		fmt.Println("RoundTrip Error: ", err)
    42  		return
    43  	}
    44  	ioutil.ReadAll(resp.Body)
    45  	fmt.Println(string(resp.Status))
    46  }
    47  
    48  //HTTPS transport via HTTP proxy
    49  func doAuthenticatedHTTPS() {
    50  	dialTlsFn := func(netw, addr string) (net.Conn, error) {
    51  		config := &upstreamproxy.UpstreamProxyConfig{
    52  			ForwardDialFunc: net.Dial,
    53  			ProxyURIString:  "http://user:password@172.16.1.1:8080",
    54  		}
    55  
    56  		proxyDialFunc := upstreamproxy.NewProxyDialFunc(config)
    57  
    58  		conn, err := proxyDialFunc(netw, addr)
    59  		if err != nil {
    60  			return nil, err
    61  		}
    62  		tlsconfig := &tls.Config{InsecureSkipVerify: false}
    63  		tlsConn := tls.Client(conn, tlsconfig)
    64  
    65  		return tlsConn, tlsConn.Handshake()
    66  	}
    67  
    68  	r, err := http.NewRequest("GET", "https://www.reddit.com", bytes.NewReader(data))
    69  	transport = &http.Transport{DialTLS: dialTlsFn}
    70  	resp, err := transport.RoundTrip(r)
    71  	if err != nil {
    72  		log.Println("RoundTrip Error: ", err)
    73  		return
    74  	}
    75  	ioutil.ReadAll(resp.Body)
    76  	fmt.Println(string(resp.Status))
    77  }
    78  
    79  //HTTP transport via SOCKS5 proxy
    80  func doAuthenticatedHttpSocks() {
    81  	dialFn := func(netw, addr string) (net.Conn, error) {
    82  		config := &upstreamproxy.UpstreamProxyConfig{
    83  			ForwardDialFunc: net.Dial,
    84  			ProxyURIString:  "socks5://user:password@172.16.1.1:5555",
    85  		}
    86  
    87  		proxyDialFunc := upstreamproxy.NewProxyDialFunc(config)
    88  
    89  		return proxyDialFunc(netw, addr)
    90  	}
    91  
    92  	r, err := http.NewRequest("GET", "https://www.reddit.com", bytes.NewReader(data))
    93  	transport = &http.Transport{Dial: dialFn}
    94  	resp, err := transport.RoundTrip(r)
    95  	if err != nil {
    96  		log.Println("RoundTrip Error: ", err)
    97  		return
    98  	}
    99  	ioutil.ReadAll(resp.Body)
   100  	fmt.Println(string(resp.Status))
   101  }
   102  ```
   103