mirror of
https://github.com/drakkan/sftpgo.git
synced 2024-11-25 09:00:27 +00:00
remove the use of some unnecessary pointers
Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
parent
ffe7f7ff16
commit
d73be7aee5
12 changed files with 65 additions and 65 deletions
|
@ -222,7 +222,7 @@ func GetDefenderBanTime(ip string) (*time.Time, error) {
|
|||
}
|
||||
|
||||
// GetDefenderHosts returns hosts that are banned or for which some violations have been detected
|
||||
func GetDefenderHosts() ([]*dataprovider.DefenderEntry, error) {
|
||||
func GetDefenderHosts() ([]dataprovider.DefenderEntry, error) {
|
||||
if Config.defender == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -231,9 +231,9 @@ func GetDefenderHosts() ([]*dataprovider.DefenderEntry, error) {
|
|||
}
|
||||
|
||||
// GetDefenderHost returns a defender host by ip, if any
|
||||
func GetDefenderHost(ip string) (*dataprovider.DefenderEntry, error) {
|
||||
func GetDefenderHost(ip string) (dataprovider.DefenderEntry, error) {
|
||||
if Config.defender == nil {
|
||||
return nil, errors.New("defender is disabled")
|
||||
return dataprovider.DefenderEntry{}, errors.New("defender is disabled")
|
||||
}
|
||||
|
||||
return Config.defender.GetHost(ip)
|
||||
|
@ -873,13 +873,13 @@ func (conns *ActiveConnections) IsNewConnectionAllowed(ipAddr string) bool {
|
|||
}
|
||||
|
||||
// GetStats returns stats for active connections
|
||||
func (conns *ActiveConnections) GetStats() []*ConnectionStatus {
|
||||
func (conns *ActiveConnections) GetStats() []ConnectionStatus {
|
||||
conns.RLock()
|
||||
defer conns.RUnlock()
|
||||
|
||||
stats := make([]*ConnectionStatus, 0, len(conns.connections))
|
||||
stats := make([]ConnectionStatus, 0, len(conns.connections))
|
||||
for _, c := range conns.connections {
|
||||
stat := &ConnectionStatus{
|
||||
stat := ConnectionStatus{
|
||||
Username: c.GetUsername(),
|
||||
ConnectionID: c.GetID(),
|
||||
ClientVersion: c.GetClientVersion(),
|
||||
|
|
|
@ -38,8 +38,8 @@ var (
|
|||
|
||||
// Defender defines the interface that a defender must implements
|
||||
type Defender interface {
|
||||
GetHosts() ([]*dataprovider.DefenderEntry, error)
|
||||
GetHost(ip string) (*dataprovider.DefenderEntry, error)
|
||||
GetHosts() ([]dataprovider.DefenderEntry, error)
|
||||
GetHost(ip string) (dataprovider.DefenderEntry, error)
|
||||
AddEvent(ip string, event HostEvent)
|
||||
IsBanned(ip string) bool
|
||||
GetBanTime(ip string) (*time.Time, error)
|
||||
|
|
|
@ -33,12 +33,12 @@ func newDBDefender(config *DefenderConfig) (Defender, error) {
|
|||
}
|
||||
|
||||
// GetHosts returns hosts that are banned or for which some violations have been detected
|
||||
func (d *dbDefender) GetHosts() ([]*dataprovider.DefenderEntry, error) {
|
||||
func (d *dbDefender) GetHosts() ([]dataprovider.DefenderEntry, error) {
|
||||
return dataprovider.GetDefenderHosts(d.getStartObservationTime(), d.config.EntriesHardLimit)
|
||||
}
|
||||
|
||||
// GetHost returns a defender host by ip, if any
|
||||
func (d *dbDefender) GetHost(ip string) (*dataprovider.DefenderEntry, error) {
|
||||
func (d *dbDefender) GetHost(ip string) (dataprovider.DefenderEntry, error) {
|
||||
return dataprovider.GetDefenderHostByIP(ip, d.getStartObservationTime())
|
||||
}
|
||||
|
||||
|
|
|
@ -39,14 +39,14 @@ func newInMemoryDefender(config *DefenderConfig) (Defender, error) {
|
|||
}
|
||||
|
||||
// GetHosts returns hosts that are banned or for which some violations have been detected
|
||||
func (d *memoryDefender) GetHosts() ([]*dataprovider.DefenderEntry, error) {
|
||||
func (d *memoryDefender) GetHosts() ([]dataprovider.DefenderEntry, error) {
|
||||
d.RLock()
|
||||
defer d.RUnlock()
|
||||
|
||||
var result []*dataprovider.DefenderEntry
|
||||
var result []dataprovider.DefenderEntry
|
||||
for k, v := range d.banned {
|
||||
if v.After(time.Now()) {
|
||||
result = append(result, &dataprovider.DefenderEntry{
|
||||
result = append(result, dataprovider.DefenderEntry{
|
||||
IP: k,
|
||||
BanTime: v,
|
||||
})
|
||||
|
@ -60,7 +60,7 @@ func (d *memoryDefender) GetHosts() ([]*dataprovider.DefenderEntry, error) {
|
|||
}
|
||||
}
|
||||
if score > 0 {
|
||||
result = append(result, &dataprovider.DefenderEntry{
|
||||
result = append(result, dataprovider.DefenderEntry{
|
||||
IP: k,
|
||||
Score: score,
|
||||
})
|
||||
|
@ -71,13 +71,13 @@ func (d *memoryDefender) GetHosts() ([]*dataprovider.DefenderEntry, error) {
|
|||
}
|
||||
|
||||
// GetHost returns a defender host by ip, if any
|
||||
func (d *memoryDefender) GetHost(ip string) (*dataprovider.DefenderEntry, error) {
|
||||
func (d *memoryDefender) GetHost(ip string) (dataprovider.DefenderEntry, error) {
|
||||
d.RLock()
|
||||
defer d.RUnlock()
|
||||
|
||||
if banTime, ok := d.banned[ip]; ok {
|
||||
if banTime.After(time.Now()) {
|
||||
return &dataprovider.DefenderEntry{
|
||||
return dataprovider.DefenderEntry{
|
||||
IP: ip,
|
||||
BanTime: banTime,
|
||||
}, nil
|
||||
|
@ -92,14 +92,14 @@ func (d *memoryDefender) GetHost(ip string) (*dataprovider.DefenderEntry, error)
|
|||
}
|
||||
}
|
||||
if score > 0 {
|
||||
return &dataprovider.DefenderEntry{
|
||||
return dataprovider.DefenderEntry{
|
||||
IP: ip,
|
||||
Score: score,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, util.NewRecordNotFoundError("host not found")
|
||||
return dataprovider.DefenderEntry{}, util.NewRecordNotFoundError("host not found")
|
||||
}
|
||||
|
||||
// IsBanned returns true if the specified IP is banned
|
||||
|
|
|
@ -1365,16 +1365,16 @@ func (p *BoltProvider) updateShareLastUse(shareID string, numTokens int) error {
|
|||
})
|
||||
}
|
||||
|
||||
func (p *BoltProvider) getDefenderHosts(from int64, limit int) ([]*DefenderEntry, error) {
|
||||
func (p *BoltProvider) getDefenderHosts(from int64, limit int) ([]DefenderEntry, error) {
|
||||
return nil, ErrNotImplemented
|
||||
}
|
||||
|
||||
func (p *BoltProvider) getDefenderHostByIP(ip string, from int64) (*DefenderEntry, error) {
|
||||
return nil, ErrNotImplemented
|
||||
func (p *BoltProvider) getDefenderHostByIP(ip string, from int64) (DefenderEntry, error) {
|
||||
return DefenderEntry{}, ErrNotImplemented
|
||||
}
|
||||
|
||||
func (p *BoltProvider) isDefenderHostBanned(ip string) (*DefenderEntry, error) {
|
||||
return nil, ErrNotImplemented
|
||||
func (p *BoltProvider) isDefenderHostBanned(ip string) (DefenderEntry, error) {
|
||||
return DefenderEntry{}, ErrNotImplemented
|
||||
}
|
||||
|
||||
func (p *BoltProvider) updateDefenderBanTime(ip string, minutes int) error {
|
||||
|
|
|
@ -508,9 +508,9 @@ type Provider interface {
|
|||
getShares(limit int, offset int, order, username string) ([]Share, error)
|
||||
dumpShares() ([]Share, error)
|
||||
updateShareLastUse(shareID string, numTokens int) error
|
||||
getDefenderHosts(from int64, limit int) ([]*DefenderEntry, error)
|
||||
getDefenderHostByIP(ip string, from int64) (*DefenderEntry, error)
|
||||
isDefenderHostBanned(ip string) (*DefenderEntry, error)
|
||||
getDefenderHosts(from int64, limit int) ([]DefenderEntry, error)
|
||||
getDefenderHostByIP(ip string, from int64) (DefenderEntry, error)
|
||||
isDefenderHostBanned(ip string) (DefenderEntry, error)
|
||||
updateDefenderBanTime(ip string, minutes int) error
|
||||
deleteDefenderHost(ip string) error
|
||||
addDefenderEvent(ip string, score int) error
|
||||
|
@ -926,17 +926,17 @@ func CheckKeyboardInteractiveAuth(username, authHook string, client ssh.Keyboard
|
|||
}
|
||||
|
||||
// GetDefenderHosts returns hosts that are banned or for which some violations have been detected
|
||||
func GetDefenderHosts(from int64, limit int) ([]*DefenderEntry, error) {
|
||||
func GetDefenderHosts(from int64, limit int) ([]DefenderEntry, error) {
|
||||
return provider.getDefenderHosts(from, limit)
|
||||
}
|
||||
|
||||
// GetDefenderHostByIP returns a defender host by ip, if any
|
||||
func GetDefenderHostByIP(ip string, from int64) (*DefenderEntry, error) {
|
||||
func GetDefenderHostByIP(ip string, from int64) (DefenderEntry, error) {
|
||||
return provider.getDefenderHostByIP(ip, from)
|
||||
}
|
||||
|
||||
// IsDefenderHostBanned returns a defender entry and no error if the specified host is banned
|
||||
func IsDefenderHostBanned(ip string) (*DefenderEntry, error) {
|
||||
func IsDefenderHostBanned(ip string) (DefenderEntry, error) {
|
||||
return provider.isDefenderHostBanned(ip)
|
||||
}
|
||||
|
||||
|
@ -952,9 +952,9 @@ func DeleteDefenderHost(ip string) error {
|
|||
|
||||
// AddDefenderEvent adds an event for the given IP with the given score
|
||||
// and returns the host with the updated score
|
||||
func AddDefenderEvent(ip string, score int, from int64) (*DefenderEntry, error) {
|
||||
func AddDefenderEvent(ip string, score int, from int64) (DefenderEntry, error) {
|
||||
if err := provider.addDefenderEvent(ip, score); err != nil {
|
||||
return nil, err
|
||||
return DefenderEntry{}, err
|
||||
}
|
||||
return provider.getDefenderHostByIP(ip, from)
|
||||
}
|
||||
|
|
|
@ -1256,16 +1256,16 @@ func (p *MemoryProvider) updateShareLastUse(shareID string, numTokens int) error
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *MemoryProvider) getDefenderHosts(from int64, limit int) ([]*DefenderEntry, error) {
|
||||
func (p *MemoryProvider) getDefenderHosts(from int64, limit int) ([]DefenderEntry, error) {
|
||||
return nil, ErrNotImplemented
|
||||
}
|
||||
|
||||
func (p *MemoryProvider) getDefenderHostByIP(ip string, from int64) (*DefenderEntry, error) {
|
||||
return nil, ErrNotImplemented
|
||||
func (p *MemoryProvider) getDefenderHostByIP(ip string, from int64) (DefenderEntry, error) {
|
||||
return DefenderEntry{}, ErrNotImplemented
|
||||
}
|
||||
|
||||
func (p *MemoryProvider) isDefenderHostBanned(ip string) (*DefenderEntry, error) {
|
||||
return nil, ErrNotImplemented
|
||||
func (p *MemoryProvider) isDefenderHostBanned(ip string) (DefenderEntry, error) {
|
||||
return DefenderEntry{}, ErrNotImplemented
|
||||
}
|
||||
|
||||
func (p *MemoryProvider) updateDefenderBanTime(ip string, minutes int) error {
|
||||
|
|
|
@ -304,15 +304,15 @@ func (p *MySQLProvider) updateShareLastUse(shareID string, numTokens int) error
|
|||
return sqlCommonUpdateShareLastUse(shareID, numTokens, p.dbHandle)
|
||||
}
|
||||
|
||||
func (p *MySQLProvider) getDefenderHosts(from int64, limit int) ([]*DefenderEntry, error) {
|
||||
func (p *MySQLProvider) getDefenderHosts(from int64, limit int) ([]DefenderEntry, error) {
|
||||
return sqlCommonGetDefenderHosts(from, limit, p.dbHandle)
|
||||
}
|
||||
|
||||
func (p *MySQLProvider) getDefenderHostByIP(ip string, from int64) (*DefenderEntry, error) {
|
||||
func (p *MySQLProvider) getDefenderHostByIP(ip string, from int64) (DefenderEntry, error) {
|
||||
return sqlCommonGetDefenderHostByIP(ip, from, p.dbHandle)
|
||||
}
|
||||
|
||||
func (p *MySQLProvider) isDefenderHostBanned(ip string) (*DefenderEntry, error) {
|
||||
func (p *MySQLProvider) isDefenderHostBanned(ip string) (DefenderEntry, error) {
|
||||
return sqlCommonIsDefenderHostBanned(ip, p.dbHandle)
|
||||
}
|
||||
|
||||
|
|
|
@ -316,15 +316,15 @@ func (p *PGSQLProvider) updateShareLastUse(shareID string, numTokens int) error
|
|||
return sqlCommonUpdateShareLastUse(shareID, numTokens, p.dbHandle)
|
||||
}
|
||||
|
||||
func (p *PGSQLProvider) getDefenderHosts(from int64, limit int) ([]*DefenderEntry, error) {
|
||||
func (p *PGSQLProvider) getDefenderHosts(from int64, limit int) ([]DefenderEntry, error) {
|
||||
return sqlCommonGetDefenderHosts(from, limit, p.dbHandle)
|
||||
}
|
||||
|
||||
func (p *PGSQLProvider) getDefenderHostByIP(ip string, from int64) (*DefenderEntry, error) {
|
||||
func (p *PGSQLProvider) getDefenderHostByIP(ip string, from int64) (DefenderEntry, error) {
|
||||
return sqlCommonGetDefenderHostByIP(ip, from, p.dbHandle)
|
||||
}
|
||||
|
||||
func (p *PGSQLProvider) isDefenderHostBanned(ip string) (*DefenderEntry, error) {
|
||||
func (p *PGSQLProvider) isDefenderHostBanned(ip string) (DefenderEntry, error) {
|
||||
return sqlCommonIsDefenderHostBanned(ip, p.dbHandle)
|
||||
}
|
||||
|
||||
|
|
|
@ -970,8 +970,8 @@ func sqlCommonGetUsers(limit int, offset int, order string, dbHandle sqlQuerier)
|
|||
return getUsersWithVirtualFolders(ctx, users, dbHandle)
|
||||
}
|
||||
|
||||
func sqlCommonGetDefenderHosts(from int64, limit int, dbHandle sqlQuerier) ([]*DefenderEntry, error) {
|
||||
hosts := make([]*DefenderEntry, 0, 100)
|
||||
func sqlCommonGetDefenderHosts(from int64, limit int, dbHandle sqlQuerier) ([]DefenderEntry, error) {
|
||||
hosts := make([]DefenderEntry, 0, 100)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
||||
defer cancel()
|
||||
|
@ -1009,7 +1009,7 @@ func sqlCommonGetDefenderHosts(from int64, limit int, dbHandle sqlQuerier) ([]*D
|
|||
} else {
|
||||
host.BanTime = hostBanTime
|
||||
}
|
||||
hosts = append(hosts, &host)
|
||||
hosts = append(hosts, host)
|
||||
}
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
|
@ -1020,7 +1020,7 @@ func sqlCommonGetDefenderHosts(from int64, limit int, dbHandle sqlQuerier) ([]*D
|
|||
return getDefenderHostsWithScores(ctx, hosts, from, idForScores, dbHandle)
|
||||
}
|
||||
|
||||
func sqlCommonIsDefenderHostBanned(ip string, dbHandle sqlQuerier) (*DefenderEntry, error) {
|
||||
func sqlCommonIsDefenderHostBanned(ip string, dbHandle sqlQuerier) (DefenderEntry, error) {
|
||||
var host DefenderEntry
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
||||
|
@ -1030,7 +1030,7 @@ func sqlCommonIsDefenderHostBanned(ip string, dbHandle sqlQuerier) (*DefenderEnt
|
|||
stmt, err := dbHandle.PrepareContext(ctx, q)
|
||||
if err != nil {
|
||||
providerLog(logger.LevelError, "error preparing database query %#v: %v", q, err)
|
||||
return nil, err
|
||||
return host, err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
|
@ -1038,16 +1038,16 @@ func sqlCommonIsDefenderHostBanned(ip string, dbHandle sqlQuerier) (*DefenderEnt
|
|||
err = row.Scan(&host.ID)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, util.NewRecordNotFoundError("host not found")
|
||||
return host, util.NewRecordNotFoundError("host not found")
|
||||
}
|
||||
providerLog(logger.LevelError, "unable to check ban status for host %#v: %v", ip, err)
|
||||
return nil, err
|
||||
return host, err
|
||||
}
|
||||
|
||||
return &host, nil
|
||||
return host, nil
|
||||
}
|
||||
|
||||
func sqlCommonGetDefenderHostByIP(ip string, from int64, dbHandle sqlQuerier) (*DefenderEntry, error) {
|
||||
func sqlCommonGetDefenderHostByIP(ip string, from int64, dbHandle sqlQuerier) (DefenderEntry, error) {
|
||||
var host DefenderEntry
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
||||
|
@ -1057,7 +1057,7 @@ func sqlCommonGetDefenderHostByIP(ip string, from int64, dbHandle sqlQuerier) (*
|
|||
stmt, err := dbHandle.PrepareContext(ctx, q)
|
||||
if err != nil {
|
||||
providerLog(logger.LevelError, "error preparing database query %#v: %v", q, err)
|
||||
return nil, err
|
||||
return host, err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
|
@ -1066,25 +1066,25 @@ func sqlCommonGetDefenderHostByIP(ip string, from int64, dbHandle sqlQuerier) (*
|
|||
err = row.Scan(&host.ID, &host.IP, &banTime)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, util.NewRecordNotFoundError("host not found")
|
||||
return host, util.NewRecordNotFoundError("host not found")
|
||||
}
|
||||
providerLog(logger.LevelError, "unable to get host for ip %#v: %v", ip, err)
|
||||
return nil, err
|
||||
return host, err
|
||||
}
|
||||
if banTime.Valid && banTime.Int64 > 0 {
|
||||
hostBanTime := util.GetTimeFromMsecSinceEpoch(banTime.Int64)
|
||||
if !hostBanTime.IsZero() && hostBanTime.After(time.Now()) {
|
||||
host.BanTime = hostBanTime
|
||||
return &host, nil
|
||||
return host, nil
|
||||
}
|
||||
}
|
||||
|
||||
hosts, err := getDefenderHostsWithScores(ctx, []*DefenderEntry{&host}, from, []int64{host.ID}, dbHandle)
|
||||
hosts, err := getDefenderHostsWithScores(ctx, []DefenderEntry{host}, from, []int64{host.ID}, dbHandle)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return host, err
|
||||
}
|
||||
if len(hosts) == 0 {
|
||||
return nil, util.NewRecordNotFoundError("host not found")
|
||||
return host, util.NewRecordNotFoundError("host not found")
|
||||
}
|
||||
|
||||
return hosts[0], nil
|
||||
|
@ -1709,9 +1709,9 @@ func getUserWithVirtualFolders(ctx context.Context, user User, dbHandle sqlQueri
|
|||
return users[0], err
|
||||
}
|
||||
|
||||
func getDefenderHostsWithScores(ctx context.Context, hosts []*DefenderEntry, from int64, idForScores []int64,
|
||||
func getDefenderHostsWithScores(ctx context.Context, hosts []DefenderEntry, from int64, idForScores []int64,
|
||||
dbHandle sqlQuerier) (
|
||||
[]*DefenderEntry,
|
||||
[]DefenderEntry,
|
||||
error,
|
||||
) {
|
||||
if len(idForScores) == 0 {
|
||||
|
@ -1752,7 +1752,7 @@ func getDefenderHostsWithScores(ctx context.Context, hosts []*DefenderEntry, fro
|
|||
return hosts, err
|
||||
}
|
||||
|
||||
result := make([]*DefenderEntry, 0, len(hosts))
|
||||
result := make([]DefenderEntry, 0, len(hosts))
|
||||
|
||||
for idx := range hosts {
|
||||
hosts[idx].Score = hostsWithScores[hosts[idx].ID]
|
||||
|
|
|
@ -301,15 +301,15 @@ func (p *SQLiteProvider) updateShareLastUse(shareID string, numTokens int) error
|
|||
return sqlCommonUpdateShareLastUse(shareID, numTokens, p.dbHandle)
|
||||
}
|
||||
|
||||
func (p *SQLiteProvider) getDefenderHosts(from int64, limit int) ([]*DefenderEntry, error) {
|
||||
func (p *SQLiteProvider) getDefenderHosts(from int64, limit int) ([]DefenderEntry, error) {
|
||||
return sqlCommonGetDefenderHosts(from, limit, p.dbHandle)
|
||||
}
|
||||
|
||||
func (p *SQLiteProvider) getDefenderHostByIP(ip string, from int64) (*DefenderEntry, error) {
|
||||
func (p *SQLiteProvider) getDefenderHostByIP(ip string, from int64) (DefenderEntry, error) {
|
||||
return sqlCommonGetDefenderHostByIP(ip, from, p.dbHandle)
|
||||
}
|
||||
|
||||
func (p *SQLiteProvider) isDefenderHostBanned(ip string) (*DefenderEntry, error) {
|
||||
func (p *SQLiteProvider) isDefenderHostBanned(ip string) (DefenderEntry, error) {
|
||||
return sqlCommonIsDefenderHostBanned(ip, p.dbHandle)
|
||||
}
|
||||
|
||||
|
|
|
@ -135,7 +135,7 @@ type foldersPage struct {
|
|||
|
||||
type connectionsPage struct {
|
||||
basePage
|
||||
Connections []*common.ConnectionStatus
|
||||
Connections []common.ConnectionStatus
|
||||
}
|
||||
|
||||
type statusPage struct {
|
||||
|
|
Loading…
Reference in a new issue