Split into separate files and add todo

This commit is contained in:
Anders Pitman 2020-09-26 15:56:57 -06:00
parent c55b7283ec
commit 09770d2c7a
6 changed files with 89 additions and 122 deletions

33
admin_listener.go Normal file
View file

@ -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
}

View file

@ -8,7 +8,6 @@ import (
"crypto/tls" "crypto/tls"
"io" "io"
"sync" "sync"
"errors"
"strconv" "strconv"
"encoding/json" "encoding/json"
) )
@ -190,73 +189,3 @@ func (p *BoringProxy) handleTunnelConnection(decryptedConn net.Conn, serverName
wg.Wait() 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()
}

13
main.go Normal file
View file

@ -0,0 +1,13 @@
package main
import (
"log"
)
func main() {
log.Println("Starting up")
proxy := NewBoringProxy()
proxy.Run()
}

View file

@ -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")
}

1
todo.md Normal file
View file

@ -0,0 +1 @@
* Implement auth

42
tunnel_manager.go Normal file
View file

@ -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
}