Skip to content

Commit c8860da

Browse files
authored
TCP and Privileged Ping Fixes (#19)
* Fix nil remote address on premature connection close * Set timeout for unused connections * Add timer to reap connections not used by client * Enable TCP keepalives between client and server * Add timeout to privileged pings * Make TCP keepalive options configurable
1 parent 2d0d7bc commit c8860da

File tree

5 files changed

+767
-66
lines changed

5 files changed

+767
-66
lines changed

src/cmd/serve.go

+57-24
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"strings"
1111
"sync"
12+
"time"
1213

1314
"github.com/spf13/cobra"
1415
"github.com/spf13/viper"
@@ -28,16 +29,21 @@ import (
2829
)
2930

3031
type serveCmdConfig struct {
31-
configFile string
32-
clientAddr4E2EE string
33-
clientAddr6E2EE string
34-
clientAddr4Relay string
35-
clientAddr6Relay string
36-
quiet bool
37-
debug bool
38-
simple bool
39-
logging bool
40-
logFile string
32+
configFile string
33+
clientAddr4E2EE string
34+
clientAddr6E2EE string
35+
clientAddr4Relay string
36+
clientAddr6Relay string
37+
quiet bool
38+
debug bool
39+
simple bool
40+
logging bool
41+
logFile string
42+
catchTimeout uint
43+
connTimeout uint
44+
keepaliveIdle uint
45+
keepaliveCount uint
46+
keepaliveInterval uint
4147
}
4248

4349
type wiretapDefaultConfig struct {
@@ -56,16 +62,21 @@ type wiretapDefaultConfig struct {
5662

5763
// Defaults for serve command.
5864
var serveCmd = serveCmdConfig{
59-
configFile: "",
60-
clientAddr4E2EE: ClientE2EESubnet4.Addr().Next().String(),
61-
clientAddr6E2EE: ClientE2EESubnet6.Addr().Next().String(),
62-
clientAddr4Relay: ClientRelaySubnet4.Addr().Next().Next().String(),
63-
clientAddr6Relay: ClientRelaySubnet6.Addr().Next().Next().String(),
64-
quiet: false,
65-
debug: false,
66-
simple: false,
67-
logging: false,
68-
logFile: "wiretap.log",
65+
configFile: "",
66+
clientAddr4E2EE: ClientE2EESubnet4.Addr().Next().String(),
67+
clientAddr6E2EE: ClientE2EESubnet6.Addr().Next().String(),
68+
clientAddr4Relay: ClientRelaySubnet4.Addr().Next().Next().String(),
69+
clientAddr6Relay: ClientRelaySubnet6.Addr().Next().Next().String(),
70+
quiet: false,
71+
debug: false,
72+
simple: false,
73+
logging: false,
74+
logFile: "wiretap.log",
75+
catchTimeout: 5 * 1000,
76+
connTimeout: 5 * 1000,
77+
keepaliveIdle: 60,
78+
keepaliveCount: 3,
79+
keepaliveInterval: 60,
6980
}
7081

7182
var wiretapDefault = wiretapDefaultConfig{
@@ -105,6 +116,11 @@ func init() {
105116
cmd.Flags().BoolVarP(&serveCmd.simple, "simple", "", serveCmd.simple, "disable multihop and multiclient features for a simpler setup")
106117
cmd.Flags().BoolVarP(&serveCmd.logging, "log", "l", serveCmd.logging, "enable logging to file")
107118
cmd.Flags().StringVarP(&serveCmd.logFile, "log-file", "o", serveCmd.logFile, "write log to this filename")
119+
cmd.Flags().UintVarP(&serveCmd.catchTimeout, "completion-timeout", "", serveCmd.catchTimeout, "time in ms for client to complete TCP connection to server")
120+
cmd.Flags().UintVarP(&serveCmd.connTimeout, "conn-timeout", "", serveCmd.connTimeout, "time in ms for server to wait for outgoing TCP handshakes to complete")
121+
cmd.Flags().UintVarP(&serveCmd.connTimeout, "keepalive-idle", "", serveCmd.keepaliveIdle, "time in seconds before TCP keepalives are sent to client")
122+
cmd.Flags().UintVarP(&serveCmd.connTimeout, "keepalive-interval", "", serveCmd.keepaliveInterval, "time in seconds between TCP keepalives")
123+
cmd.Flags().UintVarP(&serveCmd.connTimeout, "keepalive-count", "", serveCmd.keepaliveCount, "number of unacknowledged TCP keepalives before closing connection")
108124

109125
cmd.Flags().StringVarP(&serveCmd.clientAddr4Relay, "ipv4-relay-client", "", serveCmd.clientAddr4Relay, "ipv4 relay address of client")
110126
cmd.Flags().StringVarP(&serveCmd.clientAddr6Relay, "ipv6-relay-client", "", serveCmd.clientAddr6Relay, "ipv6 relay address of client")
@@ -212,6 +228,11 @@ func init() {
212228
"api",
213229
"keepalive",
214230
"mtu",
231+
"conn-timeout",
232+
"completion-timeout",
233+
"keepalive-interval",
234+
"keepalive-count",
235+
"keepalive-idle",
215236
} {
216237
err := cmd.Flags().MarkHidden(f)
217238
if err != nil {
@@ -298,9 +319,10 @@ func (c serveCmdConfig) Run() {
298319
ListenPort: E2EEPort,
299320
Peers: []peer.PeerConfigArgs{
300321
{
301-
PublicKey: viper.GetString("E2EE.Peer.publickey"),
302-
Endpoint: viper.GetString("E2EE.Peer.endpoint"),
303-
AllowedIPs: []string{c.clientAddr4E2EE + "/32", c.clientAddr6E2EE + "/128"},
322+
PublicKey: viper.GetString("E2EE.Peer.publickey"),
323+
Endpoint: viper.GetString("E2EE.Peer.endpoint"),
324+
AllowedIPs: []string{c.clientAddr4E2EE + "/32", c.clientAddr6E2EE + "/128"},
325+
PersistentKeepaliveInterval: viper.GetInt("Relay.Peer.keepalive"),
304326
},
305327
},
306328
Addresses: []string{viper.GetString("E2EE.Interface.ipv4") + "/32", viper.GetString("E2EE.Interface.ipv6") + "/128", viper.GetString("E2EE.Interface.api") + "/128"},
@@ -415,11 +437,22 @@ func (c serveCmdConfig) Run() {
415437
return tnetE2EE
416438
}
417439
}()
440+
418441
// Start transport layer handlers under the e2ee device.
419442
wg.Add(1)
420443
lock.Lock()
421444
go func() {
422-
tcp.Handle(transportHandler, ipv4Addr, ipv6Addr, 1337, &lock)
445+
config := tcp.TcpConfig{
446+
CatchTimeout: time.Duration(c.catchTimeout) * time.Millisecond,
447+
ConnTimeout: time.Duration(c.connTimeout) * time.Millisecond,
448+
KeepaliveIdle: time.Duration(c.keepaliveIdle) * time.Second,
449+
KeepaliveInterval: time.Duration(c.keepaliveInterval) * time.Second,
450+
KeepaliveCount: int(c.keepaliveCount),
451+
Ipv4Addr: ipv4Addr,
452+
Ipv6Addr: ipv6Addr,
453+
Port: 1337,
454+
}
455+
tcp.Handle(transportHandler, config, &lock)
423456
wg.Done()
424457
}()
425458

src/transport/icmp/ping.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55
"os/exec"
66
"runtime"
7+
"time"
78

89
"github.com/go-ping/ping"
910
)
@@ -41,13 +42,19 @@ func (socketPing) ping(addr string) (success bool, err error) {
4142
return false, err
4243
}
4344

45+
pinger.RecordRtts = false
46+
pinger.Timeout = 1 * time.Second
4447
pinger.Count = 1
4548
err = pinger.Run()
4649
if err != nil {
4750
return false, err
4851
}
4952

50-
return true, nil
53+
if pinger.PacketsRecv > 0 {
54+
return true, nil
55+
} else {
56+
return false, nil
57+
}
5158
}
5259

5360
// execPing attempts to ping destination address via ping binary on the local machine.

0 commit comments

Comments
 (0)