Skip to content

Commit

Permalink
Support MAC addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
p-l- committed Jan 2, 2023
1 parent fe251de commit 4fafa60
Showing 1 changed file with 114 additions and 1 deletion.
115 changes: 114 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ interface IvreTag {

interface IvreHost {
addr: string;
addresses: {
mac: string[];
};
categories: string[];
tags: IvreTag[];
hostnames: IvreHostname[];
Expand All @@ -110,10 +113,14 @@ enum ElementType {
IpAddress,
IpNetwork,
HostName,
MacAddress,

Unknown,
}

const MAC_ADDRESS =
/^[0-9a-f]{1,2}:[0-9a-f]{1,2}:[0-9a-f]{1,2}:[0-9a-f]{1,2}:[0-9a-f]{1,2}:[0-9a-f]{1,2}$/i;

function ivre_guess_type(element: string): ElementType {
if (isIPAddress(element)) {
return ElementType.IpAddress;
Expand All @@ -133,6 +140,9 @@ function ivre_guess_type(element: string): ElementType {
if (isValidHostname(element)) {
return ElementType.HostName;
}
if (MAC_ADDRESS.test(element)) {
return ElementType.MacAddress;
}
return ElementType.Unknown;
}

Expand Down Expand Up @@ -207,6 +217,21 @@ function ivre_create_category(category: string, base_directory: string) {
const fname = [base, `${category}.md`].join("/");
this.app.vault.create(fname, `Category ${category}\n`);
}
function ivre_create_mac(mac: string, base_directory: string) {
const base = [base_directory, "MAC"].join("/");
create_folder(base);
const fname = [base, `${mac.toLowerCase().replace(/:/g, "")}.md`].join("/");
const ivre_ipdata = spawnSync("ivre", ["macdata", "--json", mac]);
let content = `MAC ${mac.toLowerCase()}\n`;
const data = JSON.parse(ivre_ipdata.stdout.toString());
if ("manufacturer_code" in data) {
content += `- Manufacturer code: ${data.manufacturer_code}\n`;
}
if ("manufacturer_name" in data) {
content += `- Manufacturer name: ${data.manufacturer_name}\n`;
}
this.app.vault.create(fname, content);
}
function ivre_create_hostname(name: string, base_directory: string) {
const base = [base_directory, "Hostname"].join("/");
create_folder(base);
Expand Down Expand Up @@ -398,6 +423,53 @@ function ivre_handle_network(
}
return undefined;
}
function ivre_handle_mac(
mac: string,
settings: IvrePluginSettings
): string | undefined {
const inst = new IvreSearchView();
let one_answer = false;
mac = mac.toLowerCase();
if (settings.use_data) {
const inst_data = new IvreSearchData();
for (const result of inst.process_mac(mac, settings)) {
const data = [];
const data_result = inst_data.process_ipaddress(
result.address,
settings
);
if (data_result) {
data.push(data_result);
}
data.push(result.data);
const base = [settings.base_directory, "IP"].join("/");
create_folder(base);
this.app.vault.create(
[base, `${result.address.replace(/:/g, "_")}.md`].join("/"),
data.join("\n")
);
one_answer = true;
}
} else {
for (const result of inst.process_mac(mac, settings)) {
const base = [settings.base_directory, "IP"].join("/");
create_folder(base);
this.app.vault.create(
[base, `${result.address.replace(/:/g, "_")}.md`].join("/"),
result
);
one_answer = true;
}
}
if (one_answer) {
return [
settings.base_directory,
"MAC",
`${mac.replace(/:/g, "")}.md`,
].join("/");
}
return undefined;
}

class IvreSearch {}

Expand Down Expand Up @@ -471,7 +543,10 @@ class IvreSearchView extends IvreSearch {
let answer = "";
let tmp_answer = "";
(data.tags || []).forEach((tag: IvreTag) => {
tmp_answer += `${tag_emoji(tag)} #${tag.value.replace(" ", "_")}\n`;
tmp_answer += `${tag_emoji(tag)} #${tag.value.replace(
/ /g,
"_"
)}\n`;
});
if (tmp_answer) {
answer += tmp_answer;
Expand All @@ -485,6 +560,16 @@ class IvreSearchView extends IvreSearch {
answer += `\n## Categories ##\n${tmp_answer}`;
}
tmp_answer = "";
((data.addresses || {}).mac || []).forEach((addr: string) => {
ivre_create_mac(addr, settings.base_directory);
tmp_answer += `- [[${settings.base_directory}/MAC/${addr
.toLowerCase()
.replace(/:/g, "")}|${addr}]]\n`;
});
if (tmp_answer) {
answer += `\n## MAC addresses ##\n${tmp_answer}`;
}
tmp_answer = "";
(data.hostnames || []).forEach((hname: IvreHostname) => {
tmp_answer += `- [[${settings.base_directory}/Hostname/${hname.name}|${hname.name}]] (${hname.type})\n`;
ivre_create_hostname(hname.name, settings.base_directory);
Expand Down Expand Up @@ -598,6 +683,26 @@ class IvreSearchView extends IvreSearch {
}
}
}
*process_mac(
mac: string,
settings: IvrePluginSettings
): Generator<IvreMarkdownResult, void, unknown> {
const options = ["view", "--json"];
if (settings.db_url_view) {
options.push("--from-db", settings.db_url_view);
}
options.push("--mac", mac);
const ivre_view = spawnSync("ivre", options);
for (const line of ivre_view.stdout.toString().split(/\r?\n/)) {
const data = this.process_line(line, settings);
if (data) {
yield {
address: JSON.parse(line).addr,
data: `\n# View #\n${data}`,
};
}
}
}
}

function ivre_analyze_selection(
Expand Down Expand Up @@ -633,6 +738,14 @@ function ivre_analyze_selection(
}
break;
}
case ElementType.MacAddress: {
new Notice(`Processing MAC address: ${element}`);
const fname = ivre_handle_mac(element, settings);
if (fname) {
links.push({ element: element, link: fname.slice(0, -3) });
}
break;
}
default: {
if (element) {
new Notice(`Element ignored: ${element}`);
Expand Down

0 comments on commit 4fafa60

Please sign in to comment.