From 8bec57d0349b48dbe27e0f4882181b52dc00ac1a Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 19 Apr 2023 13:17:33 +0200 Subject: [PATCH] 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} }