• Windows网络「SSL错误问题」及解决方案


    文章目录

    问题

    当我们使用了神秘力量加持网络后,可能会和国内的镜像源网站的之间发生冲突,典型的有 Python 从网络中安装包,如执行 pip install pingouin 时,受网络影响导致无法完成安装的情况:

    • pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple,设置了清华源镜像用于全局下载包
    • SSL 错误 (SSLError(SSLEOFError(8, ‘EOF occurred in violation of protocol (_ssl.c:1123)’))),表明在尝试建立一个安全的 HTTPS 连接时遇到了一个问题。通常与 SSL 握手过程中的问题有关,可能是由于客户端和服务器之间的 SSL/TLS 版本或加密套件不兼容

    方案

    将需要访问的网络地址或域名加入到网络代理的白名单中即可解决。可以从神秘力量入手,也可以从 Windows 系统设置入手,神秘力量修改其代理规则即可(有时可能无效),Windows 系统设置手动修改编辑代理服务器即可(重启可能重置)。Windows 系统设置为例以如下图所示:
    请添加图片描述
    由于手动设置比较麻烦,这里提供一个 powershell 脚本「代理管理.ps1」自动处理以上步骤,如下所示:

    # 关键的传入参数
    param (
        [string]$action = "off",
        [string]$additionalBypassItems = "pypi.tuna.tsinghua.edu.cn",
        [switch]$help
    )
    
    # 示例帮助
    function Display-Help {
        Write-Host "Usage of 代理管理.ps1:"
        Write-Host "    To enable proxy: 盘符:\具体路径\代理管理.ps1 -action 'on' -additionalBypassItems 'domain.com;172.100.*'"
        Write-Host "    To disable proxy: 盘符:\具体路径\代理管理.ps1 -action 'off'"
    }
    
    if ($help) {
        Display-Help
        return
    }
    
    # 注册表路径
    $registryPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
    Write-Host "default registryPath: $registryPath"
    
    # 清楚代理
    function Clear-Proxy {
        Set-ItemProperty -Path $registryPath -Name ProxyEnable -Value 0
        Remove-ItemProperty -Path $registryPath -Name ProxyServer -ErrorAction SilentlyContinue
        Remove-ItemProperty -Path $registryPath -Name ProxyOverride -ErrorAction SilentlyContinue
        Write-Host "Proxy disabled. ProxyEnable set 0, ProxyServer and ProxyOverride is remove."
    }
    
    if ($action -eq "off") {
        Clear-Proxy
        return
    } elseif ($action -eq "on") {
    
    } else {
        Write-Host "Invalid action. Use 'on' to enable or 'off' to disable the proxy."
    }
    
    # 代理服务器地址
    $proxyServer = "127.0.0.1:7890"
    Write-Host "default proxyServer: $proxyServer"
    
    # 基本代理排除白名单(本地和内部网络的流量,用分号分隔各个条目)
    $baseProxyBypassList = "localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;192.168.*;127.0.0.1"
    Write-Host "default baseProxyBypassList: $baseProxyBypassList"
    
    # 宽泛的ip正确性校验
    function IsValid-IPAddress($ip) {
        return $ip -match "^\d{1,3}(\.\d{1,3}|\.\*){0,3}(\.\*)?$"
    }
    
    # 宽泛的domain正确性校验
    function IsValid-Domain($domain) {
        return $domain -match "^((?!-)[A-Za-z0-9-]{1,63}(?
    }
    
    # 合并基本白名单和额外的白名单条目
    $proxyBypassList = $baseProxyBypassList
    if ($additionalBypassItems -ne "") {
        $items = $additionalBypassItems.Split(';')
        foreach ($item in $items) {
    	$isIPValid = IsValid-IPAddress $item
        	$isDomainValid = IsValid-Domain $item
            if ($isIPValid -or $isDomainValid) {
                $proxyBypassList += ";" + $item
    	    Write-Host "new additionalBypassItem: $item"
            } else {
                Write-Host "Invalid item: $item"
            }
        }
    }
    
    # 设置代理
    function Set-Proxy {
        Set-ItemProperty -Path $registryPath -Name ProxyEnable -Value 1
        Set-ItemProperty -Path $registryPath -Name ProxyServer -Value $proxyServer
        Set-ItemProperty -Path $registryPath -Name ProxyOverride -Value $proxyBypassList
        Write-Host "Proxy enabled. ProxyEnable set 1, ProxyServer and ProxyOverride is set."
    }
    
    # 代理管理
    if ($action -eq "on") {
        Set-Proxy
        return
    } elseif ($action -eq "off") {
    
    } else {
        Write-Host "Invalid action. Use 'on' to enable or 'off' to disable the proxy."
    }
    
    • 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
  • 相关阅读:
    pdf转图片,pdf转图片在线转
    计算机网络知识点(七)
    c++基础知识点总结
    Makefile 精要(用得最多的规则-附示例)
    什么是缓存雪崩、击穿、穿透?
    面试经典-9-跳跃游戏
    FPGA通信—千兆网(UDP)软件设计
    如何用java校验SQL语句的合法性?(提供五种解决方案)
    布隆过滤器
    100行代码自建Flutter状态管理库
  • 原文地址:https://blog.csdn.net/weixin_49371288/article/details/134489276