make a portmap protobuf & ConfigSave()
This commit is contained in:
parent
93852523e6
commit
eaedaded62
2
Makefile
2
Makefile
|
@ -30,7 +30,7 @@ vet:
|
||||||
@echo this go binary package builds okay
|
@echo this go binary package builds okay
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f go.*
|
rm -f go.* *.pb.go
|
||||||
rm -f gus
|
rm -f gus
|
||||||
|
|
||||||
portmap.pb.go: portmap.proto
|
portmap.pb.go: portmap.proto
|
||||||
|
|
|
@ -0,0 +1,107 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
// functions to import and export the protobuf
|
||||||
|
// data to and from config files
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"go.wit.com/log"
|
||||||
|
"google.golang.org/protobuf/proto"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (m *Portmaps) ConfigSave() error {
|
||||||
|
if m == nil {
|
||||||
|
return fmt.Errorf("ConfigSave() nil")
|
||||||
|
}
|
||||||
|
s := m.FormatTEXT()
|
||||||
|
log.Info("proto.Marshal() worked len", len(s))
|
||||||
|
configWrite([]byte(s))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Portmaps) ConfigLoad() error {
|
||||||
|
if m == nil {
|
||||||
|
return errors.New("It's not safe to run ConfigLoad() on a nil ?")
|
||||||
|
}
|
||||||
|
if os.Getenv("CLOUD_HOME") == "" {
|
||||||
|
homeDir, _ := os.UserHomeDir()
|
||||||
|
fullpath := filepath.Join(homeDir, ".config/cloud")
|
||||||
|
os.Setenv("CLOUD_HOME", fullpath)
|
||||||
|
}
|
||||||
|
|
||||||
|
var data []byte
|
||||||
|
var err error
|
||||||
|
if data, err = loadFile("gus.text"); err != nil {
|
||||||
|
// something went wrong loading the file
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if data != nil {
|
||||||
|
if err = proto.Unmarshal(data, m); err != nil {
|
||||||
|
log.Warn("broken gus.text config file", "gus.text")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
data, err := os.ReadFile(fullname)
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
// if file does not exist, just return nil. this
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
// log.Info("open config file :", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Portmaps) loadFile(fname string) ([]byte, error) {
|
||||||
|
fullname := filepath.Join(os.Getenv("CLOUD_HOME"), fname)
|
||||||
|
|
||||||
|
data, err := os.ReadFile(fullname)
|
||||||
|
if err != nil {
|
||||||
|
// log.Info("open config file :", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
log.Warn("open config file :", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cfgfile.Write(data)
|
||||||
|
return nil
|
||||||
|
}
|
44
main.go
44
main.go
|
@ -4,6 +4,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -35,5 +37,47 @@ func main() {
|
||||||
|
|
||||||
// go NewWatchdog()
|
// go NewWatchdog()
|
||||||
|
|
||||||
|
go listen3000()
|
||||||
|
|
||||||
startHTTP()
|
startHTTP()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func listen3000() {
|
||||||
|
// Listen on local port 3000
|
||||||
|
listener, err := net.Listen("tcp", "0.0.0.0:3000")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to listen on port 3000: %v", err)
|
||||||
|
}
|
||||||
|
defer listener.Close()
|
||||||
|
log.Println("Listening on port 3000...")
|
||||||
|
|
||||||
|
for {
|
||||||
|
// Accept incoming connection
|
||||||
|
clientConn, err := listener.Accept()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to accept client connection: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
log.Printf("Client connected: %s", clientConn.RemoteAddr())
|
||||||
|
|
||||||
|
// Handle the connection in a separate goroutine
|
||||||
|
go handleConnection(clientConn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleConnection(clientConn net.Conn) {
|
||||||
|
defer clientConn.Close()
|
||||||
|
|
||||||
|
// Connect to the target server
|
||||||
|
targetConn, err := net.Dial("tcp", "go.wit.com:44355")
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to connect to go.wit.com %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer targetConn.Close()
|
||||||
|
log.Printf("Connected to target server: %s", targetConn.RemoteAddr())
|
||||||
|
|
||||||
|
// Bidirectional copy of data
|
||||||
|
go io.Copy(targetConn, clientConn) // Client -> Target
|
||||||
|
io.Copy(clientConn, targetConn) // Target -> Client
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,17 @@ syntax = "proto3";
|
||||||
|
|
||||||
package gus;
|
package gus;
|
||||||
|
|
||||||
|
message Event {
|
||||||
|
string hostname = 1;
|
||||||
|
string version = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message Events {
|
||||||
|
string uuid = 1;
|
||||||
|
string version = 2;
|
||||||
|
repeated Event events = 3;
|
||||||
|
}
|
||||||
|
|
||||||
message Portmap {
|
message Portmap {
|
||||||
int64 listen = 1;
|
int64 listen = 1;
|
||||||
string connect = 2;
|
string connect = 2;
|
||||||
|
|
Loading…
Reference in New Issue