···11+---
22+'lan-network': minor
33+---
44+55+Add `noProbe` and `noDhcp` options
+40-25
src/index.ts
···99} from './network';
1010import type { GatewayAssignment } from './types';
11111212-export async function lanNetwork(): Promise<GatewayAssignment> {
1212+export interface NetworkOptions {
1313+ noProbe?: boolean;
1414+ noDhcp?: boolean;
1515+}
1616+1717+export async function lanNetwork(
1818+ opts?: NetworkOptions
1919+): Promise<GatewayAssignment> {
1320 // Get IPv4 network assignments, sorted by:
1421 // - external first
1522 // - LAN-reserved IP range priority
···24312532 // First, we attempt to probe the default route to a publicly routed IP
2633 // This will generally fail if there's no route, e.g. if the network is offline
2727- try {
2828- const defaultRoute = await probeDefaultRoute();
2929- // If this route matches a known assignment, return it without a gateway
3030- if (
3131- (assignment = matchAssignment(assignments, defaultRoute)) &&
3232- !isInternal(assignment)
3333- ) {
3434- return assignment;
3434+ if (!opts?.noProbe) {
3535+ try {
3636+ const defaultRoute = await probeDefaultRoute();
3737+ // If this route matches a known assignment, return it without a gateway
3838+ if (
3939+ (assignment = matchAssignment(assignments, defaultRoute)) &&
4040+ !isInternal(assignment)
4141+ ) {
4242+ return assignment;
4343+ }
4444+ } catch {
4545+ // Ignore errors, since we have a fallback method
3546 }
3636- } catch {
3737- // Ignore errors, since we have a fallback method
3847 }
39484049 // Second, attempt to discover a gateway's DHCP network
4150 // Because without a gateway we won't get a reply, we do this in parallel
4242- const discoveries = await Promise.allSettled(
4343- assignments.map(assignment => {
4444- // For each assignment, we send a DHCPDISCOVER packet to its network mask
4545- return dhcpDiscover(assignment);
4646- })
4747- );
4848- for (const discovery of discoveries) {
4949- // The first discovered gateway is returned, if it matches an assignment
5050- if (discovery.status === 'fulfilled' && discovery.value) {
5151- const dhcpRoute = discovery.value;
5252- if ((assignment = matchAssignment(assignments, dhcpRoute))) {
5353- return assignment;
5151+ if (!opts?.noDhcp) {
5252+ const discoveries = await Promise.allSettled(
5353+ assignments.map(assignment => {
5454+ // For each assignment, we send a DHCPDISCOVER packet to its network mask
5555+ return dhcpDiscover(assignment);
5656+ })
5757+ );
5858+ for (const discovery of discoveries) {
5959+ // The first discovered gateway is returned, if it matches an assignment
6060+ if (discovery.status === 'fulfilled' && discovery.value) {
6161+ const dhcpRoute = discovery.value;
6262+ if ((assignment = matchAssignment(assignments, dhcpRoute))) {
6363+ return assignment;
6464+ }
5465 }
5566 }
5667 }
···6071 return { ...assignments[0], gateway: null };
6172}
62736363-export function lanNetworkSync(): GatewayAssignment {
7474+export function lanNetworkSync(opts?: NetworkOptions): GatewayAssignment {
6475 const subprocessPath = require.resolve('lan-network/subprocess');
6576 const { error, status, stdout } = spawnSync(
6677 process.execPath,
6767- [subprocessPath],
7878+ [
7979+ subprocessPath,
8080+ opts?.noProbe ? '--no-probe' : null,
8181+ opts?.noDhcp ? '--no-dhcp' : null,
8282+ ].filter((x): x is string => !!x),
6883 {
6984 shell: false,
7085 timeout: 500,