add sshd listener

This commit is contained in:
ginuerzh
2022-01-26 15:53:33 +08:00
parent a134026e76
commit 04dfc8c4c3
39 changed files with 1101 additions and 848 deletions

View File

@ -3,6 +3,7 @@ package ssh
import (
"fmt"
"net"
"time"
auth_util "github.com/go-gost/gost/pkg/common/util/auth"
ssh_util "github.com/go-gost/gost/pkg/internal/util/ssh"
@ -29,13 +30,14 @@ type sshListener struct {
}
func NewListener(opts ...listener.Option) listener.Listener {
options := &listener.Options{}
options := listener.Options{}
for _, opt := range opts {
opt(options)
opt(&options)
}
return &sshListener{
addr: options.Addr,
logger: options.Logger,
addr: options.Addr,
logger: options.Logger,
options: options,
}
}
@ -96,6 +98,14 @@ func (l *sshListener) listenLoop() {
}
func (l *sshListener) serveConn(conn net.Conn) {
start := time.Now()
l.logger.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
defer func() {
l.logger.WithFields(map[string]interface{}{
"duration": time.Since(start),
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
}()
sc, chans, reqs, err := ssh.NewServerConn(conn, l.config)
if err != nil {
l.logger.Error(err)
@ -122,8 +132,9 @@ func (l *sshListener) serveConn(conn net.Conn) {
select {
case l.cqueue <- cc:
default:
cc.Close()
l.logger.Warnf("connection queue is full, client %s discarded", conn.RemoteAddr())
newChannel.Reject(ssh.ResourceShortage, "connection queue is full")
cc.Close()
}
default: