You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.2 KiB
Markdown

9 years ago
socks-go
=======
8 years ago
A socks proxy protocol implemented by golang, support socks 4, 4a and 5.
9 years ago
8 years ago
Only CONNECT command support now.
9 years ago
usage example
=============
9 years ago
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)
}
9 years ago
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:
// 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)
9 years ago
}
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)
9 years ago
}