当前位置:   article > 正文

【Android】VpnService连接如何验证账号与密码

【Android】VpnService连接如何验证账号与密码

当前主流的代理IP授权方式有两种,即绑定IP白名单帐密验证

简单说一下Android VpnService连接账密验证

  1. 生成授权字符串:将用户名和密码以"用户名:密码"的形式拼接起来,并进行Base64编码
  2. 在请求头的"Authorization"字段中添加基本认证信息:将授权字符串以"Basic"开头,添加到请求头中。
//拼接
private String makeAuthorization() {
    return Base64.encodeToString((m_Config.UserName + ":" + m_Config.Password).getBytes(), Base64.DEFAULT).trim();
}
//添加请求头
"Proxy-Authorization:Basic +makeAuthorization()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  1. 正常发送请求

Tunnel连接中部分示例:

    @Override
    protected void onConnected(ByteBuffer buffer) throws Exception {
        String request;
        if (TextUtils.isEmpty(m_Config.UserName) || TextUtils.isEmpty(m_Config.Password)) {
            request = String.format(Locale.ENGLISH, "CONNECT %s:%d HTTP/1.0\r\n" +
                            "Proxy-Connection: keep-alive\r\n" +
                            "User-Agent: %s\r\n" +
                            "X-App-Install-ID: %s" +
                            "\r\n\r\n",
                    m_DestAddress.getHostName(),
                    m_DestAddress.getPort(),
                    ProxyConfig.Instance.getUserAgent(),
                    ProxyConfig.AppInstallID);
        } else {
            request = String.format(Locale.ENGLISH, "CONNECT %s:%d HTTP/1.0\r\n" +
                            "Proxy-Authorization: Basic %s\r\n" +
                            "Proxy-Connection: keep-alive\r\n" +
                            "User-Agent: %s\r\n" +
                            "X-App-Install-ID: %s" +
                            "\r\n\r\n",
                    m_DestAddress.getHostName(),
                    m_DestAddress.getPort(),
                    makeAuthorization(),
                    ProxyConfig.Instance.getUserAgent(),
                    ProxyConfig.AppInstallID);
        }

        buffer.clear();
        buffer.put(request.getBytes());
        buffer.flip();
        if (this.write(buffer, true)) {
            this.beginReceive();
        }
    }
  • 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
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号