-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.ts
More file actions
107 lines (89 loc) · 2.81 KB
/
index.ts
File metadata and controls
107 lines (89 loc) · 2.81 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
import { Host } from "@/adapters";
import {
ProxyProfile,
ProxyAuthInfo,
SystemProfile,
getProfile,
ProfileAutoSwitch,
} from "../profile";
import { ProxySettingResultDetails } from "@/adapters";
import { ProfileConverter } from "./profile2config";
import { ProfileAuthProvider } from "./auth";
import { deepClone } from "../utils";
export type ProxySetting = {
activeProfile?: ProxyProfile;
setting: ProxySettingResultDetails;
};
const keyActiveProfile = "active-profile";
async function wrapProxySetting(setting: ProxySettingResultDetails) {
const ret: ProxySetting = {
setting,
};
if (setting.levelOfControl == "controlled_by_this_extension") {
ret.activeProfile =
(await Host.get<ProxyProfile>(keyActiveProfile)) || undefined;
}
switch (setting.value?.mode) {
case "system":
ret.activeProfile = SystemProfile.SYSTEM;
break;
case "direct":
ret.activeProfile = SystemProfile.DIRECT;
break;
}
return ret;
}
export async function getCurrentProxySetting() {
const setting = await Host.getProxySettings();
return await wrapProxySetting(setting);
}
export async function setProxy(val: ProxyProfile) {
switch (val.proxyType) {
case "system":
await Host.clearProxy();
break;
default:
const profile = new ProfileConverter(val, getProfile);
await Host.setProxy(await profile.toProxyConfig());
break;
}
// Deep clone to remove any Proxy objects before saving
await Host.set<ProxyProfile>(keyActiveProfile, deepClone(val));
}
/**
* Refresh the current proxy setting. This is useful when the proxy setting is changed by user.
* @returns
*/
export async function refreshProxy() {
const current = await getCurrentProxySetting();
// if it's not controlled by this extension, then do nothing
if (!current.activeProfile) {
return;
}
const newProfile = await getProfile(current.activeProfile.profileID);
// if it's preset profiles, then do nothing
if (!newProfile || ["system", "direct"].includes(current.activeProfile.proxyType)) {
return;
}
const profile = new ProfileConverter(newProfile, getProfile);
await Host.setProxy(await profile.toProxyConfig());
}
export async function previewAutoSwitchPac(val: ProfileAutoSwitch) {
const profile = new ProfileConverter(val, getProfile);
return await profile.toPAC();
}
export async function getAuthInfos(
host: string,
port: number
): Promise<ProxyAuthInfo[]> {
const profile = await Host.get<ProxyProfile>(keyActiveProfile);
if (!profile) {
return [];
}
const authProvider = new ProfileAuthProvider(profile, getProfile);
return await authProvider.getAuthInfos(host, port);
}
export async function findProfile(profile: ProxyProfile, url: URL) {
const converter = new ProfileConverter(profile, getProfile);
return await converter.findProfile(url);
}