-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (146 loc) · 3.73 KB
/
main.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
168
169
170
171
172
173
174
// SPDX-FileCopyrightText: Stefan Tatschner
//
// SPDX-License-Identifier: MIT
package main
import (
"fmt"
"io"
"log/slog"
"net"
"os"
"sync"
"time"
"github.com/spf13/pflag"
)
func bidirectCopy(left io.ReadWriteCloser, right io.ReadWriteCloser) (int, int, error) {
var (
n1 = 0
n2 = 0
err error
err1 error
err2 error
wg sync.WaitGroup
)
wg.Add(2)
go func() {
if n, err := io.Copy(right, left); err != nil {
err1 = err
} else {
n1 = int(n)
}
right.Close()
wg.Done()
}()
go func() {
if n, err := io.Copy(left, right); err != nil {
err2 = err
} else {
n2 = int(n)
}
left.Close()
wg.Done()
}()
wg.Wait()
if err1 != nil && err2 != nil {
err = fmt.Errorf("both copier failed; left: %s; right: %s", err1, err2)
} else {
if err1 != nil {
err = err1
} else if err2 != nil {
err = err2
}
}
return n1, n2, err
}
type tcpRelay struct {
listen string
target string
keepAlive bool
keepAliveTime time.Duration
}
func (p *tcpRelay) handleClient(conn *net.TCPConn) {
fromTCPConn := conn
targetConn, err := net.Dial("tcp", p.target)
if err != nil {
slog.Error(err.Error())
return
}
targetTCPConn := targetConn.(*net.TCPConn)
if p.keepAlive {
if err := fromTCPConn.SetKeepAlive(true); err != nil {
slog.Warn(fmt.Sprintf("Set KeepAlive failed: %s", err))
}
if err := fromTCPConn.SetKeepAlivePeriod(p.keepAliveTime); err != nil {
slog.Warn(fmt.Sprintf("Set KeepAlivePeriod failed: %s", err))
}
if err := targetTCPConn.SetKeepAlive(true); err != nil {
slog.Warn(fmt.Sprintf("Set KeepAlive failed: %s", err))
}
if err := targetTCPConn.SetKeepAlivePeriod(p.keepAliveTime); err != nil {
slog.Warn(fmt.Sprintf("Set KeepAlivePeriod failed: %s", err))
}
}
slog.Debug(fmt.Sprintf("established connection: %s", targetConn.RemoteAddr()))
defer slog.Debug(fmt.Sprintf("association lost: %s<->%s", conn.RemoteAddr(), targetConn.RemoteAddr()))
if _, _, err = bidirectCopy(fromTCPConn, targetTCPConn); err != nil {
slog.Debug(err.Error())
}
}
func (p *tcpRelay) Serve() error {
ln, err := net.Listen("tcp", p.listen)
if err != nil {
return err
}
for {
conn, err := ln.Accept()
if err != nil {
slog.Warn(err.Error())
continue
}
slog.Debug(fmt.Sprintf("got connection: %s", conn.RemoteAddr()))
go p.handleClient(conn.(*net.TCPConn))
}
}
type runtimeOptions struct {
listen string
keepAlive bool
keepAliveTime time.Duration
target string
verbose bool
help bool
}
func main() {
opts := runtimeOptions{}
pflag.StringVarP(&opts.listen, "listen", "l", "", "listen on this addr:port; hostnames are allowed")
pflag.StringVarP(&opts.target, "target", "t", "", "specify relay target")
pflag.BoolVar(&opts.keepAlive, "keep-alive", false, "enable tcp keepalive probes")
pflag.DurationVar(&opts.keepAliveTime, "keep-alive-time", 25*time.Second, "specify keepalive time in seconds")
pflag.BoolVarP(&opts.verbose, "verbose", "v", false, "enable debugging output")
pflag.Parse()
var level slog.Level
if opts.verbose {
level = slog.LevelDebug
} else {
level = slog.LevelInfo
}
handler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: level,
})
logger := slog.New(handler)
slog.SetDefault(logger)
if opts.listen == "" || opts.target == "" {
slog.Error("no address mapping specified; specify --listen and --target")
os.Exit(1)
}
slog.Info(fmt.Sprintf("tcp relay listening on '%s'; forwarding to '%s'", opts.listen, opts.target))
proxy := tcpRelay{
listen: opts.listen,
target: opts.target,
keepAlive: opts.keepAlive,
keepAliveTime: time.Duration(opts.keepAliveTime) * time.Second,
}
if err := proxy.Serve(); err != nil {
slog.Error(err.Error())
os.Exit(1)
}
}