diff --git a/client.go b/client.go index d22da0a..a46db0f 100755 --- a/client.go +++ b/client.go @@ -80,8 +80,8 @@ func (sc *Client) handShake() error { return fmt.Errorf("password rejected") } -// Dial dial to the addr from socks server -// this is net.Dial style +// Dial dial to the addr from socks server, +// this is net.Dial style, // can call sc.Connect instead func (sc *Client) Dial(network, addr string) (net.Conn, error) { switch network { diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..2dec658 --- /dev/null +++ b/doc.go @@ -0,0 +1,101 @@ +/* +package socks implement a socks4/4a/5 protocol server and client + +only support CONNECT command now + +server example: + + // listen 1080 and act as socks server + // support socks4, socks4a and socks5 + + package main + + import ( + socks "github.com/fangdingjun/socks-go" + "log" + "net" + "time" + ) + + func main() { + conn, err := net.Listen("tcp", ":1080") + if err != nil { + log.Fatal(err) + } + + for { + c, err := conn.Accept() + if err != nil { + log.Println(err) + continue + } + + log.Printf("connected from %s", c.RemoteAddr()) + + d := net.Dialer{Timeout: 10 * time.Second} + s := socks.Conn{Conn: c, Dial: d.Dial} + go s.Serve() + } + } + + + + +client example: + + // visit https://www.google.com through socks proxy server + + package main + + import ( + "bufio" + "crypto/tls" + socks "github.com/fangdingjun/socks-go" + "io" + "log" + "net" + "net/http" + "os" + ) + + func main() { + // connect to socks server + c, err := net.Dial("tcp", "localhost:1080") + if err != nil { + log.Fatal(err) + } + defer c.Close() + + sc := &socks.Client{Conn: c} + + // connect to remote server + if err := sc.Connect("www.google.com", 443); err != nil { + log.Fatal(err) + } + + // tls + conn := tls.Client(sc, &tls.Config{ServerName: "www.google.com"}) + if err := conn.Handshake(); err != nil { + log.Fatal(err) + } + + // send http request + req, err := http.NewRequest("GET", "https://www.google.com/", nil) + if err != nil { + log.Fatal(err) + } + req.Write(conn) + + bio := bufio.NewReader(conn) + + // read response + res, err := http.ReadResponse(bio, req) + if err != nil { + log.Fatal(err) + } + defer res.Body.Close() + + io.Copy(os.Stdout, res.Body) + } +*/ +package socks diff --git a/socks.go b/socks.go index ffa89bc..edc7737 100755 --- a/socks.go +++ b/socks.go @@ -19,7 +19,7 @@ const ( // DialFunc the function dial to remote type DialFunc func(network, addr string) (net.Conn, error) -// Conn present a client connection +// Conn present a socks connection type Conn struct { net.Conn // the function to dial to upstream server