-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
174 lines (147 loc) · 3.46 KB
/
server.go
File metadata and controls
174 lines (147 loc) · 3.46 KB
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
package gobore
import (
"errors"
"fmt"
"net"
"sync"
"time"
"github.com/google/uuid"
"go.uber.org/zap"
)
type Server struct {
minPort uint16
auth *authenticator
dataConns map[string]net.Conn
guard sync.Mutex
availablePorts chan uint16
}
func NewServer(minPort uint16, secret string, ports []uint16) *Server {
s := &Server{minPort: minPort}
s.dataConns = map[string]net.Conn{}
if secret != "" {
s.auth = newAuthenticator(secret)
}
s.availablePorts = func() chan uint16 {
ch := make(chan uint16, len(ports))
for _, port := range ports {
ch <- port
}
return ch
}()
return s
}
func (s *Server) Listen() error {
l, err := net.Listen("tcp", fmt.Sprintf(":%d", controlPort))
if err != nil {
return err
}
for {
conn, err := l.Accept()
if err != nil {
return err
}
id := uuid.New().String()
go func() {
err = s.handleConn(conn)
if err != nil {
zap.L().Warn("connection exited with error", zap.String("id", id), zap.Error(err))
return
}
zap.L().Info("connection exited", zap.String("id", id))
}()
}
}
func (s *Server) handleConn(conn net.Conn) error {
if s.auth != nil {
err := s.auth.serverHandshake(conn)
if err != nil {
err = sendJson(conn, &serverMessage{Type: serverMessageError, Error: err.Error()})
if err != nil {
return err
}
}
}
cm := clientMessage{}
err := recvJsonWithTimeout(conn, &cm)
if err != nil {
return err
}
zap.L().Sugar().Debugf("Receive clientMessage: %#v", cm)
switch cm.Type {
case clientMessageAuthenticate:
return errors.New("unexpected authenticate")
case clientMessageAccept:
zap.L().Info("forward connection")
s.guard.Lock()
dataConn, ok := s.dataConns[cm.Accept]
s.guard.Unlock()
if !ok {
return fmt.Errorf("missing connection %s", cm.Accept)
}
proxy(conn, dataConn)
case clientMessageHello:
if cm.Hello != 0 && cm.Hello < s.minPort {
return fmt.Errorf("client port number %d too low", cm.Hello)
}
select {
case cm.Hello = <-s.availablePorts:
defer func() {
s.availablePorts <- cm.Hello
}()
default:
return fmt.Errorf("no available port in server")
}
zap.L().Info("new client", zap.Uint16("port", cm.Hello))
l, err := net.Listen("tcp", fmt.Sprintf(":%d", cm.Hello))
if err != nil {
return err
}
defer func() {
zap.L().Info("close listener", zap.String("addr", l.Addr().String()))
_ = sendJson(conn, serverMessage{
Type: serverMessageError,
Error: fmt.Sprintf("Closed connection %d", l.Addr().(*net.TCPAddr).Port),
})
_ = l.Close()
}()
err = sendJson(conn, serverMessage{Type: serverMessageHello, Hello: uint16(l.Addr().(*net.TCPAddr).Port)})
if err != nil {
zap.L().Error("Failed to sendJson", zap.Error(err))
return err
}
var count int
go func() {
for {
dataConn, err := l.Accept()
if err != nil {
return
}
zap.L().Info("new connection", zap.String("addr", dataConn.RemoteAddr().String()))
count = 0
id := uuid.New().String()
s.guard.Lock()
s.dataConns[id] = dataConn
s.guard.Unlock()
err = sendJson(conn, serverMessage{Type: serverMessageConnection, Connection: id})
if err != nil {
zap.L().Error("Failed to sendJson", zap.String("id", id))
}
go func() {
time.Sleep(10 * time.Second)
s.guard.Lock()
delete(s.dataConns, id)
s.guard.Unlock()
}()
}
}()
t := time.NewTicker(30 * time.Minute)
defer t.Stop()
for range t.C {
count++
if count >= 1 {
return nil
}
}
}
return nil
}