-
Notifications
You must be signed in to change notification settings - Fork 0
/
WhoisData.ts
167 lines (139 loc) · 4.68 KB
/
WhoisData.ts
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
163
164
165
166
167
import {decode} from "html-entities";
import {camelCase} from "change-case";
import { DateHelper } from "../helpers/DateHelper";
interface WhoisResponse {
origin: 'whois',
domainExpirationDate: string | null
registrar: {
name: string | null
url: string | null
owner: string | null
tech: string | null
},
provider: string | null
nameServers: string | null
}
class WhoisData {
protected rawData: string | undefined;
protected whoisResponse: { [key: string]: string } | undefined;
protected currentServer: any;
constructor(data: string | { [key: string]: string }) {
this.rawData = (typeof data === 'string') ? data : undefined
this.whoisResponse = (typeof data === 'object')? data : undefined
}
public parse() {
if (!this.rawData || typeof this.rawData !== 'string') {
throw new Error('rawData is missing or is not a string');
}
const decodedRawData: string = decode(this.rawData)
let result: { [key: string]: string } = {};
decodedRawData
.split('\n')
.filter(line => !line.startsWith('%'))
.filter(line => line.includes(':'))
.forEach(line => {
const lineParts = line
.replace(/<+$/, '')
.trim()
.split(/:(?!\/\/)/)
.filter(part => part.trim() !== '');
if (lineParts.length > 1) {
const key = camelCase(lineParts[0])
const value = lineParts
.splice(1)
.join(':')
.replace(/<+$/, '')
.trim()
if (key in result) {
result[key] = `${result[key]} ${value}`;
return
}
result[key] = value;
}
});
this.whoisResponse = result;
return this;
}
public getParsed() {
if (!this.whoisResponse || typeof this.whoisResponse !== 'object') {
throw new Error('whoisResponse is missing or is not an object. Run the parser first.');
}
return this.whoisResponse;
}
protected fetchData(keyIncludes: string[], processor?: (data: any) => any) {
if (!this.whoisResponse || typeof this.whoisResponse !== 'object') {
throw new Error('whoisResponse is missing or is not an object');
}
// Check for the exact key match first
for (const key of keyIncludes) {
const exactMatchData = this.whoisResponse[key];
if (exactMatchData) {
return processor ? processor(exactMatchData) : exactMatchData;
}
}
// If no exact match found, perform partial key match
const whoisResponseEntries = Object.entries(this.whoisResponse || {});
for (const [key, data] of whoisResponseEntries) {
if (keyIncludes.some(inclusion => key.includes(inclusion)) && data) {
return processor ? processor(data) : data;
}
}
return null;
}
protected isBrDomain() {
if (!this.whoisResponse || typeof this.whoisResponse !== 'object') {
throw new Error('whoisResponse is missing or is not an object');
}
return this.whoisResponse?.domainName?.endsWith('.br') || this.whoisResponse?.domain?.endsWith('.br') || false;
}
protected getExpirationDate() {
return this.fetchData(['xpir'], dateFromWhois => new DateHelper().getDateFromString(dateFromWhois));
}
protected getNameServers() {
return this.fetchData(['nserver', 'nameServer'], nameServers => nameServers.split(' '));
}
protected getRegistrar() {
return this.fetchData(['registrar']) || (this.isBrDomain() ? 'Registro BR' : null);
}
protected getProvider() {
return this.fetchData(['provider']) || this.getRegistrar() || (this.isBrDomain() ? 'Registro BR' : null);
}
protected getRegistrarUrl() {
return this.fetchData(['registrarUrl']) || (this.isBrDomain() ? 'https://registro.br' : null);
}
protected getRegistrarOwner() {
return this.fetchData(['ownerC']) || (this.isBrDomain() ? this.currentServer.data.ownerC : null);
}
protected getRegistrarTech() {
return this.fetchData(['techC']) || (this.isBrDomain() ? this.currentServer.data.techC : null);
}
public buildResponse(): WhoisResponse {
if (!this.whoisResponse) {
return {
origin: 'whois',
domainExpirationDate: null,
registrar: {
name: null,
url: null,
owner: null,
tech: null,
},
provider: null,
nameServers: null,
}
}
return {
origin: 'whois',
domainExpirationDate: this.getExpirationDate(),
registrar: {
name: this.getRegistrar(),
url: this.getRegistrarUrl(),
owner: this.getRegistrarOwner(),
tech: this.getRegistrarTech()
},
provider: this.getProvider(),
nameServers: this.getNameServers(),
}
}
}
export { WhoisData, WhoisResponse }