@cloud-cli/dns 1.1.0 → 2.0.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/dist/index.d.ts CHANGED
@@ -9,17 +9,18 @@ interface DomainAndTarget {
9
9
  declare function add(input: DomainAndTarget): boolean;
10
10
  declare function remove(input: DomainAndTarget): boolean;
11
11
  declare function list(): DomainAndTarget[];
12
+ declare function get(options: {
13
+ domain: string;
14
+ }): DomainAndTarget | null;
12
15
  declare function reload(): Promise<true>;
13
16
  declare function addDnsConfig(options: DNSConfig): void;
14
- export declare function parseDNSLine(line: string): {
15
- domain: string;
16
- target: string;
17
- };
17
+ export declare function parseDNSLine(line: string): DomainAndTarget[];
18
18
  declare const _default: {
19
19
  add: typeof add;
20
20
  remove: typeof remove;
21
21
  list: typeof list;
22
22
  reload: typeof reload;
23
+ get: typeof get;
23
24
  [init]: typeof addDnsConfig;
24
25
  };
25
26
  export default _default;
package/dist/index.js CHANGED
@@ -2,8 +2,7 @@ import fs from 'fs';
2
2
  import { join } from 'path';
3
3
  import { exec } from '@cloud-cli/exec';
4
4
  import { init } from '@cloud-cli/cli';
5
- const lineParse = /^address=\/(.+)\/(.+)$/;
6
- const filePath = join(process.cwd(), 'configuration', 'apps.conf');
5
+ const filePath = join(process.cwd(), 'configuration', 'hosts.conf');
7
6
  const dnsConfig = {
8
7
  defaultTarget: '127.0.0.1'
9
8
  };
@@ -32,11 +31,15 @@ function list() {
32
31
  .trim()
33
32
  .split('\n')
34
33
  .filter(Boolean)
35
- .map(parseDNSLine);
34
+ .flatMap(parseDNSLine);
36
35
  return entries;
37
36
  }
37
+ function get(options) {
38
+ return list().filter(d => d.domain === options.domain)[0] || null;
39
+ }
38
40
  async function reload() {
39
- const cmd = await exec('systemctl', ['restart', 'dnsmasq']);
41
+ const getPid = await exec('pidof', ['dnsmasq']);
42
+ const cmd = await exec('kill', ['-s', 'HUP', getPid.stdout.trim()]);
40
43
  return cmd.ok || Promise.reject(new Error('Failed to reload'));
41
44
  }
42
45
  function addDnsConfig(options) {
@@ -45,12 +48,17 @@ function addDnsConfig(options) {
45
48
  }
46
49
  }
47
50
  function save(list) {
48
- const txt = list.map(line => `address=/${line.domain}/${line.target}`);
49
- console.log('save', filePath, txt);
50
- fs.writeFileSync(filePath, txt.join('\n'));
51
+ const targets = list.reduce((all, next) => {
52
+ var _a;
53
+ all[_a = next.target] || (all[_a] = []);
54
+ all[next.target].push(next.domain);
55
+ return all;
56
+ }, {});
57
+ const txt = Object.entries(targets).map(([ip, domains]) => `${ip} ${domains.join(' ')}`);
58
+ fs.writeFileSync(filePath, txt.join('\n').trim());
51
59
  }
52
60
  export function parseDNSLine(line) {
53
- const parts = line.match(lineParse);
54
- return { domain: parts[1], target: parts[2] };
61
+ const [ip, ...domains] = line.split(/\s+/);
62
+ return domains.map(d => ({ domain: d, target: ip }));
55
63
  }
56
- export default { add, remove, list, reload, [init]: addDnsConfig };
64
+ export default { add, remove, list, reload, get, [init]: addDnsConfig };
@@ -8,13 +8,16 @@ beforeEach(() => {
8
8
  });
9
9
  describe('dns', () => {
10
10
  it('should parse a DNS configuration line', () => {
11
- const line = 'address=/foo/1.2.3.4';
11
+ const line = '1.2.3.4 foo bar';
12
12
  const output = parseDNSLine(line);
13
- expect(output).toEqual({ target: '1.2.3.4', domain: 'foo' });
13
+ expect(output).toEqual([
14
+ { target: '1.2.3.4', domain: 'foo' },
15
+ { target: '1.2.3.4', domain: 'bar' },
16
+ ]);
14
17
  });
15
18
  it('should load entries from file', () => {
16
19
  let fileExists = false;
17
- const buffer = `address=/test/1.2.3.4\naddress=/foo/5.6.7.8`;
20
+ const buffer = `1.2.3.4 test\n5.6.7.8 foo`;
18
21
  jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
19
22
  jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
20
23
  expect(dns.list()).toEqual([]);
@@ -25,26 +28,40 @@ describe('dns', () => {
25
28
  { domain: 'foo', target: '5.6.7.8' },
26
29
  ]);
27
30
  });
31
+ it('should get a DNS entry by domain', () => {
32
+ let fileExists = false;
33
+ const buffer = `1.2.3.4 test.com\n5.6.7.8 foo.com`;
34
+ jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
35
+ jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
36
+ expect(dns.get({ domain: 'foo.com' })).toBe(null);
37
+ fileExists = true;
38
+ expect(dns.get({ domain: 'foo.com' })).toEqual({ domain: 'foo.com', target: '5.6.7.8' });
39
+ });
28
40
  it('should add and remove local DNS entries', () => {
29
41
  let text = '';
30
42
  jest.spyOn(fs, 'writeFileSync').mockImplementation((_file, value) => { text = value; });
31
43
  jest.spyOn(fs, 'readFileSync').mockImplementation(() => text);
32
44
  dns.add({ domain: 'foo', target: '2.3.4.5' });
33
- dns.add({ domain: 'bar' });
34
- const lines = ['address=/foo/2.3.4.5', 'address=/bar/127.0.0.1'];
35
- expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), lines[0]);
45
+ dns.add({ domain: 'bar', target: '2.3.4.5' });
46
+ dns.add({ domain: 'baz' });
47
+ const lines = ['2.3.4.5 foo bar', '127.0.0.1 baz'];
48
+ expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '2.3.4.5 foo');
49
+ expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '2.3.4.5 foo bar');
36
50
  expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), lines.join('\n'));
