-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
181 lines (164 loc) · 4.02 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"flag"
"fmt"
socks5 "github.com/armon/go-socks5"
"github.com/hashicorp/yamux"
"io"
"net"
"log"
"math/rand"
"time"
)
var clientServerConns []*yamux.Session
var encKey [32]byte
func listenClientServerConn(port string){
log.Println("Listening for the ClientServer")
ln, err := net.Listen("tcp", "0.0.0.0:"+port)
if err != nil {
panic(err)
}
for{
conn, err := ln.Accept()
if err != nil {
log.Println("ClientServer connect fail")
}
buf := make([]byte, 32)
conn.Read(buf)
if string(buf)==string(encKey[:]) {
session, err := yamux.Client(conn, nil)
if err != nil {
log.Println("ClientServer session create failed")
}
clientServerConns = append(clientServerConns, session)
}
}
}
func listenClientClientConn(port string){
log.Println("Listening for the ClientClient")
ln, err := net.Listen("tcp", "0.0.0.0:"+port)
if err != nil {
panic(err)
}
for{
conn, err := ln.Accept()
if err != nil {
log.Println("ClientClient connect fail")
}
if len(clientServerConns)>0 {
//流量转发到客户端服务器
clientServerConn:=getStream()
forward :=func(src,dest net.Conn){
defer src.Close()
defer dest.Close()
io.Copy(dest,src)
}
go forward(clientServerConn,conn)
go forward(conn,clientServerConn)
}else{
log.Print("there is no client server...")
time.Sleep(5 * time.Second)
}
}
}
func clientServerServeSocks(remoteAddress,username,password string){
//设置sock5账号密码
cred := socks5.StaticCredentials{
username: password,
}
cator := socks5.UserPassAuthenticator{Credentials: cred}
conf := &socks5.Config{AuthMethods:[]socks5.Authenticator{cator}}
server, err := socks5.New(conf)
if err != nil {
panic(err)
}
log.Println("Connecting to server...")
for {
conn := func(remoteAddress string) (conn net.Conn) {
for {
conn, err := net.Dial("tcp", remoteAddress)
if err != nil {
log.Print("trying to reconnect to server...")
time.Sleep(5 * time.Second)
continue
}
conn.Write(encKey[:])
return conn
}
}(remoteAddress)
session, err := yamux.Server(conn, nil)
if err != nil {
continue
}
for {
stream, err := session.Accept()
if err != nil {
break
}
go func() {
err = server.ServeConn(stream)
if err != nil {
log.Println(err)
}
}()
}
}
}
func getStream() net.Conn{
for {
clientServerConnsLen:=len(clientServerConns)
if clientServerConnsLen>0 {
num := rand.Intn(clientServerConnsLen)
clientServerStream, err := clientServerConns[num].Open()
//去除不存活的连接
if err != nil {
switch num{
case 0:
clientServerConns=clientServerConns[1:]
case clientServerConnsLen-1:
clientServerConns=clientServerConns[:clientServerConnsLen-1]
default:
clientServerConns=append(clientServerConns[:num-1],clientServerConns[num+1:]...)
}
continue
}
return clientServerStream
}else{
log.Print("there is no client server...")
time.Sleep(5 * time.Second)
continue
}
}
}
func main(){
mode := flag.String("m", "", "client or server")
remoteAddress := flag.String("r", "", "remote server address 1.1.1.1:1234")
port := flag.String("p", "", "server's socks5 port")
clientConnectPort:=flag.String("ccp", "", "client connected port")
username:=flag.String("u", "", "socks5 username")
password:=flag.String("pwd", "", "socks5 password")
key:=flag.String("key", "", "connect key")
flag.Usage = func() {
fmt.Println("yipmanproxy -m server -p port1 -ccp port2 -key xxxx(max len 32)")
fmt.Println("yipmanproxy -m client -r x.x.x.x:port2 -u foo -pwd bar -key xxxx(max len 32)")
}
flag.Parse()
if *mode=="server"{
if *port!="" && *clientConnectPort!=""{
copy(encKey[:],*key)
go listenClientServerConn(*clientConnectPort)
listenClientClientConn(*port)
}else{
flag.Usage()
}
}else if *mode=="client"{
if *remoteAddress!="" && *username!="" && *password !=""{
copy(encKey[:],*key)
clientServerServeSocks(*remoteAddress,*username,*password)
}else{
flag.Usage()
}
}else {
flag.Usage()
}
}