From 01a9751d0816ac95447dae80c8b1a18725f1ae25 Mon Sep 17 00:00:00 2001 From: Ronald Date: Tue, 9 Sep 2025 21:13:25 +0100 Subject: [PATCH] Able to connect to a client --- DesignDoc.md | 16 ++++++++-------- main.go | 31 +++++++++++++------------------ types.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 26 deletions(-) create mode 100644 types.go diff --git a/DesignDoc.md b/DesignDoc.md index 86921d5..5548031 100644 --- a/DesignDoc.md +++ b/DesignDoc.md @@ -4,20 +4,20 @@ ### Types | TYPE -0 => Request, a new request -1 => Result, the result +1 => Request, a new request +2 => Result, the result #### Request types | REQUEST -0 => New server client -1 => New desktop client -2 => Data +1 => New server client +2 => New desktop client +3 => Data #### Response types | RESPONSE -0 => OK -1 => Created -2 => Not OK +1 => OK +2 => Created +3 => Not OK #### Data types | DATA diff --git a/main.go b/main.go index 5980466..bae1dd4 100644 --- a/main.go +++ b/main.go @@ -16,16 +16,9 @@ import ( ) // Global variables +// DB global database oject var DB *gorm.DB -type Data struct { - T *int `json:"TYPE"` // TYPE - Request *int `json:"REQUEST"` // REQUEST TYPE - Result *int `json:"RESPONSE,omitempty"` // RESPONSE TYPE - Hostname *string `json:"HOSTNAME"` // HOSTNAME - Data *map[string]any `json:"DATA,omitempty"` // DATA -} - func handleInitialConnection(conn net.Conn) { initial_request := make([]byte, 256) @@ -54,25 +47,23 @@ func handleInitialConnection(conn net.Conn) { return } - if *data.T != 0 || *data.Request != 0 || len(*data.Hostname) < 1 { - fmt.Fprintf(os.Stderr, "data: %v\n", data) - fmt.Fprintf(os.Stderr, "data.Hostname: \"%s\"\n", data.Hostname) + if *data.Type != REQUEST && (*data.Request != NEW_SERVER_CLIENT || *data.Request != NEW_DESKTOP_CLIENT) { // TODO: I think this logic may be a little wrong fmt.Fprintf(os.Stderr, "ERROR: invalid initial request, closing connection from %s\n", conn.RemoteAddr()) conn.Close() return } switch *data.Request { - case 0: + case NEW_SERVER_CLIENT: var connectedResponse Data - connectedResponse.T = new(int) - connectedResponse.Result = new(int) - *connectedResponse.T = 1 + connectedResponse.Type = new(Type) + connectedResponse.Response = new(Response_Type) + *connectedResponse.Type = RESPONSE foundHost := false for _, host := range GetAllHosts() { if strings.ToLower(host.Hostname) == strings.ToLower(*data.Hostname) { - *connectedResponse.T = 1 - *connectedResponse.Result = 0 + *connectedResponse.Type = RESPONSE + *connectedResponse.Response = OK fmt.Printf("Found host in database\n") @@ -81,14 +72,16 @@ func handleInitialConnection(conn net.Conn) { break } } + if !foundHost { if ok := CreateHost(*data.Hostname); !ok { fmt.Fprintf(os.Stderr, "ERROR: failed to create host in database, disconnecting client\n") conn.Close() return } - fmt.Printf("Created host in database") + fmt.Printf("Created host in database\n") } + bytes, err := json.Marshal(connectedResponse) if err != nil { fmt.Fprintf(os.Stderr, "ERROR: failed to create JSON to send back to client, closing connection\n") @@ -96,6 +89,8 @@ func handleInitialConnection(conn net.Conn) { return } + fmt.Println("Response: ", string(bytes)) + conn.Write(bytes) // TODO: Handle client connections differently diff --git a/types.go b/types.go new file mode 100644 index 0000000..4254eff --- /dev/null +++ b/types.go @@ -0,0 +1,30 @@ +package main + +type Type int +type Request_Type int +type Response_Type int + +type Data struct { + Type *Type `json:"TYPE",omitempty` // TYPE + Request *Request_Type `json:"REQUEST,omitempty"` // REQUEST TYPE + Response *Response_Type `json:"RESPONSE,omitempty"` // RESPONSE TYPE + Hostname *string `json:"HOSTNAME,omitempty"` // HOSTNAME + Data *map[string]any `json:"DATA,omitempty"` // DATA +} + +const ( // TYPE + REQUEST Type = 1 + RESPONSE Type = 2 +) + +const ( // REQUEST TYPES + NEW_SERVER_CLIENT Request_Type = 1 + NEW_DESKTOP_CLIENT Request_Type = 2 + DATA Request_Type = 3 +) + +const ( // RESPONSE TYPES + OK Response_Type = 1 + CREATED Response_Type = 2 + NOT_OK Response_Type = 3 +)