Fast Orange v3.5.3版本的 MD5 值为:33b91aba50c566533dfd683a85f79158

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


package com.github.shadowsocks.bg;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.ProxyInfo;
import android.net.VpnService;
import android.os.Build;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.PowerManager;
import android.system.ErrnoException;
import android.system.Os;
import android.util.Log;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.exifinterface.media.ExifInterface;
import com.free.vpn.proxy.hotspot.an1;
import com.free.vpn.proxy.hotspot.m70;
import com.free.vpn.proxy.hotspot.q00;
import com.free.vpn.proxy.hotspot.t00;
import com.free.vpn.proxy.hotspot.x74;
import com.free.vpn.proxy.hotspot.zm2;
import com.free.vpn.proxy.hotspot.zs4;
import com.github.shadowsocks.Core;
import com.github.shadowsocks.Key;
import com.github.shadowsocks.bg.BaseService;
import com.github.shadowsocks.bg.LocalResolver;
import com.github.shadowsocks.bg.proto.ProxyInstance;
import com.github.shadowsocks.core.R;
import com.github.shadowsocks.database.DataStore;
import com.github.shadowsocks.database.ProxyEntity;
import com.github.shadowsocks.database.ShadowsocksDatabase;
import com.github.shadowsocks.database.StatsEntity;
import com.github.shadowsocks.fmt.ConfigBuilderKt;
import com.github.shadowsocks.fmt.hysteria.HysteriaBean;
import com.github.shadowsocks.ktx.Logs;
import com.github.shadowsocks.utils.DefaultNetworkListener;
import com.github.shadowsocks.utils.PackageCache;
import com.github.shadowsocks.utils.Subnet;
import com.google.android.gms.appindex.ThingPropertyKeys;
import com.google.android.gms.measurement.api.AppMeasurementSdk;
import go.Seq;
import java.io.FileDescriptor;
import java.io.IOException;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import kotlin.Lazy;
import kotlin.LazyKt;
import kotlin.Metadata;
import kotlin.ResultKt;
import kotlin.Unit;
import kotlin.coroutines.Continuation;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlinx.coroutines.BuildersKt__Builders_commonKt;
import kotlinx.coroutines.Dispatchers;
import kotlinx.coroutines.GlobalScope;
import libcore.AppStats;
import libcore.Libcore;
import libcore.Protector;
import libcore.QueryContext;
import libcore.TrafficListener;
import libcore.Tun2ray;
import libcore.TunConfig;
import okhttp3.internal.ws.WebSocketProtocol;

