chore: Strip unneeded components from code.

This commit is contained in:
Tera << 8 2025-02-16 19:12:17 -05:00
parent 62cc8b39ad
commit cf90ddb104
Signed by: imterah
GPG key ID: 8FA7DD57BA6CEA37
19 changed files with 228 additions and 762 deletions

View file

@ -15,8 +15,8 @@ import (
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
) )
func handleCommand(commandType string, command interface{}, sock net.Conn, rtcChan chan interface{}) error { func handleCommand(command interface{}, sock net.Conn, rtcChan chan interface{}) error {
bytes, err := commonbackend.Marshal(commandType, command) bytes, err := commonbackend.Marshal(command)
if err != nil { if err != nil {
log.Warnf("Failed to marshal message: %s", err.Error()) log.Warnf("Failed to marshal message: %s", err.Error())
@ -32,7 +32,7 @@ func handleCommand(commandType string, command interface{}, sock net.Conn, rtcCh
return fmt.Errorf("failed to write message: %s", err.Error()) return fmt.Errorf("failed to write message: %s", err.Error())
} }
_, data, err := commonbackend.Unmarshal(sock) data, err := commonbackend.Unmarshal(sock)
if err != nil { if err != nil {
log.Warnf("Failed to unmarshal message: %s", err.Error()) log.Warnf("Failed to unmarshal message: %s", err.Error())
@ -117,9 +117,7 @@ func (runtime *Runtime) goRoutineHandler() error {
// To be safe here, we have to use the proper (yet annoying) facilities to prevent cross-talk, since we're in // To be safe here, we have to use the proper (yet annoying) facilities to prevent cross-talk, since we're in
// a goroutine, and can't talk directly. This actually has benefits, as the OuterLoop should exit on its own, if we // a goroutine, and can't talk directly. This actually has benefits, as the OuterLoop should exit on its own, if we
// encounter a critical error. // encounter a critical error.
statusResponse, err := runtime.ProcessCommand(&commonbackend.BackendStatusRequest{ statusResponse, err := runtime.ProcessCommand(&commonbackend.BackendStatusRequest{})
Type: "backendStatusRequest",
})
if err != nil { if err != nil {
log.Warnf("Failed to get response for backend (in backend runtime keep alive): %s", err.Error()) log.Warnf("Failed to get response for backend (in backend runtime keep alive): %s", err.Error())
@ -167,9 +165,7 @@ func (runtime *Runtime) goRoutineHandler() error {
continue continue
} }
switch command := messageData.Message.(type) { err := handleCommand(messageData.Message, sock, messageData.Channel)
case *commonbackend.AddProxy:
err := handleCommand("addProxy", command, sock, messageData.Channel)
if err != nil { if err != nil {
log.Warnf("failed to handle command in backend runtime instance: %s", err.Error()) log.Warnf("failed to handle command in backend runtime instance: %s", err.Error())
@ -178,100 +174,6 @@ func (runtime *Runtime) goRoutineHandler() error {
break OuterLoop break OuterLoop
} }
} }
case *commonbackend.BackendStatusRequest:
err := handleCommand("backendStatusRequest", command, sock, messageData.Channel)
if err != nil {
log.Warnf("failed to handle command in backend runtime instance: %s", err.Error())
if strings.HasPrefix(err.Error(), "failed to write message") {
break OuterLoop
}
}
case *commonbackend.CheckClientParameters:
err := handleCommand("checkClientParameters", command, sock, messageData.Channel)
if err != nil {
log.Warnf("failed to handle command in backend runtime instance: %s", err.Error())
if strings.HasPrefix(err.Error(), "failed to write message") {
break OuterLoop
}
}
case *commonbackend.CheckServerParameters:
err := handleCommand("checkServerParameters", command, sock, messageData.Channel)
if err != nil {
log.Warnf("failed to handle command in backend runtime instance: %s", err.Error())
if strings.HasPrefix(err.Error(), "failed to write message") {
break OuterLoop
}
}
case *commonbackend.ProxyConnectionsRequest:
err := handleCommand("proxyConnectionsRequest", command, sock, messageData.Channel)
if err != nil {
log.Warnf("failed to handle command in backend runtime instance: %s", err.Error())
if strings.HasPrefix(err.Error(), "failed to write message") {
break OuterLoop
}
}
case *commonbackend.ProxyInstanceRequest:
err := handleCommand("proxyInstanceRequest", command, sock, messageData.Channel)
if err != nil {
log.Warnf("failed to handle command in backend runtime instance: %s", err.Error())
if strings.HasPrefix(err.Error(), "failed to write message") {
break OuterLoop
}
}
case *commonbackend.ProxyStatusRequest:
err := handleCommand("proxyStatusRequest", command, sock, messageData.Channel)
if err != nil {
log.Warnf("failed to handle command in backend runtime instance: %s", err.Error())
if strings.HasPrefix(err.Error(), "failed to write message") {
break OuterLoop
}
}
case *commonbackend.RemoveProxy:
err := handleCommand("removeProxy", command, sock, messageData.Channel)
if err != nil {
log.Warnf("failed to handle command in backend runtime instance: %s", err.Error())
if strings.HasPrefix(err.Error(), "failed to write message") {
break OuterLoop
}
}
case *commonbackend.Start:
err := handleCommand("start", command, sock, messageData.Channel)
if err != nil {
log.Warnf("failed to handle command in backend runtime instance: %s", err.Error())
if strings.HasPrefix(err.Error(), "failed to write message") {
break OuterLoop
}
}
case *commonbackend.Stop:
err := handleCommand("stop", command, sock, messageData.Channel)
if err != nil {
log.Warnf("failed to handle command in backend runtime instance: %s", err.Error())
if strings.HasPrefix(err.Error(), "failed to write message") {
break OuterLoop
}
}
default:
log.Warnf("Recieved unknown command type from channel: %T", command)
messageData.Channel <- fmt.Errorf("unknown command recieved")
}
runtime.messageBuffer[chanIndex] = nil runtime.messageBuffer[chanIndex] = nil
} }

View file

@ -126,7 +126,6 @@ func CreateBackend(c *gin.Context) {
} }
backendParamCheckResponse, err := backend.ProcessCommand(&commonbackend.CheckServerParameters{ backendParamCheckResponse, err := backend.ProcessCommand(&commonbackend.CheckServerParameters{
Type: "checkServerParameters",
Arguments: backendParameters, Arguments: backendParameters,
}) })
@ -216,7 +215,6 @@ func CreateBackend(c *gin.Context) {
} }
backendStartResponse, err := backend.ProcessCommand(&commonbackend.Start{ backendStartResponse, err := backend.ProcessCommand(&commonbackend.Start{
Type: "start",
Arguments: backendParameters, Arguments: backendParameters,
}) })

View file

@ -118,9 +118,7 @@ func GetConnections(c *gin.Context) {
return return
} }
backendResponse, err := backendRuntime.ProcessCommand(&commonbackend.ProxyConnectionsRequest{ backendResponse, err := backendRuntime.ProcessCommand(&commonbackend.ProxyConnectionsRequest{})
Type: "proxyConnectionsRequest",
})
if err != nil { if err != nil {
log.Warnf("Failed to get response for backend: %s", err.Error()) log.Warnf("Failed to get response for backend: %s", err.Error())

View file

@ -142,7 +142,6 @@ func CreateProxy(c *gin.Context) {
} }
backendResponse, err := backend.ProcessCommand(&commonbackend.AddProxy{ backendResponse, err := backend.ProcessCommand(&commonbackend.AddProxy{
Type: "addProxy",
SourceIP: proxy.SourceIP, SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort, SourcePort: proxy.SourcePort,
DestPort: proxy.DestinationPort, DestPort: proxy.DestinationPort,

View file

@ -111,7 +111,6 @@ func RemoveProxy(c *gin.Context) {
} }
backendResponse, err := backend.ProcessCommand(&commonbackend.RemoveProxy{ backendResponse, err := backend.ProcessCommand(&commonbackend.RemoveProxy{
Type: "removeProxy",
SourceIP: proxy.SourceIP, SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort, SourcePort: proxy.SourcePort,
DestPort: proxy.DestinationPort, DestPort: proxy.DestinationPort,

View file

@ -101,7 +101,6 @@ func StartProxy(c *gin.Context) {
} }
backendResponse, err := backend.ProcessCommand(&commonbackend.AddProxy{ backendResponse, err := backend.ProcessCommand(&commonbackend.AddProxy{
Type: "addProxy",
SourceIP: proxy.SourceIP, SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort, SourcePort: proxy.SourcePort,
DestPort: proxy.DestinationPort, DestPort: proxy.DestinationPort,

View file

@ -101,7 +101,6 @@ func StopProxy(c *gin.Context) {
} }
backendResponse, err := backend.ProcessCommand(&commonbackend.RemoveProxy{ backendResponse, err := backend.ProcessCommand(&commonbackend.RemoveProxy{
Type: "removeProxy",
SourceIP: proxy.SourceIP, SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort, SourcePort: proxy.SourcePort,
DestPort: proxy.DestinationPort, DestPort: proxy.DestinationPort,

View file

@ -108,8 +108,7 @@ func apiEntrypoint(cCtx *cli.Context) error {
return return
} }
marshalledStartCommand, err := commonbackend.Marshal("start", &commonbackend.Start{ marshalledStartCommand, err := commonbackend.Marshal(&commonbackend.Start{
Type: "start",
Arguments: backendParameters, Arguments: backendParameters,
}) })
@ -123,7 +122,7 @@ func apiEntrypoint(cCtx *cli.Context) error {
return return
} }
_, backendResponse, err := commonbackend.Unmarshal(conn) backendResponse, err := commonbackend.Unmarshal(conn)
if err != nil { if err != nil {
log.Errorf("Failed to get start command response for backend #%d: %s", backend.ID, err.Error()) log.Errorf("Failed to get start command response for backend #%d: %s", backend.ID, err.Error())
@ -152,8 +151,7 @@ func apiEntrypoint(cCtx *cli.Context) error {
for _, proxy := range autoStartProxies { for _, proxy := range autoStartProxies {
log.Infof("Starting up route #%d for backend #%d: %s", proxy.ID, backend.ID, proxy.Name) log.Infof("Starting up route #%d for backend #%d: %s", proxy.ID, backend.ID, proxy.Name)
marhalledCommand, err := commonbackend.Marshal("addProxy", &commonbackend.AddProxy{ marhalledCommand, err := commonbackend.Marshal(&commonbackend.AddProxy{
Type: "addProxy",
SourceIP: proxy.SourceIP, SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort, SourcePort: proxy.SourcePort,
DestPort: proxy.DestinationPort, DestPort: proxy.DestinationPort,
@ -170,7 +168,7 @@ func apiEntrypoint(cCtx *cli.Context) error {
continue continue
} }
_, backendResponse, err := commonbackend.Unmarshal(conn) backendResponse, err := commonbackend.Unmarshal(conn)
if err != nil { if err != nil {
log.Errorf("Failed to get response for backend #%d and route #%d: %s", proxy.BackendID, proxy.ID, err.Error()) log.Errorf("Failed to get response for backend #%d and route #%d: %s", proxy.BackendID, proxy.ID, err.Error())
@ -204,7 +202,6 @@ func apiEntrypoint(cCtx *cli.Context) error {
} }
backendStartResponse, err := backendInstance.ProcessCommand(&commonbackend.Start{ backendStartResponse, err := backendInstance.ProcessCommand(&commonbackend.Start{
Type: "start",
Arguments: backendParameters, Arguments: backendParameters,
}) })
@ -257,7 +254,6 @@ func apiEntrypoint(cCtx *cli.Context) error {
log.Infof("Starting up route #%d for backend #%d: %s", proxy.ID, backend.ID, proxy.Name) log.Infof("Starting up route #%d for backend #%d: %s", proxy.ID, backend.ID, proxy.Name)
backendResponse, err := backendInstance.ProcessCommand(&commonbackend.AddProxy{ backendResponse, err := backendInstance.ProcessCommand(&commonbackend.AddProxy{
Type: "addProxy",
SourceIP: proxy.SourceIP, SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort, SourcePort: proxy.SourcePort,
DestPort: proxy.DestinationPort, DestPort: proxy.DestinationPort,

View file

@ -34,7 +34,7 @@ func (helper *BackendApplicationHelper) Start() error {
log.Debug("Sucessfully connected") log.Debug("Sucessfully connected")
for { for {
_, commandRaw, err := commonbackend.Unmarshal(helper.socket) commandRaw, err := commonbackend.Unmarshal(helper.socket)
if err != nil { if err != nil {
return err return err
@ -57,13 +57,12 @@ func (helper *BackendApplicationHelper) Start() error {
} }
response := &commonbackend.BackendStatusResponse{ response := &commonbackend.BackendStatusResponse{
Type: "backendStatusResponse",
IsRunning: ok, IsRunning: ok,
StatusCode: statusCode, StatusCode: statusCode,
Message: message, Message: message,
} }
responseMarshalled, err := commonbackend.Marshal(response.Type, response) responseMarshalled, err := commonbackend.Marshal(response)
if err != nil { if err != nil {
log.Error("failed to marshal response: %s", err.Error()) log.Error("failed to marshal response: %s", err.Error())
@ -87,13 +86,12 @@ func (helper *BackendApplicationHelper) Start() error {
} }
response := &commonbackend.BackendStatusResponse{ response := &commonbackend.BackendStatusResponse{
Type: "backendStatusResponse",
IsRunning: ok, IsRunning: ok,
StatusCode: statusCode, StatusCode: statusCode,
Message: message, Message: message,
} }
responseMarshalled, err := commonbackend.Marshal(response.Type, response) responseMarshalled, err := commonbackend.Marshal(response)
if err != nil { if err != nil {
log.Error("failed to marshal response: %s", err.Error()) log.Error("failed to marshal response: %s", err.Error())
@ -117,13 +115,12 @@ func (helper *BackendApplicationHelper) Start() error {
} }
response := &commonbackend.BackendStatusResponse{ response := &commonbackend.BackendStatusResponse{
Type: "backendStatusResponse",
IsRunning: !ok, IsRunning: !ok,
StatusCode: statusCode, StatusCode: statusCode,
Message: message, Message: message,
} }
responseMarshalled, err := commonbackend.Marshal(response.Type, response) responseMarshalled, err := commonbackend.Marshal(response)
if err != nil { if err != nil {
log.Error("failed to marshal response: %s", err.Error()) log.Error("failed to marshal response: %s", err.Error())
@ -144,7 +141,6 @@ func (helper *BackendApplicationHelper) Start() error {
} }
response := &commonbackend.ProxyStatusResponse{ response := &commonbackend.ProxyStatusResponse{
Type: "proxyStatusResponse",
SourceIP: command.SourceIP, SourceIP: command.SourceIP,
SourcePort: command.SourcePort, SourcePort: command.SourcePort,
DestPort: command.DestPort, DestPort: command.DestPort,
@ -152,7 +148,7 @@ func (helper *BackendApplicationHelper) Start() error {
IsActive: !hasAnyFailed, IsActive: !hasAnyFailed,
} }
responseMarshalled, err := commonbackend.Marshal(response.Type, response) responseMarshalled, err := commonbackend.Marshal(response)
if err != nil { if err != nil {
log.Error("failed to marshal response: %s", err.Error()) log.Error("failed to marshal response: %s", err.Error())
@ -173,7 +169,6 @@ func (helper *BackendApplicationHelper) Start() error {
} }
response := &commonbackend.ProxyStatusResponse{ response := &commonbackend.ProxyStatusResponse{
Type: "proxyStatusResponse",
SourceIP: command.SourceIP, SourceIP: command.SourceIP,
SourcePort: command.SourcePort, SourcePort: command.SourcePort,
DestPort: command.DestPort, DestPort: command.DestPort,
@ -181,7 +176,7 @@ func (helper *BackendApplicationHelper) Start() error {
IsActive: hasAnyFailed, IsActive: hasAnyFailed,
} }
responseMarshalled, err := commonbackend.Marshal(response.Type, response) responseMarshalled, err := commonbackend.Marshal(response)
if err != nil { if err != nil {
log.Error("failed to marshal response: %s", err.Error()) log.Error("failed to marshal response: %s", err.Error())
@ -193,11 +188,10 @@ func (helper *BackendApplicationHelper) Start() error {
connections := helper.Backend.GetAllClientConnections() connections := helper.Backend.GetAllClientConnections()
serverParams := &commonbackend.ProxyConnectionsResponse{ serverParams := &commonbackend.ProxyConnectionsResponse{
Type: "proxyConnectionsResponse",
Connections: connections, Connections: connections,
} }
byteData, err := commonbackend.Marshal(serverParams.Type, serverParams) byteData, err := commonbackend.Marshal(serverParams)
if err != nil { if err != nil {
return err return err
@ -208,10 +202,9 @@ func (helper *BackendApplicationHelper) Start() error {
} }
case *commonbackend.CheckClientParameters: case *commonbackend.CheckClientParameters:
resp := helper.Backend.CheckParametersForConnections(command) resp := helper.Backend.CheckParametersForConnections(command)
resp.Type = "checkParametersResponse"
resp.InResponseTo = "checkClientParameters" resp.InResponseTo = "checkClientParameters"
byteData, err := commonbackend.Marshal(resp.Type, resp) byteData, err := commonbackend.Marshal(resp)
if err != nil { if err != nil {
return err return err
@ -222,10 +215,9 @@ func (helper *BackendApplicationHelper) Start() error {
} }
case *commonbackend.CheckServerParameters: case *commonbackend.CheckServerParameters:
resp := helper.Backend.CheckParametersForBackend(command.Arguments) resp := helper.Backend.CheckParametersForBackend(command.Arguments)
resp.Type = "checkParametersResponse"
resp.InResponseTo = "checkServerParameters" resp.InResponseTo = "checkServerParameters"
byteData, err := commonbackend.Marshal(resp.Type, resp) byteData, err := commonbackend.Marshal(resp)
if err != nil { if err != nil {
return err return err

View file

@ -1,16 +1,13 @@
package commonbackend package commonbackend
type Start struct { type Start struct {
Type string // Will be 'start' always
Arguments []byte Arguments []byte
} }
type Stop struct { type Stop struct {
Type string // Will be 'stop' always
} }
type AddProxy struct { type AddProxy struct {
Type string // Will be 'addProxy' always
SourceIP string SourceIP string
SourcePort uint16 SourcePort uint16
DestPort uint16 DestPort uint16
@ -18,7 +15,6 @@ type AddProxy struct {
} }
type RemoveProxy struct { type RemoveProxy struct {
Type string // Will be 'removeProxy' always
SourceIP string SourceIP string
SourcePort uint16 SourcePort uint16
DestPort uint16 DestPort uint16
@ -26,7 +22,6 @@ type RemoveProxy struct {
} }
type ProxyStatusRequest struct { type ProxyStatusRequest struct {
Type string // Will be 'proxyStatusRequest' always
SourceIP string SourceIP string
SourcePort uint16 SourcePort uint16
DestPort uint16 DestPort uint16
@ -34,7 +29,6 @@ type ProxyStatusRequest struct {
} }
type ProxyStatusResponse struct { type ProxyStatusResponse struct {
Type string // Will be 'proxyStatusResponse' always
SourceIP string SourceIP string
SourcePort uint16 SourcePort uint16
DestPort uint16 DestPort uint16
@ -50,27 +44,22 @@ type ProxyInstance struct {
} }
type ProxyInstanceResponse struct { type ProxyInstanceResponse struct {
Type string // Will be 'proxyConnectionResponse' always
Proxies []*ProxyInstance // List of connections Proxies []*ProxyInstance // List of connections
} }
type ProxyInstanceRequest struct { type ProxyInstanceRequest struct {
Type string // Will be 'proxyConnectionRequest' always
} }
type BackendStatusResponse struct { type BackendStatusResponse struct {
Type string // Will be 'backendStatusResponse' always
IsRunning bool // True if running, false if not running IsRunning bool // True if running, false if not running
StatusCode int // Either the 'Success' or 'Failure' constant StatusCode int // Either the 'Success' or 'Failure' constant
Message string // String message from the client (ex. failed to dial TCP) Message string // String message from the client (ex. failed to dial TCP)
} }
type BackendStatusRequest struct { type BackendStatusRequest struct {
Type string // Will be 'backendStatusRequest' always
} }
type ProxyConnectionsRequest struct { type ProxyConnectionsRequest struct {
Type string // Will be 'proxyConnectionsRequest' always
} }
// Client's connection to a specific proxy // Client's connection to a specific proxy
@ -83,12 +72,10 @@ type ProxyClientConnection struct {
} }
type ProxyConnectionsResponse struct { type ProxyConnectionsResponse struct {
Type string // Will be 'proxyConnectionsResponse' always
Connections []*ProxyClientConnection // List of connections Connections []*ProxyClientConnection // List of connections
} }
type CheckClientParameters struct { type CheckClientParameters struct {
Type string // Will be 'checkClientParameters' always
SourceIP string SourceIP string
SourcePort uint16 SourcePort uint16
DestPort uint16 DestPort uint16
@ -96,13 +83,11 @@ type CheckClientParameters struct {
} }
type CheckServerParameters struct { type CheckServerParameters struct {
Type string // Will be 'checkServerParameters' always
Arguments []byte Arguments []byte
} }
// Sent as a response to either CheckClientParameters or CheckBackendParameters // Sent as a response to either CheckClientParameters or CheckBackendParameters
type CheckParametersResponse struct { type CheckParametersResponse struct {
Type string // Will be 'checkParametersResponse' always
InResponseTo string // Will be either 'checkClientParameters' or 'checkServerParameters' InResponseTo string // Will be either 'checkClientParameters' or 'checkServerParameters'
IsValid bool // If true, valid, and if false, invalid IsValid bool // If true, valid, and if false, invalid
Message string // String message from the client (ex. failed to unmarshal JSON: x is not defined) Message string // String message from the client (ex. failed to unmarshal JSON: x is not defined)

View file

@ -84,7 +84,7 @@ func marshalIndividualProxyStruct(conn *ProxyInstance) ([]byte, error) {
return proxyBlock, nil return proxyBlock, nil
} }
func Marshal(_ string, command interface{}) ([]byte, error) { func Marshal(command interface{}) ([]byte, error) {
switch command := command.(type) { switch command := command.(type) {
case *Start: case *Start:
startCommandBytes := make([]byte, 1+2+len(command.Arguments)) startCommandBytes := make([]byte, 1+2+len(command.Arguments))

View file

@ -11,11 +11,10 @@ var logLevel = os.Getenv("HERMES_LOG_LEVEL")
func TestStart(t *testing.T) { func TestStart(t *testing.T) {
commandInput := &Start{ commandInput := &Start{
Type: "start",
Arguments: []byte("Hello from automated testing"), Arguments: []byte("Hello from automated testing"),
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -26,39 +25,27 @@ func TestStart(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*Start) commandUnmarshalled, ok := commandUnmarshalledRaw.(*Start)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if !bytes.Equal(commandInput.Arguments, commandUnmarshalled.Arguments) { if !bytes.Equal(commandInput.Arguments, commandUnmarshalled.Arguments) {
log.Fatalf("Arguments are not equal (orig: '%s', unmsh: '%s')", string(commandInput.Arguments), string(commandUnmarshalled.Arguments)) log.Fatalf("Arguments are not equal (orig: '%s', unmsh: '%s')", string(commandInput.Arguments), string(commandUnmarshalled.Arguments))
} }
} }
func TestStop(t *testing.T) { func TestStop(t *testing.T) {
commandInput := &Stop{ commandInput := &Stop{}
Type: "stop",
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -69,39 +56,28 @@ func TestStop(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type { _, ok := commandUnmarshalledRaw.(*Stop)
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*Stop)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
} }
func TestAddConnection(t *testing.T) { func TestAddConnection(t *testing.T) {
commandInput := &AddProxy{ commandInput := &AddProxy{
Type: "addProxy",
SourceIP: "192.168.0.139", SourceIP: "192.168.0.139",
SourcePort: 19132, SourcePort: 19132,
DestPort: 19132, DestPort: 19132,
Protocol: "tcp", Protocol: "tcp",
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -112,28 +88,18 @@ func TestAddConnection(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*AddProxy) commandUnmarshalled, ok := commandUnmarshalledRaw.(*AddProxy)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.SourceIP != commandUnmarshalled.SourceIP { if commandInput.SourceIP != commandUnmarshalled.SourceIP {
t.Fail() t.Fail()
log.Printf("SourceIP's are not equal (orig: %s, unmsh: %s)", commandInput.SourceIP, commandUnmarshalled.SourceIP) log.Printf("SourceIP's are not equal (orig: %s, unmsh: %s)", commandInput.SourceIP, commandUnmarshalled.SourceIP)
@ -157,14 +123,13 @@ func TestAddConnection(t *testing.T) {
func TestRemoveConnection(t *testing.T) { func TestRemoveConnection(t *testing.T) {
commandInput := &RemoveProxy{ commandInput := &RemoveProxy{
Type: "removeProxy",
SourceIP: "192.168.0.139", SourceIP: "192.168.0.139",
SourcePort: 19132, SourcePort: 19132,
DestPort: 19132, DestPort: 19132,
Protocol: "tcp", Protocol: "tcp",
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -175,28 +140,18 @@ func TestRemoveConnection(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*RemoveProxy) commandUnmarshalled, ok := commandUnmarshalledRaw.(*RemoveProxy)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.SourceIP != commandUnmarshalled.SourceIP { if commandInput.SourceIP != commandUnmarshalled.SourceIP {
t.Fail() t.Fail()
log.Printf("SourceIP's are not equal (orig: %s, unmsh: %s)", commandInput.SourceIP, commandUnmarshalled.SourceIP) log.Printf("SourceIP's are not equal (orig: %s, unmsh: %s)", commandInput.SourceIP, commandUnmarshalled.SourceIP)
@ -220,7 +175,6 @@ func TestRemoveConnection(t *testing.T) {
func TestGetAllConnections(t *testing.T) { func TestGetAllConnections(t *testing.T) {
commandInput := &ProxyConnectionsResponse{ commandInput := &ProxyConnectionsResponse{
Type: "proxyConnectionsResponse",
Connections: []*ProxyClientConnection{ Connections: []*ProxyClientConnection{
{ {
SourceIP: "127.0.0.1", SourceIP: "127.0.0.1",
@ -246,7 +200,7 @@ func TestGetAllConnections(t *testing.T) {
}, },
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -257,28 +211,18 @@ func TestGetAllConnections(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionsResponse) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionsResponse)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
for commandIndex, originalConnection := range commandInput.Connections { for commandIndex, originalConnection := range commandInput.Connections {
remoteConnection := commandUnmarshalled.Connections[commandIndex] remoteConnection := commandUnmarshalled.Connections[commandIndex]
@ -311,14 +255,13 @@ func TestGetAllConnections(t *testing.T) {
func TestCheckClientParameters(t *testing.T) { func TestCheckClientParameters(t *testing.T) {
commandInput := &CheckClientParameters{ commandInput := &CheckClientParameters{
Type: "checkClientParameters",
SourceIP: "192.168.0.139", SourceIP: "192.168.0.139",
SourcePort: 19132, SourcePort: 19132,
DestPort: 19132, DestPort: 19132,
Protocol: "tcp", Protocol: "tcp",
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -329,28 +272,18 @@ func TestCheckClientParameters(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Printf("command type does not match up! (orig: %s, unmsh: %s)", commandType, commandInput.Type)
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*CheckClientParameters) commandUnmarshalled, ok := commandUnmarshalledRaw.(*CheckClientParameters)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.SourceIP != commandUnmarshalled.SourceIP { if commandInput.SourceIP != commandUnmarshalled.SourceIP {
t.Fail() t.Fail()
log.Printf("SourceIP's are not equal (orig: %s, unmsh: %s)", commandInput.SourceIP, commandUnmarshalled.SourceIP) log.Printf("SourceIP's are not equal (orig: %s, unmsh: %s)", commandInput.SourceIP, commandUnmarshalled.SourceIP)
@ -374,11 +307,10 @@ func TestCheckClientParameters(t *testing.T) {
func TestCheckServerParameters(t *testing.T) { func TestCheckServerParameters(t *testing.T) {
commandInput := &CheckServerParameters{ commandInput := &CheckServerParameters{
Type: "checkServerParameters",
Arguments: []byte("Hello from automated testing"), Arguments: []byte("Hello from automated testing"),
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -389,28 +321,18 @@ func TestCheckServerParameters(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*CheckServerParameters) commandUnmarshalled, ok := commandUnmarshalledRaw.(*CheckServerParameters)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if !bytes.Equal(commandInput.Arguments, commandUnmarshalled.Arguments) { if !bytes.Equal(commandInput.Arguments, commandUnmarshalled.Arguments) {
log.Fatalf("Arguments are not equal (orig: '%s', unmsh: '%s')", string(commandInput.Arguments), string(commandUnmarshalled.Arguments)) log.Fatalf("Arguments are not equal (orig: '%s', unmsh: '%s')", string(commandInput.Arguments), string(commandUnmarshalled.Arguments))
} }
@ -418,13 +340,12 @@ func TestCheckServerParameters(t *testing.T) {
func TestCheckParametersResponse(t *testing.T) { func TestCheckParametersResponse(t *testing.T) {
commandInput := &CheckParametersResponse{ commandInput := &CheckParametersResponse{
Type: "checkParametersResponse",
InResponseTo: "checkClientParameters", InResponseTo: "checkClientParameters",
IsValid: true, IsValid: true,
Message: "Hello from automated testing", Message: "Hello from automated testing",
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -435,28 +356,18 @@ func TestCheckParametersResponse(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Printf("command type does not match up! (orig: %s, unmsh: %s)", commandType, commandInput.Type)
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*CheckParametersResponse) commandUnmarshalled, ok := commandUnmarshalledRaw.(*CheckParametersResponse)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.InResponseTo != commandUnmarshalled.InResponseTo { if commandInput.InResponseTo != commandUnmarshalled.InResponseTo {
t.Fail() t.Fail()
log.Printf("InResponseTo's are not equal (orig: %s, unmsh: %s)", commandInput.InResponseTo, commandUnmarshalled.InResponseTo) log.Printf("InResponseTo's are not equal (orig: %s, unmsh: %s)", commandInput.InResponseTo, commandUnmarshalled.InResponseTo)
@ -474,11 +385,8 @@ func TestCheckParametersResponse(t *testing.T) {
} }
func TestBackendStatusRequest(t *testing.T) { func TestBackendStatusRequest(t *testing.T) {
commandInput := &BackendStatusRequest{ commandInput := &BackendStatusRequest{}
Type: "backendStatusRequest", commandMarshalled, err := Marshal(commandInput)
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -489,38 +397,27 @@ func TestBackendStatusRequest(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type { _, ok := commandUnmarshalledRaw.(*BackendStatusRequest)
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*BackendStatusRequest)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
} }
func TestBackendStatusResponse(t *testing.T) { func TestBackendStatusResponse(t *testing.T) {
commandInput := &BackendStatusResponse{ commandInput := &BackendStatusResponse{
Type: "backendStatusResponse",
IsRunning: true, IsRunning: true,
StatusCode: StatusFailure, StatusCode: StatusFailure,
Message: "Hello from automated testing", Message: "Hello from automated testing",
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -531,28 +428,18 @@ func TestBackendStatusResponse(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*BackendStatusResponse) commandUnmarshalled, ok := commandUnmarshalledRaw.(*BackendStatusResponse)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.IsRunning != commandUnmarshalled.IsRunning { if commandInput.IsRunning != commandUnmarshalled.IsRunning {
t.Fail() t.Fail()
log.Printf("IsRunning's are not equal (orig: %t, unmsh: %t)", commandInput.IsRunning, commandUnmarshalled.IsRunning) log.Printf("IsRunning's are not equal (orig: %t, unmsh: %t)", commandInput.IsRunning, commandUnmarshalled.IsRunning)
@ -571,14 +458,13 @@ func TestBackendStatusResponse(t *testing.T) {
func TestProxyStatusRequest(t *testing.T) { func TestProxyStatusRequest(t *testing.T) {
commandInput := &ProxyStatusRequest{ commandInput := &ProxyStatusRequest{
Type: "proxyStatusRequest",
SourceIP: "192.168.0.139", SourceIP: "192.168.0.139",
SourcePort: 19132, SourcePort: 19132,
DestPort: 19132, DestPort: 19132,
Protocol: "tcp", Protocol: "tcp",
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -589,28 +475,18 @@ func TestProxyStatusRequest(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyStatusRequest) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyStatusRequest)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.SourceIP != commandUnmarshalled.SourceIP { if commandInput.SourceIP != commandUnmarshalled.SourceIP {
t.Fail() t.Fail()
log.Printf("SourceIP's are not equal (orig: %s, unmsh: %s)", commandInput.SourceIP, commandUnmarshalled.SourceIP) log.Printf("SourceIP's are not equal (orig: %s, unmsh: %s)", commandInput.SourceIP, commandUnmarshalled.SourceIP)
@ -634,7 +510,6 @@ func TestProxyStatusRequest(t *testing.T) {
func TestProxyStatusResponse(t *testing.T) { func TestProxyStatusResponse(t *testing.T) {
commandInput := &ProxyStatusResponse{ commandInput := &ProxyStatusResponse{
Type: "proxyStatusResponse",
SourceIP: "192.168.0.139", SourceIP: "192.168.0.139",
SourcePort: 19132, SourcePort: 19132,
DestPort: 19132, DestPort: 19132,
@ -642,7 +517,7 @@ func TestProxyStatusResponse(t *testing.T) {
IsActive: true, IsActive: true,
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -653,28 +528,18 @@ func TestProxyStatusResponse(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyStatusResponse) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyStatusResponse)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.SourceIP != commandUnmarshalled.SourceIP { if commandInput.SourceIP != commandUnmarshalled.SourceIP {
t.Fail() t.Fail()
log.Printf("SourceIP's are not equal (orig: %s, unmsh: %s)", commandInput.SourceIP, commandUnmarshalled.SourceIP) log.Printf("SourceIP's are not equal (orig: %s, unmsh: %s)", commandInput.SourceIP, commandUnmarshalled.SourceIP)
@ -702,11 +567,9 @@ func TestProxyStatusResponse(t *testing.T) {
} }
func TestProxyConnectionRequest(t *testing.T) { func TestProxyConnectionRequest(t *testing.T) {
commandInput := &ProxyInstanceRequest{ commandInput := &ProxyInstanceRequest{}
Type: "proxyInstanceRequest",
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -717,32 +580,21 @@ func TestProxyConnectionRequest(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type { _, ok := commandUnmarshalledRaw.(*ProxyInstanceRequest)
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInstanceRequest)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
} }
func TestProxyConnectionResponse(t *testing.T) { func TestProxyConnectionResponse(t *testing.T) {
commandInput := &ProxyInstanceResponse{ commandInput := &ProxyInstanceResponse{
Type: "proxyInstanceResponse",
Proxies: []*ProxyInstance{ Proxies: []*ProxyInstance{
{ {
SourceIP: "192.168.0.168", SourceIP: "192.168.0.168",
@ -765,7 +617,7 @@ func TestProxyConnectionResponse(t *testing.T) {
}, },
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -776,28 +628,18 @@ func TestProxyConnectionResponse(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInstanceResponse) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInstanceResponse)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
for proxyIndex, originalProxy := range commandInput.Proxies { for proxyIndex, originalProxy := range commandInput.Proxies {
remoteProxy := commandUnmarshalled.Proxies[proxyIndex] remoteProxy := commandUnmarshalled.Proxies[proxyIndex]

View file

@ -142,11 +142,11 @@ func unmarshalIndividualProxyStruct(conn io.Reader) (*ProxyInstance, error) {
}, nil }, nil
} }
func Unmarshal(conn io.Reader) (string, interface{}, error) { func Unmarshal(conn io.Reader) (interface{}, error) {
commandType := make([]byte, 1) commandType := make([]byte, 1)
if _, err := conn.Read(commandType); err != nil { if _, err := conn.Read(commandType); err != nil {
return "", nil, fmt.Errorf("couldn't read command") return nil, fmt.Errorf("couldn't read command")
} }
switch commandType[0] { switch commandType[0] {
@ -154,28 +154,25 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
argumentsLength := make([]byte, 2) argumentsLength := make([]byte, 2)
if _, err := conn.Read(argumentsLength); err != nil { if _, err := conn.Read(argumentsLength); err != nil {
return "", nil, fmt.Errorf("couldn't read argument length") return nil, fmt.Errorf("couldn't read argument length")
} }
arguments := make([]byte, binary.BigEndian.Uint16(argumentsLength)) arguments := make([]byte, binary.BigEndian.Uint16(argumentsLength))
if _, err := conn.Read(arguments); err != nil { if _, err := conn.Read(arguments); err != nil {
return "", nil, fmt.Errorf("couldn't read arguments") return nil, fmt.Errorf("couldn't read arguments")
} }
return "start", &Start{ return &Start{
Type: "start",
Arguments: arguments, Arguments: arguments,
}, nil }, nil
case StopID: case StopID:
return "stop", &Stop{ return &Stop{}, nil
Type: "stop",
}, nil
case AddProxyID: case AddProxyID:
ipVersion := make([]byte, 1) ipVersion := make([]byte, 1)
if _, err := conn.Read(ipVersion); err != nil { if _, err := conn.Read(ipVersion); err != nil {
return "", nil, fmt.Errorf("couldn't read ip version") return nil, fmt.Errorf("couldn't read ip version")
} }
var ipSize uint8 var ipSize uint8
@ -185,31 +182,31 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if ipVersion[0] == 6 { } else if ipVersion[0] == 6 {
ipSize = IPv6Size ipSize = IPv6Size
} else { } else {
return "", nil, fmt.Errorf("invalid IP version recieved") return nil, fmt.Errorf("invalid IP version recieved")
} }
ip := make(net.IP, ipSize) ip := make(net.IP, ipSize)
if _, err := conn.Read(ip); err != nil { if _, err := conn.Read(ip); err != nil {
return "", nil, fmt.Errorf("couldn't read source IP") return nil, fmt.Errorf("couldn't read source IP")
} }
sourcePort := make([]byte, 2) sourcePort := make([]byte, 2)
if _, err := conn.Read(sourcePort); err != nil { if _, err := conn.Read(sourcePort); err != nil {
return "", nil, fmt.Errorf("couldn't read source port") return nil, fmt.Errorf("couldn't read source port")
} }
destPort := make([]byte, 2) destPort := make([]byte, 2)
if _, err := conn.Read(destPort); err != nil { if _, err := conn.Read(destPort); err != nil {
return "", nil, fmt.Errorf("couldn't read destination port") return nil, fmt.Errorf("couldn't read destination port")
} }
protocolBytes := make([]byte, 1) protocolBytes := make([]byte, 1)
if _, err := conn.Read(protocolBytes); err != nil { if _, err := conn.Read(protocolBytes); err != nil {
return "", nil, fmt.Errorf("couldn't read protocol") return nil, fmt.Errorf("couldn't read protocol")
} }
var protocol string var protocol string
@ -219,11 +216,10 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if protocolBytes[1] == UDP { } else if protocolBytes[1] == UDP {
protocol = "udp" protocol = "udp"
} else { } else {
return "", nil, fmt.Errorf("invalid protocol") return nil, fmt.Errorf("invalid protocol")
} }
return "addProxy", &AddProxy{ return &AddProxy{
Type: "addProxy",
SourceIP: ip.String(), SourceIP: ip.String(),
SourcePort: binary.BigEndian.Uint16(sourcePort), SourcePort: binary.BigEndian.Uint16(sourcePort),
DestPort: binary.BigEndian.Uint16(destPort), DestPort: binary.BigEndian.Uint16(destPort),
@ -233,7 +229,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
ipVersion := make([]byte, 1) ipVersion := make([]byte, 1)
if _, err := conn.Read(ipVersion); err != nil { if _, err := conn.Read(ipVersion); err != nil {
return "", nil, fmt.Errorf("couldn't read ip version") return nil, fmt.Errorf("couldn't read ip version")
} }
var ipSize uint8 var ipSize uint8
@ -243,31 +239,31 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if ipVersion[0] == 6 { } else if ipVersion[0] == 6 {
ipSize = IPv6Size ipSize = IPv6Size
} else { } else {
return "", nil, fmt.Errorf("invalid IP version recieved") return nil, fmt.Errorf("invalid IP version recieved")
} }
ip := make(net.IP, ipSize) ip := make(net.IP, ipSize)
if _, err := conn.Read(ip); err != nil { if _, err := conn.Read(ip); err != nil {
return "", nil, fmt.Errorf("couldn't read source IP") return nil, fmt.Errorf("couldn't read source IP")
} }
sourcePort := make([]byte, 2) sourcePort := make([]byte, 2)
if _, err := conn.Read(sourcePort); err != nil { if _, err := conn.Read(sourcePort); err != nil {
return "", nil, fmt.Errorf("couldn't read source port") return nil, fmt.Errorf("couldn't read source port")
} }
destPort := make([]byte, 2) destPort := make([]byte, 2)
if _, err := conn.Read(destPort); err != nil { if _, err := conn.Read(destPort); err != nil {
return "", nil, fmt.Errorf("couldn't read destination port") return nil, fmt.Errorf("couldn't read destination port")
} }
protocolBytes := make([]byte, 1) protocolBytes := make([]byte, 1)
if _, err := conn.Read(protocolBytes); err != nil { if _, err := conn.Read(protocolBytes); err != nil {
return "", nil, fmt.Errorf("couldn't read protocol") return nil, fmt.Errorf("couldn't read protocol")
} }
var protocol string var protocol string
@ -277,11 +273,10 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if protocolBytes[1] == UDP { } else if protocolBytes[1] == UDP {
protocol = "udp" protocol = "udp"
} else { } else {
return "", nil, fmt.Errorf("invalid protocol") return nil, fmt.Errorf("invalid protocol")
} }
return "removeProxy", &RemoveProxy{ return &RemoveProxy{
Type: "removeProxy",
SourceIP: ip.String(), SourceIP: ip.String(),
SourcePort: binary.BigEndian.Uint16(sourcePort), SourcePort: binary.BigEndian.Uint16(sourcePort),
DestPort: binary.BigEndian.Uint16(destPort), DestPort: binary.BigEndian.Uint16(destPort),
@ -301,13 +296,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
break break
} }
return "", nil, err return nil, err
} }
connections = append(connections, connection) connections = append(connections, connection)
if _, err := conn.Read(delimiter); err != nil { if _, err := conn.Read(delimiter); err != nil {
return "", nil, fmt.Errorf("couldn't read delimiter") return nil, fmt.Errorf("couldn't read delimiter")
} }
if delimiter[0] == '\r' { if delimiter[0] == '\r' {
@ -321,15 +316,14 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} }
} }
return "proxyConnectionsResponse", &ProxyConnectionsResponse{ return &ProxyConnectionsResponse{
Type: "proxyConnectionsResponse",
Connections: connections, Connections: connections,
}, errorReturn }, errorReturn
case CheckClientParametersID: case CheckClientParametersID:
ipVersion := make([]byte, 1) ipVersion := make([]byte, 1)
if _, err := conn.Read(ipVersion); err != nil { if _, err := conn.Read(ipVersion); err != nil {
return "", nil, fmt.Errorf("couldn't read ip version") return nil, fmt.Errorf("couldn't read ip version")
} }
var ipSize uint8 var ipSize uint8
@ -339,31 +333,31 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if ipVersion[0] == 6 { } else if ipVersion[0] == 6 {
ipSize = IPv6Size ipSize = IPv6Size
} else { } else {
return "", nil, fmt.Errorf("invalid IP version recieved") return nil, fmt.Errorf("invalid IP version recieved")
} }
ip := make(net.IP, ipSize) ip := make(net.IP, ipSize)
if _, err := conn.Read(ip); err != nil { if _, err := conn.Read(ip); err != nil {
return "", nil, fmt.Errorf("couldn't read source IP") return nil, fmt.Errorf("couldn't read source IP")
} }
sourcePort := make([]byte, 2) sourcePort := make([]byte, 2)
if _, err := conn.Read(sourcePort); err != nil { if _, err := conn.Read(sourcePort); err != nil {
return "", nil, fmt.Errorf("couldn't read source port") return nil, fmt.Errorf("couldn't read source port")
} }
destPort := make([]byte, 2) destPort := make([]byte, 2)
if _, err := conn.Read(destPort); err != nil { if _, err := conn.Read(destPort); err != nil {
return "", nil, fmt.Errorf("couldn't read destination port") return nil, fmt.Errorf("couldn't read destination port")
} }
protocolBytes := make([]byte, 1) protocolBytes := make([]byte, 1)
if _, err := conn.Read(protocolBytes); err != nil { if _, err := conn.Read(protocolBytes); err != nil {
return "", nil, fmt.Errorf("couldn't read protocol") return nil, fmt.Errorf("couldn't read protocol")
} }
var protocol string var protocol string
@ -373,11 +367,10 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if protocolBytes[1] == UDP { } else if protocolBytes[1] == UDP {
protocol = "udp" protocol = "udp"
} else { } else {
return "", nil, fmt.Errorf("invalid protocol") return nil, fmt.Errorf("invalid protocol")
} }
return "checkClientParameters", &CheckClientParameters{ return &CheckClientParameters{
Type: "checkClientParameters",
SourceIP: ip.String(), SourceIP: ip.String(),
SourcePort: binary.BigEndian.Uint16(sourcePort), SourcePort: binary.BigEndian.Uint16(sourcePort),
DestPort: binary.BigEndian.Uint16(destPort), DestPort: binary.BigEndian.Uint16(destPort),
@ -387,24 +380,23 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
argumentsLength := make([]byte, 2) argumentsLength := make([]byte, 2)
if _, err := conn.Read(argumentsLength); err != nil { if _, err := conn.Read(argumentsLength); err != nil {
return "", nil, fmt.Errorf("couldn't read argument length") return nil, fmt.Errorf("couldn't read argument length")
} }
arguments := make([]byte, binary.BigEndian.Uint16(argumentsLength)) arguments := make([]byte, binary.BigEndian.Uint16(argumentsLength))
if _, err := conn.Read(arguments); err != nil { if _, err := conn.Read(arguments); err != nil {
return "", nil, fmt.Errorf("couldn't read arguments") return nil, fmt.Errorf("couldn't read arguments")
} }
return "checkServerParameters", &CheckServerParameters{ return &CheckServerParameters{
Type: "checkServerParameters",
Arguments: arguments, Arguments: arguments,
}, nil }, nil
case CheckParametersResponseID: case CheckParametersResponseID:
checkMethodByte := make([]byte, 1) checkMethodByte := make([]byte, 1)
if _, err := conn.Read(checkMethodByte); err != nil { if _, err := conn.Read(checkMethodByte); err != nil {
return "", nil, fmt.Errorf("couldn't read check method byte") return nil, fmt.Errorf("couldn't read check method byte")
} }
var checkMethod string var checkMethod string
@ -414,19 +406,19 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if checkMethodByte[0] == CheckServerParametersID { } else if checkMethodByte[0] == CheckServerParametersID {
checkMethod = "checkServerParameters" checkMethod = "checkServerParameters"
} else { } else {
return "", nil, fmt.Errorf("invalid check method recieved") return nil, fmt.Errorf("invalid check method recieved")
} }
isValid := make([]byte, 1) isValid := make([]byte, 1)
if _, err := conn.Read(isValid); err != nil { if _, err := conn.Read(isValid); err != nil {
return "", nil, fmt.Errorf("couldn't read isValid byte") return nil, fmt.Errorf("couldn't read isValid byte")
} }
messageLengthBytes := make([]byte, 2) messageLengthBytes := make([]byte, 2)
if _, err := conn.Read(messageLengthBytes); err != nil { if _, err := conn.Read(messageLengthBytes); err != nil {
return "", nil, fmt.Errorf("couldn't read message length") return nil, fmt.Errorf("couldn't read message length")
} }
messageLength := binary.BigEndian.Uint16(messageLengthBytes) messageLength := binary.BigEndian.Uint16(messageLengthBytes)
@ -436,14 +428,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
messageBytes := make([]byte, messageLength) messageBytes := make([]byte, messageLength)
if _, err := conn.Read(messageBytes); err != nil { if _, err := conn.Read(messageBytes); err != nil {
return "", nil, fmt.Errorf("couldn't read message") return nil, fmt.Errorf("couldn't read message")
} }
message = string(messageBytes) message = string(messageBytes)
} }
return "checkParametersResponse", &CheckParametersResponse{ return &CheckParametersResponse{
Type: "checkParametersResponse",
InResponseTo: checkMethod, InResponseTo: checkMethod,
IsValid: isValid[0] == 1, IsValid: isValid[0] == 1,
Message: message, Message: message,
@ -452,19 +443,19 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
isRunning := make([]byte, 1) isRunning := make([]byte, 1)
if _, err := conn.Read(isRunning); err != nil { if _, err := conn.Read(isRunning); err != nil {
return "", nil, fmt.Errorf("couldn't read isRunning field") return nil, fmt.Errorf("couldn't read isRunning field")
} }
statusCode := make([]byte, 1) statusCode := make([]byte, 1)
if _, err := conn.Read(statusCode); err != nil { if _, err := conn.Read(statusCode); err != nil {
return "", nil, fmt.Errorf("couldn't read status code field") return nil, fmt.Errorf("couldn't read status code field")
} }
messageLengthBytes := make([]byte, 2) messageLengthBytes := make([]byte, 2)
if _, err := conn.Read(messageLengthBytes); err != nil { if _, err := conn.Read(messageLengthBytes); err != nil {
return "", nil, fmt.Errorf("couldn't read message length") return nil, fmt.Errorf("couldn't read message length")
} }
messageLength := binary.BigEndian.Uint16(messageLengthBytes) messageLength := binary.BigEndian.Uint16(messageLengthBytes)
@ -474,27 +465,24 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
messageBytes := make([]byte, messageLength) messageBytes := make([]byte, messageLength)
if _, err := conn.Read(messageBytes); err != nil { if _, err := conn.Read(messageBytes); err != nil {
return "", nil, fmt.Errorf("couldn't read message") return nil, fmt.Errorf("couldn't read message")
} }
message = string(messageBytes) message = string(messageBytes)
} }
return "backendStatusResponse", &BackendStatusResponse{ return &BackendStatusResponse{
Type: "backendStatusResponse",
IsRunning: isRunning[0] == 1, IsRunning: isRunning[0] == 1,
StatusCode: int(statusCode[0]), StatusCode: int(statusCode[0]),
Message: message, Message: message,
}, nil }, nil
case BackendStatusRequestID: case BackendStatusRequestID:
return "backendStatusRequest", &BackendStatusRequest{ return &BackendStatusRequest{}, nil
Type: "backendStatusRequest",
}, nil
case ProxyStatusRequestID: case ProxyStatusRequestID:
ipVersion := make([]byte, 1) ipVersion := make([]byte, 1)
if _, err := conn.Read(ipVersion); err != nil { if _, err := conn.Read(ipVersion); err != nil {
return "", nil, fmt.Errorf("couldn't read ip version") return nil, fmt.Errorf("couldn't read ip version")
} }
var ipSize uint8 var ipSize uint8
@ -504,31 +492,31 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if ipVersion[0] == 6 { } else if ipVersion[0] == 6 {
ipSize = IPv6Size ipSize = IPv6Size
} else { } else {
return "", nil, fmt.Errorf("invalid IP version recieved") return nil, fmt.Errorf("invalid IP version recieved")
} }
ip := make(net.IP, ipSize) ip := make(net.IP, ipSize)
if _, err := conn.Read(ip); err != nil { if _, err := conn.Read(ip); err != nil {
return "", nil, fmt.Errorf("couldn't read source IP") return nil, fmt.Errorf("couldn't read source IP")
} }
sourcePort := make([]byte, 2) sourcePort := make([]byte, 2)
if _, err := conn.Read(sourcePort); err != nil { if _, err := conn.Read(sourcePort); err != nil {
return "", nil, fmt.Errorf("couldn't read source port") return nil, fmt.Errorf("couldn't read source port")
} }
destPort := make([]byte, 2) destPort := make([]byte, 2)
if _, err := conn.Read(destPort); err != nil { if _, err := conn.Read(destPort); err != nil {
return "", nil, fmt.Errorf("couldn't read destination port") return nil, fmt.Errorf("couldn't read destination port")
} }
protocolBytes := make([]byte, 1) protocolBytes := make([]byte, 1)
if _, err := conn.Read(protocolBytes); err != nil { if _, err := conn.Read(protocolBytes); err != nil {
return "", nil, fmt.Errorf("couldn't read protocol") return nil, fmt.Errorf("couldn't read protocol")
} }
var protocol string var protocol string
@ -538,11 +526,10 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if protocolBytes[1] == UDP { } else if protocolBytes[1] == UDP {
protocol = "udp" protocol = "udp"
} else { } else {
return "", nil, fmt.Errorf("invalid protocol") return nil, fmt.Errorf("invalid protocol")
} }
return "proxyStatusRequest", &ProxyStatusRequest{ return &ProxyStatusRequest{
Type: "proxyStatusRequest",
SourceIP: ip.String(), SourceIP: ip.String(),
SourcePort: binary.BigEndian.Uint16(sourcePort), SourcePort: binary.BigEndian.Uint16(sourcePort),
DestPort: binary.BigEndian.Uint16(destPort), DestPort: binary.BigEndian.Uint16(destPort),
@ -552,7 +539,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
ipVersion := make([]byte, 1) ipVersion := make([]byte, 1)
if _, err := conn.Read(ipVersion); err != nil { if _, err := conn.Read(ipVersion); err != nil {
return "", nil, fmt.Errorf("couldn't read ip version") return nil, fmt.Errorf("couldn't read ip version")
} }
var ipSize uint8 var ipSize uint8
@ -562,31 +549,31 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if ipVersion[0] == 6 { } else if ipVersion[0] == 6 {
ipSize = IPv6Size ipSize = IPv6Size
} else { } else {
return "", nil, fmt.Errorf("invalid IP version recieved") return nil, fmt.Errorf("invalid IP version recieved")
} }
ip := make(net.IP, ipSize) ip := make(net.IP, ipSize)
if _, err := conn.Read(ip); err != nil { if _, err := conn.Read(ip); err != nil {
return "", nil, fmt.Errorf("couldn't read source IP") return nil, fmt.Errorf("couldn't read source IP")
} }
sourcePort := make([]byte, 2) sourcePort := make([]byte, 2)
if _, err := conn.Read(sourcePort); err != nil { if _, err := conn.Read(sourcePort); err != nil {
return "", nil, fmt.Errorf("couldn't read source port") return nil, fmt.Errorf("couldn't read source port")
} }
destPort := make([]byte, 2) destPort := make([]byte, 2)
if _, err := conn.Read(destPort); err != nil { if _, err := conn.Read(destPort); err != nil {
return "", nil, fmt.Errorf("couldn't read destination port") return nil, fmt.Errorf("couldn't read destination port")
} }
protocolBytes := make([]byte, 1) protocolBytes := make([]byte, 1)
if _, err := conn.Read(protocolBytes); err != nil { if _, err := conn.Read(protocolBytes); err != nil {
return "", nil, fmt.Errorf("couldn't read protocol") return nil, fmt.Errorf("couldn't read protocol")
} }
var protocol string var protocol string
@ -596,17 +583,16 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if protocolBytes[1] == UDP { } else if protocolBytes[1] == UDP {
protocol = "udp" protocol = "udp"
} else { } else {
return "", nil, fmt.Errorf("invalid protocol") return nil, fmt.Errorf("invalid protocol")
} }
isActive := make([]byte, 1) isActive := make([]byte, 1)
if _, err := conn.Read(isActive); err != nil { if _, err := conn.Read(isActive); err != nil {
return "", nil, fmt.Errorf("couldn't read isActive field") return nil, fmt.Errorf("couldn't read isActive field")
} }
return "proxyStatusResponse", &ProxyStatusResponse{ return &ProxyStatusResponse{
Type: "proxyStatusResponse",
SourceIP: ip.String(), SourceIP: ip.String(),
SourcePort: binary.BigEndian.Uint16(sourcePort), SourcePort: binary.BigEndian.Uint16(sourcePort),
DestPort: binary.BigEndian.Uint16(destPort), DestPort: binary.BigEndian.Uint16(destPort),
@ -614,9 +600,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
IsActive: isActive[0] == 1, IsActive: isActive[0] == 1,
}, nil }, nil
case ProxyInstanceRequestID: case ProxyInstanceRequestID:
return "proxyInstanceRequest", &ProxyInstanceRequest{ return &ProxyInstanceRequest{}, nil
Type: "proxyInstanceRequest",
}, nil
case ProxyInstanceResponseID: case ProxyInstanceResponseID:
proxies := []*ProxyInstance{} proxies := []*ProxyInstance{}
delimiter := make([]byte, 1) delimiter := make([]byte, 1)
@ -631,13 +615,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
break break
} }
return "", nil, err return nil, err
} }
proxies = append(proxies, proxy) proxies = append(proxies, proxy)
if _, err := conn.Read(delimiter); err != nil { if _, err := conn.Read(delimiter); err != nil {
return "", nil, fmt.Errorf("couldn't read delimiter") return nil, fmt.Errorf("couldn't read delimiter")
} }
if delimiter[0] == '\r' { if delimiter[0] == '\r' {
@ -651,15 +635,12 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} }
} }
return "proxyInstanceResponse", &ProxyInstanceResponse{ return &ProxyInstanceResponse{
Type: "proxyInstanceResponse",
Proxies: proxies, Proxies: proxies,
}, errorReturn }, errorReturn
case ProxyConnectionsRequestID: case ProxyConnectionsRequestID:
return "proxyConnectionsRequest", &ProxyConnectionsRequest{ return &ProxyConnectionsRequest{}, nil
Type: "proxyConnectionsRequest",
}, nil
} }
return "", nil, fmt.Errorf("couldn't match command ID") return nil, fmt.Errorf("couldn't match command ID")
} }

View file

@ -112,11 +112,10 @@ func entrypoint(cCtx *cli.Context) error {
defer sock.Close() defer sock.Close()
startCommand := &commonbackend.Start{ startCommand := &commonbackend.Start{
Type: "start",
Arguments: backendParameters, Arguments: backendParameters,
} }
startMarshalledCommand, err := commonbackend.Marshal("start", startCommand) startMarshalledCommand, err := commonbackend.Marshal(startCommand)
if err != nil { if err != nil {
log.Errorf("failed to generate start command: %s", err.Error()) log.Errorf("failed to generate start command: %s", err.Error())
@ -128,18 +127,13 @@ func entrypoint(cCtx *cli.Context) error {
continue continue
} }
commandType, commandRaw, err := commonbackend.Unmarshal(sock) commandRaw, err := commonbackend.Unmarshal(sock)
if err != nil { if err != nil {
log.Errorf("failed to read from/unmarshal from socket: %s", err.Error()) log.Errorf("failed to read from/unmarshal from socket: %s", err.Error())
continue continue
} }
if commandType != "backendStatusResponse" {
log.Errorf("recieved commandType '%s', expecting 'backendStatusResponse'", commandType)
continue
}
command, ok := commandRaw.(*commonbackend.BackendStatusResponse) command, ok := commandRaw.(*commonbackend.BackendStatusResponse)
if !ok { if !ok {
@ -168,14 +162,13 @@ func entrypoint(cCtx *cli.Context) error {
log.Infof("initializing proxy %s:%d -> remote:%d", proxy.SourceIP, proxy.SourcePort, proxy.DestPort) log.Infof("initializing proxy %s:%d -> remote:%d", proxy.SourceIP, proxy.SourcePort, proxy.DestPort)
proxyAddCommand := &commonbackend.AddProxy{ proxyAddCommand := &commonbackend.AddProxy{
Type: "addProxy",
SourceIP: proxy.SourceIP, SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort, SourcePort: proxy.SourcePort,
DestPort: proxy.DestPort, DestPort: proxy.DestPort,
Protocol: proxy.Protocol, Protocol: proxy.Protocol,
} }
marshalledProxyCommand, err := commonbackend.Marshal("addProxy", proxyAddCommand) marshalledProxyCommand, err := commonbackend.Marshal(proxyAddCommand)
if err != nil { if err != nil {
log.Errorf("failed to generate start command: %s", err.Error()) log.Errorf("failed to generate start command: %s", err.Error())
@ -189,7 +182,7 @@ func entrypoint(cCtx *cli.Context) error {
continue continue
} }
commandType, commandRaw, err := commonbackend.Unmarshal(sock) commandRaw, err := commonbackend.Unmarshal(sock)
if err != nil { if err != nil {
log.Errorf("failed to read from/unmarshal from socket: %s", err.Error()) log.Errorf("failed to read from/unmarshal from socket: %s", err.Error())
@ -197,12 +190,6 @@ func entrypoint(cCtx *cli.Context) error {
continue continue
} }
if commandType != "proxyStatusResponse" {
log.Errorf("recieved commandType '%s', expecting 'proxyStatusResponse'", commandType)
hasAnyFailed = true
continue
}
command, ok := commandRaw.(*commonbackend.ProxyStatusResponse) command, ok := commandRaw.(*commonbackend.ProxyStatusResponse)
if !ok { if !ok {

View file

@ -1,57 +1,47 @@
package datacommands package datacommands
type ProxyStatusRequest struct { type ProxyStatusRequest struct {
Type string
ProxyID uint16 ProxyID uint16
} }
type ProxyStatusResponse struct { type ProxyStatusResponse struct {
Type string
ProxyID uint16 ProxyID uint16
IsActive bool IsActive bool
} }
type RemoveProxy struct { type RemoveProxy struct {
Type string
ProxyID uint16 ProxyID uint16
} }
type ProxyInstanceResponse struct { type ProxyInstanceResponse struct {
Type string
Proxies []uint16 Proxies []uint16
} }
type ProxyConnectionsRequest struct { type ProxyConnectionsRequest struct {
Type string
ProxyID uint16 ProxyID uint16
} }
type ProxyConnectionsResponse struct { type ProxyConnectionsResponse struct {
Type string
Connections []uint16 Connections []uint16
} }
type TCPConnectionOpened struct { type TCPConnectionOpened struct {
Type string
ProxyID uint16 ProxyID uint16
ConnectionID uint16 ConnectionID uint16
} }
type TCPConnectionClosed struct { type TCPConnectionClosed struct {
Type string
ProxyID uint16 ProxyID uint16
ConnectionID uint16 ConnectionID uint16
} }
type TCPProxyData struct { type TCPProxyData struct {
Type string
ProxyID uint16 ProxyID uint16
ConnectionID uint16 ConnectionID uint16
DataLength uint16 DataLength uint16
} }
type UDPProxyData struct { type UDPProxyData struct {
Type string
ProxyID uint16 ProxyID uint16
ClientIP string ClientIP string
ClientPort uint16 ClientPort uint16
@ -59,12 +49,10 @@ type UDPProxyData struct {
} }
type ProxyInformationRequest struct { type ProxyInformationRequest struct {
Type string
ProxyID uint16 ProxyID uint16
} }
type ProxyInformationResponse struct { type ProxyInformationResponse struct {
Type string
Exists bool Exists bool
SourceIP string SourceIP string
SourcePort uint16 SourcePort uint16
@ -73,13 +61,11 @@ type ProxyInformationResponse struct {
} }
type ProxyConnectionInformationRequest struct { type ProxyConnectionInformationRequest struct {
Type string
ProxyID uint16 ProxyID uint16
ConnectionID uint16 ConnectionID uint16
} }
type ProxyConnectionInformationResponse struct { type ProxyConnectionInformationResponse struct {
Type string
Exists bool Exists bool
ClientIP string ClientIP string
ClientPort uint16 ClientPort uint16

View file

@ -16,7 +16,7 @@ const (
) )
// Marshal takes a command (pointer to one of our structs) and converts it to a byte slice. // Marshal takes a command (pointer to one of our structs) and converts it to a byte slice.
func Marshal(_ string, command interface{}) ([]byte, error) { func Marshal(command interface{}) ([]byte, error) {
switch cmd := command.(type) { switch cmd := command.(type) {
// ProxyStatusRequest: 1 byte for the command ID + 2 bytes for the ProxyID. // ProxyStatusRequest: 1 byte for the command ID + 2 bytes for the ProxyID.
case *ProxyStatusRequest: case *ProxyStatusRequest:

View file

@ -11,11 +11,10 @@ var logLevel = os.Getenv("HERMES_LOG_LEVEL")
func TestProxyStatusRequest(t *testing.T) { func TestProxyStatusRequest(t *testing.T) {
commandInput := &ProxyStatusRequest{ commandInput := &ProxyStatusRequest{
Type: "proxyStatusRequest",
ProxyID: 19132, ProxyID: 19132,
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -26,28 +25,18 @@ func TestProxyStatusRequest(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyStatusRequest) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyStatusRequest)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.ProxyID != commandUnmarshalled.ProxyID { if commandInput.ProxyID != commandUnmarshalled.ProxyID {
t.Fail() t.Fail()
log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID) log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID)
@ -56,12 +45,11 @@ func TestProxyStatusRequest(t *testing.T) {
func TestProxyStatusResponse(t *testing.T) { func TestProxyStatusResponse(t *testing.T) {
commandInput := &ProxyStatusResponse{ commandInput := &ProxyStatusResponse{
Type: "proxyStatusResponse",
ProxyID: 19132, ProxyID: 19132,
IsActive: true, IsActive: true,
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -72,28 +60,18 @@ func TestProxyStatusResponse(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyStatusResponse) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyStatusResponse)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.ProxyID != commandUnmarshalled.ProxyID { if commandInput.ProxyID != commandUnmarshalled.ProxyID {
t.Fail() t.Fail()
log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID) log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID)
@ -107,11 +85,10 @@ func TestProxyStatusResponse(t *testing.T) {
func TestRemoveProxy(t *testing.T) { func TestRemoveProxy(t *testing.T) {
commandInput := &RemoveProxy{ commandInput := &RemoveProxy{
Type: "removeProxy",
ProxyID: 19132, ProxyID: 19132,
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -122,28 +99,18 @@ func TestRemoveProxy(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*RemoveProxy) commandUnmarshalled, ok := commandUnmarshalledRaw.(*RemoveProxy)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.ProxyID != commandUnmarshalled.ProxyID { if commandInput.ProxyID != commandUnmarshalled.ProxyID {
t.Fail() t.Fail()
log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID) log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID)
@ -152,11 +119,10 @@ func TestRemoveProxy(t *testing.T) {
func TestProxyConnectionsRequest(t *testing.T) { func TestProxyConnectionsRequest(t *testing.T) {
commandInput := &ProxyConnectionsRequest{ commandInput := &ProxyConnectionsRequest{
Type: "proxyConnectionsRequest",
ProxyID: 19132, ProxyID: 19132,
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -167,28 +133,18 @@ func TestProxyConnectionsRequest(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionsRequest) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionsRequest)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.ProxyID != commandUnmarshalled.ProxyID { if commandInput.ProxyID != commandUnmarshalled.ProxyID {
t.Fail() t.Fail()
log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID) log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID)
@ -197,11 +153,10 @@ func TestProxyConnectionsRequest(t *testing.T) {
func TestProxyConnectionsResponse(t *testing.T) { func TestProxyConnectionsResponse(t *testing.T) {
commandInput := &ProxyConnectionsResponse{ commandInput := &ProxyConnectionsResponse{
Type: "proxyConnectionsResponse",
Connections: []uint16{12831, 9455, 64219, 12, 32}, Connections: []uint16{12831, 9455, 64219, 12, 32},
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -212,28 +167,18 @@ func TestProxyConnectionsResponse(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionsResponse) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionsResponse)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
for connectionIndex, originalConnection := range commandInput.Connections { for connectionIndex, originalConnection := range commandInput.Connections {
remoteConnection := commandUnmarshalled.Connections[connectionIndex] remoteConnection := commandUnmarshalled.Connections[connectionIndex]
@ -246,11 +191,10 @@ func TestProxyConnectionsResponse(t *testing.T) {
func TestProxyInstanceResponse(t *testing.T) { func TestProxyInstanceResponse(t *testing.T) {
commandInput := &ProxyInstanceResponse{ commandInput := &ProxyInstanceResponse{
Type: "proxyInstanceResponse",
Proxies: []uint16{12831, 9455, 64219, 12, 32}, Proxies: []uint16{12831, 9455, 64219, 12, 32},
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -261,28 +205,18 @@ func TestProxyInstanceResponse(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInstanceResponse) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInstanceResponse)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
for proxyIndex, originalProxy := range commandInput.Proxies { for proxyIndex, originalProxy := range commandInput.Proxies {
remoteProxy := commandUnmarshalled.Proxies[proxyIndex] remoteProxy := commandUnmarshalled.Proxies[proxyIndex]
@ -295,12 +229,11 @@ func TestProxyInstanceResponse(t *testing.T) {
func TestTCPConnectionOpened(t *testing.T) { func TestTCPConnectionOpened(t *testing.T) {
commandInput := &TCPConnectionOpened{ commandInput := &TCPConnectionOpened{
Type: "tcpConnectionOpened",
ProxyID: 19132, ProxyID: 19132,
ConnectionID: 25565, ConnectionID: 25565,
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -311,28 +244,18 @@ func TestTCPConnectionOpened(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*TCPConnectionOpened) commandUnmarshalled, ok := commandUnmarshalledRaw.(*TCPConnectionOpened)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.ProxyID != commandUnmarshalled.ProxyID { if commandInput.ProxyID != commandUnmarshalled.ProxyID {
t.Fail() t.Fail()
log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID) log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID)
@ -346,12 +269,11 @@ func TestTCPConnectionOpened(t *testing.T) {
func TestTCPConnectionClosed(t *testing.T) { func TestTCPConnectionClosed(t *testing.T) {
commandInput := &TCPConnectionClosed{ commandInput := &TCPConnectionClosed{
Type: "tcpConnectionClosed",
ProxyID: 19132, ProxyID: 19132,
ConnectionID: 25565, ConnectionID: 25565,
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -362,28 +284,18 @@ func TestTCPConnectionClosed(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*TCPConnectionClosed) commandUnmarshalled, ok := commandUnmarshalledRaw.(*TCPConnectionClosed)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.ProxyID != commandUnmarshalled.ProxyID { if commandInput.ProxyID != commandUnmarshalled.ProxyID {
t.Fail() t.Fail()
log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID) log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID)
@ -397,13 +309,12 @@ func TestTCPConnectionClosed(t *testing.T) {
func TestTCPProxyData(t *testing.T) { func TestTCPProxyData(t *testing.T) {
commandInput := &TCPProxyData{ commandInput := &TCPProxyData{
Type: "tcpProxyData",
ProxyID: 19132, ProxyID: 19132,
ConnectionID: 25565, ConnectionID: 25565,
DataLength: 1234, DataLength: 1234,
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -414,28 +325,18 @@ func TestTCPProxyData(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*TCPProxyData) commandUnmarshalled, ok := commandUnmarshalledRaw.(*TCPProxyData)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.ProxyID != commandUnmarshalled.ProxyID { if commandInput.ProxyID != commandUnmarshalled.ProxyID {
t.Fail() t.Fail()
log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID) log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID)
@ -454,14 +355,13 @@ func TestTCPProxyData(t *testing.T) {
func TestUDPProxyData(t *testing.T) { func TestUDPProxyData(t *testing.T) {
commandInput := &UDPProxyData{ commandInput := &UDPProxyData{
Type: "udpProxyData",
ProxyID: 19132, ProxyID: 19132,
ClientIP: "68.51.23.54", ClientIP: "68.51.23.54",
ClientPort: 28173, ClientPort: 28173,
DataLength: 1234, DataLength: 1234,
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -472,28 +372,18 @@ func TestUDPProxyData(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*UDPProxyData) commandUnmarshalled, ok := commandUnmarshalledRaw.(*UDPProxyData)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.ProxyID != commandUnmarshalled.ProxyID { if commandInput.ProxyID != commandUnmarshalled.ProxyID {
t.Fail() t.Fail()
log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID) log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID)
@ -517,11 +407,10 @@ func TestUDPProxyData(t *testing.T) {
func TestProxyInformationRequest(t *testing.T) { func TestProxyInformationRequest(t *testing.T) {
commandInput := &ProxyInformationRequest{ commandInput := &ProxyInformationRequest{
Type: "proxyInformationRequest",
ProxyID: 19132, ProxyID: 19132,
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -532,28 +421,18 @@ func TestProxyInformationRequest(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInformationRequest) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInformationRequest)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.ProxyID != commandUnmarshalled.ProxyID { if commandInput.ProxyID != commandUnmarshalled.ProxyID {
t.Fail() t.Fail()
log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID) log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID)
@ -562,7 +441,6 @@ func TestProxyInformationRequest(t *testing.T) {
func TestProxyInformationResponseExists(t *testing.T) { func TestProxyInformationResponseExists(t *testing.T) {
commandInput := &ProxyInformationResponse{ commandInput := &ProxyInformationResponse{
Type: "proxyInformationResponse",
Exists: true, Exists: true,
SourceIP: "192.168.0.139", SourceIP: "192.168.0.139",
SourcePort: 19132, SourcePort: 19132,
@ -570,7 +448,7 @@ func TestProxyInformationResponseExists(t *testing.T) {
Protocol: "tcp", Protocol: "tcp",
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -581,28 +459,18 @@ func TestProxyInformationResponseExists(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInformationResponse) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInformationResponse)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.Exists != commandUnmarshalled.Exists { if commandInput.Exists != commandUnmarshalled.Exists {
t.Fail() t.Fail()
log.Printf("Exists's are not equal (orig: '%t', unmsh: '%t')", commandInput.Exists, commandUnmarshalled.Exists) log.Printf("Exists's are not equal (orig: '%t', unmsh: '%t')", commandInput.Exists, commandUnmarshalled.Exists)
@ -631,11 +499,10 @@ func TestProxyInformationResponseExists(t *testing.T) {
func TestProxyInformationResponseNoExist(t *testing.T) { func TestProxyInformationResponseNoExist(t *testing.T) {
commandInput := &ProxyInformationResponse{ commandInput := &ProxyInformationResponse{
Type: "proxyInformationResponse",
Exists: false, Exists: false,
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -646,28 +513,18 @@ func TestProxyInformationResponseNoExist(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInformationResponse) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInformationResponse)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.Exists != commandUnmarshalled.Exists { if commandInput.Exists != commandUnmarshalled.Exists {
t.Fail() t.Fail()
log.Printf("Exists's are not equal (orig: '%t', unmsh: '%t')", commandInput.Exists, commandUnmarshalled.Exists) log.Printf("Exists's are not equal (orig: '%t', unmsh: '%t')", commandInput.Exists, commandUnmarshalled.Exists)
@ -676,12 +533,11 @@ func TestProxyInformationResponseNoExist(t *testing.T) {
func TestProxyConnectionInformationRequest(t *testing.T) { func TestProxyConnectionInformationRequest(t *testing.T) {
commandInput := &ProxyConnectionInformationRequest{ commandInput := &ProxyConnectionInformationRequest{
Type: "proxyConnectionInformationRequest",
ProxyID: 19132, ProxyID: 19132,
ConnectionID: 25565, ConnectionID: 25565,
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" { if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled) log.Printf("Generated array contents: %v", commandMarshalled)
@ -692,28 +548,18 @@ func TestProxyConnectionInformationRequest(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionInformationRequest) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionInformationRequest)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.ProxyID != commandUnmarshalled.ProxyID { if commandInput.ProxyID != commandUnmarshalled.ProxyID {
t.Fail() t.Fail()
log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID) log.Printf("ProxyID's are not equal (orig: '%d', unmsh: '%d')", commandInput.ProxyID, commandUnmarshalled.ProxyID)
@ -727,13 +573,12 @@ func TestProxyConnectionInformationRequest(t *testing.T) {
func TestProxyConnectionInformationResponseExists(t *testing.T) { func TestProxyConnectionInformationResponseExists(t *testing.T) {
commandInput := &ProxyConnectionInformationResponse{ commandInput := &ProxyConnectionInformationResponse{
Type: "proxyConnectionInformationResponse",
Exists: true, Exists: true,
ClientIP: "192.168.0.139", ClientIP: "192.168.0.139",
ClientPort: 19132, ClientPort: 19132,
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -744,28 +589,18 @@ func TestProxyConnectionInformationResponseExists(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionInformationResponse) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionInformationResponse)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.Exists != commandUnmarshalled.Exists { if commandInput.Exists != commandUnmarshalled.Exists {
t.Fail() t.Fail()
log.Printf("Exists's are not equal (orig: '%t', unmsh: '%t')", commandInput.Exists, commandUnmarshalled.Exists) log.Printf("Exists's are not equal (orig: '%t', unmsh: '%t')", commandInput.Exists, commandUnmarshalled.Exists)
@ -784,11 +619,10 @@ func TestProxyConnectionInformationResponseExists(t *testing.T) {
func TestProxyConnectionInformationResponseNoExists(t *testing.T) { func TestProxyConnectionInformationResponseNoExists(t *testing.T) {
commandInput := &ProxyConnectionInformationResponse{ commandInput := &ProxyConnectionInformationResponse{
Type: "proxyConnectionInformationResponse",
Exists: false, Exists: false,
} }
commandMarshalled, err := Marshal(commandInput.Type, commandInput) commandMarshalled, err := Marshal(commandInput)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
@ -799,28 +633,18 @@ func TestProxyConnectionInformationResponseNoExists(t *testing.T) {
} }
buf := bytes.NewBuffer(commandMarshalled) buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf) commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionInformationResponse) commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionInformationResponse)
if !ok { if !ok {
t.Fatal("failed typecast") t.Fatal("failed typecast")
} }
if commandInput.Type != commandUnmarshalled.Type {
t.Fail()
log.Printf("Types are not equal (orig: %s, unmsh: %s)", commandInput.Type, commandUnmarshalled.Type)
}
if commandInput.Exists != commandUnmarshalled.Exists { if commandInput.Exists != commandUnmarshalled.Exists {
t.Fail() t.Fail()
log.Printf("Exists's are not equal (orig: '%t', unmsh: '%t')", commandInput.Exists, commandUnmarshalled.Exists) log.Printf("Exists's are not equal (orig: '%t', unmsh: '%t')", commandInput.Exists, commandUnmarshalled.Exists)

View file

@ -9,11 +9,12 @@ import (
// Unmarshal reads from the provided connection and returns // Unmarshal reads from the provided connection and returns
// the message type (as a string), the unmarshalled struct, or an error. // the message type (as a string), the unmarshalled struct, or an error.
func Unmarshal(conn io.Reader) (string, interface{}, error) { func Unmarshal(conn io.Reader) (interface{}, error) {
// Every command starts with a 1-byte command ID. // Every command starts with a 1-byte command ID.
header := make([]byte, 1) header := make([]byte, 1)
if _, err := io.ReadFull(conn, header); err != nil { if _, err := io.ReadFull(conn, header); err != nil {
return "", nil, fmt.Errorf("couldn't read command ID: %w", err) return nil, fmt.Errorf("couldn't read command ID: %w", err)
} }
cmdID := header[0] cmdID := header[0]
@ -23,13 +24,12 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2) buf := make([]byte, 2)
if _, err := io.ReadFull(conn, buf); err != nil { if _, err := io.ReadFull(conn, buf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyStatusRequest ProxyID: %w", err) return nil, fmt.Errorf("couldn't read ProxyStatusRequest ProxyID: %w", err)
} }
proxyID := binary.BigEndian.Uint16(buf) proxyID := binary.BigEndian.Uint16(buf)
return "proxyStatusRequest", &ProxyStatusRequest{ return &ProxyStatusRequest{
Type: "proxyStatusRequest",
ProxyID: proxyID, ProxyID: proxyID,
}, nil }, nil
@ -38,20 +38,19 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2) buf := make([]byte, 2)
if _, err := io.ReadFull(conn, buf); err != nil { if _, err := io.ReadFull(conn, buf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyStatusResponse ProxyID: %w", err) return nil, fmt.Errorf("couldn't read ProxyStatusResponse ProxyID: %w", err)
} }
proxyID := binary.BigEndian.Uint16(buf) proxyID := binary.BigEndian.Uint16(buf)
boolBuf := make([]byte, 1) boolBuf := make([]byte, 1)
if _, err := io.ReadFull(conn, boolBuf); err != nil { if _, err := io.ReadFull(conn, boolBuf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyStatusResponse IsActive: %w", err) return nil, fmt.Errorf("couldn't read ProxyStatusResponse IsActive: %w", err)
} }
isActive := boolBuf[0] != 0 isActive := boolBuf[0] != 0
return "proxyStatusResponse", &ProxyStatusResponse{ return &ProxyStatusResponse{
Type: "proxyStatusResponse",
ProxyID: proxyID, ProxyID: proxyID,
IsActive: isActive, IsActive: isActive,
}, nil }, nil
@ -61,13 +60,12 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2) buf := make([]byte, 2)
if _, err := io.ReadFull(conn, buf); err != nil { if _, err := io.ReadFull(conn, buf); err != nil {
return "", nil, fmt.Errorf("couldn't read RemoveProxy ProxyID: %w", err) return nil, fmt.Errorf("couldn't read RemoveProxy ProxyID: %w", err)
} }
proxyID := binary.BigEndian.Uint16(buf) proxyID := binary.BigEndian.Uint16(buf)
return "removeProxy", &RemoveProxy{ return &RemoveProxy{
Type: "removeProxy",
ProxyID: proxyID, ProxyID: proxyID,
}, nil }, nil
@ -76,13 +74,12 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2) buf := make([]byte, 2)
if _, err := io.ReadFull(conn, buf); err != nil { if _, err := io.ReadFull(conn, buf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyConnectionsRequest ProxyID: %w", err) return nil, fmt.Errorf("couldn't read ProxyConnectionsRequest ProxyID: %w", err)
} }
proxyID := binary.BigEndian.Uint16(buf) proxyID := binary.BigEndian.Uint16(buf)
return "proxyConnectionsRequest", &ProxyConnectionsRequest{ return &ProxyConnectionsRequest{
Type: "proxyConnectionsRequest",
ProxyID: proxyID, ProxyID: proxyID,
}, nil }, nil
@ -91,7 +88,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2) buf := make([]byte, 2)
if _, err := io.ReadFull(conn, buf); err != nil { if _, err := io.ReadFull(conn, buf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyConnectionsResponse length: %w", err) return nil, fmt.Errorf("couldn't read ProxyConnectionsResponse length: %w", err)
} }
length := binary.BigEndian.Uint16(buf) length := binary.BigEndian.Uint16(buf)
@ -108,8 +105,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
connections[connectionIndex] = binary.BigEndian.Uint16(buf) connections[connectionIndex] = binary.BigEndian.Uint16(buf)
} }
return "proxyConnectionsResponse", &ProxyConnectionsResponse{ return &ProxyConnectionsResponse{
Type: "proxyConnectionsResponse",
Connections: connections, Connections: connections,
}, failedDuringReading }, failedDuringReading
@ -118,7 +114,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2) buf := make([]byte, 2)
if _, err := io.ReadFull(conn, buf); err != nil { if _, err := io.ReadFull(conn, buf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyConnectionsResponse length: %w", err) return nil, fmt.Errorf("couldn't read ProxyConnectionsResponse length: %w", err)
} }
length := binary.BigEndian.Uint16(buf) length := binary.BigEndian.Uint16(buf)
@ -135,8 +131,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
proxies[connectionIndex] = binary.BigEndian.Uint16(buf) proxies[connectionIndex] = binary.BigEndian.Uint16(buf)
} }
return "proxyInstanceResponse", &ProxyInstanceResponse{ return &ProxyInstanceResponse{
Type: "proxyInstanceResponse",
Proxies: proxies, Proxies: proxies,
}, failedDuringReading }, failedDuringReading
@ -145,14 +140,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2+2) buf := make([]byte, 2+2)
if _, err := io.ReadFull(conn, buf); err != nil { if _, err := io.ReadFull(conn, buf); err != nil {
return "", nil, fmt.Errorf("couldn't read TCPConnectionOpened fields: %w", err) return nil, fmt.Errorf("couldn't read TCPConnectionOpened fields: %w", err)
} }
proxyID := binary.BigEndian.Uint16(buf[0:2]) proxyID := binary.BigEndian.Uint16(buf[0:2])
connectionID := binary.BigEndian.Uint16(buf[2:4]) connectionID := binary.BigEndian.Uint16(buf[2:4])
return "tcpConnectionOpened", &TCPConnectionOpened{ return &TCPConnectionOpened{
Type: "tcpConnectionOpened",
ProxyID: proxyID, ProxyID: proxyID,
ConnectionID: connectionID, ConnectionID: connectionID,
}, nil }, nil
@ -162,14 +156,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2+2) buf := make([]byte, 2+2)
if _, err := io.ReadFull(conn, buf); err != nil { if _, err := io.ReadFull(conn, buf); err != nil {
return "", nil, fmt.Errorf("couldn't read TCPConnectionClosed fields: %w", err) return nil, fmt.Errorf("couldn't read TCPConnectionClosed fields: %w", err)
} }
proxyID := binary.BigEndian.Uint16(buf[0:2]) proxyID := binary.BigEndian.Uint16(buf[0:2])
connectionID := binary.BigEndian.Uint16(buf[2:4]) connectionID := binary.BigEndian.Uint16(buf[2:4])
return "tcpConnectionClosed", &TCPConnectionClosed{ return &TCPConnectionClosed{
Type: "tcpConnectionClosed",
ProxyID: proxyID, ProxyID: proxyID,
ConnectionID: connectionID, ConnectionID: connectionID,
}, nil }, nil
@ -179,15 +172,14 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2+2+2) buf := make([]byte, 2+2+2)
if _, err := io.ReadFull(conn, buf); err != nil { if _, err := io.ReadFull(conn, buf); err != nil {
return "", nil, fmt.Errorf("couldn't read TCPProxyData fields: %w", err) return nil, fmt.Errorf("couldn't read TCPProxyData fields: %w", err)
} }
proxyID := binary.BigEndian.Uint16(buf[0:2]) proxyID := binary.BigEndian.Uint16(buf[0:2])
connectionID := binary.BigEndian.Uint16(buf[2:4]) connectionID := binary.BigEndian.Uint16(buf[2:4])
dataLength := binary.BigEndian.Uint16(buf[4:6]) dataLength := binary.BigEndian.Uint16(buf[4:6])
return "tcpProxyData", &TCPProxyData{ return &TCPProxyData{
Type: "tcpProxyData",
ProxyID: proxyID, ProxyID: proxyID,
ConnectionID: connectionID, ConnectionID: connectionID,
DataLength: dataLength, DataLength: dataLength,
@ -201,7 +193,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2) buf := make([]byte, 2)
if _, err := io.ReadFull(conn, buf); err != nil { if _, err := io.ReadFull(conn, buf); err != nil {
return "", nil, fmt.Errorf("couldn't read UDPProxyData ProxyID/ConnectionID: %w", err) return nil, fmt.Errorf("couldn't read UDPProxyData ProxyID/ConnectionID: %w", err)
} }
proxyID := binary.BigEndian.Uint16(buf) proxyID := binary.BigEndian.Uint16(buf)
@ -210,7 +202,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
ipVerBuf := make([]byte, 1) ipVerBuf := make([]byte, 1)
if _, err := io.ReadFull(conn, ipVerBuf); err != nil { if _, err := io.ReadFull(conn, ipVerBuf); err != nil {
return "", nil, fmt.Errorf("couldn't read UDPProxyData IP version: %w", err) return nil, fmt.Errorf("couldn't read UDPProxyData IP version: %w", err)
} }
var ipSize int var ipSize int
@ -220,13 +212,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if ipVerBuf[0] == 6 { } else if ipVerBuf[0] == 6 {
ipSize = IPv6Size ipSize = IPv6Size
} else { } else {
return "", nil, fmt.Errorf("invalid IP version received: %v", ipVerBuf[0]) return nil, fmt.Errorf("invalid IP version received: %v", ipVerBuf[0])
} }
// Read the IP bytes. // Read the IP bytes.
ipBytes := make([]byte, ipSize) ipBytes := make([]byte, ipSize)
if _, err := io.ReadFull(conn, ipBytes); err != nil { if _, err := io.ReadFull(conn, ipBytes); err != nil {
return "", nil, fmt.Errorf("couldn't read UDPProxyData IP bytes: %w", err) return nil, fmt.Errorf("couldn't read UDPProxyData IP bytes: %w", err)
} }
clientIP := net.IP(ipBytes).String() clientIP := net.IP(ipBytes).String()
@ -234,7 +226,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
portBuf := make([]byte, 2) portBuf := make([]byte, 2)
if _, err := io.ReadFull(conn, portBuf); err != nil { if _, err := io.ReadFull(conn, portBuf); err != nil {
return "", nil, fmt.Errorf("couldn't read UDPProxyData ClientPort: %w", err) return nil, fmt.Errorf("couldn't read UDPProxyData ClientPort: %w", err)
} }
clientPort := binary.BigEndian.Uint16(portBuf) clientPort := binary.BigEndian.Uint16(portBuf)
@ -243,13 +235,12 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
dataLengthBuf := make([]byte, 2) dataLengthBuf := make([]byte, 2)
if _, err := io.ReadFull(conn, dataLengthBuf); err != nil { if _, err := io.ReadFull(conn, dataLengthBuf); err != nil {
return "", nil, fmt.Errorf("couldn't read UDPProxyData DataLength: %w", err) return nil, fmt.Errorf("couldn't read UDPProxyData DataLength: %w", err)
} }
dataLength := binary.BigEndian.Uint16(dataLengthBuf) dataLength := binary.BigEndian.Uint16(dataLengthBuf)
return "udpProxyData", &UDPProxyData{ return &UDPProxyData{
Type: "udpProxyData",
ProxyID: proxyID, ProxyID: proxyID,
ClientIP: clientIP, ClientIP: clientIP,
ClientPort: clientPort, ClientPort: clientPort,
@ -261,13 +252,12 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2) buf := make([]byte, 2)
if _, err := io.ReadFull(conn, buf); err != nil { if _, err := io.ReadFull(conn, buf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyInformationRequest ProxyID: %w", err) return nil, fmt.Errorf("couldn't read ProxyInformationRequest ProxyID: %w", err)
} }
proxyID := binary.BigEndian.Uint16(buf) proxyID := binary.BigEndian.Uint16(buf)
return "proxyInformationRequest", &ProxyInformationRequest{ return &ProxyInformationRequest{
Type: "proxyInformationRequest",
ProxyID: proxyID, ProxyID: proxyID,
}, nil }, nil
@ -279,14 +269,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
boolBuf := make([]byte, 1) boolBuf := make([]byte, 1)
if _, err := io.ReadFull(conn, boolBuf); err != nil { if _, err := io.ReadFull(conn, boolBuf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyInformationResponse Exists flag: %w", err) return nil, fmt.Errorf("couldn't read ProxyInformationResponse Exists flag: %w", err)
} }
exists := boolBuf[0] != 0 exists := boolBuf[0] != 0
if !exists { if !exists {
return "proxyInformationResponse", &ProxyInformationResponse{ return &ProxyInformationResponse{
Type: "proxyInformationResponse",
Exists: exists, Exists: exists,
}, nil }, nil
} }
@ -295,7 +284,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
ipVerBuf := make([]byte, 1) ipVerBuf := make([]byte, 1)
if _, err := io.ReadFull(conn, ipVerBuf); err != nil { if _, err := io.ReadFull(conn, ipVerBuf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyInformationResponse IP version: %w", err) return nil, fmt.Errorf("couldn't read ProxyInformationResponse IP version: %w", err)
} }
var ipSize int var ipSize int
@ -305,14 +294,14 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if ipVerBuf[0] == 6 { } else if ipVerBuf[0] == 6 {
ipSize = IPv6Size ipSize = IPv6Size
} else { } else {
return "", nil, fmt.Errorf("invalid IP version in ProxyInformationResponse: %v", ipVerBuf[0]) return nil, fmt.Errorf("invalid IP version in ProxyInformationResponse: %v", ipVerBuf[0])
} }
// Read the source IP bytes. // Read the source IP bytes.
ipBytes := make([]byte, ipSize) ipBytes := make([]byte, ipSize)
if _, err := io.ReadFull(conn, ipBytes); err != nil { if _, err := io.ReadFull(conn, ipBytes); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyInformationResponse IP bytes: %w", err) return nil, fmt.Errorf("couldn't read ProxyInformationResponse IP bytes: %w", err)
} }
sourceIP := net.IP(ipBytes).String() sourceIP := net.IP(ipBytes).String()
@ -321,7 +310,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
portsBuf := make([]byte, 2+2) portsBuf := make([]byte, 2+2)
if _, err := io.ReadFull(conn, portsBuf); err != nil { if _, err := io.ReadFull(conn, portsBuf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyInformationResponse ports: %w", err) return nil, fmt.Errorf("couldn't read ProxyInformationResponse ports: %w", err)
} }
sourcePort := binary.BigEndian.Uint16(portsBuf[0:2]) sourcePort := binary.BigEndian.Uint16(portsBuf[0:2])
@ -331,19 +320,20 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
protoBuf := make([]byte, 1) protoBuf := make([]byte, 1)
if _, err := io.ReadFull(conn, protoBuf); err != nil { if _, err := io.ReadFull(conn, protoBuf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyInformationResponse protocol: %w", err) return nil, fmt.Errorf("couldn't read ProxyInformationResponse protocol: %w", err)
} }
var protocol string var protocol string
if protoBuf[0] == TCP { if protoBuf[0] == TCP {
protocol = "tcp" protocol = "tcp"
} else if protoBuf[0] == UDP { } else if protoBuf[0] == UDP {
protocol = "udp" protocol = "udp"
} else { } else {
return "", nil, fmt.Errorf("invalid protocol value in ProxyInformationResponse: %d", protoBuf[0]) return nil, fmt.Errorf("invalid protocol value in ProxyInformationResponse: %d", protoBuf[0])
} }
return "proxyInformationResponse", &ProxyInformationResponse{ return &ProxyInformationResponse{
Type: "proxyInformationResponse",
Exists: exists, Exists: exists,
SourceIP: sourceIP, SourceIP: sourceIP,
SourcePort: sourcePort, SourcePort: sourcePort,
@ -356,14 +346,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2+2) buf := make([]byte, 2+2)
if _, err := io.ReadFull(conn, buf); err != nil { if _, err := io.ReadFull(conn, buf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyConnectionInformationRequest fields: %w", err) return nil, fmt.Errorf("couldn't read ProxyConnectionInformationRequest fields: %w", err)
} }
proxyID := binary.BigEndian.Uint16(buf[0:2]) proxyID := binary.BigEndian.Uint16(buf[0:2])
connectionID := binary.BigEndian.Uint16(buf[2:4]) connectionID := binary.BigEndian.Uint16(buf[2:4])
return "proxyConnectionInformationRequest", &ProxyConnectionInformationRequest{ return &ProxyConnectionInformationRequest{
Type: "proxyConnectionInformationRequest",
ProxyID: proxyID, ProxyID: proxyID,
ConnectionID: connectionID, ConnectionID: connectionID,
}, nil }, nil
@ -374,14 +363,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
// Read Exists flag. // Read Exists flag.
boolBuf := make([]byte, 1) boolBuf := make([]byte, 1)
if _, err := io.ReadFull(conn, boolBuf); err != nil { if _, err := io.ReadFull(conn, boolBuf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyConnectionInformationResponse Exists flag: %w", err) return nil, fmt.Errorf("couldn't read ProxyConnectionInformationResponse Exists flag: %w", err)
} }
exists := boolBuf[0] != 0 exists := boolBuf[0] != 0
if !exists { if !exists {
return "proxyConnectionInformationResponse", &ProxyConnectionInformationResponse{ return &ProxyConnectionInformationResponse{
Type: "proxyConnectionInformationResponse",
Exists: exists, Exists: exists,
}, nil }, nil
} }
@ -390,11 +378,11 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
ipVerBuf := make([]byte, 1) ipVerBuf := make([]byte, 1)
if _, err := io.ReadFull(conn, ipVerBuf); err != nil { if _, err := io.ReadFull(conn, ipVerBuf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyConnectionInformationResponse IP version: %w", err) return nil, fmt.Errorf("couldn't read ProxyConnectionInformationResponse IP version: %w", err)
} }
if ipVerBuf[0] != 4 && ipVerBuf[0] != 6 { if ipVerBuf[0] != 4 && ipVerBuf[0] != 6 {
return "", nil, fmt.Errorf("invalid IP version in ProxyConnectionInformationResponse: %v", ipVerBuf[0]) return nil, fmt.Errorf("invalid IP version in ProxyConnectionInformationResponse: %v", ipVerBuf[0])
} }
var ipSize int var ipSize int
@ -409,7 +397,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
ipBytes := make([]byte, ipSize) ipBytes := make([]byte, ipSize)
if _, err := io.ReadFull(conn, ipBytes); err != nil { if _, err := io.ReadFull(conn, ipBytes); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyConnectionInformationResponse IP bytes: %w", err) return nil, fmt.Errorf("couldn't read ProxyConnectionInformationResponse IP bytes: %w", err)
} }
clientIP := net.IP(ipBytes).String() clientIP := net.IP(ipBytes).String()
@ -418,18 +406,17 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
portBuf := make([]byte, 2) portBuf := make([]byte, 2)
if _, err := io.ReadFull(conn, portBuf); err != nil { if _, err := io.ReadFull(conn, portBuf); err != nil {
return "", nil, fmt.Errorf("couldn't read ProxyConnectionInformationResponse ClientPort: %w", err) return nil, fmt.Errorf("couldn't read ProxyConnectionInformationResponse ClientPort: %w", err)
} }
clientPort := binary.BigEndian.Uint16(portBuf) clientPort := binary.BigEndian.Uint16(portBuf)
return "proxyConnectionInformationResponse", &ProxyConnectionInformationResponse{ return &ProxyConnectionInformationResponse{
Type: "proxyConnectionInformationResponse",
Exists: exists, Exists: exists,
ClientIP: clientIP, ClientIP: clientIP,
ClientPort: clientPort, ClientPort: clientPort,
}, nil }, nil
default: default:
return "", nil, fmt.Errorf("unknown command id: %v", cmdID) return nil, fmt.Errorf("unknown command id: %v", cmdID)
} }
} }

View file

@ -36,7 +36,7 @@ func (helper *BackendApplicationHelper) Start() error {
log.Debug("Sucessfully connected") log.Debug("Sucessfully connected")
for { for {
_, commandRaw, err := datacommands.Unmarshal(helper.socket) commandRaw, err := datacommands.Unmarshal(helper.socket)
if err != nil && err.Error() != "couldn't match command ID" { if err != nil && err.Error() != "couldn't match command ID" {
return err return err
@ -47,11 +47,10 @@ func (helper *BackendApplicationHelper) Start() error {
connections := helper.Backend.GetAllClientConnections(command.ProxyID) connections := helper.Backend.GetAllClientConnections(command.ProxyID)
serverParams := &datacommands.ProxyConnectionsResponse{ serverParams := &datacommands.ProxyConnectionsResponse{
Type: "proxyConnectionsResponse",
Connections: connections, Connections: connections,
} }
byteData, err := datacommands.Marshal(serverParams.Type, serverParams) byteData, err := datacommands.Marshal(serverParams)
if err != nil { if err != nil {
return err return err
@ -73,12 +72,11 @@ func (helper *BackendApplicationHelper) Start() error {
} }
response := &datacommands.ProxyStatusResponse{ response := &datacommands.ProxyStatusResponse{
Type: "proxyStatusResponse",
ProxyID: command.ProxyID, ProxyID: command.ProxyID,
IsActive: hasAnyFailed, IsActive: hasAnyFailed,
} }
responseMarshalled, err := commonbackend.Marshal(response.Type, response) responseMarshalled, err := datacommands.Marshal(response)
if err != nil { if err != nil {
log.Error("failed to marshal response: %s", err.Error()) log.Error("failed to marshal response: %s", err.Error())
@ -87,7 +85,7 @@ func (helper *BackendApplicationHelper) Start() error {
helper.socket.Write(responseMarshalled) helper.socket.Write(responseMarshalled)
default: default:
_, commandRaw, err := commonbackend.Unmarshal(helper.socket) commandRaw, err := commonbackend.Unmarshal(helper.socket)
if err != nil { if err != nil {
return err return err
@ -110,13 +108,12 @@ func (helper *BackendApplicationHelper) Start() error {
} }
response := &commonbackend.BackendStatusResponse{ response := &commonbackend.BackendStatusResponse{
Type: "backendStatusResponse",
IsRunning: ok, IsRunning: ok,
StatusCode: statusCode, StatusCode: statusCode,
Message: message, Message: message,
} }
responseMarshalled, err := commonbackend.Marshal(response.Type, response) responseMarshalled, err := commonbackend.Marshal(response)
if err != nil { if err != nil {
log.Error("failed to marshal response: %s", err.Error()) log.Error("failed to marshal response: %s", err.Error())
@ -140,13 +137,12 @@ func (helper *BackendApplicationHelper) Start() error {
} }
response := &commonbackend.BackendStatusResponse{ response := &commonbackend.BackendStatusResponse{
Type: "backendStatusResponse",
IsRunning: !ok, IsRunning: !ok,
StatusCode: statusCode, StatusCode: statusCode,
Message: message, Message: message,
} }
responseMarshalled, err := commonbackend.Marshal(response.Type, response) responseMarshalled, err := commonbackend.Marshal(response)
if err != nil { if err != nil {
log.Error("failed to marshal response: %s", err.Error()) log.Error("failed to marshal response: %s", err.Error())
@ -170,13 +166,12 @@ func (helper *BackendApplicationHelper) Start() error {
} }
response := &commonbackend.BackendStatusResponse{ response := &commonbackend.BackendStatusResponse{
Type: "backendStatusResponse",
IsRunning: ok, IsRunning: ok,
StatusCode: statusCode, StatusCode: statusCode,
Message: message, Message: message,
} }
responseMarshalled, err := commonbackend.Marshal(response.Type, response) responseMarshalled, err := commonbackend.Marshal(response)
if err != nil { if err != nil {
log.Error("failed to marshal response: %s", err.Error()) log.Error("failed to marshal response: %s", err.Error())
@ -197,12 +192,11 @@ func (helper *BackendApplicationHelper) Start() error {
} }
response := &datacommands.ProxyStatusResponse{ response := &datacommands.ProxyStatusResponse{
Type: "proxyStatusResponse",
ProxyID: id, ProxyID: id,
IsActive: !hasAnyFailed, IsActive: !hasAnyFailed,
} }
responseMarshalled, err := commonbackend.Marshal(response.Type, response) responseMarshalled, err := datacommands.Marshal(response)
if err != nil { if err != nil {
log.Error("failed to marshal response: %s", err.Error()) log.Error("failed to marshal response: %s", err.Error())
@ -212,10 +206,9 @@ func (helper *BackendApplicationHelper) Start() error {
helper.socket.Write(responseMarshalled) helper.socket.Write(responseMarshalled)
case *commonbackend.CheckClientParameters: case *commonbackend.CheckClientParameters:
resp := helper.Backend.CheckParametersForConnections(command) resp := helper.Backend.CheckParametersForConnections(command)
resp.Type = "checkParametersResponse"
resp.InResponseTo = "checkClientParameters" resp.InResponseTo = "checkClientParameters"
byteData, err := commonbackend.Marshal(resp.Type, resp) byteData, err := commonbackend.Marshal(resp)
if err != nil { if err != nil {
return err return err
@ -226,10 +219,9 @@ func (helper *BackendApplicationHelper) Start() error {
} }
case *commonbackend.CheckServerParameters: case *commonbackend.CheckServerParameters:
resp := helper.Backend.CheckParametersForBackend(command.Arguments) resp := helper.Backend.CheckParametersForBackend(command.Arguments)
resp.Type = "checkParametersResponse"
resp.InResponseTo = "checkServerParameters" resp.InResponseTo = "checkServerParameters"
byteData, err := commonbackend.Marshal(resp.Type, resp) byteData, err := commonbackend.Marshal(resp)
if err != nil { if err != nil {
return err return err