Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#166: Added support for the interface name in the config file #167

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ NAME := glutton
BUILDSTRING := $(shell git log --pretty=format:'%h' -n 1)
VERSIONSTRING := $(NAME) version $(VERSION)+$(BUILDSTRING)
BUILDDATE := $(shell date -u -Iseconds)
CONFIG_FILE=config/config.yaml
glaslos marked this conversation as resolved.
Show resolved Hide resolved
INTERFACE=$(shell grep 'interface:' $(CONFIG_FILE) | awk '{print $$2}')

LDFLAGS := "-X \"main.VERSION=$(VERSIONSTRING)\" -X \"main.BUILDDATE=$(BUILDDATE)\""

Expand Down Expand Up @@ -30,7 +32,7 @@ clean:
rm -rf bin/

run: build
sudo bin/server -i eth0
sudo bin/server -i $(INTERFACE)
glaslos marked this conversation as resolved.
Show resolved Hide resolved

docker:
docker build -t glutton .
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ Arch:
pacman -S gcc libpcap iptables
```

Fedora:
```
sudo dnf install gcc libpcap-devel iptables
glaslos marked this conversation as resolved.
Show resolved Hide resolved
```

Build glutton:
```
make build
```

To run/test glutton:
```
bin/server
sudo bin/server
```

To get this to work on WSL, use this kernel: https://github.com/Locietta/xanmod-kernel-WSL2
5 changes: 5 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
interface: eth0
tcpPort: 8080
udpPort: 8081
sshPort: 2222
glaslos marked this conversation as resolved.
Show resolved Hide resolved

ports:
tcp: 5000
udp: 5001
Expand Down
26 changes: 21 additions & 5 deletions glutton.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
_ "embed"
"flag"
"fmt"
"io"
"log/slog"
Expand Down Expand Up @@ -40,9 +41,18 @@ type Glutton struct {
publicAddrs []net.IP
}

var (
interfaceName string
)

//go:embed config/rules.yaml
var defaultRules []byte

func init() {
glaslos marked this conversation as resolved.
Show resolved Hide resolved
flag.StringVar(&interfaceName, "i", "", "Network interface name")
flag.Parse()
}

func (g *Glutton) initConfig() error {
viper.SetConfigName("config")
viper.AddConfigPath(viper.GetString("confpath"))
Expand All @@ -58,6 +68,12 @@ func (g *Glutton) initConfig() error {
viper.SetDefault("max_tcp_payload", 4096)
viper.SetDefault("conn_timeout", 45)
viper.SetDefault("rules_path", "rules/rules.yaml")
viper.SetDefault("interface", "eth0") // Default interface name

if interfaceName == "" {
glaslos marked this conversation as resolved.
Show resolved Hide resolved
interfaceName = viper.GetString("interface")
}

g.Logger.Debug("configuration set successfully", slog.String("reporter", "glutton"))
return nil
}
Expand Down Expand Up @@ -108,7 +124,7 @@ func New(ctx context.Context) (*Glutton, error) {
// Init initializes server and handles
func (g *Glutton) Init() error {
var err error
g.publicAddrs, err = getNonLoopbackIPs(viper.GetString("interface"))
g.publicAddrs, err = getNonLoopbackIPs(interfaceName)
glaslos marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down Expand Up @@ -241,11 +257,11 @@ func (g *Glutton) Start() error {
g.startMonitor()

sshPort := viper.GetUint32("ports.ssh")
if err := setTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "tcp", uint32(g.Server.tcpPort), sshPort); err != nil {
if err := setTProxyIPTables(interfaceName, g.publicAddrs[0].String(), "tcp", uint32(g.Server.tcpPort), sshPort); err != nil {
glaslos marked this conversation as resolved.
Show resolved Hide resolved
return err
}

if err := setTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "udp", uint32(g.Server.udpPort), sshPort); err != nil {
if err := setTProxyIPTables(interfaceName, g.publicAddrs[0].String(), "udp", uint32(g.Server.udpPort), sshPort); err != nil {
return err
}

Expand Down Expand Up @@ -353,11 +369,11 @@ func (g *Glutton) Shutdown() {
g.cancel() // close all connection

g.Logger.Info("Flushing TCP iptables")
if err := flushTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "tcp", uint32(g.Server.tcpPort), uint32(viper.GetInt("ports.ssh"))); err != nil {
if err := flushTProxyIPTables(interfaceName, g.publicAddrs[0].String(), "tcp", uint32(g.Server.tcpPort), uint32(viper.GetInt("ports.ssh"))); err != nil {
g.Logger.Error("Failed to drop tcp iptables", producer.ErrAttr(err))
}
g.Logger.Info("Flushing UDP iptables")
if err := flushTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "udp", uint32(g.Server.udpPort), uint32(viper.GetInt("ports.ssh"))); err != nil {
if err := flushTProxyIPTables(interfaceName, g.publicAddrs[0].String(), "udp", uint32(g.Server.udpPort), uint32(viper.GetInt("ports.ssh"))); err != nil {
g.Logger.Error("Failed to drop udp iptables", producer.ErrAttr(err))
}

Expand Down