QQ浏览器 v12.2.3.7053版本的 MD5 值为:8285ab3059e5c8b521a264dfbc5c3685

以下内容为反编译后的 MttLocalProxy.java 源代码,内容仅作参考


package com.tencent.common.http;

import android.text.TextUtils;
import android.util.Log;
import com.tencent.mtt.base.task.NetworkTask;
import com.tencent.tbs.common.internal.service.StatServerHolder;
import com.xiaomi.mipush.sdk.Constants;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.StringTokenizer;
import kotlin.jvm.internal.ByteCompanionObject;

public class MttLocalProxy extends Thread {

    private static MttLocalProxy f9612a;

    private Thread f9613b;

    private int f9614c;

    private ServerSocket f9615d;
    private boolean e = false;
    private boolean f = false;

    private MttLocalProxy() {
    }

    public int a(MttRequestBase mttRequestBase) {
        String header = mttRequestBase.getHeader("Range");
        if (TextUtils.isEmpty(header)) {
            return 0;
        }
        return Integer.valueOf(header.substring(header.indexOf(NetUtils.RANGE_PARAMS) + 6, header.indexOf(Constants.ACCEPT_TIME_SEPARATOR_SERVER))).intValue();
    }

    public int a(MttResponse mttResponse) {
        String headerField = mttResponse.getHeaderField("Content-Range");
        return headerField != null ? Integer.valueOf(headerField.substring(headerField.indexOf(Constants.ACCEPT_TIME_SEPARATOR_SERVER) + 1, headerField.indexOf("/"))).intValue() + 1 : Integer.valueOf(mttResponse.getHeaderField("Content-Length")).intValue();
    }

    private String a(String[] strArr) {
        try {
            StringTokenizer stringTokenizer = new StringTokenizer(strArr[0]);
            stringTokenizer.nextToken();
            return stringTokenizer.nextToken().substring(1);
        } catch (Exception unused) {
            return null;
        }
    }

    public static MttLocalProxy getInstance() {
        synchronized (MttLocalProxy.class) {
            if (f9612a == null) {
                f9612a = new MttLocalProxy();
            }
        }
        return f9612a;
    }

    public String getProxyURL(String str) {
        return !this.f ? str : (str.startsWith(NetUtils.SCHEME_HTTP) || str.startsWith(NetUtils.SCHEME_HTTPS)) ? String.format("http://127.0.0.1:%d/%s", Integer.valueOf(this.f9614c), str) : str;
    }

    public void init() throws Exception {
        this.f9615d = new ServerSocket(this.f9614c, 1, InetAddress.getByAddress(new byte[]{ByteCompanionObject.MAX_VALUE, 0, 0, 1}));
        this.f9614c = this.f9615d.getLocalPort();
    }

    public void initProxyIfNeeded() {
        if (this.f) {
            return;
        }
        synchronized (MttLocalProxy.class) {
            if (!this.f) {
                try {
                    init();
                    startProxy();
                    this.f = true;
                } catch (Exception e) {
                    HashMap hashMap = new HashMap();
                    hashMap.put("ERROR_POINT", String.valueOf(2));
                    hashMap.put("STACK", Log.getStackTraceString(e));
                    StatServerHolder.statWithBeacon("MTT_LOCAL_PROXY_EXCEPTION", hashMap);
                    try {
                        init();
                        startProxy();
                        this.f = true;
                    } catch (Exception e2) {
                        this.f = false;
                        hashMap.put("ERROR_POINT1", String.valueOf(1));
                        hashMap.put("STACK1", Log.getStackTraceString(e2));
                        StatServerHolder.statWithBeacon("MTT_LOCAL_PROXY_EXCEPTION", hashMap);
                    }
                }
            }
        }
    }

    public boolean isStarted() {
        return this.f;
    }

    @Override
    public void run() {
        try {
            this.e = true;
            while (this.e) {
                try {
                    System.currentTimeMillis();
                    this.f9615d.getReceiveBufferSize();
                    final Socket accept = this.f9615d.accept();
                    if (accept != null) {
                        String[] requestPartsFromSocket = NetUtils.getRequestPartsFromSocket(accept);
                        String a2 = a(requestPartsFromSocket);
                        if (!TextUtils.isEmpty(a2)) {
                            NetworkTask networkTask = new NetworkTask(a2, new NetworkTask.NetworkTaskCallback() {
                                @Override
                                public void onTaskFailed(MttRequestBase mttRequestBase, int i) {
                                }

                                @Override
                                public void onTaskSuccess(MttRequestBase mttRequestBase, MttResponse mttResponse) {
                                    MttInputStream inputStream;
                                    byte[] bArr;
                                    try {
                                        int a3 = MttLocalProxy.this.a(mttRequestBase);
                                        int a4 = MttLocalProxy.this.a(mttResponse);
                                        byte[] bytes = NetUtils.genResponseHeader(a3, a4 - 1, a4).toString().getBytes();
                                        accept.getOutputStream().write(bytes, 0, bytes.length);
                                        inputStream = mttResponse.getInputStream();
                                        bArr = new byte[1024];
                                    } catch (Exception unused) {
                                    } catch (Throwable th) {
                                        try {
                                            accept.close();
                                        } catch (IOException unused2) {
                                        }
                                        throw th;
                                    }
                                    while (true) {
                                        int read = inputStream.read(bArr);
                                        if (read != -1) {
                                            byte[] bArr2 = new byte[read];
                                            System.arraycopy(bArr, 0, bArr2, 0, read);
                                            accept.getOutputStream().write(bArr2);
                                            accept.getOutputStream().flush();
                                        }
                                        try {
                                            break;
                                        } catch (IOException unused3) {
                                            return;
                                        }
                                    }
                                    accept.close();
                                }
                            });
                            for (int i = 1; i < requestPartsFromSocket.length; i++) {
                                int indexOf = requestPartsFromSocket[i].indexOf(Constants.COLON_SEPARATOR);
                                String trim = requestPartsFromSocket[i].substring(0, indexOf).trim();
                                String trim2 = requestPartsFromSocket[i].substring(indexOf + 1).trim();
                                if (!trim.equals("Host")) {
                                    networkTask.a(trim, trim2);
                                }
                            }
                            networkTask.a(true);
                            if (networkTask.getMttRequest().getHeader("Range") == null) {
                                networkTask.a("Range", NetUtils.RANGE_PARAMS_0);
                            }
                            networkTask.d();
                        }
                    }
                } catch (SocketTimeoutException | IOException unused) {
                }
            }
        } catch (Exception e) {
            this.f = false;
            HashMap hashMap = new HashMap();
            hashMap.put("ERROR_POINT", String.valueOf(1));
            hashMap.put("STACK", Log.getStackTraceString(e));
            StatServerHolder.statWithBeacon("MTT_LOCAL_PROXY_EXCEPTION", hashMap);
        }
    }

    public void startProxy() {
        this.f9613b = new Thread(this, "MTT_Local_Proxy");
        this.f9613b.start();
    }

    public void stopProxy() {
        if (this.f9613b == null) {
            throw new IllegalStateException("Cannot stop proxy; it has not been started.");
        }
        this.e = false;
        ServerSocket serverSocket = this.f9615d;
        if (serverSocket != null) {
            try {
                serverSocket.close();
            } catch (IOException unused) {
            }
        }
    }
}