V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zeep
V2EX  ›  macOS

macOS 有沒有快捷修改 socks 代理的小工具

  •  
  •   zeep · 2023-04-08 13:04:27 +08:00 · 1214 次点击
    这是一个创建于 377 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近頻繁需要修改 Mac 的 socks 代理,每次都點進 Wi-Fi 設置裡很麻煩,有沒有那種方便設置 socks 代理的小工具小軟件?

    7 条回复    2023-06-26 10:56:42 +08:00
    1423
        1
    1423  
       2023-04-08 13:15:29 +08:00
    可以自己写一个,也可以借住 Raycast + 脚本
    mingli
        2
    mingli  
       2023-04-08 13:25:35 +08:00 via iPhone
    networksetup
    cosmain
        3
    cosmain  
       2023-04-08 13:49:12 +08:00
    如果是浏览器,可以用 SwitchyOmega 插件
    如果是命令行,可以用 export ALL_PROXY=socks5://127.0.0.1:1080 这样的方式切换

    如果确实需要系统级的设置,应该有相应系统命令。
    zeep
        4
    zeep  
    OP
       2023-04-08 14:09:55 +08:00
    @cosmain 插件好用謝謝,命令行完全不懂 hh
    josephxrays
        5
    josephxrays  
       2023-04-08 15:42:33 +08:00
    把下面的 shell 脚本保存到一个文件:set_proxy.sh

    ```shell

    #!/bin/bash

    function scutil_query {
    key=$1
    scutil <<EOT
    open
    get $key
    d.show
    close
    EOT
    }

    SERVICE_GUID=$(scutil_query State:/Network/Global/IPv4 | grep "PrimaryService" | awk '{print $3}')
    currentservice=$(scutil_query Setup:/Network/Service/$SERVICE_GUID | grep "UserDefinedName" | awk -F': ' '{print $2}')

    set_proxy() {
    http=$1
    socks=$2
    if [[ -z $http ]] && [[ -z $socks ]]; then
    return 0
    fi

    # http 代理
    if [[ ! -z $http ]]; then
    networksetup -setwebproxystate "$currentservice" on
    networksetup -setsecurewebproxystate "$currentservice" on
    hostname=$(echo "$http" | awk -F ':' '{print $1}')
    port=$(echo "$http" | awk -F ':' '{print $2}')
    networksetup -setwebproxy "$currentservice" $hostname $port
    networksetup -setsecurewebproxy "$currentservice" $hostname $port
    fi

    # socks 代理
    if [[ ! -z $socks ]]; then
    networksetup -setsocksfirewallproxystate "$currentservice" on
    hostname=$(echo "$socks" | awk -F ':' '{print $1}')
    port=$(echo "$socks" | awk -F ':' '{print $2}')
    networksetup -setsocksfirewallproxy "$currentservice" $hostname $port
    networksetup -setsocksfirewallproxy "$currentservice" $hostname $port
    fi

    # 设置代理绕过的域名
    networksetup -setproxybypassdomains "$currentservice" 192.168.0.0/16 10.0.0.0/8 172.16.0.0/12 127.0.0.1 localhost "*.local" timestamp.apple.com
    }

    # 取消代理设置
    unset_proxy() {
    networksetup -setwebproxy "$currentservice" "" "" off "" ""
    networksetup -setsecurewebproxy "$currentservice" "" "" off "" ""
    networksetup -setsocksfirewallproxy "$currentservice" "" "" off "" ""

    networksetup -setwebproxystate "$currentservice" off
    networksetup -setsecurewebproxystate "$currentservice" off
    networksetup -setsocksfirewallproxystate "$currentservice" off

    networksetup -setproxybypassdomains "$currentservice" Empty
    }

    if [[ $1 == "http" ]]; then
    set_proxy $2 ""
    elif [[ $1 == "socks" ]]; then
    set_proxy "" $2
    else
    unset_proxy
    fi

    ```

    使用:
    ```shell
    bash set_proxy.sh http 127.0.0.1:10086 # 设置 http 和 https 代理
    bash set_proxy.sh socks 127.0.0.1:10086 # 设置 socks5 代理
    bash set_proxy.sh # 恢复
    ```
    BlackAdlerChi
        6
    BlackAdlerChi  
       2023-04-10 10:24:18 +08:00
    Chromium 的浏览器+SwitchyOmega 扩展是最好的解决方案
    lossfunc
        7
    lossfunc  
       298 天前
    @josephxrays 很好用,谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3944 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 05:10 · PVG 13:10 · LAX 22:10 · JFK 01:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.