37
- dns.remove({ domain: 'bar' });
51
+ dns.remove({ domain: 'baz' });
38
52
  expect(text).toBe(lines[0]);
39
53
  });
40
54
  describe('reload', () => {
41
55
  it('should reload the DNS service', async () => {
42
- jest.spyOn(exec, 'exec').mockResolvedValue({ ok: true });
56
+ jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: true, stdout: '123' });
57
+ jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: true });
43
58
  await expect(dns.reload()).resolves.toBe(true);
44
- expect(exec.exec).toHaveBeenCalledWith('systemctl', ['restart', 'dnsmasq']);
59
+ expect(exec.exec).toHaveBeenCalledWith('pidof', ['dnsmasq']);
60
+ expect(exec.exec).toHaveBeenCalledWith('kill', ['-s', 'HUP', '123']);
45
61
  });
46
62
  it('should show error on reload', async () => {
47
- jest.spyOn(exec, 'exec').mockResolvedValue({ ok: false, stderr: 'error' });
63
+ jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: true, stdout: '123' });
64
+ jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: false, stderr: 'error' });
48
65
  await expect(dns.reload()).rejects.toEqual(new Error('Failed to reload'));
49
66
  });
50
67
  });
@@ -52,7 +69,7 @@ describe('dns', () => {
52
69
  it('should configure the default target', () => {
53
70
  dns[init]({ defaultTarget: '1.1.2.2' });
54
71
  dns.add({ domain: 'bar' });
55
- expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), 'address=/bar/1.1.2.2');
72
+ expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '1.1.2.2 bar');
56
73
  });
57
74
  });
58
75
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/dns",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",
package/src/index.spec.ts CHANGED
@@ -10,15 +10,18 @@ beforeEach(() => {
10
10
 
11
11
  describe('dns', () => {
12
12
  it('should parse a DNS configuration line', () => {
13
- const line = 'address=/foo/1.2.3.4';
13
+ const line = '1.2.3.4 foo bar';
14
14
  const output = parseDNSLine(line);
15
15
 
16
- expect(output).toEqual({ target: '1.2.3.4', domain: 'foo' });
16
+ expect(output).toEqual([
17
+ { target: '1.2.3.4', domain: 'foo' },
18
+ { target: '1.2.3.4', domain: 'bar' },
19
+ ]);
17
20
  });
18
21
 
19
22
  it('should load entries from file', () => {
20
23
  let fileExists = false;
21
- const buffer = `address=/test/1.2.3.4\naddress=/foo/5.6.7.8`;
24
+ const buffer = `1.2.3.4 test\n5.6.7.8 foo`;
22
25
  jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
23
26
  jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
24
27
 
@@ -32,33 +35,50 @@ describe('dns', () => {
32
35
  ])
33
36
  });
34
37
 
38
+ it('should get a DNS entry by domain', () => {
39
+ let fileExists = false;
40
+ const buffer = `1.2.3.4 test.com\n5.6.7.8 foo.com`;
41
+ jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
42
+ jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
43
+
44
+ expect(dns.get({ domain: 'foo.com' })).toBe(null);
45
+
46
+ fileExists = true;
47
+ expect(dns.get({ domain: 'foo.com' })).toEqual({ domain: 'foo.com', target: '5.6.7.8' });
48
+ });
49
+
35
50
  it('should add and remove local DNS entries', () => {
36
51
  let text = '';
37
52
  jest.spyOn(fs, 'writeFileSync').mockImplementation((_file, value: string) => { text = value; });
38
53
  jest.spyOn(fs, 'readFileSync').mockImplementation(() => text);
39
54
 
40
55
  dns.add({ domain: 'foo', target: '2.3.4.5' });
41
- dns.add({ domain: 'bar' });
56
+ dns.add({ domain: 'bar', target: '2.3.4.5' });
57
+ dns.add({ domain: 'baz' });
42
58
 
43
- const lines = ['address=/foo/2.3.4.5', 'address=/bar/127.0.0.1']
59
+ const lines = ['2.3.4.5 foo bar', '127.0.0.1 baz'];
44
60
 
45
- expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), lines[0]);
61
+ expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '2.3.4.5 foo');
62
+ expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '2.3.4.5 foo bar');
46
63
  expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), lines.join('\n'));