@Metadata(d1 = {"\u0000\u0094\u0001\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010!\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0010\u0011\n\u0002\b\u0003\n\u0002\u0010\b\n\u0002\b\f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0010\n\u0002\u0010 \n\u0000\u0018\u0000 [2\u00020\u00012\u00020\u00022\u00020\u00032\u00020\u00042\u00020\u0005:\u0002[\\B\u0005¢\u0006\u0002\u0010\u0006J\b\u0010B\u001a\u00020CH\u0017J\u0010\u0010D\u001a\u00020E2\u0006\u0010F\u001a\u00020\u001aH\u0016J\n\u0010G\u001a\u0004\u0018\u00010$H\u0002J\u000e\u0010H\u001a\u00020/2\u0006\u0010I\u001a\u00020$J\b\u0010J\u001a\u00020CH\u0016J\u0012\u0010K\u001a\u0004\u0018\u00010L2\u0006\u0010M\u001a\u00020NH\u0016J\b\u0010O\u001a\u00020CH\u0016J\b\u0010P\u001a\u00020CH\u0016J\"\u0010Q\u001a\u00020/2\b\u0010M\u001a\u0004\u0018\u00010N2\u0006\u0010R\u001a\u00020/2\u0006\u0010S\u001a\u00020/H\u0016J\u0006\u0010T\u001a\u00020CJ\u0011\u0010U\u001a\u00020CH\u0096@ø\u0001\u0000¢\u0006\u0002\u0010VJ\u0011\u0010W\u001a\u00020CH\u0096@ø\u0001\u0000¢\u0006\u0002\u0010VJ\b\u0010X\u001a\u00020CH\u0002J\u0012\u0010Y\u001a\u00020C2\b\u0010Z\u001a\u0004\u0018\u00010\u000bH\u0016R\u000e\u0010\u0007\u001a\u00020\bX\u0082\u000e¢\u0006\u0002\n\u0000R\u0017\u0010\t\u001a\b\u0012\u0004\u0012\u00020\u000b0\n¢\u0006\b\n\u0000\u001a\u0004\b\f\u0010\rR\u001a\u0010\u000e\u001a\u00020\u000fX\u0086.¢\u0006\u000e\n\u0000\u001a\u0004\b\u0010\u0010\u0011\"\u0004\b\u0012\u0010\u0013R\u0014\u0010\u0014\u001a\u00020\u0015X\u0096\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0016\u0010\u0017R\u000e\u0010\u0018\u001a\u00020\bX\u0082\u000e¢\u0006\u0002\n\u0000R\u0014\u0010\u0019\u001a\u00020\u001aX\u0096D¢\u0006\b\n\u0000\u001a\u0004\b\u001b\u0010\u001cR\u001c\u0010\u001d\u001a\u0004\u0018\u00010\u001eX\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u001f\u0010 \"\u0004\b!\u0010\"R(\u0010%\u001a\u0004\u0018\u00010$2\b\u0010#\u001a\u0004\u0018\u00010$@VX\u0096\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b&\u0010'\"\u0004\b(\u0010)R\u001c\u0010*\u001a\n\u0012\u0004\u0012\u00020$\u0018\u00010+8BX\u0082\u0004¢\u0006\u0006\u001a\u0004\b,\u0010-R\u001a\u0010.\u001a\u00020/X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b0\u00101\"\u0004\b2\u00103R\u001c\u00104\u001a\u0004\u0018\u00010\u001aX\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b5\u0010\u001c\"\u0004\b6\u00107R\u0014\u00108\u001a\u00020\bX\u0086D¢\u0006\b\n\u0000\u001a\u0004\b9\u0010:R \u0010;\u001a\b\u0018\u00010<R\u00020=X\u0096\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b>\u0010?\"\u0004\b@\u0010A\u0082\u0002\u0004\n\u0002\b\u0019¨\u0006]²\u0006\u0018\u0010^\u001a\u0010\u0012\f\u0012\n `*\u0004\u0018\u00010\u001a0\u001a0_X\u008a\u0084\u0002"}, d2 = {"Lcom/github/shadowsocks/bg/VpnService;", "Landroid/net/VpnService;", "Lcom/github/shadowsocks/bg/BaseService$Interface;", "Llibcore/TrafficListener;", "Llibcore/Protector;", "Lcom/github/shadowsocks/bg/LocalResolver;", "()V", AppMeasurementSdk.ConditionalUserProperty.ACTIVE, "", "appStats", "", "Llibcore/AppStats;", "getAppStats", "()Ljava/util/List;", "conn", "Landroid/os/ParcelFileDescriptor;", "getConn", "()Landroid/os/ParcelFileDescriptor;", "setConn", "(Landroid/os/ParcelFileDescriptor;)V", "data", "Lcom/github/shadowsocks/bg/BaseService$Data;", "getData", "()Lcom/github/shadowsocks/bg/BaseService$Data;", "metered", "tag", "", "getTag", "()Ljava/lang/String;", "tun", "Llibcore/Tun2ray;", "getTun", "()Llibcore/Tun2ray;", "setTun", "(Llibcore/Tun2ray;)V", "value", "Landroid/net/Network;", "underlyingNetwork", "getUnderlyingNetwork", "()Landroid/net/Network;", "setUnderlyingNetwork", "(Landroid/net/Network;)V", "underlyingNetworks", "", "getUnderlyingNetworks", "()[Landroid/net/Network;", "upstreamInterfaceMTU", "", "getUpstreamInterfaceMTU", "()I", "setUpstreamInterfaceMTU", "(I)V", "upstreamInterfaceName", "getUpstreamInterfaceName", "setUpstreamInterfaceName", "(Ljava/lang/String;)V", "useUpstreamInterfaceMTU", "getUseUpstreamInterfaceMTU", "()Z", "wakeLock", "Landroid/os/PowerManager$WakeLock;", "Landroid/os/PowerManager;", "getWakeLock", "()Landroid/os/PowerManager$WakeLock;", "setWakeLock", "(Landroid/os/PowerManager$WakeLock;)V", Key.ACQUIRE_WAKE_LOCK, "", "createNotification", "Lcom/github/shadowsocks/bg/ServiceNotification;", Key.PROFILE_NAME, "getActiveNetworkUnder23", "getMTU", "network", "killProcesses", "onBind", "Landroid/os/IBinder;", ThingPropertyKeys.APP_INTENT, "Landroid/content/Intent;", "onDestroy", "onRevoke", "onStartCommand", "flags", "startId", "persistAppStats", "preInit", "(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", "startProcesses", "startVpn", "updateStats", "stats", "Companion", "NullConnectionException", "shadowsocks_release", "allApps", "", "kotlin.jvm.PlatformType"}, k = 1, mv = {1, 8, 0}, xi = ConstraintLayout.LayoutParams.Table.LAYOUT_CONSTRAINT_VERTICAL_CHAINSTYLE)
public final class VpnService extends android.net.VpnService implements BaseService.Interface, TrafficListener, Protector, LocalResolver {

    public static final Companion INSTANCE = new Companion(null);
    public static final int DEFAULT_MTU = 1500;
    public static final String PRIVATE_VLAN4_CLIENT = "172.19.0.1";
    public static final String PRIVATE_VLAN4_GATEWAY = "172.19.0.2";
    public static final String PRIVATE_VLAN6_CLIENT = "fdfe:dcba:9876::1";
    public static final String PRIVATE_VLAN6_GATEWAY = "fdfe:dcba:9876::2";
    private static VpnService instance;
    private boolean active;
    public ParcelFileDescriptor conn;
    private boolean metered;
    private Tun2ray tun;
    private volatile Network underlyingNetwork;
    private int upstreamInterfaceMTU;
    private String upstreamInterfaceName;
    private PowerManager.WakeLock wakeLock;
    private final BaseService.Data data = new BaseService.Data(this);
    private final String tag = "VpnService";
    private final boolean useUpstreamInterfaceMTU = true;
    private final List<AppStats> appStats = new ArrayList();

