Skip to content

Commit f164198

Browse files
committed
feat(clash): add network proxy setup script
1 parent f0f65ea commit f164198

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

scripts/clash/networksetup.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import subprocess
2+
import argparse
3+
4+
def get_current_wifi_interface():
5+
try:
6+
result = subprocess.run(
7+
["networksetup", "-listallnetworkservices"],
8+
capture_output=True,
9+
text=True,
10+
check=True
11+
)
12+
services = result.stdout.splitlines()
13+
for service in services:
14+
if "Wi-Fi" in service:
15+
return service.strip()
16+
return None
17+
except subprocess.CalledProcessError as e:
18+
print(f"Error getting Wi-Fi interface: {e}")
19+
return None
20+
21+
def set_proxy(interface, proxy_host, proxy_port):
22+
try:
23+
subprocess.run(
24+
[
25+
"networksetup",
26+
"-setwebproxy",
27+
interface,
28+
proxy_host,
29+
str(proxy_port)
30+
],
31+
check=True
32+
)
33+
subprocess.run(
34+
[
35+
"networksetup",
36+
"-setsecurewebproxy",
37+
interface,
38+
proxy_host,
39+
str(proxy_port)
40+
],
41+
check=True
42+
)
43+
print(f"Successfully set HTTP and HTTPS proxies to {proxy_host}:{proxy_port} for {interface}")
44+
except subprocess.CalledProcessError as e:
45+
print(f"Error setting proxy: {e}")
46+
47+
def unset_proxy(interface):
48+
try:
49+
subprocess.run(
50+
["networksetup", "-setwebproxystate", interface, "off"],
51+
check=True
52+
)
53+
subprocess.run(
54+
["networksetup", "-setsecurewebproxystate", interface, "off"],
55+
check=True
56+
)
57+
print(f"Successfully unset HTTP and HTTPS proxies for {interface}")
58+
except subprocess.CalledProcessError as e:
59+
print(f"Error unsetting proxy: {e}")
60+
61+
def main():
62+
parser = argparse.ArgumentParser(description="Manage HTTP and HTTPS proxy settings for Wi-Fi on macOS")
63+
parser.add_argument(
64+
"action",
65+
choices=["set", "unset"],
66+
help="Action to perform: set or unset proxy"
67+
)
68+
args = parser.parse_args()
69+
70+
proxy_host = "127.0.0.1"
71+
proxy_port = 7890
72+
73+
wifi_interface = get_current_wifi_interface()
74+
75+
if not wifi_interface:
76+
print("No Wi-Fi interface found. Please ensure Wi-Fi is connected.")
77+
return
78+
79+
print(f"Found Wi-Fi interface: {wifi_interface}")
80+
81+
if args.action == "set":
82+
set_proxy(wifi_interface, proxy_host, proxy_port)
83+
else:
84+
unset_proxy(wifi_interface)
85+
86+
if __name__ == "__main__":
87+
main()

0 commit comments

Comments
 (0)