/
tunnel_connection.go
161 lines (134 loc) · 3.03 KB
/
tunnel_connection.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
package teaproxy
import (
"bufio"
"errors"
"github.com/TeaWeb/code/teaconfigs"
"github.com/TeaWeb/code/teautils"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/timers"
"net"
"net/http"
"net/http/httputil"
"strings"
"sync"
"time"
)
// 隧道连接
type TunnelConnection struct {
conn net.Conn
reader *bufio.Reader
locker *sync.Mutex
secret string
isAuthenticated bool
isClosed bool
closeHandler func(tunnelConn *TunnelConnection)
ticker *teautils.Ticker
}
// 获取新对象
func NewTunnelConnection(conn net.Conn, tunnelConfig *teaconfigs.TunnelConfig) *TunnelConnection {
tunnelConn := &TunnelConnection{
conn: conn,
reader: bufio.NewReader(conn),
locker: &sync.Mutex{},
isAuthenticated: true,
}
if len(tunnelConfig.Secret) > 0 {
// 认证
tunnelConn.secret = tunnelConfig.Secret
tunnelConn.isAuthenticated = false
timers.Delay(5*time.Second, func(timer *time.Timer) {
if !tunnelConn.isAuthenticated {
tunnelConn.Close()
}
})
// Ping
tunnelConn.ticker = teautils.Every(30*time.Second, func(ticker *teautils.Ticker) {
err := tunnelConn.Ping()
if err != nil {
tunnelConn.Close()
}
})
tunnelConn.auth()
}
return tunnelConn
}
// 判断是否已认证
func (this *TunnelConnection) IsAuthenticated() bool {
return this.isAuthenticated
}
// 发送请求
func (this *TunnelConnection) Write(req *http.Request) (*http.Response, error) {
if !this.isAuthenticated {
return nil, errors.New("[tunnel]not been authenticated")
}
if this.reader == nil {
return nil, errors.New("[tunnel]no tunnel reader")
}
this.locker.Lock()
data, err := httputil.DumpRequest(req, true)
_, err = this.conn.Write(data)
if err != nil {
this.locker.Unlock()
return nil, err
}
resp, err := http.ReadResponse(this.reader, req)
if err != nil {
this.locker.Unlock()
return resp, err
}
resp.Body = &TunnelResponseBody{
ReadCloser: resp.Body,
locker: this.locker,
}
return resp, nil
}
// 远程地址
func (this *TunnelConnection) RemoteAddr() string {
return this.conn.RemoteAddr().String()
}
// 设置关闭回调
func (this *TunnelConnection) OnClose(handler func(tunnelConn *TunnelConnection)) {
this.closeHandler = handler
}
// Ping客户端
func (this *TunnelConnection) Ping() error {
if this.isClosed || !this.isAuthenticated {
return nil
}
req, err := http.NewRequest(http.MethodGet, "/$$TEA/ping", nil)
if err != nil {
logs.Error(err)
return nil
}
resp, err := this.Write(req)
if err != nil {
return err
}
resp.Body.Close()
return nil
}
// 关闭
func (this *TunnelConnection) Close() error {
this.isClosed = true
err := this.conn.Close()
if this.closeHandler != nil {
this.closeHandler(this)
}
if this.ticker != nil {
this.ticker.Stop()
}
return err
}
// 认证
func (this *TunnelConnection) auth() {
data, _, err := this.reader.ReadLine()
if err != nil {
this.Close()
return
}
this.isAuthenticated = strings.TrimSpace(string(data)) == this.secret
if !this.isAuthenticated {
this.Close()
return
}
}