Merge pull request #6 from thefeli73/dry
Reduce repeated code in pcsdk module
This commit is contained in:
commit
268824fb91
@ -10,11 +10,11 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
proxy := pcsdk.BuildProxy(netip.MustParseAddrPort("127.0.0.1:14000"))
|
||||
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"))
|
||||
err := proxy.Create(5555, 8080, ip, state.CustomUUID(uuid))
|
||||
if err != nil {
|
||||
fmt.Printf("error executing create command: %s\n", err)
|
||||
} else {
|
||||
|
@ -10,10 +10,10 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
proxy := pcsdk.BuildProxy(netip.MustParseAddrPort("127.0.0.1:14000"))
|
||||
uuid := uuid.MustParse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
|
||||
|
||||
m := pcsdk.NewCommandDelete(state.CustomUUID(uuid))
|
||||
err := m.Execute(netip.MustParseAddrPort("127.0.0.1:3000"))
|
||||
err := proxy.Delete(state.CustomUUID(uuid))
|
||||
if err != nil {
|
||||
fmt.Printf("error executing delete command: %s\n", err)
|
||||
} else {
|
||||
|
@ -10,11 +10,11 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
proxy := pcsdk.BuildProxy(netip.MustParseAddrPort("127.0.0.1:14000"))
|
||||
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"))
|
||||
err := proxy.Modify(9111, ip, state.CustomUUID(uuid))
|
||||
if err != nil {
|
||||
fmt.Printf("error executing modify command: %s\n", err)
|
||||
} else {
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
|
||||
@ -13,132 +13,104 @@ import (
|
||||
"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 Proxy struct {
|
||||
signing_key string
|
||||
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))
|
||||
}
|
||||
body, err := io.ReadAll(res.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"`
|
||||
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())
|
||||
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))
|
||||
}
|
||||
body, err := ioutil.ReadAll(res.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 create(iport uint16, oport uint16, oip netip.Addr, id state.CustomUUID) command {
|
||||
cr:= commandCreate{iport, oport, oip, uuid.UUID.String(uuid.UUID(id))}
|
||||
c:= command{}
|
||||
c.Create = &cr
|
||||
return c
|
||||
}
|
||||
|
||||
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 {
|
||||
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))
|
||||
}
|
||||
body, err := ioutil.ReadAll(res.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 modify(oport uint16, oip netip.Addr, id state.CustomUUID) command {
|
||||
m:= commandModify{oport, oip, uuid.UUID.String(uuid.UUID(id))}
|
||||
c:= command{}
|
||||
c.Modify = &m
|
||||
return c
|
||||
}
|
||||
|
||||
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 {
|
||||
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))
|
||||
}
|
||||
body, err := ioutil.ReadAll(res.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}
|
||||
}
|
||||
func delete(id state.CustomUUID) command {
|
||||
d:= commandDelete{uuid.UUID.String(uuid.UUID(id))}
|
||||
c:= command{}
|
||||
c.Delete = &d
|
||||
return c
|
||||
}
|
@ -4,15 +4,16 @@ 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))
|
||||
id, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
|
||||
uuid := state.CustomUUID(id)
|
||||
m := create(5555, 6666, ip, uuid)
|
||||
t.Logf("%+v\n", m)
|
||||
msg, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
t.Fatalf(`%q`, err)
|
||||
@ -27,8 +28,9 @@ 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))
|
||||
id, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
|
||||
uuid := state.CustomUUID(id)
|
||||
m := modify(8888, ip, uuid)
|
||||
msg, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
t.Fatalf(`%q`, err)
|
||||
@ -42,8 +44,9 @@ func TestCommandModifyJsonParse(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCommandDeleteJsonParse(t *testing.T) {
|
||||
uuid, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
|
||||
m := NewCommandDelete(state.CustomUUID(uuid))
|
||||
id, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
|
||||
uuid := state.CustomUUID(id)
|
||||
m := delete(uuid)
|
||||
msg, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
t.Fatalf(`%q`, err)
|
||||
|
Loading…
Reference in New Issue
Block a user