2023-07-31 15:08:51 +08:00
|
|
|
import {
|
|
|
|
|
STOKEY_SYNC,
|
|
|
|
|
DEFAULT_SYNC,
|
|
|
|
|
KV_SETTING_KEY,
|
|
|
|
|
KV_RULES_KEY,
|
2023-08-20 23:30:08 +08:00
|
|
|
KV_RULES_SHARE_KEY,
|
2023-07-31 15:08:51 +08:00
|
|
|
STOKEY_SETTING,
|
|
|
|
|
STOKEY_RULES,
|
2023-08-20 23:30:08 +08:00
|
|
|
KV_SALT_SHARE,
|
2023-07-31 15:08:51 +08:00
|
|
|
} from "../config";
|
|
|
|
|
import storage from "../libs/storage";
|
|
|
|
|
import { getSetting, getRules } from ".";
|
|
|
|
|
import { apiSyncData } from "../apis";
|
2023-08-20 23:30:08 +08:00
|
|
|
import { sha256 } from "./utils";
|
2023-07-31 15:08:51 +08:00
|
|
|
|
2023-08-20 23:30:08 +08:00
|
|
|
export const loadSyncOpt = async () =>
|
|
|
|
|
(await storage.getObj(STOKEY_SYNC)) || DEFAULT_SYNC;
|
2023-07-31 15:08:51 +08:00
|
|
|
|
|
|
|
|
export const syncSetting = async () => {
|
2023-08-17 13:27:22 +08:00
|
|
|
try {
|
2023-08-20 23:30:08 +08:00
|
|
|
const { syncUrl, syncKey, settingUpdateAt } = await loadSyncOpt();
|
2023-08-17 13:27:22 +08:00
|
|
|
if (!syncUrl || !syncKey) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-31 15:08:51 +08:00
|
|
|
|
2023-08-17 13:27:22 +08:00
|
|
|
const setting = await getSetting();
|
|
|
|
|
const res = await apiSyncData(syncUrl, syncKey, {
|
|
|
|
|
key: KV_SETTING_KEY,
|
|
|
|
|
value: setting,
|
|
|
|
|
updateAt: settingUpdateAt,
|
2023-07-31 15:08:51 +08:00
|
|
|
});
|
2023-08-17 13:27:22 +08:00
|
|
|
|
|
|
|
|
if (res && res.updateAt > settingUpdateAt) {
|
|
|
|
|
await storage.putObj(STOKEY_SYNC, {
|
|
|
|
|
settingUpdateAt: res.updateAt,
|
|
|
|
|
settingSyncAt: res.updateAt,
|
|
|
|
|
});
|
|
|
|
|
await storage.setObj(STOKEY_SETTING, res.value);
|
|
|
|
|
} else {
|
|
|
|
|
await storage.putObj(STOKEY_SYNC, {
|
|
|
|
|
settingSyncAt: res.updateAt,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log("[sync setting]", err);
|
2023-07-31 15:08:51 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const syncRules = async () => {
|
2023-08-17 13:27:22 +08:00
|
|
|
try {
|
2023-08-20 23:30:08 +08:00
|
|
|
const { syncUrl, syncKey, rulesUpdateAt } = await loadSyncOpt();
|
2023-08-17 13:27:22 +08:00
|
|
|
if (!syncUrl || !syncKey) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-31 15:08:51 +08:00
|
|
|
|
2023-08-17 13:27:22 +08:00
|
|
|
const rules = await getRules();
|
|
|
|
|
const res = await apiSyncData(syncUrl, syncKey, {
|
|
|
|
|
key: KV_RULES_KEY,
|
|
|
|
|
value: rules,
|
|
|
|
|
updateAt: rulesUpdateAt,
|
2023-07-31 15:08:51 +08:00
|
|
|
});
|
2023-08-17 13:27:22 +08:00
|
|
|
|
|
|
|
|
if (res && res.updateAt > rulesUpdateAt) {
|
|
|
|
|
await storage.putObj(STOKEY_SYNC, {
|
|
|
|
|
rulesUpdateAt: res.updateAt,
|
|
|
|
|
rulesSyncAt: res.updateAt,
|
|
|
|
|
});
|
|
|
|
|
await storage.setObj(STOKEY_RULES, res.value);
|
|
|
|
|
} else {
|
|
|
|
|
await storage.putObj(STOKEY_SYNC, {
|
|
|
|
|
rulesSyncAt: res.updateAt,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log("[sync rules]", err);
|
2023-07-31 15:08:51 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-08-20 23:30:08 +08:00
|
|
|
export const syncShareRules = async ({ rules, syncUrl, syncKey }) => {
|
|
|
|
|
await apiSyncData(syncUrl, syncKey, {
|
|
|
|
|
key: KV_RULES_SHARE_KEY,
|
|
|
|
|
value: rules,
|
|
|
|
|
updateAt: Date.now(),
|
|
|
|
|
});
|
|
|
|
|
const psk = await sha256(syncKey, KV_SALT_SHARE);
|
|
|
|
|
const shareUrl = `${syncUrl}?psk=${psk}`;
|
|
|
|
|
return shareUrl;
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-31 15:08:51 +08:00
|
|
|
export const syncAll = async () => {
|
2023-08-17 13:27:22 +08:00
|
|
|
await syncSetting();
|
|
|
|
|
await syncRules();
|
2023-07-31 15:08:51 +08:00
|
|
|
};
|