-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
76 lines (67 loc) · 2.2 KB
/
mod.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
import type { Host } from './types.ts';
import { hostEntryToString } from './utils.ts';
export default class Hosts {
#origin : Host[];
constructor(hosts : string){
this.#origin = [];
this.parse(hosts || "");
}
public toObject() : Host[]{
return this.#origin;
}
public toText() : string {
return this.#origin.map(hostEntryToString).join('\n');
}
public resolve(hostname : string) : string | undefined {
let matching : Host | undefined = undefined;
for(let i=this.#origin.length-1; i>=0; i--){
if(this.#origin[i].hostname === hostname || this.#origin[i].alias === hostname){
matching = this.#origin[i];
break;
}
}
return matching?.ip;
}
public reverse(ip: string) : string | undefined {
let matching : Host | undefined = undefined;
for(let i=this.#origin.length-1; i>=0; i--){
if(this.#origin[i].ip === ip){
matching = this.#origin[i];
break;
}
}
return matching?.hostname;
}
public match(str : string) : Host | undefined{
let matching : Host | undefined = undefined;
for(let i=this.#origin.length-1; i>=0; i--){
if(this.#origin[i].ip === str || this.#origin[i].hostname === str || this.#origin[i].alias === str){
matching = this.#origin[i];
break;
}
}
return matching;
}
private parse(textData : string) : void{
const hosts = textData.split('\n');
hosts.forEach((line) => {
line = line.trim();
const indexOfHash = line.indexOf('#');
if(indexOfHash > -1){
line = line.slice(0, indexOfHash);
}
const matched = line.trim().split(/\s+/);
if(matched.length < 2){
return;
}
const ipAddress = matched[0];
const hostname = matched[1];
const alias = matched[2] ?? null;
this.#origin.push({
ip: ipAddress,
hostname: hostname,
alias: alias
});
});
}
}