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