性メ囘め意 v4.1.7版本的 MD5 值为:454244359250cd2ed989b9b0210c6279

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


package com.wind.imlib.bean;

import android.text.TextUtils;
import android.util.Log;
import com.cf.msc.sdk.AppVest;
import com.cf.msc.sdk.SecurityConnection;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.just.agentweb.DefaultWebClient;
import com.wind.imlib.WindClient;
import com.wind.imlib.api.response.ApiResponse;
import com.wind.imlib.api.response.ApiServerConfigResponse;
import com.wind.imlib.connect.TokenManager;
import com.wind.imlib.connect.http.HttpConnection;
import com.wind.imlib.connect.utils.SuService;
import com.xiaomi.mipush.sdk.Constants;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.lang3.StringUtils;
import timber.log.Timber;

public class ServerBucket {
    private List<String> allAppServers;
    private List<String> allCdnNodes;
    private List<String> allFileServers;
    private Set<String> availableAppServers;
    private Set<String> availableCdnNodes;
    private Set<String> availableFileServers;
    private String hideServer;
    private List<String> ignoreIpServers;
    private String realIpAddress;
    private Integer realIpPort;
    private String schema;
    private String selectedAppServer;
    private String selectedCdnNode;
    private String selectedFileServer;
    private String serverConfig;
    private WindClient.ConnectMethod connectMethod = WindClient.ConnectMethod.HTTP;
    private String ignoreIpStr = "";
    private ConcurrentHashMap<String, Integer> addressNodeMap = new ConcurrentHashMap<>();
    private ScheduledExecutorService updateExecutorService = Executors.newSingleThreadScheduledExecutor();
    private AtomicBoolean updating = new AtomicBoolean(false);
    private AtomicBoolean timeOutUpdating = new AtomicBoolean(false);
    private AtomicBoolean hideUpdating = new AtomicBoolean(false);

    public void initAgentServer() {
        String sortHost = getSortHost(this.selectedAppServer);
        int intValue = getSortPort(this.selectedAppServer).intValue();
        if (!TextUtils.isEmpty("d54bd5ac75afabf91078e5295c9df199") && !TextUtils.isEmpty("Y9Dzb98W/UYg6EaKPpzCr7kT/TK6/s7mXccUJ4i7nd92LoGoR6tdWw0tiLjwEy1xYwkjyutizgo8l2Y0W+/DW55/FmRqHEn+eDQPYu4nUoWZPDkCXB4uSFYHqhlEoO0/KNu5a/5DdI7HU5cv5bDc4BtyV0EYoi7UMRgFX63pTvCsfZnetxP7ho6c")) {
            SecurityConnection serverIPAndPort = AppVest.getServerIPAndPort(sortHost, intValue);
            if (serverIPAndPort.getServerPort().intValue() == -1) {
                return;
            }
            String clientIP = AppVest.getClientIP();
            Timber.i("MainActivityclient_ip " + clientIP, new Object[0]);
            if (AppVest.setTimeouts(120, 5) == -1) {
                Timber.i("MainActivityappvest setTimeouts failed", new Object[0]);
                return;
            }
            Timber.i("MainActivityclient_ip=" + clientIP, new Object[0]);
            new AgentServer();
            this.realIpAddress = serverIPAndPort.getServerIp();
            this.realIpPort = serverIPAndPort.getServerPort();
            return;
        }
        this.realIpAddress = sortHost;
        this.realIpPort = Integer.valueOf(intValue);
    }

    public String getRealIpAddress() {
        return this.realIpAddress;
    }

    public Integer getRealIpPort() {
        return this.realIpPort;
    }

