From f9af813d490072f075531d51d5a333ecf24bc5cc Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 18 Apr 2023 15:28:55 +0200 Subject: [PATCH 1/8] Add create command structure and parsing --- pcsdk/pcsdk.go | 24 ++++++++++++++++++++++++ pcsdk/pcsdk_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 pcsdk/pcsdk.go create mode 100644 pcsdk/pcsdk_test.go diff --git a/pcsdk/pcsdk.go b/pcsdk/pcsdk.go new file mode 100644 index 0000000..4b9610c --- /dev/null +++ b/pcsdk/pcsdk.go @@ -0,0 +1,24 @@ +package pcsdk + +import ( + "net/netip" + + "github.com/google/uuid" + "github.com/thefeli73/polemos/state" +) + +type ProxyCommandCreate struct { + Command CommandCreate `json:"create"` + // signature Signature +} + +type CommandCreate struct { + IncomingPort uint16 `json:"incoming_port"` + DestinationPort uint16 `json:"destination_port"` + DestinationIP netip.Addr `json:"destination_ip"` + Id string `json:"id"` +} + +func NewCommandCreate(iport uint16, oport uint16, oip netip.Addr, id state.CustomUUID) CommandCreate { + return CommandCreate{iport, oport, oip, uuid.UUID.String(uuid.UUID(id))} +} diff --git a/pcsdk/pcsdk_test.go b/pcsdk/pcsdk_test.go new file mode 100644 index 0000000..b14462e --- /dev/null +++ b/pcsdk/pcsdk_test.go @@ -0,0 +1,29 @@ +package pcsdk + +import ( + "encoding/json" + "net/netip" + "testing" + + "github.com/google/uuid" + "github.com/thefeli73/polemos/state" +) + +func TestCommandCreateJsonParse(t *testing.T) { + ip, _ := netip.ParseAddr("127.0.0.99") + uuid, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1") + c := NewCommandCreate(5555, 6666, ip, state.CustomUUID(uuid)) + m := ProxyCommandCreate{c} + msg, err := json.Marshal(m) + if err != nil { + t.Fatalf(`%q`, err) + } + + expected := "{\"create\":{\"incoming_port\":5555,\"destination_port\":6666,\"destination_ip\":\"127.0.0.99\",\"id\":\"87e79cbc-6df6-4462-8412-85d6c473e3b1\"}}" + if string(msg) != expected { + t.Fatalf( + "\nExpected:\t %q\nGot:\t\t %q\n", expected, msg) + } +} + + From 8bec57d0349b48dbe27e0f4882181b52dc00ac1a Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 19 Apr 2023 13:17:33 +0200 Subject: [PATCH 2/8] Add structures and functions for making create/modify/delete commands --- cmd/examples/create/create.go | 56 +++++++++++++++++++++++++++++++++++ cmd/examples/delete/delete.go | 54 +++++++++++++++++++++++++++++++++ cmd/examples/modify/modify.go | 56 +++++++++++++++++++++++++++++++++++ pcsdk/pcsdk.go | 33 +++++++++++++++++++-- 4 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 cmd/examples/create/create.go create mode 100644 cmd/examples/delete/delete.go create mode 100644 cmd/examples/modify/modify.go diff --git a/cmd/examples/create/create.go b/cmd/examples/create/create.go new file mode 100644 index 0000000..02e72f2 --- /dev/null +++ b/cmd/examples/create/create.go @@ -0,0 +1,56 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "net/netip" + "os" + + "github.com/google/uuid" + "github.com/thefeli73/polemos/pcsdk" + "github.com/thefeli73/polemos/state" +) + +func main() { + ip := netip.MustParseAddr("127.0.0.1") + uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") + + m := pcsdk.NewCommandCreate(5555, 6666, ip, state.CustomUUID(uuid)) + data, err := json.Marshal(m) + if err != nil { + fmt.Printf("client: could not serialize into JSON") + os.Exit(1) + } + + fmt.Printf(string(data)) + + requestURL := "http://localhost:3000/command" + bodyReader := bytes.NewReader(data) + req, err := http.NewRequest(http.MethodPost, requestURL, bodyReader) + + if err != nil { + fmt.Printf("client: could not create request: %s\n", err) + os.Exit(1) + } + + req.Header.Set("Content-Type", "application/json") + fmt.Println(req) + res, err := http.DefaultClient.Post(requestURL, "application/json", bodyReader) + if err != nil { + fmt.Printf("client: error making http request: %s\n", err) + os.Exit(1) + } + + fmt.Printf("client: got response!\n") + fmt.Printf("client: status code: %d\n", res.StatusCode) + + resBody, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Printf("client: could not read response body: %s\n", err) + os.Exit(1) + } + fmt.Printf("client: response body: %s\n", resBody) +} diff --git a/cmd/examples/delete/delete.go b/cmd/examples/delete/delete.go new file mode 100644 index 0000000..1c72fec --- /dev/null +++ b/cmd/examples/delete/delete.go @@ -0,0 +1,54 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "os" + + "github.com/google/uuid" + "github.com/thefeli73/polemos/pcsdk" + "github.com/thefeli73/polemos/state" +) + +func main() { + uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") + + m := pcsdk.NewCommandDelete(state.CustomUUID(uuid)) + data, err := json.Marshal(m) + if err != nil { + fmt.Printf("client: could not serialize into JSON") + os.Exit(1) + } + + fmt.Printf(string(data)) + + requestURL := "http://localhost:3000/command" + bodyReader := bytes.NewReader(data) + req, err := http.NewRequest(http.MethodPost, requestURL, bodyReader) + + if err != nil { + fmt.Printf("client: could not create request: %s\n", err) + os.Exit(1) + } + + req.Header.Set("Content-Type", "application/json") + fmt.Println(req) + res, err := http.DefaultClient.Post(requestURL, "application/json", bodyReader) + if err != nil { + fmt.Printf("client: error making http request: %s\n", err) + os.Exit(1) + } + + fmt.Printf("client: got response!\n") + fmt.Printf("client: status code: %d\n", res.StatusCode) + + resBody, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Printf("client: could not read response body: %s\n", err) + os.Exit(1) + } + fmt.Printf("client: response body: %s\n", resBody) +} diff --git a/cmd/examples/modify/modify.go b/cmd/examples/modify/modify.go new file mode 100644 index 0000000..ff7bedf --- /dev/null +++ b/cmd/examples/modify/modify.go @@ -0,0 +1,56 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "net/netip" + "os" + + "github.com/google/uuid" + "github.com/thefeli73/polemos/pcsdk" + "github.com/thefeli73/polemos/state" +) + +func main() { + ip := netip.MustParseAddr("127.0.0.1") + uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") + + m := pcsdk.NewCommandModify(8888, ip, state.CustomUUID(uuid)) + data, err := json.Marshal(m) + if err != nil { + fmt.Printf("client: could not serialize into JSON") + os.Exit(1) + } + + fmt.Printf(string(data)) + + requestURL := "http://localhost:3000/command" + bodyReader := bytes.NewReader(data) + req, err := http.NewRequest(http.MethodPost, requestURL, bodyReader) + + if err != nil { + fmt.Printf("client: could not create request: %s\n", err) + os.Exit(1) + } + + req.Header.Set("Content-Type", "application/json") + fmt.Println(req) + res, err := http.DefaultClient.Post(requestURL, "application/json", bodyReader) + if err != nil { + fmt.Printf("client: error making http request: %s\n", err) + os.Exit(1) + } + + fmt.Printf("client: got response!\n") + fmt.Printf("client: status code: %d\n", res.StatusCode) + + resBody, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Printf("client: could not read response body: %s\n", err) + os.Exit(1) + } + fmt.Printf("client: response body: %s\n", resBody) +} diff --git a/pcsdk/pcsdk.go b/pcsdk/pcsdk.go index 4b9610c..7825a4a 100644 --- a/pcsdk/pcsdk.go +++ b/pcsdk/pcsdk.go @@ -19,6 +19,35 @@ type CommandCreate struct { Id string `json:"id"` } -func NewCommandCreate(iport uint16, oport uint16, oip netip.Addr, id state.CustomUUID) CommandCreate { - return CommandCreate{iport, oport, oip, uuid.UUID.String(uuid.UUID(id))} +func NewCommandCreate(iport uint16, oport uint16, oip netip.Addr, id state.CustomUUID) ProxyCommandCreate { + c := CommandCreate{iport, oport, oip, uuid.UUID.String(uuid.UUID(id))} + return ProxyCommandCreate{c} +} + +type ProxyCommandModify struct { + Command CommandModify `json:"modify"` +} + +type CommandModify struct { + DestinationPort uint16 `json:"destination_port"` + DestinationIP netip.Addr `json:"destination_ip"` + Id string `json:"id"` +} + +func NewCommandModify(oport uint16, oip netip.Addr, id state.CustomUUID) ProxyCommandModify { + c := CommandModify{oport, oip, uuid.UUID.String(uuid.UUID(id))} + return ProxyCommandModify{c} +} + +type ProxyCommandDelete struct { + Command CommandDelete `json:"delete"` +} + +type CommandDelete struct { + Id string `json:"id"` +} + +func NewCommandDelete(id state.CustomUUID) ProxyCommandDelete { + c := CommandDelete{uuid.UUID.String(uuid.UUID(id))} + return ProxyCommandDelete{c} } From 1a31a304d6f0a40216006f1b95bf4f69e87ad602 Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 19 Apr 2023 13:53:16 +0200 Subject: [PATCH 3/8] Rename pcsdk module to commands --- cmd/examples/create/create.go | 2 +- cmd/examples/delete/delete.go | 2 +- cmd/examples/modify/modify.go | 2 +- pcsdk/pcsdk.go => commands/commands.go | 0 pcsdk/pcsdk_test.go => commands/commands_test.go | 0 5 files changed, 3 insertions(+), 3 deletions(-) rename pcsdk/pcsdk.go => commands/commands.go (100%) rename pcsdk/pcsdk_test.go => commands/commands_test.go (100%) diff --git a/cmd/examples/create/create.go b/cmd/examples/create/create.go index 02e72f2..c68db08 100644 --- a/cmd/examples/create/create.go +++ b/cmd/examples/create/create.go @@ -10,7 +10,7 @@ import ( "os" "github.com/google/uuid" - "github.com/thefeli73/polemos/pcsdk" + pcsdk "github.com/thefeli73/polemos/commands" "github.com/thefeli73/polemos/state" ) diff --git a/cmd/examples/delete/delete.go b/cmd/examples/delete/delete.go index 1c72fec..2c8bd35 100644 --- a/cmd/examples/delete/delete.go +++ b/cmd/examples/delete/delete.go @@ -9,7 +9,7 @@ import ( "os" "github.com/google/uuid" - "github.com/thefeli73/polemos/pcsdk" + pcsdk "github.com/thefeli73/polemos/commands" "github.com/thefeli73/polemos/state" ) diff --git a/cmd/examples/modify/modify.go b/cmd/examples/modify/modify.go index ff7bedf..8aeb434 100644 --- a/cmd/examples/modify/modify.go +++ b/cmd/examples/modify/modify.go @@ -10,7 +10,7 @@ import ( "os" "github.com/google/uuid" - "github.com/thefeli73/polemos/pcsdk" + pcsdk "github.com/thefeli73/polemos/commands" "github.com/thefeli73/polemos/state" ) diff --git a/pcsdk/pcsdk.go b/commands/commands.go similarity index 100% rename from pcsdk/pcsdk.go rename to commands/commands.go diff --git a/pcsdk/pcsdk_test.go b/commands/commands_test.go similarity index 100% rename from pcsdk/pcsdk_test.go rename to commands/commands_test.go From e3e72de1f4dd589820d7b71bbaa27afb6f66131e Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 19 Apr 2023 14:00:41 +0200 Subject: [PATCH 4/8] Add tests for modify and delete --- commands/commands_test.go | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/commands/commands_test.go b/commands/commands_test.go index b14462e..611721c 100644 --- a/commands/commands_test.go +++ b/commands/commands_test.go @@ -12,8 +12,7 @@ import ( func TestCommandCreateJsonParse(t *testing.T) { ip, _ := netip.ParseAddr("127.0.0.99") uuid, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1") - c := NewCommandCreate(5555, 6666, ip, state.CustomUUID(uuid)) - m := ProxyCommandCreate{c} + m := NewCommandCreate(5555, 6666, ip, state.CustomUUID(uuid)) msg, err := json.Marshal(m) if err != nil { t.Fatalf(`%q`, err) @@ -26,4 +25,33 @@ func TestCommandCreateJsonParse(t *testing.T) { } } +func TestCommandModifyJsonParse(t *testing.T) { + ip, _ := netip.ParseAddr("127.0.0.99") + uuid, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1") + m := NewCommandModify(8888, ip, state.CustomUUID(uuid)) + msg, err := json.Marshal(m) + if err != nil { + t.Fatalf(`%q`, err) + } + expected := "{\"modify\":{\"destination_port\":8888,\"destination_ip\":\"127.0.0.99\",\"id\":\"87e79cbc-6df6-4462-8412-85d6c473e3b1\"}}" + if string(msg) != expected { + t.Fatalf( + "\nExpected:\t %q\nGot:\t\t %q\n", expected, msg) + } +} + +func TestCommandDeleteJsonParse(t *testing.T) { + uuid, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1") + m := NewCommandDelete(state.CustomUUID(uuid)) + msg, err := json.Marshal(m) + if err != nil { + t.Fatalf(`%q`, err) + } + + expected := "{\"delete\":{\"id\":\"87e79cbc-6df6-4462-8412-85d6c473e3b1\"}}" + if string(msg) != expected { + t.Fatalf( + "\nExpected:\t %q\nGot:\t\t %q\n", expected, msg) + } +} From b65e42304cc493472899c8d64d21cba0aa8d2c28 Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 19 Apr 2023 16:00:07 +0200 Subject: [PATCH 5/8] Add execute for create command --- cmd/examples/create/create.go | 41 ++++---------------------------- commands/commands.go | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 37 deletions(-) diff --git a/cmd/examples/create/create.go b/cmd/examples/create/create.go index c68db08..4a96771 100644 --- a/cmd/examples/create/create.go +++ b/cmd/examples/create/create.go @@ -1,13 +1,8 @@ package main import ( - "bytes" - "encoding/json" "fmt" - "io/ioutil" - "net/http" "net/netip" - "os" "github.com/google/uuid" pcsdk "github.com/thefeli73/polemos/commands" @@ -19,38 +14,10 @@ func main() { uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") m := pcsdk.NewCommandCreate(5555, 6666, ip, state.CustomUUID(uuid)) - data, err := json.Marshal(m) + err := m.Execute("http://localhost:3000") if err != nil { - fmt.Printf("client: could not serialize into JSON") - os.Exit(1) + fmt.Printf("error executing create command: %s\n", err) + } else { + fmt.Println("executing create command completed") } - - fmt.Printf(string(data)) - - requestURL := "http://localhost:3000/command" - bodyReader := bytes.NewReader(data) - req, err := http.NewRequest(http.MethodPost, requestURL, bodyReader) - - if err != nil { - fmt.Printf("client: could not create request: %s\n", err) - os.Exit(1) - } - - req.Header.Set("Content-Type", "application/json") - fmt.Println(req) - res, err := http.DefaultClient.Post(requestURL, "application/json", bodyReader) - if err != nil { - fmt.Printf("client: error making http request: %s\n", err) - os.Exit(1) - } - - fmt.Printf("client: got response!\n") - fmt.Printf("client: status code: %d\n", res.StatusCode) - - resBody, err := ioutil.ReadAll(res.Body) - if err != nil { - fmt.Printf("client: could not read response body: %s\n", err) - os.Exit(1) - } - fmt.Printf("client: response body: %s\n", resBody) } diff --git a/commands/commands.go b/commands/commands.go index 7825a4a..4f343b2 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -1,12 +1,26 @@ package pcsdk import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io/ioutil" + "net/http" "net/netip" "github.com/google/uuid" "github.com/thefeli73/polemos/state" ) +type ExecuteCommand interface { + Execute(string) error +} + +type response struct { + message string `json:"message"` +} + type ProxyCommandCreate struct { Command CommandCreate `json:"create"` // signature Signature @@ -19,6 +33,36 @@ type CommandCreate struct { Id string `json:"id"` } +func (c ProxyCommandCreate) Execute(url string) error { + data, err := json.Marshal(c) + if err != nil { + return errors.New(fmt.Sprintf("could not serialize: %s\n", err)) + } + + requestURL := fmt.Sprintf("%s/command", url) + + bodyReader := bytes.NewReader(data) + + res, err := http.DefaultClient.Post(requestURL, "application/json", bodyReader) + if err != nil { + return errors.New(fmt.Sprintf("error making http request: %s\n", err)) + } + + fmt.Println(res) + + body, err := ioutil.ReadAll(res.Body) + fmt.Println(string(body)) + if err != nil { + return errors.New(fmt.Sprintf("error reading response: %s\n", err)) + } + + if res.StatusCode != 202 { + return errors.New(fmt.Sprintf("error processing command: (%d) %s\n", res.StatusCode, body)) + } else { + return nil + } +} + func NewCommandCreate(iport uint16, oport uint16, oip netip.Addr, id state.CustomUUID) ProxyCommandCreate { c := CommandCreate{iport, oport, oip, uuid.UUID.String(uuid.UUID(id))} return ProxyCommandCreate{c} From d00d03b0cccdc76c06ce29c16a5d82b3cb46f9d2 Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 25 Apr 2023 12:54:21 +0200 Subject: [PATCH 6/8] Change directory back to pcsdk --- {commands => pcsdk}/commands.go | 0 {commands => pcsdk}/commands_test.go | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {commands => pcsdk}/commands.go (100%) rename {commands => pcsdk}/commands_test.go (100%) diff --git a/commands/commands.go b/pcsdk/commands.go similarity index 100% rename from commands/commands.go rename to pcsdk/commands.go diff --git a/commands/commands_test.go b/pcsdk/commands_test.go similarity index 100% rename from commands/commands_test.go rename to pcsdk/commands_test.go From a7ae121750bac95cc945508fc23f3f91e5a9fdcb Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 25 Apr 2023 12:55:00 +0200 Subject: [PATCH 7/8] Add Execute interface to pcsdk commands --- cmd/examples/create/create.go | 4 +-- cmd/examples/delete/delete.go | 43 +++---------------------- cmd/examples/modify/modify.go | 45 ++++---------------------- pcsdk/commands.go | 60 +++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 79 deletions(-) diff --git a/cmd/examples/create/create.go b/cmd/examples/create/create.go index 4a96771..2085941 100644 --- a/cmd/examples/create/create.go +++ b/cmd/examples/create/create.go @@ -5,7 +5,7 @@ import ( "net/netip" "github.com/google/uuid" - pcsdk "github.com/thefeli73/polemos/commands" + "github.com/thefeli73/polemos/pcsdk" "github.com/thefeli73/polemos/state" ) @@ -13,7 +13,7 @@ func main() { ip := netip.MustParseAddr("127.0.0.1") uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") - m := pcsdk.NewCommandCreate(5555, 6666, ip, state.CustomUUID(uuid)) + m := pcsdk.NewCommandCreate(5555, 8080, ip, state.CustomUUID(uuid)) err := m.Execute("http://localhost:3000") if err != nil { fmt.Printf("error executing create command: %s\n", err) diff --git a/cmd/examples/delete/delete.go b/cmd/examples/delete/delete.go index 2c8bd35..86bd1cf 100644 --- a/cmd/examples/delete/delete.go +++ b/cmd/examples/delete/delete.go @@ -1,15 +1,10 @@ package main import ( - "bytes" - "encoding/json" "fmt" - "io/ioutil" - "net/http" - "os" "github.com/google/uuid" - pcsdk "github.com/thefeli73/polemos/commands" + "github.com/thefeli73/polemos/pcsdk" "github.com/thefeli73/polemos/state" ) @@ -17,38 +12,10 @@ func main() { uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") m := pcsdk.NewCommandDelete(state.CustomUUID(uuid)) - data, err := json.Marshal(m) + err := m.Execute("http://localhost:3000") if err != nil { - fmt.Printf("client: could not serialize into JSON") - os.Exit(1) + fmt.Printf("error executing modify command: %s\n", err) + } else { + fmt.Println("executing modify command completed") } - - fmt.Printf(string(data)) - - requestURL := "http://localhost:3000/command" - bodyReader := bytes.NewReader(data) - req, err := http.NewRequest(http.MethodPost, requestURL, bodyReader) - - if err != nil { - fmt.Printf("client: could not create request: %s\n", err) - os.Exit(1) - } - - req.Header.Set("Content-Type", "application/json") - fmt.Println(req) - res, err := http.DefaultClient.Post(requestURL, "application/json", bodyReader) - if err != nil { - fmt.Printf("client: error making http request: %s\n", err) - os.Exit(1) - } - - fmt.Printf("client: got response!\n") - fmt.Printf("client: status code: %d\n", res.StatusCode) - - resBody, err := ioutil.ReadAll(res.Body) - if err != nil { - fmt.Printf("client: could not read response body: %s\n", err) - os.Exit(1) - } - fmt.Printf("client: response body: %s\n", resBody) } diff --git a/cmd/examples/modify/modify.go b/cmd/examples/modify/modify.go index 8aeb434..ac31b63 100644 --- a/cmd/examples/modify/modify.go +++ b/cmd/examples/modify/modify.go @@ -1,16 +1,11 @@ package main import ( - "bytes" - "encoding/json" "fmt" - "io/ioutil" - "net/http" "net/netip" - "os" "github.com/google/uuid" - pcsdk "github.com/thefeli73/polemos/commands" + "github.com/thefeli73/polemos/pcsdk" "github.com/thefeli73/polemos/state" ) @@ -18,39 +13,11 @@ func main() { ip := netip.MustParseAddr("127.0.0.1") uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") - m := pcsdk.NewCommandModify(8888, ip, state.CustomUUID(uuid)) - data, err := json.Marshal(m) + m := pcsdk.NewCommandModify(9111, ip, state.CustomUUID(uuid)) + err := m.Execute("http://localhost:3000") if err != nil { - fmt.Printf("client: could not serialize into JSON") - os.Exit(1) + fmt.Printf("error executing modify command: %s\n", err) + } else { + fmt.Println("executing modify command completed") } - - fmt.Printf(string(data)) - - requestURL := "http://localhost:3000/command" - bodyReader := bytes.NewReader(data) - req, err := http.NewRequest(http.MethodPost, requestURL, bodyReader) - - if err != nil { - fmt.Printf("client: could not create request: %s\n", err) - os.Exit(1) - } - - req.Header.Set("Content-Type", "application/json") - fmt.Println(req) - res, err := http.DefaultClient.Post(requestURL, "application/json", bodyReader) - if err != nil { - fmt.Printf("client: error making http request: %s\n", err) - os.Exit(1) - } - - fmt.Printf("client: got response!\n") - fmt.Printf("client: status code: %d\n", res.StatusCode) - - resBody, err := ioutil.ReadAll(res.Body) - if err != nil { - fmt.Printf("client: could not read response body: %s\n", err) - os.Exit(1) - } - fmt.Printf("client: response body: %s\n", resBody) } diff --git a/pcsdk/commands.go b/pcsdk/commands.go index 4f343b2..fe47030 100644 --- a/pcsdk/commands.go +++ b/pcsdk/commands.go @@ -78,6 +78,36 @@ type CommandModify struct { Id string `json:"id"` } +func (c ProxyCommandModify) Execute(url string) error { + data, err := json.Marshal(c) + if err != nil { + return errors.New(fmt.Sprintf("could not serialize: %s\n", err)) + } + + requestURL := fmt.Sprintf("%s/command", url) + + bodyReader := bytes.NewReader(data) + + res, err := http.DefaultClient.Post(requestURL, "application/json", bodyReader) + if err != nil { + return errors.New(fmt.Sprintf("error making http request: %s\n", err)) + } + + fmt.Println(res) + + body, err := ioutil.ReadAll(res.Body) + fmt.Println(string(body)) + if err != nil { + return errors.New(fmt.Sprintf("error reading response: %s\n", err)) + } + + if res.StatusCode != 202 { + return errors.New(fmt.Sprintf("error processing command: (%d) %s\n", res.StatusCode, body)) + } else { + return nil + } +} + func NewCommandModify(oport uint16, oip netip.Addr, id state.CustomUUID) ProxyCommandModify { c := CommandModify{oport, oip, uuid.UUID.String(uuid.UUID(id))} return ProxyCommandModify{c} @@ -91,6 +121,36 @@ type CommandDelete struct { Id string `json:"id"` } +func (c ProxyCommandDelete) Execute(url string) error { + data, err := json.Marshal(c) + if err != nil { + return errors.New(fmt.Sprintf("could not serialize: %s\n", err)) + } + + requestURL := fmt.Sprintf("%s/command", url) + + bodyReader := bytes.NewReader(data) + + res, err := http.DefaultClient.Post(requestURL, "application/json", bodyReader) + if err != nil { + return errors.New(fmt.Sprintf("error making http request: %s\n", err)) + } + + fmt.Println(res) + + body, err := ioutil.ReadAll(res.Body) + fmt.Println(string(body)) + if err != nil { + return errors.New(fmt.Sprintf("error reading response: %s\n", err)) + } + + if res.StatusCode != 202 { + return errors.New(fmt.Sprintf("error processing command: (%d) %s\n", res.StatusCode, body)) + } else { + return nil + } +} + func NewCommandDelete(id state.CustomUUID) ProxyCommandDelete { c := CommandDelete{uuid.UUID.String(uuid.UUID(id))} return ProxyCommandDelete{c} From 738432f0dfaa8ab52e8deb9fea987ec7bf0cd33d Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 25 Apr 2023 14:55:57 +0200 Subject: [PATCH 8/8] Change execute interface to use IP/Port pair --- cmd/examples/create/create.go | 2 +- cmd/examples/delete/delete.go | 7 ++++--- cmd/examples/modify/modify.go | 2 +- pcsdk/commands.go | 16 ++++++++-------- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/cmd/examples/create/create.go b/cmd/examples/create/create.go index 2085941..60ff6fb 100644 --- a/cmd/examples/create/create.go +++ b/cmd/examples/create/create.go @@ -14,7 +14,7 @@ func main() { uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") m := pcsdk.NewCommandCreate(5555, 8080, ip, state.CustomUUID(uuid)) - err := m.Execute("http://localhost:3000") + err := m.Execute(netip.MustParseAddrPort("127.0.0.1:3000")) if err != nil { fmt.Printf("error executing create command: %s\n", err) } else { diff --git a/cmd/examples/delete/delete.go b/cmd/examples/delete/delete.go index 86bd1cf..91fda1c 100644 --- a/cmd/examples/delete/delete.go +++ b/cmd/examples/delete/delete.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "net/netip" "github.com/google/uuid" "github.com/thefeli73/polemos/pcsdk" @@ -12,10 +13,10 @@ func main() { uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") m := pcsdk.NewCommandDelete(state.CustomUUID(uuid)) - err := m.Execute("http://localhost:3000") + err := m.Execute(netip.MustParseAddrPort("127.0.0.1:3000")) if err != nil { - fmt.Printf("error executing modify command: %s\n", err) + fmt.Printf("error executing delete command: %s\n", err) } else { - fmt.Println("executing modify command completed") + fmt.Println("executing delete command completed") } } diff --git a/cmd/examples/modify/modify.go b/cmd/examples/modify/modify.go index ac31b63..bb3ff3b 100644 --- a/cmd/examples/modify/modify.go +++ b/cmd/examples/modify/modify.go @@ -14,7 +14,7 @@ func main() { uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") m := pcsdk.NewCommandModify(9111, ip, state.CustomUUID(uuid)) - err := m.Execute("http://localhost:3000") + err := m.Execute(netip.MustParseAddrPort("127.0.0.1:3000")) if err != nil { fmt.Printf("error executing modify command: %s\n", err) } else { diff --git a/pcsdk/commands.go b/pcsdk/commands.go index fe47030..5073da8 100644 --- a/pcsdk/commands.go +++ b/pcsdk/commands.go @@ -14,7 +14,7 @@ import ( ) type ExecuteCommand interface { - Execute(string) error + Execute(netip.AddrPort) error } type response struct { @@ -33,14 +33,14 @@ type CommandCreate struct { Id string `json:"id"` } -func (c ProxyCommandCreate) Execute(url string) error { +func (c ProxyCommandCreate) Execute(url netip.AddrPort) error { data, err := json.Marshal(c) if err != nil { return errors.New(fmt.Sprintf("could not serialize: %s\n", err)) } - requestURL := fmt.Sprintf("%s/command", url) - + requestURL := fmt.Sprintf("http://%s:%d/command", url.Addr().String(), url.Port()) + fmt.Println(requestURL) bodyReader := bytes.NewReader(data) res, err := http.DefaultClient.Post(requestURL, "application/json", bodyReader) @@ -78,13 +78,13 @@ type CommandModify struct { Id string `json:"id"` } -func (c ProxyCommandModify) Execute(url string) error { +func (c ProxyCommandModify) Execute(url netip.AddrPort) error { data, err := json.Marshal(c) if err != nil { return errors.New(fmt.Sprintf("could not serialize: %s\n", err)) } - requestURL := fmt.Sprintf("%s/command", url) + requestURL := fmt.Sprintf("http://%s:%d/command", url.Addr().String(), url.Port()) bodyReader := bytes.NewReader(data) @@ -121,13 +121,13 @@ type CommandDelete struct { Id string `json:"id"` } -func (c ProxyCommandDelete) Execute(url string) error { +func (c ProxyCommandDelete) Execute(url netip.AddrPort) error { data, err := json.Marshal(c) if err != nil { return errors.New(fmt.Sprintf("could not serialize: %s\n", err)) } - requestURL := fmt.Sprintf("%s/command", url) + requestURL := fmt.Sprintf("http://%s:%d/command", url.Addr().String(), url.Port()) bodyReader := bytes.NewReader(data)