content: package top.niunaijun.blackbox.fake.service;
  
  import java.lang.reflect.Method;
  import java.util.List;
  
  import black.com.android.internal.net.BRVpnConfig;
  import black.com.android.internal.net.VpnConfigContext;
  import top.niunaijun.blackbox.BlackBoxCore;
  import top.niunaijun.blackbox.app.BActivityThread;
  import top.niunaijun.blackbox.fake.hook.MethodHook;
  import top.niunaijun.blackbox.fake.hook.ProxyMethod;
  import top.niunaijun.blackbox.proxy.ProxyVpnService;
  import top.niunaijun.blackbox.utils.MethodParameterUtils;
  
  /**
   * Created by BlackBox on 2022/2/26.
   */
  public class VpnCommonProxy {
      @ProxyMethod("setVpnPackageAuthorization")
      public static class setVpnPackageAuthorization extends MethodHook {
  
          @Override
          protected Object hook(Object who, Method method, Object[] args) throws Throwable {
              MethodParameterUtils.replaceFirstAppPkg(args);
              return method.invoke(who, args);
          }
      }
  
      @ProxyMethod("prepareVpn")
      public static class PrepareVpn extends MethodHook {
  
          @Override
          protected Object hook(Object who, Method method, Object[] args) throws Throwable {
              MethodParameterUtils.replaceFirstAppPkg(args);
              return method.invoke(who, args);
          }
      }
  
      @ProxyMethod("establishVpn")
      public static class establishVpn extends MethodHook {
  
          @Override
          protected Object hook(Object who, Method method, Object[] args) throws Throwable {
              VpnConfigContext vpnConfigContext = BRVpnConfig.get(args[0]);
              vpnConfigContext._set_user(ProxyVpnService.class.getName());
  
              handlePackage(vpnConfigContext.allowedApplications());
              handlePackage(vpnConfigContext.disallowedApplications());
              return method.invoke(who, args);
          }
  
          private void handlePackage(List<String> applications) {
              if (applications == null)
                  return;
              if (applications.contains(BActivityThread.getAppPackageName())) {
                  applications.add(BlackBoxCore.getHostPkg());
              }
          }
      }
  
  }
  