mirror of https://github.com/v2ray/v2ray-core
doh URL controls full path
parent
b4b4b3d032
commit
d6df5d7cf9
|
@ -8,6 +8,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
@ -41,25 +42,25 @@ type DoHNameServer struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDoHNameServer creates DOH client object for remote resolving
|
// NewDoHNameServer creates DOH client object for remote resolving
|
||||||
func NewDoHNameServer(dohHost string, dohPort uint32, dispatcher routing.Dispatcher, clientIP net.IP) (*DoHNameServer, error) {
|
func NewDoHNameServer(url *url.URL, dispatcher routing.Dispatcher, clientIP net.IP) (*DoHNameServer, error) {
|
||||||
|
|
||||||
dohAddr := net.ParseAddress(dohHost)
|
dohAddr := net.ParseAddress(url.Hostname())
|
||||||
var dests []net.Destination
|
dohPort := "443"
|
||||||
|
if url.Port() != "" {
|
||||||
if dohPort == 0 {
|
dohPort = url.Port()
|
||||||
dohPort = 443
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parseIPDest := func(ip net.IP, port uint32) net.Destination {
|
parseIPDest := func(ip net.IP, port string) net.Destination {
|
||||||
strIP := ip.String()
|
strIP := ip.String()
|
||||||
if len(ip) == net.IPv6len {
|
if len(ip) == net.IPv6len {
|
||||||
strIP = fmt.Sprintf("[%s]", strIP)
|
strIP = fmt.Sprintf("[%s]", strIP)
|
||||||
}
|
}
|
||||||
dest, err := net.ParseDestination(fmt.Sprintf("tcp:%s:%d", strIP, port))
|
dest, err := net.ParseDestination(fmt.Sprintf("tcp:%s:%s", strIP, port))
|
||||||
common.Must(err)
|
common.Must(err)
|
||||||
return dest
|
return dest
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var dests []net.Destination
|
||||||
if dohAddr.Family().IsDomain() {
|
if dohAddr.Family().IsDomain() {
|
||||||
// resolve DOH server in advance
|
// resolve DOH server in advance
|
||||||
ips, err := net.LookupIP(dohAddr.Domain())
|
ips, err := net.LookupIP(dohAddr.Domain())
|
||||||
|
@ -74,8 +75,8 @@ func NewDoHNameServer(dohHost string, dohPort uint32, dispatcher routing.Dispatc
|
||||||
dests = append(dests, parseIPDest(ip, dohPort))
|
dests = append(dests, parseIPDest(ip, dohPort))
|
||||||
}
|
}
|
||||||
|
|
||||||
newError("DNS: created remote DOH client for https://", dohHost, ":", dohPort).AtInfo().WriteToLog()
|
newError("DNS: created Remote DOH client for ", url.String(), ", preresolved Dests: ", dests).AtInfo().WriteToLog()
|
||||||
s := baseDOHNameServer(dohHost, dohPort, "DOH", clientIP)
|
s := baseDOHNameServer(url, "DOH", clientIP)
|
||||||
s.dispatcher = dispatcher
|
s.dispatcher = dispatcher
|
||||||
s.dohDests = dests
|
s.dohDests = dests
|
||||||
|
|
||||||
|
@ -102,32 +103,24 @@ func NewDoHNameServer(dohHost string, dohPort uint32, dispatcher routing.Dispatc
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDoHLocalNameServer creates DOH client object for local resolving
|
// NewDoHLocalNameServer creates DOH client object for local resolving
|
||||||
func NewDoHLocalNameServer(dohHost string, dohPort uint32, clientIP net.IP) *DoHNameServer {
|
func NewDoHLocalNameServer(url *url.URL, clientIP net.IP) *DoHNameServer {
|
||||||
|
url.Scheme = "https"
|
||||||
if dohPort == 0 {
|
s := baseDOHNameServer(url, "DOHL", clientIP)
|
||||||
dohPort = 443
|
|
||||||
}
|
|
||||||
|
|
||||||
s := baseDOHNameServer(dohHost, dohPort, "DOHL", clientIP)
|
|
||||||
s.httpClient = &http.Client{
|
s.httpClient = &http.Client{
|
||||||
Timeout: time.Second * 180,
|
Timeout: time.Second * 180,
|
||||||
}
|
}
|
||||||
newError("DNS: created local DOH client for https://", dohHost, ":", dohPort).AtInfo().WriteToLog()
|
newError("DNS: created Local DOH client for ", url.String()).AtInfo().WriteToLog()
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func baseDOHNameServer(dohHost string, dohPort uint32, prefix string, clientIP net.IP) *DoHNameServer {
|
func baseDOHNameServer(url *url.URL, prefix string, clientIP net.IP) *DoHNameServer {
|
||||||
|
|
||||||
if dohPort == 0 {
|
|
||||||
dohPort = 443
|
|
||||||
}
|
|
||||||
|
|
||||||
s := &DoHNameServer{
|
s := &DoHNameServer{
|
||||||
ips: make(map[string]record),
|
ips: make(map[string]record),
|
||||||
clientIP: clientIP,
|
clientIP: clientIP,
|
||||||
pub: pubsub.NewService(),
|
pub: pubsub.NewService(),
|
||||||
name: fmt.Sprintf("%s:%s:%d", prefix, dohHost, dohPort),
|
name: fmt.Sprintf("%s//%s", prefix, url.Host),
|
||||||
dohURL: fmt.Sprintf("https://%s:%d/dns-query", dohHost, dohPort),
|
dohURL: url.String(),
|
||||||
}
|
}
|
||||||
s.cleanup = &task.Periodic{
|
s.cleanup = &task.Periodic{
|
||||||
Interval: time.Minute,
|
Interval: time.Minute,
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -87,40 +86,22 @@ func New(ctx context.Context, config *Config) (*Server, error) {
|
||||||
}
|
}
|
||||||
server.hosts = hosts
|
server.hosts = hosts
|
||||||
|
|
||||||
parseDOHURI := func(d string, endpoint *net.Endpoint) (host string, port uint32, err error) {
|
|
||||||
u, err := url.Parse(d)
|
|
||||||
if err != nil {
|
|
||||||
return "", 0, err
|
|
||||||
}
|
|
||||||
host = u.Hostname()
|
|
||||||
port = 443
|
|
||||||
if u.Port() != "" {
|
|
||||||
p, err := strconv.ParseUint(u.Port(), 10, 16)
|
|
||||||
if err != nil {
|
|
||||||
return "", 0, err
|
|
||||||
}
|
|
||||||
port = uint32(p)
|
|
||||||
}
|
|
||||||
if endpoint.Port != 0 {
|
|
||||||
port = endpoint.Port
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
addNameServer := func(endpoint *net.Endpoint) int {
|
addNameServer := func(endpoint *net.Endpoint) int {
|
||||||
address := endpoint.Address.AsAddress()
|
address := endpoint.Address.AsAddress()
|
||||||
if address.Family().IsDomain() && address.Domain() == "localhost" {
|
if address.Family().IsDomain() && address.Domain() == "localhost" {
|
||||||
server.clients = append(server.clients, NewLocalNameServer())
|
server.clients = append(server.clients, NewLocalNameServer())
|
||||||
} else if address.Family().IsDomain() && strings.HasPrefix(address.Domain(), "https+local://") {
|
} else if address.Family().IsDomain() && strings.HasPrefix(address.Domain(), "https+local://") {
|
||||||
// URI schemed string treated as domain
|
// URI schemed string treated as domain
|
||||||
dohlHost, dohlPort, err := parseDOHURI(address.Domain(), endpoint)
|
// DOH Local mode
|
||||||
|
u, err := url.Parse(address.Domain())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(newError("DNS config error").Base(err))
|
log.Fatalln(newError("DNS config error").Base(err))
|
||||||
}
|
}
|
||||||
server.clients = append(server.clients, NewDoHLocalNameServer(dohlHost, dohlPort, server.clientIP))
|
server.clients = append(server.clients, NewDoHLocalNameServer(u, server.clientIP))
|
||||||
} else if address.Family().IsDomain() &&
|
} else if address.Family().IsDomain() &&
|
||||||
strings.HasPrefix(address.Domain(), "https://") {
|
strings.HasPrefix(address.Domain(), "https://") {
|
||||||
dohHost, dohPort, err := parseDOHURI(address.Domain(), endpoint)
|
// DOH Remote mode
|
||||||
|
u, err := url.Parse(address.Domain())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(newError("DNS config error").Base(err))
|
log.Fatalln(newError("DNS config error").Base(err))
|
||||||
}
|
}
|
||||||
|
@ -129,13 +110,14 @@ func New(ctx context.Context, config *Config) (*Server, error) {
|
||||||
|
|
||||||
// need the core dispatcher, register DOHClient at callback
|
// need the core dispatcher, register DOHClient at callback
|
||||||
common.Must(core.RequireFeatures(ctx, func(d routing.Dispatcher) {
|
common.Must(core.RequireFeatures(ctx, func(d routing.Dispatcher) {
|
||||||
c, err := NewDoHNameServer(dohHost, dohPort, d, server.clientIP)
|
c, err := NewDoHNameServer(u, d, server.clientIP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(newError("DNS config error").Base(err))
|
log.Fatalln(newError("DNS config error").Base(err))
|
||||||
}
|
}
|
||||||
server.clients[idx] = c
|
server.clients[idx] = c
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
|
// UDP classic DNS mode
|
||||||
dest := endpoint.AsDestination()
|
dest := endpoint.AsDestination()
|
||||||
if dest.Network == net.Network_Unknown {
|
if dest.Network == net.Network_Unknown {
|
||||||
dest.Network = net.Network_UDP
|
dest.Network = net.Network_UDP
|
||||||
|
|
Loading…
Reference in New Issue