Replace zap with slog

This commit is contained in:
2025-03-19 23:16:38 +01:00
parent 49553fa965
commit 46d9f4d64a
9 changed files with 70 additions and 91 deletions

View File

@@ -13,7 +13,7 @@ var mmdb []byte
func (s *HoneypotServer) LookupCountry(ip net.IP) string {
db, err := maxminddb.FromBytes(mmdb)
if err != nil {
s.Logger.Warnw("Error opening geoip database", "error", err)
s.Logger.Warn("Error opening geoip database", "error", err)
return "??"
}
@@ -25,7 +25,7 @@ func (s *HoneypotServer) LookupCountry(ip net.IP) string {
err = db.Lookup(ip, &record)
if err != nil {
s.Logger.Warnw("Error doing geoip lookup", "error", err)
s.Logger.Warn("Error doing geoip lookup", "error", err)
return "??"
}
if record.Country.ISOCode == "None" {

View File

@@ -3,6 +3,7 @@ package ssh
import (
"context"
"io"
"log/slog"
"net"
"os"
"time"
@@ -16,11 +17,10 @@ import (
"git.t-juice.club/torjus/apiary/models"
sshlib "github.com/gliderlabs/ssh"
"github.com/google/uuid"
"go.uber.org/zap"
)
type HoneypotServer struct {
Logger *zap.SugaredLogger
Logger *slog.Logger
sshServer *sshlib.Server
@@ -33,7 +33,7 @@ type HoneypotServer struct {
func NewHoneypotServer(cfg config.HoneypotConfig, store store.LoginAttemptStore) (*HoneypotServer, error) {
var hs HoneypotServer
hs.attemptStore = store
hs.Logger = zap.NewNop().Sugar()
hs.Logger = slog.New(slog.NewTextHandler(io.Discard, nil))
hs.sshServer = &sshlib.Server{
Addr: cfg.ListenAddr,
@@ -94,19 +94,19 @@ func (hs *HoneypotServer) passwordHandler(ctx sshlib.Context, password string) b
}
country := hs.LookupCountry(la.RemoteIP)
if utf8.RuneCountInString(country) > 2 {
hs.Logger.Warnw("Too many characters in country", "country", country, "runecount", utf8.RuneCountInString(country))
hs.Logger.Warn("Too many characters in country", "country", country, "runecount", utf8.RuneCountInString(country))
country = "??"
}
la.Country = country
hs.Logger.Infow("Login attempt",
hs.Logger.Info("Login attempt",
"remote_ip", la.RemoteIP.String(),
"username", la.Username,
"password", la.Password,
"country", la.Country)
if err := hs.attemptStore.AddAttempt(&la); err != nil {
hs.Logger.Warnw("Error adding attempt to store", "error", err)
hs.Logger.Warn("Error adding attempt to store", "error", err)
}
for _, cFunc := range hs.attemptsCallbacks {
@@ -117,7 +117,7 @@ func (hs *HoneypotServer) passwordHandler(ctx sshlib.Context, password string) b
}
func (s *HoneypotServer) connCallback(ctx sshlib.Context, conn net.Conn) net.Conn {
s.Logger.Debugw("Connection received.", "remote_addr", conn.RemoteAddr())
s.Logger.Debug("Connection received.", "remote_addr", conn.RemoteAddr())
throttledConn := newThrottledConn(conn)
throttledConn.SetSpeed(s.throttleSpeed)