feat: expose keepalive and resolve interval options

master
Shengjing Zhu 2022-03-09 23:29:54 +08:00
parent b5132a1677
commit eb00d070da
2 changed files with 19 additions and 19 deletions

20
conf.go
View File

@ -12,16 +12,10 @@ import (
"golang.zx2c4.com/wireguard/device"
)
const (
resolvePeerInterval = time.Second * 10
keepaliveInterval = "10"
)
type peer struct {
dialer *net.Dialer
pubKey string
keepalive bool
pubKey string
addr string
ipPort string
@ -60,9 +54,8 @@ func newPeerEndpoint() (*peer, error) {
},
},
},
pubKey: hex.EncodeToString(pubKey),
keepalive: opts.ExitMode == "local",
addr: opts.PeerEndpoint,
pubKey: hex.EncodeToString(pubKey),
addr: opts.PeerEndpoint,
}
p.ipPort, err = p.resolveAddr()
if err != nil {
@ -77,8 +70,8 @@ func (p *peer) initConf() string {
conf += "allowed_ip=0.0.0.0/0\n"
conf += "allowed_ip=::/0\n"
if p.keepalive {
conf += "persistent_keepalive_interval=" + keepaliveInterval + "\n"
if opts.KeepaliveInterval > 0 {
conf += fmt.Sprintf("persistent_keepalive_interval=%.f\n", opts.KeepaliveInterval.Seconds())
}
return conf
@ -123,6 +116,7 @@ func ipcSet(dev *device.Device) error {
return err
}
conf += peer.initConf()
logger.Verbosef("Device config:\n%s", conf)
if err := dev.IpcSet(conf); err != nil {
return err
@ -130,7 +124,7 @@ func ipcSet(dev *device.Device) error {
if peer.addr != peer.ipPort {
go func() {
c := time.Tick(resolvePeerInterval)
c := time.Tick(opts.ResolveInterval)
for range c {
conf, needUpdate := peer.updateConf()

18
main.go
View File

@ -9,6 +9,7 @@ import (
"net/http"
"os"
"strings"
"time"
"github.com/jessevdk/go-flags"
"golang.zx2c4.com/wireguard/device"
@ -32,12 +33,17 @@ type options struct {
PeerKey string `long:"peer-key" env:"PEER_KEY" description:"WireGuard server public key in base64 format"`
PrivateKey string `long:"private-key" env:"PRIVATE_KEY" description:"WireGuard client private key in base64 format"`
ClientIPs []string `long:"client-ip" env:"CLIENT_IP" env-delim:"," description:"WireGuard client IP address"`
DNS string `long:"dns" env:"DNS" description:"DNS server for WireGuard network and resolving server address"`
DoT string `long:"dot" env:"DOT" description:"Port for DNS over TLS, used to resolve WireGuard server address if available"`
MTU int `long:"mtu" env:"MTU" default:"1280" description:"MTU for WireGuard network"`
Listen string `long:"listen" env:"LISTEN" default:"localhost:8080" description:"HTTP & SOCKS5 server address"`
ExitMode string `long:"exit-mode" env:"EXIT_MODE" choice:"remote" choice:"local" default:"remote" description:"Exit mode"`
Verbose bool `short:"v" long:"verbose" description:"Show verbose debug information"`
DNS string `long:"dns" env:"DNS" description:"DNS server for WireGuard network and resolving server address"`
DoT string `long:"dot" env:"DOT" description:"Port for DNS over TLS, used to resolve WireGuard server address if available"`
MTU int `long:"mtu" env:"MTU" default:"1280" description:"MTU for WireGuard network"`
KeepaliveInterval time.Duration `long:"keepalive-interval" env:"KEEPALIVE_INTERVAL" description:"Interval for sending keepalive packet"`
ResolveInterval time.Duration `long:"resolve-interval" env:"RESOLVE_INTERVAL" default:"1m" description:"Interval for resolving WireGuard server address"`
Listen string `long:"listen" env:"LISTEN" default:"localhost:8080" description:"HTTP & SOCKS5 server address"`
ExitMode string `long:"exit-mode" env:"EXIT_MODE" choice:"remote" choice:"local" default:"remote" description:"Exit mode"`
Verbose bool `short:"v" long:"verbose" description:"Show verbose debug information"`
ClientID string `long:"client-id" env:"CLIENT_ID" hidden:"true"`
}