Skip to content

Commit

Permalink
sanitize payload
Browse files Browse the repository at this point in the history
  • Loading branch information
glaslos committed Jul 15, 2022
1 parent 7236d0c commit b1cf38b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
10 changes: 10 additions & 0 deletions glutton.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/kung-foo/freki"
Expand All @@ -31,6 +32,7 @@ type Glutton struct {
sshProxy *sshProxy
ctx context.Context
cancel context.CancelFunc
publicAddrs []net.IP
}

func (g *Glutton) initConfig() error {
Expand Down Expand Up @@ -90,6 +92,11 @@ func (g *Glutton) Init() error {
return err
}

g.publicAddrs, err = getNonLoopbackIPs(viper.GetString("interface"))
if err != nil {
return err
}

// Initiating glutton server
g.Processor.AddServer(freki.NewUserConnServer(gluttonServerPort))
// Initiating log producers
Expand Down Expand Up @@ -260,6 +267,9 @@ func (g *Glutton) MetadataByConnection(conn net.Conn) (*freki.Metadata, error) {

func (g *Glutton) Produce(conn net.Conn, md *freki.Metadata, payload []byte) error {
if g.Producer != nil {
for _, ip := range g.publicAddrs {
payload = []byte(strings.ReplaceAll(string(payload), ip.String(), "1.2.3.4"))
}
return g.Producer.Log(conn, md, payload)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module github.com/mushorg/glutton

require (
github.com/fw42/go-hpfeeds v0.0.0-20130602172243-651ce0355974
github.com/google/gopacket v1.1.19
github.com/jart/gosip v0.0.0-20200629215808-4e7924e19438
github.com/kung-foo/freki v1.1.0
github.com/lunixbochs/vtclean v1.0.0
Expand All @@ -23,7 +24,6 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
Expand Down
28 changes: 28 additions & 0 deletions system.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"errors"
"fmt"
"log"
"net"
"os"
"os/exec"
"runtime"
"strings"
"time"

"github.com/google/gopacket/pcap"
)

func countOpenFiles() (int, error) {
Expand Down Expand Up @@ -54,3 +57,28 @@ func isCommandAvailable(name string) bool {
}
return true
}

func getNonLoopbackIPs(ifaceName string) ([]net.IP, error) {
nonLoopback := []net.IP{}

ifs, err := pcap.FindAllDevs()
if err != nil {
return nonLoopback, err
}

for _, iface := range ifs {
if strings.EqualFold(iface.Name, ifaceName) {
for _, addr := range iface.Addresses {
if !addr.IP.IsLoopback() && addr.IP.To4() != nil {
nonLoopback = append(nonLoopback, addr.IP)
}
}
}
}

if len(nonLoopback) == 0 {
return nonLoopback, fmt.Errorf("unable to find any non-loopback addresses for: %s", ifaceName)
}

return nonLoopback, nil
}

0 comments on commit b1cf38b

Please sign in to comment.