mirror of
https://github.com/Alexey71/opera-proxy.git
synced 2026-05-14 06:30:59 +00:00
resolver: more liberal factory
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package resolver
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/ncruces/go-dns"
|
||||
)
|
||||
|
||||
func FromURL(u string) (*net.Resolver, error) {
|
||||
parsed, err := url.Parse(u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
host := parsed.Hostname()
|
||||
port := parsed.Port()
|
||||
switch scheme := strings.ToLower(parsed.Scheme); scheme {
|
||||
case "", "udp", "dns":
|
||||
if port == "" {
|
||||
port = "53"
|
||||
}
|
||||
return NewPlainResolver(net.JoinHostPort(host, port)), nil
|
||||
case "tcp":
|
||||
if port == "" {
|
||||
port = "53"
|
||||
}
|
||||
return NewTCPResolver(net.JoinHostPort(host, port)), nil
|
||||
case "http", "https", "doh":
|
||||
if port == "" {
|
||||
if scheme == "http" {
|
||||
port = "80"
|
||||
} else {
|
||||
port = "443"
|
||||
}
|
||||
}
|
||||
if scheme == "doh" {
|
||||
parsed.Scheme = "https"
|
||||
u = parsed.String()
|
||||
}
|
||||
return dns.NewDoHResolver(u, dns.DoHAddresses(net.JoinHostPort(host, port)))
|
||||
case "tls", "dot":
|
||||
if port == "" {
|
||||
port = "853"
|
||||
}
|
||||
hp := net.JoinHostPort(host, port)
|
||||
return dns.NewDoTResolver(hp, dns.DoTAddresses(hp))
|
||||
default:
|
||||
return nil, errors.New("not implemented")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user