zgrzyt 2.2.3 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Readme.md CHANGED
@@ -49,6 +49,7 @@ timeout=250 ; optional, in millis
49
49
  retry=2 ; optional, by default zgrzyt retries 2 times before assuming API endpoint is down
50
50
  domain=example.net ; optional, domain for which zgrzyt will update DNS record
51
51
  method=HEAD ; optional, HTTP method used by zgrzyt
52
+ proxied=treue ; optional, by default zgrzyt preserves Cloudflare proxy status when updating DNS record
52
53
  ```
53
54
 
54
55
  If `api.domain` is not specified its value is deduced from `api.url`.
package/lib/check.js CHANGED
@@ -1,13 +1,12 @@
1
1
  import http from 'node:http';
2
2
  import https from 'node:https';
3
3
  import makeDebug from 'debug';
4
-
5
- const debug = makeDebug('zgrzyt:check');
6
-
4
+ import packageJson from '../package.json' with { type: 'json' };
7
5
  import { resolve } from './dns.js';
8
6
  import { updateHealth } from './state.js';
9
7
 
10
- import packageJson from '../package.json' with { type: 'json' };
8
+ const debug = makeDebug('zgrzyt:check');
9
+
11
10
  const {
12
11
  name,
13
12
  version,
@@ -16,10 +15,6 @@ const {
16
15
 
17
16
  const USER_AGENT = `${name}/${version} (${homepage})`;
18
17
 
19
- /* global URL */
20
-
21
- export { checkServices };
22
-
23
18
  async function checkService(server, api) {
24
19
  const { ipv4 = true, ipv6 = false } = api;
25
20
  const addresses = await resolve(server, { ipv4, ipv6 });
@@ -102,7 +97,7 @@ async function checkApi(api, { address, family }) {
102
97
  }
103
98
  }
104
99
 
105
- async function checkServices(servers, selected, api, { force, repair }) {
100
+ export async function checkServices(servers, selected, api, { force, repair }) {
106
101
  const items = await Promise.all(servers.map(s => checkService(s, api)));
107
102
  const okItems = items.filter(item => item.ok);
108
103
  if (okItems.length === 0) {
package/lib/cloudflare.js CHANGED
@@ -2,8 +2,6 @@ import makeDebug from 'debug';
2
2
 
3
3
  const debug = makeDebug('zgrzyt:cloudflare');
4
4
 
5
- export default client;
6
-
7
5
  class CloudflareError extends Error {
8
6
  constructor(errors) {
9
7
  super(`Cloudflare API error: ${errors[0].message}`);
@@ -48,7 +46,7 @@ function makeFetch({ token, timeout, retry }) {
48
46
  }
49
47
  }
50
48
 
51
- function client({ token, timeout = 4000, retry = 2 }) {
49
+ export default function client({ token, timeout = 4000, retry = 2 }) {
52
50
  const cf = makeFetch({ token, timeout, retry });
53
51
  const cacheZones = Object.create(null);
54
52
  let pZones;
@@ -61,7 +59,7 @@ function client({ token, timeout = 4000, retry = 2 }) {
61
59
  const zone = await getZone(zoneName);
62
60
  const record = await getRecord(zone.id, domain);
63
61
  if (record.type === 'CNAME') {
64
- return updateRecord(zone.id, record.id, record.proxied, domain, good);
62
+ return updateRecord(zone.id, record.id, good.proxied, domain, good);
65
63
  }
66
64
  console.error('Cannot only update CNAME records');
67
65
  }
package/lib/config.js CHANGED
@@ -1,8 +1,6 @@
1
1
  import { fromUrl, parseDomain } from 'parse-domain';
2
2
  import makeClient from './cloudflare.js';
3
3
 
4
- export default prepareConfig;
5
-
6
4
  function getDomainAndZone({ url, domain: apiDomain }) {
7
5
  if (!apiDomain) {
8
6
  apiDomain = fromUrl(url);
@@ -14,7 +12,7 @@ function getDomainAndZone({ url, domain: apiDomain }) {
14
12
  };
15
13
  }
16
14
 
17
- function prepareConfig(config) {
15
+ export default function prepareConfig(config) {
18
16
  const { cloudflare, api = {}, cluster = {} } = config;
19
17
 
20
18
  if (!cloudflare?.token) {
@@ -22,10 +20,10 @@ function prepareConfig(config) {
22
20
  return;
23
21
  }
24
22
  if (typeof cloudflare.timeout === 'string') {
25
- cloudflare.timeout = Number.parseInt(cloudflare.timeout);
23
+ cloudflare.timeout = Number.parseInt(cloudflare.timeout, 10);
26
24
  }
27
25
  if (typeof cloudflare.retry === 'string') {
28
- cloudflare.retry = Number.parseInt(cloudflare.retry);
26
+ cloudflare.retry = Number.parseInt(cloudflare.retry, 10);
29
27
  }
30
28
 
31
29
  // collect all APIs
@@ -48,6 +46,7 @@ function prepareConfig(config) {
48
46
  const timeout = Number.parseInt(api.timeout || config.timeout, 10) || 250;
49
47
  const retry = Number.parseInt(api.retry || config.retry, 10) || 2;
50
48
  const force = 'force' in api ? api.force : config.force;
49
+ const proxied = 'proxied' in api ? api.proxied : config.proxied;
51
50
  const repair = Number.parseInt(api.repair || config.repair, 10) || 5;
52
51
  const ipv6 = Boolean(api.ipv6 ?? config.ipv6);
53
52
  const ipv4 = Boolean(api.ipv4 ?? config.ipv4 ?? true);
@@ -71,6 +70,7 @@ function prepareConfig(config) {
71
70
  servers,
72
71
  api: {
73
72
  url: api.url,
73
+ proxied,
74
74
  method: api.method || 'HEAD',
75
75
  headers,
76
76
  timeout,
package/lib/report.js CHANGED
@@ -14,19 +14,19 @@ function collect(context, { url, domain, record, good, switched }) {
14
14
  context.missing.push(url);
15
15
  } else if (switched) {
16
16
  const line = sprintf(
17
- '%-25s %-25s => %-25s [%s]',
17
+ '%-25s %-25s => %-25s %s',
18
18
  domain,
19
19
  record.content,
20
20
  good.server,
21
- good.address
21
+ good.proxied ? '[proxied]' : ''
22
22
  );
23
23
  context.switched.push(line);
24
24
  } else {
25
25
  const line = sprintf(
26
- '%-25s %-25s [%s]',
26
+ '%-25s %-25s %s',
27
27
  record.name,
28
28
  good.server,
29
- good.address
29
+ record.proxied ? '[proxied]' : ''
30
30
  );
31
31
  context.noops.push(line);
32
32
  }
package/lib/state.js CHANGED
@@ -1,19 +1,17 @@
1
1
  import { readFile, writeFile } from 'node:fs/promises';
2
2
 
3
- export { updateHealth, onExit };
4
-
5
3
  const { ZGRZYT_STATE = '/var/lib/zgrzyt/state.json' } = process.env;
6
4
 
7
5
  const state_p = read();
8
6
  let hook;
9
7
 
10
- async function onExit() {
8
+ export async function onExit() {
11
9
  if (hook) {
12
10
  await hook();
13
11
  }
14
12
  }
15
13
 
16
- async function updateHealth({ url }, serverName, ok) {
14
+ export async function updateHealth({ url }, serverName, ok) {
17
15
  const state = await peak(url, serverName);
18
16
  let health = 0;
19
17
  if (ok) {
package/lib/zgrzyt.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { checkServices } from './check.js';
2
2
 
3
3
  export async function zgrzyt({ servers, api, client, force, repair }) {
4
- const { domain, zone } = api;
4
+ const { domain, zone, proxied } = api;
5
5
  const record = await client.listRecords(zone, domain);
6
6
  const good = await checkServices(servers, record.content, api, {
7
7
  force,
@@ -13,7 +13,12 @@ export async function zgrzyt({ servers, api, client, force, repair }) {
13
13
  record,
14
14
  good
15
15
  };
16
- if (good && good.server !== record.content) {
16
+ if (!good) {
17
+ return result;
18
+ }
19
+ good.proxied = proxied ?? record.proxied;
20
+ // need to switch if different good server is found or different proxied setting
21
+ if (good.server !== record.content || good.proxied !== record.proxied) {
17
22
  result.switched = await client.switchToService(zone, domain, good);
18
23
  }
19
24
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zgrzyt",
3
- "version": "2.2.3",
3
+ "version": "2.3.0",
4
4
  "description": "Poor man's load balancing DNS switcher.",
5
5
  "type": "module",
6
6
  "author": {
@@ -32,7 +32,7 @@
32
32
  "sprintfjs": "^1.2.16"
33
33
  },
34
34
  "devDependencies": {
35
- "@biomejs/biome": "^1.9.4"
35
+ "@biomejs/biome": "2.2.5"
36
36
  },
37
37
  "scripts": {
38
38
  "test": "make check"