From fba2d24625d844b7f6115a6cb86f7fc6e1f9e3d5 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 10 Mar 2025 03:12:09 -0500 Subject: [PATCH] start making it work in the real world --- Makefile | 13 ++++++--- Makefile.help | 20 ++++++------- argv.go | 1 + config.go | 81 +++++++++++++++++++++++++++++++-------------------- http.go | 25 +++++++++------- main.go | 14 ++++----- portmap.proto | 1 + 7 files changed, 91 insertions(+), 64 deletions(-) diff --git a/Makefile b/Makefile index 466aee7..55c3e98 100644 --- a/Makefile +++ b/Makefile @@ -3,12 +3,14 @@ VERSION = $(shell git describe --tags) BUILDTIME = $(shell date +%Y.%m.%d_%H%M) -all: portmap.pb.go goimports vet build +all: portmap.pb.go build ./gus --version - ./gus --no-gui - # ./gus --gui gocui >/tmp/gocui.log 2>&1 + ./gus --no-gui --config /etc/gus/gus.text -build: goimports +gocui: build + ./gus --gui gocui --config /etc/gus/gus.text >/tmp/gocui.log 2>&1 + +build: goimports vet GO111MODULE=off go build -v -x \ -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" @@ -36,3 +38,6 @@ clean: portmap.pb.go: portmap.proto autogenpb --proto portmap.proto + +list: + curl "http://localhost:2522/list" diff --git a/Makefile.help b/Makefile.help index c64a2dc..aec5c44 100644 --- a/Makefile.help +++ b/Makefile.help @@ -1,11 +1,11 @@ all: @echo "make log # watch the zoo daemon log" - @echo "make status # show the systemd status of zood" - @echo "make restart # restart zood" - @echo "make enable # enable zood on boot" + @echo "make status # show the systemd status of gus" + @echo "make restart # restart gus" + @echo "make enable # enable gus on boot" log: - journalctl -f -xeu zood.service + journalctl -f -xeu gus.service curl-toggle-PING-output: curl "http://localhost:2521/flag?flag-PING" @@ -14,16 +14,16 @@ curl-kill: curl http://localhost:2521/kill status: - dpkg -s zood - systemctl status zood.service + dpkg -s gus + systemctl status gus.service enable: - systemctl enable zood.service + systemctl enable gus.service stop: - systemctl stop zood.service + systemctl stop gus.service restart: - systemctl stop zood.service - systemctl start zood.service + systemctl stop gus.service + systemctl start gus.service make log diff --git a/argv.go b/argv.go index a66764d..25c03c1 100644 --- a/argv.go +++ b/argv.go @@ -19,6 +19,7 @@ type args struct { Daemon bool `arg:"--daemon" default:"false" help:"run in daemon mode"` Port int `arg:"--port" default:"2522" help:"port to run on"` URL string `arg:"--url" help:"url to use"` + Config string `arg:"--config" help:"config file (default is ~/.config/cloud/gus.text"` } func (args) Version() string { diff --git a/config.go b/config.go index c46106f..3ec3619 100644 --- a/config.go +++ b/config.go @@ -10,7 +10,6 @@ import ( "path/filepath" "go.wit.com/log" - "google.golang.org/protobuf/proto" ) func (m *Portmaps) ConfigSave() error { @@ -19,15 +18,25 @@ func (m *Portmaps) ConfigSave() error { } s := m.FormatTEXT() log.Info("proto.Marshal() worked len", len(s)) - configWrite([]byte(s)) + configWrite(argv.Config, []byte(s)) return nil } func ConfigLoad() *Portmaps { - if os.Getenv("CLOUD_HOME") == "" { + var fullname string + if os.Getenv("CLOUD_HOME") != "" { + fullname = filepath.Join(os.Getenv("CLOUD_HOME"), "gus.text") + if argv.Config == "" { + argv.Config = fullname + } + } + if argv.Config != "" { + fullname = argv.Config + } + if fullname == "" { homeDir, _ := os.UserHomeDir() - fullpath := filepath.Join(homeDir, ".config/cloud") - os.Setenv("CLOUD_HOME", fullpath) + fullname = filepath.Join(homeDir, ".config/cloud", "gus.text") + argv.Config = fullname } var data []byte @@ -51,19 +60,30 @@ func ConfigLoad() *Portmaps { return p } +/* func (m *Portmaps) ConfigLoad() error { + var fullname string if m == nil { return errors.New("It's not safe to run ConfigLoad() on a nil ?") } - if os.Getenv("CLOUD_HOME") == "" { + if os.Getenv("CLOUD_HOME") != "" { + fullname = filepath.Join(os.Getenv("CLOUD_HOME"), "gus.text") + if argv.Config == "" { + argv.Config = fullname + } + } + if argv.Config != "" { + fullname = argv.Config + } + if fullname == "" { homeDir, _ := os.UserHomeDir() - fullpath := filepath.Join(homeDir, ".config/cloud") - os.Setenv("CLOUD_HOME", fullpath) + fullname = filepath.Join(homeDir, ".config/cloud", "gus.text") + argv.Config = fullname } var data []byte var err error - if data, err = loadFile("gus.text"); err != nil { + if data, err = loadFile(); err != nil { // something went wrong loading the file return err } @@ -79,11 +99,9 @@ func (m *Portmaps) ConfigLoad() error { log.Log(INFO, "gus.ConfigLoad() has", m.Len(), "port mappings") return nil } +*/ -func loadFile(filename string) ([]byte, error) { - homeDir, err := os.UserHomeDir() - p := filepath.Join(homeDir, ".config/cloud") - fullname := filepath.Join(p, filename) +func loadFile(fullname string) ([]byte, error) { data, err := os.ReadFile(fullname) if errors.Is(err, os.ErrNotExist) { // if file does not exist, just return nil. this @@ -96,9 +114,7 @@ func loadFile(filename string) ([]byte, error) { return data, nil } -func (m *Portmaps) loadFile(fname string) ([]byte, error) { - fullname := filepath.Join(os.Getenv("CLOUD_HOME"), fname) - +func (m *Portmaps) loadFile(fullname string) ([]byte, error) { data, err := os.ReadFile(fullname) if err != nil { // log.Info("open config file :", err) @@ -107,23 +123,24 @@ func (m *Portmaps) loadFile(fname string) ([]byte, error) { return data, nil } -func configWrite(data []byte) error { - homeDir, err := os.UserHomeDir() - p := filepath.Join(homeDir, ".config/cloud") - fname := filepath.Join(p, "gus.text") - cfgfile, err := os.OpenFile(fname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) - defer cfgfile.Close() - if err != nil { - log.Warn("open config file :", err) - return err +func configWrite(fullname string, data []byte) error { + if _, base := filepath.Split(fullname); base == "" { + return fmt.Errorf("--config option not set") + } + cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + defer cfgfile.Close() + if err != nil { + log.Warn("open config file :", err) + return err + } + cfgfile.Write(data) + return nil +} + +func (m *Portmaps) configWrite(fullname string, data []byte) error { + if _, base := filepath.Split(fullname); base == "" { + return fmt.Errorf("--config option not set") } - cfgfile.Write(data) - return nil -} - -func (m *Portmaps) configWrite(fname string, data []byte) error { - fullname := filepath.Join(os.Getenv("CLOUD_HOME"), fname) - cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) defer cfgfile.Close() if err != nil { diff --git a/http.go b/http.go index b884187..d478fff 100644 --- a/http.go +++ b/http.go @@ -23,7 +23,7 @@ func okHandler(w http.ResponseWriter, r *http.Request) { log.Info("Got URL Path: ", r.URL.Path) route := cleanURL(r.URL.Path) - domname := r.URL.Query().Get("domain") + // domname := r.URL.Query().Get("domain") flag := r.URL.Query().Get("flag") msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte @@ -38,12 +38,7 @@ func okHandler(w http.ResponseWriter, r *http.Request) { return } - // exit the virtigo daemon & have systemd restart it - // this can happen & when it does, access to - // to libvirtd will hang (aka: virsh list will hang) - // One way to trigger this is to not properly close - // domain sockets opened from go-qemu/hypervisor - // it's a good idea in any case so leave it here + // exit gus if route == "/kill" { log.Warn("KILLED") fmt.Fprintln(w, "KILLED") @@ -51,10 +46,18 @@ func okHandler(w http.ResponseWriter, r *http.Request) { return } - // curl http://localhost:2520/import?domain=foo.bar.com - if route == "/import" { - fmt.Fprint(w, "import domain:", domname) - + if route == "/list" { + all := me.portmaps.All() + for all.Scan() { + pm := all.Next() + if !pm.Enabled { + continue + } + s := fmt.Sprintf("portmap enabled for port %d to %s", pm.Listen, pm.Connect) + log.Info(s) + fmt.Fprintln(w, s) + } + startHTTP() return } diff --git a/main.go b/main.go index 6d02a2b..dbfbb90 100644 --- a/main.go +++ b/main.go @@ -50,9 +50,6 @@ func main() { os.Exit(0) } - // go NewWatchdog() - go startHTTP() - if gui.NoGui() { all := me.portmaps.All() for all.Scan() { @@ -61,10 +58,13 @@ func main() { continue } log.Info("portmap enabled for port", pm.Listen, "to", pm.Connect) - gus3000(int(pm.Listen), pm.Connect) + go gus3000(int(pm.Listen), pm.Connect) } + // startHTTP() os.Exit(0) } + // go NewWatchdog() + go startHTTP() doGui() } @@ -85,10 +85,10 @@ func gus3000(port int, connect string) { log.Printf("Failed to accept client connection: %v", err) continue } - log.Printf("Client connected: %s", clientConn.RemoteAddr()) + // log.Printf("Client connected: %s", clientConn.RemoteAddr()) // Handle the connection in a separate goroutine - go handleConnection(clientConn, s) + go handleConnection(clientConn, connect) } } @@ -103,7 +103,7 @@ func handleConnection(clientConn net.Conn, where string) { return } defer targetConn.Close() - log.Printf("Connected to target server: %s", targetConn.RemoteAddr()) + log.Printf("Connected to target server: %s where = %s\n", targetConn.RemoteAddr(), where) // Bidirectional copy of data go io.Copy(targetConn, clientConn) // Client -> Target diff --git a/portmap.proto b/portmap.proto index e9e21ff..7b29545 100644 --- a/portmap.proto +++ b/portmap.proto @@ -17,6 +17,7 @@ message Portmap { int64 listen = 1; // `autogenpb:unique` string connect = 2; // `autogenpb:unique` bool enabled = 3; + string uuid = 4; } message Portmaps { // `autogenpb:marshal` `autogenpb:gui` `autogenpb:nomutex`