123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package com.taiyi.tyusbsdk.pulse;
- import android.text.TextUtils;
- import android.webkit.WebResourceRequest;
- import android.webkit.WebResourceResponse;
- import com.taiyi.tyusbsdk.OssFileUtil;
- import com.taiyi.tyusbsdk.pulse.net.HttpUtil;
- import java.io.FilterInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import io.reactivex.annotations.Nullable;
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.Response;
- import okhttp3.ResponseBody;
- public class ProxyManager {
- private static volatile ProxyManager instance;
- public static ProxyManager getInstance() {
- if (instance == null) {
- synchronized (ProxyManager.class) {
- if (instance == null) instance = new ProxyManager();
- }
- }
- return instance;
- }
- private ProxyManager() {
- }
- public HashMap<String, String> pool = new HashMap<>();
- public void init(HashMap<String, String> pool) {
- this.pool = pool;
- this.client = new OkHttpClient();
- Map.Entry<String, String> oss = findEntry(entry -> entry.getKey().startsWith(OssFileUtil.target));
- if (oss != null) OssFileUtil.setProxy(oss.getValue());
- Map.Entry<String, String> api = findEntry(entry -> entry.getKey().startsWith(HttpUtil.getBaseUrl()));
- if (api != null) HttpUtil.setBaseUrl(api.getValue());
- }
- private OkHttpClient client;
- @Nullable
- public WebResourceResponse interceptWebViewRequest(WebResourceRequest request) {
- String targetUrl = request.getUrl().toString();
- Map.Entry<String, String> pool = findEntry(entry -> targetUrl.startsWith(entry.getKey()));
- if (pool != null) {
- String proxyUrl = targetUrl.replaceFirst(pool.getKey(), pool.getValue());
- android.util.Log.i("log:proxy", "[webview] OkHttp URL: " + proxyUrl + " (" + pool.getKey() + ")");
- try {
- Request.Builder okRequest = new Request.Builder();
- okRequest.url(proxyUrl);
- okRequest.method(request.getMethod(), null);
- Map<String, String> requestHeaders = request.getRequestHeaders();
- if (requestHeaders != null) for (Map.Entry<String, String> header : requestHeaders.entrySet()) okRequest.addHeader(header.getKey(), header.getValue());
- Response okResponse = client.newCall(okRequest.build()).execute();
- String contentType = okResponse.header("Content-Type", "text/html; charset=utf-8");
- String mimeType = "text/html";
- String encoding = "utf-8";
- if (contentType != null) {
- String[] parts = contentType.split(";");
- if (parts.length > 0) {
- mimeType = parts[0].trim();
- }
- for (String part : parts) {
- part = part.trim();
- if (part.toLowerCase().startsWith("charset=")) {
- encoding = part.substring("charset=".length()).trim();
- }
- }
- }
- Map<String, String> responseHeaders = new HashMap<>();
- for (Map.Entry<String, List<String>> entry : okResponse.headers().toMultimap().entrySet()) responseHeaders.put(entry.getKey(), TextUtils.join(";", entry.getValue()));
- ResponseBody body = okResponse.body();
- InputStream stream = null;
- if (body != null) stream = new FilterInputStream(body.byteStream()) {
- @Override
- public void close() throws IOException {
- super.close();
- okResponse.close();
- }
- };
- return new WebResourceResponse(mimeType, encoding, okResponse.code(), okResponse.message(), responseHeaders, stream);
- } catch (Exception ignored) {
- }
- }
- return null;
- }
- public Map.Entry<String, String> findEntry(Predicate<Map.Entry<String, String>> predicate) {
- for (Map.Entry<String, String> entry : pool.entrySet()) {
- if (predicate.test(entry)) {
- return entry;
- }
- }
- return null;
- }
- public interface Predicate<T> {
- boolean test(T t);
- }
- }
|