Skip to content

Commit

Permalink
feat: ✨ add DNS configuration support #30
Browse files Browse the repository at this point in the history
- Add default DNS address (1.1.1.1), and a command-line option (--dns) to customize it.
  • Loading branch information
pizokh authored and markpash committed May 19, 2024
1 parent 3a9504a commit 6179fff
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
9 changes: 9 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type WarpOptions struct {
Bind netip.AddrPort
Endpoint string
License string
DnsAddr netip.Addr
Psiphon *PsiphonOptions
Gool bool
Scan *wiresocks.ScanOptions
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions example_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"bind": "127.0.0.1:8086",
"endpoint": "",
"key": "",
"dns": "1.1.1.1",
"gool": false,
"cfon": false,
"country": "DE",
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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),
Expand Down
14 changes: 1 addition & 13 deletions warp/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
21 changes: 9 additions & 12 deletions wiresocks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 6179fff

Please sign in to comment.