-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathproxy_client.go
98 lines (91 loc) · 1.82 KB
/
proxy_client.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Created by lock
* Date: 2019-10-28
* Time: 11:30
*/
package main
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"net"
"os"
)
type ProxyClient struct {
A, B, C, D byte
Port uint16
ListenTarget string
}
func (pc *ProxyClient) toAddr() string {
return fmt.Sprintf("%d.%d.%d.%d:%d", pc.A, pc.B, pc.C, pc.D, pc.Port)
}
func (pc *ProxyClient) handleClientRequest(client net.Conn) {
if client == nil {
return
}
defer client.Close()
var b [1024]byte
n, err := client.Read(b[:])
if err != nil {
return
}
//deal socks5 protocol
if b[0] == 0x05 {
client.Write([]byte{0x05, 0x00})
n, err = client.Read(b[:])
var addr string
switch b[3] {
case 0x01:
if err := binary.Read(bytes.NewReader(b[4:n]), binary.BigEndian, &pc); err != nil {
fmt.Println("request parse error")
return
}
addr = pc.toAddr()
case 0x03:
host := string(b[5 : n-2])
var port uint16
err = binary.Read(bytes.NewReader(b[n-2:n]), binary.BigEndian, &port)
if err != nil {
fmt.Println(err)
return
}
addr = fmt.Sprintf("%s:%d", host, port)
}
server, err := net.Dial("tcp", addr)
if err != nil {
return
}
defer server.Close()
//response client success
client.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
//stream forward
go io.Copy(server, client)
io.Copy(client, server)
}
}
func (pc *ProxyClient) handle() string {
var RemoteConn net.Conn
var err error
fmt.Println(fmt.Sprintf("start run at --> %s", pc.ListenTarget))
for {
for {
RemoteConn, err = net.Dial("tcp", pc.ListenTarget)
if err == nil {
break
}
}
go pc.handleClientRequest(RemoteConn)
}
}
func main() {
args := os.Args
if len(args) != 2 {
fmt.Println("args error! please input ip:port")
os.Exit(0)
}
pc := &ProxyClient{
ListenTarget: args[1],
}
pc.handle()
}