summaryrefslogtreecommitdiff
path: root/src/untls.go
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-08-11 07:12:53 -0300
committerEuAndreh <eu@euandre.org>2024-08-11 07:12:53 -0300
commitdb44fd6aa6e0c3c9ab826e6f217cb555465c0fc4 (patch)
treec680e3b1874186c006fe08ddbceeed61cc7886ca /src/untls.go
parentInitial empty commit (diff)
downloaduntls-db44fd6aa6e0c3c9ab826e6f217cb555465c0fc4.tar.gz
untls-db44fd6aa6e0c3c9ab826e6f217cb555465c0fc4.tar.xz
Initial implementation: copy structure from "binder" project
Diffstat (limited to 'src/untls.go')
-rw-r--r--src/untls.go112
1 files changed, 112 insertions, 0 deletions
diff --git a/src/untls.go b/src/untls.go
new file mode 100644
index 0000000..545ee2e
--- /dev/null
+++ b/src/untls.go
@@ -0,0 +1,112 @@
+package untls
+
+import (
+ "crypto/tls"
+ "fmt"
+ "io"
+ "net"
+ "os"
+
+ g "gobang"
+)
+
+
+
+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,
+ "Usage: %s CERT.pem KEY.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()
+ args := parseArgs(os.Args)
+ listener := listen(args.certFile, args.keyFile, args.fromAddr)
+ start(args.toAddr, listener)
+}