UNPKG

3.56 kBPlain TextView Raw
1/*
2 * Copyright 2021 gRPC authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import { isIPv4, isIPv6 } from 'net';
18import { StatusObject } from './call-interface';
19import { ChannelOptions } from './channel-options';
20import { LogVerbosity, Status } from './constants';
21import { Metadata } from './metadata';
22import { registerResolver, Resolver, ResolverListener } from './resolver';
23import { SubchannelAddress } from './subchannel-address';
24import { GrpcUri, splitHostPort, uriToString } from './uri-parser';
25import * as logging from './logging';
26
27const TRACER_NAME = 'ip_resolver';
28
29function trace(text: string): void {
30 logging.trace(LogVerbosity.DEBUG, TRACER_NAME, text);
31}
32
33const IPV4_SCHEME = 'ipv4';
34const IPV6_SCHEME = 'ipv6';
35
36/**
37 * The default TCP port to connect to if not explicitly specified in the target.
38 */
39const DEFAULT_PORT = 443;
40
41class IpResolver implements Resolver {
42 private addresses: SubchannelAddress[] = [];
43 private error: StatusObject | null = null;
44 constructor(
45 private target: GrpcUri,
46 private listener: ResolverListener,
47 channelOptions: ChannelOptions
48 ) {
49 trace('Resolver constructed for target ' + uriToString(target));
50 const addresses: SubchannelAddress[] = [];
51 if (!(target.scheme === IPV4_SCHEME || target.scheme === IPV6_SCHEME)) {
52 this.error = {
53 code: Status.UNAVAILABLE,
54 details: `Unrecognized scheme ${target.scheme} in IP resolver`,
55 metadata: new Metadata(),
56 };
57 return;
58 }
59 const pathList = target.path.split(',');
60 for (const path of pathList) {
61 const hostPort = splitHostPort(path);
62 if (hostPort === null) {
63 this.error = {
64 code: Status.UNAVAILABLE,
65 details: `Failed to parse ${target.scheme} address ${path}`,
66 metadata: new Metadata(),
67 };
68 return;
69 }
70 if (
71 (target.scheme === IPV4_SCHEME && !isIPv4(hostPort.host)) ||
72 (target.scheme === IPV6_SCHEME && !isIPv6(hostPort.host))
73 ) {
74 this.error = {
75 code: Status.UNAVAILABLE,
76 details: `Failed to parse ${target.scheme} address ${path}`,
77 metadata: new Metadata(),
78 };
79 return;
80 }
81 addresses.push({
82 host: hostPort.host,
83 port: hostPort.port ?? DEFAULT_PORT,
84 });
85 }
86 this.addresses = addresses;
87 trace('Parsed ' + target.scheme + ' address list ' + this.addresses);
88 }
89 updateResolution(): void {
90 process.nextTick(() => {
91 if (this.error) {
92 this.listener.onError(this.error);
93 } else {
94 this.listener.onSuccessfulResolution(
95 this.addresses,
96 null,
97 null,
98 null,
99 {}
100 );
101 }
102 });
103 }
104 destroy(): void {
105 // This resolver owns no resources, so we do nothing here.
106 }
107
108 static getDefaultAuthority(target: GrpcUri): string {
109 return target.path.split(',')[0];
110 }
111}
112
113export function setup() {
114 registerResolver(IPV4_SCHEME, IpResolver);
115 registerResolver(IPV6_SCHEME, IpResolver);
116}