-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.go
156 lines (143 loc) · 3.17 KB
/
server.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
package main
import (
"context"
"errors"
"fmt"
"net"
"sync"
"time"
"snet/config"
"snet/proxy"
"snet/redirector"
"snet/sniffer"
"snet/stats"
"snet/utils"
)
const (
SO_ORIGINAL_DST = 80 // /usr/includ/linux/netfilter_ipv4.h
)
type HostBytesMap struct {
sync.RWMutex
m map[string]uint64
}
type Server struct {
ctx context.Context
cfg *config.Config
listener *net.TCPListener
proxy proxy.Proxy
timeout time.Duration
// Total number from start
HostRxBytesTotal *HostBytesMap
HostTxBytesTotal *HostBytesMap
rxCh chan *stats.P
txCh chan *stats.P
}
func NewServer(ctx context.Context, c *config.Config) (*Server, error) {
addr := fmt.Sprintf("%s:%d", c.LHost, c.LPort)
ln, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
p, err := proxy.Get(c.ProxyType)
if err != nil {
return nil, err
}
cfg, err := genConfigByType(c, c.ProxyType)
if err != nil {
return nil, err
}
if err := p.Init(cfg); err != nil {
return nil, err
}
var rxCh, txCh chan *stats.P
if c.EnableStats {
rxCh = make(chan *stats.P, 1)
txCh = make(chan *stats.P, 1)
}
return &Server{
ctx: ctx,
cfg: c,
listener: ln.(*net.TCPListener),
proxy: p,
timeout: time.Duration(c.ProxyTimeout) * time.Second,
HostRxBytesTotal: &HostBytesMap{m: make(map[string]uint64)},
HostTxBytesTotal: &HostBytesMap{m: make(map[string]uint64)},
rxCh: rxCh,
txCh: txCh,
}, nil
}
func (s *Server) Run() error {
l.Infof("Proxy server listen on tcp %s:%d", s.cfg.LHost, s.cfg.LPort)
if s.cfg.EnableStats {
go s.receiveStat()
}
for {
conn, err := s.listener.AcceptTCP()
if err != nil {
return err
}
go func(conn *net.TCPConn) {
if err := s.handle(conn); err != nil {
l.Error(err)
}
}(conn)
}
}
func (s *Server) receiveStat() {
for {
select {
case p := <-s.rxCh:
s.HostRxBytesTotal.Lock()
if _, ok := s.HostRxBytesTotal.m[p.Host]; ok {
s.HostRxBytesTotal.m[p.Host] += p.Rx
} else {
s.HostRxBytesTotal.m[p.Host] = p.Rx
}
s.HostRxBytesTotal.Unlock()
case p := <-s.txCh:
s.HostTxBytesTotal.Lock()
if _, ok := s.HostTxBytesTotal.m[p.Host]; ok {
s.HostTxBytesTotal.m[p.Host] += p.Tx
} else {
s.HostTxBytesTotal.m[p.Host] = p.Tx
}
s.HostTxBytesTotal.Unlock()
case <-s.ctx.Done():
return
}
}
}
func (s *Server) handle(conn *net.TCPConn) error {
defer conn.Close()
dstHost, dstPort, err := redirector.GetDstAddr(conn)
if err != nil {
return err
}
if dstHost == "127.0.0.1" {
return errors.New("drop connection to localhost")
}
remoteConn, err := s.proxy.Dial(dstHost, dstPort)
if err != nil {
return err
}
defer remoteConn.Close()
var sn *sniffer.Sniffer
if s.cfg.EnableStats {
sn = sniffer.NewSniffer(s.cfg.StatsEnableTLSSNISniffer, s.cfg.StatsEnableHTTPHostSniffer)
}
if err := utils.Pipe(s.ctx, conn, remoteConn, s.timeout, s.rxCh, s.txCh, dstHost, dstPort, sn); err != nil {
l.Error(err)
}
return nil
}
func (s *Server) Shutdown() error {
err := s.listener.Close()
if err != nil {
return err
}
l.Info("redirector tcp server shutdown")
return nil
}