diff --git a/cmd/examples/create/create.go b/cmd/examples/create/create.go new file mode 100644 index 0000000..60ff6fb --- /dev/null +++ b/cmd/examples/create/create.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "net/netip" + + "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, 8080, ip, state.CustomUUID(uuid)) + err := m.Execute(netip.MustParseAddrPort("127.0.0.1:3000")) + if err != nil { + fmt.Printf("error executing create command: %s\n", err) + } else { + fmt.Println("executing create command completed") + } +} diff --git a/cmd/examples/delete/delete.go b/cmd/examples/delete/delete.go new file mode 100644 index 0000000..91fda1c --- /dev/null +++ b/cmd/examples/delete/delete.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "net/netip" + + "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)) + err := m.Execute(netip.MustParseAddrPort("127.0.0.1:3000")) + if err != nil { + fmt.Printf("error executing delete command: %s\n", err) + } else { + fmt.Println("executing delete command completed") + } +} diff --git a/cmd/examples/modify/modify.go b/cmd/examples/modify/modify.go new file mode 100644 index 0000000..bb3ff3b --- /dev/null +++ b/cmd/examples/modify/modify.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "net/netip" + + "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(9111, ip, state.CustomUUID(uuid)) + err := m.Execute(netip.MustParseAddrPort("127.0.0.1:3000")) + if err != nil { + fmt.Printf("error executing modify command: %s\n", err) + } else { + fmt.Println("executing modify command completed") + } +} diff --git a/pcsdk/commands.go b/pcsdk/commands.go new file mode 100644 index 0000000..5073da8 --- /dev/null +++ b/pcsdk/commands.go @@ -0,0 +1,157 @@ +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(netip.AddrPort) error +} + +type response struct { + message string `json:"message"` +} + +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 (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("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) + 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} +} + +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 (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("http://%s:%d/command", url.Addr().String(), url.Port()) + + 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} +} + +type ProxyCommandDelete struct { + Command CommandDelete `json:"delete"` +} + +type CommandDelete struct { + Id string `json:"id"` +} + +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("http://%s:%d/command", url.Addr().String(), url.Port()) + + 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} +} diff --git a/pcsdk/commands_test.go b/pcsdk/commands_test.go new file mode 100644 index 0000000..611721c --- /dev/null +++ b/pcsdk/commands_test.go @@ -0,0 +1,57 @@ +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") + m := NewCommandCreate(5555, 6666, ip, state.CustomUUID(uuid)) + 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) + } +} + +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) + } +}