-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.go
More file actions
45 lines (41 loc) · 840 Bytes
/
server.go
File metadata and controls
45 lines (41 loc) · 840 Bytes
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
package proxy
import (
"github.com/google/uuid"
"net"
"strings"
)
func ListenAndServe(addr string, cfg *Config) error {
inner, err := net.Listen("tcp", addr)
if err != nil {
return err
}
return Serve(inner, cfg)
}
func Serve(ln net.Listener, cfg *Config) error {
return NewListener(ln, cfg).Serve()
}
func (ln *Listener) Serve() error {
defer ln.Close()
for {
id := uuid.New().String()
id = strings.ReplaceAll(id, "-", "")
ctx := NewContext(ctxLogger, id[:16], ln.cfg)
inner, err := ln.Accept()
if err != nil {
ctx.Error(err)
continue
}
ctx.Conn = NewConn(inner)
go func() {
defer ctx.Conn.Close()
if ctx.Negotiator != nil { //代理协议握手
err = ctx.Negotiator.Handshake(ctx)
if err != nil {
ctx.Error(err)
return
}
}
_ = ctx.Dispatcher.Dispatch(ctx)
}()
}
}