UNPKG

@codetoz/dns

Version:
168 lines (167 loc) 7.6 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CommandHandler = void 0; const dns_1 = require("dns"); const network_1 = require("network"); const options_1 = require("./options"); const platform_enum_1 = require("./platform.enum"); const data_1 = require("../data"); const helpers_1 = require("../helpers"); class CommandHandler { constructor() { } init() { return __awaiter(this, void 0, void 0, function* () { const osType = yield (0, helpers_1.getPlatform)(); this.isMac = osType.includes(platform_enum_1.PlatformEnum.MACOS); this.isLinux = osType.includes(platform_enum_1.PlatformEnum.LINUX); this.isWindows = osType.includes(platform_enum_1.PlatformEnum.WINDOWS); }); } set(ips) { return __awaiter(this, void 0, void 0, function* () { if (this.isMac) { const ipString = ips.reduce((prev, current) => `${prev}${current} `, ''); yield (0, helpers_1.execute)(`networksetup -setdnsservers Wi-Fi ${ipString}`); return true; } if (this.isLinux) { const ipString = ips.reduce((prev, current) => `${prev}\nnameserver ${current}`, ''); yield (0, helpers_1.execute)(`echo "${ipString}" > /etc/resolv.conf`); return true; } if (this.isWindows) { const isAdministrator = (0, helpers_1.isAdmin)(); if (!isAdministrator) { (0, helpers_1.message)('Administrator privilege are required to change DNS settings'); return false; } return new Promise((resolve, reject) => { (0, network_1.get_interfaces_list)(function (err, obj) { return __awaiter(this, void 0, void 0, function* () { if (err) reject(err); try { const interfaces = obj; // set DNS servers per ethernet interface for (const inf in interfaces) { if ((0, helpers_1.isNetsh)() /*|| windowsPreferNetsh === true*/) { yield (0, helpers_1.execute)(`netsh interface ipv4 set dns name="${interfaces[inf].name}" static "${ips[0]}" primary`); yield (0, helpers_1.execute)(`netsh interface ipv4 add dns name="${interfaces[inf].name}" "${ips[1]}" index=2`); } else { yield (0, helpers_1.execute)(`powershell Set-DnsClientServerAddress -InterfaceAlias '${interfaces[inf].name}' -ServerAddresses '${ips[0]},${ips[1]}'`); } } yield (0, helpers_1.execute)('ipconfig /flushdns'); resolve(true); } catch (e) { resolve(false); } }); }); }); } }); } get() { var _a; return __awaiter(this, void 0, void 0, function* () { if (this.isWindows) { const isAdministrator = (0, helpers_1.isAdmin)(); if (!isAdministrator) { (0, helpers_1.message)('Administrator privilege are required to change DNS settings'); return null; } } const ips = (0, dns_1.getServers)(); if (ips !== null && ips.length >= 1) { const name = (_a = data_1.dnsProviders.find((opt) => opt.ips.find((optionIp) => optionIp === ips[0]))) === null || _a === void 0 ? void 0 : _a.name; return name !== null && name !== void 0 ? name : ''; } return ''; }); } setDns(args) { return __awaiter(this, void 0, void 0, function* () { if (args.length < 2) { (0, helpers_1.message)('Provide a DNS name from list of options\n'); this.list(); return; } const option = data_1.dnsProviders.find((opt) => opt.name === args[1]); if (!option) { (0, helpers_1.message)('Provide a valid DNS name\n'); this.list(); return; } const successful = yield this.set(option.ips); successful && (0, helpers_1.message)(`DNS Set [${option.name}]`); }); } changeDns() { return __awaiter(this, void 0, void 0, function* () { const currentDnsName = yield this.get(); this.printCurrentDnsName(currentDnsName); const option = this.getRandomOption(data_1.dnsProviders, [ currentDnsName, 'google', ]); const successful = yield this.set(option.ips); successful && (0, helpers_1.message)(`DNS Set [${option.name}]`); }); } removeDns() { return __awaiter(this, void 0, void 0, function* () { if (this.isMac) { yield (0, helpers_1.execute)('networksetup -setdnsservers Wi-Fi "empty"'); (0, helpers_1.message)('DNS Removed'); } if (this.isLinux || this.isWindows) { const googleDns = data_1.dnsProviders.find((dns) => dns.name === 'google'); if (googleDns) { const successful = yield this.set(googleDns.ips); successful && (0, helpers_1.message)('DNS Removed'); } } }); } showCurrentDns() { return __awaiter(this, void 0, void 0, function* () { const dnsName = yield this.get(); this.printCurrentDnsName(dnsName); }); } version() { (0, helpers_1.message)(`Package Version: ${helpers_1.packageVersion}`); } help() { (0, helpers_1.message)('Commands:'); console.table(options_1.commandOptions); } list() { console.table(data_1.dnsProviders); } printCurrentDnsName(dnsName) { if (dnsName) (0, helpers_1.message)(`Current DNS: [${dnsName}]`); else if (dnsName === '') (0, helpers_1.message)('Current DNS: No DNS'); else return; } getRandomOption(list, excludeNames) { const filtered = list.filter((option) => !excludeNames.includes(option.name)); return filtered[Math.floor(Math.random() * filtered.length)]; } } exports.CommandHandler = CommandHandler;