mirror of
https://github.com/veops/oneterm.git
synced 2025-10-04 23:12:45 +08:00
feat: add ssh server
This commit is contained in:
47
backend/pkg/proto/ssh/handler/sider.go
Normal file
47
backend/pkg/proto/ssh/handler/sider.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
gossh "github.com/gliderlabs/ssh"
|
||||
|
||||
"github.com/veops/oneterm/pkg/logger"
|
||||
"github.com/veops/oneterm/pkg/proto/ssh/config"
|
||||
)
|
||||
|
||||
func RegisterMonitorSession(sessionId string, sess gossh.Session) {
|
||||
_, ok := config.TotalHostSession.Load(sessionId)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
config.TotalMonitors.LoadOrStore(sessionId, sess)
|
||||
|
||||
if _, ok := config.TotalMonitors.Load(sessionId); !ok {
|
||||
config.TotalMonitors.Store(sessionId, sess)
|
||||
}
|
||||
|
||||
<-sess.Context().Done()
|
||||
DeleteMonitorSession(sessionId)
|
||||
}
|
||||
|
||||
func DeleteMonitorSession(sessionId string) {
|
||||
config.TotalMonitors.Delete(sessionId)
|
||||
}
|
||||
|
||||
func getMonitorSession(sessionId string) gossh.Session {
|
||||
if v, ok := config.TotalMonitors.Load(sessionId); ok {
|
||||
return v.(gossh.Session)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Monitor(sessionId string, p []byte) {
|
||||
if s := getMonitorSession(sessionId); s != nil {
|
||||
_, err := s.Write(p)
|
||||
if err != nil {
|
||||
logger.L.Error(fmt.Sprintf("moninor session %s failed: %s", sessionId, err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user