Compare commits

..

5 Commits

Author SHA1 Message Date
schulze
262ca61619 more explanitory variable names (also matches rest of code) 2023-05-03 14:13:06 +02:00
schulze
75e5e991cd golint warnings 2023-05-03 14:06:32 +02:00
schulze
5ca3917254 comments 2023-05-03 14:06:19 +02:00
schulze
cd284d1214 add basic command status (so program is able to run) 2023-05-03 14:01:27 +02:00
schulze
3b4c0f49d4 delete debug print 2023-05-03 14:00:52 +02:00

View File

@ -17,31 +17,42 @@ type response struct {
message string `json:"message"`
}
// Proxy owns the interfaces for the pcsdk.
type Proxy struct {
signing_key string
url netip.AddrPort
}
func BuildProxy(control netip.AddrPort) Proxy {
return Proxy {"", control}
// BuildProxy creates a proxy struct for the given url to easily interact with that proxy instance (create, edit, delete tunnels etc)
func BuildProxy(proxyIP netip.AddrPort) Proxy {
return Proxy {"", proxyIP}
}
func (p Proxy) Create(iport uint16, oport uint16, oip netip.Addr, id state.CustomUUID) error {
_, err := p.execute(create(iport, oport, oip, id))
// Create a tunnel with the given parameters.
func (p Proxy) Create(entryPort uint16, servicePort uint16, serviceIP netip.Addr, serviceUUID state.CustomUUID) error {
_, err := p.execute(create(entryPort, servicePort, serviceIP, serviceUUID))
return err
}
func (p Proxy) Modify(oport uint16, oip netip.Addr, id state.CustomUUID) error {
_, err := p.execute(modify(oport, oip, id))
// Modify a tunnel with the given parameters.
func (p Proxy) Modify(servicePort uint16, serviceIP netip.Addr, serviceUUID state.CustomUUID) error {
_, err := p.execute(modify(servicePort, serviceIP, serviceUUID))
return err
}
func (p Proxy) Delete(id state.CustomUUID) error {
_, err := p.execute(delete(id))
// Delete a tunnel with the given parameters.
func (p Proxy) Delete(serviceUUID state.CustomUUID) error {
_, err := p.execute(delete(serviceUUID))
return err
}
// TODO: status function returning map of tunnels
// Status returns a list of tunnels for the given proxy.
func (p Proxy) Status() error {
_, err := p.execute(status())
return err
}
func (p Proxy) execute(c command) (string, error) {
data, err := json.Marshal(c)
@ -50,7 +61,6 @@ func (p Proxy) execute(c command) (string, error) {
}
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)
@ -73,6 +83,7 @@ type command struct {
Create *commandCreate `json:"create,omitempty"`
Modify *commandModify `json:"modify,omitempty"`
Delete *commandDelete `json:"delete,omitempty"`
Status *commandStatus `json:"status,omitempty"`
Timestamp uint64 `json:"timestamp,omitempty"`
Signature string `json:"signature,omitempty"`
}
@ -81,11 +92,11 @@ type commandCreate struct {
IncomingPort uint16 `json:"incoming_port"`
DestinationPort uint16 `json:"destination_port"`
DestinationIP netip.Addr `json:"destination_ip"`
Id string `json:"id"`
ID string `json:"id"`
}
func create(iport uint16, oport uint16, oip netip.Addr, id state.CustomUUID) command {
cr:= commandCreate{iport, oport, oip, uuid.UUID.String(uuid.UUID(id))}
func create(entryPort uint16, servicePort uint16, serviceIP netip.Addr, serviceUUID state.CustomUUID) command {
cr:= commandCreate{entryPort, servicePort, serviceIP, uuid.UUID.String(uuid.UUID(serviceUUID))}
c:= command{}
c.Create = &cr
return c
@ -94,23 +105,34 @@ func create(iport uint16, oport uint16, oip netip.Addr, id state.CustomUUID) com
type commandModify struct {
DestinationPort uint16 `json:"destination_port"`
DestinationIP netip.Addr `json:"destination_ip"`
Id string `json:"id"`
ID string `json:"id"`
}
func modify(oport uint16, oip netip.Addr, id state.CustomUUID) command {
m:= commandModify{oport, oip, uuid.UUID.String(uuid.UUID(id))}
func modify(servicePort uint16, serviceIP netip.Addr, serviceUUID state.CustomUUID) command {
m:= commandModify{servicePort, serviceIP, uuid.UUID.String(uuid.UUID(serviceUUID))}
c:= command{}
c.Modify = &m
return c
}
type commandDelete struct {
Id string `json:"id"`
ID string `json:"id"`
}
func delete(id state.CustomUUID) command {
d:= commandDelete{uuid.UUID.String(uuid.UUID(id))}
func delete(serviceUUID state.CustomUUID) command {
d:= commandDelete{uuid.UUID.String(uuid.UUID(serviceUUID))}
c:= command{}
c.Delete = &d
return c
}
}
type commandStatus struct {
}
func status() command {
d:= commandStatus{}
c:= command{}
c.Status = &d
return c
}