修复sshd无法启动的问题

This commit is contained in:
dushixiang 2022-02-13 13:32:22 +08:00
parent edcec79fd0
commit 77f253c748
2 changed files with 13 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import (
"next-terminal/server/config" "next-terminal/server/config"
"next-terminal/server/constant" "next-terminal/server/constant"
"next-terminal/server/service" "next-terminal/server/service"
"next-terminal/server/sshd"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
@ -110,6 +111,10 @@ func Run() error {
return _cli.ChangeEncryptionKey(config.GlobalCfg.EncryptionKey, config.GlobalCfg.NewEncryptionKey) 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 != "" { 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) return app.Server.StartTLS(config.GlobalCfg.Server.Addr, config.GlobalCfg.Server.Cert, config.GlobalCfg.Server.Key)
} else { } else {

View File

@ -20,19 +20,20 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
type Sshd struct { var Sshd *sshd
type sshd struct {
gui *Gui gui *Gui
} }
func init() { func init() {
gui := &Gui{} gui := &Gui{}
sshd := &Sshd{ Sshd = &sshd{
gui: gui, 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() username := ctx.User()
remoteAddr := strings.Split(ctx.RemoteAddr().String(), ":")[0] remoteAddr := strings.Split(ctx.RemoteAddr().String(), ":")[0]
user, err := repository.UserRepository.FindByUsername(context.TODO(), username) user, err := repository.UserRepository.FindByUsername(context.TODO(), username)
@ -51,7 +52,7 @@ func (sshd Sshd) passwordAuth(ctx ssh.Context, pass string) bool {
return true 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() securities := security.GlobalSecurityManager.Values()
if len(securities) == 0 { if len(securities) == 0 {
return conn return conn
@ -100,7 +101,7 @@ func (sshd Sshd) connCallback(ctx ssh.Context, conn net.Conn) net.Conn {
return conn return conn
} }
func (sshd Sshd) sessionHandler(sess *ssh.Session) { func (sshd sshd) sessionHandler(sess *ssh.Session) {
defer func() { defer func() {
_ = (*sess).Close() _ = (*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) { ssh.Handle(func(s ssh.Session) {
_, _ = io.WriteString(s, fmt.Sprintf(constant.Banner, constant.Version)) _, _ = io.WriteString(s, fmt.Sprintf(constant.Banner, constant.Version))
sshd.sessionHandler(&s) sshd.sessionHandler(&s)