File size: 4,158 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { filter, mapValues } from 'lodash';

function validateAllFields( fieldValues, domainName, domain ) {
	return mapValues( fieldValues, ( value, fieldName ) => {
		const isValid = validateField( {
			value,
			domain,
			domainName,
			name: fieldName,
			type: fieldValues.type,
		} );

		return isValid ? [] : [ 'Invalid' ];
	} );
}

function validateField( { name, value, type, domain, domainName } ) {
	switch ( name ) {
		case 'name':
			return isValidName( value, type, domainName, domain );
		case 'target':
			return isValidDomain( value, type );
		case 'data':
		case 'value':
			return isValidData( value, type );
		case 'protocol':
			return [ '_tcp', '_udp', '_tls' ].includes( value );
		case 'flags': {
			const intValue = parseInt( value, 10 );
			return intValue >= 0 && intValue <= 255;
		}
		case 'weight':
		case 'aux':
		case 'port': {
			const intValue = parseInt( value, 10 );
			return intValue >= 0 && intValue <= 65535;
		}
		case 'ttl': {
			const intValue = parseInt( value, 10 );
			return intValue >= 300 && intValue <= 86400;
		}
		case 'service':
			return value.match( /^[^\s.]+$/ );
		default:
			return true;
	}
}

function isValidDomain( name, type ) {
	const maxLength = name.endsWith( '.' ) ? 254 : 253;

	if ( name.length > maxLength ) {
		return false;
	}

	if ( type === 'SRV' && name === '.' ) {
		return true;
	}

	return /^([a-z0-9-_]{1,63}\.)*[a-z0-9-]{1,63}\.[a-z]{2,63}(\.)?$/i.test( name );
}

function isValidName( name, type, domainName, domain ) {
	if ( isRootDomain( name, domainName ) && canBeRootDomain( type, domain ) ) {
		return true;
	}

	switch ( type ) {
		case 'A':
		case 'AAAA':
			return /^([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)*[a-z0-9]([a-z0-9-]*[a-z0-9])?$/i.test( name );
		case 'CNAME':
			return /^([a-z0-9-_]{1,63}\.)*([a-z0-9-_]{1,63})$/i.test( name ) || name === '*';
		case 'TXT':
			return /^(\*\.|)([a-z0-9-_]{1,63}\.)*([a-z0-9-_]{1,63})$/i.test( name );
		default:
			return /^([a-z0-9-_]{1,63}\.)*([a-z0-9-_]{1,63})$/i.test( name );
	}
}

function isValidData( data, type ) {
	switch ( type ) {
		case 'A':
			return data.match( /^(\d{1,3}\.){3}\d{1,3}$/ );
		case 'AAAA':
			return data.match( /^[a-f0-9:]+$/i );
		case 'ALIAS':
		case 'CNAME':
		case 'MX':
		case 'NS':
			return isValidDomain( data );
		case 'CAA':
		case 'TXT':
			return data.length > 0 && data.length <= 2048;
	}
}

function getNormalizedData( record, selectedDomainName, selectedDomain ) {
	const normalizedRecord = Object.assign( {}, record );
	normalizedRecord.data = getFieldWithDot( record.data );
	normalizedRecord.name = getNormalizedName(
		record.name,
		record.type,
		selectedDomainName,
		selectedDomain
	);
	if ( record.target ) {
		normalizedRecord.target = getFieldWithDot( record.target );
	}

	return normalizedRecord;
}

function getNormalizedName( name, type, selectedDomainName, selectedDomain ) {
	const endsWithDomain = name.endsWith( '.' + selectedDomainName );

	if ( isRootDomain( name, selectedDomainName ) && canBeRootDomain( type, selectedDomain ) ) {
		return selectedDomainName + '.';
	}

	if ( endsWithDomain ) {
		return name.replace( new RegExp( '\\.+' + selectedDomainName + '\\.?$', 'i' ), '' );
	}

	return name;
}

function isRootDomain( name, domainName ) {
	const rootDomainVariations = [
		'@',
		domainName,
		domainName + '.',
		'@.' + domainName,
		'@.' + domainName + '.',
	];
	return ! name || rootDomainVariations.includes( name );
}

function canBeRootDomain( type, domain ) {
	// Root NS records can be edited only for subdomains
	if ( type === 'NS' && domain?.isSubdomain ) {
		return true;
	}

	return [ 'A', 'AAAA', 'ALIAS', 'CAA', 'MX', 'SRV', 'TXT' ].includes( type );
}

function getFieldWithDot( field ) {
	// something that looks like domain but doesn't end with a dot
	return typeof field === 'string' && field.match( /^([a-z0-9-_]+\.)+\.?[a-z]+$/i )
		? field + '.'
		: field;
}

function isDeletingLastMXRecord( recordToDelete, records ) {
	const currentMXRecords = filter( records, { type: 'MX' } );

	return recordToDelete.type === 'MX' && currentMXRecords.length === 1;
}

export { getNormalizedData, validateAllFields, isDeletingLastMXRecord };