Skip to content

Commit

Permalink
fix(electron): 🩹 fix connection issue of domain name keys (#1763)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyyi1 authored Oct 30, 2023
1 parent 07a5651 commit c68836d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/electron/connectivity.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
return timeoutPromise(
new Promise<string>((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'
);
}
7 changes: 7 additions & 0 deletions src/electron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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. "<host>/32"), therefore <host> 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);
Expand Down

0 comments on commit c68836d

Please sign in to comment.