Skip to content

Instantly share code, notes, and snippets.

@tianweidut
Created February 9, 2020 11:58
Show Gist options
  • Save tianweidut/f72d522584f239db6c45384cafde23a1 to your computer and use it in GitHub Desktop.
Save tianweidut/f72d522584f239db6c45384cafde23a1 to your computer and use it in GitHub Desktop.
golang proxy with unix domain socket
package main
import (
"crypto/tls"
"fmt"
"golang.org/x/net/proxy"
"io/ioutil"
"net"
"net/http"
"os"
"time"
)
const targetURL = "http://x.y.z/a/b/c"
func GetHttpClient() *http.Client{
return &http.Client{
Timeout: 90 * time.Second,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 60 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 180 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
func showOutput(resp *http.Response, err error){
if err != nil{
fmt.Printf("error: %q", err)
}else{
defer resp.Body.Close()
content, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("content: %s", string(content))
}
}
func socksProxyHttpClient(network, address string) *http.Client{
dialer, err := proxy.SOCKS5(network, address, nil, proxy.Direct)
if err != nil{
fmt.Printf("failed to connect to")
os.Exit(1)
}
ht := &http.Transport{}
client := &http.Client{Transport: ht}
ht.Dial = dialer.Dial
return client
}
func main(){
fmt.Println("-----> 1.direct request")
showOutput(GetHttpClient().Get(targetURL))
fmt.Println("\n-----> 2.socks5 proxy with ip+port")
showOutput(socksProxyHttpClient("tcp", "localhost:17777").Get(targetURL))
fmt.Println("\n-----> 3.socks5 proxy with unix domain socket")
showOutput(socksProxyHttpClient("unix", "./dynamic_port.proxy").Get(targetURL))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment