clean up reverse proxy

This commit is contained in:
fyears 2024-04-05 11:06:16 +08:00
parent ae28cf9183
commit 28b99557a8
6 changed files with 35 additions and 24 deletions

View File

@ -27,12 +27,12 @@ export interface S3Config {
remotePrefix?: string; remotePrefix?: string;
useAccurateMTime?: boolean; useAccurateMTime?: boolean;
reverseProxyNoSignUrl?: string;
/** /**
* @deprecated * @deprecated
*/ */
bypassCorsLocally?: boolean; bypassCorsLocally?: boolean;
reverseProxyUrl: string;
} }
export interface DropboxConfig { export interface DropboxConfig {

View File

@ -184,8 +184,8 @@
"settings_s3_accuratemtime_desc": "Read the uploaded accurate last modified time for better sync algorithm. But it causes extra api requests / time / money to the S3 endpoint.", "settings_s3_accuratemtime_desc": "Read the uploaded accurate last modified time for better sync algorithm. But it causes extra api requests / time / money to the S3 endpoint.",
"settings_s3_urlstyle": "S3 URL style", "settings_s3_urlstyle": "S3 URL style",
"settings_s3_urlstyle_desc": "Whether to force path-style URLs for S3 objects (e.g., https://s3.amazonaws.com/*/ instead of https://*.s3.amazonaws.com/).", "settings_s3_urlstyle_desc": "Whether to force path-style URLs for S3 objects (e.g., https://s3.amazonaws.com/*/ instead of https://*.s3.amazonaws.com/).",
"settings_s3_reverse_proxy_url": "S3 Reverse Proxy Url", "settings_s3_reverse_proxy_no_sign_url": "S3 Reverse Proxy (No Sign) Url (experimental)",
"settings_s3_reverse_proxy_url_desc": "S3 Reverse Proxy Url.(Leave blank if you don't have a reverse proxy)", "settings_s3_reverse_proxy_no_sign_url_desc": "S3 reverse proxy url without signature. This is useful if you use a revers proxy but do not change the original credential signature. No http(s):// prefix. Leave it blank if you don't know what it is.",
"settings_s3_connect_succ": "Great! The bucket can be accessed.", "settings_s3_connect_succ": "Great! The bucket can be accessed.",
"settings_s3_connect_fail": "The S3 bucket cannot be reached.", "settings_s3_connect_fail": "The S3 bucket cannot be reached.",
"settings_dropbox": "Remote For Dropbox", "settings_dropbox": "Remote For Dropbox",

View File

@ -183,8 +183,8 @@
"settings_s3_accuratemtime_desc": "读取(已上传的)准确的文件修改时间,有助于同步算法更加准确和稳定。但是它也会导致额外的 api 请求、时间、金钱花费。", "settings_s3_accuratemtime_desc": "读取(已上传的)准确的文件修改时间,有助于同步算法更加准确和稳定。但是它也会导致额外的 api 请求、时间、金钱花费。",
"settings_s3_urlstyle": "S3 URL style", "settings_s3_urlstyle": "S3 URL style",
"settings_s3_urlstyle_desc": "是否对 S3 对象强制使用 path style URL例如使用 https://s3.amazonaws.com/*/ 而不是 https://*.s3.amazonaws.com/)。", "settings_s3_urlstyle_desc": "是否对 S3 对象强制使用 path style URL例如使用 https://s3.amazonaws.com/*/ 而不是 https://*.s3.amazonaws.com/)。",
"settings_s3_reverse_proxy_url": "S3 反向代理地址", "settings_s3_reverse_proxy_no_sign_url": "S3 反向代理(不签名)地址(实验性质)",
"settings_s3_reverse_proxy_url_desc": "S3 反向代理地址。(如果没有设置反向代理请留空)", "settings_s3_reverse_proxy_no_sign_url_desc": "不会参与到签名的 S3 反向代理地址。如果您有一个反向代理,但是不想修改原始鉴权签名,这里就可以填写。没有 http(s):// 前缀。如果您不知道这是什么,留空即可。",
"settings_s3_connect_succ": "很好!可以访问到对应存储桶。", "settings_s3_connect_succ": "很好!可以访问到对应存储桶。",
"settings_s3_connect_fail": "无法访问到对应存储桶。", "settings_s3_connect_fail": "无法访问到对应存储桶。",
"settings_dropbox": "Dropbox 设置", "settings_dropbox": "Dropbox 设置",

View File

@ -182,8 +182,8 @@
"settings_s3_accuratemtime_desc": "讀取(已上傳的)準確的檔案修改時間,有助於同步演算法更加準確和穩定。但是它也會導致額外的 api 請求、時間、金錢花費。", "settings_s3_accuratemtime_desc": "讀取(已上傳的)準確的檔案修改時間,有助於同步演算法更加準確和穩定。但是它也會導致額外的 api 請求、時間、金錢花費。",
"settings_s3_urlstyle": "S3 URL style", "settings_s3_urlstyle": "S3 URL style",
"settings_s3_urlstyle_desc": "是否對 S3 物件強制使用 path style URL例如使用 https://s3.amazonaws.com/*/ 而不是 https://*.s3.amazonaws.com/)。", "settings_s3_urlstyle_desc": "是否對 S3 物件強制使用 path style URL例如使用 https://s3.amazonaws.com/*/ 而不是 https://*.s3.amazonaws.com/)。",
"settings_s3_reverse_proxy_url": "S3 反向代理地址", "settings_s3_reverse_proxy_no_sign_url": "S3 反向代理(不簽名)地址(實驗性質)",
"settings_s3_reverse_proxy_url_desc": "S3 反向代理地址。(如果沒有設置反向代理請留空)", "settings_s3_reverse_proxy_no_sign_url_desc": "不會參與到簽名的 S3 反向代理地址。如果您有一個反向代理,但是不想修改原始鑑權簽名,這裡就可以填寫。沒有 http(s):// 字首。如果您不知道這是什麼,留空即可。",
"settings_s3_connect_succ": "很好!可以訪問到對應儲存桶。", "settings_s3_connect_succ": "很好!可以訪問到對應儲存桶。",
"settings_s3_connect_fail": "無法訪問到對應儲存桶。", "settings_s3_connect_fail": "無法訪問到對應儲存桶。",
"settings_dropbox": "Dropbox 設定", "settings_dropbox": "Dropbox 設定",

View File

@ -56,12 +56,15 @@ import { Cipher } from "./encryptUnified";
*/ */
class ObsHttpHandler extends FetchHttpHandler { class ObsHttpHandler extends FetchHttpHandler {
requestTimeoutInMs: number | undefined; requestTimeoutInMs: number | undefined;
s3Config: S3Config | undefined; reverseProxyNoSignUrl: string | undefined;
constructor(options?: FetchHttpHandlerOptions, s3Config?: S3Config) { constructor(
options?: FetchHttpHandlerOptions,
reverseProxyNoSignUrl?: string
) {
super(options); super(options);
this.requestTimeoutInMs = this.requestTimeoutInMs =
options === undefined ? undefined : options.requestTimeout; options === undefined ? undefined : options.requestTimeout;
this.s3Config = s3Config; this.reverseProxyNoSignUrl = reverseProxyNoSignUrl;
} }
async handle( async handle(
request: HttpRequest, request: HttpRequest,
@ -82,10 +85,16 @@ class ObsHttpHandler extends FetchHttpHandler {
} }
const { port, method } = request; const { port, method } = request;
let url = `${request.protocol}//${request.hostname}${port ? `:${port}` : "" let url = `${request.protocol}//${request.hostname}${
port ? `:${port}` : ""
}${path}`; }${path}`;
if (this.s3Config && this.s3Config.reverseProxyUrl && this.s3Config.reverseProxyUrl !== "") { if (
url = url.replace(this.s3Config.s3Endpoint, this.s3Config.reverseProxyUrl); this.reverseProxyNoSignUrl !== undefined &&
this.reverseProxyNoSignUrl !== ""
) {
const urlObj = new URL(url);
urlObj.host = this.reverseProxyNoSignUrl;
url = urlObj.href;
} }
const body = const body =
method === "GET" || method === "HEAD" ? undefined : request.body; method === "GET" || method === "HEAD" ? undefined : request.body;
@ -171,7 +180,7 @@ export const DEFAULT_S3_CONFIG: S3Config = {
forcePathStyle: false, forcePathStyle: false,
remotePrefix: "", remotePrefix: "",
useAccurateMTime: false, // it causes money, disable by default useAccurateMTime: false, // it causes money, disable by default
reverseProxyUrl: "", reverseProxyNoSignUrl: "",
}; };
export type S3ObjectType = _Object; export type S3ObjectType = _Object;
@ -310,9 +319,7 @@ export const getS3Client = (s3Config: S3Config) => {
} }
let s3Client: S3Client; let s3Client: S3Client;
if ((VALID_REQURL && s3Config.bypassCorsLocally) || (s3Config.reverseProxyUrl && s3Config.reverseProxyUrl !== "")) { if (VALID_REQURL && s3Config.bypassCorsLocally) {
console.log("reverseProxyUrl", s3Config.reverseProxyUrl);
s3Client = new S3Client({ s3Client = new S3Client({
region: s3Config.s3Region, region: s3Config.s3Region,
endpoint: endpoint, endpoint: endpoint,
@ -321,7 +328,10 @@ export const getS3Client = (s3Config: S3Config) => {
accessKeyId: s3Config.s3AccessKeyID, accessKeyId: s3Config.s3AccessKeyID,
secretAccessKey: s3Config.s3SecretAccessKey, secretAccessKey: s3Config.s3SecretAccessKey,
}, },
requestHandler: new ObsHttpHandler(undefined, s3Config), requestHandler: new ObsHttpHandler(
undefined,
s3Config.reverseProxyNoSignUrl
),
}); });
} else { } else {
s3Client = new S3Client({ s3Client = new S3Client({

View File

@ -976,7 +976,8 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
dropdown dropdown
.setValue( .setValue(
`${this.plugin.settings.s3.bypassCorsLocally ? "enable" : "disable" `${
this.plugin.settings.s3.bypassCorsLocally ? "enable" : "disable"
}` }`
) )
.onChange(async (value) => { .onChange(async (value) => {
@ -1056,14 +1057,14 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
}); });
}); });
new Setting(s3Div) new Setting(s3Div)
.setName(t("settings_s3_reverse_proxy_url")) .setName(t("settings_s3_reverse_proxy_no_sign_url"))
.setDesc(t("settings_s3_reverse_proxy_url_desc")) .setDesc(t("settings_s3_reverse_proxy_no_sign_url_desc"))
.addText((text) => .addText((text) =>
text text
.setPlaceholder("") .setPlaceholder("")
.setValue(this.plugin.settings.s3.reverseProxyUrl) .setValue(this.plugin.settings.s3.reverseProxyNoSignUrl ?? "")
.onChange(async (value) => { .onChange(async (value) => {
this.plugin.settings.s3.reverseProxyUrl = value.trim(); this.plugin.settings.s3.reverseProxyNoSignUrl = value.trim();
await this.plugin.saveSettings(); await this.plugin.saveSettings();
}) })
); );