-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
167 lines (149 loc) · 3.68 KB
/
app.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
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"context"
"fmt"
"golang.org/x/sync/errgroup"
"io"
"log/slog"
"net"
"os"
"time"
)
type App struct {
config Config
}
func (app App) Error(format string, args ...any) {
if !app.config.Quiet {
fmt.Fprintf(os.Stderr, format+"\n", args...)
}
}
func (app App) LogInfo(format string, args ...any) {
if !app.config.Quiet {
slog.Info(format, args...)
}
}
func (app App) LogDebug(format string, args ...any) {
if !app.config.Quiet {
slog.Debug(format, args...)
}
}
func (app App) LogError(format string, args ...any) {
if !app.config.Quiet {
slog.Error(format, args...)
}
}
func (app App) Run() error {
ctx := context.WithValue(context.Background(), "app", &app)
stats := Stats{}
go stats.RunServer(ctx)
if len(app.config.Bind) > 1 {
var g *errgroup.Group
g, ctx = errgroup.WithContext(ctx)
for _, addr := range app.config.Bind {
g.Go(func() error {
return app.Listen(ctx, addr, stats)
})
}
return g.Wait()
}
return app.Listen(ctx, app.config.Bind[0], stats)
}
func (app App) Listen(ctx context.Context, addr string, stats Stats) error {
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
defer ln.Close()
app.LogInfo("listening", "addr", addr)
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
var c net.Conn
c, err = ln.Accept()
if err != nil {
app.LogError("accepting", "reason", err.Error(), "addr", ln.Addr())
continue
}
key := c.RemoteAddr().String()
if _, ok := stats[key]; ok {
_ = c.Close()
app.LogInfo("rejected", "reason", "already connected", "raddr", c.RemoteAddr())
continue
}
info := &ConnInfo{}
stats[key] = info
go func() {
defer func() {
_ = c.Close()
delete(stats, key)
}()
app.HandleConnection(c, info)
}()
}
}
func (app App) HandleConnection(localConn net.Conn, info *ConnInfo) {
startTime := time.Now()
info.StartTime = startTime
laddr := localConn.LocalAddr()
raddr := localConn.RemoteAddr()
app.LogInfo("accepted", "laddr", laddr, "raddr", raddr)
defer func() {
duration := time.Now().Sub(startTime) - time.Second
app.LogInfo("closed",
"laddr", laddr,
"raddr", raddr,
"lifetime", int(duration.Seconds()),
"reversed", info.IsReversed)
}()
for _, port := range app.config.RemotePorts {
if err := app.ConnectRemote(localConn, port, info); err != nil {
app.LogDebug("error", "reason", err.Error())
} else {
return
}
}
if slog.Default().Enabled(context.Background(), slog.LevelDebug) {
buf := make([]byte, 1024)
if n, err := localConn.Read(buf); err != nil {
if err != io.EOF {
app.LogError("reading", "reason", err, "laddr", laddr, "raddr", raddr)
}
return
} else {
app.LogDebug("received", "laddr", laddr, "raddr", raddr, "payload", string(buf[:n]))
}
}
payload := make([]byte, 1)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
<-ticker.C
RandBytes(payload)
if _, err := localConn.Write(payload); err != nil {
app.LogDebug("writing", "reason", err.Error(), "laddr", laddr, "raddr", raddr)
return
}
app.LogDebug("sent", "laddr", laddr, "raddr", raddr, "payload", string(payload))
}
}
func (app App) ConnectRemote(localConn net.Conn, port int, info *ConnInfo) error {
remoteAddr, _ := localConn.RemoteAddr().(*net.TCPAddr)
remoteConn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", remoteAddr.IP, port))
if err != nil {
return err
}
laddr := remoteConn.LocalAddr()
raddr := remoteConn.RemoteAddr()
defer func() {
remoteConn.Close()
app.LogInfo("closed", "laddr", laddr, "raddr", raddr)
}()
app.LogInfo("connected", "laddr", laddr, "raddr", raddr)
info.IsReversed = true
go io.Copy(localConn, remoteConn)
_, err = io.Copy(remoteConn, localConn)
return err
}