use net.Conn instead of ssh.Conn to close connections

This commit is contained in:
Nicola Murino 2019-09-11 09:41:46 +02:00
parent c1effdf701
commit 784f75f45b
4 changed files with 6 additions and 9 deletions

View file

@ -114,7 +114,7 @@ The `sftpgo` configuration file contains the following sections:
- **"sftpd"**, the configuration for the SFTP server
- `bind_port`, integer. The port used for serving SFTP requests. Default: 2022
- `bind_address`, string. Leave blank to listen on all available network interfaces. Default: ""
- `idle_timeout`, integer. Time in minutes after which an idle client will be disconnected. Default: 15
- `idle_timeout`, integer. Time in minutes after which an idle client will be disconnected. 0 menas disabled. Default: 15
- `max_auth_tries` integer. Maximum number of authentication attempts permitted per connection. If set to a negative number, the number of attempts are unlimited. If set to zero, the number of attempts are limited to 6.
- `umask`, string. Umask for the new files and directories. This setting has no effect on Windows. Default: "0022"
- `banner`, string. Identification string used by the server. Default "SFTPGo"

View file

@ -16,7 +16,6 @@ import (
"github.com/drakkan/sftpgo/dataprovider"
"github.com/drakkan/sftpgo/logger"
"golang.org/x/crypto/ssh"
"github.com/pkg/sftp"
)
@ -37,7 +36,7 @@ type Connection struct {
lastActivity time.Time
protocol string
lock *sync.Mutex
sshConn *ssh.ServerConn
netConn net.Conn
}
// Log outputs a log entry to the configured logger

View file

@ -197,7 +197,6 @@ func (c Configuration) configureLoginBanner(serverConfig *ssh.ServerConfig, conf
// AcceptInboundConnection handles an inbound connection to the server instance and determines if the request should be served or not.
func (c Configuration) AcceptInboundConnection(conn net.Conn, config *ssh.ServerConfig) {
defer conn.Close()
// Before beginning a handshake must be performed on the incoming net.Conn
sconn, chans, reqs, err := ssh.NewServerConn(conn, config)
@ -205,7 +204,6 @@ func (c Configuration) AcceptInboundConnection(conn net.Conn, config *ssh.Server
logger.Warn(logSender, "", "failed to accept an incoming connection: %v", err)
return
}
defer sconn.Close()
logger.Debug(logSender, "", "accepted inbound connection, ip: %v", conn.RemoteAddr().String())
@ -230,7 +228,7 @@ func (c Configuration) AcceptInboundConnection(conn net.Conn, config *ssh.Server
StartTime: time.Now(),
lastActivity: time.Now(),
lock: new(sync.Mutex),
sshConn: sconn,
netConn: conn,
}
connection.Log(logger.LevelInfo, logSender, "User id: %d, logged in with: %#v, username: %#v, home_dir: %#v",
user.ID, loginType, user.Username, user.HomeDir)

View file

@ -173,8 +173,8 @@ func CloseActiveConnection(connectionID string) bool {
defer mutex.RUnlock()
for _, c := range openConnections {
if c.ID == connectionID {
c.Log(logger.LevelDebug, logSender, "closing connection")
c.sshConn.Close()
err := c.netConn.Close()
c.Log(logger.LevelDebug, logSender, "close connection requested, err: %v", err)
result = true
break
}
@ -255,7 +255,7 @@ func CheckIdleConnections() {
}
if idleTime > idleTimeout {
c.Log(logger.LevelInfo, logSender, "close idle connection, idle time: %v", idleTime)
err := c.sshConn.Close()
err := c.netConn.Close()
if err != nil {
c.Log(logger.LevelWarn, logSender, "idle connection close failed: %v", err)
}