-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
conn.go
144 lines (118 loc) · 3.52 KB
/
conn.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
package main
import (
"errors"
"fmt"
"io"
"net"
"strconv"
"sync"
"time"
"github.com/fatih/color"
"github.com/juju/ratelimit"
"github.com/kevwan/tproxy/display"
"github.com/kevwan/tproxy/protocol"
)
const (
useOfClosedConn = "use of closed network connection"
statInterval = time.Second * 5
)
var (
errClientCanceled = errors.New("client canceled")
stat Stater
)
type PairedConnection struct {
id int
cliConn net.Conn
svrConn net.Conn
once sync.Once
stopChan chan struct{}
}
func NewPairedConnection(id int, cliConn net.Conn) *PairedConnection {
return &PairedConnection{
id: id,
cliConn: cliConn,
stopChan: make(chan struct{}),
}
}
func (c *PairedConnection) copyData(dst io.Writer, src io.Reader, tag string) {
_, e := io.Copy(dst, src)
if e != nil {
netOpError, ok := e.(*net.OpError)
if ok && netOpError.Err.Error() != useOfClosedConn {
reason := netOpError.Unwrap().Error()
display.PrintlnWithTime(color.HiRedString("[%d] %s error, %s", c.id, tag, reason))
}
}
}
func (c *PairedConnection) copyDataWithRateLimit(dst io.Writer, src io.Reader, tag string, limit int64) {
if limit > 0 {
bucket := ratelimit.NewBucket(time.Second, limit)
src = ratelimit.Reader(src, bucket)
}
c.copyData(dst, src, tag)
}
func (c *PairedConnection) handleClientMessage() {
// client closed also trigger server close.
defer c.stop()
r, w := io.Pipe()
tee := io.MultiWriter(c.svrConn, w)
go protocol.CreateInterop(settings.Protocol).Dump(r, protocol.ClientSide, c.id, settings.Quiet)
c.copyDataWithRateLimit(tee, c.cliConn, protocol.ClientSide, settings.UpLimit)
}
func (c *PairedConnection) handleServerMessage() {
// server closed also trigger client close.
defer c.stop()
r, w := io.Pipe()
tee := io.MultiWriter(newDelayedWriter(c.cliConn, settings.Delay, c.stopChan), w)
go protocol.CreateInterop(settings.Protocol).Dump(r, protocol.ServerSide, c.id, settings.Quiet)
c.copyDataWithRateLimit(tee, c.svrConn, protocol.ServerSide, settings.DownLimit)
}
func (c *PairedConnection) process() {
defer c.stop()
conn, err := net.Dial("tcp", settings.Remote)
if err != nil {
display.PrintlnWithTime(color.HiRedString("[x][%d] Couldn't connect to server: %v", c.id, err))
return
}
display.PrintlnWithTime(color.HiGreenString("[%d] Connected to server: %s", c.id, conn.RemoteAddr()))
stat.AddConn(strconv.Itoa(c.id), conn.(*net.TCPConn))
c.svrConn = conn
go c.handleServerMessage()
c.handleClientMessage()
}
func (c *PairedConnection) stop() {
c.once.Do(func() {
close(c.stopChan)
stat.DelConn(strconv.Itoa(c.id))
if c.cliConn != nil {
display.PrintlnWithTime(color.HiBlueString("[%d] Client connection closed", c.id))
c.cliConn.Close()
}
if c.svrConn != nil {
display.PrintlnWithTime(color.HiBlueString("[%d] Server connection closed", c.id))
c.svrConn.Close()
}
})
}
func startListener() error {
stat = NewStater(NewConnCounter(), NewStatPrinter(statInterval))
go stat.Start()
conn, err := net.Listen("tcp", fmt.Sprintf("%s:%d", settings.LocalHost, settings.LocalPort))
if err != nil {
return fmt.Errorf("failed to start listener: %w", err)
}
defer conn.Close()
display.PrintfWithTime("Listening on %s...\n", conn.Addr().String())
var connIndex int
for {
cliConn, err := conn.Accept()
if err != nil {
return fmt.Errorf("server: accept: %w", err)
}
connIndex++
display.PrintlnWithTime(color.HiGreenString("[%d] Accepted from: %s",
connIndex, cliConn.RemoteAddr()))
pconn := NewPairedConnection(connIndex, cliConn)
go pconn.process()
}
}