@u4/adbkit
Version:
A Typescript client for the Android Debug Bridge.
88 lines • 3.11 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IpRuleEntry = void 0;
const command_1 = __importDefault(require("../../command"));
/**
* Usage: ip rule { add | del } SELECTOR ACTION
* ip rule { flush | save | restore }
* ip rule [ list [ SELECTOR ]]
* SELECTOR := [ not ] [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK[/MASK] ]
* [ iif STRING ] [ oif STRING ] [ pref NUMBER ] [ l3mdev ]
* [ uidrange NUMBER-NUMBER ]
* ACTION := [ table TABLE_ID ]
* [ nat ADDRESS ]
* [ realms [SRCREALM/]DSTREALM ]
* [ goto NUMBER ]
* SUPPRESSOR
* SUPPRESSOR := [ suppress_prefixlength NUMBER ]
* [ suppress_ifgroup DEVGROUP ]
* TABLE_ID := [ local | main | default | NUMBER ]
*/
class IpRuleCommand extends command_1.default {
async execute(...args) {
this.sendCommand(['shell:ip', 'rule', ...args].join(' '));
await this.readOKAY();
const data = await this.parser.readAll();
return this.parseIpRule(data.toString());
}
parseIpRule(value) {
const lines = value.split(/[\r\n]+/g).filter(a => a);
const result = [];
for (const line of lines) {
result.push(new IpRuleEntry(line));
}
return result;
}
}
exports.default = IpRuleCommand;
/**
* unix route model
* ROUTE := NODE_SPEC [ INFO_SPEC ]
*
* NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ] [ table TABLE_ID ] [ proto RTPROTO ] [ scope SCOPE ] [ metric METRIC ]
*/
class IpRuleEntry {
constructor(line) {
const words = line.split(/\s+/g).filter(a => a);
const id = words.shift();
if (!id)
throw Error(`Failed to parse line:\n ${line}\n line should start with an id, Fix me in ipRule.ts`);
this.id = Number(id.replace(':', ''));
while (words.length) {
const next = words.shift();
switch (next) {
case 'from':
case 'fwmark':
case 'lookup':
case 'iif':
case 'oif':
case 'uidrange':
this[next] = words.shift();
break;
case 'unreachable':
this[next] = true;
break;
default:
throw Error(`Failed to parse line:\n ${line}\n token: ${next} in ip route response, Fix me in ipRule.ts`);
}
}
}
toStirng() {
const opt = [];
for (const field of ['from', 'fwmark', 'iif', 'oif', 'uidrange', 'lookup']) {
const value = this[field];
if (value) {
opt.push(field);
opt.push(value);
}
}
if (this.unreachable)
opt.push('unreachable');
return `${this.id}:\t${opt.join(' ')}`;
}
}
exports.IpRuleEntry = IpRuleEntry;
//# sourceMappingURL=ipRule.js.map