mirror of
https://github.com/lissy93/networking-toolbox.git
synced 2026-11-21 13:34:06 +03:00
DHCP Option 119 - Domain Search List
This commit is contained in:
1 parent
1f333fea38
commit
ecd250cd80
8 files changed
+1806
-1
No files matched your search
@@ -566,6 +566,10 @@
|
||||
grid-column: span 2;
|
||||
}
|
||||
|
||||
.theme-option {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
&.theme-section,
|
||||
&.accessibility-section,
|
||||
@@ -622,7 +626,7 @@
|
||||
|
||||
.theme-option {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
gap: var(--spacing-xs);
|
||||
padding: var(--spacing-sm);
|
||||
|
||||
@@ -0,0 +1,894 @@
|
||||
<script lang="ts">
|
||||
import { untrack } from 'svelte';
|
||||
import Icon from '$lib/components/global/Icon.svelte';
|
||||
import ToolContentContainer from '$lib/components/global/ToolContentContainer.svelte';
|
||||
import ExamplesCard from '$lib/components/common/ExamplesCard.svelte';
|
||||
import { useClipboard } from '$lib/composables';
|
||||
import {
|
||||
buildOption119,
|
||||
parseOption119,
|
||||
getDefaultOption119Config,
|
||||
type DomainSearchConfig,
|
||||
type DomainSearchResult,
|
||||
type ParsedDomainSearch,
|
||||
} from '$lib/utils/dhcp-option119.js';
|
||||
|
||||
const modeOptions = [
|
||||
{ value: 'encode' as const, label: 'Encode', icon: 'wrench' },
|
||||
{ value: 'decode' as const, label: 'Decode', icon: 'search' },
|
||||
];
|
||||
|
||||
let mode = $state<'encode' | 'decode'>('encode');
|
||||
let config = $state<DomainSearchConfig>({
|
||||
...getDefaultOption119Config(),
|
||||
network: {
|
||||
subnet: '',
|
||||
netmask: '',
|
||||
rangeStart: '',
|
||||
rangeEnd: '',
|
||||
},
|
||||
});
|
||||
let result = $state<DomainSearchResult | null>(null);
|
||||
let decodeInput = $state<string>('');
|
||||
let decodeResult = $state<ParsedDomainSearch | null>(null);
|
||||
let validationErrors = $state<string[]>([]);
|
||||
let networkValidationErrors = $state<string[]>([]);
|
||||
let selectedExampleIndex = $state<number | null>(null);
|
||||
|
||||
const clipboard = useClipboard();
|
||||
|
||||
interface EncodeExample {
|
||||
label: string;
|
||||
domains: string[];
|
||||
description: string;
|
||||
}
|
||||
|
||||
interface DecodeExample {
|
||||
label: string;
|
||||
hexInput: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
const encodeExamples: EncodeExample[] = [
|
||||
{
|
||||
label: 'Corporate',
|
||||
domains: ['corp.example.com', 'example.com'],
|
||||
description: 'Corporate network with domain compression',
|
||||
},
|
||||
{
|
||||
label: 'Multi-site',
|
||||
domains: ['site1.example.com', 'site2.example.com', 'example.com'],
|
||||
description: 'Multiple sites sharing common suffix',
|
||||
},
|
||||
{
|
||||
label: 'Development',
|
||||
domains: ['dev.example.com', 'staging.example.com', 'example.com'],
|
||||
description: 'Development environments',
|
||||
},
|
||||
];
|
||||
|
||||
const decodeExamples: DecodeExample[] = [
|
||||
{
|
||||
label: 'Corporate',
|
||||
hexInput: '04636f7270076578616d706c6503636f6d00c005',
|
||||
description: 'corp.example.com, example.com (with compression)',
|
||||
},
|
||||
{
|
||||
label: 'Multi-site',
|
||||
hexInput: '057369746531076578616d706c6503636f6d00057369746532c006c006',
|
||||
description: 'site1.example.com, site2.example.com, example.com',
|
||||
},
|
||||
{
|
||||
label: 'Single Domain',
|
||||
hexInput: '076578616d706c6503636f6d00',
|
||||
description: 'example.com (no compression)',
|
||||
},
|
||||
];
|
||||
|
||||
// Reactive generation - use untrack to prevent infinite loop
|
||||
$effect(() => {
|
||||
if (mode === 'encode') {
|
||||
// Track config and all its nested properties
|
||||
const currentDomains = [...config.domains];
|
||||
const currentNetwork = config.network ? { ...config.network } : undefined;
|
||||
|
||||
untrack(() => {
|
||||
validateAndEncode({ domains: currentDomains, network: currentNetwork });
|
||||
checkIfExampleStillMatches();
|
||||
});
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||
decodeInput;
|
||||
|
||||
untrack(() => {
|
||||
checkIfExampleStillMatches();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Clear selected example when switching modes
|
||||
$effect(() => {
|
||||
void mode;
|
||||
untrack(() => {
|
||||
selectedExampleIndex = null;
|
||||
});
|
||||
});
|
||||
|
||||
function validateAndEncode(cfg: DomainSearchConfig = config) {
|
||||
const domainErrors: string[] = [];
|
||||
const netErrors: string[] = [];
|
||||
|
||||
// Validate domains
|
||||
if (cfg.domains.length === 0) {
|
||||
domainErrors.push('At least one domain is required');
|
||||
}
|
||||
|
||||
for (let i = 0; i < cfg.domains.length; i++) {
|
||||
const domain = cfg.domains[i];
|
||||
|
||||
if (!domain.trim()) {
|
||||
domainErrors.push(`Domain ${i + 1}: Value is required`);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!/^[a-zA-Z0-9.-]+$/.test(domain)) {
|
||||
domainErrors.push(`Domain ${i + 1}: Invalid characters (use only letters, numbers, dots, hyphens)`);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (domain.startsWith('.') || domain.endsWith('.')) {
|
||||
domainErrors.push(`Domain ${i + 1}: Cannot start or end with a dot`);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (domain.includes('..')) {
|
||||
domainErrors.push(`Domain ${i + 1}: Cannot contain consecutive dots`);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (domain.length > 253) {
|
||||
domainErrors.push(`Domain ${i + 1}: Exceeds maximum length of 253 characters`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const labels = domain.split('.');
|
||||
for (const label of labels) {
|
||||
if (label.length === 0) {
|
||||
domainErrors.push(`Domain ${i + 1}: Empty label found`);
|
||||
break;
|
||||
}
|
||||
if (label.length > 63) {
|
||||
domainErrors.push(`Domain ${i + 1}: Label "${label}" exceeds maximum length of 63 characters`);
|
||||
break;
|
||||
}
|
||||
if (label.startsWith('-') || label.endsWith('-')) {
|
||||
domainErrors.push(`Domain ${i + 1}: Label "${label}" cannot start or end with hyphen`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validate network settings if provided
|
||||
if (cfg.network) {
|
||||
const ipv4Regex = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
|
||||
|
||||
if (cfg.network.subnet && cfg.network.subnet.trim() && !ipv4Regex.test(cfg.network.subnet)) {
|
||||
netErrors.push('Invalid subnet address');
|
||||
}
|
||||
|
||||
if (cfg.network.netmask && cfg.network.netmask.trim() && !ipv4Regex.test(cfg.network.netmask)) {
|
||||
netErrors.push('Invalid netmask');
|
||||
}
|
||||
|
||||
if (cfg.network.rangeStart && cfg.network.rangeStart.trim() && !ipv4Regex.test(cfg.network.rangeStart)) {
|
||||
netErrors.push('Invalid range start address');
|
||||
}
|
||||
|
||||
if (cfg.network.rangeEnd && cfg.network.rangeEnd.trim() && !ipv4Regex.test(cfg.network.rangeEnd)) {
|
||||
netErrors.push('Invalid range end address');
|
||||
}
|
||||
}
|
||||
|
||||
validationErrors = domainErrors;
|
||||
networkValidationErrors = netErrors;
|
||||
|
||||
if (domainErrors.length === 0) {
|
||||
try {
|
||||
result = buildOption119(cfg);
|
||||
} catch (error) {
|
||||
validationErrors = [error instanceof Error ? error.message : 'Encoding failed'];
|
||||
result = null;
|
||||
}
|
||||
} else {
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
|
||||
function decode() {
|
||||
if (!decodeInput.trim()) {
|
||||
decodeResult = null;
|
||||
validationErrors = [];
|
||||
return;
|
||||
}
|
||||
|
||||
if (!/^[0-9a-fA-F\s:]+$/.test(decodeInput)) {
|
||||
validationErrors = ['Invalid hex input: only hexadecimal characters allowed'];
|
||||
decodeResult = null;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
validationErrors = [];
|
||||
decodeResult = parseOption119(decodeInput);
|
||||
} catch (error) {
|
||||
validationErrors = [error instanceof Error ? error.message : 'Decoding failed'];
|
||||
decodeResult = null;
|
||||
}
|
||||
}
|
||||
|
||||
function addDomain() {
|
||||
config.domains = [...config.domains, ''];
|
||||
}
|
||||
|
||||
function removeDomain(index: number) {
|
||||
if (config.domains.length > 1) {
|
||||
config.domains = config.domains.filter((_, i) => i !== index);
|
||||
}
|
||||
}
|
||||
|
||||
function loadEncodeExample(example: EncodeExample, index: number) {
|
||||
config = {
|
||||
domains: [...example.domains],
|
||||
network: {
|
||||
subnet: '',
|
||||
netmask: '',
|
||||
rangeStart: '',
|
||||
rangeEnd: '',
|
||||
},
|
||||
};
|
||||
selectedExampleIndex = index;
|
||||
}
|
||||
|
||||
function loadDecodeExample(example: DecodeExample, index: number) {
|
||||
decodeInput = example.hexInput;
|
||||
selectedExampleIndex = index;
|
||||
decode();
|
||||
}
|
||||
|
||||
function checkIfExampleStillMatches() {
|
||||
if (selectedExampleIndex === null) return;
|
||||
|
||||
if (mode === 'encode') {
|
||||
const example = encodeExamples[selectedExampleIndex];
|
||||
if (!example) {
|
||||
selectedExampleIndex = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if current config matches the selected example
|
||||
const matches =
|
||||
config.domains.length === example.domains.length &&
|
||||
config.domains.every((_domain, i) => _domain === example.domains[i]);
|
||||
|
||||
if (!matches) {
|
||||
selectedExampleIndex = null;
|
||||
}
|
||||
} else {
|
||||
const example = decodeExamples[selectedExampleIndex];
|
||||
if (!example) {
|
||||
selectedExampleIndex = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (decodeInput !== example.hexInput) {
|
||||
selectedExampleIndex = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<ToolContentContainer
|
||||
title="DHCP Option 119 - Domain Search List"
|
||||
description="Encode and decode Domain Search List (RFC 3397/6731) to/from RFC 1035 wire format with domain compression. Generate configurations for ISC dhcpd and Kea DHCP."
|
||||
navOptions={modeOptions}
|
||||
bind:selectedNav={mode}
|
||||
>
|
||||
{#if mode === 'encode'}
|
||||
<ExamplesCard
|
||||
examples={encodeExamples}
|
||||
onSelect={loadEncodeExample}
|
||||
getLabel={(ex) => ex.label}
|
||||
getDescription={(ex) => ex.description}
|
||||
selectedIndex={selectedExampleIndex}
|
||||
/>
|
||||
{:else}
|
||||
<ExamplesCard
|
||||
examples={decodeExamples}
|
||||
onSelect={loadDecodeExample}
|
||||
getLabel={(ex) => ex.label}
|
||||
getDescription={(ex) => ex.description}
|
||||
selectedIndex={selectedExampleIndex}
|
||||
/>
|
||||
{/if}
|
||||
|
||||
{#if mode === 'encode'}
|
||||
<div class="card input-card">
|
||||
<div class="card-header">
|
||||
<h3>Domain List</h3>
|
||||
</div>
|
||||
<div class="card-content">
|
||||
{#each config.domains as _, i (`domain-${i}`)}
|
||||
<div class="domain-group">
|
||||
<div class="domain-header">
|
||||
<h4>
|
||||
<Icon name="globe" size="sm" />Domain {i + 1}
|
||||
</h4>
|
||||
<!-- <label for="domain-{i}">
|
||||
Domain Name
|
||||
</label> -->
|
||||
{#if config.domains.length > 1}
|
||||
<button type="button" class="btn-icon" onclick={() => removeDomain(i)}>
|
||||
<Icon name="x" size="sm" />
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="input-group">
|
||||
<input id="domain-{i}" type="text" bind:value={config.domains[i]} placeholder="example.com" />
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
|
||||
<button type="button" class="btn-add" onclick={addDomain}>
|
||||
<Icon name="plus" size="sm" />
|
||||
Add Domain
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if validationErrors.length > 0}
|
||||
<div class="card errors-card">
|
||||
<h3>Validation Errors</h3>
|
||||
{#each validationErrors as error, i (i)}
|
||||
<div class="error-message">
|
||||
<Icon name="alert-triangle" size="sm" />
|
||||
{error}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if result && validationErrors.length === 0}
|
||||
<div class="card results">
|
||||
<h3>Encoded Option 119</h3>
|
||||
|
||||
<div class="output-group">
|
||||
<div class="output-header">
|
||||
<h4>Hex-Encoded (Compact)</h4>
|
||||
<button
|
||||
type="button"
|
||||
class="copy-btn"
|
||||
class:copied={clipboard.isCopied('hex')}
|
||||
onclick={() => clipboard.copy(result!.hexEncoded, 'hex')}
|
||||
>
|
||||
<Icon name={clipboard.isCopied('hex') ? 'check' : 'copy'} size="xs" />
|
||||
{clipboard.isCopied('hex') ? 'Copied' : 'Copy'}
|
||||
</button>
|
||||
</div>
|
||||
<pre class="output-value code-block">{result.hexEncoded}</pre>
|
||||
</div>
|
||||
|
||||
<div class="output-group">
|
||||
<div class="output-header">
|
||||
<h4>Wire Format (Spaced)</h4>
|
||||
<button
|
||||
type="button"
|
||||
class="copy-btn"
|
||||
class:copied={clipboard.isCopied('wire')}
|
||||
onclick={() => clipboard.copy(result!.wireFormat, 'wire')}
|
||||
>
|
||||
<Icon name={clipboard.isCopied('wire') ? 'check' : 'copy'} size="xs" />
|
||||
{clipboard.isCopied('wire') ? 'Copied' : 'Copy'}
|
||||
</button>
|
||||
</div>
|
||||
<pre class="output-value code-block">{result.wireFormat}</pre>
|
||||
</div>
|
||||
|
||||
<div class="summary-card">
|
||||
<div><strong>Total Length:</strong> {result.totalLength} bytes</div>
|
||||
<div><strong>Domains:</strong> {result.domainList.length}</div>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<hr />
|
||||
|
||||
{#if result}
|
||||
<div class="card input-card">
|
||||
<div class="card-header">
|
||||
<h3>Network Settings (Optional)</h3>
|
||||
<p class="help-text">Customize network values for configuration examples below</p>
|
||||
</div>
|
||||
<div class="card-content">
|
||||
<div class="input-row">
|
||||
<div class="input-group">
|
||||
<label for="subnet">
|
||||
<Icon name="network" size="sm" />
|
||||
Subnet
|
||||
</label>
|
||||
<input id="subnet" type="text" bind:value={config.network!.subnet} placeholder="192.168.1.0" />
|
||||
</div>
|
||||
|
||||
<div class="input-group">
|
||||
<label for="netmask">
|
||||
<Icon name="network" size="sm" />
|
||||
Netmask
|
||||
</label>
|
||||
<input id="netmask" type="text" bind:value={config.network!.netmask} placeholder="255.255.255.0" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="input-row">
|
||||
<div class="input-group">
|
||||
<label for="range-start">
|
||||
<Icon name="arrow-right" size="sm" />
|
||||
Range Start
|
||||
</label>
|
||||
<input id="range-start" type="text" bind:value={config.network!.rangeStart} placeholder="192.168.1.100" />
|
||||
</div>
|
||||
|
||||
<div class="input-group">
|
||||
<label for="range-end">
|
||||
<Icon name="arrow-right" size="sm" />
|
||||
Range End
|
||||
</label>
|
||||
<input id="range-end" type="text" bind:value={config.network!.rangeEnd} placeholder="192.168.1.200" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if networkValidationErrors.length > 0}
|
||||
<div class="network-errors">
|
||||
<h4>Network Settings Errors</h4>
|
||||
{#each networkValidationErrors as error, i (i)}
|
||||
<div class="network-error-item">
|
||||
<Icon name="alert-triangle" size="sm" />
|
||||
{error}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if result && networkValidationErrors.length === 0}
|
||||
<div class="card results">
|
||||
<h3>Configuration Examples</h3>
|
||||
|
||||
{#if result.examples.iscDhcpd}
|
||||
<div class="output-group">
|
||||
<div class="output-header">
|
||||
<h4>ISC dhcpd Configuration</h4>
|
||||
<button
|
||||
type="button"
|
||||
class="copy-btn"
|
||||
class:copied={clipboard.isCopied('isc')}
|
||||
onclick={() => clipboard.copy(result!.examples.iscDhcpd!, 'isc')}
|
||||
>
|
||||
<Icon name={clipboard.isCopied('isc') ? 'check' : 'copy'} size="xs" />
|
||||
{clipboard.isCopied('isc') ? 'Copied' : 'Copy'}
|
||||
</button>
|
||||
</div>
|
||||
<pre class="output-value code-block">{result.examples.iscDhcpd}</pre>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if result.examples.keaDhcp4}
|
||||
<div class="output-group">
|
||||
<div class="output-header">
|
||||
<h4>Kea DHCPv4 Configuration</h4>
|
||||
<button
|
||||
type="button"
|
||||
class="copy-btn"
|
||||
class:copied={clipboard.isCopied('kea')}
|
||||
onclick={() => clipboard.copy(result!.examples.keaDhcp4!, 'kea')}
|
||||
>
|
||||
<Icon name={clipboard.isCopied('kea') ? 'check' : 'copy'} size="xs" />
|
||||
{clipboard.isCopied('kea') ? 'Copied' : 'Copy'}
|
||||
</button>
|
||||
</div>
|
||||
<pre class="output-value code-block">{result.examples.keaDhcp4}</pre>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
{:else}
|
||||
<div class="card input-card">
|
||||
<div class="card-header">
|
||||
<h3>Decode Option 119 Hex</h3>
|
||||
</div>
|
||||
<div class="card-content">
|
||||
<div class="input-group">
|
||||
<label for="decode-input">
|
||||
<Icon name="code" size="sm" />
|
||||
Hex-Encoded Option 119
|
||||
</label>
|
||||
<textarea
|
||||
id="decode-input"
|
||||
bind:value={decodeInput}
|
||||
placeholder="Enter hex string (e.g., 0765786d706c6503636f6d00)"
|
||||
rows="4"
|
||||
></textarea>
|
||||
</div>
|
||||
<button type="button" class="btn-primary" onclick={decode}>
|
||||
<Icon name="search" size="sm" />
|
||||
Decode
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if validationErrors.length > 0}
|
||||
<div class="card errors-card">
|
||||
<h3>Validation Errors</h3>
|
||||
{#each validationErrors as error, i (i)}
|
||||
<div class="error-message">
|
||||
<Icon name="alert-triangle" size="sm" />
|
||||
{error}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if decodeResult && validationErrors.length === 0}
|
||||
<div class="card results">
|
||||
<h3>Decoded Domain Search List</h3>
|
||||
|
||||
<div class="summary-card">
|
||||
<div><strong>Total Length:</strong> {decodeResult.totalLength} bytes</div>
|
||||
<div><strong>Domains Found:</strong> {decodeResult.domains.length}</div>
|
||||
</div>
|
||||
|
||||
<div class="domains-section">
|
||||
<h4>Domain List</h4>
|
||||
{#each decodeResult.domains as domain, i (i)}
|
||||
<div class="domain-item">
|
||||
<Icon name="globe" size="sm" />
|
||||
<span>{domain}</span>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
</ToolContentContainer>
|
||||
|
||||
<style lang="scss">
|
||||
.card {
|
||||
background: var(--bg-secondary);
|
||||
border-radius: var(--radius-lg);
|
||||
padding: var(--spacing-lg);
|
||||
border: 1px solid var(--border-primary);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
|
||||
&.input-card {
|
||||
background: var(--bg-tertiary);
|
||||
.card-header {
|
||||
margin-bottom: var(--spacing-sm);
|
||||
}
|
||||
}
|
||||
|
||||
h3 {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
margin: 0 0 var(--spacing-sm);
|
||||
font-size: 1.25rem;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
}
|
||||
|
||||
.card-header {
|
||||
.help-text {
|
||||
margin: var(--spacing-xs) 0 0;
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-secondary);
|
||||
font-style: italic;
|
||||
}
|
||||
}
|
||||
|
||||
.card-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-md);
|
||||
}
|
||||
|
||||
.domain-group {
|
||||
padding: var(--spacing-md);
|
||||
background: var(--bg-secondary);
|
||||
border-radius: var(--radius-md);
|
||||
border: 1px solid var(--border-secondary);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-md);
|
||||
}
|
||||
|
||||
.domain-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding-bottom: var(--spacing-sm);
|
||||
border-bottom: 1px solid var(--border-secondary);
|
||||
|
||||
h4 {
|
||||
margin: 0;
|
||||
font-size: 0.9375rem;
|
||||
color: var(--text-secondary);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-xs);
|
||||
}
|
||||
}
|
||||
|
||||
.input-group {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-xs);
|
||||
|
||||
label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-xs);
|
||||
font-weight: 500;
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
input,
|
||||
textarea {
|
||||
padding: var(--spacing-sm);
|
||||
background: var(--bg-primary);
|
||||
border: 1px solid var(--border-primary);
|
||||
border-radius: var(--radius-md);
|
||||
color: var(--text-primary);
|
||||
font-family: inherit;
|
||||
font-size: 0.9375rem;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:focus {
|
||||
outline: none;
|
||||
border-color: var(--color-primary);
|
||||
box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-primary), transparent 90%);
|
||||
}
|
||||
}
|
||||
|
||||
textarea {
|
||||
resize: vertical;
|
||||
font-family: var(--font-mono);
|
||||
}
|
||||
}
|
||||
|
||||
.input-row {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: var(--spacing-md);
|
||||
|
||||
@media (max-width: 768px) {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
.btn-icon {
|
||||
padding: var(--spacing-xs);
|
||||
background: transparent;
|
||||
border: 1px solid var(--border-secondary);
|
||||
border-radius: var(--radius-sm);
|
||||
color: var(--color-error);
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background: color-mix(in srgb, var(--color-error), transparent 90%);
|
||||
border-color: var(--color-error);
|
||||
}
|
||||
}
|
||||
|
||||
.btn-add,
|
||||
.btn-primary {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: var(--spacing-xs);
|
||||
padding: var(--spacing-sm);
|
||||
background: transparent;
|
||||
border: 1px dashed var(--border-secondary);
|
||||
border-radius: var(--radius-md);
|
||||
color: var(--text-secondary);
|
||||
font-size: 0.875rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background: var(--surface-hover);
|
||||
border-color: var(--color-primary);
|
||||
}
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
border-style: solid;
|
||||
background: var(--color-primary);
|
||||
color: var(--bg-primary);
|
||||
border-color: var(--color-primary);
|
||||
|
||||
&:hover {
|
||||
background: var(--color-primary-dark);
|
||||
}
|
||||
}
|
||||
|
||||
.errors-card {
|
||||
background: color-mix(in srgb, var(--color-error), transparent 95%);
|
||||
border-color: var(--color-error);
|
||||
}
|
||||
|
||||
.error-message {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
padding: var(--spacing-sm);
|
||||
color: var(--color-error);
|
||||
font-size: 0.9375rem;
|
||||
}
|
||||
|
||||
.results {
|
||||
background: var(--bg-tertiary);
|
||||
animation: slideIn 0.3s ease-out;
|
||||
}
|
||||
|
||||
@keyframes slideIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(-10px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
.output-group {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-xs);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
|
||||
&:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.output-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
|
||||
h4 {
|
||||
margin: 0;
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
}
|
||||
|
||||
.copy-btn {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-xs);
|
||||
padding: var(--spacing-xs) var(--spacing-sm);
|
||||
background: transparent;
|
||||
border: 1px solid var(--border-secondary);
|
||||
border-radius: var(--radius-sm);
|
||||
color: var(--text-secondary);
|
||||
font-size: 0.875rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background: var(--surface-hover);
|
||||
border-color: var(--color-primary);
|
||||
color: var(--color-primary);
|
||||
}
|
||||
|
||||
&.copied {
|
||||
background: color-mix(in srgb, var(--color-success), transparent 90%);
|
||||
border-color: var(--color-success);
|
||||
color: var(--color-success);
|
||||
}
|
||||
}
|
||||
|
||||
.output-value {
|
||||
padding: var(--spacing-md);
|
||||
background: var(--bg-primary);
|
||||
border: 1px solid var(--border-primary);
|
||||
border-radius: var(--radius-md);
|
||||
font-family: var(--font-mono);
|
||||
font-size: 0.875rem;
|
||||
color: var(--color-primary);
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
.code-block {
|
||||
white-space: pre;
|
||||
word-break: normal;
|
||||
}
|
||||
|
||||
.summary-card {
|
||||
display: flex;
|
||||
gap: var(--spacing-lg);
|
||||
padding: var(--spacing-md);
|
||||
background: color-mix(in srgb, var(--color-info), transparent 95%);
|
||||
border: 1px solid var(--color-info);
|
||||
border-radius: var(--radius-md);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
font-size: 0.9375rem;
|
||||
|
||||
div {
|
||||
strong {
|
||||
color: var(--text-primary);
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-xs);
|
||||
}
|
||||
}
|
||||
|
||||
.domains-section {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-md);
|
||||
|
||||
h4 {
|
||||
margin: 0 0 var(--spacing-xs);
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
}
|
||||
|
||||
.domain-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
padding: var(--spacing-sm) var(--spacing-md);
|
||||
background: var(--bg-primary);
|
||||
border: 1px solid var(--border-primary);
|
||||
border-radius: var(--radius-md);
|
||||
font-family: var(--font-mono);
|
||||
font-size: 0.9375rem;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.network-errors {
|
||||
margin-top: var(--spacing-md);
|
||||
padding: var(--spacing-md);
|
||||
background: color-mix(in srgb, var(--color-error), transparent 95%);
|
||||
border: 1px solid var(--color-error);
|
||||
border-radius: var(--radius-md);
|
||||
|
||||
h4 {
|
||||
margin: 0 0 var(--spacing-sm);
|
||||
font-size: 0.875rem;
|
||||
font-weight: 600;
|
||||
color: var(--color-error);
|
||||
}
|
||||
}
|
||||
|
||||
.network-error-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-xs);
|
||||
padding: var(--spacing-xs) 0;
|
||||
color: var(--color-error);
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
</style>
|
||||
@@ -917,6 +917,36 @@ export const SUB_NAV: Record<string, (NavItem | NavGroup)[]> = {
|
||||
'infrastructure',
|
||||
],
|
||||
},
|
||||
{
|
||||
href: makePath('/dhcp/options/option119-domain-search'),
|
||||
label: 'Option 119 - Domain Search',
|
||||
description:
|
||||
'Encode and decode Domain Search List (RFC 3397/6731) to/from RFC 1035 wire format with domain compression',
|
||||
icon: 'globe',
|
||||
keywords: [
|
||||
'dhcp',
|
||||
'option',
|
||||
'119',
|
||||
'domain',
|
||||
'search',
|
||||
'list',
|
||||
'rfc',
|
||||
'3397',
|
||||
'6731',
|
||||
'1035',
|
||||
'wire',
|
||||
'format',
|
||||
'compression',
|
||||
'encoder',
|
||||
'decoder',
|
||||
'isc',
|
||||
'kea',
|
||||
'dns',
|
||||
'suffix',
|
||||
'client',
|
||||
'configuration',
|
||||
],
|
||||
},
|
||||
],
|
||||
'/dns': [
|
||||
{
|
||||
|
||||
@@ -0,0 +1,345 @@
|
||||
/**
|
||||
* DHCP Option 119 - Domain Search List (RFC 3397 / RFC 6731)
|
||||
* Utilities for encoding/decoding domain names to/from RFC 1035 wire format
|
||||
*/
|
||||
|
||||
export interface DomainSearchConfig {
|
||||
domains: string[];
|
||||
network?: {
|
||||
subnet?: string;
|
||||
netmask?: string;
|
||||
rangeStart?: string;
|
||||
rangeEnd?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface DomainSearchResult {
|
||||
hexEncoded: string;
|
||||
wireFormat: string;
|
||||
domainList: string[];
|
||||
totalLength: number;
|
||||
examples: {
|
||||
iscDhcpd?: string;
|
||||
keaDhcp4?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ParsedDomainSearch {
|
||||
domains: string[];
|
||||
totalLength: number;
|
||||
rawHex: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode domain name to RFC 1035 wire format (without compression)
|
||||
* Example: "example.com" -> 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00
|
||||
* Note: This function is kept for reference but not currently used.
|
||||
* The encodeDomainsWithCompression function is used instead for better efficiency.
|
||||
*/
|
||||
function _encodeDomainName(domain: string): number[] {
|
||||
const labels = domain.split('.');
|
||||
const bytes: number[] = [];
|
||||
|
||||
for (const label of labels) {
|
||||
if (label.length === 0) continue;
|
||||
if (label.length > 63) {
|
||||
throw new Error(`Label "${label}" exceeds maximum length of 63 characters`);
|
||||
}
|
||||
|
||||
// Length byte
|
||||
bytes.push(label.length);
|
||||
|
||||
// Label characters
|
||||
for (let i = 0; i < label.length; i++) {
|
||||
bytes.push(label.charCodeAt(i));
|
||||
}
|
||||
}
|
||||
|
||||
// Terminating null byte
|
||||
bytes.push(0);
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode multiple domains with compression for common suffixes
|
||||
*/
|
||||
function encodeDomainsWithCompression(domains: string[]): number[] {
|
||||
const bytes: number[] = [];
|
||||
const domainOffsets: Map<string, number> = new Map();
|
||||
|
||||
for (const domain of domains) {
|
||||
const labels = domain.split('.');
|
||||
let currentOffset = bytes.length;
|
||||
|
||||
for (let i = 0; i < labels.length; i++) {
|
||||
const suffix = labels.slice(i).join('.');
|
||||
const existingOffset = domainOffsets.get(suffix);
|
||||
|
||||
if (existingOffset !== undefined && existingOffset < 16384) {
|
||||
// Use compression pointer (0xC0 | high bits, low bits)
|
||||
const pointer = 0xc000 | existingOffset;
|
||||
bytes.push((pointer >> 8) & 0xff);
|
||||
bytes.push(pointer & 0xff);
|
||||
break;
|
||||
} else {
|
||||
// Store this suffix offset for future compression
|
||||
domainOffsets.set(suffix, currentOffset);
|
||||
|
||||
const label = labels[i];
|
||||
if (label.length > 63) {
|
||||
throw new Error(`Label "${label}" exceeds maximum length of 63 characters`);
|
||||
}
|
||||
|
||||
// Length byte
|
||||
bytes.push(label.length);
|
||||
currentOffset++;
|
||||
|
||||
// Label characters
|
||||
for (let j = 0; j < label.length; j++) {
|
||||
bytes.push(label.charCodeAt(j));
|
||||
currentOffset++;
|
||||
}
|
||||
|
||||
// If this was the last label, add terminating null
|
||||
if (i === labels.length - 1) {
|
||||
bytes.push(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse RFC 1035 wire format to domain names
|
||||
*/
|
||||
function parseWireFormat(bytes: number[]): string[] {
|
||||
const domains: string[] = [];
|
||||
let offset = 0;
|
||||
|
||||
while (offset < bytes.length) {
|
||||
const domain = parseDomainAtOffset(bytes, offset);
|
||||
if (domain.name === '') break;
|
||||
|
||||
domains.push(domain.name);
|
||||
offset = domain.nextOffset;
|
||||
}
|
||||
|
||||
return domains;
|
||||
}
|
||||
|
||||
interface ParsedDomain {
|
||||
name: string;
|
||||
nextOffset: number;
|
||||
}
|
||||
|
||||
function parseDomainAtOffset(bytes: number[], startOffset: number): ParsedDomain {
|
||||
const labels: string[] = [];
|
||||
let offset = startOffset;
|
||||
const visited = new Set<number>();
|
||||
|
||||
while (offset < bytes.length) {
|
||||
if (visited.has(offset)) {
|
||||
throw new Error('Compression pointer loop detected');
|
||||
}
|
||||
visited.add(offset);
|
||||
|
||||
const length = bytes[offset];
|
||||
|
||||
// Check for compression pointer (top 2 bits set: 11xxxxxx)
|
||||
if ((length & 0xc0) === 0xc0) {
|
||||
if (offset + 1 >= bytes.length) {
|
||||
throw new Error('Invalid compression pointer');
|
||||
}
|
||||
const pointer = ((length & 0x3f) << 8) | bytes[offset + 1];
|
||||
const compressed = parseDomainAtOffset(bytes, pointer);
|
||||
labels.push(compressed.name);
|
||||
return {
|
||||
name: labels.join('.'),
|
||||
nextOffset: offset + 2,
|
||||
};
|
||||
}
|
||||
|
||||
// Terminating null byte
|
||||
if (length === 0) {
|
||||
return {
|
||||
name: labels.join('.'),
|
||||
nextOffset: offset + 1,
|
||||
};
|
||||
}
|
||||
|
||||
// Regular label
|
||||
if (offset + 1 + length > bytes.length) {
|
||||
throw new Error('Label extends beyond data');
|
||||
}
|
||||
|
||||
let label = '';
|
||||
for (let i = 1; i <= length; i++) {
|
||||
label += String.fromCharCode(bytes[offset + i]);
|
||||
}
|
||||
labels.push(label);
|
||||
offset += length + 1;
|
||||
}
|
||||
|
||||
return {
|
||||
name: labels.join('.'),
|
||||
nextOffset: offset,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Build Option 119 hex string from domain list
|
||||
*/
|
||||
export function buildOption119(config: DomainSearchConfig): DomainSearchResult {
|
||||
// Validate domains
|
||||
for (const domain of config.domains) {
|
||||
if (!domain.trim()) {
|
||||
throw new Error('Empty domain name not allowed');
|
||||
}
|
||||
if (!/^[a-zA-Z0-9.-]+$/.test(domain)) {
|
||||
throw new Error(`Invalid domain name: ${domain}`);
|
||||
}
|
||||
// Check for leading/trailing dots or consecutive dots
|
||||
if (domain.startsWith('.') || domain.endsWith('.') || domain.includes('..')) {
|
||||
throw new Error(`Invalid domain name format: ${domain}`);
|
||||
}
|
||||
}
|
||||
|
||||
const bytes = encodeDomainsWithCompression(config.domains);
|
||||
const hexEncoded = bytes.map((b) => b.toString(16).padStart(2, '0')).join('');
|
||||
const wireFormat = bytes.map((b) => b.toString(16).padStart(2, '0')).join(' ');
|
||||
|
||||
const examples = generateExamples(config, hexEncoded);
|
||||
|
||||
return {
|
||||
hexEncoded,
|
||||
wireFormat,
|
||||
domainList: config.domains,
|
||||
totalLength: bytes.length,
|
||||
examples,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse Option 119 hex string
|
||||
*/
|
||||
export function parseOption119(hexString: string): ParsedDomainSearch {
|
||||
const cleanHex = hexString.replace(/[^0-9a-fA-F]/g, '');
|
||||
|
||||
if (cleanHex.length === 0) {
|
||||
throw new Error('Empty hex string');
|
||||
}
|
||||
|
||||
if (cleanHex.length % 2 !== 0) {
|
||||
throw new Error('Invalid hex string: odd number of characters');
|
||||
}
|
||||
|
||||
const bytes: number[] = [];
|
||||
for (let i = 0; i < cleanHex.length; i += 2) {
|
||||
bytes.push(parseInt(cleanHex.substring(i, i + 2), 16));
|
||||
}
|
||||
|
||||
try {
|
||||
const domains = parseWireFormat(bytes);
|
||||
|
||||
return {
|
||||
domains,
|
||||
totalLength: bytes.length,
|
||||
rawHex: cleanHex,
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
throw new Error(`Invalid wire format: ${error.message}`);
|
||||
}
|
||||
throw new Error('Invalid wire format data');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate configuration examples for different DHCP servers
|
||||
*/
|
||||
function generateExamples(config: DomainSearchConfig, hexEncoded: string): DomainSearchResult['examples'] {
|
||||
const examples: DomainSearchResult['examples'] = {};
|
||||
|
||||
// Use network settings or defaults
|
||||
const subnet = config.network?.subnet?.trim() || '192.168.1.0';
|
||||
const netmask = config.network?.netmask?.trim() || '255.255.255.0';
|
||||
const rangeStart = config.network?.rangeStart?.trim() || '192.168.1.100';
|
||||
const rangeEnd = config.network?.rangeEnd?.trim() || '192.168.1.200';
|
||||
|
||||
// Calculate CIDR notation from netmask
|
||||
const cidrBits =
|
||||
netmask
|
||||
.split('.')
|
||||
.map((octet) => parseInt(octet, 10).toString(2).padStart(8, '0'))
|
||||
.join('')
|
||||
.split('1').length - 1;
|
||||
const cidr = `${subnet}/${cidrBits}`;
|
||||
|
||||
// ISC dhcpd example
|
||||
const iscLines: string[] = [];
|
||||
iscLines.push('# Domain Search List (Option 119)');
|
||||
iscLines.push('# RFC 3397 - domain search list for DHCP clients');
|
||||
iscLines.push('');
|
||||
iscLines.push(`subnet ${subnet} netmask ${netmask} {`);
|
||||
iscLines.push(` range ${rangeStart} ${rangeEnd};`);
|
||||
iscLines.push(' ');
|
||||
iscLines.push(' # Specify domain search list');
|
||||
iscLines.push(` option domain-search "${config.domains.join('", "')}";`);
|
||||
iscLines.push(' ');
|
||||
iscLines.push(' # Alternative: Hex format');
|
||||
iscLines.push(` # option domain-search ${hexEncoded.match(/.{1,2}/g)?.join(':')};`);
|
||||
iscLines.push('}');
|
||||
|
||||
examples.iscDhcpd = iscLines.join('\n');
|
||||
|
||||
// Kea DHCPv4 example
|
||||
const keaLines: string[] = [];
|
||||
keaLines.push('// Domain Search List (Option 119)');
|
||||
keaLines.push('// RFC 3397 - domain search list configuration');
|
||||
keaLines.push('{');
|
||||
keaLines.push(' "Dhcp4": {');
|
||||
keaLines.push(' "subnet4": [');
|
||||
keaLines.push(' {');
|
||||
keaLines.push(` "subnet": "${cidr}",`);
|
||||
keaLines.push(' "pools": [');
|
||||
keaLines.push(' {');
|
||||
keaLines.push(` "pool": "${rangeStart} - ${rangeEnd}"`);
|
||||
keaLines.push(' }');
|
||||
keaLines.push(' ],');
|
||||
keaLines.push(' "option-data": [');
|
||||
keaLines.push(' {');
|
||||
keaLines.push(' "name": "domain-search",');
|
||||
keaLines.push(' "code": 119,');
|
||||
keaLines.push(` "data": "${config.domains.join(', ')}"`);
|
||||
keaLines.push(' }');
|
||||
keaLines.push(' ]');
|
||||
keaLines.push(' }');
|
||||
keaLines.push(' ]');
|
||||
keaLines.push(' }');
|
||||
keaLines.push('}');
|
||||
|
||||
examples.keaDhcp4 = keaLines.join('\n');
|
||||
|
||||
return examples;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default config
|
||||
*/
|
||||
export function getDefaultOption119Config(): DomainSearchConfig {
|
||||
return {
|
||||
domains: ['example.com', 'corp.example.com'],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Common domain search examples
|
||||
*/
|
||||
export const DOMAIN_SEARCH_EXAMPLES = [
|
||||
{ label: 'Corporate', domains: ['corp.example.com', 'example.com'] },
|
||||
{ label: 'Multi-site', domains: ['site1.example.com', 'site2.example.com', 'example.com'] },
|
||||
{ label: 'Development', domains: ['dev.example.com', 'staging.example.com', 'example.com'] },
|
||||
];
|
||||
@@ -0,0 +1,41 @@
|
||||
<script lang="ts">
|
||||
import { SUB_NAV } from '$lib/constants/nav';
|
||||
import ToolsGrid from '$lib/components/global/ToolsGrid.svelte';
|
||||
import type { NavItem, NavGroup } from '$lib/constants/nav';
|
||||
import '../../styles/pages.scss';
|
||||
|
||||
// Extract tools for DHCP section (excluding category headers with title)
|
||||
function extractNavItems(items: (NavItem | NavGroup)[]): NavItem[] {
|
||||
const navItems: NavItem[] = [];
|
||||
for (const item of items) {
|
||||
if ('items' in item) {
|
||||
// It's a category - extract its children recursively
|
||||
navItems.push(...extractNavItems(item.items));
|
||||
} else if ('href' in item && 'label' in item && !('title' in item)) {
|
||||
// It's a tool (has href and label but no title)
|
||||
navItems.push(item as NavItem);
|
||||
}
|
||||
}
|
||||
return navItems;
|
||||
}
|
||||
|
||||
const dhcpTools = extractNavItems(SUB_NAV['/dhcp'] || []);
|
||||
</script>
|
||||
|
||||
<div class="page-container">
|
||||
<header class="page-header">
|
||||
<h1>DHCP Tools & Option Generators</h1>
|
||||
<p class="page-description">
|
||||
DHCP configuration tools for network administrators. Generate vendor-specific options, build relay agent
|
||||
information, create class-based policies, and configure DHCP servers with complete subnet snippets.
|
||||
</p>
|
||||
</header>
|
||||
|
||||
<ToolsGrid tools={dhcpTools} />
|
||||
</div>
|
||||
|
||||
<style>
|
||||
header {
|
||||
margin-bottom: var(--spacing-md);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,5 @@
|
||||
<script lang="ts">
|
||||
import DHCPOption119Builder from '$lib/components/tools/DHCPOption119Builder.svelte';
|
||||
</script>
|
||||
|
||||
<DHCPOption119Builder />
|
||||
@@ -184,6 +184,9 @@ pre {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
.icon {
|
||||
display: flex;
|
||||
}
|
||||
}
|
||||
|
||||
.logo-icon svg {
|
||||
|
||||
@@ -0,0 +1,483 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
buildOption119,
|
||||
parseOption119,
|
||||
getDefaultOption119Config,
|
||||
DOMAIN_SEARCH_EXAMPLES,
|
||||
type DomainSearchConfig,
|
||||
} from '../../../src/lib/utils/dhcp-option119';
|
||||
|
||||
describe('dhcp-option119.ts', () => {
|
||||
describe('buildOption119', () => {
|
||||
describe('basic encoding', () => {
|
||||
it('encodes a single domain without compression', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['example.com'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
|
||||
// "example.com" -> 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00
|
||||
expect(result.hexEncoded).toBe('076578616d706c6503636f6d00');
|
||||
expect(result.domainList).toEqual(['example.com']);
|
||||
expect(result.totalLength).toBe(13);
|
||||
});
|
||||
|
||||
it('encodes multiple domains with compression', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['corp.example.com', 'example.com'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
|
||||
// First domain: "corp.example.com" fully encoded
|
||||
// Second domain: uses compression pointer to "example.com"
|
||||
expect(result.hexEncoded).toBe('04636f7270076578616d706c6503636f6d00c005');
|
||||
expect(result.domainList).toEqual(['corp.example.com', 'example.com']);
|
||||
});
|
||||
|
||||
it('encodes complex multi-site domains', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['site1.example.com', 'site2.example.com', 'example.com'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
|
||||
// All three domains with appropriate compression
|
||||
expect(result.hexEncoded).toBe('057369746531076578616d706c6503636f6d00057369746532c006c006');
|
||||
expect(result.domainList).toEqual(['site1.example.com', 'site2.example.com', 'example.com']);
|
||||
});
|
||||
|
||||
it('provides wire format with spaces', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['example.com'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
|
||||
expect(result.wireFormat).toBe('07 65 78 61 6d 70 6c 65 03 63 6f 6d 00');
|
||||
});
|
||||
});
|
||||
|
||||
describe('validation', () => {
|
||||
it('rejects empty domain names', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: [''],
|
||||
};
|
||||
|
||||
expect(() => buildOption119(config)).toThrow('Empty domain name not allowed');
|
||||
});
|
||||
|
||||
it('rejects domains with invalid characters', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['example@test.com'],
|
||||
};
|
||||
|
||||
expect(() => buildOption119(config)).toThrow('Invalid domain name');
|
||||
});
|
||||
|
||||
it('rejects domains with leading dots', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['.example.com'],
|
||||
};
|
||||
|
||||
expect(() => buildOption119(config)).toThrow('Invalid domain name format');
|
||||
});
|
||||
|
||||
it('rejects domains with trailing dots', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['example.com.'],
|
||||
};
|
||||
|
||||
expect(() => buildOption119(config)).toThrow('Invalid domain name format');
|
||||
});
|
||||
|
||||
it('rejects domains with consecutive dots', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['example..com'],
|
||||
};
|
||||
|
||||
expect(() => buildOption119(config)).toThrow('Invalid domain name format');
|
||||
});
|
||||
|
||||
it('rejects labels longer than 63 characters', () => {
|
||||
const longLabel = 'a'.repeat(64);
|
||||
const config: DomainSearchConfig = {
|
||||
domains: [`${longLabel}.example.com`],
|
||||
};
|
||||
|
||||
expect(() => buildOption119(config)).toThrow('exceeds maximum length of 63 characters');
|
||||
});
|
||||
|
||||
it('accepts labels with exactly 63 characters', () => {
|
||||
const maxLabel = 'a'.repeat(63);
|
||||
const config: DomainSearchConfig = {
|
||||
domains: [`${maxLabel}.com`],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
expect(result.domainList).toEqual([`${maxLabel}.com`]);
|
||||
});
|
||||
|
||||
it('accepts valid domain names with hyphens', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['my-domain.example-site.com'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
expect(result.domainList).toEqual(['my-domain.example-site.com']);
|
||||
});
|
||||
|
||||
it('accepts valid domain names with numbers', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['site123.example.com'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
expect(result.domainList).toEqual(['site123.example.com']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('configuration examples', () => {
|
||||
it('generates ISC dhcpd configuration', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['example.com', 'corp.example.com'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
|
||||
expect(result.examples.iscDhcpd).toBeDefined();
|
||||
expect(result.examples.iscDhcpd).toContain('option domain-search');
|
||||
expect(result.examples.iscDhcpd).toContain('example.com');
|
||||
expect(result.examples.iscDhcpd).toContain('corp.example.com');
|
||||
});
|
||||
|
||||
it('generates Kea DHCP configuration', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['example.com', 'corp.example.com'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
|
||||
expect(result.examples.keaDhcp4).toBeDefined();
|
||||
expect(result.examples.keaDhcp4).toContain('"name": "domain-search"');
|
||||
expect(result.examples.keaDhcp4).toContain('"code": 119');
|
||||
expect(result.examples.keaDhcp4).toContain('example.com');
|
||||
});
|
||||
|
||||
it('uses default network settings when not provided', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['example.com'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
|
||||
expect(result.examples.iscDhcpd).toContain('192.168.1.0');
|
||||
expect(result.examples.iscDhcpd).toContain('255.255.255.0');
|
||||
expect(result.examples.iscDhcpd).toContain('192.168.1.100');
|
||||
expect(result.examples.iscDhcpd).toContain('192.168.1.200');
|
||||
});
|
||||
|
||||
it('uses custom network settings when provided', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['example.com'],
|
||||
network: {
|
||||
subnet: '10.0.0.0',
|
||||
netmask: '255.255.255.0',
|
||||
rangeStart: '10.0.0.50',
|
||||
rangeEnd: '10.0.0.150',
|
||||
},
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
|
||||
expect(result.examples.iscDhcpd).toContain('10.0.0.0');
|
||||
expect(result.examples.iscDhcpd).toContain('255.255.255.0');
|
||||
expect(result.examples.iscDhcpd).toContain('10.0.0.50');
|
||||
expect(result.examples.iscDhcpd).toContain('10.0.0.150');
|
||||
});
|
||||
|
||||
it('calculates correct CIDR notation in Kea config', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['example.com'],
|
||||
network: {
|
||||
subnet: '10.0.0.0',
|
||||
netmask: '255.255.255.0',
|
||||
},
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
|
||||
expect(result.examples.keaDhcp4).toContain('"subnet": "10.0.0.0/24"');
|
||||
});
|
||||
});
|
||||
|
||||
describe('compression pointer generation', () => {
|
||||
it('generates correct compression pointers for shared suffixes', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['corp.example.com', 'example.com'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
|
||||
// The hex should end with c005 (compression pointer to offset 5)
|
||||
expect(result.hexEncoded).toContain('c005');
|
||||
});
|
||||
|
||||
it('reuses compression pointers for multiple matches', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['site1.example.com', 'site2.example.com', 'example.com'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
|
||||
// Should have multiple compression pointers
|
||||
expect(result.hexEncoded).toContain('c006');
|
||||
});
|
||||
|
||||
it('handles no compression for unrelated domains', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['example.com', 'another.org'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
|
||||
// Both domains encoded fully, no compression pointers
|
||||
expect(result.hexEncoded).toBe('076578616d706c6503636f6d0007616e6f74686572036f726700');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseOption119', () => {
|
||||
describe('basic decoding', () => {
|
||||
it('decodes a single domain without compression', () => {
|
||||
const hexInput = '076578616d706c6503636f6d00';
|
||||
|
||||
const result = parseOption119(hexInput);
|
||||
|
||||
expect(result.domains).toEqual(['example.com']);
|
||||
expect(result.totalLength).toBe(13);
|
||||
expect(result.rawHex).toBe(hexInput);
|
||||
});
|
||||
|
||||
it('decodes multiple domains with compression', () => {
|
||||
const hexInput = '04636f7270076578616d706c6503636f6d00c005';
|
||||
|
||||
const result = parseOption119(hexInput);
|
||||
|
||||
expect(result.domains).toEqual(['corp.example.com', 'example.com']);
|
||||
});
|
||||
|
||||
it('decodes complex multi-site domains', () => {
|
||||
const hexInput = '057369746531076578616d706c6503636f6d00057369746532c006c006';
|
||||
|
||||
const result = parseOption119(hexInput);
|
||||
|
||||
expect(result.domains).toEqual(['site1.example.com', 'site2.example.com', 'example.com']);
|
||||
});
|
||||
|
||||
it('handles hex input with spaces', () => {
|
||||
const hexInput = '07 65 78 61 6d 70 6c 65 03 63 6f 6d 00';
|
||||
|
||||
const result = parseOption119(hexInput);
|
||||
|
||||
expect(result.domains).toEqual(['example.com']);
|
||||
});
|
||||
|
||||
it('handles hex input with colons', () => {
|
||||
const hexInput = '07:65:78:61:6d:70:6c:65:03:63:6f:6d:00';
|
||||
|
||||
const result = parseOption119(hexInput);
|
||||
|
||||
expect(result.domains).toEqual(['example.com']);
|
||||
});
|
||||
|
||||
it('handles mixed case hex input', () => {
|
||||
const hexInput = '076578616D706C6503636F6D00';
|
||||
|
||||
const result = parseOption119(hexInput);
|
||||
|
||||
expect(result.domains).toEqual(['example.com']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('error handling', () => {
|
||||
it('rejects empty hex string', () => {
|
||||
expect(() => parseOption119('')).toThrow('Empty hex string');
|
||||
});
|
||||
|
||||
it('rejects hex string with odd number of characters', () => {
|
||||
expect(() => parseOption119('076578616d706c6503636f6d0')).toThrow('odd number of characters');
|
||||
});
|
||||
|
||||
it('rejects invalid compression pointer', () => {
|
||||
// Compression pointer at the end without second byte
|
||||
const hexInput = '076578616d706c6503636f6d00c0';
|
||||
|
||||
expect(() => parseOption119(hexInput)).toThrow('Invalid wire format');
|
||||
});
|
||||
|
||||
it('detects compression pointer loops', () => {
|
||||
// Create a loop: pointer points back to itself or creates a cycle
|
||||
// This is a malformed case
|
||||
const hexInput = 'c000'; // Points to offset 0 (itself)
|
||||
|
||||
expect(() => parseOption119(hexInput)).toThrow('Invalid wire format');
|
||||
});
|
||||
|
||||
it('detects label extending beyond data', () => {
|
||||
// Label length says 10 bytes but only 5 remain
|
||||
const hexInput = '0a6578616d706c'; // Length 10, but only 7 bytes follow
|
||||
|
||||
expect(() => parseOption119(hexInput)).toThrow('Invalid wire format');
|
||||
});
|
||||
});
|
||||
|
||||
describe('round-trip encoding/decoding', () => {
|
||||
it('maintains data integrity for single domain', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['example.com'],
|
||||
};
|
||||
|
||||
const encoded = buildOption119(config);
|
||||
const decoded = parseOption119(encoded.hexEncoded);
|
||||
|
||||
expect(decoded.domains).toEqual(config.domains);
|
||||
});
|
||||
|
||||
it('maintains data integrity for multiple domains', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['corp.example.com', 'example.com', 'dev.example.com'],
|
||||
};
|
||||
|
||||
const encoded = buildOption119(config);
|
||||
const decoded = parseOption119(encoded.hexEncoded);
|
||||
|
||||
expect(decoded.domains).toEqual(config.domains);
|
||||
});
|
||||
|
||||
it('maintains data integrity for complex scenarios', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['site1.example.com', 'site2.example.com', 'example.com', 'another.org'],
|
||||
};
|
||||
|
||||
const encoded = buildOption119(config);
|
||||
const decoded = parseOption119(encoded.hexEncoded);
|
||||
|
||||
expect(decoded.domains).toEqual(config.domains);
|
||||
});
|
||||
|
||||
it('handles domains with hyphens and numbers', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['my-domain-123.test-site.com', 'test-site.com'],
|
||||
};
|
||||
|
||||
const encoded = buildOption119(config);
|
||||
const decoded = parseOption119(encoded.hexEncoded);
|
||||
|
||||
expect(decoded.domains).toEqual(config.domains);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getDefaultOption119Config', () => {
|
||||
it('returns default configuration with sample domains', () => {
|
||||
const config = getDefaultOption119Config();
|
||||
|
||||
expect(config.domains).toBeDefined();
|
||||
expect(config.domains.length).toBeGreaterThan(0);
|
||||
expect(config.domains).toContain('example.com');
|
||||
});
|
||||
|
||||
it('returns valid configuration that can be encoded', () => {
|
||||
const config = getDefaultOption119Config();
|
||||
|
||||
expect(() => buildOption119(config)).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('DOMAIN_SEARCH_EXAMPLES', () => {
|
||||
it('provides multiple example configurations', () => {
|
||||
expect(DOMAIN_SEARCH_EXAMPLES.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('each example has required properties', () => {
|
||||
for (const example of DOMAIN_SEARCH_EXAMPLES) {
|
||||
expect(example.label).toBeDefined();
|
||||
expect(example.domains).toBeDefined();
|
||||
expect(example.domains.length).toBeGreaterThan(0);
|
||||
}
|
||||
});
|
||||
|
||||
it('all examples can be successfully encoded', () => {
|
||||
for (const example of DOMAIN_SEARCH_EXAMPLES) {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: example.domains,
|
||||
};
|
||||
|
||||
expect(() => buildOption119(config)).not.toThrow();
|
||||
}
|
||||
});
|
||||
|
||||
it('all examples maintain round-trip integrity', () => {
|
||||
for (const example of DOMAIN_SEARCH_EXAMPLES) {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: example.domains,
|
||||
};
|
||||
|
||||
const encoded = buildOption119(config);
|
||||
const decoded = parseOption119(encoded.hexEncoded);
|
||||
|
||||
expect(decoded.domains).toEqual(example.domains);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('edge cases', () => {
|
||||
it('handles single-character labels', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['a.b.c'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
const decoded = parseOption119(result.hexEncoded);
|
||||
|
||||
expect(decoded.domains).toEqual(['a.b.c']);
|
||||
});
|
||||
|
||||
it('handles domains with many labels', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['sub1.sub2.sub3.sub4.example.com'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
const decoded = parseOption119(result.hexEncoded);
|
||||
|
||||
expect(decoded.domains).toEqual(['sub1.sub2.sub3.sub4.example.com']);
|
||||
});
|
||||
|
||||
it('handles mixed alphanumeric domains', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['srv01.dc02.region3.example.com'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
const decoded = parseOption119(result.hexEncoded);
|
||||
|
||||
expect(decoded.domains).toEqual(['srv01.dc02.region3.example.com']);
|
||||
});
|
||||
|
||||
it('handles compression with partial suffix matches', () => {
|
||||
const config: DomainSearchConfig = {
|
||||
domains: ['www.example.com', 'mail.example.com', 'ftp.example.com', 'example.com'],
|
||||
};
|
||||
|
||||
const result = buildOption119(config);
|
||||
const decoded = parseOption119(result.hexEncoded);
|
||||
|
||||
expect(decoded.domains).toEqual(config.domains);
|
||||
// Verify compression is actually happening (should be shorter than uncompressed)
|
||||
expect(result.totalLength).toBeLessThan(60); // Would be ~60 bytes uncompressed
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user