forked from txthinking/socks5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
73 lines (68 loc) · 1.35 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package socks5_test
import (
"encoding/hex"
"io/ioutil"
"log"
"net"
"net/http"
"github.com/leeshung/socks5"
)
func ExampleServer() {
s, err := socks5.NewClassicServer("127.0.0.1:1080", "127.0.0.1", "", "", 0, 60)
if err != nil {
panic(err)
}
// You can pass in custom Handler
s.ListenAndServe(nil)
// #Output:
}
func ExampleClient_tcp() {
c, err := socks5.NewClient("127.0.0.1:1080", "", "", 0, 60)
if err != nil {
panic(err)
}
client := &http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
return c.Dial(network, addr)
},
},
}
res, err := client.Get("https://ifconfig.co")
if err != nil {
panic(err)
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
log.Println(string(b))
// Output:
}
func ExampleClient_udp() {
c, err := socks5.NewClient("127.0.0.1:1080", "", "", 0, 60)
if err != nil {
panic(err)
}
conn, err := c.Dial("udp", "180.76.76.76:53")
if err != nil {
panic(err)
}
b, err := hex.DecodeString("0001010000010000000000000a74787468696e6b696e6703636f6d0000010001")
if err != nil {
panic(err)
}
if _, err := conn.Write(b); err != nil {
panic(err)
}
b = make([]byte, 2048)
n, err := conn.Read(b)
if err != nil {
panic(err)
}
b = b[:n]
b = b[len(b)-4:]
log.Println(net.IPv4(b[0], b[1], b[2], b[3]))
// Output:
}