From 77f253c748e9d808bbcdb62dbff9a8efd7ad5b0b Mon Sep 17 00:00:00 2001 From: dushixiang Date: Sun, 13 Feb 2022 13:32:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dsshd=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/app/app.go | 5 +++++ server/sshd/sshd.go | 15 ++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/server/app/app.go b/server/app/app.go index ca5a555..b4b732d 100644 --- a/server/app/app.go +++ b/server/app/app.go @@ -8,6 +8,7 @@ import ( "next-terminal/server/config" "next-terminal/server/constant" "next-terminal/server/service" + "next-terminal/server/sshd" "github.com/labstack/echo/v4" ) @@ -110,6 +111,10 @@ func Run() error { return _cli.ChangeEncryptionKey(config.GlobalCfg.EncryptionKey, config.GlobalCfg.NewEncryptionKey) } + if config.GlobalCfg.Sshd.Enable { + go sshd.Sshd.Serve() + } + if config.GlobalCfg.Server.Cert != "" && config.GlobalCfg.Server.Key != "" { return app.Server.StartTLS(config.GlobalCfg.Server.Addr, config.GlobalCfg.Server.Cert, config.GlobalCfg.Server.Key) } else { diff --git a/server/sshd/sshd.go b/server/sshd/sshd.go index 83a4e6d..77f7a51 100644 --- a/server/sshd/sshd.go +++ b/server/sshd/sshd.go @@ -20,19 +20,20 @@ import ( "gorm.io/gorm" ) -type Sshd struct { +var Sshd *sshd + +type sshd struct { gui *Gui } func init() { gui := &Gui{} - sshd := &Sshd{ + Sshd = &sshd{ gui: gui, } - go sshd.Serve() } -func (sshd Sshd) passwordAuth(ctx ssh.Context, pass string) bool { +func (sshd sshd) passwordAuth(ctx ssh.Context, pass string) bool { username := ctx.User() remoteAddr := strings.Split(ctx.RemoteAddr().String(), ":")[0] user, err := repository.UserRepository.FindByUsername(context.TODO(), username) @@ -51,7 +52,7 @@ func (sshd Sshd) passwordAuth(ctx ssh.Context, pass string) bool { return true } -func (sshd Sshd) connCallback(ctx ssh.Context, conn net.Conn) net.Conn { +func (sshd sshd) connCallback(ctx ssh.Context, conn net.Conn) net.Conn { securities := security.GlobalSecurityManager.Values() if len(securities) == 0 { return conn @@ -100,7 +101,7 @@ func (sshd Sshd) connCallback(ctx ssh.Context, conn net.Conn) net.Conn { return conn } -func (sshd Sshd) sessionHandler(sess *ssh.Session) { +func (sshd sshd) sessionHandler(sess *ssh.Session) { defer func() { _ = (*sess).Close() }() @@ -128,7 +129,7 @@ func (sshd Sshd) sessionHandler(sess *ssh.Session) { } } -func (sshd Sshd) Serve() { +func (sshd sshd) Serve() { ssh.Handle(func(s ssh.Session) { _, _ = io.WriteString(s, fmt.Sprintf(constant.Banner, constant.Version)) sshd.sessionHandler(&s)