Skip to content

Commit b3b5944

Browse files
authored
custom ssh port (#164)
1 parent 0d53adf commit b3b5944

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

app/server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func main() {
3535
fmt.Printf("%s %s\n", VERSION, BUILDDATE)
3636

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

48+
if viper.IsSet("ssh") {
49+
viper.Set("ports.ssh", viper.GetInt("ssh"))
50+
}
51+
4752
if viper.GetBool("version") {
4853
return
4954
}

config/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ports:
22
tcp: 5000
33
udp: 5001
4+
ssh: 2222
45

56
rules_path: config/rules.yaml
67

glutton.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func (g *Glutton) initConfig() error {
5353
// If no config is found, use the defaults
5454
viper.SetDefault("ports.tcp", 5000)
5555
viper.SetDefault("ports.udp", 5001)
56+
viper.SetDefault("ports.ssh", 22)
5657
viper.SetDefault("max_tcp_payload", 4096)
5758
viper.SetDefault("conn_timeout", 45)
5859
viper.SetDefault("rules_path", "rules/rules.yaml")
@@ -186,11 +187,11 @@ func (g *Glutton) Start() error {
186187

187188
g.startMonitor(quit)
188189

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

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

@@ -335,11 +336,11 @@ func (g *Glutton) Shutdown() {
335336
}
336337

337338
g.Logger.Info("FLushing TCP iptables")
338-
if err := flushTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "tcp", uint32(g.Server.tcpPort)); err != nil {
339+
if err := flushTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "tcp", uint32(g.Server.tcpPort), uint32(viper.GetInt("ports.ssh"))); err != nil {
339340
g.Logger.Error("failed to drop tcp iptables", producer.ErrAttr(err))
340341
}
341342
g.Logger.Info("FLushing UDP iptables")
342-
if err := flushTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "udp", uint32(g.Server.udpPort)); err != nil {
343+
if err := flushTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "udp", uint32(g.Server.udpPort), uint32(viper.GetInt("ports.ssh"))); err != nil {
343344
g.Logger.Error("failed to drop udp iptables", producer.ErrAttr(err))
344345
}
345346

iptables.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ func genRuleSpec(chain, iface, protocol, _ string, sshPort, dport uint32) []stri
3030
return strings.Split(fmt.Sprintf(spec, iface, protocol, sshPort, dport), ";")
3131
}
3232

33-
func setTProxyIPTables(iface, srcIP, protocol string, port uint32) error {
33+
func setTProxyIPTables(iface, srcIP, protocol string, port, sshPort uint32) error {
3434
ipt, err := iptables.NewWithProtocol(iptables.ProtocolIPv4)
3535
if err != nil {
3636
return err
3737
}
38-
return ipt.AppendUnique("mangle", "PREROUTING", genRuleSpec("PREROUTING", iface, protocol, srcIP, 22, port)...)
38+
return ipt.AppendUnique("mangle", "PREROUTING", genRuleSpec("PREROUTING", iface, protocol, srcIP, sshPort, port)...)
3939
}
4040

41-
func flushTProxyIPTables(iface, srcIP, protocol string, port uint32) error {
41+
func flushTProxyIPTables(iface, srcIP, protocol string, port, sshPort uint32) error {
4242
ipt, err := iptables.NewWithProtocol(iptables.ProtocolIPv4)
4343
if err != nil {
4444
return err
4545
}
4646

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

0 commit comments

Comments
 (0)