@cloud-cli/dns 1.0.1 → 1.1.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 +11 -7
- package/dist/index.js +32 -21
- package/dist/index.spec.js +19 -4
- package/package.json +2 -1
- package/src/index.spec.ts +25 -5
- package/src/index.ts +39 -22
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
target: any;
|
|
5
|
-
};
|
|
1
|
+
import { init } from '@cloud-cli/cli';
|
|
2
|
+
interface DNSConfig {
|
|
3
|
+
defaultTarget?: string;
|
|
6
4
|
}
|
|
7
5
|
interface DomainAndTarget {
|
|
8
6
|
domain: string;
|
|
@@ -10,12 +8,18 @@ interface DomainAndTarget {
|
|
|
10
8
|
}
|
|
11
9
|
declare function add(input: DomainAndTarget): boolean;
|
|
12
10
|
declare function remove(input: DomainAndTarget): boolean;
|
|
13
|
-
declare function
|
|
11
|
+
declare function list(): DomainAndTarget[];
|
|
14
12
|
declare function reload(): Promise<true>;
|
|
13
|
+
declare function addDnsConfig(options: DNSConfig): void;
|
|
14
|
+
export declare function parseDNSLine(line: string): {
|
|
15
|
+
domain: string;
|
|
16
|
+
target: string;
|
|
17
|
+
};
|
|
15
18
|
declare const _default: {
|
|
16
19
|
add: typeof add;
|
|
17
20
|
remove: typeof remove;
|
|
18
|
-
|
|
21
|
+
list: typeof list;
|
|
19
22
|
reload: typeof reload;
|
|
23
|
+
[init]: typeof addDnsConfig;
|
|
20
24
|
};
|
|
21
25
|
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -1,45 +1,56 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import { join } from 'path';
|
|
3
3
|
import { exec } from '@cloud-cli/exec';
|
|
4
|
+
import { init } from '@cloud-cli/cli';
|
|
4
5
|
const lineParse = /^address=\/(.+)\/(.+)$/;
|
|
5
|
-
const filePath = join(process.cwd(), 'configuration
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return { domain: parts[1], target: parts[2] };
|
|
10
|
-
}
|
|
11
|
-
}
|
|
6
|
+
const filePath = join(process.cwd(), 'configuration', 'apps.conf');
|
|
7
|
+
const dnsConfig = {
|
|
8
|
+
defaultTarget: '127.0.0.1'
|
|
9
|
+
};
|
|
12
10
|
function add(input) {
|
|
13
|
-
let
|
|
11
|
+
let current = list();
|
|
14
12
|
if (!input.target) {
|
|
15
|
-
input.target =
|
|
13
|
+
input.target = dnsConfig.defaultTarget;
|
|
16
14
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
save(
|
|
15
|
+
current = current.filter(item => item.domain !== input.domain);
|
|
16
|
+
current.push(input);
|
|
17
|
+
save(current);
|
|
20
18
|
return true;
|
|
21
19
|
}
|
|
22
20
|
function remove(input) {
|
|
23
|
-
const
|
|
24
|
-
const newList =
|
|
21
|
+
const current = list();
|
|
22
|
+
const newList = current.filter(item => item.domain !== input.domain);
|
|
25
23
|
save(newList);
|
|
26
24
|
return true;
|
|
27
25
|
}
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
function show() {
|
|
26
|
+
function list() {
|
|
27
|
+
if (!fs.existsSync(filePath)) {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
33
30
|
const input = fs.readFileSync(filePath, 'utf8');
|
|
34
31
|
const entries = input
|
|
35
32
|
.trim()
|
|
36
33
|
.split('\n')
|
|
37
34
|
.filter(Boolean)
|
|
38
|
-
.map(
|
|
35
|
+
.map(parseDNSLine);
|
|
39
36
|
return entries;
|
|
40
37
|
}
|
|
41
38
|
async function reload() {
|
|
42
39
|
const cmd = await exec('systemctl', ['restart', 'dnsmasq']);
|
|
43
40
|
return cmd.ok || Promise.reject(new Error('Failed to reload'));
|
|
44
41
|
}
|
|
45
|
-
|
|
42
|
+
function addDnsConfig(options) {
|
|
43
|
+
if (options && options.defaultTarget) {
|
|
44
|
+
dnsConfig.defaultTarget = options.defaultTarget;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
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
|
+
}
|
|
52
|
+
export function parseDNSLine(line) {
|
|
53
|
+
const parts = line.match(lineParse);
|
|
54
|
+
return { domain: parts[1], target: parts[2] };
|
|
55
|
+
}
|
|
56
|
+
export default { add, remove, list, reload, [init]: addDnsConfig };
|
package/dist/index.spec.js
CHANGED
|
@@ -1,17 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import dns from './index';
|
|
1
|
+
import dns, { parseDNSLine } from './index';
|
|
3
2
|
import fs from 'fs';
|
|
4
3
|
import * as exec from '@cloud-cli/exec';
|
|
4
|
+
import { init } from '@cloud-cli/cli';
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
jest.spyOn(fs, 'writeFileSync').mockImplementation();
|
|
7
|
+
jest.spyOn(fs, 'readFileSync').mockImplementation(() => '');
|
|
8
|
+
});
|
|
5
9
|
describe('dns', () => {
|
|
6
10
|
it('should parse a DNS configuration line', () => {
|
|
7
11
|
const line = 'address=/foo/1.2.3.4';
|
|
8
|
-
const output =
|
|
12
|
+
const output = parseDNSLine(line);
|
|
9
13
|
expect(output).toEqual({ target: '1.2.3.4', domain: 'foo' });
|
|
10
14
|
});
|
|
11
15
|
it('should load entries from file', () => {
|
|
16
|
+
let fileExists = false;
|
|
12
17
|
const buffer = `address=/test/1.2.3.4\naddress=/foo/5.6.7.8`;
|
|
18
|
+
jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
|
|
13
19
|
jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
|
|
14
|
-
|
|
20
|
+
expect(dns.list()).toEqual([]);
|
|
21
|
+
fileExists = true;
|
|
22
|
+
const list = dns.list();
|
|
15
23
|
expect(list).toEqual([
|
|
16
24
|
{ domain: 'test', target: '1.2.3.4' },
|
|
17
25
|
{ domain: 'foo', target: '5.6.7.8' },
|
|
@@ -40,4 +48,11 @@ describe('dns', () => {
|
|
|
40
48
|
await expect(dns.reload()).rejects.toEqual(new Error('Failed to reload'));
|
|
41
49
|
});
|
|
42
50
|
});
|
|
51
|
+
describe('dns configuration', () => {
|
|
52
|
+
it('should configure the default target', () => {
|
|
53
|
+
dns[init]({ defaultTarget: '1.1.2.2' });
|
|
54
|
+
dns.add({ domain: 'bar' });
|
|
55
|
+
expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), 'address=/bar/1.1.2.2');
|
|
56
|
+
});
|
|
57
|
+
});
|
|
43
58
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloud-cli/dns",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"@cloud-cli/typescript-config": "^1.0.0"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
+
"@cloud-cli/cli": "^1.6.0",
|
|
26
27
|
"@cloud-cli/exec": "^1.0.4"
|
|
27
28
|
}
|
|
28
29
|
}
|
package/src/index.spec.ts
CHANGED
|
@@ -1,20 +1,31 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import dns from './index';
|
|
1
|
+
import dns, { parseDNSLine } from './index';
|
|
3
2
|
import fs from 'fs';
|
|
4
3
|
import * as exec from '@cloud-cli/exec';
|
|
4
|
+
import { init } from '@cloud-cli/cli';
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
jest.spyOn(fs, 'writeFileSync').mockImplementation();
|
|
8
|
+
jest.spyOn(fs, 'readFileSync').mockImplementation(() => '');
|
|
9
|
+
});
|
|
5
10
|
|
|
6
11
|
describe('dns', () => {
|
|
7
12
|
it('should parse a DNS configuration line', () => {
|
|
8
13
|
const line = 'address=/foo/1.2.3.4';
|
|
9
|
-
const output =
|
|
14
|
+
const output = parseDNSLine(line);
|
|
15
|
+
|
|
10
16
|
expect(output).toEqual({ target: '1.2.3.4', domain: 'foo' });
|
|
11
17
|
});
|
|
12
18
|
|
|
13
19
|
it('should load entries from file', () => {
|
|
20
|
+
let fileExists = false;
|
|
14
21
|
const buffer = `address=/test/1.2.3.4\naddress=/foo/5.6.7.8`;
|
|
22
|
+
jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
|
|
15
23
|
jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
|
|
16
24
|
|
|
17
|
-
|
|
25
|
+
expect(dns.list()).toEqual([]);
|
|
26
|
+
|
|
27
|
+
fileExists = true;
|
|
28
|
+
const list = dns.list();
|
|
18
29
|
expect(list).toEqual([
|
|
19
30
|
{ domain: 'test', target: '1.2.3.4' },
|
|
20
31
|
{ domain: 'foo', target: '5.6.7.8' },
|
|
@@ -52,4 +63,13 @@ describe('dns', () => {
|
|
|
52
63
|
await expect(dns.reload()).rejects.toEqual(new Error('Failed to reload'));
|
|
53
64
|
});
|
|
54
65
|
});
|
|
55
|
-
|
|
66
|
+
|
|
67
|
+
describe('dns configuration', () => {
|
|
68
|
+
it('should configure the default target', () => {
|
|
69
|
+
dns[init]({ defaultTarget: '1.1.2.2' });
|
|
70
|
+
dns.add({ domain: 'bar' });
|
|
71
|
+
|
|
72
|
+
expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), 'address=/bar/1.1.2.2');
|
|
73
|
+
});
|
|
74
|
+
})
|
|
75
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import { join } from 'path';
|
|
3
3
|
import { exec } from '@cloud-cli/exec';
|
|
4
|
+
import { init } from '@cloud-cli/cli';
|
|
5
|
+
|
|
6
|
+
interface DNSConfig {
|
|
7
|
+
defaultTarget?: string;
|
|
8
|
+
}
|
|
4
9
|
|
|
5
10
|
const lineParse = /^address=\/(.+)\/(.+)$/;
|
|
6
|
-
const filePath = join(process.cwd(), 'configuration
|
|
11
|
+
const filePath = join(process.cwd(), 'configuration', 'apps.conf');
|
|
12
|
+
const dnsConfig: DNSConfig = {
|
|
13
|
+
defaultTarget: '127.0.0.1'
|
|
14
|
+
};
|
|
7
15
|
|
|
8
|
-
export class DNS {
|
|
9
|
-
static parse(line) {
|
|
10
|
-
const parts = line.match(lineParse);
|
|
11
|
-
return { domain: parts[1], target: parts[2] }
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
16
|
interface DomainAndTarget {
|
|
15
17
|
domain: string;
|
|
16
18
|
target?: string;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
function add(input: DomainAndTarget) {
|
|
20
|
-
let
|
|
22
|
+
let current = list();
|
|
21
23
|
|
|
22
24
|
if (!input.target) {
|
|
23
|
-
input.target =
|
|
25
|
+
input.target = dnsConfig.defaultTarget;
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
save(
|
|
28
|
+
current = current.filter(item => item.domain !== input.domain);
|
|
29
|
+
current.push(input);
|
|
30
|
+
save(current);
|
|
29
31
|
|
|
30
32
|
return true;
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
function remove(input: DomainAndTarget) {
|
|
34
|
-
const
|
|
35
|
-
const newList =
|
|
36
|
+
const current = list();
|
|
37
|
+
const newList = current.filter(item => item.domain !== input.domain);
|
|
36
38
|
save(newList);
|
|
37
|
-
|
|
38
39
|
return true;
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
function
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
42
|
+
function list(): DomainAndTarget[] {
|
|
43
|
+
if (!fs.existsSync(filePath)) {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
45
46
|
|
|
46
|
-
function show(): DomainAndTarget[] {
|
|
47
47
|
const input = fs.readFileSync(filePath, 'utf8');
|
|
48
48
|
const entries = input
|
|
49
49
|
.trim()
|
|
50
50
|
.split('\n')
|
|
51
51
|
.filter(Boolean)
|
|
52
|
-
.map(
|
|
52
|
+
.map(parseDNSLine)
|
|
53
53
|
|
|
54
54
|
return entries;
|
|
55
55
|
}
|
|
@@ -59,4 +59,21 @@ async function reload() {
|
|
|
59
59
|
return cmd.ok || Promise.reject(new Error('Failed to reload'));
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
function addDnsConfig(options: DNSConfig) {
|
|
63
|
+
if (options && options.defaultTarget) {
|
|
64
|
+
dnsConfig.defaultTarget = options.defaultTarget;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
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'));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function parseDNSLine(line: string) {
|
|
75
|
+
const parts = line.match(lineParse);
|
|
76
|
+
return { domain: parts[1], target: parts[2] }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export default { add, remove, list, reload, [init]: addDnsConfig }
|