-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
srv_polling.ts
165 lines (138 loc) · 4.14 KB
/
srv_polling.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
import * as dns from 'dns';
import { setTimeout } from 'timers';
import { MongoRuntimeError } from '../error';
import { Logger, LoggerOptions } from '../logger';
import { TypedEventEmitter } from '../mongo_types';
import { HostAddress } from '../utils';
/**
* Determines whether a provided address matches the provided parent domain in order
* to avoid certain attack vectors.
*
* @param srvAddress - The address to check against a domain
* @param parentDomain - The domain to check the provided address against
* @returns Whether the provided address matches the parent domain
*/
function matchesParentDomain(srvAddress: string, parentDomain: string): boolean {
const regex = /^.*?\./;
const srv = `.${srvAddress.replace(regex, '')}`;
const parent = `.${parentDomain.replace(regex, '')}`;
return srv.endsWith(parent);
}
/**
* @internal
* @category Event
*/
export class SrvPollingEvent {
srvRecords: dns.SrvRecord[];
constructor(srvRecords: dns.SrvRecord[]) {
this.srvRecords = srvRecords;
}
hostnames(): Set<string> {
return new Set(this.srvRecords.map(r => HostAddress.fromSrvRecord(r).toString()));
}
}
/** @internal */
export interface SrvPollerOptions extends LoggerOptions {
srvServiceName: string;
srvMaxHosts: number;
srvHost: string;
heartbeatFrequencyMS: number;
}
/** @internal */
export type SrvPollerEvents = {
srvRecordDiscovery(event: SrvPollingEvent): void;
};
/** @internal */
export class SrvPoller extends TypedEventEmitter<SrvPollerEvents> {
srvHost: string;
rescanSrvIntervalMS: number;
heartbeatFrequencyMS: number;
logger: Logger;
haMode: boolean;
generation: number;
srvMaxHosts: number;
srvServiceName: string;
_timeout?: NodeJS.Timeout;
/** @event */
static readonly SRV_RECORD_DISCOVERY = 'srvRecordDiscovery' as const;
constructor(options: SrvPollerOptions) {
super();
if (!options || !options.srvHost) {
throw new MongoRuntimeError('Options for SrvPoller must exist and include srvHost');
}
this.srvHost = options.srvHost;
this.srvMaxHosts = options.srvMaxHosts ?? 0;
this.srvServiceName = options.srvServiceName ?? 'mongodb';
this.rescanSrvIntervalMS = 60000;
this.heartbeatFrequencyMS = options.heartbeatFrequencyMS ?? 10000;
this.logger = new Logger('srvPoller', options);
this.haMode = false;
this.generation = 0;
this._timeout = undefined;
}
get srvAddress(): string {
return `_${this.srvServiceName}._tcp.${this.srvHost}`;
}
get intervalMS(): number {
return this.haMode ? this.heartbeatFrequencyMS : this.rescanSrvIntervalMS;
}
start(): void {
if (!this._timeout) {
this.schedule();
}
}
stop(): void {
if (this._timeout) {
clearTimeout(this._timeout);
this.generation += 1;
this._timeout = undefined;
}
}
schedule(): void {
if (this._timeout) {
clearTimeout(this._timeout);
}
this._timeout = setTimeout(() => this._poll(), this.intervalMS);
}
success(srvRecords: dns.SrvRecord[]): void {
this.haMode = false;
this.schedule();
this.emit(SrvPoller.SRV_RECORD_DISCOVERY, new SrvPollingEvent(srvRecords));
}
failure(message: string, obj?: NodeJS.ErrnoException): void {
this.logger.warn(message, obj);
this.haMode = true;
this.schedule();
}
parentDomainMismatch(srvRecord: dns.SrvRecord): void {
this.logger.warn(
`parent domain mismatch on SRV record (${srvRecord.name}:${srvRecord.port})`,
srvRecord
);
}
_poll(): void {
const generation = this.generation;
dns.resolveSrv(this.srvAddress, (err, srvRecords) => {
if (generation !== this.generation) {
return;
}
if (err) {
this.failure('DNS error', err);
return;
}
const finalAddresses: dns.SrvRecord[] = [];
for (const record of srvRecords) {
if (matchesParentDomain(record.name, this.srvHost)) {
finalAddresses.push(record);
} else {
this.parentDomainMismatch(record);
}
}
if (!finalAddresses.length) {
this.failure('No valid addresses found at host');
return;
}
this.success(finalAddresses);
});
}
}