upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/integration/helpers/network.mjs
blob: 020e8e897b3ce61f41e10f799420317e7dba4251 (plain)
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
import { execSync } from 'child_process';

const DEFAULT_IP = '10.192.45.1';
const WIFI_IFACE = 'wlp59s0';

export function getPortalIP() {
  return process.env.TOLLGATE_IP || DEFAULT_IP;
}

export function curl(url, timeout = 30) {
  try {
    return execSync(
      `curl -s -o /dev/null -w "%{http_code}" --connect-timeout ${timeout} --max-time ${timeout + 10} "${url}"`,
      { encoding: 'utf8', timeout: (timeout + 10) * 1000 }
    ).trim();
  } catch {
    return null;
  }
}

export function curlBody(url, timeout = 30) {
  try {
    return execSync(
      `curl -s --connect-timeout ${timeout} --max-time ${timeout + 10} "${url}"`,
      { encoding: 'utf8', timeout: (timeout + 10) * 1000 }
    );
  } catch {
    return null;
  }
}

export function canPing(host = '8.8.8.8', count = 1) {
  try {
    const result = execSync(
      `ping -c ${count} -W 3 -I ${WIFI_IFACE} ${host} 2>/dev/null`,
      { encoding: 'utf8', timeout: 10000 }
    );
    return result && !result.includes('100% packet loss');
  } catch {
    return false;
  }
}

export function canResolve(domain, timeout = 5) {
  try {
    const result = execSync(
      `dig +short +timeout=${timeout} +tries=1 ${domain} 2>&1`,
      { encoding: 'utf8', timeout: (timeout + 2) * 1000 }
    ).trim();
    return result.length > 0 && !result.includes('NXDOMAIN');
  } catch {
    return false;
  }
}

export function dnsResolvesToSelf(domain) {
  const ip = getPortalIP();
  try {
    const result = execSync(
      `dig +short +timeout=5 ${domain} @${ip} 2>&1`,
      { encoding: 'utf8', timeout: 10000 }
    ).trim();
    return result === ip;
  } catch {
    return false;
  }
}