From 09770d2c7a64e39348990434c6f671e8320d769b Mon Sep 17 00:00:00 2001 From: Anders Pitman Date: Sat, 26 Sep 2020 15:56:57 -0600 Subject: [PATCH] Split into separate files and add todo --- admin_listener.go | 33 ++++++++++++++++++++++ boringproxy.go | 71 ----------------------------------------------- main.go | 13 +++++++++ sirtunnel.go | 51 ---------------------------------- todo.md | 1 + tunnel_manager.go | 42 ++++++++++++++++++++++++++++ 6 files changed, 89 insertions(+), 122 deletions(-) create mode 100644 admin_listener.go create mode 100644 main.go delete mode 100644 sirtunnel.go create mode 100644 todo.md create mode 100644 tunnel_manager.go diff --git a/admin_listener.go b/admin_listener.go new file mode 100644 index 0000000..0a63845 --- /dev/null +++ b/admin_listener.go @@ -0,0 +1,33 @@ +package main + +import( + "fmt" + "net" +) + + +type AdminListener struct { + connChan chan(net.Conn) +} + +func NewAdminListener() *AdminListener { + connChan := make(chan(net.Conn)) + return &AdminListener{connChan} +} + +// implement net.Listener +func (l *AdminListener) Accept() (net.Conn, error) { + // TODO: error conditions? + conn := <-l.connChan + return conn, nil +} +func (l *AdminListener) Close() error { + // TODO + fmt.Println("AdminListener Close") + return nil +} +func (l *AdminListener) Addr() net.Addr { + // TODO + fmt.Println("AdminListener Addr") + return nil +} diff --git a/boringproxy.go b/boringproxy.go index 9146478..3357cac 100644 --- a/boringproxy.go +++ b/boringproxy.go @@ -8,7 +8,6 @@ import ( "crypto/tls" "io" "sync" - "errors" "strconv" "encoding/json" ) @@ -190,73 +189,3 @@ func (p *BoringProxy) handleTunnelConnection(decryptedConn net.Conn, serverName wg.Wait() } - - -type TunnelManager struct { - tunnels map[string]int - mutex *sync.Mutex -} - -func NewTunnelManager() *TunnelManager { - tunnels := make(map[string]int) - mutex := &sync.Mutex{} - return &TunnelManager{tunnels, mutex} -} - -func (m *TunnelManager) SetTunnel(host string, port int) { - m.mutex.Lock() - m.tunnels[host] = port - m.mutex.Unlock() -} - -func (m *TunnelManager) DeleteTunnel(host string) { - m.mutex.Lock() - delete(m.tunnels, host) - m.mutex.Unlock() -} - -func (m *TunnelManager) GetPort(serverName string) (int, error) { - m.mutex.Lock() - port, exists := m.tunnels[serverName] - m.mutex.Unlock() - - if !exists { - return 0, errors.New("Doesn't exist") - } - - return port, nil -} - - -type AdminListener struct { - connChan chan(net.Conn) -} - -func NewAdminListener() *AdminListener { - connChan := make(chan(net.Conn)) - return &AdminListener{connChan} -} - -// implement net.Listener -func (l *AdminListener) Accept() (net.Conn, error) { - // TODO: error conditions? - conn := <-l.connChan - return conn, nil -} -func (l *AdminListener) Close() error { - // TODO - fmt.Println("AdminListener Close") - return nil -} -func (l *AdminListener) Addr() net.Addr { - // TODO - fmt.Println("AdminListener Addr") - return nil -} - -func main() { - log.Println("Starting up") - - proxy := NewBoringProxy() - proxy.Run() -} diff --git a/main.go b/main.go new file mode 100644 index 0000000..eb8bccb --- /dev/null +++ b/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "log" +) + + +func main() { + log.Println("Starting up") + + proxy := NewBoringProxy() + proxy.Run() +} diff --git a/sirtunnel.go b/sirtunnel.go deleted file mode 100644 index 6d74f15..0000000 --- a/sirtunnel.go +++ /dev/null @@ -1,51 +0,0 @@ -package main - -import ( - "fmt" - "os" - "os/signal" - "strings" - "net/http" - "bytes" -) - - -func main() { - - args := os.Args[1:] - tunnelSpecParts := strings.Split(args[0], ":") - tunnelId := args[0] - host := tunnelSpecParts[0] - port := tunnelSpecParts[1] - - client := &http.Client{} - - caddyAddRouteStr := fmt.Sprintf("{\"@id\":\"%s\",\"match\":[{\"host\":[\"%s\"]}],\"handle\":[{\"handler\":\"reverse_proxy\",\"upstreams\":[{\"dial\":\":%s\"}]}]}", tunnelId, host, port); - - resp, err := http.Post("http://127.0.0.1:2019/config/apps/http/servers/sirtunnel/routes", "application/json", bytes.NewBuffer([]byte(caddyAddRouteStr))) - - if err != nil { - fmt.Println("Tunnel creation failed") - panic(err) - } - defer resp.Body.Close() - - fmt.Println("Tunnel created successfully") - - // wait for CTRL-C - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - <-c - - fmt.Println("Cleaning up tunnel") - - req, err := http.NewRequest("DELETE", fmt.Sprintf("http://127.0.0.1:2019/id/%s", tunnelId), nil) - if err != nil { - panic(err) - } - - req.Header.Add("Content-Type", "application/json") - _, err = client.Do(req) - - fmt.Println("Exiting") -} diff --git a/todo.md b/todo.md new file mode 100644 index 0000000..885efb5 --- /dev/null +++ b/todo.md @@ -0,0 +1 @@ +* Implement auth diff --git a/tunnel_manager.go b/tunnel_manager.go new file mode 100644 index 0000000..943e372 --- /dev/null +++ b/tunnel_manager.go @@ -0,0 +1,42 @@ +package main + +import ( + "errors" + "sync" +) + + +type TunnelManager struct { + tunnels map[string]int + mutex *sync.Mutex +} + +func NewTunnelManager() *TunnelManager { + tunnels := make(map[string]int) + mutex := &sync.Mutex{} + return &TunnelManager{tunnels, mutex} +} + +func (m *TunnelManager) SetTunnel(host string, port int) { + m.mutex.Lock() + m.tunnels[host] = port + m.mutex.Unlock() +} + +func (m *TunnelManager) DeleteTunnel(host string) { + m.mutex.Lock() + delete(m.tunnels, host) + m.mutex.Unlock() +} + +func (m *TunnelManager) GetPort(serverName string) (int, error) { + m.mutex.Lock() + port, exists := m.tunnels[serverName] + m.mutex.Unlock() + + if !exists { + return 0, errors.New("Doesn't exist") + } + + return port, nil +}