    @Metadata(d1 = {"\u0000,\n\u0000\n\u0002\u0010\u0000\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\b\b\u0086\u0003\u0018\u00002\u00020\u0001B\t\b\u0002¢\u0006\u0004\b\u0018\u0010\u0019J-\u0010\u0006\u001a\u00028\u0000\"\u0004\b\u0000\u0010\u0002*\u00020\u00032\u0012\u0010\u0005\u001a\u000e\u0012\u0004\u0012\u00020\u0003\u0012\u0004\u0012\u00028\u00000\u0004H\u0002¢\u0006\u0004\b\u0006\u0010\u0007R$\u0010\t\u001a\u0004\u0018\u00010\b8\u0006@\u0006X\u0086\u000e¢\u0006\u0012\n\u0004\b\t\u0010\n\u001a\u0004\b\u000b\u0010\f\"\u0004\b\r\u0010\u000eR\u0014\u0010\u0010\u001a\u00020\u000f8\u0006X\u0086T¢\u0006\u0006\n\u0004\b\u0010\u0010\u0011R\u0014\u0010\u0013\u001a\u00020\u00128\u0006X\u0086T¢\u0006\u0006\n\u0004\b\u0013\u0010\u0014R\u0014\u0010\u0015\u001a\u00020\u00128\u0006X\u0086T¢\u0006\u0006\n\u0004\b\u0015\u0010\u0014R\u0014\u0010\u0016\u001a\u00020\u00128\u0006X\u0086T¢\u0006\u0006\n\u0004\b\u0016\u0010\u0014R\u0014\u0010\u0017\u001a\u00020\u00128\u0006X\u0086T¢\u0006\u0006\n\u0004\b\u0017\u0010\u0014¨\u0006\u001a"}, d2 = {"com/github/shadowsocks/bg/VpnService$Companion", "", ExifInterface.GPS_DIRECTION_TRUE, "Ljava/io/FileDescriptor;", "Lkotlin/Function1;", ConfigBuilderKt.TAG_BLOCK, "use", "(Ljava/io/FileDescriptor;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;", "Lcom/github/shadowsocks/bg/VpnService;", "instance", "Lcom/github/shadowsocks/bg/VpnService;", "getInstance", "()Lcom/github/shadowsocks/bg/VpnService;", "setInstance", "(Lcom/github/shadowsocks/bg/VpnService;)V", "", "DEFAULT_MTU", "I", "", "PRIVATE_VLAN4_CLIENT", "Ljava/lang/String;", "PRIVATE_VLAN4_GATEWAY", "PRIVATE_VLAN6_CLIENT", "PRIVATE_VLAN6_GATEWAY", "<init>", "()V", "shadowsocks_release"}, k = 1, mv = {1, 8, 0})
    public final class Companion {
        private Companion() {
        }

        public Companion(DefaultConstructorMarker defaultConstructorMarker) {
            this();
        }

        private final <T> T use(FileDescriptor fileDescriptor, Function1 function1) {
            try {
                return (T) function1.invoke(fileDescriptor);
            } finally {
                try {
                    Os.close(fileDescriptor);
                } catch (ErrnoException unused) {
                }
            }
        }

        public final VpnService getInstance() {
            return VpnService.instance;
        }

        public final void setInstance(VpnService vpnService) {
            VpnService.instance = vpnService;
        }
    }

    @Metadata(d1 = {"\u0000\u0016\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0086\u0004\u0018\u00002\u00060\u0001j\u0002`\u00022\u00020\u0003B\u0007¢\u0006\u0004\b\u0006\u0010\u0007J\b\u0010\u0005\u001a\u00020\u0004H\u0016¨\u0006\b"}, d2 = {"com/github/shadowsocks/bg/VpnService$NullConnectionException", "Ljava/lang/NullPointerException;", "Lkotlin/NullPointerException;", "Lcom/github/shadowsocks/bg/BaseService$ExpectedException;", "", "getLocalizedMessage", "<init>", "(Lcom/github/shadowsocks/bg/VpnService;)V", "shadowsocks_release"}, k = 1, mv = {1, 8, 0})
    public final class NullConnectionException extends NullPointerException implements BaseService.ExpectedException {
        public NullConnectionException() {
        }

        @Override
        public String getLocalizedMessage() {
            String string = VpnService.this.getString(R.string.reboot_required);
            zs4.n(string, "getString(R.string.reboot_required)");
            return string;
        }
    }

    private final Network getActiveNetworkUnder23() {
        Core core = Core.INSTANCE;
        NetworkInfo activeNetworkInfo = core.getConnectivity().getActiveNetworkInfo();
        if (activeNetworkInfo == null) {
            return null;
        }
        Network[] allNetworks = core.getConnectivity().getAllNetworks();
        zs4.n(allNetworks, "Core.connectivity.allNetworks");
        for (Network network : allNetworks) {
            NetworkInfo networkInfo = Core.INSTANCE.getConnectivity().getNetworkInfo(network);
            if (networkInfo != null && networkInfo.getType() == activeNetworkInfo.getType() && networkInfo.isConnected() == activeNetworkInfo.isConnected() && networkInfo.isAvailable() == activeNetworkInfo.isAvailable()) {
                return network;
            }
        }
        return null;
    }

    private final Network[] getUnderlyingNetworks() {
        Network underlyingNetwork;
        if ((Build.VERSION.SDK_INT == 28 && this.metered) || (underlyingNetwork = getUnderlyingNetwork()) == null) {
            return null;
        }
        return new Network[]{underlyingNetwork};
    }

    private final void startVpn() {
        int mtu;
        VpnService.Builder session;
        DataStore dataStore;
        int ipv6Mode;
        int i;
        String packageName;
        boolean proxyApps;
        boolean z;
        Collection<ProxyEntity> values;
        boolean z2;
        boolean z3;
        int i2;
        boolean z4;
        boolean z5;
        Logs logs;
        StringBuilder sb;
        List<String> list;
        int i3;
        ParcelFileDescriptor establish;
        int i4;
        instance = this;
        Libcore.setLocalhostResolver(this);
        if (this.useUpstreamInterfaceMTU) {
            mtu = this.upstreamInterfaceMTU;
            if (mtu > 0) {
                Logs.INSTANCE.d("Use MTU of upstream network: " + mtu);
            } else {
                Network activeNetwork = Core.INSTANCE.getConnectivity().getActiveNetwork();
                if (activeNetwork != null) {
                    try {
                        i4 = getMTU(activeNetwork);
                    } catch (Exception e) {
                        e = e;
                        i4 = 0;
                    }
                    try {
                        this.upstreamInterfaceMTU = i4;
                        Logs.INSTANCE.d("Use MTU of upstream network: " + i4);
                    } catch (Exception e2) {
                        e = e2;
                        Logs.INSTANCE.w("Failed to get MTU of current network", e);
                        mtu = i4;
                        if (mtu == 0) {
                        }
                        int i5 = mtu;
                        ProxyInstance proxy = getData().getProxy();
                        zs4.l(proxy);
                        session = new VpnService.Builder(this).setConfigureIntent((PendingIntent) Core.INSTANCE.getConfigureIntent().invoke(this)).setSession(proxy.getProfile().displayName());
                        zs4.n(session, "Builder().setConfigureIn…on(profile.displayName())");
                        if (!this.useUpstreamInterfaceMTU) {
                        }
                        dataStore = DataStore.INSTANCE;
                        ipv6Mode = dataStore.getIpv6Mode();
                        session.addAddress(PRIVATE_VLAN4_CLIENT, 30);
                        if (ipv6Mode != 0) {
                        }
                        if (dataStore.getBypassLan()) {
                        }
                        session.addRoute("0.0.0.0", 0);
                        if (ipv6Mode != 0) {
                        }
                        i = Build.VERSION.SDK_INT;
                        session.setUnderlyingNetworks(getUnderlyingNetworks());
                        if (i >= 29) {
                        }
                        packageName = getPackageName();
                        DataStore dataStore2 = DataStore.INSTANCE;
                        proxyApps = dataStore2.getProxyApps();
                        int tunImplementation = dataStore2.getTunImplementation();
                        if (tunImplementation != 1) {
                        }
                        ProxyInstance proxy2 = getData().getProxy();
                        zs4.l(proxy2);
                        values = proxy2.getConfig().getOutboundTagsAll().values();
                        if (values instanceof Collection) {
                        }
                        while (r13.hasNext()) {
                        }
                        z3 = false;
                        if (proxyApps) {
                        }
                        DataStore dataStore3 = DataStore.INSTANCE;
                        boolean bypass = dataStore3.getBypass();
                        LinkedHashSet<String> linkedHashSet = new LinkedHashSet();
                        Lazy lazy = LazyKt.lazy(new VpnService$startVpn$allApps$2(this, packageName));
                        if (proxyApps) {
                        }
                        linkedHashSet.addAll(list);
                        z5 = z4;
                        if (z5 ^ z) {
                        }
                        while (r6.hasNext()) {
                        }
                        session.addDnsServer(PRIVATE_VLAN4_GATEWAY);
                        i3 = Build.VERSION.SDK_INT;
                        if (i3 >= 29) {
                        }
                        DataStore dataStore4 = DataStore.INSTANCE;
                        boolean meteredNetwork = dataStore4.getMeteredNetwork();
                        this.metered = meteredNetwork;
                        if (i3 >= 29) {
                        }
                        establish = session.establish();
                        if (establish != null) {
                        }
                    }
                    mtu = i4;
                } else {
                    Logs.INSTANCE.d("Failed to get current network");
                    mtu = 0;
                }
                if (mtu == 0) {
                    mtu = DataStore.INSTANCE.getMtu();
                }
            }
        } else {
            mtu = DataStore.INSTANCE.getMtu();
            Logs.INSTANCE.d("Use MTU: " + this.upstreamInterfaceMTU);
        }
        int i52 = mtu;
        ProxyInstance proxy3 = getData().getProxy();
        zs4.l(proxy3);
        session = new VpnService.Builder(this).setConfigureIntent((PendingIntent) Core.INSTANCE.getConfigureIntent().invoke(this)).setSession(proxy3.getProfile().displayName());
        zs4.n(session, "Builder().setConfigureIn…on(profile.displayName())");
        if (!this.useUpstreamInterfaceMTU) {
            session.setMtu(i52);
        }
        dataStore = DataStore.INSTANCE;
        ipv6Mode = dataStore.getIpv6Mode();
        session.addAddress(PRIVATE_VLAN4_CLIENT, 30);
        if (ipv6Mode != 0) {
            session.addAddress(PRIVATE_VLAN6_CLIENT, WebSocketProtocol.PAYLOAD_SHORT);
        }
        if (dataStore.getBypassLan() || dataStore.getBypassLanInCoreOnly()) {
            session.addRoute("0.0.0.0", 0);
            if (ipv6Mode != 0) {
                session.addRoute("::", 0);
            }
        } else {
            String[] stringArray = getResources().getStringArray(R.array.bypass_private_route);
            zs4.n(stringArray, "resources.getStringArray…ray.bypass_private_route)");
            for (String str : stringArray) {
                Subnet.Companion companion = Subnet.INSTANCE;
                zs4.n(str, "it");
                Subnet fromString$default = Subnet.Companion.fromString$default(companion, str, 0, 2, null);
                zs4.l(fromString$default);
                String hostAddress = fromString$default.getAddress().getHostAddress();
                zs4.l(hostAddress);
                session.addRoute(hostAddress, fromString$default.getPrefixSize());
            }
            session.addRoute(PRIVATE_VLAN4_GATEWAY, 32);
            if (ipv6Mode != 0) {
                session.addRoute("2000::", 3);
                session.addRoute(PRIVATE_VLAN6_GATEWAY, 128);
            }
        }
        i = Build.VERSION.SDK_INT;
        session.setUnderlyingNetworks(getUnderlyingNetworks());
        if (i >= 29) {
            session.setMetered(this.metered);
        }
        packageName = getPackageName();
        DataStore dataStore22 = DataStore.INSTANCE;
        proxyApps = dataStore22.getProxyApps();
        int tunImplementation2 = dataStore22.getTunImplementation();
        z = tunImplementation2 != 1;
        ProxyInstance proxy22 = getData().getProxy();
        zs4.l(proxy22);
        values = proxy22.getConfig().getOutboundTagsAll().values();
        if ((values instanceof Collection) || !values.isEmpty()) {
            for (ProxyEntity proxyEntity : values) {
                if (proxyEntity.getPtBean() == null) {
                    HysteriaBean hysteriaBean = proxyEntity.getHysteriaBean();
                    if (!(hysteriaBean != null && hysteriaBean.getProtocol() == 1)) {
                        z2 = false;
                        if (!z2) {
                            z3 = true;
                            break;
                        }
                    }
                }
                z2 = true;
                if (!z2) {
                }
            }
        }
        z3 = false;
        if (!proxyApps || z3) {
            DataStore dataStore32 = DataStore.INSTANCE;
            boolean bypass2 = dataStore32.getBypass();
            LinkedHashSet<String> linkedHashSet2 = new LinkedHashSet();
            Lazy lazy2 = LazyKt.lazy(new VpnService$startVpn$allApps$2(this, packageName));
            if (proxyApps) {
                i2 = tunImplementation2;
                z4 = false;
                list = startVpn$lambda$6(lazy2);
            } else {
                z4 = false;
                i2 = tunImplementation2;
                List o1 = x74.o1(dataStore32.getIndividual(), new char[]{'\n'}, 0, 6);
                ArrayList arrayList = new ArrayList();
                for (Object obj : o1) {
                    if (!x74.a1((String) obj)) {
                        arrayList.add(obj);
                    }
                }
                linkedHashSet2.addAll(arrayList);
                if (bypass2 && z3) {
                    ArrayList I1 = t00.I1(startVpn$lambda$6(lazy2));
                    I1.removeAll(linkedHashSet2);
                    linkedHashSet2.clear();
                    list = I1;
                } else {
                    z5 = bypass2;
                    if (z5 ^ z) {
                        zs4.n(packageName, "packageName");
                        linkedHashSet2.add(packageName);
                    } else {
                        linkedHashSet2.remove(packageName);
                    }
                    for (String str2 : linkedHashSet2) {
                        if (z5) {
                            try {
                                session.addDisallowedApplication(str2);
                                logs = Logs.INSTANCE;
                                sb = new StringBuilder();
                                sb.append("Add bypass: ");
                                sb.append(str2);
                            } catch (PackageManager.NameNotFoundException e3) {
                                Logs.INSTANCE.w(e3);
                            }
                        } else {
                            session.addAllowedApplication(str2);
                            logs = Logs.INSTANCE;
                            sb = new StringBuilder();
                            sb.append("Add allow: ");
                            sb.append(str2);
                        }
                        logs.d(sb.toString());
                    }
                }
            }
            linkedHashSet2.addAll(list);
            z5 = z4;
            if (z5 ^ z) {
            }
            while (r6.hasNext()) {
            }
        } else {
            if (!z) {
                session.addDisallowedApplication(packageName);
            }
            i2 = tunImplementation2;
        }
        session.addDnsServer(PRIVATE_VLAN4_GATEWAY);
        i3 = Build.VERSION.SDK_INT;
        if (i3 >= 29) {
            DataStore dataStore5 = DataStore.INSTANCE;
            if (dataStore5.getAppendHttpProxy() && dataStore5.getRequireHttp()) {
                session.setHttpProxy(ProxyInfo.buildDirectProxy("127.0.0.1", dataStore5.getHttpPort()));
            }
        }
        DataStore dataStore42 = DataStore.INSTANCE;
        boolean meteredNetwork2 = dataStore42.getMeteredNetwork();
        this.metered = meteredNetwork2;
        if (i3 >= 29) {
            session.setMetered(meteredNetwork2);
        }
        establish = session.establish();
        if (establish != null) {
            throw new NullConnectionException();
        }
        setConn(establish);
        this.active = true;
        TunConfig tunConfig = new TunConfig();
        tunConfig.setFileDescriptor(getConn().getFd());
        tunConfig.setProtect(z);
        tunConfig.setMTU(i52);
        ProxyInstance proxy4 = getData().getProxy();
        zs4.l(proxy4);
        tunConfig.setV2Ray(proxy4.getV2rayPoint());
        tunConfig.setGateway4(PRIVATE_VLAN4_GATEWAY);
        tunConfig.setGateway6(PRIVATE_VLAN6_GATEWAY);
        tunConfig.setIPv6Mode(ipv6Mode);
        tunConfig.setImplementation(i2);
        tunConfig.setSniffing(dataStore42.getTrafficSniffing());
        tunConfig.setOverrideDestination(dataStore42.getDestinationOverride());
        tunConfig.setDebug(dataStore42.getEnableLog());
        ProxyInstance proxy5 = getData().getProxy();
        zs4.l(proxy5);
        tunConfig.setDumpUID(proxy5.getConfig().getDumpUid());
        tunConfig.setTrafficStats(dataStore42.getAppTrafficStatistics());
        tunConfig.setPCap(dataStore42.getEnablePcap());
        tunConfig.setBindUpstream(new Protector() {
            @Override
            public final boolean protect(int i6) {
                boolean startVpn$lambda$11$lambda$10;
                startVpn$lambda$11$lambda$10 = com.github.shadowsocks.bg.VpnService.startVpn$lambda$11$lambda$10(com.github.shadowsocks.bg.VpnService.this, i6);
                return startVpn$lambda$11$lambda$10;
            }
        });
        tunConfig.setErrorHandler(this);
        tunConfig.setProtector(this);
        this.tun = Libcore.newTun2ray(tunConfig);
        Log.e("Config", tunConfig.toString());
    }

    public static final boolean startVpn$lambda$11$lambda$10(VpnService vpnService, int i) {
        zs4.o(vpnService, "this$0");
        vpnService.protect(i);
        try {
            ParcelFileDescriptor fromFd = ParcelFileDescriptor.fromFd(i);
            Network underlyingNetwork = vpnService.getUnderlyingNetwork();
            if (underlyingNetwork != null) {
                underlyingNetwork.bindSocket(fromFd.getFileDescriptor());
            }
            fromFd.close();
            return true;
        } catch (IOException e) {
            Log.e("VpnService", "failed to bind socket to upstream", e);
            return true;
        }
    }

    private static final List<String> startVpn$lambda$6(Lazy<? extends List<String>> lazy) {
        return lazy.getValue();
    }

    @Override
    @SuppressLint({"WakelockTimeout"})
    public void acquireWakeLock() {
        PowerManager.WakeLock newWakeLock = Core.INSTANCE.getPower().newWakeLock(1, "sagernet:vpn");
        newWakeLock.acquire();
        setWakeLock(newWakeLock);
    }

    @Override
    public ServiceNotification createNotification(String profileName) {
        zs4.o(profileName, Key.PROFILE_NAME);
        return new ServiceNotification(this);
    }

    @Override
    public void forceLoad() {
        BaseService.Interface.DefaultImpls.forceLoad(this);
    }

    public final List<AppStats> getAppStats() {
        return this.appStats;
    }

    public final ParcelFileDescriptor getConn() {
        ParcelFileDescriptor parcelFileDescriptor = this.conn;
        if (parcelFileDescriptor != null) {
            return parcelFileDescriptor;
        }
        zs4.s0("conn");
        throw null;
    }

    @Override
    public BaseService.Data getData() {
        return this.data;
    }

    public final int getMTU(Network network) {
        zs4.o(network, "network");
        LinkProperties linkProperties = Core.INSTANCE.getConnectivity().getLinkProperties(network);
        if (linkProperties == null) {
            return 0;
        }
        int mtu = Build.VERSION.SDK_INT >= 29 ? linkProperties.getMtu() : 0;
        if (mtu != 0) {
            return mtu;
        }
        NetworkInterface byName = NetworkInterface.getByName(linkProperties.getInterfaceName());
        return byName != null ? byName.getMTU() : 1500;
    }

    @Override
    public String getTag() {
        return this.tag;
    }

    public final Tun2ray getTun() {
        return this.tun;
    }

    @Override
    public Network getUnderlyingNetwork() {
        return this.underlyingNetwork;
    }

    public final int getUpstreamInterfaceMTU() {
        return this.upstreamInterfaceMTU;
    }

    public final String getUpstreamInterfaceName() {
        return this.upstreamInterfaceName;
    }

    public final boolean getUseUpstreamInterfaceMTU() {
        return this.useUpstreamInterfaceMTU;
    }

    @Override
    public PowerManager.WakeLock getWakeLock() {
        return this.wakeLock;
    }

    @Override
    public void handleError(String str) {
        BaseService.Interface.DefaultImpls.handleError(this, str);
    }

    @Override
    public boolean hasRawSupport() {
        return LocalResolver.DefaultImpls.hasRawSupport(this);
    }

    @Override
    public boolean isVpnService() {
        return BaseService.Interface.DefaultImpls.isVpnService(this);
    }

    @Override
    public void killProcesses() {
        Logs.INSTANCE.e("VpnService: killProcesses");
        Libcore.setLocalhostResolver(null);
        Tun2ray tun2ray = this.tun;
        if (tun2ray != null) {
            tun2ray.close();
        }
        if (this.conn != null) {
            getConn().close();
        }
        BaseService.Interface.DefaultImpls.killProcesses(this);
        persistAppStats();
        this.active = false;
        Tun2ray tun2ray2 = this.tun;
        if (tun2ray2 != null) {
            this.tun = null;
            Seq.destroyRef(tun2ray2.refnum);
        }
        BuildersKt__Builders_commonKt.launch$default(GlobalScope.INSTANCE, Dispatchers.getDefault(), null, new VpnService$killProcesses$4(null), 2, null);
    }

    @Override
    public Object lateInit(Continuation<? super Unit> continuation) {
        return BaseService.Interface.DefaultImpls.lateInit(this, continuation);
    }

    @Override
    public void lookupIP(QueryContext queryContext, String str, String str2) {
        LocalResolver.DefaultImpls.lookupIP(this, queryContext, str, str2);
    }

