diff options
| author | Paul Bütow <pbuetow@mimecast.com> | 2020-01-20 18:41:05 +0000 |
|---|---|---|
| committer | Paul Bütow <pbuetow@mimecast.com> | 2020-01-21 14:35:23 +0000 |
| commit | c128865c4c7411c29a59fca9a3a2f95537686d7b (patch) | |
| tree | 193bccc70d942c8b70cc93fae2670263701e43aa /server/handlers/controlhandler.go | |
| parent | 3755a9911ecb05886577095f2b8cc8b9e4066a3a (diff) | |
Move commands to cmd/ and move internal dependencies to internal/
Diffstat (limited to 'server/handlers/controlhandler.go')
| -rw-r--r-- | server/handlers/controlhandler.go | 105 |
1 files changed, 0 insertions, 105 deletions
diff --git a/server/handlers/controlhandler.go b/server/handlers/controlhandler.go deleted file mode 100644 index c09eb52..0000000 --- a/server/handlers/controlhandler.go +++ /dev/null @@ -1,105 +0,0 @@ -package handlers - -import ( - "dtail/logger" - "dtail/server/user" - "fmt" - "io" - "os" - "strings" -) - -// ControlHandler is used for control functions and health monitoring. -type ControlHandler struct { - serverMessages chan string - pong chan struct{} - stop chan struct{} - payload []byte - hostname string - user *user.User -} - -// NewControlHandler returns a new control handler. -func NewControlHandler(user *user.User) *ControlHandler { - logger.Debug(user, "Creating control handler") - - h := ControlHandler{ - serverMessages: make(chan string, 10), - pong: make(chan struct{}, 10), - stop: make(chan struct{}), - user: user, - } - - fqdn, err := os.Hostname() - if err != nil { - logger.FatalExit(err) - } - - s := strings.Split(fqdn, ".") - h.hostname = s[0] - return &h -} - -// Read is to send data to the client via the Reader interface. -func (h *ControlHandler) Read(p []byte) (n int, err error) { - for { - select { - case message := <-h.serverMessages: - wholePayload := []byte(fmt.Sprintf("SERVER|%s|%s\n", h.hostname, message)) - n = copy(p, wholePayload) - return - case <-h.pong: - logger.Info(h.user, "Sending pong") - n = copy(p, []byte(".pong\n")) - return - case <-h.stop: - return 0, io.EOF - } - } -} - -// Write is to read data to the client via the Writer interface. -func (h *ControlHandler) Write(p []byte) (n int, err error) { - for _, c := range p { - switch c { - case ';': - wholePayload := strings.TrimSpace(string(h.payload)) - h.handleCommand(wholePayload) - h.payload = nil - - default: - h.payload = append(h.payload, c) - } - } - - n = len(p) - return -} - -// Close the control handler. -func (h *ControlHandler) Close() { - close(h.stop) -} - -// Wait returns the handler stop channel. -func (h *ControlHandler) Wait() <-chan struct{} { - return h.stop -} - -func (h *ControlHandler) handleCommand(command string) { - logger.Info(h.user, command) - s := strings.Split(command, " ") - logger.Debug(h.user, "Receiving command", command, s) - - switch s[0] { - case "health": - h.serverMessages <- "OK: DTail SSH Server seems fine" - h.serverMessages <- "done;" - case "ping": - h.pong <- struct{}{} - case "debug": - h.serverMessages <- logger.Debug(h.user, "Receiving debug command", command, s) - default: - h.serverMessages <- logger.Warn(h.user, "Received unknown command", command, s) - } -} |
