导航菜单

页面标题

页面副标题

RummyCircle v11000.89 - LogPusher.java 源代码

正在查看: RummyCircle v11000.89 应用的 LogPusher.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package in.juspay.hypersdk.analytics;

import android.content.Context;
import androidx.activity.F;
import androidx.activity.r;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.camera.camera2.internal.l1;
import androidx.camera.camera2.internal.n;
import androidx.camera.camera2.internal.o;
import androidx.camera.core.A;
import androidx.camera.core.W;
import in.juspay.hyper.constants.Labels;
import in.juspay.hyper.constants.LogCategory;
import in.juspay.hyper.constants.LogLevel;
import in.juspay.hyper.constants.LogSubCategory;
import in.juspay.hyper.core.ExecutorManager;
import in.juspay.hyper.core.JuspayCoreLib;
import in.juspay.hyper.core.JuspayLogger;
import in.juspay.hypersdk.R;
import in.juspay.hypersdk.core.SdkTracker;
import in.juspay.hypersdk.security.EncryptionHelper;
import in.juspay.hypersdk.security.JOSEUtils;
import in.juspay.hypersdk.services.SdkConfigService;
import in.juspay.hypersdk.utils.Utils;
import in.juspay.hypersdk.utils.network.JuspayHttpsResponse;
import in.juspay.hypersdk.utils.network.NetUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.interfaces.RSAPublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class LogPusher {
    private static final String TAG = "LogPusher";
    private static int currentBatchRetryAttempts;
    private static TimerTask logPushTimerTask;
    private static final ConcurrentLinkedQueue<byte[]> logsQueue = new ConcurrentLinkedQueue<>();

    @NonNull
    private static final Map<String, String> headers = new HashMap();
    private static int getLogsToPushErrorCounter = 0;
    private static int logFlushTimerTaskErrorCounter = 0;
    private static int logPushTimerTaskErrorCounter = 0;
    private static int setHeaderParametersErrorCounter = 0;
    private static boolean isSandboxEnv = false;
    private static boolean stopPushingLogs = false;

    @NonNull
    private static Timer logPushTimer = new Timer();

    @NonNull
    private static final AtomicInteger logPusherNumCounter = new AtomicInteger(0);

    public static class IterableJSONArray implements Iterable<JSONObject> {
        JSONArray original;

        public IterableJSONArray(JSONArray jSONArray) {
            this.original = jSONArray;
        }

        @Override
        @NonNull
        public Iterator<JSONObject> iterator() {
            return new Iterator<JSONObject>() {
                int curr = 0;

                @Override
                public boolean hasNext() {
                    return this.curr < IterableJSONArray.this.original.length();
                }

                @Override
                public JSONObject next() {
                    try {
                        JSONArray jSONArray = IterableJSONArray.this.original;
                        int i = this.curr;
                        this.curr = i + 1;
                        return jSONArray.optJSONObject(i);
                    } catch (ArrayIndexOutOfBoundsException unused) {
                        return null;
                    }
                }

                @Override
                public void remove() {
                }
            };
        }
    }

    public static class LogPushTimerTask extends TimerTask {
        private static final String TAG = "LogPushTimerTask";
        private static boolean isExceptionTracked = false;

        private LogPushTimerTask() {
        }

        public static void lambda$run$0() {
            if (LogUtils.isMinMemoryAvailable().booleanValue()) {
                int fromSharedPreference = LogUtils.getFromSharedPreference(LogConstants.PERSISTENT_LOGS_READING_FILE);
                int fromSharedPreference2 = LogUtils.getFromSharedPreference(LogConstants.PERSISTENT_LOGS_WRITING_FILE);
                if (JuspayCoreLib.getApplicationContext() != null && (fromSharedPreference2 - fromSharedPreference) + 1 > LogConstants.maxFilesAllowed) {
                    while ((fromSharedPreference2 - fromSharedPreference) + 1 > LogConstants.numFilesToLeaveIfMaxFilesExceeded) {
                        File file = LogUtils.getFile(LogConstants.PERSISTENT_LOGS_FILE + fromSharedPreference + ".dat");
                        if (file != null) {
                            try {
                                file.delete();
                            } catch (Exception unused) {
                            }
                        }
                        fromSharedPreference++;
                    }
                    LogUtils.writeToSharedPreference(LogConstants.PERSISTENT_LOGS_READING_FILE, String.valueOf(fromSharedPreference));
                }
                while (LogPusher.logsQueue.size() > 0) {
                    try {
                        JSONArray logsToPush = LogPusher.getLogsToPush();
                        if (logsToPush.length() > 0) {
                            int pushLogsToServer = LogPusher.pushLogsToServer(logsToPush);
                            if (pushLogsToServer != 200 && (LogConstants.maxRetryPerBatch == -1 || LogPusher.currentBatchRetryAttempts < LogConstants.maxRetryPerBatch)) {
                                SdkTracker.trackBootAction(LogSubCategory.Action.SYSTEM, "error", Labels.System.LOG_PUSHER, "error_response", "" + pushLogsToServer);
                                LogPusher.access$308();
                                return;
                            }
                            int unused2 = LogPusher.currentBatchRetryAttempts = 0;
                            LogPusher.acknowledgeLogsPushed(logsToPush.length());
                        }
                    } catch (Exception e) {
                        if (!isExceptionTracked) {
                            SdkTracker.trackAndLogBootException(TAG, LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.LOG_PUSHER, "Error while creating the payload to post", e);
                        }
                        isExceptionTracked = true;
                    }
                }
                int fromSharedPreference3 = LogUtils.getFromSharedPreference(LogConstants.PERSISTENT_LOGS_WRITING_FILE);
                if (JuspayCoreLib.getApplicationContext() != null) {
                    for (int fromSharedPreference4 = LogUtils.getFromSharedPreference(LogConstants.PERSISTENT_LOGS_READING_FILE); fromSharedPreference4 <= fromSharedPreference3; fromSharedPreference4++) {
                        new File(JuspayCoreLib.getApplicationContext().getCacheDir(), W.e(LogConstants.PERSISTENT_LOGS_FILE, fromSharedPreference4, ".dat")).delete();
                    }
                }
                LogUtils.writeToSharedPreference(LogConstants.PERSISTENT_LOGS_READING_FILE, String.valueOf(0));
                LogUtils.writeToSharedPreference(LogConstants.PERSISTENT_LOGS_WRITING_FILE, String.valueOf(0));
            }
        }

        @Override
        public void run() {
            ExecutorManager.runOnLogsPool(new a());
        }
    }

    static {
        ExecutorManager.runOnLogsPool(new com.google.android.datatransport.runtime.scheduling.jobscheduling.a(1));
    }

    public static int access$308() {
        int i = currentBatchRetryAttempts;
        currentBatchRetryAttempts = i + 1;
        return i;
    }

    public static void acknowledgeLogsPushed(int i) {
        while (i > 0) {
            try {
                logsQueue.poll();
                i--;
            } catch (Exception e) {
                int i2 = logPushTimerTaskErrorCounter + 1;
                logPushTimerTaskErrorCounter = i2;
                if (i2 <= 2) {
                    SdkTracker.trackAndLogBootException(TAG, LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.LOG_PUSHER, "Exception in removal of logs from persisted Queue file", e);
                    return;
                } else {
                    JuspayLogger.e(TAG, "Exception in removal of logs from persisted Queue file", e);
                    return;
                }
            }
        }
    }

    public static void addLogLines(Iterable<JSONObject> iterable) {
        if (stopPushingLogs) {
            return;
        }
        ExecutorManager.runOnLogsPool(new F(iterable, 13));
    }

    public static void addLogsFromSessioniser(ArrayList<JSONObject> arrayList) {
        ExecutorManager.runOnLogsPool(new o(arrayList, 12));
    }

    public static void addLogsToPersistedQueue(String str) {
        ExecutorManager.runOnLogsPool(new A(str, 8));
    }

    @Keep
    public static void flushLogPush() {
        logPushTimerTask.run();
    }

    private static String getEndPoint() {
        Context applicationContext = JuspayCoreLib.getApplicationContext();
        if (applicationContext == null) {
            return "https://logs.juspay.in/godel/analytics";
        }
        String string = applicationContext.getResources().getString(isSandboxEnv ? R.string.juspay_analytics_endpoint_sandbox : R.string.juspay_analytics_endpoint);
        JSONObject cachedSdkConfig = SdkConfigService.getCachedSdkConfig();
        if (cachedSdkConfig != null) {
            try {
                return isSandboxEnv ? cachedSdkConfig.getJSONObject("logsConfig").optString("logsUrlKeySandbox", string) : cachedSdkConfig.getJSONObject("logsConfig").optString("logsUrlKey", string);
            } catch (Exception unused) {
                SdkTracker.trackBootAction(LogSubCategory.Action.SYSTEM, LogLevel.WARNING, Labels.System.LOG_PUSHER, "sdk_config", "unable to fetch log endpoint from sdk config");
            }
        }
        return string;
    }

    private static RSAPublicKey getLogEncryptionKey() {
        JSONObject jSONObject;
        String str;
        JSONObject cachedSdkConfig = SdkConfigService.getCachedSdkConfig();
        if (cachedSdkConfig == null) {
            SdkTracker.trackBootAction(LogSubCategory.Action.SYSTEM, LogLevel.WARNING, Labels.System.LOG_PUSHER, "sdk_config", "unable to fetch analytics public key from sdk config");
            return null;
        }
        try {
            if (isSandboxEnv) {
                jSONObject = cachedSdkConfig.getJSONObject("logsConfig");
                str = "publicKeySandbox";
            } else {
                jSONObject = cachedSdkConfig.getJSONObject("logsConfig");
                str = "publicKey";
            }
            return JOSEUtils.JWKtoRSAPublicKey(jSONObject.getJSONObject(str));
        } catch (Exception e) {
            SdkTracker.trackBootException(LogSubCategory.Action.SYSTEM, LogLevel.WARNING, Labels.System.LOG_PUSHER, "sdk_config", e);
            return null;
        }
    }

    private static String getLogEncryptionLevel() {
        JSONObject cachedSdkConfig = SdkConfigService.getCachedSdkConfig();
        if (cachedSdkConfig != null) {
            try {
                return cachedSdkConfig.getJSONObject("logsConfig").optString("encryptionLevelKey", "gzip");
            } catch (Exception unused) {
                SdkTracker.trackBootAction(LogSubCategory.Action.SYSTEM, LogLevel.WARNING, Labels.System.LOG_PUSHER, "sdk_config", "unable to log encryption level from sdk config");
            }
        }
        return "gzip";
    }

    public static JSONArray getLogsToPush() {
        JSONArray jSONArray = new JSONArray();
        Iterator<byte[]> it = logsQueue.iterator();
        while (true) {
            if ((LogConstants.maxLogsPerPush == -1 || jSONArray.length() < LogConstants.maxLogsPerPush) && it.hasNext()) {
                try {
                    jSONArray.put(new JSONObject(new String(it.next())));
                } catch (JSONException e) {
                    it.remove();
                    int i = getLogsToPushErrorCounter + 1;
                    getLogsToPushErrorCounter = i;
                    if (i <= 2) {
                        SdkTracker.trackAndLogBootException(TAG, LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.LOG_PUSHER, "Bad JSON while reading the Persisted Queue for Logs", e);
                    } else {
                        JuspayLogger.e(TAG, "Bad JSON while reading the Persisted Queue for Logs", e);
                    }
                }
            }
        }
        return jSONArray;
    }

    public static void lambda$addLogLines$5(Iterable iterable) {
        Exception exc;
        FileOutputStream fileOutputStream;
        try {
            int fromSharedPreference = LogUtils.getFromSharedPreference(LogConstants.PERSISTENT_LOGS_WRITING_FILE);
            if (fromSharedPreference == -1) {
                fromSharedPreference = 0;
            }
            FileOutputStream fileOutputStream2 = new FileOutputStream(LogUtils.getFile(LogConstants.PERSISTENT_LOGS_FILE + fromSharedPreference + ".dat"), true);
            Iterator it = iterable.iterator();
            while (it.hasNext()) {
                JSONObject jSONObject = (JSONObject) it.next();
                if (jSONObject != null) {
                    try {
                        String str = jSONObject + LogConstants.LOG_DELIMITER;
                        Charset charset = StandardCharsets.UTF_8;
                        byte[] bytes = str.getBytes(charset);
                        if (LogUtils.getFileLength(LogConstants.PERSISTENT_LOGS_FILE + fromSharedPreference + ".dat") + bytes.length <= LogConstants.maxLogFileSize) {
                            logsQueue.add(jSONObject.toString().getBytes(charset));
                            fileOutputStream2.write(bytes);
                        } else if (bytes.length <= LogConstants.maxLogLineSize) {
                            logsQueue.add(jSONObject.toString().getBytes(charset));
                            fromSharedPreference++;
                            LogUtils.writeToSharedPreference(LogConstants.PERSISTENT_LOGS_WRITING_FILE, String.valueOf(fromSharedPreference));
                            FileOutputStream fileOutputStream3 = new FileOutputStream(LogUtils.getFile(LogConstants.PERSISTENT_LOGS_FILE + fromSharedPreference + ".dat"), true);
                            try {
                                fileOutputStream3.write(bytes);
                                fileOutputStream2 = fileOutputStream3;
                            } catch (Exception e) {
                                exc = e;
                                fileOutputStream = fileOutputStream3;
                                int i = logFlushTimerTaskErrorCounter + 1;
                                logFlushTimerTaskErrorCounter = i;
                                if (i <= 2) {
                                    SdkTracker.trackAndLogBootException(TAG, LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.LOG_PUSHER, "Exception while flushing the logs to persisted queue file", exc);
                                }
                                fileOutputStream2 = fileOutputStream;
                            }
                        }
                    } catch (Exception e2) {
                        exc = e2;
                        fileOutputStream = fileOutputStream2;
                    }
                }
            }
            fileOutputStream2.close();
        } catch (Exception e3) {
            int i2 = logFlushTimerTaskErrorCounter + 1;
            logFlushTimerTaskErrorCounter = i2;
            if (i2 <= 2) {
                SdkTracker.trackAndLogBootException(TAG, LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.LOG_PUSHER, "Exception while flushing the logs to persisted queue file", e3);
            }
        }
    }

    public static void lambda$addLogsFromSessioniser$4(ArrayList arrayList) {
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            byte[] bytes = ((JSONObject) it.next()).toString().getBytes(StandardCharsets.UTF_8);
            if (bytes.length <= LogConstants.maxLogLineSize) {
                logsQueue.add(bytes);
            }
        }
    }

    public static void lambda$addLogsToPersistedQueue$3(String str) {
        try {
            if (JuspayCoreLib.getApplicationContext() != null) {
                File file = new File(JuspayCoreLib.getApplicationContext().getCacheDir(), LogConstants.CRASH_LOGS_FILE);
                byte[] bytes = (str + LogConstants.LOG_DELIMITER).getBytes(StandardCharsets.UTF_8);
                if (bytes.length < LogConstants.maxLogLineSize) {
                    FileOutputStream fileOutputStream = new FileOutputStream(file, true);
                    fileOutputStream.write(bytes);
                    fileOutputStream.close();
                }
            }
        } catch (Exception e) {
            JuspayLogger.e(TAG, "addLogsToPersistedQueue failed", e);
        }
    }

    public static void lambda$setHeaders$6(JSONObject jSONObject) {
        Iterator<String> keys = jSONObject.keys();
        while (keys.hasNext()) {
            String next = keys.next();
            try {
                headers.put(next, jSONObject.getString(next));
            } catch (JSONException unused) {
            }
        }
    }

    public static void lambda$setLogHeaderValues$7(JSONObject jSONObject) {
        JSONObject jSONObject2 = LogConstants.logHeaders;
        Iterator<String> keys = jSONObject2.keys();
        while (keys.hasNext()) {
            try {
                String next = keys.next();
                String optString = jSONObject2.optString(next);
                int indexOf = optString.indexOf(36);
                int indexOf2 = optString.indexOf(123);
                int lastIndexOf = optString.lastIndexOf(125);
                if (indexOf != -1 && indexOf2 != -1 && lastIndexOf != -1 && indexOf2 - indexOf == 1 && indexOf2 < lastIndexOf) {
                    String substring = optString.substring(indexOf2 + 1, lastIndexOf);
                    String str = "${" + substring + "}";
                    if (jSONObject.has(substring)) {
                        optString = optString.replace(str, jSONObject.optString(substring));
                    }
                }
                headers.put(next, optString);
            } catch (Exception unused) {
                int i = setHeaderParametersErrorCounter + 1;
                setHeaderParametersErrorCounter = i;
                if (i <= 2) {
                    SdkTracker.trackBootAction(LogSubCategory.LifeCycle.HYPER_SDK, LogLevel.WARNING, Labels.System.LOG_PUSHER, "sdk_config", "Unable log header properties in log headers");
                    return;
                }
                return;
            }
        }
    }

    public static void lambda$startLogPusherTimer$1() {
        try {
            if (logPusherNumCounter.getAndIncrement() == 0) {
                stopPushingLogs = false;
                LogSessioniser.startLogSessioniser();
                logPushTimer = new Timer();
                LogPushTimerTask logPushTimerTask2 = new LogPushTimerTask();
                logPushTimerTask = logPushTimerTask2;
                Timer timer = logPushTimer;
                long j = LogConstants.logPostInterval;
                timer.scheduleAtFixedRate(logPushTimerTask2, j, j);
            }
        } catch (Exception unused) {
        }
    }

    public static void lambda$static$0() {
        ExecutorManager.setLogsThreadId(Thread.currentThread().getId());
        int fromSharedPreference = LogUtils.getFromSharedPreference(LogConstants.PERSISTENT_LOGS_READING_FILE);
        int i = 0;
        if (fromSharedPreference == -1) {
            LogUtils.writeToSharedPreference(LogConstants.PERSISTENT_LOGS_READING_FILE, String.valueOf(0));
            fromSharedPreference = 0;
        }
        int fromSharedPreference2 = LogUtils.getFromSharedPreference(LogConstants.PERSISTENT_LOGS_WRITING_FILE);
        if (fromSharedPreference2 == -1) {
            LogUtils.writeToSharedPreference(LogConstants.PERSISTENT_LOGS_WRITING_FILE, String.valueOf(0));
        } else {
            i = fromSharedPreference2;
        }
        pushAllFiles(fromSharedPreference, i);
    }

    public static void lambda$stopLogPusherOnTerminate$2() {
        AtomicInteger atomicInteger = logPusherNumCounter;
        if (atomicInteger.decrementAndGet() <= 0) {
            atomicInteger.set(0);
            try {
                LogSessioniser.stopLogSessioniserOnTerminate();
                logPushTimer.cancel();
                logPushTimer = new Timer();
                LogPushTimerTask logPushTimerTask2 = new LogPushTimerTask();
                logPushTimerTask = logPushTimerTask2;
                logPushTimerTask2.run();
                stopPushingLogs = true;
            } catch (Exception unused) {
            }
        }
    }

    private static void pushAllFiles(int i, int i2) {
        while (i <= i2) {
            File file = LogUtils.getFile(LogConstants.PERSISTENT_LOGS_FILE + i + ".dat");
            if (file != null) {
                if (file.exists() && LogUtils.isFileEligibleToPush(file)) {
                    pushFileContentToServer(file);
                } else {
                    file.delete();
                }
            }
            i++;
        }
        if (JuspayCoreLib.getApplicationContext() != null) {
            File file2 = new File(JuspayCoreLib.getApplicationContext().getCacheDir(), LogConstants.CRASH_LOGS_FILE);
            if (file2.exists() && LogUtils.isFileEligibleToPush(file2)) {
                pushFileContentToServer(file2);
            } else {
                file2.delete();
            }
        }
    }

    private static void pushFileContentToServer(File file) {
        if (file != null) {
            ArrayList<JSONObject> logsFromFile = LogUtils.getLogsFromFile(file);
            try {
                JSONArray jSONArray = new JSONArray();
                boolean z = true;
                while (logsFromFile.size() > 0) {
                    while (true) {
                        if ((LogConstants.maxLogsPerPush == -1 || jSONArray.length() < LogConstants.maxLogsPerPush) && logsFromFile.size() > 0) {
                            jSONArray.put(logsFromFile.remove(0));
                        }
                    }
                    if (pushLogsToServer(jSONArray) != 200) {
                        z = false;
                    }
                    jSONArray = new JSONArray();
                }
                if (z) {
                    file.delete();
                }
            } catch (Exception unused) {
            }
        }
    }

    public static int pushLogsToServer(JSONArray jSONArray) {
        JuspayHttpsResponse juspayHttpsResponse;
        JSONObject jSONObject = new JSONObject();
        jSONObject.put("data", jSONArray);
        byte[] bytes = jSONObject.toString().getBytes(StandardCharsets.UTF_8);
        String logEncryptionLevel = getLogEncryptionLevel();
        RSAPublicKey logEncryptionKey = getLogEncryptionKey();
        NetUtils netUtils = new NetUtils(10000, 10000);
        Map<String, String> map = headers;
        map.put("x-logscount", String.valueOf(jSONArray.length()));
        if ("encryption".equals(logEncryptionLevel) && logEncryptionKey != null) {
            juspayHttpsResponse = new JuspayHttpsResponse(netUtils.doPost(new URL(getEndPoint()), EncryptionHelper.gzipThenEncrypt(bytes, logEncryptionKey), "application/x-godel-gzip-pubkey-encrypted", map, new JSONObject()));
        } else if ("gzip".equals(logEncryptionLevel)) {
            byte[] gzipContent = Utils.gzipContent(bytes);
            map.put("Content-Encoding", "gzip");
            juspayHttpsResponse = new JuspayHttpsResponse(netUtils.doPost(new URL(getEndPoint()), gzipContent, "application/gzip", map, new JSONObject()));
        } else {
            juspayHttpsResponse = new JuspayHttpsResponse(netUtils.doPost(new URL(getEndPoint()), bytes, "application/json", map, new JSONObject()));
        }
        return juspayHttpsResponse.responseCode;
    }

    public static void setEndPointSandbox(Boolean bool) {
        isSandboxEnv = bool.booleanValue();
    }

    public static void setHeaders(JSONObject jSONObject) {
        ExecutorManager.runOnLogsPool(new r(jSONObject, 10));
    }

    public static void setLogHeaderValues(JSONObject jSONObject) {
        ExecutorManager.runOnLogsPool(new l1(jSONObject, 7));
    }

    public static void startLogPusherTimer() {
        ExecutorManager.runOnLogsPool(new com.games24x7.coregame.common.deeplink.router.unity.addcash.a(1));
    }

    public static void stopLogPusherOnTerminate() {
        ExecutorManager.runOnLogsPool(new n(2));
    }

    public static void addLogLines(JSONArray jSONArray) {
        if (stopPushingLogs) {
            return;
        }
        addLogLines(new IterableJSONArray(jSONArray));
    }
}