chore: Restructure files.
This commit is contained in:
parent
559588f726
commit
d25da9091e
93 changed files with 38 additions and 26 deletions
18
backend/backendlauncher/backendlauncher.go
Normal file
18
backend/backendlauncher/backendlauncher.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package backendlauncher
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
"net"
|
||||
)
|
||||
|
||||
func GetUnixSocket(folder string) (string, net.Listener, error) {
|
||||
socketPath := fmt.Sprintf("%s/sock-%d.sock", folder, rand.Uint())
|
||||
listener, err := net.Listen("unix", socketPath)
|
||||
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
return socketPath, listener, nil
|
||||
}
|
241
backend/backendutil/application.go
Normal file
241
backend/backendutil/application.go
Normal file
|
@ -0,0 +1,241 @@
|
|||
package backendutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"git.terah.dev/imterah/hermes/commonbackend"
|
||||
"github.com/charmbracelet/log"
|
||||
)
|
||||
|
||||
type BackendApplicationHelper struct {
|
||||
Backend BackendInterface
|
||||
SocketPath string
|
||||
|
||||
socket net.Conn
|
||||
}
|
||||
|
||||
func (helper *BackendApplicationHelper) Start() error {
|
||||
log.Debug("BackendApplicationHelper is starting")
|
||||
log.Debug("Currently waiting for Unix socket connection...")
|
||||
|
||||
var err error
|
||||
helper.socket, err = net.Dial("unix", helper.SocketPath)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debug("Sucessfully connected")
|
||||
|
||||
for {
|
||||
commandType, commandRaw, err := commonbackend.Unmarshal(helper.socket)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch commandType {
|
||||
case "start":
|
||||
// TODO: implement response logic
|
||||
command, ok := commandRaw.(*commonbackend.Start)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
ok, err = helper.Backend.StartBackend(command.Arguments)
|
||||
|
||||
var (
|
||||
message string
|
||||
statusCode int
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
message = err.Error()
|
||||
statusCode = commonbackend.StatusFailure
|
||||
} else {
|
||||
statusCode = commonbackend.StatusSuccess
|
||||
}
|
||||
|
||||
response := &commonbackend.BackendStatusResponse{
|
||||
Type: "backendStatusResponse",
|
||||
IsRunning: ok,
|
||||
StatusCode: statusCode,
|
||||
Message: message,
|
||||
}
|
||||
|
||||
responseMarshalled, err := commonbackend.Marshal(response.Type, response)
|
||||
|
||||
if err != nil {
|
||||
log.Error("failed to marshal response: %s", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
helper.socket.Write(responseMarshalled)
|
||||
case "stop":
|
||||
// TODO: implement response logic
|
||||
_, ok := commandRaw.(*commonbackend.Stop)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
ok, err = helper.Backend.StopBackend()
|
||||
|
||||
var (
|
||||
message string
|
||||
statusCode int
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
message = err.Error()
|
||||
statusCode = commonbackend.StatusFailure
|
||||
} else {
|
||||
statusCode = commonbackend.StatusSuccess
|
||||
}
|
||||
|
||||
response := &commonbackend.BackendStatusResponse{
|
||||
Type: "backendStatusResponse",
|
||||
IsRunning: !ok,
|
||||
StatusCode: statusCode,
|
||||
Message: message,
|
||||
}
|
||||
|
||||
responseMarshalled, err := commonbackend.Marshal(response.Type, response)
|
||||
|
||||
if err != nil {
|
||||
log.Error("failed to marshal response: %s", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
helper.socket.Write(responseMarshalled)
|
||||
case "addProxy":
|
||||
// TODO: implement response logic
|
||||
command, ok := commandRaw.(*commonbackend.AddProxy)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
ok, err = helper.Backend.StartProxy(command)
|
||||
var hasAnyFailed bool
|
||||
|
||||
if !ok {
|
||||
log.Warnf("failed to add proxy (%s:%d -> remote:%d): StartProxy returned into failure state", command.SourceIP, command.SourcePort, command.DestPort)
|
||||
hasAnyFailed = true
|
||||
} else if err != nil {
|
||||
log.Warnf("failed to add proxy (%s:%d -> remote:%d): %s", command.SourceIP, command.SourcePort, command.DestPort, err.Error())
|
||||
hasAnyFailed = true
|
||||
}
|
||||
|
||||
response := &commonbackend.ProxyStatusResponse{
|
||||
Type: "proxyStatusResponse",
|
||||
SourceIP: command.SourceIP,
|
||||
SourcePort: command.SourcePort,
|
||||
DestPort: command.DestPort,
|
||||
Protocol: command.Protocol,
|
||||
IsActive: !hasAnyFailed,
|
||||
}
|
||||
|
||||
responseMarshalled, err := commonbackend.Marshal(response.Type, response)
|
||||
|
||||
if err != nil {
|
||||
log.Error("failed to marshal response: %s", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
helper.socket.Write(responseMarshalled)
|
||||
case "removeProxy":
|
||||
// TODO: implement response logic
|
||||
command, ok := commandRaw.(*commonbackend.RemoveProxy)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
_, _ = helper.Backend.StopProxy(command)
|
||||
case "proxyConnectionsRequest":
|
||||
_, ok := commandRaw.(*commonbackend.ProxyConnectionsRequest)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
connections := helper.Backend.GetAllClientConnections()
|
||||
|
||||
serverParams := &commonbackend.ProxyConnectionsResponse{
|
||||
Type: "proxyConnectionsResponse",
|
||||
Connections: connections,
|
||||
}
|
||||
|
||||
byteData, err := commonbackend.Marshal(serverParams.Type, serverParams)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = helper.socket.Write(byteData); err != nil {
|
||||
return err
|
||||
}
|
||||
case "checkClientParameters":
|
||||
// TODO: implement response logic
|
||||
command, ok := commandRaw.(*commonbackend.CheckClientParameters)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
resp := helper.Backend.CheckParametersForConnections(command)
|
||||
resp.Type = "checkParametersResponse"
|
||||
resp.InResponseTo = "checkClientParameters"
|
||||
|
||||
byteData, err := commonbackend.Marshal(resp.Type, resp)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = helper.socket.Write(byteData); err != nil {
|
||||
return err
|
||||
}
|
||||
case "checkServerParameters":
|
||||
// TODO: implement response logic
|
||||
command, ok := commandRaw.(*commonbackend.CheckServerParameters)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
resp := helper.Backend.CheckParametersForBackend(command.Arguments)
|
||||
resp.Type = "checkParametersResponse"
|
||||
resp.InResponseTo = "checkServerParameters"
|
||||
|
||||
byteData, err := commonbackend.Marshal(resp.Type, resp)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = helper.socket.Write(byteData); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewHelper(backend BackendInterface) *BackendApplicationHelper {
|
||||
socketPath, ok := os.LookupEnv("HERMES_API_SOCK")
|
||||
|
||||
if !ok {
|
||||
log.Warn("HERMES_API_SOCK is not defined! This will cause an issue unless the backend manually overwrites it")
|
||||
}
|
||||
|
||||
helper := &BackendApplicationHelper{
|
||||
Backend: backend,
|
||||
SocketPath: socketPath,
|
||||
}
|
||||
|
||||
return helper
|
||||
}
|
13
backend/backendutil/structure.go
Normal file
13
backend/backendutil/structure.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package backendutil
|
||||
|
||||
import "git.terah.dev/imterah/hermes/commonbackend"
|
||||
|
||||
type BackendInterface interface {
|
||||
StartBackend(arguments []byte) (bool, error)
|
||||
StopBackend() (bool, error)
|
||||
StartProxy(command *commonbackend.AddProxy) (bool, error)
|
||||
StopProxy(command *commonbackend.RemoveProxy) (bool, error)
|
||||
GetAllClientConnections() []*commonbackend.ProxyClientConnection
|
||||
CheckParametersForConnections(clientParameters *commonbackend.CheckClientParameters) *commonbackend.CheckParametersResponse
|
||||
CheckParametersForBackend(arguments []byte) *commonbackend.CheckParametersResponse
|
||||
}
|
147
backend/commonbackend/constants.go
Normal file
147
backend/commonbackend/constants.go
Normal file
|
@ -0,0 +1,147 @@
|
|||
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
|
||||
Protocol string // Will be either 'tcp' or 'udp'
|
||||
}
|
||||
|
||||
type RemoveProxy struct {
|
||||
Type string // Will be 'removeProxy' always
|
||||
SourceIP string
|
||||
SourcePort uint16
|
||||
DestPort uint16
|
||||
Protocol string // Will be either 'tcp' or 'udp'
|
||||
}
|
||||
|
||||
type ProxyStatusRequest struct {
|
||||
Type string // Will be 'proxyStatusRequest' always
|
||||
SourceIP string
|
||||
SourcePort uint16
|
||||
DestPort uint16
|
||||
Protocol string // Will be either 'tcp' or 'udp'
|
||||
}
|
||||
|
||||
type ProxyStatusResponse struct {
|
||||
Type string // Will be 'proxyStatusResponse' always
|
||||
SourceIP string
|
||||
SourcePort uint16
|
||||
DestPort uint16
|
||||
Protocol string // Will be either 'tcp' or 'udp'
|
||||
IsActive bool
|
||||
}
|
||||
|
||||
type ProxyInstance struct {
|
||||
SourceIP string
|
||||
SourcePort uint16
|
||||
DestPort uint16
|
||||
Protocol string // Will be either 'tcp' or 'udp'
|
||||
}
|
||||
|
||||
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
|
||||
type ProxyClientConnection struct {
|
||||
SourceIP string
|
||||
SourcePort uint16
|
||||
DestPort uint16
|
||||
ClientIP string
|
||||
ClientPort uint16
|
||||
}
|
||||
|
||||
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
|
||||
Protocol string // Will be either 'tcp' or 'udp'
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
const (
|
||||
StartID = iota
|
||||
StopID
|
||||
AddProxyID
|
||||
RemoveProxyID
|
||||
ProxyConnectionsResponseID
|
||||
CheckClientParametersID
|
||||
CheckServerParametersID
|
||||
CheckParametersResponseID
|
||||
ProxyConnectionsRequestID
|
||||
BackendStatusResponseID
|
||||
BackendStatusRequestID
|
||||
ProxyStatusRequestID
|
||||
ProxyStatusResponseID
|
||||
ProxyInstanceResponseID
|
||||
ProxyInstanceRequestID
|
||||
)
|
||||
|
||||
const (
|
||||
TCP = iota
|
||||
UDP
|
||||
)
|
||||
|
||||
const (
|
||||
StatusSuccess = iota
|
||||
StatusFailure
|
||||
)
|
||||
|
||||
const (
|
||||
// IP versions
|
||||
IPv4 = 4
|
||||
IPv6 = 6
|
||||
|
||||
// TODO: net has these constants defined already. We should switch to these
|
||||
IPv4Size = 4
|
||||
IPv6Size = 16
|
||||
)
|
498
backend/commonbackend/marshal.go
Normal file
498
backend/commonbackend/marshal.go
Normal file
|
@ -0,0 +1,498 @@
|
|||
package commonbackend
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
func marshalIndividualConnectionStruct(conn *ProxyClientConnection) []byte {
|
||||
sourceIPOriginal := net.ParseIP(conn.SourceIP)
|
||||
clientIPOriginal := net.ParseIP(conn.ClientIP)
|
||||
|
||||
var serverIPVer uint8
|
||||
var sourceIP []byte
|
||||
|
||||
if sourceIPOriginal.To4() == nil {
|
||||
serverIPVer = IPv6
|
||||
sourceIP = sourceIPOriginal.To16()
|
||||
} else {
|
||||
serverIPVer = IPv4
|
||||
sourceIP = sourceIPOriginal.To4()
|
||||
}
|
||||
|
||||
var clientIPVer uint8
|
||||
var clientIP []byte
|
||||
|
||||
if clientIPOriginal.To4() == nil {
|
||||
clientIPVer = IPv6
|
||||
clientIP = clientIPOriginal.To16()
|
||||
} else {
|
||||
clientIPVer = IPv4
|
||||
clientIP = clientIPOriginal.To4()
|
||||
}
|
||||
|
||||
connectionBlock := make([]byte, 8+len(sourceIP)+len(clientIP))
|
||||
|
||||
connectionBlock[0] = serverIPVer
|
||||
copy(connectionBlock[1:len(sourceIP)+1], sourceIP)
|
||||
|
||||
binary.BigEndian.PutUint16(connectionBlock[1+len(sourceIP):3+len(sourceIP)], conn.SourcePort)
|
||||
binary.BigEndian.PutUint16(connectionBlock[3+len(sourceIP):5+len(sourceIP)], conn.DestPort)
|
||||
|
||||
connectionBlock[5+len(sourceIP)] = clientIPVer
|
||||
copy(connectionBlock[6+len(sourceIP):6+len(sourceIP)+len(clientIP)], clientIP)
|
||||
binary.BigEndian.PutUint16(connectionBlock[6+len(sourceIP)+len(clientIP):8+len(sourceIP)+len(clientIP)], conn.ClientPort)
|
||||
|
||||
return connectionBlock
|
||||
}
|
||||
|
||||
func marshalIndividualProxyStruct(conn *ProxyInstance) ([]byte, error) {
|
||||
sourceIPOriginal := net.ParseIP(conn.SourceIP)
|
||||
|
||||
var sourceIPVer uint8
|
||||
var sourceIP []byte
|
||||
|
||||
if sourceIPOriginal.To4() == nil {
|
||||
sourceIPVer = IPv6
|
||||
sourceIP = sourceIPOriginal.To16()
|
||||
} else {
|
||||
sourceIPVer = IPv4
|
||||
sourceIP = sourceIPOriginal.To4()
|
||||
}
|
||||
|
||||
proxyBlock := make([]byte, 6+len(sourceIP))
|
||||
|
||||
proxyBlock[0] = sourceIPVer
|
||||
copy(proxyBlock[1:len(sourceIP)+1], sourceIP)
|
||||
|
||||
binary.BigEndian.PutUint16(proxyBlock[1+len(sourceIP):3+len(sourceIP)], conn.SourcePort)
|
||||
binary.BigEndian.PutUint16(proxyBlock[3+len(sourceIP):5+len(sourceIP)], conn.DestPort)
|
||||
|
||||
var protocolVersion uint8
|
||||
|
||||
if conn.Protocol == "tcp" {
|
||||
protocolVersion = TCP
|
||||
} else if conn.Protocol == "udp" {
|
||||
protocolVersion = UDP
|
||||
} else {
|
||||
return proxyBlock, fmt.Errorf("invalid protocol recieved")
|
||||
}
|
||||
|
||||
proxyBlock[5+len(sourceIP)] = protocolVersion
|
||||
|
||||
return proxyBlock, nil
|
||||
}
|
||||
|
||||
func Marshal(commandType string, command interface{}) ([]byte, error) {
|
||||
switch commandType {
|
||||
case "start":
|
||||
startCommand, ok := command.(*Start)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
startCommandBytes := make([]byte, 1+2+len(startCommand.Arguments))
|
||||
startCommandBytes[0] = StartID
|
||||
binary.BigEndian.PutUint16(startCommandBytes[1:3], uint16(len(startCommand.Arguments)))
|
||||
copy(startCommandBytes[3:], startCommand.Arguments)
|
||||
|
||||
return startCommandBytes, nil
|
||||
case "stop":
|
||||
_, ok := command.(*Stop)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
return []byte{StopID}, nil
|
||||
case "addProxy":
|
||||
addConnectionCommand, ok := command.(*AddProxy)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
sourceIP := net.ParseIP(addConnectionCommand.SourceIP)
|
||||
|
||||
var ipVer uint8
|
||||
var ipBytes []byte
|
||||
|
||||
if sourceIP.To4() == nil {
|
||||
ipBytes = sourceIP.To16()
|
||||
ipVer = IPv6
|
||||
} else {
|
||||
ipBytes = sourceIP.To4()
|
||||
ipVer = IPv4
|
||||
}
|
||||
|
||||
addConnectionBytes := make([]byte, 1+1+len(ipBytes)+2+2+1)
|
||||
|
||||
addConnectionBytes[0] = AddProxyID
|
||||
addConnectionBytes[1] = ipVer
|
||||
|
||||
copy(addConnectionBytes[2:2+len(ipBytes)], ipBytes)
|
||||
|
||||
binary.BigEndian.PutUint16(addConnectionBytes[2+len(ipBytes):4+len(ipBytes)], addConnectionCommand.SourcePort)
|
||||
binary.BigEndian.PutUint16(addConnectionBytes[4+len(ipBytes):6+len(ipBytes)], addConnectionCommand.DestPort)
|
||||
|
||||
var protocol uint8
|
||||
|
||||
if addConnectionCommand.Protocol == "tcp" {
|
||||
protocol = TCP
|
||||
} else if addConnectionCommand.Protocol == "udp" {
|
||||
protocol = UDP
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid protocol")
|
||||
}
|
||||
|
||||
addConnectionBytes[6+len(ipBytes)] = protocol
|
||||
|
||||
return addConnectionBytes, nil
|
||||
case "removeProxy":
|
||||
removeConnectionCommand, ok := command.(*RemoveProxy)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
sourceIP := net.ParseIP(removeConnectionCommand.SourceIP)
|
||||
|
||||
var ipVer uint8
|
||||
var ipBytes []byte
|
||||
|
||||
if sourceIP.To4() == nil {
|
||||
ipBytes = sourceIP.To16()
|
||||
ipVer = IPv6
|
||||
} else {
|
||||
ipBytes = sourceIP.To4()
|
||||
ipVer = IPv4
|
||||
}
|
||||
|
||||
removeConnectionBytes := make([]byte, 1+1+len(ipBytes)+2+2+1)
|
||||
|
||||
removeConnectionBytes[0] = RemoveProxyID
|
||||
removeConnectionBytes[1] = ipVer
|
||||
copy(removeConnectionBytes[2:2+len(ipBytes)], ipBytes)
|
||||
binary.BigEndian.PutUint16(removeConnectionBytes[2+len(ipBytes):4+len(ipBytes)], removeConnectionCommand.SourcePort)
|
||||
binary.BigEndian.PutUint16(removeConnectionBytes[4+len(ipBytes):6+len(ipBytes)], removeConnectionCommand.DestPort)
|
||||
|
||||
var protocol uint8
|
||||
|
||||
if removeConnectionCommand.Protocol == "tcp" {
|
||||
protocol = TCP
|
||||
} else if removeConnectionCommand.Protocol == "udp" {
|
||||
protocol = UDP
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid protocol")
|
||||
}
|
||||
|
||||
removeConnectionBytes[6+len(ipBytes)] = protocol
|
||||
|
||||
return removeConnectionBytes, nil
|
||||
case "proxyConnectionsResponse":
|
||||
allConnectionsCommand, ok := command.(*ProxyConnectionsResponse)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
connectionsArray := make([][]byte, len(allConnectionsCommand.Connections))
|
||||
totalSize := 0
|
||||
|
||||
for connIndex, conn := range allConnectionsCommand.Connections {
|
||||
connectionsArray[connIndex] = marshalIndividualConnectionStruct(conn)
|
||||
totalSize += len(connectionsArray[connIndex]) + 1
|
||||
}
|
||||
|
||||
connectionCommandArray := make([]byte, totalSize+1)
|
||||
connectionCommandArray[0] = ProxyConnectionsResponseID
|
||||
|
||||
currentPosition := 1
|
||||
|
||||
for _, connection := range connectionsArray {
|
||||
copy(connectionCommandArray[currentPosition:currentPosition+len(connection)], connection)
|
||||
connectionCommandArray[currentPosition+len(connection)] = '\r'
|
||||
currentPosition += len(connection) + 1
|
||||
}
|
||||
|
||||
connectionCommandArray[totalSize] = '\n'
|
||||
return connectionCommandArray, nil
|
||||
case "checkClientParameters":
|
||||
checkClientCommand, ok := command.(*CheckClientParameters)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
sourceIP := net.ParseIP(checkClientCommand.SourceIP)
|
||||
|
||||
var ipVer uint8
|
||||
var ipBytes []byte
|
||||
|
||||
if sourceIP.To4() == nil {
|
||||
ipBytes = sourceIP.To16()
|
||||
ipVer = IPv6
|
||||
} else {
|
||||
ipBytes = sourceIP.To4()
|
||||
ipVer = IPv4
|
||||
}
|
||||
|
||||
checkClientBytes := make([]byte, 1+1+len(ipBytes)+2+2+1)
|
||||
|
||||
checkClientBytes[0] = CheckClientParametersID
|
||||
checkClientBytes[1] = ipVer
|
||||
copy(checkClientBytes[2:2+len(ipBytes)], ipBytes)
|
||||
binary.BigEndian.PutUint16(checkClientBytes[2+len(ipBytes):4+len(ipBytes)], checkClientCommand.SourcePort)
|
||||
binary.BigEndian.PutUint16(checkClientBytes[4+len(ipBytes):6+len(ipBytes)], checkClientCommand.DestPort)
|
||||
|
||||
var protocol uint8
|
||||
|
||||
if checkClientCommand.Protocol == "tcp" {
|
||||
protocol = TCP
|
||||
} else if checkClientCommand.Protocol == "udp" {
|
||||
protocol = UDP
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid protocol")
|
||||
}
|
||||
|
||||
checkClientBytes[6+len(ipBytes)] = protocol
|
||||
|
||||
return checkClientBytes, nil
|
||||
case "checkServerParameters":
|
||||
checkServerCommand, ok := command.(*CheckServerParameters)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
serverCommandBytes := make([]byte, 1+2+len(checkServerCommand.Arguments))
|
||||
serverCommandBytes[0] = CheckServerParametersID
|
||||
binary.BigEndian.PutUint16(serverCommandBytes[1:3], uint16(len(checkServerCommand.Arguments)))
|
||||
copy(serverCommandBytes[3:], checkServerCommand.Arguments)
|
||||
|
||||
return serverCommandBytes, nil
|
||||
case "checkParametersResponse":
|
||||
checkParametersCommand, ok := command.(*CheckParametersResponse)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
var checkMethod uint8
|
||||
|
||||
if checkParametersCommand.InResponseTo == "checkClientParameters" {
|
||||
checkMethod = CheckClientParametersID
|
||||
} else if checkParametersCommand.InResponseTo == "checkServerParameters" {
|
||||
checkMethod = CheckServerParametersID
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid mode recieved (must be either checkClientParameters or checkServerParameters)")
|
||||
}
|
||||
|
||||
var isValid uint8
|
||||
|
||||
if checkParametersCommand.IsValid {
|
||||
isValid = 1
|
||||
}
|
||||
|
||||
checkResponseBytes := make([]byte, 3+2+len(checkParametersCommand.Message))
|
||||
checkResponseBytes[0] = CheckParametersResponseID
|
||||
checkResponseBytes[1] = checkMethod
|
||||
checkResponseBytes[2] = isValid
|
||||
|
||||
binary.BigEndian.PutUint16(checkResponseBytes[3:5], uint16(len(checkParametersCommand.Message)))
|
||||
|
||||
if len(checkParametersCommand.Message) != 0 {
|
||||
copy(checkResponseBytes[5:], []byte(checkParametersCommand.Message))
|
||||
}
|
||||
|
||||
return checkResponseBytes, nil
|
||||
case "backendStatusResponse":
|
||||
backendStatusResponse, ok := command.(*BackendStatusResponse)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
var isRunning uint8
|
||||
|
||||
if backendStatusResponse.IsRunning {
|
||||
isRunning = 1
|
||||
} else {
|
||||
isRunning = 0
|
||||
}
|
||||
|
||||
statusResponseBytes := make([]byte, 3+2+len(backendStatusResponse.Message))
|
||||
statusResponseBytes[0] = BackendStatusResponseID
|
||||
statusResponseBytes[1] = isRunning
|
||||
statusResponseBytes[2] = byte(backendStatusResponse.StatusCode)
|
||||
|
||||
binary.BigEndian.PutUint16(statusResponseBytes[3:5], uint16(len(backendStatusResponse.Message)))
|
||||
|
||||
if len(backendStatusResponse.Message) != 0 {
|
||||
copy(statusResponseBytes[5:], []byte(backendStatusResponse.Message))
|
||||
}
|
||||
|
||||
return statusResponseBytes, nil
|
||||
case "backendStatusRequest":
|
||||
_, ok := command.(*BackendStatusRequest)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
statusRequestBytes := make([]byte, 2)
|
||||
statusRequestBytes[0] = BackendStatusRequestID
|
||||
|
||||
return statusRequestBytes, nil
|
||||
case "proxyStatusRequest":
|
||||
proxyStatusRequest, ok := command.(*ProxyStatusRequest)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
sourceIP := net.ParseIP(proxyStatusRequest.SourceIP)
|
||||
|
||||
var ipVer uint8
|
||||
var ipBytes []byte
|
||||
|
||||
if sourceIP.To4() == nil {
|
||||
ipBytes = sourceIP.To16()
|
||||
ipVer = IPv6
|
||||
} else {
|
||||
ipBytes = sourceIP.To4()
|
||||
ipVer = IPv4
|
||||
}
|
||||
|
||||
proxyStatusRequestBytes := make([]byte, 1+1+len(ipBytes)+2+2+1)
|
||||
|
||||
proxyStatusRequestBytes[0] = ProxyStatusRequestID
|
||||
proxyStatusRequestBytes[1] = ipVer
|
||||
|
||||
copy(proxyStatusRequestBytes[2:2+len(ipBytes)], ipBytes)
|
||||
|
||||
binary.BigEndian.PutUint16(proxyStatusRequestBytes[2+len(ipBytes):4+len(ipBytes)], proxyStatusRequest.SourcePort)
|
||||
binary.BigEndian.PutUint16(proxyStatusRequestBytes[4+len(ipBytes):6+len(ipBytes)], proxyStatusRequest.DestPort)
|
||||
|
||||
var protocol uint8
|
||||
|
||||
if proxyStatusRequest.Protocol == "tcp" {
|
||||
protocol = TCP
|
||||
} else if proxyStatusRequest.Protocol == "udp" {
|
||||
protocol = UDP
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid protocol")
|
||||
}
|
||||
|
||||
proxyStatusRequestBytes[6+len(ipBytes)] = protocol
|
||||
|
||||
return proxyStatusRequestBytes, nil
|
||||
case "proxyStatusResponse":
|
||||
proxyStatusResponse, ok := command.(*ProxyStatusResponse)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
sourceIP := net.ParseIP(proxyStatusResponse.SourceIP)
|
||||
|
||||
var ipVer uint8
|
||||
var ipBytes []byte
|
||||
|
||||
if sourceIP.To4() == nil {
|
||||
ipBytes = sourceIP.To16()
|
||||
ipVer = IPv6
|
||||
} else {
|
||||
ipBytes = sourceIP.To4()
|
||||
ipVer = IPv4
|
||||
}
|
||||
|
||||
proxyStatusResponseBytes := make([]byte, 1+1+len(ipBytes)+2+2+1+1)
|
||||
|
||||
proxyStatusResponseBytes[0] = ProxyStatusResponseID
|
||||
proxyStatusResponseBytes[1] = ipVer
|
||||
|
||||
copy(proxyStatusResponseBytes[2:2+len(ipBytes)], ipBytes)
|
||||
|
||||
binary.BigEndian.PutUint16(proxyStatusResponseBytes[2+len(ipBytes):4+len(ipBytes)], proxyStatusResponse.SourcePort)
|
||||
binary.BigEndian.PutUint16(proxyStatusResponseBytes[4+len(ipBytes):6+len(ipBytes)], proxyStatusResponse.DestPort)
|
||||
|
||||
var protocol uint8
|
||||
|
||||
if proxyStatusResponse.Protocol == "tcp" {
|
||||
protocol = TCP
|
||||
} else if proxyStatusResponse.Protocol == "udp" {
|
||||
protocol = UDP
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid protocol")
|
||||
}
|
||||
|
||||
proxyStatusResponseBytes[6+len(ipBytes)] = protocol
|
||||
|
||||
var isActive uint8
|
||||
|
||||
if proxyStatusResponse.IsActive {
|
||||
isActive = 1
|
||||
} else {
|
||||
isActive = 0
|
||||
}
|
||||
|
||||
proxyStatusResponseBytes[7+len(ipBytes)] = isActive
|
||||
|
||||
return proxyStatusResponseBytes, nil
|
||||
case "proxyInstanceResponse":
|
||||
proxyConectionResponse, ok := command.(*ProxyInstanceResponse)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
proxyArray := make([][]byte, len(proxyConectionResponse.Proxies))
|
||||
totalSize := 0
|
||||
|
||||
for proxyIndex, proxy := range proxyConectionResponse.Proxies {
|
||||
var err error
|
||||
proxyArray[proxyIndex], err = marshalIndividualProxyStruct(proxy)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
totalSize += len(proxyArray[proxyIndex]) + 1
|
||||
}
|
||||
|
||||
connectionCommandArray := make([]byte, totalSize+1)
|
||||
connectionCommandArray[0] = ProxyInstanceResponseID
|
||||
|
||||
currentPosition := 1
|
||||
|
||||
for _, connection := range proxyArray {
|
||||
copy(connectionCommandArray[currentPosition:currentPosition+len(connection)], connection)
|
||||
connectionCommandArray[currentPosition+len(connection)] = '\r'
|
||||
currentPosition += len(connection) + 1
|
||||
}
|
||||
|
||||
connectionCommandArray[totalSize] = '\n'
|
||||
return connectionCommandArray, nil
|
||||
case "proxyInstanceRequest":
|
||||
_, ok := command.(*ProxyInstanceRequest)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
return []byte{ProxyInstanceRequestID}, nil
|
||||
case "proxyConnectionsRequest":
|
||||
_, ok := command.(*ProxyConnectionsRequest)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to typecast")
|
||||
}
|
||||
|
||||
return []byte{ProxyConnectionsRequestID}, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("couldn't match command name")
|
||||
}
|
824
backend/commonbackend/marshalling_test.go
Normal file
824
backend/commonbackend/marshalling_test.go
Normal file
|
@ -0,0 +1,824 @@
|
|||
package commonbackend
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var logLevel = os.Getenv("HERMES_LOG_LEVEL")
|
||||
|
||||
func TestStartCommandMarshalSupport(t *testing.T) {
|
||||
commandInput := &Start{
|
||||
Type: "start",
|
||||
Arguments: []byte("Hello from automated testing"),
|
||||
}
|
||||
|
||||
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Generated array contents: %v", commandMarshalled)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(commandMarshalled)
|
||||
commandType, 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 TestStopCommandMarshalSupport(t *testing.T) {
|
||||
commandInput := &Stop{
|
||||
Type: "stop",
|
||||
}
|
||||
|
||||
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Generated array contents: %v", commandMarshalled)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(commandMarshalled)
|
||||
commandType, 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)
|
||||
|
||||
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 TestAddConnectionCommandMarshalSupport(t *testing.T) {
|
||||
commandInput := &AddProxy{
|
||||
Type: "addProxy",
|
||||
SourceIP: "192.168.0.139",
|
||||
SourcePort: 19132,
|
||||
DestPort: 19132,
|
||||
Protocol: "tcp",
|
||||
}
|
||||
|
||||
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Generated array contents: %v", commandMarshalled)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(commandMarshalled)
|
||||
commandType, 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)
|
||||
}
|
||||
|
||||
if commandInput.SourcePort != commandUnmarshalled.SourcePort {
|
||||
t.Fail()
|
||||
log.Printf("SourcePort's are not equal (orig: %d, unmsh: %d)", commandInput.SourcePort, commandUnmarshalled.SourcePort)
|
||||
}
|
||||
|
||||
if commandInput.DestPort != commandUnmarshalled.DestPort {
|
||||
t.Fail()
|
||||
log.Printf("DestPort's are not equal (orig: %d, unmsh: %d)", commandInput.DestPort, commandUnmarshalled.DestPort)
|
||||
}
|
||||
|
||||
if commandInput.Protocol != commandUnmarshalled.Protocol {
|
||||
t.Fail()
|
||||
log.Printf("Protocols are not equal (orig: %s, unmsh: %s)", commandInput.Protocol, commandUnmarshalled.Protocol)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveConnectionCommandMarshalSupport(t *testing.T) {
|
||||
commandInput := &RemoveProxy{
|
||||
Type: "removeProxy",
|
||||
SourceIP: "192.168.0.139",
|
||||
SourcePort: 19132,
|
||||
DestPort: 19132,
|
||||
Protocol: "tcp",
|
||||
}
|
||||
|
||||
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Generated array contents: %v", commandMarshalled)
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(commandMarshalled)
|
||||
commandType, 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)
|
||||
}
|
||||
|
||||
if commandInput.SourcePort != commandUnmarshalled.SourcePort {
|
||||
t.Fail()
|
||||
log.Printf("SourcePort's are not equal (orig: %d, unmsh: %d)", commandInput.SourcePort, commandUnmarshalled.SourcePort)
|
||||
}
|
||||
|
||||
if commandInput.DestPort != commandUnmarshalled.DestPort {
|
||||
t.Fail()
|
||||
log.Printf("DestPort's are not equal (orig: %d, unmsh: %d)", commandInput.DestPort, commandUnmarshalled.DestPort)
|
||||
}
|
||||
|
||||
if commandInput.Protocol != commandUnmarshalled.Protocol {
|
||||
t.Fail()
|
||||
log.Printf("Protocols are not equal (orig: %s, unmsh: %s)", commandInput.Protocol, commandUnmarshalled.Protocol)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAllConnectionsCommandMarshalSupport(t *testing.T) {
|
||||
commandInput := &ProxyConnectionsResponse{
|
||||
Type: "proxyConnectionsResponse",
|
||||
Connections: []*ProxyClientConnection{
|
||||
{
|
||||
SourceIP: "127.0.0.1",
|
||||
SourcePort: 19132,
|
||||
DestPort: 19132,
|
||||
ClientIP: "127.0.0.1",
|
||||
ClientPort: 12321,
|
||||
},
|
||||
{
|
||||
SourceIP: "127.0.0.1",
|
||||
SourcePort: 19132,
|
||||
DestPort: 19132,
|
||||
ClientIP: "192.168.0.168",
|
||||
ClientPort: 23457,
|
||||
},
|
||||
{
|
||||
SourceIP: "127.0.0.1",
|
||||
SourcePort: 19132,
|
||||
DestPort: 19132,
|
||||
ClientIP: "68.42.203.47",
|
||||
ClientPort: 38721,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Generated array contents: %v", commandMarshalled)
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(commandMarshalled)
|
||||
commandType, 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]
|
||||
|
||||
if originalConnection.SourceIP != remoteConnection.SourceIP {
|
||||
t.Fail()
|
||||
log.Printf("(in #%d) SourceIP's are not equal (orig: %s, unmsh: %s)", commandIndex, originalConnection.SourceIP, remoteConnection.SourceIP)
|
||||
}
|
||||
|
||||
if originalConnection.SourcePort != remoteConnection.SourcePort {
|
||||
t.Fail()
|
||||
log.Printf("(in #%d) SourcePort's are not equal (orig: %d, unmsh: %d)", commandIndex, originalConnection.SourcePort, remoteConnection.SourcePort)
|
||||
}
|
||||
|
||||
if originalConnection.DestPort != remoteConnection.DestPort {
|
||||
t.Fail()
|
||||
log.Printf("(in #%d) DestPort's are not equal (orig: %d, unmsh: %d)", commandIndex, originalConnection.DestPort, remoteConnection.DestPort)
|
||||
}
|
||||
|
||||
if originalConnection.ClientIP != remoteConnection.ClientIP {
|
||||
t.Fail()
|
||||
log.Printf("(in #%d) ClientIP's are not equal (orig: %s, unmsh: %s)", commandIndex, originalConnection.ClientIP, remoteConnection.ClientIP)
|
||||
}
|
||||
|
||||
if originalConnection.ClientPort != remoteConnection.ClientPort {
|
||||
t.Fail()
|
||||
log.Printf("(in #%d) ClientPort's are not equal (orig: %d, unmsh: %d)", commandIndex, originalConnection.ClientPort, remoteConnection.ClientPort)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckClientParametersMarshalSupport(t *testing.T) {
|
||||
commandInput := &CheckClientParameters{
|
||||
Type: "checkClientParameters",
|
||||
SourceIP: "192.168.0.139",
|
||||
SourcePort: 19132,
|
||||
DestPort: 19132,
|
||||
Protocol: "tcp",
|
||||
}
|
||||
|
||||
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Generated array contents: %v", commandMarshalled)
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(commandMarshalled)
|
||||
commandType, 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)
|
||||
}
|
||||
|
||||
if commandInput.SourcePort != commandUnmarshalled.SourcePort {
|
||||
t.Fail()
|
||||
log.Printf("SourcePort's are not equal (orig: %d, unmsh: %d)", commandInput.SourcePort, commandUnmarshalled.SourcePort)
|
||||
}
|
||||
|
||||
if commandInput.DestPort != commandUnmarshalled.DestPort {
|
||||
t.Fail()
|
||||
log.Printf("DestPort's are not equal (orig: %d, unmsh: %d)", commandInput.DestPort, commandUnmarshalled.DestPort)
|
||||
}
|
||||
|
||||
if commandInput.Protocol != commandUnmarshalled.Protocol {
|
||||
t.Fail()
|
||||
log.Printf("Protocols are not equal (orig: %s, unmsh: %s)", commandInput.Protocol, commandUnmarshalled.Protocol)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckServerParametersMarshalSupport(t *testing.T) {
|
||||
commandInput := &CheckServerParameters{
|
||||
Type: "checkServerParameters",
|
||||
Arguments: []byte("Hello from automated testing"),
|
||||
}
|
||||
|
||||
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Generated array contents: %v", commandMarshalled)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(commandMarshalled)
|
||||
commandType, 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))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckParametersResponseMarshalSupport(t *testing.T) {
|
||||
commandInput := &CheckParametersResponse{
|
||||
Type: "checkParametersResponse",
|
||||
InResponseTo: "checkClientParameters",
|
||||
IsValid: true,
|
||||
Message: "Hello from automated testing",
|
||||
}
|
||||
|
||||
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Generated array contents: %v", commandMarshalled)
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(commandMarshalled)
|
||||
commandType, 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)
|
||||
}
|
||||
|
||||
if commandInput.IsValid != commandUnmarshalled.IsValid {
|
||||
t.Fail()
|
||||
log.Printf("IsValid's are not equal (orig: %t, unmsh: %t)", commandInput.IsValid, commandUnmarshalled.IsValid)
|
||||
}
|
||||
|
||||
if commandInput.Message != commandUnmarshalled.Message {
|
||||
t.Fail()
|
||||
log.Printf("Messages are not equal (orig: %s, unmsh: %s)", commandInput.Message, commandUnmarshalled.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendStatusRequestMarshalSupport(t *testing.T) {
|
||||
commandInput := &BackendStatusRequest{
|
||||
Type: "backendStatusRequest",
|
||||
}
|
||||
|
||||
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Generated array contents: %v", commandMarshalled)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(commandMarshalled)
|
||||
commandType, 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)
|
||||
|
||||
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 TestBackendStatusResponseMarshalSupport(t *testing.T) {
|
||||
commandInput := &BackendStatusResponse{
|
||||
Type: "backendStatusResponse",
|
||||
IsRunning: true,
|
||||
StatusCode: StatusFailure,
|
||||
Message: "Hello from automated testing",
|
||||
}
|
||||
|
||||
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Generated array contents: %v", commandMarshalled)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(commandMarshalled)
|
||||
commandType, 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)
|
||||
}
|
||||
|
||||
if commandInput.StatusCode != commandUnmarshalled.StatusCode {
|
||||
t.Fail()
|
||||
log.Printf("StatusCodes are not equal (orig: %d, unmsh: %d)", commandInput.StatusCode, commandUnmarshalled.StatusCode)
|
||||
}
|
||||
|
||||
if commandInput.Message != commandUnmarshalled.Message {
|
||||
t.Fail()
|
||||
log.Printf("Messages are not equal (orig: %s, unmsh: %s)", commandInput.Message, commandUnmarshalled.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyStatusRequestMarshalSupport(t *testing.T) {
|
||||
commandInput := &ProxyStatusRequest{
|
||||
Type: "proxyStatusRequest",
|
||||
SourceIP: "192.168.0.139",
|
||||
SourcePort: 19132,
|
||||
DestPort: 19132,
|
||||
Protocol: "tcp",
|
||||
}
|
||||
|
||||
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Generated array contents: %v", commandMarshalled)
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(commandMarshalled)
|
||||
commandType, 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)
|
||||
}
|
||||
|
||||
if commandInput.SourcePort != commandUnmarshalled.SourcePort {
|
||||
t.Fail()
|
||||
log.Printf("SourcePort's are not equal (orig: %d, unmsh: %d)", commandInput.SourcePort, commandUnmarshalled.SourcePort)
|
||||
}
|
||||
|
||||
if commandInput.DestPort != commandUnmarshalled.DestPort {
|
||||
t.Fail()
|
||||
log.Printf("DestPort's are not equal (orig: %d, unmsh: %d)", commandInput.DestPort, commandUnmarshalled.DestPort)
|
||||
}
|
||||
|
||||
if commandInput.Protocol != commandUnmarshalled.Protocol {
|
||||
t.Fail()
|
||||
log.Printf("Protocols are not equal (orig: %s, unmsh: %s)", commandInput.Protocol, commandUnmarshalled.Protocol)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyStatusResponseMarshalSupport(t *testing.T) {
|
||||
commandInput := &ProxyStatusResponse{
|
||||
Type: "proxyStatusResponse",
|
||||
SourceIP: "192.168.0.139",
|
||||
SourcePort: 19132,
|
||||
DestPort: 19132,
|
||||
Protocol: "tcp",
|
||||
IsActive: true,
|
||||
}
|
||||
|
||||
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Generated array contents: %v", commandMarshalled)
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(commandMarshalled)
|
||||
commandType, 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)
|
||||
}
|
||||
|
||||
if commandInput.SourcePort != commandUnmarshalled.SourcePort {
|
||||
t.Fail()
|
||||
log.Printf("SourcePort's are not equal (orig: %d, unmsh: %d)", commandInput.SourcePort, commandUnmarshalled.SourcePort)
|
||||
}
|
||||
|
||||
if commandInput.DestPort != commandUnmarshalled.DestPort {
|
||||
t.Fail()
|
||||
log.Printf("DestPort's are not equal (orig: %d, unmsh: %d)", commandInput.DestPort, commandUnmarshalled.DestPort)
|
||||
}
|
||||
|
||||
if commandInput.Protocol != commandUnmarshalled.Protocol {
|
||||
t.Fail()
|
||||
log.Printf("Protocols are not equal (orig: %s, unmsh: %s)", commandInput.Protocol, commandUnmarshalled.Protocol)
|
||||
}
|
||||
|
||||
if commandInput.IsActive != commandUnmarshalled.IsActive {
|
||||
t.Fail()
|
||||
log.Printf("IsActive's are not equal (orig: %t, unmsh: %t)", commandInput.IsActive, commandUnmarshalled.IsActive)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyConnectionRequestMarshalSupport(t *testing.T) {
|
||||
commandInput := &ProxyInstanceRequest{
|
||||
Type: "proxyInstanceRequest",
|
||||
}
|
||||
|
||||
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Generated array contents: %v", commandMarshalled)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(commandMarshalled)
|
||||
commandType, 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)
|
||||
|
||||
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 TestProxyConnectionResponseMarshalSupport(t *testing.T) {
|
||||
commandInput := &ProxyInstanceResponse{
|
||||
Type: "proxyInstanceResponse",
|
||||
Proxies: []*ProxyInstance{
|
||||
{
|
||||
SourceIP: "192.168.0.168",
|
||||
SourcePort: 25565,
|
||||
DestPort: 25565,
|
||||
Protocol: "tcp",
|
||||
},
|
||||
{
|
||||
SourceIP: "127.0.0.1",
|
||||
SourcePort: 19132,
|
||||
DestPort: 19132,
|
||||
Protocol: "udp",
|
||||
},
|
||||
{
|
||||
SourceIP: "68.42.203.47",
|
||||
SourcePort: 22,
|
||||
DestPort: 2222,
|
||||
Protocol: "tcp",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
commandMarshalled, err := Marshal(commandInput.Type, commandInput)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Generated array contents: %v", commandMarshalled)
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(commandMarshalled)
|
||||
commandType, 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]
|
||||
|
||||
if originalProxy.SourceIP != remoteProxy.SourceIP {
|
||||
t.Fail()
|
||||
log.Printf("(in #%d) SourceIP's are not equal (orig: %s, unmsh: %s)", proxyIndex, originalProxy.SourceIP, remoteProxy.SourceIP)
|
||||
}
|
||||
|
||||
if originalProxy.SourcePort != remoteProxy.SourcePort {
|
||||
t.Fail()
|
||||
log.Printf("(in #%d) SourcePort's are not equal (orig: %d, unmsh: %d)", proxyIndex, originalProxy.SourcePort, remoteProxy.SourcePort)
|
||||
}
|
||||
|
||||
if originalProxy.DestPort != remoteProxy.DestPort {
|
||||
t.Fail()
|
||||
log.Printf("(in #%d) DestPort's are not equal (orig: %d, unmsh: %d)", proxyIndex, originalProxy.DestPort, remoteProxy.DestPort)
|
||||
}
|
||||
|
||||
if originalProxy.Protocol != remoteProxy.Protocol {
|
||||
t.Fail()
|
||||
log.Printf("(in #%d) ClientIP's are not equal (orig: %s, unmsh: %s)", proxyIndex, originalProxy.Protocol, remoteProxy.Protocol)
|
||||
}
|
||||
}
|
||||
}
|
653
backend/commonbackend/unmarshal.go
Normal file
653
backend/commonbackend/unmarshal.go
Normal file
|
@ -0,0 +1,653 @@
|
|||
package commonbackend
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
)
|
||||
|
||||
func unmarshalIndividualConnectionStruct(conn io.Reader) (*ProxyClientConnection, error) {
|
||||
serverIPVersion := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(serverIPVersion); err != nil {
|
||||
return nil, fmt.Errorf("couldn't read server IP version")
|
||||
}
|
||||
|
||||
var serverIPSize uint8
|
||||
|
||||
if serverIPVersion[0] == 4 {
|
||||
serverIPSize = IPv4Size
|
||||
} else if serverIPVersion[0] == 6 {
|
||||
serverIPSize = IPv6Size
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid server IP version recieved")
|
||||
}
|
||||
|
||||
serverIP := make(net.IP, serverIPSize)
|
||||
|
||||
if _, err := conn.Read(serverIP); err != nil {
|
||||
return nil, fmt.Errorf("couldn't read server IP")
|
||||
}
|
||||
|
||||
sourcePort := make([]byte, 2)
|
||||
|
||||
if _, err := conn.Read(sourcePort); err != nil {
|
||||
return nil, fmt.Errorf("couldn't read source port")
|
||||
}
|
||||
|
||||
destinationPort := make([]byte, 2)
|
||||
|
||||
if _, err := conn.Read(destinationPort); err != nil {
|
||||
return nil, fmt.Errorf("couldn't read source port")
|
||||
}
|
||||
|
||||
clientIPVersion := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(clientIPVersion); err != nil {
|
||||
return nil, fmt.Errorf("couldn't read server IP version")
|
||||
}
|
||||
|
||||
var clientIPSize uint8
|
||||
|
||||
if clientIPVersion[0] == 4 {
|
||||
clientIPSize = IPv4Size
|
||||
} else if clientIPVersion[0] == 6 {
|
||||
clientIPSize = IPv6Size
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid server IP version recieved")
|
||||
}
|
||||
|
||||
clientIP := make(net.IP, clientIPSize)
|
||||
|
||||
if _, err := conn.Read(clientIP); err != nil {
|
||||
return nil, fmt.Errorf("couldn't read server IP")
|
||||
}
|
||||
|
||||
clientPort := make([]byte, 2)
|
||||
|
||||
if _, err := conn.Read(clientPort); err != nil {
|
||||
return nil, fmt.Errorf("couldn't read source port")
|
||||
}
|
||||
|
||||
return &ProxyClientConnection{
|
||||
SourceIP: serverIP.String(),
|
||||
SourcePort: binary.BigEndian.Uint16(sourcePort),
|
||||
DestPort: binary.BigEndian.Uint16(destinationPort),
|
||||
ClientIP: clientIP.String(),
|
||||
ClientPort: binary.BigEndian.Uint16(clientPort),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func unmarshalIndividualProxyStruct(conn io.Reader) (*ProxyInstance, error) {
|
||||
ipVersion := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(ipVersion); err != nil {
|
||||
return nil, fmt.Errorf("couldn't read ip version")
|
||||
}
|
||||
|
||||
var ipSize uint8
|
||||
|
||||
if ipVersion[0] == 4 {
|
||||
ipSize = IPv4Size
|
||||
} else if ipVersion[0] == 6 {
|
||||
ipSize = IPv6Size
|
||||
} else {
|
||||
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")
|
||||
}
|
||||
|
||||
sourcePort := make([]byte, 2)
|
||||
|
||||
if _, err := conn.Read(sourcePort); err != nil {
|
||||
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")
|
||||
}
|
||||
|
||||
protocolBytes := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(protocolBytes); err != nil {
|
||||
return nil, fmt.Errorf("couldn't read protocol")
|
||||
}
|
||||
|
||||
var protocol string
|
||||
|
||||
if protocolBytes[0] == TCP {
|
||||
protocol = "tcp"
|
||||
} else if protocolBytes[0] == UDP {
|
||||
protocol = "udp"
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid protocol")
|
||||
}
|
||||
|
||||
return &ProxyInstance{
|
||||
SourceIP: ip.String(),
|
||||
SourcePort: binary.BigEndian.Uint16(sourcePort),
|
||||
DestPort: binary.BigEndian.Uint16(destPort),
|
||||
Protocol: protocol,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func Unmarshal(conn io.Reader) (string, interface{}, error) {
|
||||
commandType := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(commandType); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read command")
|
||||
}
|
||||
|
||||
switch commandType[0] {
|
||||
case StartID:
|
||||
argumentsLength := make([]byte, 2)
|
||||
|
||||
if _, err := conn.Read(argumentsLength); err != nil {
|
||||
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 "start", &Start{
|
||||
Type: "start",
|
||||
Arguments: arguments,
|
||||
}, nil
|
||||
case StopID:
|
||||
return "stop", &Stop{
|
||||
Type: "stop",
|
||||
}, nil
|
||||
case AddProxyID:
|
||||
ipVersion := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(ipVersion); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read ip version")
|
||||
}
|
||||
|
||||
var ipSize uint8
|
||||
|
||||
if ipVersion[0] == 4 {
|
||||
ipSize = IPv4Size
|
||||
} else if ipVersion[0] == 6 {
|
||||
ipSize = IPv6Size
|
||||
} else {
|
||||
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")
|
||||
}
|
||||
|
||||
sourcePort := make([]byte, 2)
|
||||
|
||||
if _, err := conn.Read(sourcePort); err != nil {
|
||||
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")
|
||||
}
|
||||
|
||||
protocolBytes := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(protocolBytes); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read protocol")
|
||||
}
|
||||
|
||||
var protocol string
|
||||
|
||||
if protocolBytes[0] == TCP {
|
||||
protocol = "tcp"
|
||||
} else if protocolBytes[1] == UDP {
|
||||
protocol = "udp"
|
||||
} else {
|
||||
return "", nil, fmt.Errorf("invalid protocol")
|
||||
}
|
||||
|
||||
return "addProxy", &AddProxy{
|
||||
Type: "addProxy",
|
||||
SourceIP: ip.String(),
|
||||
SourcePort: binary.BigEndian.Uint16(sourcePort),
|
||||
DestPort: binary.BigEndian.Uint16(destPort),
|
||||
Protocol: protocol,
|
||||
}, nil
|
||||
case RemoveProxyID:
|
||||
ipVersion := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(ipVersion); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read ip version")
|
||||
}
|
||||
|
||||
var ipSize uint8
|
||||
|
||||
if ipVersion[0] == 4 {
|
||||
ipSize = IPv4Size
|
||||
} else if ipVersion[0] == 6 {
|
||||
ipSize = IPv6Size
|
||||
} else {
|
||||
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")
|
||||
}
|
||||
|
||||
sourcePort := make([]byte, 2)
|
||||
|
||||
if _, err := conn.Read(sourcePort); err != nil {
|
||||
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")
|
||||
}
|
||||
|
||||
protocolBytes := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(protocolBytes); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read protocol")
|
||||
}
|
||||
|
||||
var protocol string
|
||||
|
||||
if protocolBytes[0] == TCP {
|
||||
protocol = "tcp"
|
||||
} else if protocolBytes[1] == UDP {
|
||||
protocol = "udp"
|
||||
} else {
|
||||
return "", nil, fmt.Errorf("invalid protocol")
|
||||
}
|
||||
|
||||
return "removeProxy", &RemoveProxy{
|
||||
Type: "removeProxy",
|
||||
SourceIP: ip.String(),
|
||||
SourcePort: binary.BigEndian.Uint16(sourcePort),
|
||||
DestPort: binary.BigEndian.Uint16(destPort),
|
||||
Protocol: protocol,
|
||||
}, nil
|
||||
case ProxyConnectionsResponseID:
|
||||
connections := []*ProxyClientConnection{}
|
||||
delimiter := make([]byte, 1)
|
||||
var errorReturn error
|
||||
|
||||
// Infinite loop because we don't know the length
|
||||
for {
|
||||
connection, err := unmarshalIndividualConnectionStruct(conn)
|
||||
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
connections = append(connections, connection)
|
||||
|
||||
if _, err := conn.Read(delimiter); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read delimiter")
|
||||
}
|
||||
|
||||
if delimiter[0] == '\r' {
|
||||
continue
|
||||
} else if delimiter[0] == '\n' {
|
||||
break
|
||||
} else {
|
||||
// WTF? This shouldn't happen. Break out and return, but give an error
|
||||
errorReturn = fmt.Errorf("invalid delimiter recieved while processing stream")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return "proxyConnectionsResponse", &ProxyConnectionsResponse{
|
||||
Type: "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")
|
||||
}
|
||||
|
||||
var ipSize uint8
|
||||
|
||||
if ipVersion[0] == 4 {
|
||||
ipSize = IPv4Size
|
||||
} else if ipVersion[0] == 6 {
|
||||
ipSize = IPv6Size
|
||||
} else {
|
||||
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")
|
||||
}
|
||||
|
||||
sourcePort := make([]byte, 2)
|
||||
|
||||
if _, err := conn.Read(sourcePort); err != nil {
|
||||
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")
|
||||
}
|
||||
|
||||
protocolBytes := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(protocolBytes); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read protocol")
|
||||
}
|
||||
|
||||
var protocol string
|
||||
|
||||
if protocolBytes[0] == TCP {
|
||||
protocol = "tcp"
|
||||
} else if protocolBytes[1] == UDP {
|
||||
protocol = "udp"
|
||||
} else {
|
||||
return "", nil, fmt.Errorf("invalid protocol")
|
||||
}
|
||||
|
||||
return "checkClientParameters", &CheckClientParameters{
|
||||
Type: "checkClientParameters",
|
||||
SourceIP: ip.String(),
|
||||
SourcePort: binary.BigEndian.Uint16(sourcePort),
|
||||
DestPort: binary.BigEndian.Uint16(destPort),
|
||||
Protocol: protocol,
|
||||
}, nil
|
||||
case CheckServerParametersID:
|
||||
argumentsLength := make([]byte, 2)
|
||||
|
||||
if _, err := conn.Read(argumentsLength); err != nil {
|
||||
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 "checkServerParameters", &CheckServerParameters{
|
||||
Type: "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")
|
||||
}
|
||||
|
||||
var checkMethod string
|
||||
|
||||
if checkMethodByte[0] == CheckClientParametersID {
|
||||
checkMethod = "checkClientParameters"
|
||||
} else if checkMethodByte[1] == CheckServerParametersID {
|
||||
checkMethod = "checkServerParameters"
|
||||
} else {
|
||||
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")
|
||||
}
|
||||
|
||||
messageLengthBytes := make([]byte, 2)
|
||||
|
||||
if _, err := conn.Read(messageLengthBytes); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read message length")
|
||||
}
|
||||
|
||||
messageLength := binary.BigEndian.Uint16(messageLengthBytes)
|
||||
var message string
|
||||
|
||||
if messageLength != 0 {
|
||||
messageBytes := make([]byte, messageLength)
|
||||
|
||||
if _, err := conn.Read(messageBytes); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read message")
|
||||
}
|
||||
|
||||
message = string(messageBytes)
|
||||
}
|
||||
|
||||
return "checkParametersResponse", &CheckParametersResponse{
|
||||
Type: "checkParametersResponse",
|
||||
InResponseTo: checkMethod,
|
||||
IsValid: isValid[0] == 1,
|
||||
Message: message,
|
||||
}, nil
|
||||
case BackendStatusResponseID:
|
||||
isRunning := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(isRunning); err != nil {
|
||||
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")
|
||||
}
|
||||
|
||||
messageLengthBytes := make([]byte, 2)
|
||||
|
||||
if _, err := conn.Read(messageLengthBytes); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read message length")
|
||||
}
|
||||
|
||||
messageLength := binary.BigEndian.Uint16(messageLengthBytes)
|
||||
var message string
|
||||
|
||||
if messageLength != 0 {
|
||||
messageBytes := make([]byte, messageLength)
|
||||
|
||||
if _, err := conn.Read(messageBytes); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read message")
|
||||
}
|
||||
|
||||
message = string(messageBytes)
|
||||
}
|
||||
|
||||
return "backendStatusResponse", &BackendStatusResponse{
|
||||
Type: "backendStatusResponse",
|
||||
IsRunning: isRunning[0] == 1,
|
||||
StatusCode: int(statusCode[0]),
|
||||
Message: message,
|
||||
}, nil
|
||||
case BackendStatusRequestID:
|
||||
return "backendStatusRequest", &BackendStatusRequest{
|
||||
Type: "backendStatusRequest",
|
||||
}, nil
|
||||
case ProxyStatusRequestID:
|
||||
ipVersion := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(ipVersion); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read ip version")
|
||||
}
|
||||
|
||||
var ipSize uint8
|
||||
|
||||
if ipVersion[0] == 4 {
|
||||
ipSize = IPv4Size
|
||||
} else if ipVersion[0] == 6 {
|
||||
ipSize = IPv6Size
|
||||
} else {
|
||||
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")
|
||||
}
|
||||
|
||||
sourcePort := make([]byte, 2)
|
||||
|
||||
if _, err := conn.Read(sourcePort); err != nil {
|
||||
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")
|
||||
}
|
||||
|
||||
protocolBytes := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(protocolBytes); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read protocol")
|
||||
}
|
||||
|
||||
var protocol string
|
||||
|
||||
if protocolBytes[0] == TCP {
|
||||
protocol = "tcp"
|
||||
} else if protocolBytes[1] == UDP {
|
||||
protocol = "udp"
|
||||
} else {
|
||||
return "", nil, fmt.Errorf("invalid protocol")
|
||||
}
|
||||
|
||||
return "proxyStatusRequest", &ProxyStatusRequest{
|
||||
Type: "proxyStatusRequest",
|
||||
SourceIP: ip.String(),
|
||||
SourcePort: binary.BigEndian.Uint16(sourcePort),
|
||||
DestPort: binary.BigEndian.Uint16(destPort),
|
||||
Protocol: protocol,
|
||||
}, nil
|
||||
case ProxyStatusResponseID:
|
||||
ipVersion := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(ipVersion); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read ip version")
|
||||
}
|
||||
|
||||
var ipSize uint8
|
||||
|
||||
if ipVersion[0] == 4 {
|
||||
ipSize = IPv4Size
|
||||
} else if ipVersion[0] == 6 {
|
||||
ipSize = IPv6Size
|
||||
} else {
|
||||
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")
|
||||
}
|
||||
|
||||
sourcePort := make([]byte, 2)
|
||||
|
||||
if _, err := conn.Read(sourcePort); err != nil {
|
||||
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")
|
||||
}
|
||||
|
||||
protocolBytes := make([]byte, 1)
|
||||
|
||||
if _, err := conn.Read(protocolBytes); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read protocol")
|
||||
}
|
||||
|
||||
var protocol string
|
||||
|
||||
if protocolBytes[0] == TCP {
|
||||
protocol = "tcp"
|
||||
} else if protocolBytes[1] == UDP {
|
||||
protocol = "udp"
|
||||
} else {
|
||||
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 "proxyStatusResponse", &ProxyStatusResponse{
|
||||
Type: "proxyStatusResponse",
|
||||
SourceIP: ip.String(),
|
||||
SourcePort: binary.BigEndian.Uint16(sourcePort),
|
||||
DestPort: binary.BigEndian.Uint16(destPort),
|
||||
Protocol: protocol,
|
||||
IsActive: isActive[0] == 1,
|
||||
}, nil
|
||||
case ProxyInstanceRequestID:
|
||||
return "proxyInstanceRequest", &ProxyInstanceRequest{
|
||||
Type: "proxyInstanceRequest",
|
||||
}, nil
|
||||
case ProxyInstanceResponseID:
|
||||
proxies := []*ProxyInstance{}
|
||||
delimiter := make([]byte, 1)
|
||||
var errorReturn error
|
||||
|
||||
// Infinite loop because we don't know the length
|
||||
for {
|
||||
proxy, err := unmarshalIndividualProxyStruct(conn)
|
||||
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
proxies = append(proxies, proxy)
|
||||
|
||||
if _, err := conn.Read(delimiter); err != nil {
|
||||
return "", nil, fmt.Errorf("couldn't read delimiter")
|
||||
}
|
||||
|
||||
if delimiter[0] == '\r' {
|
||||
continue
|
||||
} else if delimiter[0] == '\n' {
|
||||
break
|
||||
} else {
|
||||
// WTF? This shouldn't happen. Break out and return, but give an error
|
||||
errorReturn = fmt.Errorf("invalid delimiter recieved while processing stream")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return "proxyInstanceResponse", &ProxyInstanceResponse{
|
||||
Type: "proxyInstanceResponse",
|
||||
Proxies: proxies,
|
||||
}, errorReturn
|
||||
case ProxyConnectionsRequestID:
|
||||
return "proxyConnectionsRequest", &ProxyConnectionsRequest{
|
||||
Type: "proxyConnectionsRequest",
|
||||
}, nil
|
||||
}
|
||||
|
||||
return "", nil, fmt.Errorf("couldn't match command ID")
|
||||
}
|
2
backend/dev.env
Normal file
2
backend/dev.env
Normal file
|
@ -0,0 +1,2 @@
|
|||
HERMES_DATABASE_BACKEND=sqlite
|
||||
HERMES_SQLITE_FILEPATH=../.tmp/sqlite.db
|
83
backend/dummybackend/main.go
Normal file
83
backend/dummybackend/main.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"git.terah.dev/imterah/hermes/backendutil"
|
||||
"git.terah.dev/imterah/hermes/commonbackend"
|
||||
"github.com/charmbracelet/log"
|
||||
)
|
||||
|
||||
type DummyBackend struct {
|
||||
}
|
||||
|
||||
func (backend *DummyBackend) StartBackend(byte []byte) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (backend *DummyBackend) StopBackend() (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (backend *DummyBackend) StartProxy(command *commonbackend.AddProxy) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (backend *DummyBackend) StopProxy(command *commonbackend.RemoveProxy) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (backend *DummyBackend) GetAllClientConnections() []*commonbackend.ProxyClientConnection {
|
||||
return []*commonbackend.ProxyClientConnection{}
|
||||
}
|
||||
|
||||
func (backend *DummyBackend) CheckParametersForConnections(clientParameters *commonbackend.CheckClientParameters) *commonbackend.CheckParametersResponse {
|
||||
// You don't have to specify Type and InReplyTo. Those will be handled for you.
|
||||
// Message is optional.
|
||||
return &commonbackend.CheckParametersResponse{
|
||||
IsValid: true,
|
||||
Message: "Valid!",
|
||||
}
|
||||
}
|
||||
|
||||
func (backend *DummyBackend) CheckParametersForBackend(arguments []byte) *commonbackend.CheckParametersResponse {
|
||||
// You don't have to specify Type and InReplyTo. Those will be handled for you.
|
||||
// Message is optional.
|
||||
return &commonbackend.CheckParametersResponse{
|
||||
IsValid: true,
|
||||
Message: "Valid!",
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// When using logging, you should use charmbracelet/log, because that's what everything else uses in this ecosystem of a project. - imterah
|
||||
logLevel := os.Getenv("HERMES_LOG_LEVEL")
|
||||
|
||||
if logLevel != "" {
|
||||
switch logLevel {
|
||||
case "debug":
|
||||
log.SetLevel(log.DebugLevel)
|
||||
|
||||
case "info":
|
||||
log.SetLevel(log.InfoLevel)
|
||||
|
||||
case "warn":
|
||||
log.SetLevel(log.WarnLevel)
|
||||
|
||||
case "error":
|
||||
log.SetLevel(log.ErrorLevel)
|
||||
|
||||
case "fatal":
|
||||
log.SetLevel(log.FatalLevel)
|
||||
}
|
||||
}
|
||||
|
||||
backend := &DummyBackend{}
|
||||
|
||||
application := backendutil.NewHelper(backend)
|
||||
err := application.Start()
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("failed execution in application: %s", err.Error())
|
||||
}
|
||||
}
|
332
backend/externalbackendlauncher/main.go
Normal file
332
backend/externalbackendlauncher/main.go
Normal file
|
@ -0,0 +1,332 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.terah.dev/imterah/hermes/backendlauncher"
|
||||
"git.terah.dev/imterah/hermes/commonbackend"
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
type ProxyInstance struct {
|
||||
SourceIP string `json:"sourceIP"`
|
||||
SourcePort uint16 `json:"sourcePort"`
|
||||
DestPort uint16 `json:"destPort"`
|
||||
Protocol string `json:"protocol"`
|
||||
}
|
||||
|
||||
type WriteLogger struct {
|
||||
UseError bool
|
||||
}
|
||||
|
||||
// TODO: deprecate UseError switching
|
||||
func (writer WriteLogger) Write(p []byte) (n int, err error) {
|
||||
logSplit := strings.Split(string(p), "\n")
|
||||
|
||||
for _, line := range logSplit {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if writer.UseError {
|
||||
log.Errorf("application: %s", line)
|
||||
} else {
|
||||
log.Infof("application: %s", line)
|
||||
}
|
||||
}
|
||||
|
||||
return len(p), err
|
||||
}
|
||||
|
||||
var (
|
||||
tempDir string
|
||||
logLevel string
|
||||
)
|
||||
|
||||
func entrypoint(cCtx *cli.Context) error {
|
||||
executablePath := cCtx.Args().Get(0)
|
||||
|
||||
if executablePath == "" {
|
||||
return fmt.Errorf("executable file is not set")
|
||||
}
|
||||
|
||||
executableParamsPath := cCtx.String("params-path")
|
||||
|
||||
if executablePath == "" {
|
||||
return fmt.Errorf("executable parameters is not set")
|
||||
}
|
||||
|
||||
proxyFilePath := cCtx.String("proxies")
|
||||
proxies := []ProxyInstance{}
|
||||
|
||||
if proxyFilePath != "" {
|
||||
proxyFile, err := os.ReadFile(proxyFilePath)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read proxy file: %s", err.Error())
|
||||
}
|
||||
|
||||
err = json.Unmarshal(proxyFile, &proxies)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse proxy file: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
log.Debugf("discovered %d proxies.", len(proxies))
|
||||
|
||||
backendParameters, err := os.ReadFile(executableParamsPath)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not read backend parameters: %s", err.Error())
|
||||
}
|
||||
|
||||
_, err = os.Stat(executablePath)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get backend executable information: %s", err.Error())
|
||||
}
|
||||
|
||||
log.Debug("running socket acquisition")
|
||||
|
||||
sockPath, sockListener, err := backendlauncher.GetUnixSocket(tempDir)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to acquire unix socket: %s", err.Error())
|
||||
}
|
||||
|
||||
log.Debugf("acquisition was successful: %s", sockPath)
|
||||
|
||||
go func() {
|
||||
log.Debug("entering execution loop (in auxiliary goroutine)...")
|
||||
|
||||
for {
|
||||
log.Info("waiting for Unix socket connections...")
|
||||
sock, err := sockListener.Accept()
|
||||
log.Info("recieved connection. initializing...")
|
||||
|
||||
if err != nil {
|
||||
log.Warnf("failed to accept socket connection: %s", err.Error())
|
||||
}
|
||||
|
||||
defer sock.Close()
|
||||
|
||||
startCommand := &commonbackend.Start{
|
||||
Type: "start",
|
||||
Arguments: backendParameters,
|
||||
}
|
||||
|
||||
startMarshalledCommand, err := commonbackend.Marshal("start", startCommand)
|
||||
|
||||
if err != nil {
|
||||
log.Errorf("failed to generate start command: %s", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
if _, err = sock.Write(startMarshalledCommand); err != nil {
|
||||
log.Errorf("failed to write to socket: %s", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
commandType, 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 {
|
||||
log.Error("failed to typecast response")
|
||||
continue
|
||||
}
|
||||
|
||||
if !command.IsRunning {
|
||||
var status string
|
||||
|
||||
if command.StatusCode == commonbackend.StatusSuccess {
|
||||
status = "Success"
|
||||
} else {
|
||||
status = "Failure"
|
||||
}
|
||||
|
||||
log.Errorf("failed to start backend (status: %s): %s", status, command.Message)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Info("successfully started backend.")
|
||||
|
||||
hasAnyFailed := false
|
||||
|
||||
for _, proxy := range proxies {
|
||||
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)
|
||||
|
||||
if err != nil {
|
||||
log.Errorf("failed to generate start command: %s", err.Error())
|
||||
hasAnyFailed = true
|
||||
continue
|
||||
}
|
||||
|
||||
if _, err = sock.Write(marshalledProxyCommand); err != nil {
|
||||
log.Errorf("failed to write to socket: %s", err.Error())
|
||||
hasAnyFailed = true
|
||||
continue
|
||||
}
|
||||
|
||||
commandType, commandRaw, err := commonbackend.Unmarshal(sock)
|
||||
|
||||
if err != nil {
|
||||
log.Errorf("failed to read from/unmarshal from socket: %s", err.Error())
|
||||
hasAnyFailed = true
|
||||
continue
|
||||
}
|
||||
|
||||
if commandType != "proxyStatusResponse" {
|
||||
log.Errorf("recieved commandType '%s', expecting 'proxyStatusResponse'", commandType)
|
||||
hasAnyFailed = true
|
||||
continue
|
||||
}
|
||||
|
||||
command, ok := commandRaw.(*commonbackend.ProxyStatusResponse)
|
||||
|
||||
if !ok {
|
||||
log.Error("failed to typecast response")
|
||||
hasAnyFailed = true
|
||||
continue
|
||||
}
|
||||
|
||||
if !command.IsActive {
|
||||
log.Error("failed to activate: isActive is false in response to AddProxy{} call")
|
||||
hasAnyFailed = true
|
||||
continue
|
||||
}
|
||||
|
||||
log.Infof("successfully initialized proxy %s:%d -> remote:%d", proxy.SourceIP, proxy.SourcePort, proxy.DestPort)
|
||||
}
|
||||
|
||||
if hasAnyFailed {
|
||||
log.Error("failed to initialize all proxies (read logs above)")
|
||||
} else {
|
||||
log.Info("successfully initialized all proxies")
|
||||
}
|
||||
|
||||
log.Debug("entering infinite keepalive loop...")
|
||||
|
||||
for {
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
log.Debug("entering execution loop (in main goroutine)...")
|
||||
|
||||
stdout := WriteLogger{
|
||||
UseError: false,
|
||||
}
|
||||
|
||||
stderr := WriteLogger{
|
||||
UseError: true,
|
||||
}
|
||||
|
||||
for {
|
||||
log.Info("starting process...")
|
||||
// TODO: can we reuse cmd?
|
||||
|
||||
cmd := exec.Command(executablePath)
|
||||
cmd.Env = append(cmd.Env, fmt.Sprintf("HERMES_API_SOCK=%s", sockPath), fmt.Sprintf("HERMES_LOG_LEVEL=%s", logLevel))
|
||||
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
|
||||
err := cmd.Run()
|
||||
|
||||
if err != nil {
|
||||
if err, ok := err.(*exec.ExitError); ok {
|
||||
log.Warnf("backend died with exit code '%d' and with error '%s'", err.ExitCode(), err.Error())
|
||||
} else {
|
||||
log.Warnf("backend died with error: %s", err.Error())
|
||||
}
|
||||
} else {
|
||||
log.Info("process exited gracefully.")
|
||||
}
|
||||
|
||||
log.Info("sleeping 5 seconds, and then restarting process")
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
logLevel = os.Getenv("HERMES_LOG_LEVEL")
|
||||
|
||||
if logLevel == "" {
|
||||
logLevel = "fatal"
|
||||
}
|
||||
|
||||
switch logLevel {
|
||||
case "debug":
|
||||
log.SetLevel(log.DebugLevel)
|
||||
|
||||
case "info":
|
||||
log.SetLevel(log.InfoLevel)
|
||||
|
||||
case "warn":
|
||||
log.SetLevel(log.WarnLevel)
|
||||
|
||||
case "error":
|
||||
log.SetLevel(log.ErrorLevel)
|
||||
|
||||
case "fatal":
|
||||
log.SetLevel(log.FatalLevel)
|
||||
}
|
||||
|
||||
var err error
|
||||
tempDir, err = os.MkdirTemp("", "hermes-sockets-")
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create sockets directory: %s", err.Error())
|
||||
}
|
||||
|
||||
app := &cli.App{
|
||||
Name: "externalbackendlauncher",
|
||||
Usage: "for development purposes only -- external backend launcher for Hermes",
|
||||
Action: entrypoint,
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "params-path",
|
||||
Aliases: []string{"params", "pp"},
|
||||
Usage: "file containing the parameters that are sent to the backend",
|
||||
Required: true,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "proxies",
|
||||
Aliases: []string{"p"},
|
||||
Usage: "file that contains the list of proxies to setup in JSON format",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
23
backend/go.mod
Normal file
23
backend/go.mod
Normal file
|
@ -0,0 +1,23 @@
|
|||
module git.terah.dev/imterah/hermes
|
||||
|
||||
go 1.23.3
|
||||
|
||||
require (
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||
github.com/charmbracelet/lipgloss v0.10.0 // indirect
|
||||
github.com/charmbracelet/log v0.4.0 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
|
||||
github.com/go-logfmt/logfmt v0.6.0 // indirect
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.18 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/muesli/reflow v0.3.0 // indirect
|
||||
github.com/muesli/termenv v0.15.2 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/urfave/cli/v2 v2.27.5 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
|
||||
golang.org/x/crypto v0.29.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
|
||||
golang.org/x/sys v0.27.0 // indirect
|
||||
)
|
40
backend/go.sum
Normal file
40
backend/go.sum
Normal file
|
@ -0,0 +1,40 @@
|
|||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
|
||||
github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=
|
||||
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
|
||||
github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM=
|
||||
github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
|
||||
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
|
||||
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
|
||||
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
|
||||
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
|
||||
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
|
||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
|
||||
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
|
||||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
|
||||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
|
||||
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
|
||||
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
|
||||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
|
||||
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
342
backend/sshbackend/main.go
Normal file
342
backend/sshbackend/main.go
Normal file
|
@ -0,0 +1,342 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"git.terah.dev/imterah/hermes/backendutil"
|
||||
"git.terah.dev/imterah/hermes/commonbackend"
|
||||
"github.com/charmbracelet/log"
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
type SSHListener struct {
|
||||
SourceIP string
|
||||
SourcePort uint16
|
||||
DestPort uint16
|
||||
Protocol string // Will be either 'tcp' or 'udp'
|
||||
Listeners []net.Listener
|
||||
}
|
||||
|
||||
type SSHBackend struct {
|
||||
config SSHBackendData
|
||||
conn *ssh.Client
|
||||
clients []*commonbackend.ProxyClientConnection
|
||||
proxies []*SSHListener
|
||||
arrayPropMutex sync.Mutex
|
||||
}
|
||||
|
||||
type SSHBackendData struct {
|
||||
IP string `json:"ip"`
|
||||
Port uint16 `json:"port"`
|
||||
Username string `json:"username"`
|
||||
PrivateKey string `json:"privateKey"`
|
||||
ListenOnIPs []string `json:"listenOnIPs"`
|
||||
}
|
||||
|
||||
func (backend *SSHBackend) StartBackend(bytes []byte) (bool, error) {
|
||||
log.Info("SSHBackend is initializing...")
|
||||
var backendData SSHBackendData
|
||||
|
||||
err := json.Unmarshal(bytes, &backendData)
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
backend.config = backendData
|
||||
|
||||
if len(backend.config.ListenOnIPs) == 0 {
|
||||
backend.config.ListenOnIPs = []string{"0.0.0.0"}
|
||||
}
|
||||
|
||||
signer, err := ssh.ParsePrivateKey([]byte(backendData.PrivateKey))
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
auth := ssh.PublicKeys(signer)
|
||||
|
||||
config := &ssh.ClientConfig{
|
||||
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
||||
User: backendData.Username,
|
||||
Auth: []ssh.AuthMethod{
|
||||
auth,
|
||||
},
|
||||
}
|
||||
|
||||
conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", backendData.IP, backendData.Port), config)
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
backend.conn = conn
|
||||
|
||||
log.Info("SSHBackend has initialized successfully.")
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (backend *SSHBackend) StopBackend() (bool, error) {
|
||||
err := backend.conn.Close()
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (backend *SSHBackend) StartProxy(command *commonbackend.AddProxy) (bool, error) {
|
||||
listenerObject := &SSHListener{
|
||||
SourceIP: command.SourceIP,
|
||||
SourcePort: command.SourcePort,
|
||||
DestPort: command.DestPort,
|
||||
Protocol: command.Protocol,
|
||||
Listeners: []net.Listener{},
|
||||
}
|
||||
|
||||
for _, ipListener := range backend.config.ListenOnIPs {
|
||||
ip := net.TCPAddr{
|
||||
IP: net.ParseIP(ipListener),
|
||||
Port: int(command.DestPort),
|
||||
}
|
||||
|
||||
listener, err := backend.conn.ListenTCP(&ip)
|
||||
|
||||
if err != nil {
|
||||
// Incase we error out, we clean up all the other listeners
|
||||
for _, listener := range listenerObject.Listeners {
|
||||
err = listener.Close()
|
||||
|
||||
if err != nil {
|
||||
log.Warnf("failed to close listener upon failure cleanup: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return false, err
|
||||
}
|
||||
|
||||
listenerObject.Listeners = append(listenerObject.Listeners, listener)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
forwardedConn, err := listener.Accept()
|
||||
|
||||
if err != nil {
|
||||
log.Warnf("failed to accept listener connection: %s", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
sourceConn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", command.SourceIP, command.SourcePort))
|
||||
|
||||
if err != nil {
|
||||
log.Warnf("failed to dial source connection: %s", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
clientIPAndPort := forwardedConn.RemoteAddr().String()
|
||||
clientIP := clientIPAndPort[:strings.LastIndex(clientIPAndPort, ":")]
|
||||
clientPort, err := strconv.Atoi(clientIPAndPort[strings.LastIndex(clientIPAndPort, ":")+1:])
|
||||
|
||||
if err != nil {
|
||||
log.Warnf("failed to parse client port: %s", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
advertisedConn := &commonbackend.ProxyClientConnection{
|
||||
SourceIP: command.SourceIP,
|
||||
SourcePort: command.SourcePort,
|
||||
DestPort: command.DestPort,
|
||||
ClientIP: clientIP,
|
||||
ClientPort: uint16(clientPort),
|
||||
|
||||
// FIXME (imterah): shouldn't protocol be in here?
|
||||
// Protocol: command.Protocol,
|
||||
}
|
||||
|
||||
backend.arrayPropMutex.Lock()
|
||||
backend.clients = append(backend.clients, advertisedConn)
|
||||
backend.arrayPropMutex.Unlock()
|
||||
|
||||
cleanupJob := func() {
|
||||
defer backend.arrayPropMutex.Unlock()
|
||||
err := sourceConn.Close()
|
||||
|
||||
if err != nil {
|
||||
log.Warnf("failed to close source connection: %s", err.Error())
|
||||
}
|
||||
|
||||
err = forwardedConn.Close()
|
||||
|
||||
if err != nil {
|
||||
log.Warnf("failed to close forwarded/proxied connection: %s", err.Error())
|
||||
}
|
||||
|
||||
backend.arrayPropMutex.Lock()
|
||||
|
||||
for clientIndex, clientInstance := range backend.clients {
|
||||
// Check if memory addresses are equal for the pointer
|
||||
if clientInstance == advertisedConn {
|
||||
// Splice out the clientInstance by clientIndex
|
||||
|
||||
// TODO: change approach. It works but it's a bit wonky imho
|
||||
// I asked AI to do this as it's a relatively simple task and I forgot how to do this effectively
|
||||
backend.clients = append(backend.clients[:clientIndex], backend.clients[clientIndex+1:]...)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
log.Warn("failed to delete client from clients metadata: couldn't find client in the array")
|
||||
}
|
||||
|
||||
sourceBuffer := make([]byte, 65535)
|
||||
forwardedBuffer := make([]byte, 65535)
|
||||
|
||||
go func() {
|
||||
defer cleanupJob()
|
||||
|
||||
for {
|
||||
len, err := forwardedConn.Read(forwardedBuffer)
|
||||
if err != nil {
|
||||
log.Errorf("failed to read from forwarded connection: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
_, err = sourceConn.Write(forwardedBuffer[:len])
|
||||
if err != nil {
|
||||
log.Errorf("failed to write to source connection: %s", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer cleanupJob()
|
||||
|
||||
for {
|
||||
len, err := sourceConn.Read(sourceBuffer)
|
||||
if err != nil && err.Error() != "EOF" && strings.HasSuffix(err.Error(), "use of closed network connection") {
|
||||
log.Errorf("failed to read from source connection: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
_, err = forwardedConn.Write(sourceBuffer[:len])
|
||||
if err != nil && err.Error() != "EOF" && strings.HasSuffix(err.Error(), "use of closed network connection") {
|
||||
log.Errorf("failed to write to forwarded connection: %s", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
backend.arrayPropMutex.Lock()
|
||||
backend.proxies = append(backend.proxies, listenerObject)
|
||||
backend.arrayPropMutex.Unlock()
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (backend *SSHBackend) StopProxy(command *commonbackend.RemoveProxy) (bool, error) {
|
||||
defer backend.arrayPropMutex.Unlock()
|
||||
backend.arrayPropMutex.Lock()
|
||||
|
||||
for proxyIndex, proxy := range backend.proxies {
|
||||
// Check if memory addresses are equal for the pointer
|
||||
if command.SourceIP == proxy.SourceIP && command.SourcePort == proxy.SourcePort && command.DestPort == proxy.DestPort && command.Protocol == proxy.Protocol {
|
||||
log.Debug("found proxy in StopProxy. shutting down listeners")
|
||||
|
||||
for _, listener := range proxy.Listeners {
|
||||
err := listener.Close()
|
||||
|
||||
if err != nil {
|
||||
log.Warnf("failed to stop listener in StopProxy: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Splice out the proxy instance by proxyIndex
|
||||
|
||||
// TODO: change approach. It works but it's a bit wonky imho
|
||||
// I asked AI to do this as it's a relatively simple task and I forgot how to do this effectively
|
||||
backend.proxies = append(backend.proxies[:proxyIndex], backend.proxies[proxyIndex+1:]...)
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, fmt.Errorf("could not find the proxy")
|
||||
}
|
||||
|
||||
func (backend *SSHBackend) GetAllClientConnections() []*commonbackend.ProxyClientConnection {
|
||||
defer backend.arrayPropMutex.Unlock()
|
||||
backend.arrayPropMutex.Lock()
|
||||
|
||||
return backend.clients
|
||||
}
|
||||
|
||||
func (backend *SSHBackend) CheckParametersForConnections(clientParameters *commonbackend.CheckClientParameters) *commonbackend.CheckParametersResponse {
|
||||
if clientParameters.Protocol != "tcp" {
|
||||
return &commonbackend.CheckParametersResponse{
|
||||
IsValid: false,
|
||||
Message: "Only TCP is supported for SSH",
|
||||
}
|
||||
}
|
||||
|
||||
return &commonbackend.CheckParametersResponse{
|
||||
IsValid: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (backend *SSHBackend) CheckParametersForBackend(arguments []byte) *commonbackend.CheckParametersResponse {
|
||||
var backendData SSHBackendData
|
||||
|
||||
if err := json.Unmarshal(arguments, &backendData); err != nil {
|
||||
return &commonbackend.CheckParametersResponse{
|
||||
IsValid: false,
|
||||
Message: fmt.Sprintf("could not read json: %s", err.Error()),
|
||||
}
|
||||
}
|
||||
|
||||
return &commonbackend.CheckParametersResponse{
|
||||
IsValid: true,
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
logLevel := os.Getenv("HERMES_LOG_LEVEL")
|
||||
|
||||
if logLevel != "" {
|
||||
switch logLevel {
|
||||
case "debug":
|
||||
log.SetLevel(log.DebugLevel)
|
||||
|
||||
case "info":
|
||||
log.SetLevel(log.InfoLevel)
|
||||
|
||||
case "warn":
|
||||
log.SetLevel(log.WarnLevel)
|
||||
|
||||
case "error":
|
||||
log.SetLevel(log.ErrorLevel)
|
||||
|
||||
case "fatal":
|
||||
log.SetLevel(log.FatalLevel)
|
||||
}
|
||||
}
|
||||
|
||||
backend := &SSHBackend{}
|
||||
|
||||
application := backendutil.NewHelper(backend)
|
||||
err := application.Start()
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("failed execution in application: %s", err.Error())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue