feat:新增IP个性化配置
This commit is contained in:
@@ -13,6 +13,9 @@ import type {
|
||||
RouteEnabledMap,
|
||||
RouteRow,
|
||||
RouteStatusConfig,
|
||||
IpRule,
|
||||
IpRuleRow,
|
||||
IpRuleConfig,
|
||||
} from "./types";
|
||||
|
||||
export function dbRun(sql: string, params: unknown[] = []): Promise<void> {
|
||||
@@ -129,6 +132,19 @@ export async function initDatabase(): Promise<void> {
|
||||
value TEXT NOT NULL
|
||||
)
|
||||
`);
|
||||
await dbRun(`
|
||||
CREATE TABLE IF NOT EXISTS ip_rules (
|
||||
ip TEXT PRIMARY KEY,
|
||||
blocked INTEGER NOT NULL DEFAULT 0,
|
||||
remark TEXT NOT NULL DEFAULT '',
|
||||
use_custom_config INTEGER NOT NULL DEFAULT 0,
|
||||
mock_enabled INTEGER,
|
||||
default_content_type TEXT,
|
||||
target_host TEXT,
|
||||
target_port INTEGER,
|
||||
target_https INTEGER
|
||||
)
|
||||
`);
|
||||
// 清理不该出现在 mock 列表中的系统文件
|
||||
await dbRun(
|
||||
"DELETE FROM mock_files WHERE file_path = ? OR file_path LIKE ?",
|
||||
@@ -372,3 +388,88 @@ export async function saveServerConfig(config: AppConfig): Promise<void> {
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
function rowToIpRule(row: IpRuleRow): IpRule {
|
||||
const config: IpRuleConfig = {};
|
||||
if (row.mock_enabled != null) {
|
||||
config.mockEnabled = row.mock_enabled !== 0;
|
||||
}
|
||||
if (row.default_content_type) {
|
||||
config.defaultContentType = row.default_content_type;
|
||||
}
|
||||
if (row.target_host) {
|
||||
config.targetHost = row.target_host;
|
||||
}
|
||||
if (row.target_port != null) {
|
||||
config.targetPort = row.target_port;
|
||||
}
|
||||
if (row.target_https != null) {
|
||||
config.targetHttps = row.target_https !== 0;
|
||||
}
|
||||
return {
|
||||
ip: row.ip,
|
||||
blocked: row.blocked !== 0,
|
||||
remark: row.remark || "",
|
||||
useCustomConfig: row.use_custom_config !== 0,
|
||||
config,
|
||||
};
|
||||
}
|
||||
|
||||
export async function loadIpRulesFromDb(): Promise<IpRule[]> {
|
||||
const rows = await dbAll<IpRuleRow>(
|
||||
"SELECT ip, blocked, remark, use_custom_config, mock_enabled, default_content_type, target_host, target_port, target_https FROM ip_rules ORDER BY ip ASC",
|
||||
);
|
||||
return rows.map(rowToIpRule);
|
||||
}
|
||||
|
||||
export async function upsertIpRuleToDb(rule: IpRule): Promise<void> {
|
||||
const mockEnabled =
|
||||
rule.config.mockEnabled !== undefined
|
||||
? rule.config.mockEnabled
|
||||
? 1
|
||||
: 0
|
||||
: null;
|
||||
const targetHttps =
|
||||
rule.config.targetHttps !== undefined
|
||||
? rule.config.targetHttps
|
||||
? 1
|
||||
: 0
|
||||
: null;
|
||||
await dbRun(
|
||||
`INSERT INTO ip_rules(
|
||||
ip, blocked, remark, use_custom_config,
|
||||
mock_enabled, default_content_type, target_host, target_port, target_https
|
||||
) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(ip) DO UPDATE SET
|
||||
blocked = excluded.blocked,
|
||||
remark = excluded.remark,
|
||||
use_custom_config = excluded.use_custom_config,
|
||||
mock_enabled = excluded.mock_enabled,
|
||||
default_content_type = excluded.default_content_type,
|
||||
target_host = excluded.target_host,
|
||||
target_port = excluded.target_port,
|
||||
target_https = excluded.target_https`,
|
||||
[
|
||||
rule.ip,
|
||||
rule.blocked ? 1 : 0,
|
||||
rule.remark || "",
|
||||
rule.useCustomConfig ? 1 : 0,
|
||||
mockEnabled,
|
||||
rule.config.defaultContentType || null,
|
||||
rule.config.targetHost || null,
|
||||
rule.config.targetPort ?? null,
|
||||
targetHttps,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
export async function deleteIpRuleFromDb(ip: string): Promise<void> {
|
||||
await dbRun("DELETE FROM ip_rules WHERE ip = ?", [ip]);
|
||||
}
|
||||
|
||||
export async function saveAllIpRulesToDb(rules: IpRule[]): Promise<void> {
|
||||
await dbRun("DELETE FROM ip_rules");
|
||||
for (const rule of rules) {
|
||||
await upsertIpRuleToDb(rule);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user