2023-04-18 15:28:55 +02:00
|
|
|
package pcsdk
|
|
|
|
|
|
|
|
import (
|
2023-04-19 16:00:07 +02:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-05-02 15:27:43 +02:00
|
|
|
"io"
|
2023-04-19 16:00:07 +02:00
|
|
|
"net/http"
|
2023-04-18 15:28:55 +02:00
|
|
|
"net/netip"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/thefeli73/polemos/state"
|
|
|
|
)
|
|
|
|
|
2023-04-19 16:00:07 +02:00
|
|
|
type response struct {
|
|
|
|
message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2023-05-03 14:06:19 +02:00
|
|
|
// Proxy owns the interfaces for the pcsdk.
|
2023-05-02 15:27:43 +02:00
|
|
|
type Proxy struct {
|
|
|
|
signing_key string
|
|
|
|
url netip.AddrPort
|
2023-04-18 15:28:55 +02:00
|
|
|
}
|
|
|
|
|
2023-05-03 14:06:19 +02:00
|
|
|
// BuildProxy creates a proxy struct for the given url to easily interact with that proxy instance (create, edit, delete tunnels etc)
|
2023-05-02 15:27:43 +02:00
|
|
|
func BuildProxy(control netip.AddrPort) Proxy {
|
|
|
|
return Proxy {"", control}
|
2023-04-18 15:28:55 +02:00
|
|
|
}
|
|
|
|
|
2023-05-03 14:06:19 +02:00
|
|
|
// Create a tunnel with the given parameters.
|
2023-05-02 15:27:43 +02:00
|
|
|
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
|
2023-04-19 16:00:07 +02:00
|
|
|
}
|
|
|
|
|
2023-05-03 14:06:19 +02:00
|
|
|
// Modify a tunnel with the given parameters.
|
2023-05-02 15:27:43 +02:00
|
|
|
func (p Proxy) Modify(oport uint16, oip netip.Addr, id state.CustomUUID) error {
|
|
|
|
_, err := p.execute(modify(oport, oip, id))
|
|
|
|
return err
|
2023-04-19 13:17:33 +02:00
|
|
|
}
|
|
|
|
|
2023-05-03 14:06:19 +02:00
|
|
|
// Delete a tunnel with the given parameters.
|
2023-05-02 15:27:43 +02:00
|
|
|
func (p Proxy) Delete(id state.CustomUUID) error {
|
|
|
|
_, err := p.execute(delete(id))
|
|
|
|
return err
|
2023-04-19 13:17:33 +02:00
|
|
|
}
|
|
|
|
|
2023-05-03 14:06:19 +02:00
|
|
|
// TODO: status function returning map of tunnels
|
|
|
|
// Status returns a list of tunnels for the given proxy.
|
2023-05-03 14:01:27 +02:00
|
|
|
func (p Proxy) Status() error {
|
|
|
|
_, err := p.execute(status())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-19 13:17:33 +02:00
|
|
|
|
2023-05-02 15:27:43 +02:00
|
|
|
func (p Proxy) execute(c command) (string, error) {
|
2023-04-25 12:55:00 +02:00
|
|
|
data, err := json.Marshal(c)
|
|
|
|
if err != nil {
|
2023-05-02 15:27:43 +02:00
|
|
|
return "", errors.New(fmt.Sprintf("could not serialize: %s\n", err))
|
2023-04-25 12:55:00 +02:00
|
|
|
}
|
|
|
|
|
2023-05-02 15:27:43 +02:00
|
|
|
requestURL := fmt.Sprintf("http://%s:%d/command", p.url.Addr().String(), p.url.Port())
|
2023-04-25 12:55:00 +02:00
|
|
|
bodyReader := bytes.NewReader(data)
|
|
|
|
|
|
|
|
res, err := http.DefaultClient.Post(requestURL, "application/json", bodyReader)
|
|
|
|
if err != nil {
|
2023-05-02 15:27:43 +02:00
|
|
|
return "", errors.New(fmt.Sprintf("error making http request: %s\n", err))
|
2023-04-25 12:55:00 +02:00
|
|
|
}
|
2023-05-02 16:12:51 +02:00
|
|
|
body, err := io.ReadAll(res.Body)
|
2023-04-25 12:55:00 +02:00
|
|
|
if err != nil {
|
2023-05-02 15:27:43 +02:00
|
|
|
return "", errors.New(fmt.Sprintf("error reading response: %s\n", err))
|
2023-04-25 12:55:00 +02:00
|
|
|
}
|
|
|
|
|
2023-05-02 15:27:43 +02:00
|
|
|
if res.StatusCode != 202 && res.StatusCode != 200 {
|
|
|
|
return "", errors.New(fmt.Sprintf("error processing command: (%d) %s\n", res.StatusCode, body))
|
2023-04-25 12:55:00 +02:00
|
|
|
} else {
|
2023-05-02 15:27:43 +02:00
|
|
|
return string(body), nil
|
2023-04-25 12:55:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 15:27:43 +02:00
|
|
|
type command struct {
|
|
|
|
Create *commandCreate `json:"create,omitempty"`
|
|
|
|
Modify *commandModify `json:"modify,omitempty"`
|
|
|
|
Delete *commandDelete `json:"delete,omitempty"`
|
2023-05-03 14:01:27 +02:00
|
|
|
Status *commandStatus `json:"status,omitempty"`
|
2023-05-02 15:27:43 +02:00
|
|
|
Timestamp uint64 `json:"timestamp,omitempty"`
|
|
|
|
Signature string `json:"signature,omitempty"`
|
2023-04-19 13:17:33 +02:00
|
|
|
}
|
|
|
|
|
2023-05-02 15:27:43 +02:00
|
|
|
type commandCreate struct {
|
|
|
|
IncomingPort uint16 `json:"incoming_port"`
|
|
|
|
DestinationPort uint16 `json:"destination_port"`
|
|
|
|
DestinationIP netip.Addr `json:"destination_ip"`
|
2023-05-03 14:06:32 +02:00
|
|
|
ID string `json:"id"`
|
2023-04-19 13:17:33 +02:00
|
|
|
}
|
|
|
|
|
2023-05-02 15:27:43 +02:00
|
|
|
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
|
2023-04-19 13:17:33 +02:00
|
|
|
}
|
|
|
|
|
2023-05-02 15:27:43 +02:00
|
|
|
type commandModify struct {
|
|
|
|
DestinationPort uint16 `json:"destination_port"`
|
|
|
|
DestinationIP netip.Addr `json:"destination_ip"`
|
2023-05-03 14:06:32 +02:00
|
|
|
ID string `json:"id"`
|
2023-05-02 15:27:43 +02:00
|
|
|
}
|
2023-04-25 12:55:00 +02:00
|
|
|
|
2023-05-02 15:27:43 +02:00
|
|
|
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
|
2023-04-25 12:55:00 +02:00
|
|
|
}
|
|
|
|
|
2023-05-02 15:27:43 +02:00
|
|
|
type commandDelete struct {
|
2023-05-03 14:06:32 +02:00
|
|
|
ID string `json:"id"`
|
2023-04-18 15:28:55 +02:00
|
|
|
}
|
2023-05-02 15:27:43 +02:00
|
|
|
|
|
|
|
func delete(id state.CustomUUID) command {
|
|
|
|
d:= commandDelete{uuid.UUID.String(uuid.UUID(id))}
|
|
|
|
c:= command{}
|
|
|
|
c.Delete = &d
|
|
|
|
return c
|
2023-05-03 14:01:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type commandStatus struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func status() command {
|
|
|
|
d:= commandStatus{}
|
|
|
|
c:= command{}
|
|
|
|
c.Status = &d
|
|
|
|
return c
|
|
|
|
}
|