forked from jeessy2/ddns-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom DNS server (jeessy2#682)
* feat: custom DNS server
- Loading branch information
1 parent
7aa0b18
commit d42a7e2
Showing
4 changed files
with
47 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"os" | ||
) | ||
|
||
const DNSServerEnv = "DDNS_GO_DNS_SERVER" | ||
|
||
// customDNSResolver 当 DNSServerEnv 值不为空时,使用 Go 内置 DNS 解析器来解析其 DNS 服务器。 | ||
func customDNSResolver() *net.Resolver { | ||
s := os.Getenv(DNSServerEnv) | ||
if s != "" { | ||
return &net.Resolver{ | ||
PreferGo: true, | ||
Dial: func(ctx context.Context, network, address string) (net.Conn, error) { | ||
return net.Dial("udp", s) | ||
}, | ||
} | ||
} | ||
return &net.Resolver{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
) | ||
|
||
// TestCustomDNSResolver 测试能否通过 DNSServerEnv 值的 DNS 服务器解析域名的 IP。 | ||
func TestCustomDNSResolver(t *testing.T) { | ||
os.Setenv(DNSServerEnv, "1.1.1.1:53") | ||
_, err := customDNSResolver().LookupIP(context.Background(), "ip", "cloudflare.com") | ||
if err != nil { | ||
t.Errorf("Failed to lookup IP, err: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters