mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-29 06:27:14 +00:00
fix: stop forcing port 53 on DoH/DoQ DNS server entries (#5950)
Object-form DNS server entries always received port: 53, because DnsServerObjectInnerSchema defaulted the port unconditionally and the DnsServerModal wire adapter always wrote it. Per Xray-core, encrypted schemes must not carry a port field; a non-standard port is embedded in the URL instead. Default the port to 53 only for non-encrypted addresses and omit it for the encrypted DNS schemes Xray dispatches without a port - https, https+local, h2c, h2c+local and quic+local - both in the Zod schema and in the modal's valuesToWire adapter. Schemes are matched case-insensitively to mirror Xray-core's EqualFold comparison. A shared isEncryptedDnsAddress helper backs both paths. Fixes #5920 Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
|||||||
DnsQueryStrategySchema,
|
DnsQueryStrategySchema,
|
||||||
DnsServerObjectInnerSchema,
|
DnsServerObjectInnerSchema,
|
||||||
DnsServerObjectSchema,
|
DnsServerObjectSchema,
|
||||||
|
isEncryptedDnsAddress,
|
||||||
type DnsServerObject,
|
type DnsServerObject,
|
||||||
} from '@/schemas/dns';
|
} from '@/schemas/dns';
|
||||||
|
|
||||||
@@ -109,7 +110,6 @@ function valuesToWire(values: DnsServerForm): DnsServerValue {
|
|||||||
|
|
||||||
const out: Record<string, unknown> = {
|
const out: Record<string, unknown> = {
|
||||||
address: values.address,
|
address: values.address,
|
||||||
port: values.port,
|
|
||||||
domains: values.domains.filter(Boolean),
|
domains: values.domains.filter(Boolean),
|
||||||
expectedIPs: values.expectedIPs.filter(Boolean),
|
expectedIPs: values.expectedIPs.filter(Boolean),
|
||||||
unexpectedIPs: values.unexpectedIPs.filter(Boolean),
|
unexpectedIPs: values.unexpectedIPs.filter(Boolean),
|
||||||
@@ -121,6 +121,7 @@ function valuesToWire(values: DnsServerForm): DnsServerValue {
|
|||||||
serveExpiredTTL: values.serveExpiredTTL,
|
serveExpiredTTL: values.serveExpiredTTL,
|
||||||
timeoutMs: values.timeoutMs,
|
timeoutMs: values.timeoutMs,
|
||||||
};
|
};
|
||||||
|
if (!isEncryptedDnsAddress(values.address)) out.port = values.port;
|
||||||
if (values.tag) out.tag = values.tag;
|
if (values.tag) out.tag = values.tag;
|
||||||
if (values.clientIP) out.clientIP = values.clientIP;
|
if (values.clientIP) out.clientIP = values.clientIP;
|
||||||
return out as DnsServerValue;
|
return out as DnsServerValue;
|
||||||
|
|||||||
@@ -14,9 +14,13 @@ const DnsHostValueSchema = z.union([z.string(), z.array(z.string())]);
|
|||||||
export const DnsHostsSchema = z.record(z.string(), DnsHostValueSchema);
|
export const DnsHostsSchema = z.record(z.string(), DnsHostValueSchema);
|
||||||
export type DnsHosts = z.infer<typeof DnsHostsSchema>;
|
export type DnsHosts = z.infer<typeof DnsHostsSchema>;
|
||||||
|
|
||||||
|
export function isEncryptedDnsAddress(address: string): boolean {
|
||||||
|
return /^(https|https\+local|h2c|h2c\+local|quic\+local):\/\//i.test(address);
|
||||||
|
}
|
||||||
|
|
||||||
export const DnsServerObjectInnerSchema = z.object({
|
export const DnsServerObjectInnerSchema = z.object({
|
||||||
address: z.string(),
|
address: z.string(),
|
||||||
port: PortSchema.default(53),
|
port: PortSchema.optional(),
|
||||||
domains: z.array(z.string()).optional(),
|
domains: z.array(z.string()).optional(),
|
||||||
expectedIPs: z.array(z.string()).optional(),
|
expectedIPs: z.array(z.string()).optional(),
|
||||||
unexpectedIPs: z.array(z.string()).optional(),
|
unexpectedIPs: z.array(z.string()).optional(),
|
||||||
@@ -41,7 +45,12 @@ export const DnsServerObjectSchema = z.preprocess(
|
|||||||
return val;
|
return val;
|
||||||
},
|
},
|
||||||
DnsServerObjectInnerSchema,
|
DnsServerObjectInnerSchema,
|
||||||
);
|
).transform((v) => {
|
||||||
|
if (v.port === undefined && !isEncryptedDnsAddress(v.address)) {
|
||||||
|
return { ...v, port: 53 };
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
});
|
||||||
export type DnsServerObject = z.infer<typeof DnsServerObjectSchema>;
|
export type DnsServerObject = z.infer<typeof DnsServerObjectSchema>;
|
||||||
|
|
||||||
export const DnsServerEntrySchema = z.union([z.string(), DnsServerObjectSchema]);
|
export const DnsServerEntrySchema = z.union([z.string(), DnsServerObjectSchema]);
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ exports[`DnsObjectSchema fixtures > parses full byte-stably 1`] = `
|
|||||||
"address": "quic+local://dns.adguard.com",
|
"address": "quic+local://dns.adguard.com",
|
||||||
"disableCache": true,
|
"disableCache": true,
|
||||||
"finalQuery": true,
|
"finalQuery": true,
|
||||||
"port": 53,
|
|
||||||
"serveExpiredTTL": 60,
|
"serveExpiredTTL": 60,
|
||||||
"serveStale": false,
|
"serveStale": false,
|
||||||
"timeoutMs": 5000,
|
"timeoutMs": 5000,
|
||||||
|
|||||||
@@ -41,3 +41,45 @@ describe('DnsServerObjectSchema fixtures', () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('DnsServerObjectSchema port defaulting', () => {
|
||||||
|
it('defaults port 53 for a plain address', () => {
|
||||||
|
const parsed = DnsServerObjectSchema.parse({ address: '8.8.8.8' });
|
||||||
|
expect(parsed.port).toBe(53);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('defaults port 53 for a tcp address', () => {
|
||||||
|
const parsed = DnsServerObjectSchema.parse({ address: 'tcp://1.1.1.1' });
|
||||||
|
expect(parsed.port).toBe(53);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('omits port for a DoH (https://) address', () => {
|
||||||
|
const parsed = DnsServerObjectSchema.parse({ address: 'https://cloudflare-dns.com/dns-query' });
|
||||||
|
expect(parsed.port).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('omits port for a DoHL (https+local://) address', () => {
|
||||||
|
const parsed = DnsServerObjectSchema.parse({ address: 'https+local://dns.google/dns-query' });
|
||||||
|
expect(parsed.port).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('omits port for a DoQ (quic+local://) address', () => {
|
||||||
|
const parsed = DnsServerObjectSchema.parse({ address: 'quic+local://dns.adguard.com' });
|
||||||
|
expect(parsed.port).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('omits port for an h2c and h2c+local address', () => {
|
||||||
|
expect(DnsServerObjectSchema.parse({ address: 'h2c://dns.example.com/dns-query' }).port).toBeUndefined();
|
||||||
|
expect(DnsServerObjectSchema.parse({ address: 'h2c+local://dns.example.com/dns-query' }).port).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('omits port for an uppercase encrypted scheme', () => {
|
||||||
|
const parsed = DnsServerObjectSchema.parse({ address: 'HTTPS://dns.google/dns-query' });
|
||||||
|
expect(parsed.port).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('preserves an explicit port on an encrypted address', () => {
|
||||||
|
const parsed = DnsServerObjectSchema.parse({ address: 'https://dns.google/dns-query', port: 8443 });
|
||||||
|
expect(parsed.port).toBe(8443);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user