Learning Man's Blog

Nexus 解决显示 “无法访问互联网”

字数统计: 444阅读时长: 2 min
2019/01/24

0x01 Nexus 无法访问网络

原因

原因是Android为了对网络状态进行检测,采用了captive detection进行网络状态检测,即构造一个http请求到指定服务器,获取响应码进行网络判断。

  1. statusCode == 204,代表具备完整的Internet访问
  2. 除204外,当200 <= statusCode < 400时,一般代表需要进行Web登录的网络连接

默认的服务器地址为 http://clients3.google.com/generate_204 ,由于国内 GFW 的存在无法直接获取,可使用 http(s)://captive.v2ex.co/generate_204 代替

解决方案

可以一直开着代理但也不方便,有两种根本上的解决方案

  1. 粗暴关闭网络检测,坏处就是当真的无法访问网络时不会通知

    # 关闭
    > adb shell settings delete global captive_portal_server
    > adb shell settings put global captive_portal_detection_enabled 0
    
    # 恢复
    > adb shell settings delete global captive_portal_server
    > adb shell settings put global captive_portal_detection_enabled 1
  2. 修改检测服务器地址

    1. Version < 7

      # 修改,未开启http://204.sari3l.com,需自建204服务器,可见第二节“自建204服务器”
      > adb shell settings put global captive_portal_server http://204.sari3l.com
      
      # 恢复
      > adb shell settings delete global captive_portal_server
    2. Version >= 7 默认使用https

      # 修改,已开启https://204.sari3l.com,欢迎使用
      > adb shell settings put global captive_portal_detection_enabled 1
      > adb shell settings put global captive_portal_use_https 1
      > adb shell settings put global captive_portal_https_url https://204.sari3l.com
      
      # 恢复
      > adb shell settings delete global captive_portal_http_url

0x02 自建204服务器

配置

  1. 申请证书下载至服务器中

  2. 配置nginx

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name <204_domain_name>;

    ssl_certificate "/xxx.pem";
    ssl_certificate_key "/xxx.key";
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  5m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1.1 TLSv1.2;

    location / {
            return 204;
    }
    error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 =204;
}

效果

> curl -w %{http_code} https://204.sari3l.com
204
CATALOG
  1. 1. 0x01 Nexus 无法访问网络
    1. 1.1. 原因
    2. 1.2. 解决方案
  2. 2. 0x02 自建204服务器
    1. 2.1. 配置
    2. 2.2. 效果