@u4/adbkit
Version:
A Typescript client for the Android Debug Bridge.
108 lines • 3.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IpRouteEntry = void 0;
const command_1 = __importDefault(require("../../command"));
const ShellExecError_1 = __importDefault(require("./ShellExecError"));
class IpRouteCommand extends command_1.default {
constructor(connection, options) {
super(connection, options);
}
async execute(...args) {
super.sendCommand(['shell:ip', 'route', ...args].join(' ')); // 2>/dev/null
await this.readOKAY();
const data = await this.parser.readAll();
return this.parseIpRoute(data.toString());
}
parseIpRoute(value) {
if (value.startsWith('Error: ')) {
throw new ShellExecError_1.default(`Sending command:\n${this.lastCommand}\n cause ${value.trim()}`);
}
// const value = await this.readValue();
const lines = value.split(/[\r\n]+/g).filter(a => a);
const result = [];
for (const line of lines) {
result.push(new IpRouteEntry(line));
}
return result;
}
}
exports.default = IpRouteCommand;
const ValidTypes = ['anycast', 'unicast', 'local', 'broadcast', 'multicast', 'throw', 'unreachable', 'prohibit', 'blackhole', 'nat'];
const ValidTypesSet = new Set(ValidTypes);
/**
* unix route model
* ROUTE := NODE_SPEC [ INFO_SPEC ]
*
* NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ] [ table TABLE_ID ] [ proto RTPROTO ] [ scope SCOPE ] [ metric METRIC ]
*/
class IpRouteEntry {
constructor(line) {
const words = line.split(' ').filter(a => a);
if (ValidTypesSet.has(words[0])) {
this.type = words.shift();
}
if (words[0] === 'default') {
this.dest = words.shift();
}
else if (words[0].match(/^[0-9.]+(\/\d+)?$/)) {
// IP v4
this.dest = words.shift();
}
else if (words[0].match(/^[0-9a-f:]+(\/\d+)?$/)) {
// IP v6
this.dest = words.shift();
}
while (words.length) {
const next = words.shift();
const value = words.shift() || '';
switch (next) {
case 'dev':
case 'via':
case 'src':
case 'tos':
case 'mtu':
case 'expires':
this[next] = value;
break;
case 'metric':
case 'hoplimit':
this[next] = Number(value);
break;
case 'pref':
case 'scope':
case 'table':
case 'proto':
if (value.match(/^\d+$/))
this[next] = Number(value);
else
this[next] = value;
break;
default:
throw Error(`Failed to parse line:\n ${line}\n token: ${next} in ip route response, Fix me in ipRoute.ts`);
}
}
}
toString() {
const out = [];
for (const field of ['type', 'dest']) {
const value = this[field];
if (value)
out.push(value);
}
for (const field of ['via', 'dev', 'table', 'expires', 'proto', 'scope', 'tos', 'src', 'metric', 'pref', 'mtu']) {
if (this[field] || this[field] === 0) {
out.push(field);
out.push(String(this[field]));
}
}
return out.join(' ');
}
clone() {
return new IpRouteEntry(this.toString());
}
}
exports.IpRouteEntry = IpRouteEntry;
//# sourceMappingURL=ipRoute.js.map