    public void init(WindClient.ConnectMethod connectMethod, String appServers, String fileServers, String cdnNodes, String ignoreIpServer, boolean useSSL, final String hideServer) {
        this.connectMethod = connectMethod;
        this.hideServer = hideServer;
        if (connectMethod == WindClient.ConnectMethod.WS) {
            this.schema = useSSL ? "wss://" : "ws://";
        } else {
            this.schema = useSSL ? DefaultWebClient.HTTPS_SCHEME : DefaultWebClient.HTTP_SCHEME;
        }
        this.ignoreIpServers = new ArrayList();
        for (String str : ignoreIpServer.split(Constants.ACCEPT_TIME_SEPARATOR_SP)) {
            String trim = StringUtils.trim(str);
            if (!StringUtils.isBlank(trim)) {
                this.ignoreIpServers.add(trim);
            }
        }
        updateServers(appServers, fileServers, cdnNodes);
        initAgentServer();
        this.updateExecutorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                ServerBucket.this.initAgentServer();
            }
        }, 3L, 180L, TimeUnit.SECONDS);
        this.updateExecutorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                ServerBucket.this.checkServerTimeOut();
            }
        }, 3L, 10L, TimeUnit.SECONDS);
        this.updateExecutorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                if (!StringUtils.isNotBlank(hideServer) || hideServer.indexOf("users.qzone.qq.com") == -1) {
                    return;
                }
                ServerBucket.this.updateServerFromHideServer();
            }
        }, 3L, 180L, TimeUnit.SECONDS);
    }

    public WindClient.ConnectMethod getConnectMethod() {
        return this.connectMethod;
    }

    public String getSelectedAppServer() {
        return this.selectedAppServer;
    }

    public String getSelectedFileServer() {
        return this.selectedFileServer;
    }

    public String getSelectedCdnNode() {
        return this.selectedCdnNode;
    }

    public Set<String> getAvailableAppServers() {
        return this.availableAppServers;
    }

    public Set<String> getAvailableFileServers() {
        return this.availableFileServers;
    }

    public Set<String> getAvailableCdnNodes() {
        return this.availableCdnNodes;
    }

    public List<String> getAllAppServers() {
        return this.allAppServers;
    }

    public List<String> getAllFileServers() {
        return this.allFileServers;
    }

    public List<String> getAllCdnNodes() {
        return this.allCdnNodes;
    }

    public List<String> getIgnoreIpServers() {
        return this.ignoreIpServers;
    }

    public String getServerConfig() {
        return this.serverConfig;
    }

    public String getHideServer() {
        return this.hideServer;
    }

    public ConcurrentHashMap<String, Integer> getAddressNodeMap() {
        return this.addressNodeMap;
    }

    public String getAppServer() {
        String str = this.selectedAppServer;
        if (str.indexOf(this.schema) == -1) {
            str = this.schema + str;
        }
        this.selectedAppServer = str;
        return str;
    }

    public String getFastAppServers() {
        if (CollectionUtils.isEmpty(this.availableAppServers)) {
            return getRandomServer(this.allAppServers);
        }
        return getRandomServer(this.availableAppServers);
    }

    public void setFastAppServers() {
        this.selectedAppServer = getRandomServer(this.availableAppServers);
        this.selectedFileServer = getRandomServer(this.availableFileServers);
        this.selectedCdnNode = getRandomServer(this.availableCdnNodes);
        TokenManager.setSelectServer(this.selectedAppServer);
    }

    public void setSelectAppServers(String ipServer) {
        this.selectedAppServer = ipServer;
        this.selectedFileServer = ipServer;
        this.selectedCdnNode = getRandomServer(this.availableCdnNodes);
        TokenManager.setSelectServer(ipServer);
    }

    public String getFileServer() {
        return String.format("http://%s:%s", getRealIpAddress(), getRealIpPort());
    }

    public String getCdnNodeOrFileServer() {
        if (CollectionUtils.isEmpty(this.availableCdnNodes)) {
            return getFileServer();
        }
        if (CollectionUtils.containsAny(this.availableCdnNodes, this.selectedCdnNode)) {
            return this.selectedCdnNode;
        }
        String randomServer = getRandomServer(this.availableCdnNodes);
        this.selectedCdnNode = randomServer;
        if (StringUtils.isNotBlank(randomServer)) {
            return this.selectedCdnNode;
        }
        return getFileServer();
    }

    public void updateServers(String appServers, String fileServers, String cdnNodes) {
        try {
            List<String> parseServers = parseServers(appServers);
            if (CollectionUtils.isNotEmpty(parseServers)) {
                if (this.allAppServers == null) {
                    this.allAppServers = new ArrayList();
                }
                for (String str : parseServers) {
                    if (!this.allAppServers.contains(str)) {
                        this.allAppServers.add(str);
                    }
                }
                this.availableAppServers = new CopyOnWriteArraySet(this.allAppServers);
                if (StringUtils.isEmpty(this.selectedAppServer) || !this.availableAppServers.contains(this.selectedAppServer)) {
                    String randomServer = getRandomServer(this.availableAppServers);
                    this.selectedAppServer = randomServer;
                    TokenManager.setSelectServer(randomServer);
                }
            }
            List<String> parseServers2 = parseServers(fileServers);
            if (CollectionUtils.isNotEmpty(parseServers2)) {
                if (this.allFileServers == null) {
                    this.allFileServers = new ArrayList();
                }
                for (String str2 : parseServers2) {
                    if (!this.allFileServers.contains(str2)) {
                        this.allFileServers.add(str2);
                    }
                }
                this.availableFileServers = new CopyOnWriteArraySet(this.allFileServers);
                if (StringUtils.isEmpty(this.selectedFileServer) || !this.availableFileServers.contains(this.selectedFileServer)) {
                    this.selectedFileServer = getRandomServer(this.availableFileServers);
                }
            }
            List<String> parseServers3 = parseServers(cdnNodes);
            this.allCdnNodes = parseServers3;
            if (parseServers3 == null) {
                this.allCdnNodes = new ArrayList();
            }
            CopyOnWriteArraySet copyOnWriteArraySet = new CopyOnWriteArraySet(this.allCdnNodes);
            this.availableCdnNodes = copyOnWriteArraySet;
            if (CollectionUtils.isEmpty(copyOnWriteArraySet)) {
                this.selectedCdnNode = null;
            } else if (StringUtils.isEmpty(this.selectedCdnNode) || !this.availableCdnNodes.contains(this.selectedCdnNode)) {
                this.selectedCdnNode = getRandomServer(this.availableCdnNodes);
            }
        } catch (Throwable th) {
            Log.w("ServerBucket", "Failed to parse server configuration.", th);
        }
    }

    public void blackAppServer(String blackServerUrl) {
        if (CollectionUtils.isEmpty(this.availableAppServers)) {
            return;
        }
        this.availableAppServers.remove(blackServerUrl);
        if (StringUtils.equals(this.selectedAppServer, blackServerUrl)) {
            String randomServer = getRandomServer(this.availableAppServers);
            this.selectedAppServer = randomServer;
            TokenManager.setSelectServer(randomServer);
        }
    }

    public void blackFileServer(String blackServerUrl) {
        if (CollectionUtils.isEmpty(this.availableFileServers)) {
            return;
        }
        this.availableFileServers.remove(blackServerUrl);
        if (StringUtils.equals(this.selectedFileServer, blackServerUrl)) {
            this.selectedFileServer = getRandomServer(this.availableFileServers);
        }
    }

    private String getRandomServer(Collection<String> servers) {
        int intValue;
        String str = null;
        if (servers == null || servers.isEmpty()) {
            return null;
        }
        String selectServer = TokenManager.getSelectServer();
        if (!TextUtils.isEmpty(selectServer)) {
            return selectServer;
        }
        String str2 = this.selectedAppServer;
        if (str2 != null) {
            return str2;
        }
        Iterator<String> it2 = servers.iterator();
        if (servers.size() == 1) {
            return it2.next();
        }
        int i = 999;
        while (it2.hasNext()) {
            String next = it2.next();
            String sortHost = getSortHost(next);
            if (this.addressNodeMap.containsKey(sortHost) && (intValue = this.addressNodeMap.get(sortHost).intValue()) != -1 && intValue < i) {
                str = next;
                i = intValue;
            }
        }
        return str == null ? (String) IterableUtils.get(servers, new Random().nextInt(servers.size())) : str;
    }

    private List<String> parseServers(String serverConfig) {
        if (StringUtils.isBlank(serverConfig)) {
            return null;
        }
        ArrayList arrayList = new ArrayList();
        for (String str : serverConfig.split(Constants.ACCEPT_TIME_SEPARATOR_SP)) {
            String trim = StringUtils.trim(str);
            if (!StringUtils.isBlank(trim)) {
                if (StringUtils.startsWith(trim, "http")) {
                    arrayList.add(trim);
                } else {
                    arrayList.add(this.schema + trim);
                }
                TokenManager.addUserIpNode(trim);
                putInAddressNodeMap(trim);
            }
        }
        return arrayList;
    }

    private void putInAddressNodeMap(String server) {
        String sortHost = getSortHost(server);
        if (this.addressNodeMap.containsKey(sortHost)) {
            return;
        }
        this.addressNodeMap.put(sortHost, 999);
    }

    public static Integer getSortPort(String server) {
        return Integer.valueOf(StringUtils.substring(server, server.lastIndexOf(Constants.COLON_SEPARATOR) + 1, server.length()));
    }

    public static String getSortHost(String server) {
        if (StringUtils.contains(server, "://")) {
            return StringUtils.substringBetween(server, "://", Constants.COLON_SEPARATOR);
        }
        return StringUtils.substringBefore(server, Constants.COLON_SEPARATOR);
    }

    public void checkServerTimeOut() {
        if (this.addressNodeMap.isEmpty()) {
            return;
        }
        try {
            if (this.timeOutUpdating.compareAndSet(false, true)) {
                for (String str : this.addressNodeMap.keySet()) {
                    int ping = SuService.ping(str, 2, this.ignoreIpServers);
                    this.addressNodeMap.put(str, Integer.valueOf(ping));
                    Log.i("server time out", str + " wast : " + ping);
                }
            }
        } catch (Exception unused) {
        } catch (Throwable th) {
            this.timeOutUpdating.set(false);
            throw th;
        }
        this.timeOutUpdating.set(false);
    }

    public void updateServerFromHideServer() {
        try {
            if (this.hideUpdating.compareAndSet(false, true)) {
                OkHttpClient build = new OkHttpClient.Builder().protocols(Collections.singletonList(Protocol.HTTP_1_1)).sslSocketFactory(HttpConnection.getSslSocketFactory()).hostnameVerifier(HttpConnection.DO_NOT_VERIFY).connectTimeout(3000L, TimeUnit.MILLISECONDS).readTimeout(3000L, TimeUnit.MILLISECONDS).writeTimeout(3000L, TimeUnit.MILLISECONDS).build();
                try {
                    String str = this.hideServer;
                    Response execute = build.newCall(new Request.Builder().url(str).build()).execute();
                    if (execute.isSuccessful()) {
                        if (str.indexOf("users.qzone.qq.com") != -1) {
                            String string = execute.body().string();
                            JsonObject jsonObject = (JsonObject) new Gson().fromJson(string.substring(string.indexOf(40) + 1, string.length() - 1), JsonObject.class);
                            String asString = jsonObject.getAsJsonArray(jsonObject.keySet().iterator().next()).get(r1.size() - 2).getAsString();
                            if (asString.indexOf("http") == -1) {
                                asString = DefaultWebClient.HTTP_SCHEME + asString;
                            }
                            updateServers(asString, asString, null);
                        } else {
                            String string2 = execute.body().string();
                            this.serverConfig = string2;
                            if (StringUtils.isNotBlank(string2)) {
                                ApiServerConfigResponse apiServerConfigResponse = (ApiServerConfigResponse) ((ApiResponse) new Gson().fromJson(this.serverConfig, new TypeToken<ApiResponse<ApiServerConfigResponse>>() {
                                }.getType())).getData();
                                TokenManager.setHideServer(new Gson().toJson(apiServerConfigResponse));
                                updateServers(apiServerConfigResponse.getAppServers(), apiServerConfigResponse.getFileServers(), apiServerConfigResponse.getCdnNodes());
                            }
                        }
                    }
                } catch (Throwable th) {
                    Log.i("hide server", "load hide server error:" + th.getMessage());
                }
            }
        } finally {
            try {
            } finally {
            }
        }
    }

    private void updateServerConfig() {
        Response execute;
        if (CollectionUtils.isEmpty(this.allAppServers)) {
            return;
        }
        try {
            if (this.updating.compareAndSet(false, true)) {
                OkHttpClient build = new OkHttpClient.Builder().protocols(Collections.singletonList(Protocol.HTTP_1_1)).connectTimeout(3000L, TimeUnit.MILLISECONDS).readTimeout(3000L, TimeUnit.MILLISECONDS).writeTimeout(3000L, TimeUnit.MILLISECONDS).hostnameVerifier(HttpConnection.DO_NOT_VERIFY).sslSocketFactory(HttpConnection.getSslSocketFactory()).build();
                for (String str : this.allAppServers) {
                    try {
                        execute = build.newCall(new Request.Builder().url(String.format("http://%s:%s/api/exclude/siteConfig/servers", getRealIpAddress(), getRealIpPort())).build()).execute();
                    } catch (Throwable th) {
                        th.printStackTrace();
                    }
                    if (execute.isSuccessful()) {
                        String string = execute.body().string();
                        this.serverConfig = string;
                        if (StringUtils.isNotBlank(string)) {
                            ApiServerConfigResponse apiServerConfigResponse = (ApiServerConfigResponse) ((ApiResponse) new Gson().fromJson(this.serverConfig, new TypeToken<ApiResponse<ApiServerConfigResponse>>() {
                            }.getType())).getData();
                            updateServers(apiServerConfigResponse.getAppServers(), apiServerConfigResponse.getFileServers(), apiServerConfigResponse.getCdnNodes());
                        }
                        return;
                    }
                    continue;
                }
            }
        } finally {
            try {
            } finally {
            }
        }
    }
}