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
|
package untls
import (
"crypto/tls"
"fmt"
"io"
"log/slog"
"net"
"os"
g "gobang"
gt "gotext"
)
type _CLIArgs struct {
certFile string
keyFile string
fromAddr string
toAddr string
}
var emitActiveConnection = g.MakeGauge("active-connections")
const X = 1
func parseArgs(args []string) _CLIArgs {
if len(args) != 5 {
fmt.Fprintf(
os.Stderr,
gt.Gettext("Usage: %s CERT.pem PRIVKEY.pem FROM.socket TO.socket\n"),
args[0],
)
os.Exit(2)
}
return _CLIArgs {
certFile: args[1],
keyFile: args[2],
fromAddr: args[3],
toAddr: args[4],
}
}
func listen(certFile string, keyFile string, fromAddr string) net.Listener {
certificate, err := tls.LoadX509KeyPair(certFile, keyFile)
g.FatalIf(err)
config := &tls.Config {
MinVersion: tls.VersionTLS13,
Certificates: []tls.Certificate {
certificate,
},
}
listener, err := tls.Listen("unix", fromAddr, config)
g.FatalIf(err)
g.Info("Started listening", "listen-start", "from-address", fromAddr)
return listener
}
func copyData(c chan struct {}, from io.Reader, to io.WriteCloser) {
io.Copy(to, from)
c <- struct {} {}
// connection is closed, send signal to stop proxy FIXME
}
func start(toAddr string, listener net.Listener) {
for {
connFrom, err := listener.Accept()
if err != nil {
g.Warning(
"Error accepting connection",
"accept-connection-error",
"err", err,
)
continue
}
defer connFrom.Close()
emitActiveConnection.Inc()
connTo, err := net.Dial("unix", toAddr)
if err != nil {
g.Warning(
"Error dialing connection",
"dial-connection-error",
"err", err,
)
continue
}
defer connTo.Close()
c := make(chan struct {})
go copyData(c, connFrom, connTo)
go copyData(c, connTo, connFrom)
go func() {
<- c
emitActiveConnection.Dec()
}()
}
}
func Main() {
g.Init(slog.Group("versions", "gobang", g.Version, "this", Version))
args := parseArgs(os.Args)
listener := listen(args.certFile, args.keyFile, args.fromAddr)
start(args.toAddr, listener)
}
|