make an event log

This commit is contained in:
Jeff Carr 2025-03-10 05:46:37 -05:00
parent 8fda4d7c87
commit 2e10a2a0d9
7 changed files with 85 additions and 21 deletions

View File

@ -41,3 +41,6 @@ portmap.pb.go: portmap.proto
list:
curl "http://localhost:2522/list"
save:
curl "http://localhost:2522/save"

View File

@ -60,6 +60,46 @@ func ConfigLoad() *Portmaps {
return p
}
func EventLoad() *Events {
pb := new(Events)
var fullname string
base, _ := filepath.Split(argv.Config)
fullname = filepath.Join(base, "events.pb")
var data []byte
var err error
if data, err = loadFile(fullname); err != nil {
log.Warn("event file failed to load", err)
// something went wrong loading the file
return pb
}
if data == nil {
return pb
}
if err = pb.Unmarshal(data); err != nil {
log.Warn("unmarshal failed on config file", err)
return pb
}
log.Log(INFO, "gus.EventLoad() has", pb.Len(), "port mappings")
return pb
}
func (e *Events) Save() {
var fullname string
base, _ := filepath.Split(argv.Config)
fullname = filepath.Join(base, "events.pb")
data, err := e.Marshal()
if err != nil {
log.Info("proto.Marshal() failed", err)
return
}
log.Info("proto.Marshal() worked len", len(data))
configWrite(fullname, data)
}
func loadFile(fullname string) ([]byte, error) {
data, err := os.ReadFile(fullname)
if errors.Is(err, os.ErrNotExist) {

View File

@ -52,11 +52,8 @@ func doGui() {
})
grid.NewButton("Events", func() {
log.Info("todo: start a list here!")
pm := me.portmaps.InsertByListen(5556)
pm.Connect = "haha. gotcha"
pm.Enabled = true
me.portmaps.ConfigSave()
log.Info("event log is len =", me.events.Len())
me.events.Save()
})
// sit here forever refreshing the GUI

View File

@ -60,6 +60,12 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
return
}
if route == "/save" {
log.Info("event log is len =", me.events.Len())
me.events.Save()
return
}
// toggle logging flags
if route == "/flag" {
log.HttpMode(w)

15
main.go
View File

@ -11,9 +11,11 @@ import (
"os"
"time"
"github.com/google/uuid"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/gui"
"go.wit.com/log"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
var VERSION string
@ -31,10 +33,12 @@ func main() {
pp.WriteHelp(os.Stdout)
os.Exit(0)
}
log.Info("tmp hack", uuid.New().String())
me = new(gusconf)
me.pollDelay = 10 * time.Second
me.portmaps = ConfigLoad()
me.events = EventLoad()
if me.portmaps == nil {
me.portmaps = NewPortmaps()
@ -103,9 +107,18 @@ func handleConnection(clientConn net.Conn, where string) {
return
}
defer targetConn.Close()
log.Printf("Connected to target server: %s where = %s\n", targetConn.RemoteAddr(), where)
// log.Printf("Connected to target server: %s where = %s\n", targetConn.RemoteAddr(), where)
log.Printf("Connected to client: %s where = %s\n", clientConn.RemoteAddr(), where)
e := new(Event)
e.Address = fmt.Sprintf("%s\n", clientConn.RemoteAddr())
e.Where = where
e.Ctime = timestamppb.New(time.Now())
me.events.Append(e)
// Bidirectional copy of data
go io.Copy(targetConn, clientConn) // Client -> Target
io.Copy(clientConn, targetConn) // Target -> Client
e.Etime = timestamppb.New(time.Now())
}

View File

@ -2,27 +2,31 @@ syntax = "proto3";
package gus;
import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp
message Event {
string hostname = 1;
string version = 2;
string hostname = 1; // the hostname of the client
string address = 2; // the IP address from the client
string where = 3; // where gus was sending the client traffic
google.protobuf.Timestamp ctime = 4; // when the socket opened
google.protobuf.Timestamp etime = 5; // when the socket ended
}
message Events {
string uuid = 1;
string version = 2;
repeated Event events = 3;
message Events { // `autogenpb:marshal` `autogenpb:gui` `autogenpb:nomutex`
string uuid = 1;
string version = 2;
repeated Event events = 3;
}
message Portmap {
int64 listen = 1; // `autogenpb:unique`
string connect = 2; // `autogenpb:unique`
bool enabled = 3;
string uuid = 4;
int64 listen = 1; // `autogenpb:unique`
string connect = 2; // `autogenpb:unique`
bool enabled = 3;
string uuid = 4;
}
message Portmaps { // `autogenpb:marshal` `autogenpb:gui` `autogenpb:nomutex`
string uuid = 1; // `autogenpb:uuid:49a865ea-292d-48fd-8dc2-d0f82d5fd016`
string version = 2; // `autogenpb:version:v0.0.1`
repeated Portmap portmaps = 3;
Events events = 4;
message Portmaps { // `autogenpb:marshal` `autogenpb:gui` `autogenpb:nomutex`
string uuid = 1; // `autogenpb:uuid:49a865ea-292d-48fd-8dc2-d0f82d5fd016`
string version = 2; // `autogenpb:version:v0.0.1`
repeated Portmap portmaps = 3;
}

View File

@ -17,6 +17,7 @@ var me *gusconf
type gusconf struct {
myGui *gui.Node // the base of the gui
portmaps *Portmaps // the portmap window
events *Events // the event log
portwin *stdTableWin // the portwin window
urlbase string // the dns name for the zookeeper
hostname string // my hostname