mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 23:47:07 -05:00
Merge pull request #2086 from vcptr/rfc8484
doh config use RFC8484 url format
This commit is contained in:
commit
698b04fa2a
@ -7,6 +7,8 @@ package dns
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -85,22 +87,49 @@ 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(), "DOHL_") {
|
} else if address.Family().IsDomain() && strings.HasPrefix(address.Domain(), "https+local://") {
|
||||||
dohHost := address.Domain()[5:]
|
// URI schemed string treated as domain
|
||||||
server.clients = append(server.clients, NewDoHLocalNameServer(dohHost, endpoint.Port, server.clientIP))
|
dohlHost, dohlPort, err := parseDOHURI(address.Domain(), endpoint)
|
||||||
} else if address.Family().IsDomain() && strings.HasPrefix(address.Domain(), "DOH_") {
|
if err != nil {
|
||||||
// DOH_ prefix makes net.Address think it's a domain
|
log.Fatalln(newError("DNS config error").Base(err))
|
||||||
dohHost := address.Domain()[4:]
|
}
|
||||||
|
server.clients = append(server.clients, NewDoHLocalNameServer(dohlHost, dohlPort, server.clientIP))
|
||||||
|
} else if address.Family().IsDomain() &&
|
||||||
|
strings.HasPrefix(address.Domain(), "https://") {
|
||||||
|
dohHost, dohPort, err := parseDOHURI(address.Domain(), endpoint)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(newError("DNS config error").Base(err))
|
||||||
|
}
|
||||||
idx := len(server.clients)
|
idx := len(server.clients)
|
||||||
server.clients = append(server.clients, nil)
|
server.clients = append(server.clients, nil)
|
||||||
|
|
||||||
// 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, endpoint.Port, d, server.clientIP)
|
c, err := NewDoHNameServer(dohHost, dohPort, 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))
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,25 @@ func TestDomainParsing(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestURLParsing(t *testing.T) {
|
||||||
|
{
|
||||||
|
rawJson := "\"https://dns.google/dns-query\""
|
||||||
|
var address Address
|
||||||
|
common.Must(json.Unmarshal([]byte(rawJson), &address))
|
||||||
|
if address.Domain() != "https://dns.google/dns-query" {
|
||||||
|
t.Error("URL: ", address.Domain())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
rawJson := "\"https+local://dns.google/dns-query\""
|
||||||
|
var address Address
|
||||||
|
common.Must(json.Unmarshal([]byte(rawJson), &address))
|
||||||
|
if address.Domain() != "https+local://dns.google/dns-query" {
|
||||||
|
t.Error("URL: ", address.Domain())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestInvalidAddressJson(t *testing.T) {
|
func TestInvalidAddressJson(t *testing.T) {
|
||||||
rawJson := "1234"
|
rawJson := "1234"
|
||||||
var address Address
|
var address Address
|
||||||
|
Loading…
Reference in New Issue
Block a user