2023-04-18 15:28:55 +02:00
|
|
|
package pcsdk
|
|
|
|
|
|
|
|
import (
|
2023-04-19 16:00:07 +02:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"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 ExecuteCommand interface {
|
2023-04-25 14:55:57 +02:00
|
|
|
Execute(netip.AddrPort) error
|
2023-04-19 16:00:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2023-04-18 15:28:55 +02:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2023-04-25 14:55:57 +02:00
|
|
|
func (c ProxyCommandCreate) Execute(url netip.AddrPort) error {
|
2023-04-19 16:00:07 +02:00
|
|
|
data, err := json.Marshal(c)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("could not serialize: %s\n", err))
|
|
|
|
}
|
|
|
|
|
2023-04-25 14:55:57 +02:00
|
|
|
requestURL := fmt.Sprintf("http://%s:%d/command", url.Addr().String(), url.Port())
|
|
|
|
fmt.Println(requestURL)
|
2023-04-19 16:00:07 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 13:17:33 +02:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2023-04-25 14:55:57 +02:00
|
|
|
func (c ProxyCommandModify) Execute(url netip.AddrPort) error {
|
2023-04-25 12:55:00 +02:00
|
|
|
data, err := json.Marshal(c)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("could not serialize: %s\n", err))
|
|
|
|
}
|
|
|
|
|
2023-04-25 14:55:57 +02:00
|
|
|
requestURL := fmt.Sprintf("http://%s:%d/command", url.Addr().String(), 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 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 13:17:33 +02:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2023-04-25 14:55:57 +02:00
|
|
|
func (c ProxyCommandDelete) Execute(url netip.AddrPort) error {
|
2023-04-25 12:55:00 +02:00
|
|
|
data, err := json.Marshal(c)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("could not serialize: %s\n", err))
|
|
|
|
}
|
|
|
|
|
2023-04-25 14:55:57 +02:00
|
|
|
requestURL := fmt.Sprintf("http://%s:%d/command", url.Addr().String(), 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 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 13:17:33 +02:00
|
|
|
func NewCommandDelete(id state.CustomUUID) ProxyCommandDelete {
|
|
|
|
c := CommandDelete{uuid.UUID.String(uuid.UUID(id))}
|
|
|
|
return ProxyCommandDelete{c}
|
2023-04-18 15:28:55 +02:00
|
|
|
}
|