Config option to set DNS resolver

This commit is contained in:
Edgaru089, devel 2025-04-23 20:56:27 +08:00
parent 9bd7ba6eb9
commit f1fe70386d
3 changed files with 15 additions and 1 deletions

View File

@ -1,4 +1,5 @@
{ {
"ListenAddress": "127.0.0.1:3128", "ListenAddress": "127.0.0.1:3128",
"ListenType": "tcp4" "ListenType": "tcp4",
"DNSResolver": "192.168.1.1:53"
} }

View File

@ -4,4 +4,5 @@ type Config struct {
ListenAddress string // Address to listen on, passed to net.Listen ListenAddress string // Address to listen on, passed to net.Listen
ListenType string // Type of network to listen on, passed to net.Listen. One of tcp, tcp4 and tcp6 ListenType string // Type of network to listen on, passed to net.Listen. One of tcp, tcp4 and tcp6
DNSResolver string // Address to send UDP & TCP DNS requests to. If set, the Go resolver will also be forced.
} }

12
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
@ -8,6 +9,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"time"
"edgaru089.ink/go/regolith/internal/conf" "edgaru089.ink/go/regolith/internal/conf"
"edgaru089.ink/go/regolith/internal/http" "edgaru089.ink/go/regolith/internal/http"
@ -44,6 +46,16 @@ func main() {
} }
} }
if len(conf.DNSResolver) != 0 {
dialer := &net.Dialer{
Timeout: time.Second * 10,
}
net.DefaultResolver.PreferGo = true
net.DefaultResolver.Dial = func(ctx context.Context, network, address string) (net.Conn, error) {
return dialer.Dial(network, conf.DNSResolver)
}
}
listener, err := net.Listen(conf.ListenType, conf.ListenAddress) listener, err := net.Listen(conf.ListenType, conf.ListenAddress)
if err != nil { if err != nil {
panic(err) panic(err)