This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tcp_tunnel.go
176 lines (149 loc) · 3.13 KB
/
tcp_tunnel.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
package secretun
import (
"bytes"
"crypto/tls"
"encoding/binary"
"io"
"log"
"net"
)
type RawTCP_ST struct {
conn net.Listener
}
type RawTCP_CT struct {
conn net.Conn
}
func packetTunnel(conn net.Conn, cli_ch ClientChan) {
go func() {
var size uint16
var header [2]byte
for {
if _, err := io.ReadFull(conn, header[:]); err != nil {
cli_ch.End <- err
return
}
size = binary.BigEndian.Uint16(header[:])
data := make([]byte, size)
if _, err := io.ReadFull(conn, data); err != nil {
cli_ch.End <- err
return
}
if packet, err := DeserializePacket(data); err != nil {
cli_ch.End <- err
return
} else {
cli_ch.R <- packet
}
}
}()
go func() {
for {
packet, ok := <-cli_ch.W
if !ok {
return
}
data, err := packet.Serialize()
if err != nil {
cli_ch.End <- err
return
}
size := len(data)
buf := make([]byte, 0, 2+len(data))
w := bytes.NewBuffer(buf)
w.WriteByte(byte(size >> 8))
w.WriteByte(byte(size & 0xFF))
w.Write(data)
if _, err = conn.Write(w.Bytes()); err != nil {
cli_ch.End <- err
return
}
}
}()
}
func (t *RawTCP_ST) Init(cfg Config) (err error) {
var certFile, keyFile, addr string
var cert tls.Certificate
if err = cfg.Get("addr", &addr); err != nil {
return
}
if cfg.GetBool("tls") {
if err := cfg.Get("cert", &certFile); err != nil {
return err
} else if err := cfg.Get("key", &keyFile); err != nil {
return err
} else if cert, err = tls.LoadX509KeyPair(certFile, keyFile); err != nil {
return err
}
tls_cfg := &tls.Config{}
tls_cfg.NextProtos = []string{"http/1.1"}
tls_cfg.Certificates = []tls.Certificate{cert}
if l, err := net.Listen("tcp", addr); err != nil {
return err
} else {
t.conn = tls.NewListener(l, tls_cfg)
}
} else {
t.conn, err = net.Listen("tcp", addr)
}
log.Println("listen on ", addr)
return
}
func (t *RawTCP_ST) Accept() (cli_ch ClientChan, err error) {
var conn net.Conn
conn, err = t.conn.Accept()
if err != nil {
return
}
if tcp_conn, ok := conn.(*net.TCPConn); ok {
err = tcp_conn.SetNoDelay(true)
if err != nil {
return
}
err = tcp_conn.SetKeepAlive(true)
if err != nil {
return
}
}
cli_ch = NewClientChan()
packetTunnel(conn, cli_ch)
return cli_ch, nil
}
func (t *RawTCP_ST) Shutdown() error {
return nil
}
func (t *RawTCP_CT) Init(cfg Config) (err error) {
var addr string
if err = cfg.Get("addr", &addr); err != nil {
return
}
log.Println("connect to ", addr)
if cfg.GetBool("tls") {
tls_cfg := &tls.Config{}
tls_cfg.InsecureSkipVerify = true
if t.conn, err = tls.Dial("tcp", addr, tls_cfg); err != nil {
return
}
} else {
if t.conn, err = net.Dial("tcp", addr); err != nil {
return
}
}
if tcp_conn, ok := t.conn.(*net.TCPConn); ok {
if err = tcp_conn.SetNoDelay(true); err != nil {
return
}
err = tcp_conn.SetKeepAlive(true)
}
return
}
func (t *RawTCP_CT) Start(cli_ch ClientChan) error {
packetTunnel(t.conn, cli_ch)
return nil
}
func (t *RawTCP_CT) Shutdown() error {
return nil
}
func init() {
RegisterClientTunnel("tcp", RawTCP_CT{})
RegisterServerTunnel("tcp", RawTCP_ST{})
}