ProxyManager.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package com.taiyi.tyusbsdk.pulse;
  2. import android.text.TextUtils;
  3. import android.webkit.WebResourceRequest;
  4. import android.webkit.WebResourceResponse;
  5. import com.taiyi.tyusbsdk.OssFileUtil;
  6. import com.taiyi.tyusbsdk.pulse.net.HttpUtil;
  7. import java.io.FilterInputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. import io.reactivex.annotations.Nullable;
  14. import okhttp3.OkHttpClient;
  15. import okhttp3.Request;
  16. import okhttp3.Response;
  17. import okhttp3.ResponseBody;
  18. public class ProxyManager {
  19. private static volatile ProxyManager instance;
  20. public static ProxyManager getInstance() {
  21. if (instance == null) {
  22. synchronized (ProxyManager.class) {
  23. if (instance == null) instance = new ProxyManager();
  24. }
  25. }
  26. return instance;
  27. }
  28. private ProxyManager() {
  29. }
  30. public HashMap<String, String> pool = new HashMap<>();
  31. public void init(HashMap<String, String> pool) {
  32. this.pool = pool;
  33. this.client = new OkHttpClient();
  34. Map.Entry<String, String> oss = findEntry(entry -> entry.getKey().startsWith(OssFileUtil.target));
  35. if (oss != null) OssFileUtil.setProxy(oss.getValue());
  36. Map.Entry<String, String> api = findEntry(entry -> entry.getKey().startsWith(HttpUtil.getBaseUrl()));
  37. if (api != null) HttpUtil.setBaseUrl(api.getValue());
  38. }
  39. private OkHttpClient client;
  40. @Nullable
  41. public WebResourceResponse interceptWebViewRequest(WebResourceRequest request) {
  42. String targetUrl = request.getUrl().toString();
  43. Map.Entry<String, String> pool = findEntry(entry -> targetUrl.startsWith(entry.getKey()));
  44. if (pool != null) {
  45. String proxyUrl = targetUrl.replaceFirst(pool.getKey(), pool.getValue());
  46. android.util.Log.i("log:proxy", "[webview] OkHttp URL: " + proxyUrl + " (" + pool.getKey() + ")");
  47. try {
  48. Request.Builder okRequest = new Request.Builder();
  49. okRequest.url(proxyUrl);
  50. okRequest.method(request.getMethod(), null);
  51. Map<String, String> requestHeaders = request.getRequestHeaders();
  52. if (requestHeaders != null) for (Map.Entry<String, String> header : requestHeaders.entrySet()) okRequest.addHeader(header.getKey(), header.getValue());
  53. Response okResponse = client.newCall(okRequest.build()).execute();
  54. String contentType = okResponse.header("Content-Type", "text/html; charset=utf-8");
  55. String mimeType = "text/html";
  56. String encoding = "utf-8";
  57. if (contentType != null) {
  58. String[] parts = contentType.split(";");
  59. if (parts.length > 0) {
  60. mimeType = parts[0].trim();
  61. }
  62. for (String part : parts) {
  63. part = part.trim();
  64. if (part.toLowerCase().startsWith("charset=")) {
  65. encoding = part.substring("charset=".length()).trim();
  66. }
  67. }
  68. }
  69. Map<String, String> responseHeaders = new HashMap<>();
  70. for (Map.Entry<String, List<String>> entry : okResponse.headers().toMultimap().entrySet()) responseHeaders.put(entry.getKey(), TextUtils.join(";", entry.getValue()));
  71. ResponseBody body = okResponse.body();
  72. InputStream stream = null;
  73. if (body != null) stream = new FilterInputStream(body.byteStream()) {
  74. @Override
  75. public void close() throws IOException {
  76. super.close();
  77. okResponse.close();
  78. }
  79. };
  80. return new WebResourceResponse(mimeType, encoding, okResponse.code(), okResponse.message(), responseHeaders, stream);
  81. } catch (Exception ignored) {
  82. }
  83. }
  84. return null;
  85. }
  86. public Map.Entry<String, String> findEntry(Predicate<Map.Entry<String, String>> predicate) {
  87. for (Map.Entry<String, String> entry : pool.entrySet()) {
  88. if (predicate.test(entry)) {
  89. return entry;
  90. }
  91. }
  92. return null;
  93. }
  94. public interface Predicate<T> {
  95. boolean test(T t);
  96. }
  97. }