mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-27 03:50:08 +00:00
XDNS finalmask: Add alphabet to domains for UDP/53 in Iran (#7457)
https://github.com/XTLS/Xray-core/pull/7457#issuecomment-7075534791
This commit is contained in:
@@ -867,6 +867,7 @@ type XDNSDomain struct {
|
||||
LabelLimit int32 `json:"labelLimit"`
|
||||
Types []int32 `json:"types"`
|
||||
Edns0 int32 `json:"edns0"`
|
||||
Alphabet string `json:"alphabet"`
|
||||
}
|
||||
|
||||
type XDNSResolver struct {
|
||||
@@ -893,7 +894,7 @@ func (c *XDNS) Build() (proto.Message, error) {
|
||||
c.Domains[i].LabelLimit = 63
|
||||
}
|
||||
for j := range c.Domains[i].Names {
|
||||
domain, err := xdns.NewDomain(c.Domains[i].Names[j], int(c.Domains[i].LenLimit), int(c.Domains[i].LabelLimit), []uint16{1, 5, 16, 28}, uint16(c.Domains[i].Edns0))
|
||||
domain, err := xdns.NewDomain(c.Domains[i].Names[j], int(c.Domains[i].LenLimit), int(c.Domains[i].LabelLimit), []uint16{1, 5, 16, 28}, uint16(c.Domains[i].Edns0), c.Domains[i].Alphabet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -904,6 +905,7 @@ func (c *XDNS) Build() (proto.Message, error) {
|
||||
LabelLimit: c.Domains[i].LabelLimit,
|
||||
Types: c.Domains[i].Types,
|
||||
Edns0: c.Domains[i].Edns0,
|
||||
Alphabet: c.Domains[i].Alphabet,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ func NewClient(c *Config, dialer *finalmask.Dialer) (net.PacketConn, error) {
|
||||
if len(types) == 0 {
|
||||
types = []uint16{16}
|
||||
}
|
||||
domain, err := NewDomain(c.Domains[i].Name, int(c.Domains[i].LenLimit), int(c.Domains[i].LabelLimit), types, uint16(c.Domains[i].Edns0))
|
||||
domain, err := NewDomain(c.Domains[i].Name, int(c.Domains[i].LenLimit), int(c.Domains[i].LabelLimit), types, uint16(c.Domains[i].Edns0), c.Domains[i].Alphabet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ type DomainProto struct {
|
||||
LabelLimit int32 `protobuf:"varint,3,opt,name=label_limit,json=labelLimit,proto3" json:"label_limit,omitempty"`
|
||||
Types []int32 `protobuf:"varint,4,rep,packed,name=types,proto3" json:"types,omitempty"`
|
||||
Edns0 int32 `protobuf:"varint,5,opt,name=edns0,proto3" json:"edns0,omitempty"`
|
||||
Alphabet string `protobuf:"bytes,6,opt,name=alphabet,proto3" json:"alphabet,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
@@ -100,6 +101,13 @@ func (x *DomainProto) GetEdns0() int32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *DomainProto) GetAlphabet() string {
|
||||
if x != nil {
|
||||
return x.Alphabet
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type ResolverProto struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Type string `protobuf:"bytes,1,opt,name=type,proto3" json:"type,omitempty"`
|
||||
@@ -240,14 +248,15 @@ var File_transport_internet_finalmask_xdns_config_proto protoreflect.FileDescrip
|
||||
|
||||
const file_transport_internet_finalmask_xdns_config_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
".transport/internet/finalmask/xdns/config.proto\x12&xray.transport.internet.finalmask.xdns\x1a!common/serial/typed_message.proto\x1a#transport/internet/tls/config.proto\x1a\x1ftransport/internet/config.proto\"\x8b\x01\n" +
|
||||
".transport/internet/finalmask/xdns/config.proto\x12&xray.transport.internet.finalmask.xdns\x1a!common/serial/typed_message.proto\x1a#transport/internet/tls/config.proto\x1a\x1ftransport/internet/config.proto\"\xa7\x01\n" +
|
||||
"\vDomainProto\x12\x12\n" +
|
||||
"\x04name\x18\x01 \x01(\tR\x04name\x12\x1b\n" +
|
||||
"\tlen_limit\x18\x02 \x01(\x05R\blenLimit\x12\x1f\n" +
|
||||
"\vlabel_limit\x18\x03 \x01(\x05R\n" +
|
||||
"labelLimit\x12\x14\n" +
|
||||
"\x05types\x18\x04 \x03(\x05R\x05types\x12\x14\n" +
|
||||
"\x05edns0\x18\x05 \x01(\x05R\x05edns0\"\xd9\x01\n" +
|
||||
"\x05edns0\x18\x05 \x01(\x05R\x05edns0\x12\x1a\n" +
|
||||
"\balphabet\x18\x06 \x01(\tR\balphabet\"\xd9\x01\n" +
|
||||
"\rResolverProto\x12\x12\n" +
|
||||
"\x04type\x18\x01 \x01(\tR\x04type\x12\x12\n" +
|
||||
"\x04addr\x18\x02 \x01(\tR\x04addr\x12\x16\n" +
|
||||
|
||||
@@ -16,6 +16,7 @@ message DomainProto {
|
||||
int32 label_limit = 3;
|
||||
repeated int32 types = 4;
|
||||
int32 edns0 = 5;
|
||||
string alphabet = 6;
|
||||
}
|
||||
|
||||
message ResolverProto {
|
||||
|
||||
@@ -17,14 +17,6 @@ func Lower(c byte) byte {
|
||||
return c
|
||||
}
|
||||
|
||||
func ToUpper(b []byte) {
|
||||
for i, c := range b {
|
||||
if c >= 'a' && c <= 'z' {
|
||||
b[i] = c - 'a' + 'A'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ToLower(b []byte) {
|
||||
for i, c := range b {
|
||||
if c >= 'A' && c <= 'Z' {
|
||||
@@ -52,7 +44,7 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
base32Encoding = base32.StdEncoding.WithPadding(base32.NoPadding)
|
||||
base32Encoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding)
|
||||
table, table_ = NewTable()
|
||||
TypeMap = map[uint16]byte{
|
||||
TypeA: 0,
|
||||
@@ -74,12 +66,13 @@ type Domain struct {
|
||||
labelLimit int
|
||||
types []uint16
|
||||
edns0 uint16
|
||||
encoding *base32.Encoding
|
||||
|
||||
cap int
|
||||
lenMax int
|
||||
}
|
||||
|
||||
func NewDomain(domain string, lenLimit int, labelLimit int, types []uint16, edns0 uint16) (*Domain, error) {
|
||||
func NewDomain(domain string, lenLimit int, labelLimit int, types []uint16, edns0 uint16, alphabet string) (*Domain, error) {
|
||||
if strings.Contains(domain, "..") {
|
||||
return nil, errors.New("invalid domain")
|
||||
}
|
||||
@@ -102,6 +95,20 @@ func NewDomain(domain string, lenLimit int, labelLimit int, types []uint16, edns
|
||||
if edns0 != 0 && (edns0 < 512 || edns0 > 4096) {
|
||||
return nil, errors.New("edns0 != 0 && (edns0 < 512 || edns0 > 4096)")
|
||||
}
|
||||
encoding := base32Encoding
|
||||
if alphabet != "" {
|
||||
alphabet = strings.ToLower(alphabet)
|
||||
if len(alphabet) != 32 {
|
||||
return nil, errors.New("len(alphabet) != 32")
|
||||
}
|
||||
for i := range len(alphabet) {
|
||||
c := alphabet[i]
|
||||
if !(c >= 'a' && c <= 'z' || c >= '0' && c <= '9') || strings.IndexByte(alphabet[:i], c) >= 0 {
|
||||
return nil, errors.New("invalid alphabet")
|
||||
}
|
||||
}
|
||||
encoding = base32.NewEncoding(alphabet).WithPadding(base32.NoPadding)
|
||||
}
|
||||
|
||||
ascii, err := idna.ToASCII(domain)
|
||||
if err != nil {
|
||||
@@ -145,6 +152,7 @@ func NewDomain(domain string, lenLimit int, labelLimit int, types []uint16, edns
|
||||
labelLimit: labelLimit,
|
||||
types: types,
|
||||
edns0: edns0,
|
||||
encoding: encoding,
|
||||
|
||||
cap: cap,
|
||||
lenMax: lenMax,
|
||||
@@ -183,8 +191,7 @@ func (d *Domain) HasType(qtype uint16) bool {
|
||||
func (d *Domain) Encode(data []byte) dnsmessage.Name {
|
||||
var name dnsmessage.Name
|
||||
var encoded [255]byte
|
||||
base32Encoding.Encode(encoded[:], data)
|
||||
ToLower(encoded[:table_[len(data)]])
|
||||
d.encoding.Encode(encoded[:], data)
|
||||
b1 := name.Data[:0]
|
||||
b2 := encoded[:table_[len(data)]]
|
||||
for len(b2) > 0 {
|
||||
@@ -213,8 +220,8 @@ func (d *Domain) Decode(decoded *[255]byte, name dnsmessage.Name) int {
|
||||
b1 = append(b1, b2[i])
|
||||
}
|
||||
}
|
||||
ToUpper(b1)
|
||||
n, err := base32Encoding.Decode(decoded[:], b1)
|
||||
ToLower(b1)
|
||||
n, err := d.encoding.Decode(decoded[:], b1)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ func NewServer(c *Config, raw net.PacketConn) (net.PacketConn, error) {
|
||||
if len(types) == 0 {
|
||||
types = []uint16{1, 5, 16, 28}
|
||||
}
|
||||
domain, err := NewDomain(c.Domains[i].Name, int(c.Domains[i].LenLimit), int(c.Domains[i].LabelLimit), types, uint16(c.Domains[i].Edns0))
|
||||
domain, err := NewDomain(c.Domains[i].Name, int(c.Domains[i].LenLimit), int(c.Domains[i].LabelLimit), types, uint16(c.Domains[i].Edns0), c.Domains[i].Alphabet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@ package xdns
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/base32"
|
||||
"fmt"
|
||||
mrand "math/rand"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/xtls/xray-core/common"
|
||||
@@ -67,7 +69,7 @@ func TestXxx(t *testing.T) {
|
||||
t.Fatal("!bytes.Equal")
|
||||
}
|
||||
|
||||
domain, _ := NewDomain("a.example.com", 200, 1, []uint16{1}, 0)
|
||||
domain, _ := NewDomain("a.example.com", 200, 1, []uint16{1}, 0, "")
|
||||
fmt.Println(domain.cap, domain.lenMax)
|
||||
lenMax := domain.lenMax
|
||||
data := make([]byte, domain.cap)
|
||||
@@ -206,3 +208,40 @@ func TestTXT(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlphabet(t *testing.T) {
|
||||
data := make([]byte, 17)
|
||||
common.Must2(rand.Read(data))
|
||||
|
||||
domain := common.Must2(NewDomain("example.com", 255, 63, []uint16{16}, 0, ""))
|
||||
if domain.Encode(data).String() != strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(data))+".example.com." {
|
||||
t.Fatal("default")
|
||||
}
|
||||
|
||||
for _, alphabet := range []string{"", "0123456789ABCDEFGHIJKLMNOPQRSTUV", "v9u8t0s1rqponmlkjihgfedcba765432"} {
|
||||
domain := common.Must2(NewDomain("example.com", 255, 63, []uint16{16}, 0, alphabet))
|
||||
name := domain.Encode(data)
|
||||
for i := 0; i < int(name.Length); i += 2 {
|
||||
if c := name.Data[i]; c >= 'a' && c <= 'z' {
|
||||
name.Data[i] = c - 'a' + 'A'
|
||||
}
|
||||
}
|
||||
var decoded [255]byte
|
||||
n := domain.Decode(&decoded, name)
|
||||
if n != len(data) || !bytes.Equal(decoded[:n], data) {
|
||||
t.Fatal(alphabet)
|
||||
}
|
||||
}
|
||||
|
||||
for _, alphabet := range []string{
|
||||
"abcdefghijklmnopqrstuvwxyz23456",
|
||||
"abcdefghijklmnopqrstuvwxyz2345678",
|
||||
"abcdefghijklmnopqrstuvwxyz23456A",
|
||||
"abcdefghijklmnopqrstuvwxyz23456-",
|
||||
"abcdefghijklmnopqrstuvwxyz23456=",
|
||||
} {
|
||||
if _, err := NewDomain("example.com", 255, 63, []uint16{16}, 0, alphabet); err == nil {
|
||||
t.Fatal(alphabet)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user