From c68836d957da098df50a7192ab4f753c6b771b90 Mon Sep 17 00:00:00 2001 From: "J. Yi" <93548144+jyyi1@users.noreply.github.com> Date: Mon, 30 Oct 2023 15:45:42 -0400 Subject: [PATCH] =?UTF-8?q?fix(electron):=20=F0=9F=A9=B9=20fix=20connectio?= =?UTF-8?q?n=20issue=20of=20domain=20name=20keys=20(#1763)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/electron/connectivity.ts | 39 ++++++++++++++++++++++++++++++++++++ src/electron/index.ts | 7 +++++++ 2 files changed, 46 insertions(+) create mode 100644 src/electron/connectivity.ts diff --git a/src/electron/connectivity.ts b/src/electron/connectivity.ts new file mode 100644 index 0000000000..1aafc92e3f --- /dev/null +++ b/src/electron/connectivity.ts @@ -0,0 +1,39 @@ +// Copyright 2019 The Outline Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import * as dns from 'dns'; + +import {timeoutPromise} from '../infrastructure/timeout_promise'; +import * as errors from '../www/model/errors'; + +const DNS_LOOKUP_TIMEOUT_MS = 10000; + +// Uses the OS' built-in functions, i.e. /etc/hosts, et al.: +// https://nodejs.org/dist/latest-v10.x/docs/api/dns.html#dns_dns +// +// Effectively a no-op if hostname is already an IP. +export function lookupIp(hostname: string): Promise { + return timeoutPromise( + new Promise((fulfill, reject) => { + dns.lookup(hostname, 4, (e, address) => { + if (e) { + return reject(new errors.ServerUnreachable('could not resolve proxy server hostname')); + } + fulfill(address); + }); + }), + DNS_LOOKUP_TIMEOUT_MS, + 'DNS lookup' + ); +} diff --git a/src/electron/index.ts b/src/electron/index.ts index e7b16afaed..60820e502a 100644 --- a/src/electron/index.ts +++ b/src/electron/index.ts @@ -31,6 +31,7 @@ import {GoVpnTunnel} from './go_vpn_tunnel'; import {installRoutingServices, RoutingDaemon} from './routing_service'; import {TunnelStore, SerializableTunnel} from './tunnel_store'; import {VpnTunnel} from './vpn_tunnel'; +import {lookupIp} from './connectivity'; // TODO: can we define these macros in other .d.ts files with default values? // Build-time macros injected by webpack's DefinePlugin: @@ -466,6 +467,12 @@ function main() { console.log(`connecting to ${args.id}...`); try { + // We must convert the host from a potential "hostname" to an "IP" address + // because startVpn will add a routing table entry that prefixed with this + // host (e.g. "/32"), therefore must be an IP address. + // TODO: make sure we resolve it in the native code + args.config.host = await lookupIp(args.config.host || ''); + await startVpn(args.config, args.id); console.log(`connected to ${args.id}`); await setupAutoLaunch(args);