diff --git a/src/pkg/dbSync.go b/src/pkg/dbSync.go index 34d9c8b..7bf81e6 100644 --- a/src/pkg/dbSync.go +++ b/src/pkg/dbSync.go @@ -4,9 +4,11 @@ import ( "bytes" "database/sql" "encoding/json" - "io/ioutil" + "io" "log" + "net" "net/http" + "os" "strconv" ) @@ -35,6 +37,13 @@ func FetchDatabaseStatus(db *sql.DB, dbName string, config DBConfig) error { } log.Printf("Database: %s Active: %d seconds", dbName, uptime) + hostname, err := os.Hostname() + outboundIP := GetOutboundIP() + if err != nil { + log.Printf("Cannot retrieve hostname") + } + log.Printf("Retrieved hostname and outboundIP: %s, %s", hostname, outboundIP) + // Sync database information with the API payload := map[string]interface{}{ "orgId": orgID, @@ -45,6 +54,8 @@ func FetchDatabaseStatus(db *sql.DB, dbName string, config DBConfig) error { "host": config.Host, "status": status, "uuid": config.APIKey, + "publicIp": outboundIP, + "hostname": hostname, } payloadBytes, err := json.Marshal(payload) @@ -72,7 +83,7 @@ func FetchDatabaseStatus(db *sql.DB, dbName string, config DBConfig) error { } defer httpResp.Body.Close() - body, err := ioutil.ReadAll(httpResp.Body) + body, err := io.ReadAll(httpResp.Body) if err != nil { log.Printf("Error while reading response body: %v", err) @@ -80,3 +91,14 @@ func FetchDatabaseStatus(db *sql.DB, dbName string, config DBConfig) error { log.Default().Printf("Response from external service: %v", string(body)) return nil } + +func GetOutboundIP() net.IP { + conn, err := net.Dial("udp", "8.8.8.8:80") + if err != nil { + log.Fatal(err) + } + defer conn.Close() + + localAddr := conn.LocalAddr().(*net.UDPAddr) + return localAddr.IP +}