2018-11-12 01:18:21 +01:00
|
|
|
const unix = {
|
|
|
|
EPERM: 1,
|
|
|
|
ENOENT: 2,
|
|
|
|
ESRCH: 3,
|
|
|
|
EINTR: 4,
|
|
|
|
EIO: 5,
|
|
|
|
ENXIO: 6,
|
2019-02-28 08:38:12 +01:00
|
|
|
ENOTSUP: 45,
|
2018-11-12 01:18:21 +01:00
|
|
|
|
|
|
|
// Unix file types
|
|
|
|
S_IFMT: 0170000, // type of file mask
|
|
|
|
S_IFIFO: 010000, // named pipe (fifo)
|
|
|
|
S_IFCHR: 020000, // character special
|
|
|
|
S_IFDIR: 040000, // directory
|
|
|
|
S_IFBLK: 060000, // block special
|
|
|
|
S_IFREG: 0100000, // regular
|
|
|
|
S_IFLNK: 0120000, // symbolic link
|
|
|
|
S_IFSOCK: 0140000, // socket
|
|
|
|
}
|
|
|
|
|
2018-11-22 11:52:54 +01:00
|
|
|
function UnixError(error) {
|
|
|
|
this.name = "UnixError";
|
|
|
|
this.error = error;
|
|
|
|
}
|
|
|
|
UnixError.prototype = Error.prototype;
|
|
|
|
|
2018-11-24 07:15:29 +01:00
|
|
|
function getTab(id) {
|
|
|
|
return new Promise((resolve, reject) => chrome.tabs.get(id, resolve));
|
|
|
|
}
|
2018-11-15 08:52:26 +01:00
|
|
|
function queryTabs() {
|
|
|
|
return new Promise((resolve, reject) => chrome.tabs.query({}, resolve));
|
|
|
|
}
|
|
|
|
|
2019-02-28 10:38:22 +01:00
|
|
|
async function debugTab(tabId) {
|
|
|
|
if (!debugged[tabId]) {
|
|
|
|
await new Promise(resolve => chrome.debugger.attach({tabId}, "1.3", resolve));
|
|
|
|
debugged[tabId] = 0;
|
|
|
|
}
|
|
|
|
debugged[tabId] += 1;
|
|
|
|
}
|
2019-02-26 08:08:52 +01:00
|
|
|
function sendDebuggerCommand(tabId, method, commandParams) {
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
chrome.debugger.sendCommand({tabId}, method, commandParams, result => {
|
|
|
|
console.log(method, result);
|
|
|
|
if (result) {
|
|
|
|
resolve(result);
|
|
|
|
} else {
|
|
|
|
reject(chrome.runtime.lastError);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2018-11-24 07:15:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// tabs/by-id/ID/title
|
|
|
|
// tabs/by-id/ID/url
|
|
|
|
// tabs/by-id/ID/console
|
|
|
|
// tabs/by-id/ID/mem (?)
|
|
|
|
// tabs/by-id/ID/cpu (?)
|
|
|
|
// tabs/by-id/ID/screenshot.png
|
2018-11-29 06:43:00 +01:00
|
|
|
// tabs/by-id/ID/text.txt
|
2018-11-24 07:15:29 +01:00
|
|
|
// tabs/by-id/ID/printed.pdf
|
|
|
|
// tabs/by-id/ID/control
|
|
|
|
// tabs/by-id/ID/sources/
|
|
|
|
|
|
|
|
function pathComponent(path, i) {
|
|
|
|
const components = path.split('/');
|
|
|
|
return components[i >= 0 ? i : components.length + i];
|
|
|
|
}
|
2019-02-26 08:08:52 +01:00
|
|
|
function sanitize(s) {
|
|
|
|
return s.replace(/[^A-Za-z0-9_\-\.]/gm, '_');
|
|
|
|
}
|
2018-11-24 07:15:29 +01:00
|
|
|
|
2019-02-25 22:02:25 +01:00
|
|
|
const debugged = {};
|
|
|
|
|
2018-11-15 08:52:26 +01:00
|
|
|
const router = {
|
|
|
|
"tabs": {
|
2018-11-29 18:53:03 +01:00
|
|
|
/* "last-focused": {
|
|
|
|
* // FIXME: symlink to tab by id.
|
|
|
|
* async readlink() {
|
|
|
|
* return "../windows/last-focused/selected-tab"
|
|
|
|
* }
|
|
|
|
* },
|
|
|
|
*/
|
2019-02-28 10:05:28 +01:00
|
|
|
"by-title": {
|
|
|
|
async readdir() {
|
|
|
|
const tabs = await queryTabs();
|
2019-03-02 10:48:56 +01:00
|
|
|
return tabs.map(tab => sanitize(String(tab.title).slice(0, 200)) + "_" + String(tab.id));
|
2019-02-28 10:05:28 +01:00
|
|
|
},
|
|
|
|
"*": {
|
|
|
|
async getattr(path) {
|
|
|
|
const st_size = (await this.readlink(path)).length + 1;
|
|
|
|
return {
|
|
|
|
st_mode: unix.S_IFLNK | 0444,
|
|
|
|
st_nlink: 1,
|
|
|
|
// You _must_ return correct linkee path length from getattr!
|
|
|
|
st_size
|
|
|
|
};
|
|
|
|
},
|
|
|
|
async readlink(path) {
|
|
|
|
const parts = path.split("_");
|
|
|
|
const id = parts[parts.length - 1];
|
|
|
|
return "../by-id/" + id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-11-15 08:52:26 +01:00
|
|
|
"by-id": {
|
|
|
|
async readdir() {
|
|
|
|
const tabs = await queryTabs();
|
|
|
|
return tabs.map(tab => String(tab.id));
|
|
|
|
},
|
|
|
|
|
|
|
|
"*": {
|
2018-11-24 07:15:29 +01:00
|
|
|
"url": {
|
|
|
|
async read(path, fh, size, offset) {
|
2018-11-24 19:58:33 +01:00
|
|
|
const tab = await getTab(parseInt(pathComponent(path, -2)));
|
|
|
|
return (tab.url + "\n").substr(offset, size);
|
2018-11-29 06:43:00 +01:00
|
|
|
}
|
2018-11-24 19:58:33 +01:00
|
|
|
},
|
|
|
|
"title": {
|
|
|
|
async read(path, fh, size, offset) {
|
|
|
|
const tab = await getTab(parseInt(pathComponent(path, -2)));
|
|
|
|
return (tab.title + "\n").substr(offset, size);
|
2018-11-29 06:43:00 +01:00
|
|
|
}
|
2018-11-24 07:15:29 +01:00
|
|
|
},
|
2019-02-28 10:38:22 +01:00
|
|
|
"text": {
|
2019-02-28 10:19:37 +01:00
|
|
|
async read(path, fh, size, offset) {
|
|
|
|
const tabId = parseInt(pathComponent(path, -2));
|
2019-02-28 10:38:22 +01:00
|
|
|
await debugTab(tabId);
|
2019-02-28 10:19:37 +01:00
|
|
|
await sendDebuggerCommand(tabId, "Runtime.enable", {});
|
|
|
|
const {result} = await sendDebuggerCommand(tabId, "Runtime.evaluate", {expression: "document.body.innerText", returnByValue: true});
|
|
|
|
return result.value.substr(offset, size)
|
|
|
|
}
|
|
|
|
},
|
2019-02-28 10:38:22 +01:00
|
|
|
"snapshot.mhtml": {
|
|
|
|
async read(path, fh, size, offset) {
|
|
|
|
const tabId = parseInt(pathComponent(path, -2));
|
|
|
|
await debugTab(tabId);
|
|
|
|
await sendDebuggerCommand(tabId, "Page.enable", {});
|
|
|
|
|
|
|
|
const {data} = await sendDebuggerCommand(tabId, "Page.captureSnapshot");
|
|
|
|
return data.substr(offset, size)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"screenshot.png": {
|
|
|
|
// Broken. Filesystem hangs (? in JS?) and needs to be killed if you read this.
|
|
|
|
async read(path, fh, size, offset) {
|
|
|
|
const tabId = parseInt(pathComponent(path, -2));
|
|
|
|
await debugTab(tabId);
|
|
|
|
await sendDebuggerCommand(tabId, "Page.enable", {});
|
|
|
|
|
|
|
|
const {data} = await sendDebuggerCommand(tabId, "Page.captureScreenshot");
|
|
|
|
const buf = btoa(atob(data).substr(offset, size));
|
|
|
|
return { buf, base64Encoded: true };
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-02-28 10:22:33 +01:00
|
|
|
"resources": {
|
2019-02-25 22:02:25 +01:00
|
|
|
async opendir(path) {
|
|
|
|
const tabId = parseInt(pathComponent(path, -2));
|
2019-02-28 10:38:22 +01:00
|
|
|
await debugTab(tabId);
|
2019-02-25 22:02:25 +01:00
|
|
|
return 0;
|
2018-11-29 18:53:03 +01:00
|
|
|
},
|
2019-02-25 22:02:25 +01:00
|
|
|
async readdir(path) {
|
|
|
|
const tabId = parseInt(pathComponent(path, -2));
|
|
|
|
if (!debugged[tabId]) throw new UnixError(unix.EIO);
|
2019-02-26 08:08:52 +01:00
|
|
|
|
|
|
|
const {frameTree} = await sendDebuggerCommand(tabId, "Page.getResourceTree", {});
|
|
|
|
return frameTree.resources.map(r => sanitize(String(r.url).slice(0, 200)));
|
2018-11-29 18:53:03 +01:00
|
|
|
},
|
2019-02-25 22:02:25 +01:00
|
|
|
async releasedir(path) {
|
|
|
|
return 0;
|
2019-02-26 08:08:52 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"*": {
|
|
|
|
async read(path, fh, size, offset) {
|
|
|
|
const tabId = parseInt(pathComponent(path, -3));
|
|
|
|
const suffix = pathComponent(path, -1);
|
|
|
|
|
|
|
|
if (!debugged[tabId]) throw new UnixError(unix.EIO);
|
|
|
|
|
|
|
|
await sendDebuggerCommand(tabId, "Page.enable", {});
|
|
|
|
|
|
|
|
const {frameTree} = await sendDebuggerCommand(tabId, "Page.getResourceTree", {});
|
|
|
|
for (let resource of frameTree.resources) {
|
|
|
|
const resourceSuffix = sanitize(String(resource.url).slice(0, 200));
|
|
|
|
if (resourceSuffix === suffix) {
|
2019-02-28 07:06:37 +01:00
|
|
|
let {base64Encoded, content} = await sendDebuggerCommand(tabId, "Page.getResourceContent", {
|
2019-02-26 08:08:52 +01:00
|
|
|
frameId: frameTree.frame.id,
|
|
|
|
url: resource.url
|
|
|
|
});
|
2019-02-28 07:06:37 +01:00
|
|
|
if (base64Encoded) {
|
|
|
|
const buf = btoa(atob(content).substr(offset, size));
|
|
|
|
return { buf, base64Encoded: true };
|
|
|
|
}
|
2019-02-26 08:08:52 +01:00
|
|
|
return content.substr(offset, size);
|
|
|
|
}
|
|
|
|
}
|
2019-02-28 07:06:37 +01:00
|
|
|
throw new UnixError(unix.ENOENT);
|
2019-02-26 08:08:52 +01:00
|
|
|
}
|
2018-11-29 18:53:03 +01:00
|
|
|
}
|
2019-03-02 21:16:08 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"control": {
|
|
|
|
async write(path, buf) {
|
|
|
|
const tabId = parseInt(pathComponent(path, -2));
|
|
|
|
if (buf.trim() === 'close') {
|
|
|
|
await new Promise(resolve => chrome.tabs.remove(tabId, resolve));
|
|
|
|
} else {
|
|
|
|
throw new UnixError(unix.EIO);
|
|
|
|
}
|
|
|
|
}
|
2018-11-29 18:53:03 +01:00
|
|
|
}
|
2018-11-15 08:52:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-11-12 01:31:02 +01:00
|
|
|
|
2018-11-15 08:52:26 +01:00
|
|
|
function findRoute(path) {
|
|
|
|
let route = router;
|
2019-03-02 10:48:56 +01:00
|
|
|
let pathSegments = path.split("/");
|
|
|
|
if (pathSegments[pathSegments.length - 1].startsWith("._")) {
|
|
|
|
throw new UnixError(unix.ENOTSUP); // Apple Double file for xattrs
|
|
|
|
}
|
|
|
|
for (let segment of pathSegments) {
|
2018-11-15 08:52:26 +01:00
|
|
|
if (segment === "") continue;
|
|
|
|
route = route[segment] || route["*"];
|
2018-11-12 01:31:02 +01:00
|
|
|
|
2018-11-22 11:52:54 +01:00
|
|
|
if (!route) throw new UnixError(unix.ENOENT);
|
2018-11-15 08:52:26 +01:00
|
|
|
}
|
2018-11-22 11:52:54 +01:00
|
|
|
return route;
|
2018-11-15 08:52:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getattr(path) {
|
|
|
|
let route = findRoute(path);
|
|
|
|
if (route.getattr) {
|
2018-11-24 07:15:29 +01:00
|
|
|
return route.getattr(path);
|
2019-03-02 21:16:08 +01:00
|
|
|
} else if (route.read || route.write) {
|
2018-11-29 06:43:00 +01:00
|
|
|
// default file attrs
|
|
|
|
return {
|
2019-03-02 21:16:08 +01:00
|
|
|
st_mode: unix.S_IFREG | ((route.read && 0444) || (route.write && 0222)),
|
2018-11-29 06:43:00 +01:00
|
|
|
st_nlink: 1,
|
|
|
|
st_size: 100 // FIXME
|
|
|
|
};
|
2018-11-12 01:31:02 +01:00
|
|
|
} else {
|
2018-11-29 06:43:00 +01:00
|
|
|
// default dir attrs
|
2018-11-15 08:52:26 +01:00
|
|
|
return {
|
|
|
|
st_mode: unix.S_IFDIR | 0755,
|
|
|
|
st_nlink: 3
|
|
|
|
};
|
2018-11-12 01:31:02 +01:00
|
|
|
}
|
2018-11-22 11:52:54 +01:00
|
|
|
}
|
|
|
|
|
2018-11-24 07:15:29 +01:00
|
|
|
async function open(path) {
|
|
|
|
let route = findRoute(path);
|
|
|
|
if (route.open) return route.open(path);
|
2018-11-29 18:53:03 +01:00
|
|
|
else return 0; // empty fh
|
2018-11-24 07:15:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function read(path, fh, size, offset) {
|
|
|
|
let route = findRoute(path);
|
|
|
|
if (route.read) return route.read(path, fh, size, offset);
|
|
|
|
}
|
2019-03-02 21:16:08 +01:00
|
|
|
async function write(path, buf, offset) {
|
2019-02-28 10:05:28 +01:00
|
|
|
let route = findRoute(path);
|
2019-03-02 21:16:08 +01:00
|
|
|
if (route.write) return route.write(path, buf, offset);
|
2019-02-28 10:05:28 +01:00
|
|
|
}
|
2018-11-24 07:15:29 +01:00
|
|
|
async function release(path, fh) {
|
|
|
|
let route = findRoute(path);
|
2018-11-29 06:43:00 +01:00
|
|
|
if (route.release) return route.release(path, fh);
|
2018-11-24 07:15:29 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 21:16:08 +01:00
|
|
|
async function readlink(path) {
|
|
|
|
let route = findRoute(path);
|
|
|
|
if (route.readlink) return route.readlink(path);
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:53:03 +01:00
|
|
|
async function opendir(path) {
|
|
|
|
let route = findRoute(path);
|
|
|
|
if (route.opendir) return route.opendir(path);
|
|
|
|
else return 0; // empty fh
|
|
|
|
}
|
|
|
|
async function readdir(path) {
|
|
|
|
let route = findRoute(path);
|
|
|
|
if (route.readdir) return route.readdir(path);
|
|
|
|
return Object.keys(route);
|
|
|
|
}
|
|
|
|
async function releasedir(path) {
|
|
|
|
let route = findRoute(path);
|
|
|
|
if (route.releasedir) return route.releasedir(path);
|
|
|
|
}
|
|
|
|
|
2020-10-22 00:13:37 +02:00
|
|
|
function log(...ss) {
|
|
|
|
console.log(...ss);
|
|
|
|
}
|
|
|
|
|
2020-10-17 06:56:49 +02:00
|
|
|
let port;
|
|
|
|
/* let ws;*/
|
|
|
|
async function onMessage(req) {
|
2020-10-22 00:13:37 +02:00
|
|
|
log('req', req);
|
2018-11-11 14:32:44 +01:00
|
|
|
|
2018-11-22 11:52:54 +01:00
|
|
|
let response = { op: req.op, error: unix.EIO };
|
2018-11-24 19:58:33 +01:00
|
|
|
/* console.time(req.op + ':' + req.path);*/
|
2018-11-22 11:52:54 +01:00
|
|
|
try {
|
2018-11-24 07:15:29 +01:00
|
|
|
if (req.op === 'getattr') {
|
2018-11-22 11:52:54 +01:00
|
|
|
response = {
|
2018-11-24 07:15:29 +01:00
|
|
|
op: 'getattr',
|
2018-11-22 11:52:54 +01:00
|
|
|
st_mode: 0,
|
|
|
|
st_nlink: 0,
|
|
|
|
st_size: 0,
|
|
|
|
...(await getattr(req.path))
|
|
|
|
};
|
2018-11-24 07:15:29 +01:00
|
|
|
} else if (req.op === 'open') {
|
|
|
|
response = {
|
|
|
|
op: 'open',
|
|
|
|
fh: await open(req.path)
|
|
|
|
};
|
2018-11-12 01:31:02 +01:00
|
|
|
|
2018-11-24 07:15:29 +01:00
|
|
|
} else if (req.op === 'read') {
|
2019-02-28 07:06:37 +01:00
|
|
|
const ret = await read(req.path, req.fh, req.size, req.offset)
|
|
|
|
const buf = typeof ret === 'string' ? ret : ret.buf;
|
2018-11-24 07:15:29 +01:00
|
|
|
response = {
|
|
|
|
op: 'read',
|
2018-11-29 06:43:00 +01:00
|
|
|
buf
|
2018-11-24 07:15:29 +01:00
|
|
|
};
|
2019-02-28 07:06:37 +01:00
|
|
|
if (ret.base64Encoded) response.base64Encoded = ret.base64Encoded;
|
2018-11-24 07:15:29 +01:00
|
|
|
|
2019-03-02 21:16:08 +01:00
|
|
|
} else if (req.op === 'write') {
|
|
|
|
// FIXME: decide whether base64 should be handled here
|
|
|
|
// or in a higher layer?
|
|
|
|
const ret = await write(req.path, atob(req.buf), req.offset)
|
|
|
|
response = {
|
|
|
|
op: 'write'
|
|
|
|
};
|
|
|
|
|
2018-11-24 07:15:29 +01:00
|
|
|
} else if (req.op === 'release') {
|
|
|
|
await release(req.path, req.fh);
|
2018-11-22 11:52:54 +01:00
|
|
|
response = {
|
2018-11-24 07:15:29 +01:00
|
|
|
op: 'release'
|
2018-11-22 11:52:54 +01:00
|
|
|
};
|
2018-11-29 18:53:03 +01:00
|
|
|
|
2019-02-28 10:05:28 +01:00
|
|
|
} else if (req.op === 'readlink') {
|
|
|
|
const buf = await readlink(req.path)
|
|
|
|
response = {
|
|
|
|
op: 'readlink',
|
|
|
|
buf
|
|
|
|
};
|
|
|
|
|
2018-11-29 18:53:03 +01:00
|
|
|
} else if (req.op === 'opendir') {
|
|
|
|
response = {
|
|
|
|
op: 'opendir',
|
|
|
|
fh: await opendir(req.path)
|
|
|
|
};
|
|
|
|
|
|
|
|
} else if (req.op === 'readdir') {
|
|
|
|
response = {
|
|
|
|
op: 'readdir',
|
|
|
|
entries: [".", "..", ...(await readdir(req.path))]
|
|
|
|
};
|
|
|
|
|
|
|
|
} else if (req.op === 'releasedir') {
|
|
|
|
await releasedir(req.path, req.fh);
|
|
|
|
response = { op: 'releasedir' };
|
2018-11-22 11:52:54 +01:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-02-26 08:08:52 +01:00
|
|
|
console.error(e);
|
2018-11-11 20:44:36 +01:00
|
|
|
response = {
|
2018-11-22 11:52:54 +01:00
|
|
|
op: req.op,
|
|
|
|
error: e instanceof UnixError ? e.error : unix.EIO
|
|
|
|
}
|
2018-11-12 01:18:21 +01:00
|
|
|
}
|
2018-11-24 19:58:33 +01:00
|
|
|
/* console.timeEnd(req.op + ':' + req.path);*/
|
2018-11-11 14:32:44 +01:00
|
|
|
|
2020-10-22 00:13:37 +02:00
|
|
|
log('resp', response);
|
|
|
|
/* ws.send(JSON.stringify(response));*/
|
2018-11-11 14:32:44 +01:00
|
|
|
};
|
2018-11-29 18:53:03 +01:00
|
|
|
|
|
|
|
function tryConnect() {
|
2020-10-17 06:56:49 +02:00
|
|
|
port = chrome.runtime.connectNative('com.rsnous.TabFS');
|
2020-10-22 00:13:37 +02:00
|
|
|
/* console.log('hello', port);*/
|
|
|
|
/* updateToolbarIcon();*/
|
2020-10-17 06:56:49 +02:00
|
|
|
port.onMessage.addListener(onMessage);
|
2020-10-22 00:13:37 +02:00
|
|
|
port.onDisconnect.addListener(p => {log(p)});
|
2020-10-17 06:56:49 +02:00
|
|
|
|
|
|
|
/* ws = new WebSocket("ws://localhost:8888");
|
|
|
|
* updateToolbarIcon();
|
|
|
|
* ws.onopen = ws.onclose = updateToolbarIcon;
|
|
|
|
* ws.onmessage = onmessage;*/
|
2018-11-29 18:53:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateToolbarIcon() {
|
2020-10-17 06:56:49 +02:00
|
|
|
if (port && port.onMessage) { // OPEN
|
2018-11-29 18:53:03 +01:00
|
|
|
chrome.browserAction.setBadgeBackgroundColor({color: 'blue'});
|
|
|
|
chrome.browserAction.setBadgeText({text: 'f'});
|
|
|
|
} else {
|
|
|
|
chrome.browserAction.setBadgeBackgroundColor({color: 'red'});
|
|
|
|
chrome.browserAction.setBadgeText({text: '!'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tryConnect();
|
|
|
|
chrome.browserAction.onClicked.addListener(function() {
|
|
|
|
tryConnect();
|
|
|
|
});
|