From 6179fffccb387749a05b320ba37a322381c9d0a1 Mon Sep 17 00:00:00 2001 From: PIZO <142996773+pizokh@users.noreply.github.com> Date: Sun, 19 May 2024 13:56:26 +0330 Subject: [PATCH] feat: :sparkles: add DNS configuration support #30 - Add default DNS address (1.1.1.1), and a command-line option (--dns) to customize it. --- app/app.go | 9 +++++++++ example_config.json | 1 + main.go | 7 +++++++ warp/account.go | 14 +------------- wiresocks/config.go | 21 +++++++++------------ 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/app/app.go b/app/app.go index b6fd7f572..1d93902b9 100644 --- a/app/app.go +++ b/app/app.go @@ -22,6 +22,7 @@ type WarpOptions struct { Bind netip.AddrPort Endpoint string License string + DnsAddr netip.Addr Psiphon *PsiphonOptions Gool bool Scan *wiresocks.ScanOptions @@ -109,6 +110,8 @@ func runWarp(ctx context.Context, l *slog.Logger, opts WarpOptions, endpoint str // Set up MTU conf.Interface.MTU = singleMTU + // Set up DNS Address + conf.Interface.DNS = []netip.Addr{opts.DnsAddr} // Enable trick and keepalive on all peers in config for i, peer := range conf.Peers { @@ -168,6 +171,8 @@ func runWarpInWarp(ctx context.Context, l *slog.Logger, opts WarpOptions, endpoi // Set up MTU conf.Interface.MTU = singleMTU + // Set up DNS Address + conf.Interface.DNS = []netip.Addr{opts.DnsAddr} // Enable trick and keepalive on all peers in config for i, peer := range conf.Peers { @@ -207,6 +212,8 @@ func runWarpInWarp(ctx context.Context, l *slog.Logger, opts WarpOptions, endpoi // Set up MTU conf.Interface.MTU = doubleMTU + // Set up DNS Address + conf.Interface.DNS = []netip.Addr{opts.DnsAddr} // Enable keepalive on all peers in config for i, peer := range conf.Peers { @@ -265,6 +272,8 @@ func runWarpWithPsiphon(ctx context.Context, l *slog.Logger, opts WarpOptions, e // Set up MTU conf.Interface.MTU = singleMTU + // Set up DNS Address + conf.Interface.DNS = []netip.Addr{opts.DnsAddr} // Enable trick and keepalive on all peers in config for i, peer := range conf.Peers { diff --git a/example_config.json b/example_config.json index 6004cd894..e6a737544 100644 --- a/example_config.json +++ b/example_config.json @@ -3,6 +3,7 @@ "bind": "127.0.0.1:8086", "endpoint": "", "key": "", + "dns": "1.1.1.1", "gool": false, "cfon": false, "country": "DE", diff --git a/main.go b/main.go index 5a03f847a..1ab25bb7b 100644 --- a/main.go +++ b/main.go @@ -73,6 +73,7 @@ func main() { bind = fs.String('b', "bind", "127.0.0.1:8086", "socks bind address") endpoint = fs.String('e', "endpoint", "", "warp endpoint") key = fs.String('k', "key", "", "warp key") + dns = fs.StringLong("dns", "1.1.1.1", "DNS address") gool = fs.BoolLong("gool", "enable gool mode (warp in warp)") psiphon = fs.BoolLong("cfon", "enable psiphon mode (must provide country as well)") country = fs.StringEnumLong("country", fmt.Sprintf("psiphon country code (valid values: %s)", psiphonCountries), psiphonCountries...) @@ -131,10 +132,16 @@ func main() { fatal(l, fmt.Errorf("invalid bind address: %w", err)) } + dnsAddr, err := netip.ParseAddr(*dns) + if err != nil { + fatal(l, fmt.Errorf("invalid DNS address: %w", err)) + } + opts := app.WarpOptions{ Bind: bindAddrPort, Endpoint: *endpoint, License: *key, + DnsAddr: dnsAddr, Gool: *gool, Tun: *tun, FwMark: uint32(*fwmark), diff --git a/warp/account.go b/warp/account.go index 8b6443a7f..14132c1c3 100644 --- a/warp/account.go +++ b/warp/account.go @@ -266,19 +266,7 @@ func createConf(i Identity, path string) error { buffer.WriteString("[Interface]\n") buffer.WriteString(fmt.Sprintf("PrivateKey = %s\n", i.PrivateKey)) - buffer.WriteString("DNS = ") - buffer.WriteString("1.1.1.1, ") - buffer.WriteString("1.0.0.1, ") - buffer.WriteString("8.8.8.8, ") - buffer.WriteString("8.8.4.4, ") - buffer.WriteString("9.9.9.9, ") - buffer.WriteString("149.112.112.112, ") - buffer.WriteString("2606:4700:4700::1111, ") - buffer.WriteString("2606:4700:4700::1001, ") - buffer.WriteString("2001:4860:4860::8888, ") - buffer.WriteString("2001:4860:4860::8844, ") - buffer.WriteString("2620:fe::fe, ") - buffer.WriteString("2620:fe::9\n") + buffer.WriteString(fmt.Sprintf("Address = %s/24\n", i.Config.Interface.Addresses.V4)) buffer.WriteString(fmt.Sprintf("Address = %s/128\n", i.Config.Interface.Addresses.V6)) diff --git a/wiresocks/config.go b/wiresocks/config.go index 5d42a49e5..a060c92a6 100644 --- a/wiresocks/config.go +++ b/wiresocks/config.go @@ -78,20 +78,17 @@ func ParseInterface(cfg *ini.File) (InterfaceConfig, error) { } device.PrivateKey = privateKeyHex - key = iface.Key("DNS") - if key == nil { - return InterfaceConfig{}, nil - } - - addresses = []netip.Addr{} - for _, str := range key.StringsWithShadows(",") { - ip, err := netip.ParseAddr(str) - if err != nil { - return InterfaceConfig{}, err + if sectionKey, err := iface.GetKey("DNS"); err == nil { + addrs := sectionKey.StringsWithShadows(",") + device.DNS = make([]netip.Addr, len(addrs)) + for i, addr := range addrs { + ip, err := netip.ParseAddr(addr) + if err != nil { + return InterfaceConfig{}, err + } + device.DNS[i] = ip } - addresses = append(addresses, ip) } - device.DNS = addresses if sectionKey, err := iface.GetKey("MTU"); err == nil { value, err := sectionKey.Int()