-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.go
91 lines (79 loc) · 1.76 KB
/
connect.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
package relay
import (
"context"
"errors"
"fmt"
"net"
"time"
"github.com/go-gost/core/logger"
"github.com/go-gost/relay"
netpkg "github.com/hxdcloud/gost-x/internal/net"
)
func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network, address string, log logger.Logger) error {
log = log.WithFields(map[string]any{
"dst": fmt.Sprintf("%s/%s", address, network),
"cmd": "connect",
})
log.Infof("%s >> %s", conn.RemoteAddr(), address)
resp := relay.Response{
Version: relay.Version1,
Status: relay.StatusOK,
}
if address == "" {
resp.Status = relay.StatusBadRequest
resp.WriteTo(conn)
err := errors.New("target not specified")
log.Error(err)
return err
}
if h.options.Bypass != nil && h.options.Bypass.Contains(address) {
log.Info("bypass: ", address)
resp.Status = relay.StatusForbidden
_, err := resp.WriteTo(conn)
return err
}
cc, err := h.router.Dial(ctx, network, address)
if err != nil {
resp.Status = relay.StatusNetworkUnreachable
resp.WriteTo(conn)
return err
}
defer cc.Close()
if h.md.noDelay {
if _, err := resp.WriteTo(conn); err != nil {
log.Error(err)
return err
}
}
switch network {
case "udp", "udp4", "udp6":
rc := &udpConn{
Conn: conn,
}
if !h.md.noDelay {
// cache the header
if _, err := resp.WriteTo(&rc.wbuf); err != nil {
return err
}
}
conn = rc
default:
if !h.md.noDelay {
rc := &tcpConn{
Conn: conn,
}
// cache the header
if _, err := resp.WriteTo(&rc.wbuf); err != nil {
return err
}
conn = rc
}
}
t := time.Now()
log.Infof("%s <-> %s", conn.RemoteAddr(), address)
netpkg.Transport(conn, cc)
log.WithFields(map[string]any{
"duration": time.Since(t),
}).Infof("%s >-< %s", conn.RemoteAddr(), address)
return nil
}