-
Notifications
You must be signed in to change notification settings - Fork 1
/
relay.go
96 lines (84 loc) · 1.5 KB
/
relay.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
package main
import (
"crypto/cipher"
"crypto/rc4"
"flag"
"io"
"net"
"sync"
)
var (
laddr = flag.String("l", "0.0.0.0:8081", "local address")
raddr = flag.String("r", "127.0.0.1:8080", "remote address")
inwall = flag.Bool("w", true, "inside GFW")
)
func Start() {
ln, err := net.Listen("tcp", *laddr)
if err != nil {
return
}
for {
conn, err := ln.Accept()
if err != nil {
continue
}
go handleConn(conn)
}
}
func Pipe(src io.ReadWriteCloser, dst io.ReadWriteCloser) (int64, int64) {
var sent, received int64
var c = make(chan struct{})
var o sync.Once
close := func() {
src.Close()
dst.Close()
close(c)
}
go func() {
received, _ = io.Copy(src, dst)
o.Do(close)
}()
go func() {
sent, _ = io.Copy(dst, src)
o.Do(close)
}()
<-c
return sent, received
}
type RC4Cipher struct {
*cipher.StreamReader
*cipher.StreamWriter
}
func NewRC4Cipher(rwc io.ReadWriteCloser) (*RC4Cipher, error) {
decryptCipher, _ := rc4.NewCipher([]byte("gfw"))
encryptCipher, _ := rc4.NewCipher([]byte("gfw"))
return &RC4Cipher{
StreamReader: &cipher.StreamReader{
S: decryptCipher,
R: rwc,
},
StreamWriter: &cipher.StreamWriter{
S: encryptCipher,
W: rwc,
},
}, nil
}
func handleConn(c net.Conn) {
var local, remote io.ReadWriteCloser
rc, err := net.Dial("tcp", *raddr)
if err != nil {
return
}
if *inwall {
local = c
remote, _ = NewRC4Cipher(rc)
} else {
local, _ = NewRC4Cipher(c)
remote = rc
}
Pipe(local, remote)
}
func main() {
flag.Parse()
Start()
}