-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathprofile.ts
More file actions
156 lines (135 loc) · 4.03 KB
/
profile.ts
File metadata and controls
156 lines (135 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { Host, PacScript, SimpleProxyServer } from "@/adapters";
import { deepClone } from "./utils";
export type ProxyAuthInfo = {
username: string;
password: string;
};
export type ProxyServer = SimpleProxyServer & {
auth?: ProxyAuthInfo;
};
export function sanitizeProxyServer(v: ProxyServer): SimpleProxyServer {
return {
host: v.host,
port: v.port,
};
}
export type ProxyConfigMeta = {
profileID: string;
color: string;
profileName: string;
proxyType: "proxy" | "pac" | "system" | "direct" | "auto";
};
// the basic proxy config, with authentication and pac script support
export type ProxyConfigSimple =
| {
proxyType: "proxy";
proxyRules: {
default: ProxyServer;
http?: ProxyServer;
https?: ProxyServer;
ftp?: ProxyServer;
bypassList: string[];
};
pacScript?: PacScript;
}
| {
proxyType: "pac";
proxyRules?: {
default: ProxyServer;
http?: ProxyServer;
https?: ProxyServer;
ftp?: ProxyServer;
bypassList: string[];
};
pacScript: PacScript;
};
// advanced proxy config, with auto switch support
export type AutoSwitchType = "domain" | "cidr" | "url" | "disabled";
export type AutoSwitchRule = {
type: AutoSwitchType;
condition: string;
profileID: string;
};
export type ProxyConfigAutoSwitch = {
rules: AutoSwitchRule[];
defaultProfileID: string;
};
export type ProfileSimple = ProxyConfigMeta & ProxyConfigSimple;
export type ProfilePreset = ProxyConfigMeta & {
proxyType: "system" | "direct";
};
export type ProfileAutoSwitch = ProxyConfigMeta & {
proxyType: "auto";
} & ProxyConfigAutoSwitch;
export type ProxyProfile = ProfileSimple | ProfilePreset | ProfileAutoSwitch;
export const SystemProfile: Record<string, ProxyProfile> = {
DIRECT: {
profileID: "direct",
color: "#7ad39e",
profileName: "Direct",
proxyType: "direct",
},
SYSTEM: {
profileID: "system",
color: "#0000",
profileName: "", // should be empty
proxyType: "system",
},
};
const keyProfileStorage = "profiles";
export type ProfilesStorage = Record<string, ProxyProfile>;
const onProfileUpdateListeners: ((p: ProfilesStorage) => void)[] = [];
// list all user defined profiles. System profiles are not included
export async function listProfiles(): Promise<ProfilesStorage> {
const s = await Host.get<ProfilesStorage>(keyProfileStorage);
return s || {};
}
export function onProfileUpdate(callback: (p: ProfilesStorage) => void) {
onProfileUpdateListeners.push(callback);
}
async function overwriteProfiles(profiles: ProfilesStorage) {
// Deep clone to remove any Proxy objects before saving
const clonedProfiles = deepClone(profiles);
await Host.set(keyProfileStorage, clonedProfiles);
onProfileUpdateListeners.forEach((cb) => cb(profiles));
}
/**
* Save a single profile to the storage.
* Please be noticed that this is not promise-safe. If you want to save multiple profiles, use `saveManyProfiles` instead.
*
* @param profile
*/
export async function saveProfile(profile: ProxyProfile) {
const data = await listProfiles();
// Deep clone the profile to remove any Proxy objects before saving
data[profile.profileID] = deepClone(profile);
await overwriteProfiles(data);
}
export async function saveManyProfiles(profiles: ProxyProfile[]) {
let data = await listProfiles();
profiles.forEach((p) => {
// Deep clone each profile to remove any Proxy objects before saving
data[p.profileID] = deepClone(p);
});
await overwriteProfiles(data);
}
export async function getProfile(
profileID: string,
userProfileOnly?: boolean
): Promise<ProxyProfile | undefined> {
if (!userProfileOnly) {
// check if it's a system profile
for (const p of Object.values(SystemProfile)) {
if (p.profileID === profileID) {
return p;
}
}
}
const data = await listProfiles();
return data[profileID];
}
export async function deleteProfile(profileID: string) {
const data = await listProfiles();
delete data[profileID];
await overwriteProfiles(data);
}