Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: custom_nameservers should be dns_servers #46

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 79 additions & 9 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,88 @@ export function ip16Str(base64str: string) {
return ipv6BinaryToString(buf);
}

function ipv6BinaryToString(binary: Uint8Array) {
let parts: string[] = [];
for (let i = 0; i < binary.length; i += 2) {
let hex = (binary[i] << 8 | binary[i + 1]).toString(16);
parts.push(hex);
const digits = '0123456789abcdef';

function appendHex(b: string[], x: number): void {
if (x >= 0x1000) {
b.push(digits[(x >> 12) & 0xf]);
}
if (x >= 0x100) {
b.push(digits[(x >> 8) & 0xf]);
}
if (x >= 0x10) {
b.push(digits[(x >> 4) & 0xf]);
}
b.push(digits[x & 0xf]);
}

let ipv6 = parts.join(':');
function ipv6BinaryToString(ip: Uint8Array): string {
let ipBytes: Uint8Array;

ipv6 = ipv6.replace(/(:0)+$/, '');
if (ipv6.indexOf('::') === -1 && parts.filter(p => p === '0').length > 1) {
ipv6 = ipv6.replace(/(:0)+/, '::');
if (ip.length !== 16) {
ipBytes = new Uint8Array(16);
const len = Math.min(ip.length, 16);
ipBytes.set(ip.subarray(0, len));
} else {
ipBytes = ip;
}

const hextets: number[] = [];
for (let i = 0; i < 16; i += 2) {
hextets.push((ipBytes[i] << 8) | ipBytes[i + 1]);
}

let zeroStart = -1;
let zeroLength = 0;

for (let i = 0; i <= hextets.length;) {
let j = i;
while (j < hextets.length && hextets[j] === 0) {
j++;
}
const length = j - i;
if (length >= 2 && length > zeroLength) {
zeroStart = i;
zeroLength = length;
}
if (j === i) {
i++;
} else {
i = j;
}
}

const parts: string[] = [];
for (let i = 0; i < hextets.length; i++) {
if (zeroLength > 0 && i === zeroStart) {
parts.push('');
i += zeroLength - 1;
continue;
}

if (parts.length > 0) {
parts.push(':');
}

const b: string[] = [];
appendHex(b, hextets[i]);
parts.push(b.join(''));
}

let ipv6 = parts.join('');

if (ipv6.startsWith('::')) {

} else if (ipv6.startsWith(':')) {
ipv6 = ':' + ipv6;
}
if (ipv6.endsWith('::')) {

} else if (ipv6.endsWith(':')) {
ipv6 = ipv6 + ':';
}
if (ipv6 === '') {
ipv6 = '::';
}

return ipv6;
Expand Down
4 changes: 2 additions & 2 deletions src/routes/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
import { useTranslation } from "react-i18next";

const settingFormSchema = z.object({
custom_nameservers: asOptionalField(z.string()),
dns_servers: asOptionalField(z.string()),
ignored_ip_notification: asOptionalField(z.string()),
ip_change_notification_group_id: z.coerce.number().int().min(0),
cover: z.coerce.number().int().min(1),
Expand Down Expand Up @@ -285,7 +285,7 @@ export default function SettingsPage() {
/>
<FormField
control={form.control}
name="custom_nameservers"
name="dns_servers"
render={({ field }) => (
<FormItem>
<FormLabel>
Expand Down
Loading