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"
)
func handleCommand(commandType string, command interface{}, sock net.Conn, rtcChan chan interface{}) error {
bytes, err := commonbackend.Marshal(commandType, command)
func handleCommand(command interface{}, sock net.Conn, rtcChan chan interface{}) error {
bytes, err := commonbackend.Marshal(command)
if err != nil {
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())
}
_, data, err := commonbackend.Unmarshal(sock)
data, err := commonbackend.Unmarshal(sock)
if err != nil {
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
// 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.
statusResponse, err := runtime.ProcessCommand(&commonbackend.BackendStatusRequest{
Type: "backendStatusRequest",
})
statusResponse, err := runtime.ProcessCommand(&commonbackend.BackendStatusRequest{})
if err != nil {
log.Warnf("Failed to get response for backend (in backend runtime keep alive): %s", err.Error())
@ -167,110 +165,14 @@ func (runtime *Runtime) goRoutineHandler() error {
continue
}
switch command := messageData.Message.(type) {
case *commonbackend.AddProxy:
err := handleCommand("addProxy", command, sock, messageData.Channel)
err := handleCommand(messageData.Message, sock, messageData.Channel)
if err != nil {
log.Warnf("failed to handle command in backend runtime instance: %s", err.Error())
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
}
if strings.HasPrefix(err.Error(), "failed to write message") {
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

View file

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

View file

@ -118,9 +118,7 @@ func GetConnections(c *gin.Context) {
return
}
backendResponse, err := backendRuntime.ProcessCommand(&commonbackend.ProxyConnectionsRequest{
Type: "proxyConnectionsRequest",
})
backendResponse, err := backendRuntime.ProcessCommand(&commonbackend.ProxyConnectionsRequest{})
if err != nil {
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{
Type: "addProxy",
SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort,
DestPort: proxy.DestinationPort,

View file

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

View file

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

View file

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

View file

@ -108,8 +108,7 @@ func apiEntrypoint(cCtx *cli.Context) error {
return
}
marshalledStartCommand, err := commonbackend.Marshal("start", &commonbackend.Start{
Type: "start",
marshalledStartCommand, err := commonbackend.Marshal(&commonbackend.Start{
Arguments: backendParameters,
})
@ -123,7 +122,7 @@ func apiEntrypoint(cCtx *cli.Context) error {
return
}
_, backendResponse, err := commonbackend.Unmarshal(conn)
backendResponse, err := commonbackend.Unmarshal(conn)
if err != nil {
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 {
log.Infof("Starting up route #%d for backend #%d: %s", proxy.ID, backend.ID, proxy.Name)
marhalledCommand, err := commonbackend.Marshal("addProxy", &commonbackend.AddProxy{
Type: "addProxy",
marhalledCommand, err := commonbackend.Marshal(&commonbackend.AddProxy{
SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort,
DestPort: proxy.DestinationPort,
@ -170,7 +168,7 @@ func apiEntrypoint(cCtx *cli.Context) error {
continue
}
_, backendResponse, err := commonbackend.Unmarshal(conn)
backendResponse, err := commonbackend.Unmarshal(conn)
if err != nil {
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{
Type: "start",
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)
backendResponse, err := backendInstance.ProcessCommand(&commonbackend.AddProxy{
Type: "addProxy",
SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort,
DestPort: proxy.DestinationPort,

View file

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

View file

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

View file

@ -84,7 +84,7 @@ func marshalIndividualProxyStruct(conn *ProxyInstance) ([]byte, error) {
return proxyBlock, nil
}
func Marshal(_ string, command interface{}) ([]byte, error) {
func Marshal(command interface{}) ([]byte, error) {
switch command := command.(type) {
case *Start:
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) {
commandInput := &Start{
Type: "start",
Arguments: []byte("Hello from automated testing"),
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -26,39 +25,27 @@ func TestStart(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*Start)
if !ok {
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) {
log.Fatalf("Arguments are not equal (orig: '%s', unmsh: '%s')", string(commandInput.Arguments), string(commandUnmarshalled.Arguments))
}
}
func TestStop(t *testing.T) {
commandInput := &Stop{
Type: "stop",
}
commandInput := &Stop{}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -69,39 +56,28 @@ func TestStop(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*Stop)
_, ok := commandUnmarshalledRaw.(*Stop)
if !ok {
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) {
commandInput := &AddProxy{
Type: "addProxy",
SourceIP: "192.168.0.139",
SourcePort: 19132,
DestPort: 19132,
Protocol: "tcp",
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -112,28 +88,18 @@ func TestAddConnection(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*AddProxy)
if !ok {
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 {
t.Fail()
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) {
commandInput := &RemoveProxy{
Type: "removeProxy",
SourceIP: "192.168.0.139",
SourcePort: 19132,
DestPort: 19132,
Protocol: "tcp",
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if err != nil {
t.Fatal(err.Error())
@ -175,28 +140,18 @@ func TestRemoveConnection(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*RemoveProxy)
if !ok {
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 {
t.Fail()
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) {
commandInput := &ProxyConnectionsResponse{
Type: "proxyConnectionsResponse",
Connections: []*ProxyClientConnection{
{
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 {
t.Fatal(err.Error())
@ -257,28 +211,18 @@ func TestGetAllConnections(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionsResponse)
if !ok {
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 {
remoteConnection := commandUnmarshalled.Connections[commandIndex]
@ -311,14 +255,13 @@ func TestGetAllConnections(t *testing.T) {
func TestCheckClientParameters(t *testing.T) {
commandInput := &CheckClientParameters{
Type: "checkClientParameters",
SourceIP: "192.168.0.139",
SourcePort: 19132,
DestPort: 19132,
Protocol: "tcp",
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if err != nil {
t.Fatal(err.Error())
@ -329,28 +272,18 @@ func TestCheckClientParameters(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
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)
if !ok {
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 {
t.Fail()
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) {
commandInput := &CheckServerParameters{
Type: "checkServerParameters",
Arguments: []byte("Hello from automated testing"),
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -389,28 +321,18 @@ func TestCheckServerParameters(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*CheckServerParameters)
if !ok {
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) {
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) {
commandInput := &CheckParametersResponse{
Type: "checkParametersResponse",
InResponseTo: "checkClientParameters",
IsValid: true,
Message: "Hello from automated testing",
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if err != nil {
t.Fatal(err.Error())
@ -435,28 +356,18 @@ func TestCheckParametersResponse(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
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)
if !ok {
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 {
t.Fail()
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) {
commandInput := &BackendStatusRequest{
Type: "backendStatusRequest",
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandInput := &BackendStatusRequest{}
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -489,38 +397,27 @@ func TestBackendStatusRequest(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*BackendStatusRequest)
_, ok := commandUnmarshalledRaw.(*BackendStatusRequest)
if !ok {
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) {
commandInput := &BackendStatusResponse{
Type: "backendStatusResponse",
IsRunning: true,
StatusCode: StatusFailure,
Message: "Hello from automated testing",
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -531,28 +428,18 @@ func TestBackendStatusResponse(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*BackendStatusResponse)
if !ok {
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 {
t.Fail()
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) {
commandInput := &ProxyStatusRequest{
Type: "proxyStatusRequest",
SourceIP: "192.168.0.139",
SourcePort: 19132,
DestPort: 19132,
Protocol: "tcp",
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if err != nil {
t.Fatal(err.Error())
@ -589,28 +475,18 @@ func TestProxyStatusRequest(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyStatusRequest)
if !ok {
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 {
t.Fail()
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) {
commandInput := &ProxyStatusResponse{
Type: "proxyStatusResponse",
SourceIP: "192.168.0.139",
SourcePort: 19132,
DestPort: 19132,
@ -642,7 +517,7 @@ func TestProxyStatusResponse(t *testing.T) {
IsActive: true,
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if err != nil {
t.Fatal(err.Error())
@ -653,28 +528,18 @@ func TestProxyStatusResponse(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyStatusResponse)
if !ok {
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 {
t.Fail()
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) {
commandInput := &ProxyInstanceRequest{
Type: "proxyInstanceRequest",
}
commandInput := &ProxyInstanceRequest{}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -717,32 +580,21 @@ func TestProxyConnectionRequest(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInstanceRequest)
_, ok := commandUnmarshalledRaw.(*ProxyInstanceRequest)
if !ok {
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) {
commandInput := &ProxyInstanceResponse{
Type: "proxyInstanceResponse",
Proxies: []*ProxyInstance{
{
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 {
t.Fatal(err.Error())
@ -776,28 +628,18 @@ func TestProxyConnectionResponse(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInstanceResponse)
if !ok {
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 {
remoteProxy := commandUnmarshalled.Proxies[proxyIndex]

View file

@ -142,11 +142,11 @@ func unmarshalIndividualProxyStruct(conn io.Reader) (*ProxyInstance, error) {
}, nil
}
func Unmarshal(conn io.Reader) (string, interface{}, error) {
func Unmarshal(conn io.Reader) (interface{}, error) {
commandType := make([]byte, 1)
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] {
@ -154,28 +154,25 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
argumentsLength := make([]byte, 2)
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))
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{
Type: "start",
return &Start{
Arguments: arguments,
}, nil
case StopID:
return "stop", &Stop{
Type: "stop",
}, nil
return &Stop{}, nil
case AddProxyID:
ipVersion := make([]byte, 1)
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
@ -185,31 +182,31 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if ipVersion[0] == 6 {
ipSize = IPv6Size
} else {
return "", nil, fmt.Errorf("invalid IP version recieved")
return nil, fmt.Errorf("invalid IP version recieved")
}
ip := make(net.IP, ipSize)
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)
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)
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)
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
@ -219,11 +216,10 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if protocolBytes[1] == UDP {
protocol = "udp"
} else {
return "", nil, fmt.Errorf("invalid protocol")
return nil, fmt.Errorf("invalid protocol")
}
return "addProxy", &AddProxy{
Type: "addProxy",
return &AddProxy{
SourceIP: ip.String(),
SourcePort: binary.BigEndian.Uint16(sourcePort),
DestPort: binary.BigEndian.Uint16(destPort),
@ -233,7 +229,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
ipVersion := make([]byte, 1)
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
@ -243,31 +239,31 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if ipVersion[0] == 6 {
ipSize = IPv6Size
} else {
return "", nil, fmt.Errorf("invalid IP version recieved")
return nil, fmt.Errorf("invalid IP version recieved")
}
ip := make(net.IP, ipSize)
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)
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)
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)
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
@ -277,11 +273,10 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if protocolBytes[1] == UDP {
protocol = "udp"
} else {
return "", nil, fmt.Errorf("invalid protocol")
return nil, fmt.Errorf("invalid protocol")
}
return "removeProxy", &RemoveProxy{
Type: "removeProxy",
return &RemoveProxy{
SourceIP: ip.String(),
SourcePort: binary.BigEndian.Uint16(sourcePort),
DestPort: binary.BigEndian.Uint16(destPort),
@ -301,13 +296,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
break
}
return "", nil, err
return nil, err
}
connections = append(connections, connection)
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' {
@ -321,15 +316,14 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
}
}
return "proxyConnectionsResponse", &ProxyConnectionsResponse{
Type: "proxyConnectionsResponse",
return &ProxyConnectionsResponse{
Connections: connections,
}, errorReturn
case CheckClientParametersID:
ipVersion := make([]byte, 1)
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
@ -339,31 +333,31 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if ipVersion[0] == 6 {
ipSize = IPv6Size
} else {
return "", nil, fmt.Errorf("invalid IP version recieved")
return nil, fmt.Errorf("invalid IP version recieved")
}
ip := make(net.IP, ipSize)
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)
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)
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)
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
@ -373,11 +367,10 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if protocolBytes[1] == UDP {
protocol = "udp"
} else {
return "", nil, fmt.Errorf("invalid protocol")
return nil, fmt.Errorf("invalid protocol")
}
return "checkClientParameters", &CheckClientParameters{
Type: "checkClientParameters",
return &CheckClientParameters{
SourceIP: ip.String(),
SourcePort: binary.BigEndian.Uint16(sourcePort),
DestPort: binary.BigEndian.Uint16(destPort),
@ -387,24 +380,23 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
argumentsLength := make([]byte, 2)
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))
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{
Type: "checkServerParameters",
return &CheckServerParameters{
Arguments: arguments,
}, nil
case CheckParametersResponseID:
checkMethodByte := make([]byte, 1)
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
@ -414,19 +406,19 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if checkMethodByte[0] == CheckServerParametersID {
checkMethod = "checkServerParameters"
} else {
return "", nil, fmt.Errorf("invalid check method recieved")
return nil, fmt.Errorf("invalid check method recieved")
}
isValid := make([]byte, 1)
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)
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)
@ -436,14 +428,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
messageBytes := make([]byte, messageLength)
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)
}
return "checkParametersResponse", &CheckParametersResponse{
Type: "checkParametersResponse",
return &CheckParametersResponse{
InResponseTo: checkMethod,
IsValid: isValid[0] == 1,
Message: message,
@ -452,19 +443,19 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
isRunning := make([]byte, 1)
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)
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)
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)
@ -474,27 +465,24 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
messageBytes := make([]byte, messageLength)
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)
}
return "backendStatusResponse", &BackendStatusResponse{
Type: "backendStatusResponse",
return &BackendStatusResponse{
IsRunning: isRunning[0] == 1,
StatusCode: int(statusCode[0]),
Message: message,
}, nil
case BackendStatusRequestID:
return "backendStatusRequest", &BackendStatusRequest{
Type: "backendStatusRequest",
}, nil
return &BackendStatusRequest{}, nil
case ProxyStatusRequestID:
ipVersion := make([]byte, 1)
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
@ -504,31 +492,31 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if ipVersion[0] == 6 {
ipSize = IPv6Size
} else {
return "", nil, fmt.Errorf("invalid IP version recieved")
return nil, fmt.Errorf("invalid IP version recieved")
}
ip := make(net.IP, ipSize)
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)
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)
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)
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
@ -538,11 +526,10 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if protocolBytes[1] == UDP {
protocol = "udp"
} else {
return "", nil, fmt.Errorf("invalid protocol")
return nil, fmt.Errorf("invalid protocol")
}
return "proxyStatusRequest", &ProxyStatusRequest{
Type: "proxyStatusRequest",
return &ProxyStatusRequest{
SourceIP: ip.String(),
SourcePort: binary.BigEndian.Uint16(sourcePort),
DestPort: binary.BigEndian.Uint16(destPort),
@ -552,7 +539,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
ipVersion := make([]byte, 1)
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
@ -562,31 +549,31 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if ipVersion[0] == 6 {
ipSize = IPv6Size
} else {
return "", nil, fmt.Errorf("invalid IP version recieved")
return nil, fmt.Errorf("invalid IP version recieved")
}
ip := make(net.IP, ipSize)
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)
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)
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)
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
@ -596,17 +583,16 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if protocolBytes[1] == UDP {
protocol = "udp"
} else {
return "", nil, fmt.Errorf("invalid protocol")
return nil, fmt.Errorf("invalid protocol")
}
isActive := make([]byte, 1)
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{
Type: "proxyStatusResponse",
return &ProxyStatusResponse{
SourceIP: ip.String(),
SourcePort: binary.BigEndian.Uint16(sourcePort),
DestPort: binary.BigEndian.Uint16(destPort),
@ -614,9 +600,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
IsActive: isActive[0] == 1,
}, nil
case ProxyInstanceRequestID:
return "proxyInstanceRequest", &ProxyInstanceRequest{
Type: "proxyInstanceRequest",
}, nil
return &ProxyInstanceRequest{}, nil
case ProxyInstanceResponseID:
proxies := []*ProxyInstance{}
delimiter := make([]byte, 1)
@ -631,13 +615,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
break
}
return "", nil, err
return nil, err
}
proxies = append(proxies, proxy)
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' {
@ -651,15 +635,12 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
}
}
return "proxyInstanceResponse", &ProxyInstanceResponse{
Type: "proxyInstanceResponse",
return &ProxyInstanceResponse{
Proxies: proxies,
}, errorReturn
case ProxyConnectionsRequestID:
return "proxyConnectionsRequest", &ProxyConnectionsRequest{
Type: "proxyConnectionsRequest",
}, nil
return &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()
startCommand := &commonbackend.Start{
Type: "start",
Arguments: backendParameters,
}
startMarshalledCommand, err := commonbackend.Marshal("start", startCommand)
startMarshalledCommand, err := commonbackend.Marshal(startCommand)
if err != nil {
log.Errorf("failed to generate start command: %s", err.Error())
@ -128,18 +127,13 @@ func entrypoint(cCtx *cli.Context) error {
continue
}
commandType, commandRaw, err := commonbackend.Unmarshal(sock)
commandRaw, err := commonbackend.Unmarshal(sock)
if err != nil {
log.Errorf("failed to read from/unmarshal from socket: %s", err.Error())
continue
}
if commandType != "backendStatusResponse" {
log.Errorf("recieved commandType '%s', expecting 'backendStatusResponse'", commandType)
continue
}
command, ok := commandRaw.(*commonbackend.BackendStatusResponse)
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)
proxyAddCommand := &commonbackend.AddProxy{
Type: "addProxy",
SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort,
DestPort: proxy.DestPort,
Protocol: proxy.Protocol,
}
marshalledProxyCommand, err := commonbackend.Marshal("addProxy", proxyAddCommand)
marshalledProxyCommand, err := commonbackend.Marshal(proxyAddCommand)
if err != nil {
log.Errorf("failed to generate start command: %s", err.Error())
@ -189,7 +182,7 @@ func entrypoint(cCtx *cli.Context) error {
continue
}
commandType, commandRaw, err := commonbackend.Unmarshal(sock)
commandRaw, err := commonbackend.Unmarshal(sock)
if err != nil {
log.Errorf("failed to read from/unmarshal from socket: %s", err.Error())
@ -197,12 +190,6 @@ func entrypoint(cCtx *cli.Context) error {
continue
}
if commandType != "proxyStatusResponse" {
log.Errorf("recieved commandType '%s', expecting 'proxyStatusResponse'", commandType)
hasAnyFailed = true
continue
}
command, ok := commandRaw.(*commonbackend.ProxyStatusResponse)
if !ok {

View file

@ -1,57 +1,47 @@
package datacommands
type ProxyStatusRequest struct {
Type string
ProxyID uint16
}
type ProxyStatusResponse struct {
Type string
ProxyID uint16
IsActive bool
}
type RemoveProxy struct {
Type string
ProxyID uint16
}
type ProxyInstanceResponse struct {
Type string
Proxies []uint16
}
type ProxyConnectionsRequest struct {
Type string
ProxyID uint16
}
type ProxyConnectionsResponse struct {
Type string
Connections []uint16
}
type TCPConnectionOpened struct {
Type string
ProxyID uint16
ConnectionID uint16
}
type TCPConnectionClosed struct {
Type string
ProxyID uint16
ConnectionID uint16
}
type TCPProxyData struct {
Type string
ProxyID uint16
ConnectionID uint16
DataLength uint16
}
type UDPProxyData struct {
Type string
ProxyID uint16
ClientIP string
ClientPort uint16
@ -59,12 +49,10 @@ type UDPProxyData struct {
}
type ProxyInformationRequest struct {
Type string
ProxyID uint16
}
type ProxyInformationResponse struct {
Type string
Exists bool
SourceIP string
SourcePort uint16
@ -73,13 +61,11 @@ type ProxyInformationResponse struct {
}
type ProxyConnectionInformationRequest struct {
Type string
ProxyID uint16
ConnectionID uint16
}
type ProxyConnectionInformationResponse struct {
Type string
Exists bool
ClientIP string
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.
func Marshal(_ string, command interface{}) ([]byte, error) {
func Marshal(command interface{}) ([]byte, error) {
switch cmd := command.(type) {
// ProxyStatusRequest: 1 byte for the command ID + 2 bytes for the ProxyID.
case *ProxyStatusRequest:

View file

@ -11,11 +11,10 @@ var logLevel = os.Getenv("HERMES_LOG_LEVEL")
func TestProxyStatusRequest(t *testing.T) {
commandInput := &ProxyStatusRequest{
Type: "proxyStatusRequest",
ProxyID: 19132,
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -26,28 +25,18 @@ func TestProxyStatusRequest(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyStatusRequest)
if !ok {
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 {
t.Fail()
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) {
commandInput := &ProxyStatusResponse{
Type: "proxyStatusResponse",
ProxyID: 19132,
IsActive: true,
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -72,28 +60,18 @@ func TestProxyStatusResponse(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyStatusResponse)
if !ok {
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 {
t.Fail()
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) {
commandInput := &RemoveProxy{
Type: "removeProxy",
ProxyID: 19132,
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -122,28 +99,18 @@ func TestRemoveProxy(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*RemoveProxy)
if !ok {
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 {
t.Fail()
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) {
commandInput := &ProxyConnectionsRequest{
Type: "proxyConnectionsRequest",
ProxyID: 19132,
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -167,28 +133,18 @@ func TestProxyConnectionsRequest(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionsRequest)
if !ok {
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 {
t.Fail()
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) {
commandInput := &ProxyConnectionsResponse{
Type: "proxyConnectionsResponse",
Connections: []uint16{12831, 9455, 64219, 12, 32},
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -212,28 +167,18 @@ func TestProxyConnectionsResponse(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionsResponse)
if !ok {
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 {
remoteConnection := commandUnmarshalled.Connections[connectionIndex]
@ -246,11 +191,10 @@ func TestProxyConnectionsResponse(t *testing.T) {
func TestProxyInstanceResponse(t *testing.T) {
commandInput := &ProxyInstanceResponse{
Type: "proxyInstanceResponse",
Proxies: []uint16{12831, 9455, 64219, 12, 32},
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -261,28 +205,18 @@ func TestProxyInstanceResponse(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInstanceResponse)
if !ok {
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 {
remoteProxy := commandUnmarshalled.Proxies[proxyIndex]
@ -295,12 +229,11 @@ func TestProxyInstanceResponse(t *testing.T) {
func TestTCPConnectionOpened(t *testing.T) {
commandInput := &TCPConnectionOpened{
Type: "tcpConnectionOpened",
ProxyID: 19132,
ConnectionID: 25565,
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -311,28 +244,18 @@ func TestTCPConnectionOpened(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*TCPConnectionOpened)
if !ok {
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 {
t.Fail()
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) {
commandInput := &TCPConnectionClosed{
Type: "tcpConnectionClosed",
ProxyID: 19132,
ConnectionID: 25565,
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -362,28 +284,18 @@ func TestTCPConnectionClosed(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*TCPConnectionClosed)
if !ok {
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 {
t.Fail()
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) {
commandInput := &TCPProxyData{
Type: "tcpProxyData",
ProxyID: 19132,
ConnectionID: 25565,
DataLength: 1234,
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -414,28 +325,18 @@ func TestTCPProxyData(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*TCPProxyData)
if !ok {
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 {
t.Fail()
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) {
commandInput := &UDPProxyData{
Type: "udpProxyData",
ProxyID: 19132,
ClientIP: "68.51.23.54",
ClientPort: 28173,
DataLength: 1234,
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -472,28 +372,18 @@ func TestUDPProxyData(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*UDPProxyData)
if !ok {
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 {
t.Fail()
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) {
commandInput := &ProxyInformationRequest{
Type: "proxyInformationRequest",
ProxyID: 19132,
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -532,28 +421,18 @@ func TestProxyInformationRequest(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInformationRequest)
if !ok {
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 {
t.Fail()
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) {
commandInput := &ProxyInformationResponse{
Type: "proxyInformationResponse",
Exists: true,
SourceIP: "192.168.0.139",
SourcePort: 19132,
@ -570,7 +448,7 @@ func TestProxyInformationResponseExists(t *testing.T) {
Protocol: "tcp",
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if err != nil {
t.Fatal(err.Error())
@ -581,28 +459,18 @@ func TestProxyInformationResponseExists(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInformationResponse)
if !ok {
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 {
t.Fail()
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) {
commandInput := &ProxyInformationResponse{
Type: "proxyInformationResponse",
Exists: false,
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if err != nil {
t.Fatal(err.Error())
@ -646,28 +513,18 @@ func TestProxyInformationResponseNoExist(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyInformationResponse)
if !ok {
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 {
t.Fail()
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) {
commandInput := &ProxyConnectionInformationRequest{
Type: "proxyConnectionInformationRequest",
ProxyID: 19132,
ConnectionID: 25565,
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if logLevel == "debug" {
log.Printf("Generated array contents: %v", commandMarshalled)
@ -692,28 +548,18 @@ func TestProxyConnectionInformationRequest(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionInformationRequest)
if !ok {
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 {
t.Fail()
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) {
commandInput := &ProxyConnectionInformationResponse{
Type: "proxyConnectionInformationResponse",
Exists: true,
ClientIP: "192.168.0.139",
ClientPort: 19132,
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if err != nil {
t.Fatal(err.Error())
@ -744,28 +589,18 @@ func TestProxyConnectionInformationResponseExists(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionInformationResponse)
if !ok {
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 {
t.Fail()
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) {
commandInput := &ProxyConnectionInformationResponse{
Type: "proxyConnectionInformationResponse",
Exists: false,
}
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
commandMarshalled, err := Marshal(commandInput)
if err != nil {
t.Fatal(err.Error())
@ -799,28 +633,18 @@ func TestProxyConnectionInformationResponseNoExists(t *testing.T) {
}
buf := bytes.NewBuffer(commandMarshalled)
commandType, commandUnmarshalledRaw, err := Unmarshal(buf)
commandUnmarshalledRaw, err := Unmarshal(buf)
if err != nil {
t.Fatal(err.Error())
}
if commandType != commandInput.Type {
t.Fail()
log.Print("command type does not match up!")
}
commandUnmarshalled, ok := commandUnmarshalledRaw.(*ProxyConnectionInformationResponse)
if !ok {
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 {
t.Fail()
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
// 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.
header := make([]byte, 1)
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]
@ -23,13 +24,12 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2)
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)
return "proxyStatusRequest", &ProxyStatusRequest{
Type: "proxyStatusRequest",
return &ProxyStatusRequest{
ProxyID: proxyID,
}, nil
@ -38,20 +38,19 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2)
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)
boolBuf := make([]byte, 1)
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
return "proxyStatusResponse", &ProxyStatusResponse{
Type: "proxyStatusResponse",
return &ProxyStatusResponse{
ProxyID: proxyID,
IsActive: isActive,
}, nil
@ -61,13 +60,12 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2)
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)
return "removeProxy", &RemoveProxy{
Type: "removeProxy",
return &RemoveProxy{
ProxyID: proxyID,
}, nil
@ -76,13 +74,12 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2)
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)
return "proxyConnectionsRequest", &ProxyConnectionsRequest{
Type: "proxyConnectionsRequest",
return &ProxyConnectionsRequest{
ProxyID: proxyID,
}, nil
@ -91,7 +88,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2)
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)
@ -108,8 +105,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
connections[connectionIndex] = binary.BigEndian.Uint16(buf)
}
return "proxyConnectionsResponse", &ProxyConnectionsResponse{
Type: "proxyConnectionsResponse",
return &ProxyConnectionsResponse{
Connections: connections,
}, failedDuringReading
@ -118,7 +114,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2)
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)
@ -135,8 +131,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
proxies[connectionIndex] = binary.BigEndian.Uint16(buf)
}
return "proxyInstanceResponse", &ProxyInstanceResponse{
Type: "proxyInstanceResponse",
return &ProxyInstanceResponse{
Proxies: proxies,
}, failedDuringReading
@ -145,14 +140,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2+2)
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])
connectionID := binary.BigEndian.Uint16(buf[2:4])
return "tcpConnectionOpened", &TCPConnectionOpened{
Type: "tcpConnectionOpened",
return &TCPConnectionOpened{
ProxyID: proxyID,
ConnectionID: connectionID,
}, nil
@ -162,14 +156,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2+2)
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])
connectionID := binary.BigEndian.Uint16(buf[2:4])
return "tcpConnectionClosed", &TCPConnectionClosed{
Type: "tcpConnectionClosed",
return &TCPConnectionClosed{
ProxyID: proxyID,
ConnectionID: connectionID,
}, nil
@ -179,15 +172,14 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2+2+2)
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])
connectionID := binary.BigEndian.Uint16(buf[2:4])
dataLength := binary.BigEndian.Uint16(buf[4:6])
return "tcpProxyData", &TCPProxyData{
Type: "tcpProxyData",
return &TCPProxyData{
ProxyID: proxyID,
ConnectionID: connectionID,
DataLength: dataLength,
@ -201,7 +193,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2)
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)
@ -210,7 +202,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
ipVerBuf := make([]byte, 1)
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
@ -220,13 +212,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if ipVerBuf[0] == 6 {
ipSize = IPv6Size
} 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.
ipBytes := make([]byte, ipSize)
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()
@ -234,7 +226,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
portBuf := make([]byte, 2)
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)
@ -243,13 +235,12 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
dataLengthBuf := make([]byte, 2)
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)
return "udpProxyData", &UDPProxyData{
Type: "udpProxyData",
return &UDPProxyData{
ProxyID: proxyID,
ClientIP: clientIP,
ClientPort: clientPort,
@ -261,13 +252,12 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2)
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)
return "proxyInformationRequest", &ProxyInformationRequest{
Type: "proxyInformationRequest",
return &ProxyInformationRequest{
ProxyID: proxyID,
}, nil
@ -279,14 +269,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
boolBuf := make([]byte, 1)
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
if !exists {
return "proxyInformationResponse", &ProxyInformationResponse{
Type: "proxyInformationResponse",
return &ProxyInformationResponse{
Exists: exists,
}, nil
}
@ -295,7 +284,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
ipVerBuf := make([]byte, 1)
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
@ -305,14 +294,14 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
} else if ipVerBuf[0] == 6 {
ipSize = IPv6Size
} 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.
ipBytes := make([]byte, ipSize)
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()
@ -321,7 +310,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
portsBuf := make([]byte, 2+2)
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])
@ -331,19 +320,20 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
protoBuf := make([]byte, 1)
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
if protoBuf[0] == TCP {
protocol = "tcp"
} else if protoBuf[0] == UDP {
protocol = "udp"
} 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{
Type: "proxyInformationResponse",
return &ProxyInformationResponse{
Exists: exists,
SourceIP: sourceIP,
SourcePort: sourcePort,
@ -356,14 +346,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
buf := make([]byte, 2+2)
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])
connectionID := binary.BigEndian.Uint16(buf[2:4])
return "proxyConnectionInformationRequest", &ProxyConnectionInformationRequest{
Type: "proxyConnectionInformationRequest",
return &ProxyConnectionInformationRequest{
ProxyID: proxyID,
ConnectionID: connectionID,
}, nil
@ -374,14 +363,13 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
// Read Exists flag.
boolBuf := make([]byte, 1)
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
if !exists {
return "proxyConnectionInformationResponse", &ProxyConnectionInformationResponse{
Type: "proxyConnectionInformationResponse",
return &ProxyConnectionInformationResponse{
Exists: exists,
}, nil
}
@ -390,11 +378,11 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
ipVerBuf := make([]byte, 1)
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 {
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
@ -409,7 +397,7 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
ipBytes := make([]byte, ipSize)
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()
@ -418,18 +406,17 @@ func Unmarshal(conn io.Reader) (string, interface{}, error) {
portBuf := make([]byte, 2)
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)
return "proxyConnectionInformationResponse", &ProxyConnectionInformationResponse{
Type: "proxyConnectionInformationResponse",
return &ProxyConnectionInformationResponse{
Exists: exists,
ClientIP: clientIP,
ClientPort: clientPort,
}, nil
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")
for {
_, commandRaw, err := datacommands.Unmarshal(helper.socket)
commandRaw, err := datacommands.Unmarshal(helper.socket)
if err != nil && err.Error() != "couldn't match command ID" {
return err
@ -47,11 +47,10 @@ func (helper *BackendApplicationHelper) Start() error {
connections := helper.Backend.GetAllClientConnections(command.ProxyID)
serverParams := &datacommands.ProxyConnectionsResponse{
Type: "proxyConnectionsResponse",
Connections: connections,
}
byteData, err := datacommands.Marshal(serverParams.Type, serverParams)
byteData, err := datacommands.Marshal(serverParams)
if err != nil {
return err
@ -73,12 +72,11 @@ func (helper *BackendApplicationHelper) Start() error {
}
response := &datacommands.ProxyStatusResponse{
Type: "proxyStatusResponse",
ProxyID: command.ProxyID,
IsActive: hasAnyFailed,
}
responseMarshalled, err := commonbackend.Marshal(response.Type, response)
responseMarshalled, err := datacommands.Marshal(response)
if err != nil {
log.Error("failed to marshal response: %s", err.Error())
@ -87,7 +85,7 @@ func (helper *BackendApplicationHelper) Start() error {
helper.socket.Write(responseMarshalled)
default:
_, commandRaw, err := commonbackend.Unmarshal(helper.socket)
commandRaw, err := commonbackend.Unmarshal(helper.socket)
if err != nil {
return err
@ -110,13 +108,12 @@ func (helper *BackendApplicationHelper) Start() error {
}
response := &commonbackend.BackendStatusResponse{
Type: "backendStatusResponse",
IsRunning: ok,
StatusCode: statusCode,
Message: message,
}
responseMarshalled, err := commonbackend.Marshal(response.Type, response)
responseMarshalled, err := commonbackend.Marshal(response)
if err != nil {
log.Error("failed to marshal response: %s", err.Error())
@ -140,13 +137,12 @@ func (helper *BackendApplicationHelper) Start() error {
}
response := &commonbackend.BackendStatusResponse{
Type: "backendStatusResponse",
IsRunning: !ok,
StatusCode: statusCode,
Message: message,
}
responseMarshalled, err := commonbackend.Marshal(response.Type, response)
responseMarshalled, err := commonbackend.Marshal(response)
if err != nil {
log.Error("failed to marshal response: %s", err.Error())
@ -170,13 +166,12 @@ func (helper *BackendApplicationHelper) Start() error {
}
response := &commonbackend.BackendStatusResponse{
Type: "backendStatusResponse",
IsRunning: ok,
StatusCode: statusCode,
Message: message,
}
responseMarshalled, err := commonbackend.Marshal(response.Type, response)
responseMarshalled, err := commonbackend.Marshal(response)
if err != nil {
log.Error("failed to marshal response: %s", err.Error())
@ -197,12 +192,11 @@ func (helper *BackendApplicationHelper) Start() error {
}
response := &datacommands.ProxyStatusResponse{
Type: "proxyStatusResponse",
ProxyID: id,
IsActive: !hasAnyFailed,
}
responseMarshalled, err := commonbackend.Marshal(response.Type, response)
responseMarshalled, err := datacommands.Marshal(response)
if err != nil {
log.Error("failed to marshal response: %s", err.Error())
@ -212,10 +206,9 @@ func (helper *BackendApplicationHelper) Start() error {
helper.socket.Write(responseMarshalled)
case *commonbackend.CheckClientParameters:
resp := helper.Backend.CheckParametersForConnections(command)
resp.Type = "checkParametersResponse"
resp.InResponseTo = "checkClientParameters"
byteData, err := commonbackend.Marshal(resp.Type, resp)
byteData, err := commonbackend.Marshal(resp)
if err != nil {
return err
@ -226,10 +219,9 @@ func (helper *BackendApplicationHelper) Start() error {
}
case *commonbackend.CheckServerParameters:
resp := helper.Backend.CheckParametersForBackend(command.Arguments)
resp.Type = "checkParametersResponse"
resp.InResponseTo = "checkServerParameters"
byteData, err := commonbackend.Marshal(resp.Type, resp)
byteData, err := commonbackend.Marshal(resp)
if err != nil {
return err