diff --git a/src/config/index.js b/src/config/index.js index 5c6eb14..0edd8e7 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -293,8 +293,14 @@ export const DEFAULT_SETTING = { export const DEFAULT_RULES = [GLOBLA_RULE]; +export const OPT_SYNCTYPE_WORKER = "KISS-Worker"; +export const OPT_SYNCTYPE_WEBDAV = "WebDAV"; +export const OPT_SYNCTYPE_ALL = [OPT_SYNCTYPE_WORKER, OPT_SYNCTYPE_WEBDAV]; + export const DEFAULT_SYNC = { + syncType: OPT_SYNCTYPE_WORKER, // 同步方式 syncUrl: "", // 数据同步接口 + syncUser: "", // 数据同步用户名 syncKey: "", // 数据同步密钥 settingUpdateAt: 0, settingSyncAt: 0, diff --git a/src/libs/sync.js b/src/libs/sync.js index e3a0b92..caef94d 100644 --- a/src/libs/sync.js +++ b/src/libs/sync.js @@ -3,6 +3,7 @@ import { KV_RULES_KEY, KV_RULES_SHARE_KEY, KV_SALT_SHARE, + OPT_SYNCTYPE_WEBDAV, } from "../config"; import { getSyncWithDefault, @@ -15,36 +16,59 @@ import { import { apiSyncData } from "../apis"; import { sha256 } from "./utils"; +const syncByWorker = async ({ + key, + value, + syncUrl, + syncKey, + updateAt = 0, + syncAt = 0, + isBg = false, + isForce = false, +}) => { + if (isForce) { + updateAt = Date.now(); + } + return await apiSyncData( + `${syncUrl}/sync`, + syncKey, + { + key, + value, + updateAt: syncAt === 0 ? 0 : updateAt, + }, + isBg + ); +}; + /** * 同步设置 * @returns */ const syncSetting = async (isBg = false, isForce = false) => { - let { + const { + syncType, syncUrl, + syncUser, syncKey, settingUpdateAt = 0, settingSyncAt = 0, } = await getSyncWithDefault(); - if (!syncUrl || !syncKey) { + if (!syncUrl || !syncKey || (syncType === OPT_SYNCTYPE_WEBDAV && !syncUser)) { return; } - if (isForce) { - settingUpdateAt = Date.now(); - } - const setting = await getSettingWithDefault(); - const res = await apiSyncData( - `${syncUrl}/sync`, + const res = await syncByWorker({ + key: KV_SETTING_KEY, + value: setting, + syncUrl, syncKey, - { - key: KV_SETTING_KEY, - value: setting, - updateAt: settingSyncAt === 0 ? 0 : settingUpdateAt, - }, - isBg - ); + updateAt: settingUpdateAt, + syncAt: settingSyncAt, + isBg, + isForce, + }); if (res.updateAt > settingUpdateAt) { await setSetting(res.value); @@ -70,31 +94,29 @@ export const trySyncSetting = async (isBg = false, isForce = false) => { * @returns */ const syncRules = async (isBg = false, isForce = false) => { - let { + const { + syncType, syncUrl, + syncUser, syncKey, rulesUpdateAt = 0, rulesSyncAt = 0, } = await getSyncWithDefault(); - if (!syncUrl || !syncKey) { + if (!syncUrl || !syncKey || (syncType === OPT_SYNCTYPE_WEBDAV && !syncUser)) { return; } - if (isForce) { - rulesUpdateAt = Date.now(); - } - const rules = await getRulesWithDefault(); - const res = await apiSyncData( - `${syncUrl}/sync`, + const res = await syncByWorker({ + key: KV_RULES_KEY, + value: rules, + syncUrl, syncKey, - { - key: KV_RULES_KEY, - value: rules, - updateAt: rulesSyncAt === 0 ? 0 : rulesUpdateAt, - }, - isBg - ); + updateAt: rulesUpdateAt, + syncAt: rulesSyncAt, + isBg, + isForce, + }); if (res.updateAt > rulesUpdateAt) { await setRules(res.value); @@ -121,10 +143,13 @@ export const trySyncRules = async (isBg = false, isForce = false) => { * @returns */ export const syncShareRules = async ({ rules, syncUrl, syncKey }) => { - await apiSyncData(`${syncUrl}/sync`, syncKey, { + await syncByWorker({ key: KV_RULES_SHARE_KEY, value: rules, + syncUrl, + syncKey, updateAt: Date.now(), + syncAt: Date.now(), }); const psk = await sha256(syncKey, KV_SALT_SHARE); const shareUrl = `${syncUrl}/rules?psk=${psk}`;