-
Notifications
You must be signed in to change notification settings - Fork 68
/
server.go
94 lines (79 loc) · 1.94 KB
/
server.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
package socks5proxy
import (
"log"
"net"
"sync"
)
func handleClientRequest(client *net.TCPConn, auth socks5Auth) {
if client == nil {
return
}
defer client.Close()
// 初始化一个字符串buff
buff := make([]byte, 255)
// 认证协商
var proto ProtocolVersion
n, err := auth.DecodeRead(client, buff) //解密
resp, err := proto.HandleHandshake(buff[0:n])
auth.EncodeWrite(client, resp) //加密
if err != nil {
log.Print(client.RemoteAddr(), err)
return
}
//获取客户端代理的请求
var request Socks5Resolution
n, err = auth.DecodeRead(client, buff)
resp, err = request.LSTRequest(buff[0:n])
auth.EncodeWrite(client, resp)
if err != nil {
log.Print(client.RemoteAddr(), err)
return
}
log.Println(client.RemoteAddr(), request.DSTDOMAIN, request.DSTADDR, request.DSTPORT)
// 连接真正的远程服务
dstServer, err := net.DialTCP("tcp", nil, request.RAWADDR)
if err != nil {
log.Print(client.RemoteAddr(), err)
return
}
defer dstServer.Close()
wg := new(sync.WaitGroup)
wg.Add(2)
// 本地的内容copy到远程端
go func() {
defer wg.Done()
SecureCopy(client, dstServer, auth.Decrypt)
}()
// 远程得到的内容copy到源地址
go func() {
defer wg.Done()
SecureCopy(dstServer, client, auth.Encrypt)
}()
wg.Wait()
}
func Server(listenAddrString string, encrytype string, passwd string) {
//所有客户服务端的流都加密,
auth, err := CreateAuth(encrytype, passwd)
if err != nil {
log.Fatal(err)
}
log.Printf("你的密码是:%s ,请保管好你的密码", passwd)
// 监听客户端
listenAddr, err := net.ResolveTCPAddr("tcp", listenAddrString)
if err != nil {
log.Fatal(err)
}
log.Printf("监听服务器端口: %s ", listenAddrString)
listener, err := net.ListenTCP("tcp", listenAddr)
if err != nil {
log.Fatal(err)
}
defer listener.Close()
for {
conn, err := listener.AcceptTCP()
if err != nil {
log.Fatal(err)
}
go handleClientRequest(conn, auth)
}
}