Skip to content

Commit

Permalink
switch to slog
Browse files Browse the repository at this point in the history
  • Loading branch information
glaslos committed Feb 17, 2024
1 parent 0ff4d1b commit bf7c869
Show file tree
Hide file tree
Showing 21 changed files with 194 additions and 588 deletions.
29 changes: 14 additions & 15 deletions glutton.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"log/slog"
"net"
"os"
"path/filepath"
Expand All @@ -19,13 +20,12 @@ import (
"github.com/google/uuid"
"github.com/seud0nym/tproxy-go/tproxy"
"github.com/spf13/viper"
"go.uber.org/zap"
)

// Glutton struct
type Glutton struct {
id uuid.UUID
Logger *zap.Logger
Logger *slog.Logger
Server *Server
rules rules.Rules
Producer *producer.Producer
Expand All @@ -47,7 +47,7 @@ func (g *Glutton) initConfig() error {
viper.SetDefault("ports.glutton_server", 5000)
viper.SetDefault("max_tcp_payload", 4096)
viper.SetDefault("rules_path", "rules/rules.yaml")
g.Logger.Debug("configuration loaded successfully", zap.String("reporter", "glutton"))
g.Logger.Debug("configuration loaded successfully", slog.String("reporter", "glutton"))
return nil
}

Expand All @@ -66,7 +66,7 @@ func New(ctx context.Context) (*Glutton, error) {
g.Logger = producer.NewLogger(g.id.String())

// Loading the configuration
g.Logger.Info("Loading configurations from: config/config.yaml", zap.String("reporter", "glutton"))
g.Logger.Info("Loading configurations from: config/config.yaml", slog.String("reporter", "glutton"))
if err := g.initConfig(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -133,26 +133,26 @@ func (g *Glutton) udpListen() {
for {
n, srcAddr, dstAddr, err := tproxy.ReadFromUDP(g.Server.udpListener, buffer)
if err != nil {
g.Logger.Error("failed to read UDP packet", zap.Error(err))
g.Logger.Error("failed to read UDP packet", producer.ErrAttr(err))
}

rule, err := g.applyRules("udp", srcAddr, dstAddr)
if err != nil {
g.Logger.Error("failed to apply rules", zap.Error(err))
g.Logger.Error("failed to apply rules", producer.ErrAttr(err))
}
if rule == nil {
rule = &rules.Rule{Target: "udp"}
}
md, err := g.connTable.Register(srcAddr.IP.String(), strconv.Itoa(int(srcAddr.AddrPort().Port())), dstAddr.AddrPort().Port(), rule)
if err != nil {
g.Logger.Error("failed to register UDP packet", zap.Error(err))
g.Logger.Error("failed to register UDP packet", producer.ErrAttr(err))
}

if hfunc, ok := g.udpProtocolHandlers[rule.Target]; ok {
data := buffer[:n]
go func() {
if err := hfunc(g.ctx, srcAddr, dstAddr, data, md); err != nil {
g.Logger.Error("failed to handle UDP payload", zap.Error(err))
g.Logger.Error("failed to handle UDP payload", producer.ErrAttr(err))
}
}()
}
Expand Down Expand Up @@ -203,16 +203,16 @@ func (g *Glutton) Start() error {
return err
}

g.Logger.Debug("new connection", zap.String("addr", conn.LocalAddr().String()), zap.String("handler", rule.Target))
g.Logger.Debug("new connection", slog.String("addr", conn.LocalAddr().String()), slog.String("handler", rule.Target))

if err := g.UpdateConnectionTimeout(g.ctx, conn); err != nil {
g.Logger.Error("failed to set connection timeout", zap.Error(err))
g.Logger.Error("failed to set connection timeout", producer.ErrAttr(err))
}

if hfunc, ok := g.tcpProtocolHandlers[rule.Target]; ok {
go func() {
if err := hfunc(g.ctx, conn, md); err != nil {
g.Logger.Error("failed to handle TCP connection", zap.Error(err), zap.String("handler", rule.Target))
g.Logger.Error("failed to handle TCP connection", producer.ErrAttr(err), slog.String("handler", rule.Target))
}
}()
}
Expand Down Expand Up @@ -307,19 +307,18 @@ func (g *Glutton) ProduceUDP(handler string, srcAddr, dstAddr *net.UDPAddr, md c

// Shutdown the packet processor
func (g *Glutton) Shutdown() {
defer g.Logger.Sync()
g.cancel() // close all connection

g.Logger.Info("Shutting down listeners")
if err := g.Server.Shutdown(); err != nil {
g.Logger.Error("failed to shutdown server", zap.Error(err))
g.Logger.Error("failed to shutdown server", producer.ErrAttr(err))
}

if err := flushTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "tcp", uint32(g.Server.tcpPort)); err != nil {
g.Logger.Error("failed to drop tcp iptables", zap.Error(err))
g.Logger.Error("failed to drop tcp iptables", producer.ErrAttr(err))
}
if err := flushTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "udp", uint32(g.Server.udpPort)); err != nil {
g.Logger.Error("failed to drop udp iptables", zap.Error(err))
g.Logger.Error("failed to drop udp iptables", producer.ErrAttr(err))
}

g.Logger.Info("All done")
Expand Down
33 changes: 16 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,49 @@ go 1.21
require (
github.com/coreos/go-iptables v0.7.0
github.com/d1str0/hpfeeds v0.1.6
github.com/ghettovoice/gosip v0.0.0-20231005134608-3b981d26e5cc
github.com/ghettovoice/gosip v0.0.0-20231227123312-6b80e2d3e6f7
github.com/glaslos/lsof v0.0.0-20230723212405-b3baf9409e4b
github.com/google/gopacket v1.1.19
github.com/google/uuid v1.3.1
github.com/google/uuid v1.6.0
github.com/seud0nym/tproxy-go v0.0.0-20230917034616-0c6325a61e88
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.17.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jedib0t/go-pretty/v6 v6.4.8 // indirect
github.com/jedib0t/go-pretty/v6 v6.5.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tevino/abool v1.2.0 // indirect
github.com/x-cray/logrus-prefixed-formatter v0.5.2 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit bf7c869

Please sign in to comment.