47
64
 
48
- dns.remove({ domain: 'bar' });
65
+ dns.remove({ domain: 'baz' });
49
66
  expect(text).toBe(lines[0]);
50
67
  });
51
68
 
52
69
  describe('reload', () => {
53
70
  it('should reload the DNS service', async () => {
54
- jest.spyOn(exec, 'exec').mockResolvedValue({ ok: true } as any);
71
+ jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: true, stdout: '123' } as any);
72
+ jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: true } as any);
55
73
 
56
74
  await expect(dns.reload()).resolves.toBe(true);
57
- expect(exec.exec).toHaveBeenCalledWith('systemctl', ['restart', 'dnsmasq']);
75
+ expect(exec.exec).toHaveBeenCalledWith('pidof', ['dnsmasq']);
76
+ expect(exec.exec).toHaveBeenCalledWith('kill', ['-s', 'HUP', '123']);
58
77
  });
59
78
 
60
79
  it('should show error on reload', async () => {
61
- jest.spyOn(exec, 'exec').mockResolvedValue({ ok: false, stderr: 'error' } as any);
80
+ jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: true, stdout: '123' } as any);
81
+ jest.spyOn(exec, 'exec').mockResolvedValueOnce({ ok: false, stderr: 'error' } as any);
62
82
 
63
83
  await expect(dns.reload()).rejects.toEqual(new Error('Failed to reload'));
64
84
  });
@@ -69,7 +89,7 @@ describe('dns', () => {
69
89
  dns[init]({ defaultTarget: '1.1.2.2' });
70
90
  dns.add({ domain: 'bar' });
71
91
 
72
- expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), 'address=/bar/1.1.2.2');
92
+ expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '1.1.2.2 bar');
73
93
  });
74
94
  })
75
95
  });
package/src/index.ts CHANGED
@@ -7,8 +7,7 @@ interface DNSConfig {
7
7
  defaultTarget?: string;
8
8
  }
9
9
 
10
- const lineParse = /^address=\/(.+)\/(.+)$/;
11
- const filePath = join(process.cwd(), 'configuration', 'apps.conf');
10
+ const filePath = join(process.cwd(), 'configuration', 'hosts.conf');
12
11
  const dnsConfig: DNSConfig = {
13
12
  defaultTarget: '127.0.0.1'
14
13
  };
@@ -49,13 +48,18 @@ function list(): DomainAndTarget[] {
49
48
  .trim()
50
49
  .split('\n')
51
50
  .filter(Boolean)
52
- .map(parseDNSLine)
51
+ .flatMap(parseDNSLine)
53
52
 
54
53
  return entries;
55
54
  }
56
55
 
56
+ function get(options: { domain: string; }): DomainAndTarget | null {
57
+ return list().filter(d => d.domain === options.domain)[0] || null;
58
+ }
59
+
57
60
  async function reload() {
58
- const cmd = await exec('systemctl', ['restart', 'dnsmasq']);
61
+ const getPid = await exec('pidof', ['dnsmasq']);
62
+ const cmd = await exec('kill', ['-s', 'HUP', getPid.stdout.trim()]);
59
63
  return cmd.ok || Promise.reject(new Error('Failed to reload'));
60
64
  }
61
65
 
@@ -66,14 +70,20 @@ function addDnsConfig(options: DNSConfig) {
66
70
  }
67
71
 
68
72
  function save(list: DomainAndTarget[]) {
69
- const txt = list.map(line => `address=/${line.domain}/${line.target}`);
70
- console.log('save', filePath, txt);
71
- fs.writeFileSync(filePath, txt.join('\n'));
73
+ const targets: Record<string, string[]> = list.reduce((all, next) => {
74
+ all[next.target] ||= [];
75
+ all[next.target].push(next.domain);
76
+ return all;
77
+ }, {});
78
+
79
+ const txt = Object.entries(targets).map(([ip, domains]) => `${ip} ${domains.join(' ')}`);
80
+
81
+ fs.writeFileSync(filePath, txt.join('\n').trim());
72
82
  }
73
83
 
74
- export function parseDNSLine(line: string) {
75
- const parts = line.match(lineParse);
76
- return { domain: parts[1], target: parts[2] }
84
+ export function parseDNSLine(line: string): DomainAndTarget[] {
85
+ const [ip, ...domains] = line.split(/\s+/);
86
+ return domains.map(d => ({ domain: d, target: ip }));
77
87
  }
78
88
 
79
- export default { add, remove, list, reload, [init]: addDnsConfig }
89
+ export default { add, remove, list, reload, get, [init]: addDnsConfig }