From a6a48b8501e8c0c56382119178e1724e42f396ed Mon Sep 17 00:00:00 2001 From: Stefan Etringer Date: Tue, 27 May 2025 15:02:42 +0000 Subject: [PATCH] added real ip to be sent to the server --- src/agentconnector/agentconnector.go | 37 ++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/agentconnector/agentconnector.go b/src/agentconnector/agentconnector.go index bf3d872..51ee2ec 100644 --- a/src/agentconnector/agentconnector.go +++ b/src/agentconnector/agentconnector.go @@ -12,6 +12,7 @@ import ( "math/rand" "math" "strconv" + "net" "github.com/gorilla/websocket" ) @@ -31,7 +32,7 @@ type Agent struct { InitialContact string `json:"initialContact"` LastContact string `json:"lastContact"` AddPort string `json:"addPort"` - HostName string `json:"HostName"` + HostName string `json:"hostName"` } type Message struct { @@ -169,12 +170,44 @@ func randomInt(length int) int { } +func GetLocalIP() net.IP { + addrs, err := net.InterfaceAddrs() + if err != nil { + log.Fatal(err) + } + for _, address := range addrs { + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + return ipnet.IP + } + } + } + return nil +} + +func GetLocalIPs() []net.IP { + var ips []net.IP + addrs, err := net.InterfaceAddrs() + if err != nil { + log.Fatal(err) + } + for _, address := range addrs { + if ipnet, ok := address.(*net.IPNet); ok { + if ipnet.IP.To4() != nil { + ips = append(ips, ipnet.IP) + } + } + } + return ips +} + + // func main() { func StartServer(agentInteractivePort int){ // agentInteractivePort is only needed for interactive sessions agentName := "Agent-001" agentId := strconv.Itoa(randomInt(8)) - agentIp := "127.0.0.1" + agentIp := GetLocalIP().String() agentType := "Interactive" addPort := strconv.Itoa(agentInteractivePort) hostname, _ := os.Hostname()