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

18
main.go
View File

@ -9,6 +9,7 @@ import (
"net/http" "net/http"
"os" "os"
"strings" "strings"
"time"
"github.com/jessevdk/go-flags" "github.com/jessevdk/go-flags"
"golang.zx2c4.com/wireguard/device" "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"` 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"` 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"` 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"` DNS string `long:"dns" env:"DNS" description:"DNS server for WireGuard network and resolving server address"`
MTU int `long:"mtu" env:"MTU" default:"1280" description:"MTU for WireGuard network"` DoT string `long:"dot" env:"DOT" description:"Port for DNS over TLS, used to resolve WireGuard server address if available"`
Listen string `long:"listen" env:"LISTEN" default:"localhost:8080" description:"HTTP & SOCKS5 server address"` MTU int `long:"mtu" env:"MTU" default:"1280" description:"MTU for WireGuard network"`
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"` 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"` ClientID string `long:"client-id" env:"CLIENT_ID" hidden:"true"`
} }