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 - **"sftpd"**, the configuration for the SFTP server
- `bind_port`, integer. The port used for serving SFTP requests. Default: 2022 - `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: "" - `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. - `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" - `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" - `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/dataprovider"
"github.com/drakkan/sftpgo/logger" "github.com/drakkan/sftpgo/logger"
"golang.org/x/crypto/ssh"
"github.com/pkg/sftp" "github.com/pkg/sftp"
) )
@ -37,7 +36,7 @@ type Connection struct {
lastActivity time.Time lastActivity time.Time
protocol string protocol string
lock *sync.Mutex lock *sync.Mutex
sshConn *ssh.ServerConn netConn net.Conn
} }
// Log outputs a log entry to the configured logger // 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. // 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) { 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 // Before beginning a handshake must be performed on the incoming net.Conn
sconn, chans, reqs, err := ssh.NewServerConn(conn, config) 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) logger.Warn(logSender, "", "failed to accept an incoming connection: %v", err)
return return
} }
defer sconn.Close()
logger.Debug(logSender, "", "accepted inbound connection, ip: %v", conn.RemoteAddr().String()) 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(), StartTime: time.Now(),
lastActivity: time.Now(), lastActivity: time.Now(),
lock: new(sync.Mutex), 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", connection.Log(logger.LevelInfo, logSender, "User id: %d, logged in with: %#v, username: %#v, home_dir: %#v",
user.ID, loginType, user.Username, user.HomeDir) user.ID, loginType, user.Username, user.HomeDir)

View file

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