Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

port to mv3 #229

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion v2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ background/node_modules:
cd background && npm install

background: .FORCE background/node_modules
cd background && tsc --project src/tsconfig.json
cd background && npm run build

ui/node_modules:
git submodule init && git submodule update
Expand Down
3 changes: 0 additions & 3 deletions v2/background/background.html

This file was deleted.

3,358 changes: 3,320 additions & 38 deletions v2/background/package-lock.json

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions v2/background/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{
"name": "background",
"scripts": {
"build": "tsc --project src/tsconfig.json",
"build": "webpack",
"test": "tsc --project spec/tsconfig.json && jasmine"
},
"private": true,
"dependencies": {
"puppeteer": "^5.5.0",
"typescript": "^4.1.3"
},
"devDependencies": {
"@types/chrome": "0.0.127",
"@types/chrome": "0.0.183",
"@types/jasmine": "^3.6.2",
"@types/puppeteer": "^5.4.2",
"jasmine": "^3.6.3"
"jasmine": "^4.1.0",
"puppeteer": "^5.5.0",
"ts-loader": "^9.2.9",
"typescript": "^4.1.3",
"webpack": "^5.72.0",
"webpack-cli": "^4.9.2"
}
}
91 changes: 56 additions & 35 deletions v2/background/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,70 @@ import { Debuggee } from "./debuggee";
import { Interception } from "./interception";
import { Intercepted } from "./request";

chrome.browserAction.onClicked.addListener(async (tab: chrome.tabs.Tab) => {
const openAsync = (config: object): Promise<chrome.windows.Window | undefined> => {
return new Promise((resolve, reject) => {
chrome.windows.create(config, (window) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError.message);
}
resolve(window);
})
})
}

chrome.action.onClicked.addListener(async (tab: chrome.tabs.Tab) => {
let dbg: Debuggee = new Debuggee(tab);
try {
await dbg.attach();
} catch(e) {
console.error(e);
alert('Failed to attach to ' + tab.url + ' - probably because of enterprise policies.');
console.error(e, JSON.stringify(e));
// alert is not supported
// alert('Failed to attach to ' + tab.url + ' - probably because of enterprise policies.');
return;
}
let int: Interception = Interception.build(dbg);
const popup = open('/ui/dist/ui/index.html', `tamperchrome_${tab.id}`, 'menubar=0,innerWidth=900,innerHeight=800');
const int: Interception = Interception.build(dbg);

const popup = await openAsync({
url: '/ui/dist/ui/index.html',
type: 'popup',
width: 900,
height: 800,
});

if (!popup) {
throw new Error('Failed to open UI window');
}
popup.onmessage = async e => {
if (e.data.event == 'capture') {
await int.capture(e.data.pattern || '*');
await int.onRequest(async (req: Intercepted) => {
const mc = new MessageChannel;
popup.postMessage({'event': 'onRequest', request: JSON.parse(JSON.stringify(req))}, origin, [mc.port1]);
mc.port2.onmessage = async (e) => {
await req.continueRequest(e.data.request);
};
});
await int.onResponse(async (res: Intercepted) => {
const mc = new MessageChannel;
const bodyMc = new MessageChannel;
popup.postMessage({'event': 'onResponse', response: JSON.parse(JSON.stringify(res))}, origin, [mc.port1, bodyMc.port1]);
mc.port2.onmessage = async (e) => {
await res.continueResponse(e.data.response);
};
bodyMc.port2.onmessage = async (e) => {
bodyMc.port2.postMessage(await res.getResponseBody());
};
});
} else if (e.data.event == 'reloadTab') {
chrome.tabs.reload(tab.id!, {bypassCache: true});
}
};
popup.onload = () => popup.onunload = () => {
dbg.detach();
popup.close();
}

chrome.runtime.onConnect.addListener(port => {
if (port.name !== 'tamperchrome') return
port.onMessage.addListener(async (msg) => {
if (msg.name === "capture") {
await int.capture(msg.data.pattern || '*');
await int.onRequest(async (req: Intercepted) => {
port.postMessage({ name: 'onRequest', data: { request: req } });
port.onMessage.addListener(async (msg) => {
if (msg.name === req.id) await req.continueRequest(msg.request);
})
});
await int.onResponse(async (res: Intercepted) => {
port.postMessage({ name: 'onResponse', data: { response: res } });
port.onMessage.addListener(async (msg) => {
if (msg.name === res.id + '-res') await res.continueResponse(msg.response);
})
port.onMessage.addListener(async (msg) => {
if (msg.name === res.id + '-body') {
const data = await res.getResponseBody()
port.postMessage({ name: res.id + '-body', data })
}
})
});
} else if (msg.name === "reloadTab") {
chrome.tabs.reload(tab.id!, { bypassCache: true });
}
});
port.onDisconnect.addListener(() => dbg.detach())
});

await dbg.waitForDetach();
popup.close();
await chrome.windows.remove(popup.id!);
});
8 changes: 0 additions & 8 deletions v2/background/src/load.ts

This file was deleted.

16 changes: 0 additions & 16 deletions v2/background/src/tsconfig.json

This file was deleted.

8 changes: 0 additions & 8 deletions v2/background/sw.js

This file was deleted.

8 changes: 8 additions & 0 deletions v2/background/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"strict": true,
"sourceMap": true,
"target": "ES2018",
"alwaysStrict": true
}
}
20 changes: 20 additions & 0 deletions v2/background/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const path = require('path');

module.exports = {
entry: './src/background.ts',
output: {
filename: 'background.js',
path: path.resolve(__dirname, 'out'),
},
resolve: {
extensions: [".ts"],
},
module: {
rules: [
{
test: /\.ts$/,
loader: "ts-loader"
}
]
}
};
12 changes: 7 additions & 5 deletions v2/manifest_base.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{
"manifest_version": 2,
"manifest_version": 3,
"name": "Tamper Dev",
"version": "2",
"description" : "Intercept and edit HTTP/HTTPS requests and responses as they happen without the need of a proxy.",
"icons": {"16": "icons/white.png","48": "icons/96x96.png","128": "icons/256x256.png"},
"browser_action": {
"action": {
"default_title": "Tamper Dev",
"default_icon": {"16": "icons/white.png","48": "icons/white.png","128": "icons/256x256.png"}
},
"background": {
"page": "background/background.html",
"persistent": true
"service_worker": "background/out/background.js"
},
"commands": {
"_execute_browser_action": {
Expand All @@ -22,6 +21,9 @@
},
"minimum_chrome_version": "87",
"permissions": ["debugger", "activeTab"],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"content_security_policy": {
"script-src": "self unsafe-eval",
"object-src": "self"
},
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgXuoF2D68sbW53PO/5gK6ar52c/oANFtGFuxE1SdKKLDdF4RbDZefwK48Bc4Zc2pW81qmHC+H+gMHLh+oiH0TP2l0THjQRRf57f5Wuhk42Tdp0qlyS2qQMky5WOVZU8vQdhnRCktMPLyNWx2PeiUXkQDQ3XkcY0cttPhRAlZgx+qNfguHkmsC7HNZ/Y00TY5uUrlGl9MDt2d6fj1uUyV7pimLMA4cnTIjkf+tWhFSXTEbINdpr/YqnU/lq2I9VonBTjoNkt9hAE3y/tDgmkMh3edqqQh96m7Ker+8ZFnaQr6PrPT0vzfQdwWCnl9ItiyJNuFlCkoOzYlrp1+OIzNaQIDAQAB"
}
Loading