chore: Strip unneeded components from code.
This commit is contained in:
parent
62cc8b39ad
commit
cf90ddb104
19 changed files with 228 additions and 762 deletions
|
@ -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)
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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]
|
||||
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue