/
psl.go
179 lines (158 loc) · 3.94 KB
/
psl.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
package tlcp
import (
"crypto/tls"
"errors"
"fmt"
"io"
"net"
"sync"
)
type ProtocolDetectConn struct {
net.Conn
major, minor uint8
recordHeader []byte
}
func (c *ProtocolDetectConn) protocolVersion() (major uint8, minor uint8) {
return c.major, c.minor
}
func (c *ProtocolDetectConn) Raw() net.Conn {
return c.Conn
}
func (c *ProtocolDetectConn) ReadFirstHeader() error {
c.recordHeader = make([]byte, 5)
_, err := io.ReadFull(c.Conn, c.recordHeader)
c.major, c.minor = c.recordHeader[1], c.recordHeader[2]
return err
}
func (c *ProtocolDetectConn) Read(b []byte) (n int, err error) {
if len(c.recordHeader) == 0 {
return c.Conn.Read(b)
}
if len(b) >= len(c.recordHeader) {
n = copy(b, c.recordHeader)
c.recordHeader = nil
if len(b) > n {
var n1 = 0
n1, err = c.Conn.Read(b[n:])
n += n1
if err != nil {
return n, err
}
}
return n, nil
} else {
p := c.recordHeader[:len(b)]
n = len(b)
copy(b, p)
c.recordHeader = c.recordHeader[len(b):]
if len(c.recordHeader) == 0 {
c.recordHeader = nil
}
return n, nil
}
}
type ProtocolNotSupportError struct{}
func (ProtocolNotSupportError) Error() string { return "tlcp: unknown protocol version" }
func (ProtocolNotSupportError) Timeout() bool { return false }
func (ProtocolNotSupportError) Temporary() bool { return false }
var notSupportError = &ProtocolNotSupportError{}
type protocolSwitcherListener struct {
net.Listener
tlcpConfig *Config
tlsConfig *tls.Config
}
func (l *protocolSwitcherListener) Accept() (net.Conn, error) {
rawConn, err := l.Listener.Accept()
if err != nil {
return nil, err
}
return newProtocolSwitchServerConn(l, rawConn), nil
}
func NewProtocolSwitcherListener(inner net.Listener, config *Config, tlsConfig *tls.Config) net.Listener {
if inner == nil || (config == nil && tlsConfig == nil) {
return nil
}
l := new(protocolSwitcherListener)
l.Listener = inner
l.tlcpConfig = config
l.tlsConfig = tlsConfig
return l
}
func ListenWithAutoProtocolSwitcher(network, addr string, config *Config, tlsConfig *tls.Config) (net.Listener, error) {
if config == nil && tlsConfig == nil {
return nil, errors.New("tlcp: neither tlcp config, tls config is nil")
}
if config == nil || len(config.Certificates) == 0 &&
config.GetCertificate == nil && config.GetConfigForClient == nil {
return nil, errors.New("tlcp: neither Certificates, GetCertificate, nor GetConfigForClient set in Config")
}
l, err := net.Listen(network, addr)
if err != nil {
return nil, err
}
return NewProtocolSwitcherListener(l, config, tlsConfig), nil
}
type ProtocolSwitchServerConn struct {
net.Conn
lock *sync.Mutex
p *ProtocolDetectConn
ln *protocolSwitcherListener
wrapped net.Conn
}
func newProtocolSwitchServerConn(ln *protocolSwitcherListener, rawConn net.Conn) *ProtocolSwitchServerConn {
p := &ProtocolDetectConn{Conn: rawConn}
return &ProtocolSwitchServerConn{
Conn: rawConn,
ln: ln,
p: p,
lock: new(sync.Mutex),
wrapped: nil,
}
}
func (c *ProtocolSwitchServerConn) detect() error {
c.lock.Lock()
defer c.lock.Unlock()
if c.wrapped != nil {
return nil
}
err := c.p.ReadFirstHeader()
if err != nil {
return err
}
switch c.p.major {
case 0x01:
if c.ln.tlcpConfig == nil {
return fmt.Errorf("tlcp: tlcp config not set")
}
c.wrapped = Server(c.p, c.ln.tlcpConfig)
case 0x03:
if c.ln.tlsConfig == nil {
return fmt.Errorf("tlcp: tls config not set")
}
c.wrapped = tls.Server(c.p, c.ln.tlsConfig)
default:
return notSupportError
}
return nil
}
func (c *ProtocolSwitchServerConn) ProtectedConn() net.Conn {
return c.wrapped
}
func (c *ProtocolSwitchServerConn) Read(b []byte) (n int, err error) {
if c.wrapped == nil {
err = c.detect()
if err != nil {
return 0, err
}
}
return c.wrapped.Read(b)
}
func (c *ProtocolSwitchServerConn) Write(b []byte) (n int, err error) {
if c.wrapped == nil {
err = c.detect()
if err != nil {
return 0, err
}
}
return c.wrapped.Write(b)
}