1 | import * as pathExists from "path-exists";
|
2 | import * as fs from "fs";
|
3 | import merge = require("json-add");
|
4 | let exec = require('promised-exec');
|
5 | let outputFileSync = function(file:string,content:any){
|
6 | fs.writeFileSync(file,content)
|
7 | }
|
8 | interface ConfDnsm {
|
9 | path: string;
|
10 | interface: any;
|
11 | test: boolean;
|
12 | dhcp: {
|
13 | stop: number;
|
14 | start: number;
|
15 | lease: string;
|
16 | };
|
17 | mode?: string;
|
18 | dns: [string];
|
19 | hostIp: string;
|
20 | }
|
21 | interface OptDnsm {
|
22 | path?: string;
|
23 | interface: any;
|
24 | test?: boolean;
|
25 | mode?: string;
|
26 | dns?: [string];
|
27 | hostIp?: string;
|
28 | }
|
29 |
|
30 | interface Modes {
|
31 | ap: Mode;
|
32 | link: Mode;
|
33 | host: Mode
|
34 | };
|
35 |
|
36 | interface Mode {
|
37 | noresolv: boolean,
|
38 | dns: [string],
|
39 | dhcp: {
|
40 | stop: number;
|
41 | start: number;
|
42 | lease: string;
|
43 | };
|
44 | hostIp: string,
|
45 | test: boolean,
|
46 | interface: any,
|
47 | address?: string
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | declare let config: ConfDnsm
|
54 | config = {
|
55 | path: '/etc/dnsmasq.conf',
|
56 | interface: false,
|
57 | test: false,
|
58 | dhcp: {
|
59 | stop: 10,
|
60 | start: 3,
|
61 | lease: '3h'
|
62 | },
|
63 | dns: ['8.8.8.8', '8.8.4.4'],
|
64 | hostIp: "192.99.0.1"
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 | function parsemasq(path: string, config: Mode) {
|
70 |
|
71 | let write = '';
|
72 |
|
73 | if (config.noresolv) {
|
74 | write = write + 'no-resolv\n'
|
75 | }
|
76 |
|
77 | if (config.dhcp) {
|
78 | var root_address = config.hostIp.split('.')[0] + '.' + config.hostIp.split('.')[1] + '.' + config.hostIp.split('.')[2]
|
79 | let startIp = root_address + '.' + config.dhcp.start;
|
80 | let stopIp = root_address + '.' + config.dhcp.stop;
|
81 |
|
82 | write = write + 'dhcp-range=' + startIp + ',' + stopIp + ',' + config.dhcp.lease + '\n'
|
83 | }
|
84 |
|
85 | if (config.interface) {
|
86 | write = write + 'interface=' + config.interface + '\n'
|
87 | }
|
88 |
|
89 | if (config.dns) {
|
90 | for (var c = 0; c < config.dns.length; c++) {
|
91 | write = write + 'server=' + config.dns[c] + '\n'
|
92 | }
|
93 | }
|
94 | if (config.address) {
|
95 | write = write + 'address=' + config.address + '\n'
|
96 | }
|
97 |
|
98 |
|
99 | outputFileSync(path, write);
|
100 | if (!config.test) {
|
101 | return exec('systemctl restart dnsmasq');
|
102 |
|
103 | } else {
|
104 | return exec('echo');
|
105 |
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 | }
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | let DMasq = class DNSMasq implements ConfDnsm {
|
116 | modes: Modes;
|
117 | mode:string;
|
118 | path:string;
|
119 |
|
120 |
|
121 | interface: any;
|
122 | test: boolean;
|
123 | dhcp: {
|
124 | stop: number;
|
125 | start: number;
|
126 | lease: string;
|
127 | };
|
128 |
|
129 | dns: [string];
|
130 | hostIp: string;
|
131 |
|
132 |
|
133 | constructor(public options: OptDnsm) {
|
134 |
|
135 | if (options && typeof (options) == 'object') {
|
136 | merge(config, options)
|
137 | }
|
138 |
|
139 | if (!pathExists.sync(config.path)) {
|
140 | throw Error('No configuration file was founded')
|
141 | }
|
142 |
|
143 | this.path=config.path;
|
144 |
|
145 | if (config.hostIp.split('.').length > 4) {
|
146 | throw Error('Wrong host')
|
147 | }
|
148 |
|
149 | if (!config.interface) {
|
150 | throw Error('No configuration interface was provided')
|
151 | }
|
152 |
|
153 | for (var c = 0; c < Object.keys(config).length; c++) {
|
154 | this[Object.keys(config)[c]] = config[Object.keys(config)[c]];
|
155 | }
|
156 |
|
157 | this.modes = {
|
158 | ap: {
|
159 | noresolv: true,
|
160 | dns: config.dns,
|
161 | dhcp: config.dhcp,
|
162 | hostIp: config.hostIp,
|
163 | test: config.test,
|
164 | interface: config.interface
|
165 | },
|
166 | link: {
|
167 | noresolv: true,
|
168 | dns: config.dns,
|
169 | test: config.test,
|
170 | dhcp: config.dhcp,
|
171 | interface: config.interface,
|
172 | hostIp: config.hostIp,
|
173 | address: '/#/' + config.hostIp
|
174 | },
|
175 | host: {
|
176 | noresolv: true,
|
177 | dns: config.dns,
|
178 | test: config.test,
|
179 | dhcp: config.dhcp,
|
180 | hostIp: config.hostIp,
|
181 | interface: config.interface,
|
182 | address: '/#/' + config.hostIp
|
183 | }
|
184 | }
|
185 |
|
186 |
|
187 | }
|
188 |
|
189 |
|
190 | setmode(mode:string) {
|
191 |
|
192 | this.mode = mode;
|
193 |
|
194 | console.log(this.modes[mode])
|
195 | return parsemasq(this.path, this.modes[mode])
|
196 |
|
197 |
|
198 | };
|
199 | ap() {
|
200 |
|
201 | this.mode = 'ap';
|
202 | return parsemasq(this.path, this.modes.ap)
|
203 |
|
204 | };
|
205 | host() {
|
206 |
|
207 | this.mode = 'host';
|
208 | return parsemasq(this.path, this.modes.host)
|
209 |
|
210 | };
|
211 |
|
212 | link() {
|
213 |
|
214 | this.mode = 'link';
|
215 | return parsemasq(this.path, this.modes.link)
|
216 |
|
217 | };
|
218 |
|
219 |
|
220 | }
|
221 |
|
222 |
|
223 | export = DMasq;
|