如何在 android 中将 Netcipher 与 Retrofit 结合使用?

How to use Netcipher with Retrofit in android?

Netcipher 是一个 Android 库项目,它提供了多种方法来提高移动应用程序中的网络安全性。 “洋葱”这个名字不仅指的是 Tor 使用的洋葱路由概念(它提供匿名性和对流量监视的抵抗力),而且指的是任何应用程序都应该利用的多层安全的想法。

更具体地说,该库提供:

1. Stronger Sockets: Through support for the right cipher suites, pinning and more, we ensure your encrypted connections are as strong as possible.
2. Proxied Connection Support: HTTP and SOCKS proxy connection support for HTTP and HTTP/S traffic through specific configuration of the Apache HTTPClient library

https://guardianproject.info/code/netcipher/

您需要实现自己的 Client,它将在 Netcipher http 客户端上执行 Retrofit 请求。

  1. Request 翻译成适当的 Netcipher 请求(复制 http 方法,headers,body)
  2. 在 Netcipher http 客户端上执行转换后的请求
  3. 获取响应并将其翻译成改造 Response(复制 http 状态代码,响应,headers)
  4. Return 要反序列化为类型的响应。

将您的 Client 传递给 RestAdapter.Builder

完成。

public class NetcipherClient implements Client{
  private Context mContext;
  public NetcipherClient(Context context){
      mContext = context;
      //As far as I could see from the sample, Netcipher seems to be dependant on application `Context`.

  }
  @Override
    public retrofit.client.Response execute(retrofit.client.Request request) throws IOException {
        //Set up configuration for Netcipher (proxy, timeout etc)
        // Translate Request to Netcipher request 
        // Execute and obtain the response
        // Build Response from response
        return response;
    }


}

除了类似 Tor 的路由 OkHttp 还具有证书固定功能和代理支持。它与开箱即用的 Retrofit 一起使用!因此,如果类似 Tor 的功能对您来说不是那么重要,我建议您利用 OkHttp 的强大功能。否则,@NikolaDespotoski 的回答是您的最佳选择。

我们最近在 NetCipher 中实现了对 OkHTTP 的支持,因此通过 OkHTTP 将 Tor 支持添加到 Retrofit 应该很容易。方法如下:

compile 'info.guardianproject.netcipher:netcipher-okhttp3:2.0.0-alpha1'

核心就是运行设置方法:

  StrongOkHttpClientBuilder
    .forMaxSecurity(this)
    .withTorValidation()
    .build(this);

查看随附的内容sample-okhttp3 project for a full example, which is part of the git repo https://github.com/guardianproject/NetCipher