-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathollama-origin-bypass.js
More file actions
87 lines (80 loc) · 2.14 KB
/
Copy pathollama-origin-bypass.js
File metadata and controls
87 lines (80 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Ollama rejects chrome-extension:// Origin headers with HTTP 403.
* Strip Origin/Referer on local Ollama requests so chat works without
* requiring users to set OLLAMA_ORIGINS.
*/
const OLLAMA_ORIGIN_BYPASS_RULE_IDS = Object.freeze([11434, 11435]);
/** @type {Promise<void> | null} */
let installPromise = null;
/**
* @returns {void}
*/
export function resetOllamaOriginBypassMemoForTests() {
installPromise = null;
}
/**
* Install (or re-assert) DNR rules that strip Origin/Referer for local Ollama.
* Concurrent callers share one in-flight install so early chat/list-models
* requests wait for the rules before fetching. Failed installs clear the
* memo so a later call can retry.
*
* @returns {Promise<void>}
*/
export function ensureOllamaOriginBypass() {
if (!installPromise) {
installPromise = installOllamaOriginBypass().catch((err) => {
console.warn("Failed to install Ollama Origin bypass rule", err);
installPromise = null;
throw err;
});
}
return installPromise;
}
/**
* @returns {Promise<void>}
*/
async function installOllamaOriginBypass() {
if (
typeof chrome === "undefined" ||
!chrome.declarativeNetRequest?.updateDynamicRules
) {
return;
}
/** @type {chrome.declarativeNetRequest.Rule[]} */
const rules = [
{
id: 11434,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [
{ header: "origin", operation: "remove" },
{ header: "referer", operation: "remove" },
],
},
condition: {
urlFilter: "||localhost:11434^",
resourceTypes: ["xmlhttprequest", "other"],
},
},
{
id: 11435,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [
{ header: "origin", operation: "remove" },
{ header: "referer", operation: "remove" },
],
},
condition: {
urlFilter: "||127.0.0.1:11434^",
resourceTypes: ["xmlhttprequest", "other"],
},
},
];
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [...OLLAMA_ORIGIN_BYPASS_RULE_IDS],
addRules: rules,
});
}