sync by worker
This commit is contained in:
@@ -293,8 +293,14 @@ export const DEFAULT_SETTING = {
|
|||||||
|
|
||||||
export const DEFAULT_RULES = [GLOBLA_RULE];
|
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 = {
|
export const DEFAULT_SYNC = {
|
||||||
|
syncType: OPT_SYNCTYPE_WORKER, // 同步方式
|
||||||
syncUrl: "", // 数据同步接口
|
syncUrl: "", // 数据同步接口
|
||||||
|
syncUser: "", // 数据同步用户名
|
||||||
syncKey: "", // 数据同步密钥
|
syncKey: "", // 数据同步密钥
|
||||||
settingUpdateAt: 0,
|
settingUpdateAt: 0,
|
||||||
settingSyncAt: 0,
|
settingSyncAt: 0,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import {
|
|||||||
KV_RULES_KEY,
|
KV_RULES_KEY,
|
||||||
KV_RULES_SHARE_KEY,
|
KV_RULES_SHARE_KEY,
|
||||||
KV_SALT_SHARE,
|
KV_SALT_SHARE,
|
||||||
|
OPT_SYNCTYPE_WEBDAV,
|
||||||
} from "../config";
|
} from "../config";
|
||||||
import {
|
import {
|
||||||
getSyncWithDefault,
|
getSyncWithDefault,
|
||||||
@@ -15,36 +16,59 @@ import {
|
|||||||
import { apiSyncData } from "../apis";
|
import { apiSyncData } from "../apis";
|
||||||
import { sha256 } from "./utils";
|
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
|
* @returns
|
||||||
*/
|
*/
|
||||||
const syncSetting = async (isBg = false, isForce = false) => {
|
const syncSetting = async (isBg = false, isForce = false) => {
|
||||||
let {
|
const {
|
||||||
|
syncType,
|
||||||
syncUrl,
|
syncUrl,
|
||||||
|
syncUser,
|
||||||
syncKey,
|
syncKey,
|
||||||
settingUpdateAt = 0,
|
settingUpdateAt = 0,
|
||||||
settingSyncAt = 0,
|
settingSyncAt = 0,
|
||||||
} = await getSyncWithDefault();
|
} = await getSyncWithDefault();
|
||||||
if (!syncUrl || !syncKey) {
|
if (!syncUrl || !syncKey || (syncType === OPT_SYNCTYPE_WEBDAV && !syncUser)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isForce) {
|
|
||||||
settingUpdateAt = Date.now();
|
|
||||||
}
|
|
||||||
|
|
||||||
const setting = await getSettingWithDefault();
|
const setting = await getSettingWithDefault();
|
||||||
const res = await apiSyncData(
|
const res = await syncByWorker({
|
||||||
`${syncUrl}/sync`,
|
key: KV_SETTING_KEY,
|
||||||
|
value: setting,
|
||||||
|
syncUrl,
|
||||||
syncKey,
|
syncKey,
|
||||||
{
|
updateAt: settingUpdateAt,
|
||||||
key: KV_SETTING_KEY,
|
syncAt: settingSyncAt,
|
||||||
value: setting,
|
isBg,
|
||||||
updateAt: settingSyncAt === 0 ? 0 : settingUpdateAt,
|
isForce,
|
||||||
},
|
});
|
||||||
isBg
|
|
||||||
);
|
|
||||||
|
|
||||||
if (res.updateAt > settingUpdateAt) {
|
if (res.updateAt > settingUpdateAt) {
|
||||||
await setSetting(res.value);
|
await setSetting(res.value);
|
||||||
@@ -70,31 +94,29 @@ export const trySyncSetting = async (isBg = false, isForce = false) => {
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
const syncRules = async (isBg = false, isForce = false) => {
|
const syncRules = async (isBg = false, isForce = false) => {
|
||||||
let {
|
const {
|
||||||
|
syncType,
|
||||||
syncUrl,
|
syncUrl,
|
||||||
|
syncUser,
|
||||||
syncKey,
|
syncKey,
|
||||||
rulesUpdateAt = 0,
|
rulesUpdateAt = 0,
|
||||||
rulesSyncAt = 0,
|
rulesSyncAt = 0,
|
||||||
} = await getSyncWithDefault();
|
} = await getSyncWithDefault();
|
||||||
if (!syncUrl || !syncKey) {
|
if (!syncUrl || !syncKey || (syncType === OPT_SYNCTYPE_WEBDAV && !syncUser)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isForce) {
|
|
||||||
rulesUpdateAt = Date.now();
|
|
||||||
}
|
|
||||||
|
|
||||||
const rules = await getRulesWithDefault();
|
const rules = await getRulesWithDefault();
|
||||||
const res = await apiSyncData(
|
const res = await syncByWorker({
|
||||||
`${syncUrl}/sync`,
|
key: KV_RULES_KEY,
|
||||||
|
value: rules,
|
||||||
|
syncUrl,
|
||||||
syncKey,
|
syncKey,
|
||||||
{
|
updateAt: rulesUpdateAt,
|
||||||
key: KV_RULES_KEY,
|
syncAt: rulesSyncAt,
|
||||||
value: rules,
|
isBg,
|
||||||
updateAt: rulesSyncAt === 0 ? 0 : rulesUpdateAt,
|
isForce,
|
||||||
},
|
});
|
||||||
isBg
|
|
||||||
);
|
|
||||||
|
|
||||||
if (res.updateAt > rulesUpdateAt) {
|
if (res.updateAt > rulesUpdateAt) {
|
||||||
await setRules(res.value);
|
await setRules(res.value);
|
||||||
@@ -121,10 +143,13 @@ export const trySyncRules = async (isBg = false, isForce = false) => {
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const syncShareRules = async ({ rules, syncUrl, syncKey }) => {
|
export const syncShareRules = async ({ rules, syncUrl, syncKey }) => {
|
||||||
await apiSyncData(`${syncUrl}/sync`, syncKey, {
|
await syncByWorker({
|
||||||
key: KV_RULES_SHARE_KEY,
|
key: KV_RULES_SHARE_KEY,
|
||||||
value: rules,
|
value: rules,
|
||||||
|
syncUrl,
|
||||||
|
syncKey,
|
||||||
updateAt: Date.now(),
|
updateAt: Date.now(),
|
||||||
|
syncAt: Date.now(),
|
||||||
});
|
});
|
||||||
const psk = await sha256(syncKey, KV_SALT_SHARE);
|
const psk = await sha256(syncKey, KV_SALT_SHARE);
|
||||||
const shareUrl = `${syncUrl}/rules?psk=${psk}`;
|
const shareUrl = `${syncUrl}/rules?psk=${psk}`;
|
||||||
|
|||||||
Reference in New Issue
Block a user