Creating a Chromium Extension to Mimic --host-rules Behavior
Yes, it's possible to create a Manifest V3 extension that achieves similar functionality to --host-rules, though with some limitations compared to the native browser flag. Here's how you could approach this for your example with bbb.github.io and aaa.github.io.
Key Technical Challenges
-
SNI (Server Name Indication) modification: This is the most challenging part as extensions don't have direct access to modify TLS handshake parameters.
-
HTTP header modification: This is more straightforward with webRequest API.
Implementation Approach
1. Manifest V3 Structure
{
"manifest_version": 3,
"name": "Host Rules Extension",
"version": "1.0",
"permissions": [
"webRequest",
"webRequestBlocking",
"declarativeNetRequest",
"declarativeNetRequestWithHostAccess"
],
"host_permissions": [
"*://bbb.github.io/*",
"*://aaa.github.io/*"
],
"background": {
"service_worker": "background.js"
}
}
2. Background Script (background.js)
// This will intercept requests to bbb.github.io and modify them
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
// Check if this is a request to bbb.github.io
if (details.url.includes('bbb.github.io')) {
// Modify the Host header to keep it as bbb.github.io
const headers = details.requestHeaders || [];
const hostHeader = headers.find(h => h.name.toLowerCase() === 'host');
if (hostHeader) {
hostHeader.value = 'bbb.github.io';
} else {
headers.push({name: 'Host', value: 'bbb.github.io'});
}
// This is where we'd try to modify SNI, but it's not directly possible
// We can only modify headers, not the TLS handshake
return {requestHeaders: headers};
}
return {requestHeaders: details.requestHeaders};
},
{urls: ["*://bbb.github.io/*"]},
["blocking", "requestHeaders"]
);
// For declarative rules (alternative approach)
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [{
id: 1,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [{
header: "Host",
operation: "set",
value: "bbb.github.io"
}]
},
condition: {
urlFilter: "*://bbb.github.io/*",
resourceTypes: ["main_frame", "sub_frame", "stylesheet", "script", "image"]
}
}]
});
Limitations
-
SNI Modification: This is the critical limitation. Extensions cannot directly modify the SNI field in the TLS handshake. The --host-rules flag works at a lower level than what extensions can access.
-
Performance Impact: Extensions that modify requests can impact page load performance.
-
Security Restrictions: Chrome imposes security restrictions on what extensions can modify.
Alternative Approaches
-
Proxy Server: You could create a local proxy server that modifies SNI and headers, then configure the extension to route traffic through this proxy.
-
Native Messaging: Use a native application (via native messaging) to handle the SNI modification.
-
Enterprise Policy: If this is for internal use, consider using Chrome's enterprise policies which might offer more control.
Conclusion
While you can achieve partial functionality with an extension (modifying HTTP headers), the full --host-rules behavior including SNI modification isn't possible with a pure extension approach. You would need to combine extension techniques with either a proxy or native application for complete functionality.