1 Star 0 Fork 0

lqinggang/psiphon-tunnel-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

upstreamproxy Package

This provides upstream proxy support by extending golang.org/x/net/proxy package.

Currently supported protocols:

  • SOCKS4 via socks4a URI scheme
  • SOCKS5 via socks5 URI scheme
  • HTTP 'CONNECT' with Basic, Digest and NTLM Authentication via http URI scheme

Usage

Note: NewProxyDialFunc returns ForwardDialFunc if ProxyURIString is empty

/* 
   Proxy URI examples:
   "http://proxyhost:8080"
   "socks5://user:password@proxyhost:1080"
   "http://NTDOMAIN\NTUser:password@proxyhost:3375"
*/

//Plain HTTP transport via HTTP proxy
func doAuthenticatedHTTP() {
	proxyUrl, err := url.Parse("http://user:password@172.16.1.1:8080")
	transport := &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
	customHeader := http.Header{"Test" :{"test"}}

	authHttpTransport, err := upstreamproxy.NewProxyAuthTransport(transport, customHeader)
	if err != nil {
		fmt.Println("Error: ", err)
		return
	}
	r, err := http.NewRequest("GET", "http://www.reddit.com", bytes.NewReader(data))
	if err != nil {
		fmt.Println("Error: ", err)
		return
	}
	resp, err := authHttpTransport.RoundTrip(r)
	if err != nil {
		fmt.Println("RoundTrip Error: ", err)
		return
	}
	ioutil.ReadAll(resp.Body)
	fmt.Println(string(resp.Status))
}

//HTTPS transport via HTTP proxy
func doAuthenticatedHTTPS() {
	dialTlsFn := func(netw, addr string) (net.Conn, error) {
		config := &upstreamproxy.UpstreamProxyConfig{
			ForwardDialFunc: net.Dial,
			ProxyURIString:  "http://user:password@172.16.1.1:8080",
		}

		proxyDialFunc := upstreamproxy.NewProxyDialFunc(config)

		conn, err := proxyDialFunc(netw, addr)
		if err != nil {
			return nil, err
		}
		tlsconfig := &tls.Config{InsecureSkipVerify: false}
		tlsConn := tls.Client(conn, tlsconfig)

		return tlsConn, tlsConn.Handshake()
	}

	r, err := http.NewRequest("GET", "https://www.reddit.com", bytes.NewReader(data))
	transport = &http.Transport{DialTLS: dialTlsFn}
	resp, err := transport.RoundTrip(r)
	if err != nil {
		log.Println("RoundTrip Error: ", err)
		return
	}
	ioutil.ReadAll(resp.Body)
	fmt.Println(string(resp.Status))
}

//HTTP transport via SOCKS5 proxy
func doAuthenticatedHttpSocks() {
	dialFn := func(netw, addr string) (net.Conn, error) {
		config := &upstreamproxy.UpstreamProxyConfig{
			ForwardDialFunc: net.Dial,
			ProxyURIString:  "socks5://user:password@172.16.1.1:5555",
		}

		proxyDialFunc := upstreamproxy.NewProxyDialFunc(config)

		return proxyDialFunc(netw, addr)
	}

	r, err := http.NewRequest("GET", "https://www.reddit.com", bytes.NewReader(data))
	transport = &http.Transport{Dial: dialFn}
	resp, err := transport.RoundTrip(r)
	if err != nil {
		log.Println("RoundTrip Error: ", err)
		return
	}
	ioutil.ReadAll(resp.Body)
	fmt.Println(string(resp.Status))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/lqinggang/psiphon-tunnel-core.git
git@gitee.com:lqinggang/psiphon-tunnel-core.git
lqinggang
psiphon-tunnel-core
psiphon-tunnel-core
v2.0.0

搜索帮助