    @Override
    public IBinder onBind(Intent intent) {
        zs4.o(intent, ThingPropertyKeys.APP_INTENT);
        String action = intent.getAction();
        return (action != null && action.hashCode() == -700396143 && action.equals("android.net.VpnService")) ? super.onBind(intent) : BaseService.Interface.DefaultImpls.onBind(this, intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getData().getBinder().close();
    }

    @Override
    public void onRevoke() {
        BaseService.Interface.DefaultImpls.stopRunner$default(this, false, null, false, 7, null);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (zs4.h(DataStore.INSTANCE.getServiceMode(), Key.MODE_VPN)) {
            if (android.net.VpnService.prepare(this) == null) {
                return BaseService.Interface.DefaultImpls.onStartCommand(this, intent, flags, startId);
            }
            startActivity(new Intent(this, (Class<?>) zm2.M(Core.INSTANCE.getConfigureClass())).addFlags(268435456));
        }
        BaseService.Interface.DefaultImpls.stopRunner$default(this, false, null, false, 7, null);
        return 2;
    }

    public final void persistAppStats() {
        Tun2ray tun2ray;
        HashSet<String> hashSet;
        Iterator<String> it;
        String next;
        if (DataStore.INSTANCE.getAppTrafficStatistics() && (tun2ray = this.tun) != null) {
            this.appStats.clear();
            tun2ray.readAppTraffics(this);
            ArrayList arrayList = new ArrayList();
            List<StatsEntity> all = ShadowsocksDatabase.INSTANCE.getStatsDao().all();
            int J = an1.J(q00.A0(all, 10));
            if (J < 16) {
                J = 16;
            }
            LinkedHashMap linkedHashMap = new LinkedHashMap(J);
            for (Object obj : all) {
                linkedHashMap.put(((StatsEntity) obj).getPackageName(), obj);
            }
            for (AppStats appStats : this.appStats) {
                String str = (appStats.getUid() < 10000 || (hashSet = PackageCache.INSTANCE.getUidMap().get(Integer.valueOf(appStats.getUid()))) == null || (it = hashSet.iterator()) == null || (next = it.next()) == null) ? "android" : next;
                if (linkedHashMap.containsKey(str)) {
                    Object obj2 = linkedHashMap.get(str);
                    zs4.l(obj2);
                    StatsEntity statsEntity = (StatsEntity) obj2;
                    statsEntity.setTcpConnections(appStats.getTcpConnTotal() + statsEntity.getTcpConnections());
                    statsEntity.setUdpConnections(appStats.getUdpConnTotal() + statsEntity.getUdpConnections());
                    statsEntity.setUplink(appStats.getUplinkTotal() + statsEntity.getUplink());
                    statsEntity.setDownlink(appStats.getDownlinkTotal() + statsEntity.getDownlink());
                    arrayList.add(statsEntity);
                } else {
                    ShadowsocksDatabase.INSTANCE.getStatsDao().create(new StatsEntity(0, str, appStats.getTcpConnTotal(), appStats.getUdpConnTotal(), appStats.getUplinkTotal(), appStats.getDownlinkTotal(), 1, null));
                }
                if (!arrayList.isEmpty()) {
                    ShadowsocksDatabase.INSTANCE.getStatsDao().update(arrayList);
                }
            }
        }
    }

    @Override
    public void persistStats() {
        BaseService.Interface.DefaultImpls.persistStats(this);
    }

    @Override
    public Object preInit(Continuation<? super Unit> continuation) {
        VpnService$preInit$1 vpnService$preInit$1;
        int i;
        VpnService vpnService;
        if (continuation instanceof VpnService$preInit$1) {
            vpnService$preInit$1 = (VpnService$preInit$1) continuation;
            int i2 = vpnService$preInit$1.label;
            if ((i2 & Integer.MIN_VALUE) != 0) {
                vpnService$preInit$1.label = i2 - Integer.MIN_VALUE;
                Object obj = vpnService$preInit$1.result;
                m70 m70Var = m70.a;
                i = vpnService$preInit$1.label;
                if (i != 0) {
                    ResultKt.throwOnFailure(obj);
                    DefaultNetworkListener defaultNetworkListener = DefaultNetworkListener.INSTANCE;
                    VpnService$preInit$2 vpnService$preInit$2 = new VpnService$preInit$2(this);
                    vpnService$preInit$1.L$0 = this;
                    vpnService$preInit$1.label = 1;
                    if (defaultNetworkListener.start(this, vpnService$preInit$2, vpnService$preInit$1) == m70Var) {
                        return m70Var;
                    }
                    vpnService = this;
                } else {
                    if (i != 1) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    vpnService = (VpnService) vpnService$preInit$1.L$0;
                    ResultKt.throwOnFailure(obj);
                }
                Libcore.setLocalhostResolver(vpnService);
                return Unit.INSTANCE;
            }
        }
        vpnService$preInit$1 = new VpnService$preInit$1(this, continuation);
        Object obj2 = vpnService$preInit$1.result;
        m70 m70Var2 = m70.a;
        i = vpnService$preInit$1.label;
        if (i != 0) {
        }
        Libcore.setLocalhostResolver(vpnService);
        return Unit.INSTANCE;
    }

    @Override
    @RequiresApi(ConstraintLayout.LayoutParams.Table.LAYOUT_CONSTRAINT_HORIZONTAL_BIAS)
    public void queryRaw(QueryContext queryContext, byte[] bArr) {
        LocalResolver.DefaultImpls.queryRaw(this, queryContext, bArr);
    }

    public final void setConn(ParcelFileDescriptor parcelFileDescriptor) {
        zs4.o(parcelFileDescriptor, "<set-?>");
        this.conn = parcelFileDescriptor;
    }

    public final void setTun(Tun2ray tun2ray) {
        this.tun = tun2ray;
    }

    @Override
    public void setUnderlyingNetwork(Network network) {
        this.underlyingNetwork = network;
        if (this.active) {
            setUnderlyingNetworks(getUnderlyingNetworks());
        }
    }

    public final void setUpstreamInterfaceMTU(int i) {
        this.upstreamInterfaceMTU = i;
    }

    public final void setUpstreamInterfaceName(String str) {
        this.upstreamInterfaceName = str;
    }

    @Override
    public void setWakeLock(PowerManager.WakeLock wakeLock) {
        this.wakeLock = wakeLock;
    }

    @Override
    public Object startProcesses(Continuation<? super Unit> continuation) {
        VpnService$startProcesses$1 vpnService$startProcesses$1;
        int i;
        VpnService vpnService;
        if (continuation instanceof VpnService$startProcesses$1) {
            vpnService$startProcesses$1 = (VpnService$startProcesses$1) continuation;
            int i2 = vpnService$startProcesses$1.label;
            if ((i2 & Integer.MIN_VALUE) != 0) {
                vpnService$startProcesses$1.label = i2 - Integer.MIN_VALUE;
                Object obj = vpnService$startProcesses$1.result;
                m70 m70Var = m70.a;
                i = vpnService$startProcesses$1.label;
                if (i != 0) {
                    ResultKt.throwOnFailure(obj);
                    vpnService$startProcesses$1.L$0 = this;
                    vpnService$startProcesses$1.label = 1;
                    if (BaseService.Interface.DefaultImpls.startProcesses(this, vpnService$startProcesses$1) == m70Var) {
                        return m70Var;
                    }
                    vpnService = this;
                } else {
                    if (i != 1) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    vpnService = (VpnService) vpnService$startProcesses$1.L$0;
                    ResultKt.throwOnFailure(obj);
                }
                vpnService.startVpn();
                return Unit.INSTANCE;
            }
        }
        vpnService$startProcesses$1 = new VpnService$startProcesses$1(this, continuation);
        Object obj2 = vpnService$startProcesses$1.result;
        m70 m70Var2 = m70.a;
        i = vpnService$startProcesses$1.label;
        if (i != 0) {
        }
        vpnService.startVpn();
        return Unit.INSTANCE;
    }

    @Override
    public void startRunner() {
        BaseService.Interface.DefaultImpls.startRunner(this);
    }

    @Override
    public void stopRunner(boolean z, String str, boolean z2) {
        BaseService.Interface.DefaultImpls.stopRunner(this, z, str, z2);
    }

    @Override
    public void switchWakeLock() {
        BaseService.Interface.DefaultImpls.switchWakeLock(this);
    }

    @Override
    public void updateStats(AppStats stats) {
        if (stats != null) {
            this.appStats.add(stats);
        }
    }
}