Skip to content

Commit

Permalink
custom ssh port (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
thebigbone authored Nov 15, 2024
1 parent 0d53adf commit b3b5944
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
5 changes: 5 additions & 0 deletions app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func main() {
fmt.Printf("%s %s\n", VERSION, BUILDDATE)

pflag.StringP("interface", "i", "eth0", "Bind to this interface")
pflag.IntP("ssh", "s", 0, "Override SSH port")
pflag.StringP("logpath", "l", "/dev/null", "Log file path")
pflag.StringP("confpath", "c", "config/", "Configuration file path")
pflag.BoolP("debug", "d", false, "Enable debug mode")
Expand All @@ -44,6 +45,10 @@ func main() {
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)

if viper.IsSet("ssh") {
viper.Set("ports.ssh", viper.GetInt("ssh"))
}

if viper.GetBool("version") {
return
}
Expand Down
1 change: 1 addition & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ports:
tcp: 5000
udp: 5001
ssh: 2222

rules_path: config/rules.yaml

Expand Down
9 changes: 5 additions & 4 deletions glutton.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (g *Glutton) initConfig() error {
// If no config is found, use the defaults
viper.SetDefault("ports.tcp", 5000)
viper.SetDefault("ports.udp", 5001)
viper.SetDefault("ports.ssh", 22)
viper.SetDefault("max_tcp_payload", 4096)
viper.SetDefault("conn_timeout", 45)
viper.SetDefault("rules_path", "rules/rules.yaml")
Expand Down Expand Up @@ -186,11 +187,11 @@ func (g *Glutton) Start() error {

g.startMonitor(quit)

if err := setTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "tcp", uint32(g.Server.tcpPort)); err != nil {
if err := setTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "tcp", uint32(g.Server.tcpPort), uint32(viper.GetInt("ports.ssh"))); err != nil {
return err
}

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

Expand Down Expand Up @@ -335,11 +336,11 @@ func (g *Glutton) Shutdown() {
}

g.Logger.Info("FLushing TCP iptables")
if err := flushTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "tcp", uint32(g.Server.tcpPort)); err != nil {
if err := flushTProxyIPTables(viper.GetString("interface"), 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)); err != nil {
if err := flushTProxyIPTables(viper.GetString("interface"), 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
8 changes: 4 additions & 4 deletions iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ func genRuleSpec(chain, iface, protocol, _ string, sshPort, dport uint32) []stri
return strings.Split(fmt.Sprintf(spec, iface, protocol, sshPort, dport), ";")
}

func setTProxyIPTables(iface, srcIP, protocol string, port uint32) error {
func setTProxyIPTables(iface, srcIP, protocol string, port, sshPort uint32) error {
ipt, err := iptables.NewWithProtocol(iptables.ProtocolIPv4)
if err != nil {
return err
}
return ipt.AppendUnique("mangle", "PREROUTING", genRuleSpec("PREROUTING", iface, protocol, srcIP, 22, port)...)
return ipt.AppendUnique("mangle", "PREROUTING", genRuleSpec("PREROUTING", iface, protocol, srcIP, sshPort, port)...)
}

func flushTProxyIPTables(iface, srcIP, protocol string, port uint32) error {
func flushTProxyIPTables(iface, srcIP, protocol string, port, sshPort uint32) error {
ipt, err := iptables.NewWithProtocol(iptables.ProtocolIPv4)
if err != nil {
return err
}

return ipt.Delete("mangle", "PREROUTING", genRuleSpec("PREROUTING", iface, protocol, srcIP, 22, port)...)
return ipt.Delete("mangle", "PREROUTING", genRuleSpec("PREROUTING", iface, protocol, srcIP, sshPort, port)...)
}

0 comments on commit b3b5944

Please sign in to comment.