Reduce repeated code in pcsdk module

This commit is contained in:
Erik 2023-05-02 15:27:43 +02:00
parent 29e45d58e1
commit f6cb03e2da
5 changed files with 98 additions and 132 deletions

View File

@ -10,11 +10,11 @@ import (
) )
func main() { func main() {
proxy := pcsdk.BuildProxy(netip.MustParseAddrPort("127.0.0.1:14000"))
ip := netip.MustParseAddr("127.0.0.1") ip := netip.MustParseAddr("127.0.0.1")
uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
m := pcsdk.NewCommandCreate(5555, 8080, ip, state.CustomUUID(uuid)) err := proxy.Create(5555, 8080, ip, state.CustomUUID(uuid))
err := m.Execute(netip.MustParseAddrPort("127.0.0.1:3000"))
if err != nil { if err != nil {
fmt.Printf("error executing create command: %s\n", err) fmt.Printf("error executing create command: %s\n", err)
} else { } else {

View File

@ -10,10 +10,10 @@ import (
) )
func main() { func main() {
proxy := pcsdk.BuildProxy(netip.MustParseAddrPort("127.0.0.1:14000"))
uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
m := pcsdk.NewCommandDelete(state.CustomUUID(uuid)) err := proxy.Delete(state.CustomUUID(uuid))
err := m.Execute(netip.MustParseAddrPort("127.0.0.1:3000"))
if err != nil { if err != nil {
fmt.Printf("error executing delete command: %s\n", err) fmt.Printf("error executing delete command: %s\n", err)
} else { } else {

View File

@ -10,11 +10,11 @@ import (
) )
func main() { func main() {
proxy := pcsdk.BuildProxy(netip.MustParseAddrPort("127.0.0.1:14000"))
ip := netip.MustParseAddr("127.0.0.1") ip := netip.MustParseAddr("127.0.0.1")
uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1") uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
m := pcsdk.NewCommandModify(9111, ip, state.CustomUUID(uuid)) err := proxy.Modify(9111, ip, state.CustomUUID(uuid))
err := m.Execute(netip.MustParseAddrPort("127.0.0.1:3000"))
if err != nil { if err != nil {
fmt.Printf("error executing modify command: %s\n", err) fmt.Printf("error executing modify command: %s\n", err)
} else { } else {

View File

@ -5,7 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/netip" "net/netip"
@ -13,145 +13,108 @@ import (
"github.com/thefeli73/polemos/state" "github.com/thefeli73/polemos/state"
) )
type ExecuteCommand interface {
Execute(netip.AddrPort) error
}
type response struct { type response struct {
message string `json:"message"` message string `json:"message"`
} }
type ProxyCommandCreate struct { type Proxy struct {
Command CommandCreate `json:"create"` signing_key string
// signature Signature url netip.AddrPort
} }
type CommandCreate struct { func BuildProxy(control netip.AddrPort) Proxy {
return Proxy {"", control}
}
func (p Proxy) Create(iport uint16, oport uint16, oip netip.Addr, id state.CustomUUID) error {
_, err := p.execute(create(iport, oport, oip, id))
return err
}
func (p Proxy) Modify(oport uint16, oip netip.Addr, id state.CustomUUID) error {
_, err := p.execute(modify(oport, oip, id))
return err
}
func (p Proxy) Delete(id state.CustomUUID) error {
_, err := p.execute(delete(id))
return err
}
// TODO: status function returning map of tunnels
func (p Proxy) execute(c command) (string, 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", p.url.Addr().String(), p.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 := io.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 && res.StatusCode != 200 {
return "", errors.New(fmt.Sprintf("error processing command: (%d) %s\n", res.StatusCode, body))
} else {
return string(body), nil
}
}
type command struct {
Create *commandCreate `json:"create,omitempty"`
Modify *commandModify `json:"modify,omitempty"`
Delete *commandDelete `json:"delete,omitempty"`
Timestamp uint64 `json:"timestamp,omitempty"`
Signature string `json:"signature,omitempty"`
}
type commandCreate struct {
IncomingPort uint16 `json:"incoming_port"` IncomingPort uint16 `json:"incoming_port"`
DestinationPort uint16 `json:"destination_port"` DestinationPort uint16 `json:"destination_port"`
DestinationIP netip.Addr `json:"destination_ip"` DestinationIP netip.Addr `json:"destination_ip"`
Id string `json:"id"` Id string `json:"id"`
} }
func (c ProxyCommandCreate) Execute(url netip.AddrPort) error { func create(iport uint16, oport uint16, oip netip.Addr, id state.CustomUUID) command {
data, err := json.Marshal(c) cr:= commandCreate{iport, oport, oip, uuid.UUID.String(uuid.UUID(id))}
if err != nil { c:= command{}
return errors.New(fmt.Sprintf("could not serialize: %s\n", err)) c.Create = &cr
} return c
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 { type commandModify struct {
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"` DestinationPort uint16 `json:"destination_port"`
DestinationIP netip.Addr `json:"destination_ip"` DestinationIP netip.Addr `json:"destination_ip"`
Id string `json:"id"` Id string `json:"id"`
} }
func (c ProxyCommandModify) Execute(url netip.AddrPort) error { func modify(oport uint16, oip netip.Addr, id state.CustomUUID) command {
data, err := json.Marshal(c) m:= commandModify{oport, oip, uuid.UUID.String(uuid.UUID(id))}
if err != nil { c:= command{}
return errors.New(fmt.Sprintf("could not serialize: %s\n", err)) c.Modify = &m
} return c
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 { type commandDelete struct {
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"` Id string `json:"id"`
} }
func (c ProxyCommandDelete) Execute(url netip.AddrPort) error { func delete(id state.CustomUUID) command {
data, err := json.Marshal(c) d:= commandDelete{uuid.UUID.String(uuid.UUID(id))}
if err != nil { c:= command{}
return errors.New(fmt.Sprintf("could not serialize: %s\n", err)) c.Delete = &d
} return c
}
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}
}

View File

@ -4,15 +4,16 @@ import (
"encoding/json" "encoding/json"
"net/netip" "net/netip"
"testing" "testing"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/thefeli73/polemos/state" "github.com/thefeli73/polemos/state"
) )
func TestCommandCreateJsonParse(t *testing.T) { func TestCommandCreateJsonParse(t *testing.T) {
ip, _ := netip.ParseAddr("127.0.0.99") ip, _ := netip.ParseAddr("127.0.0.99")
uuid, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1") id, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
m := NewCommandCreate(5555, 6666, ip, state.CustomUUID(uuid)) uuid := state.CustomUUID(id)
m := create(5555, 6666, ip, uuid)
t.Logf("%+v\n", m)
msg, err := json.Marshal(m) msg, err := json.Marshal(m)
if err != nil { if err != nil {
t.Fatalf(`%q`, err) t.Fatalf(`%q`, err)
@ -27,8 +28,9 @@ func TestCommandCreateJsonParse(t *testing.T) {
func TestCommandModifyJsonParse(t *testing.T) { func TestCommandModifyJsonParse(t *testing.T) {
ip, _ := netip.ParseAddr("127.0.0.99") ip, _ := netip.ParseAddr("127.0.0.99")
uuid, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1") id, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
m := NewCommandModify(8888, ip, state.CustomUUID(uuid)) uuid := state.CustomUUID(id)
m := modify(8888, ip, uuid)
msg, err := json.Marshal(m) msg, err := json.Marshal(m)
if err != nil { if err != nil {
t.Fatalf(`%q`, err) t.Fatalf(`%q`, err)
@ -42,8 +44,9 @@ func TestCommandModifyJsonParse(t *testing.T) {
} }
func TestCommandDeleteJsonParse(t *testing.T) { func TestCommandDeleteJsonParse(t *testing.T) {
uuid, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1") id, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
m := NewCommandDelete(state.CustomUUID(uuid)) uuid := state.CustomUUID(id)
m := delete(uuid)
msg, err := json.Marshal(m) msg, err := json.Marshal(m)
if err != nil { if err != nil {
t.Fatalf(`%q`, err) t.Fatalf(`%q`, err)