From 1ccf3ab24bc537c1b7b00e7f8c31a663d20d359a Mon Sep 17 00:00:00 2001 From: idk Date: Fri, 26 Aug 2022 00:03:17 -0400 Subject: [PATCH] Proxy convenience function --- .gitignore | 3 +++ proxy.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 proxy.go diff --git a/.gitignore b/.gitignore index 6a0dfa6..15dbe8d 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ # Dependency directories (remove the comment below to include it) # vendor/ data-dir* +i2pkeys/ +onionkeys/ +tlskeys/ \ No newline at end of file diff --git a/proxy.go b/proxy.go new file mode 100644 index 0000000..d9fc6f1 --- /dev/null +++ b/proxy.go @@ -0,0 +1,56 @@ +package onramp + +import ( + "io" + "log" + "net" + "strings" +) + +type OnrampProxy struct { + Onion + Garlic +} + +// Proxy passes requests from a net.Listener to a remote server +// without touching them in any way. It can be used as a shortcut, +// set up a Garlic or Onion Listener and pass it, along with the +// address of a locally running service and the hidden service +// listener will expose the local service. +// Pass it a regular net.Listener(or a TLS listener if you like), +// and an I2P or Onion address, and it will act as a tunnel to a +// listening hidden service somewhere. +func (p *OnrampProxy) Proxy(list net.Listener, raddr string) error { + for { + conn, err := list.Accept() + if err != nil { + return err + } + go p.proxy(conn, raddr) + } +} + +func (p *OnrampProxy) proxy(conn net.Conn, raddr string) { + var remote net.Conn + var err error + checkaddr := strings.Split(raddr, ":")[0] + if strings.HasSuffix(checkaddr, ".i2p") { + remote, err = p.Garlic.Dial("tcp", raddr) + } else if strings.HasSuffix(checkaddr, ".onion") { + remote, err = p.Onion.Dial("tcp", raddr) + } else { + remote, err = net.Dial("tcp", raddr) + } + if err != nil { + log.Fatalf("cannot dial to remote: %v", err) + } + defer remote.Close() + go io.Copy(remote, conn) + io.Copy(conn, remote) +} + +var proxy *OnrampProxy = &OnrampProxy{} + +func Proxy(list net.Listener, raddr string) error { + return proxy.Proxy(list, raddr) +}