From 93f2f9cbee9c0253763309e6a3ce69417c688ad2 Mon Sep 17 00:00:00 2001 From: imterah Date: Mon, 6 Jan 2025 00:09:14 -0500 Subject: [PATCH] fix: Adds missing backend status command implementations. --- backend/backendutil/application.go | 36 ++++++++++++++++++++++++++++++ backend/backendutil/structure.go | 1 + backend/dummybackend/main.go | 4 ++++ backend/sshbackend/main.go | 4 ++++ 4 files changed, 45 insertions(+) diff --git a/backend/backendutil/application.go b/backend/backendutil/application.go index 5b99b2d..ccb21f2 100644 --- a/backend/backendutil/application.go +++ b/backend/backendutil/application.go @@ -72,6 +72,42 @@ func (helper *BackendApplicationHelper) Start() error { continue } + helper.socket.Write(responseMarshalled) + case "backendStatusRequest": + _, ok := commandRaw.(*commonbackend.BackendStatusRequest) + + if !ok { + return fmt.Errorf("failed to typecast") + } + + ok, err := helper.Backend.GetBackendStatus() + + 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": _, ok := commandRaw.(*commonbackend.Stop) diff --git a/backend/backendutil/structure.go b/backend/backendutil/structure.go index 3b97466..0eb7116 100644 --- a/backend/backendutil/structure.go +++ b/backend/backendutil/structure.go @@ -5,6 +5,7 @@ import "git.terah.dev/imterah/hermes/backend/commonbackend" type BackendInterface interface { StartBackend(arguments []byte) (bool, error) StopBackend() (bool, error) + GetBackendStatus() (bool, error) StartProxy(command *commonbackend.AddProxy) (bool, error) StopProxy(command *commonbackend.RemoveProxy) (bool, error) GetAllClientConnections() []*commonbackend.ProxyClientConnection diff --git a/backend/dummybackend/main.go b/backend/dummybackend/main.go index 92d88f5..f28615c 100644 --- a/backend/dummybackend/main.go +++ b/backend/dummybackend/main.go @@ -19,6 +19,10 @@ func (backend *DummyBackend) StopBackend() (bool, error) { return true, nil } +func (backend *DummyBackend) GetBackendStatus() (bool, error) { + return true, nil +} + func (backend *DummyBackend) StartProxy(command *commonbackend.AddProxy) (bool, error) { return true, nil } diff --git a/backend/sshbackend/main.go b/backend/sshbackend/main.go index e96ed5e..1bf35e7 100644 --- a/backend/sshbackend/main.go +++ b/backend/sshbackend/main.go @@ -96,6 +96,10 @@ func (backend *SSHBackend) StopBackend() (bool, error) { return true, nil } +func (backend *SSHBackend) GetBackendStatus() (bool, error) { + return backend.conn != nil, nil +} + func (backend *SSHBackend) StartProxy(command *commonbackend.AddProxy) (bool, error) { listenerObject := &SSHListener{ SourceIP: command.SourceIP,