diff --git a/README.md b/README.md index 21329bf..af9ec32 100755 --- a/README.md +++ b/README.md @@ -5,17 +5,100 @@ A socks server implemented by golang, support socks4/4a, socks5. Only support connect command now. -usage -==== -Usage example: +usage example +============= - import socks "github.com/fangdingjun/socks-go" +server: + + // 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) + } - func main(){ - l, _ := net.Listen("tcp", ":1080") for { - conn, _ := l.Accept() - s := socks.SocksConn{ClientConn: conn, Dial: nil} // Dial is a function which dial to the upstream server - go s.Serve() // serve the socks request + 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: + + // 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) } diff --git a/examples/client/README.md b/examples/client/README.md new file mode 100755 index 0000000..77f1a0f --- /dev/null +++ b/examples/client/README.md @@ -0,0 +1,12 @@ + +this is a socks client example + +usage + +run server first, command + + go run $GOPATH/src/github.com/fandingjun/socks-go/examples/server/server.go + +then + + go run client.go diff --git a/examples/client/client.go b/examples/client/client.go new file mode 100755 index 0000000..78fb1fb --- /dev/null +++ b/examples/client/client.go @@ -0,0 +1,52 @@ +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) +} diff --git a/examples/server/server.go b/examples/server/server.go index 527f3c8..a28089d 100755 --- a/examples/server/server.go +++ b/examples/server/server.go @@ -12,15 +12,18 @@ func main() { 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.SocksConn{ClientConn: c, Dial: d.Dial} + s := socks.Conn{Conn: c, Dial: d.Dial} go s.Serve() } }