TikTok.apk(点击下载) / HookWrapper.java


package com.swift.sandhook.wrapper;

import android.text.TextUtils;
import com.swift.sandhook.SandHook;
import com.swift.sandhook.SandHookConfig;
import com.swift.sandhook.annotation.HookClass;
import com.swift.sandhook.annotation.HookMethod;
import com.swift.sandhook.annotation.HookMethodBackup;
import com.swift.sandhook.annotation.HookObject;
import com.swift.sandhook.annotation.HookReflectClass;
import com.swift.sandhook.annotation.HookReflectObject;
import com.swift.sandhook.annotation.MethodAnyParams;
import com.swift.sandhook.annotation.MethodParams;
import com.swift.sandhook.annotation.MethodReflectParams;
import com.swift.sandhook.annotation.Param;
import com.swift.sandhook.annotation.SkipParamCheck;
import com.swift.sandhook.annotation.ThisObject;
import i.am.jaggu;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

public class HookWrapper {

    public static class HookEntity {
        public Method backup;
        public boolean backupIsStub = true;
        public Method hook;
        public boolean hookIsStub = false;
        public int hookMode;
        public boolean initClass = true;
        public Class[] pars;
        public boolean resolveDexCache = true;
        public Member target;

        public HookEntity(Member member) {
            this.target = member;
        }

        public HookEntity(Member member, Method method, Method method2) {
            this.target = member;
            this.hook = method;
            this.backup = method2;
        }

        public HookEntity(Member member, Method method, Method method2, boolean z) {
            this.target = member;
            this.hook = method;
            this.backup = method2;
            this.resolveDexCache = z;
        }

        public Object callOrigin(Object obj, Object... objArr) throws Throwable {
            return SandHook.callOriginMethod(this.backupIsStub, this.target, this.backup, obj, objArr);
        }

        public boolean isCtor() {
            return this.target instanceof Constructor;
        }
    }

    public static void addHookClass(ClassLoader classLoader, Class<?> cls) throws HookErrorException {
        Class targetHookClass = getTargetHookClass(classLoader, cls);
        if (targetHookClass != null) {
            Map<Member, HookEntity> hookMethods = getHookMethods(classLoader, targetHookClass, cls);
            try {
                fillBackupMethod(classLoader, cls, hookMethods);
                for (HookEntity hookEntity : hookMethods.values()) {
                    SandHook.hook(hookEntity);
                }
            } catch (Throwable th) {
                throw new HookErrorException(jaggu.base.decode("050F095A7505005B4D402F57150D5B02135113450E4445"), th);
            }
        } else {
            throw new HookErrorException(jaggu.base.decode("0614175945440B5F575B424513044416564641540D571716130A") + cls.getName());
        }
    }

    public static void addHookClass(ClassLoader classLoader, Class<?>... clsArr) throws HookErrorException {
        for (Class<?> cls : clsArr) {
            addHookClass(classLoader, cls);
        }
    }

    public static void addHookClass(Class<?>... clsArr) throws HookErrorException {
        addHookClass((ClassLoader) null, clsArr);
    }

    public static void checkSignature(Member member, Method method, Class[] clsArr) throws HookErrorException {
        Class<?> returnType;
        if (Modifier.isStatic(method.getModifiers())) {
            boolean z = member instanceof Constructor;
            String decode = jaggu.base.decode("06141759454411554C45105C41114D165615411A41");
            if (z) {
                if (!method.getReturnType().equals(Void.TYPE)) {
                    throw new HookErrorException(decode + method.getName());
                }
            } else if ((member instanceof Method) && (returnType = ((Method) member).getReturnType()) != method.getReturnType() && !returnType.isAssignableFrom(returnType)) {
                throw new HookErrorException(decode + method.getName());
            }
            Class<?>[] parameterTypes = method.getParameterTypes();
            if (parameterTypes == null) {
                parameterTypes = new Class[0];
            }
            if (clsArr == null) {
                clsArr = new Class[0];
            }
            if (!(clsArr.length == 0 && parameterTypes.length == 0)) {
                boolean isStatic = Modifier.isStatic(member.getModifiers());
                String decode2 = jaggu.base.decode("0B090A5D17090644505F061211044615135914441516090447530B12425B5C100C140C515E0A435D5D440A5D054414");
                int i2 = 1;
                if (!isStatic) {
                    int length = parameterTypes.length;
                    String decode3 = jaggu.base.decode("050F1745434413514A100F47121114045614155F08454545");
                    if (length == 0) {
                        throw new HookErrorException(decode3 + method.getName());
                    } else if (parameterTypes[0] != member.getDeclaringClass() && !parameterTypes[0].isAssignableFrom(member.getDeclaringClass())) {
                        throw new HookErrorException(decode3 + method.getName());
                    } else if (parameterTypes.length != clsArr.length + 1) {
                        throw new HookErrorException(decode2 + method.getName());
                    }
                } else if (parameterTypes.length == clsArr.length) {
                    i2 = 0;
                } else {
                    throw new HookErrorException(decode2 + method.getName());
                }
                for (int i3 = 0; i3 < clsArr.length; i3++) {
                    int i4 = i3 + i2;
                    if (parameterTypes[i4] != clsArr[i3] && !parameterTypes[i4].isAssignableFrom(clsArr[i3])) {
                        throw new HookErrorException(decode2 + method.getName());
                    }
                }
                return;
            }
            return;
        }
        throw new HookErrorException(jaggu.base.decode("0B090A5D17090644505F06120C10471213471556155F0744131D43") + method.getName());
    }

    /* JADX INFO: Can't fix incorrect switch cases order, some code will duplicate */
    private static Class classNameToClass(String str, ClassLoader classLoader) throws ClassNotFoundException {
        char c;
        switch (str.hashCode()) {
            case -1325958191:
                if (str.equals(jaggu.base.decode("070910545B01"))) {
                    c = 3;
                    break;
                }
                c = 65535;
                break;
            case 104431:
                if (str.equals(jaggu.base.decode("0A0811"))) {
                    c = 5;
                    break;
                }
                c = 65535;
                break;
            case 3039496:
                if (str.equals(jaggu.base.decode("011F1153"))) {
                    c = 1;
                    break;
                }
                c = 65535;
                break;
            case 3052374:
                if (str.equals(jaggu.base.decode("000E0444"))) {
                    c = 2;
                    break;
                }
                c = 65535;
                break;
            case 3327612:
                if (str.equals(jaggu.base.decode("0F090B51"))) {
                    c = 6;
                    break;
                }
                c = 65535;
                break;
            case 64711720:
                if (str.equals(jaggu.base.decode("01090A5A52050D"))) {
                    c = 0;
                    break;
                }
                c = 65535;
                break;
            case 97526364:
                if (str.equals(jaggu.base.decode("050A0A5743"))) {
                    c = 4;
                    break;
                }
                c = 65535;
                break;
            case 109413500:
                if (str.equals(jaggu.base.decode("100E0A4443"))) {
                    c = 7;
                    break;
                }
                c = 65535;
                break;
            default:
                c = 65535;
                break;
        }
        switch (c) {
            case 0:
                return Boolean.TYPE;
            case 1:
                return Byte.TYPE;
            case 2:
                return Character.TYPE;
            case 3:
                return Double.TYPE;
            case 4:
                return Float.TYPE;
            case 5:
                return Integer.TYPE;
            case 6:
                return Long.TYPE;
            case 7:
                return Short.TYPE;
            default:
                if (str.startsWith(jaggu.base.decode("4D"))) {
                    str = SandHookConfig.SELF_PACKAGE_NAME + str;
                }
                return classLoader == null ? Class.forName(str) : Class.forName(str, true, classLoader);
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:38:0x0089  */
    /* JADX WARNING: Removed duplicated region for block: B:39:0x0090  */
    /* JADX WARNING: Removed duplicated region for block: B:42:0x009c  */
    /* JADX WARNING: Removed duplicated region for block: B:60:0x00e6  */
    private static void fillBackupMethod(ClassLoader classLoader, Class<?> cls, Map<Member, HookEntity> map) {
        Field[] fieldArr;
        HookMethodBackup hookMethodBackup;
        Exception e;
        try {
            fieldArr = cls.getDeclaredFields();
        } catch (Throwable unused) {
            fieldArr = null;
        }
        if (!(fieldArr == null || fieldArr.length == 0 || map.isEmpty())) {
            for (Field field : fieldArr) {
                if (Modifier.isStatic(field.getModifiers()) && (hookMethodBackup = (HookMethodBackup) field.getAnnotation(HookMethodBackup.class)) != null) {
                    for (HookEntity hookEntity : map.values()) {
                        String value = hookMethodBackup.value();
                        if (TextUtils.isEmpty(value)) {
                            try {
                                try {
                                    value = (String) cls.getMethod(jaggu.base.decode("0403117E580B087D5D440A5D0527550558411179005B01"), Method.class).invoke(null, hookEntity.hook);
                                } catch (Exception e2) {
                                    e = e2;
                                    e.printStackTrace();
                                    if (TextUtils.equals(hookEntity.isCtor() ? jaggu.base.decode("5F0F0B5F435A") : hookEntity.target.getName(), value)) {
                                    }
                                }
                            } catch (Exception e3) {
                                e = e3;
                                e.printStackTrace();
                                if (TextUtils.equals(hookEntity.isCtor() ? jaggu.base.decode("5F0F0B5F435A") : hookEntity.target.getName(), value)) {
                                }
                            }
                        }
                        if (TextUtils.equals(hookEntity.isCtor() ? jaggu.base.decode("5F0F0B5F435A") : hookEntity.target.getName(), value)) {
                            if (samePars(classLoader, field, hookEntity.pars) || field.getAnnotation(MethodAnyParams.class) != null) {
                                field.setAccessible(true);
                                if (hookEntity.backup == null) {
                                    hookEntity.backup = StubMethodsFactory.getStubMethod();
                                    hookEntity.hookIsStub = true;
                                    hookEntity.resolveDexCache = false;
                                }
                                if (hookEntity.backup != null) {
                                    try {
                                        if (field.getType() == Method.class) {
                                            field.set(null, hookEntity.backup);
                                        } else if (field.getType() == HookEntity.class) {
                                            field.set(null, hookEntity);
                                        }
                                    } catch (IllegalAccessException e4) {
                                        e4.printStackTrace();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public static Map<Member, HookEntity> getHookMethods(ClassLoader classLoader, Class cls, Class<?> cls2) throws HookErrorException {
        Method[] methodArr;
        Member member;
        Method method;
        Class<?>[] clsArr;
        Method method2;
        Method method3;
        Exception e;
        HashMap hashMap = new HashMap();
        Object obj = null;
        try {
            methodArr = cls2.getDeclaredMethods();
        } catch (Throwable unused) {
            methodArr = null;
        }
        if (methodArr == null || methodArr.length == 0) {
            throw new HookErrorException(jaggu.base.decode("0614175945440B5F575B424513044416564641540D571716130A") + cls.getName());
        }
        int length = methodArr.length;
        char c = 0;
        int i2 = 0;
        while (i2 < length) {
            Method method4 = methodArr[i2];
            HookMethod hookMethod = (HookMethod) method4.getAnnotation(HookMethod.class);
            HookMethodBackup hookMethodBackup = (HookMethodBackup) method4.getAnnotation(HookMethodBackup.class);
            String decode = jaggu.base.decode("5F0F0B5F435A");
            if (hookMethod != null) {
                String value = hookMethod.value();
                if (value.isEmpty()) {
                    try {
                        String decode2 = jaggu.base.decode("0403117E580B087D5D440A5D05");
                        Class<?>[] clsArr2 = new Class[1];
                        clsArr2[c] = Method.class;
                        try {
                            Method method5 = cls2.getMethod(decode2, clsArr2);
                            Object[] objArr = new Object[1];
                            objArr[c] = method4;
                            method2 = (Method) method5.invoke(obj, objArr);
                            if (method2 != null) {
                                clsArr = method2.getParameterTypes();
                            }
                        } catch (Exception e2) {
                            e = e2;
                            e.printStackTrace();
                            i2++;
                            obj = null;
                            c = 0;
                        }
                    } catch (Exception e3) {
                        e = e3;
                        e.printStackTrace();
                        i2++;
                        obj = null;
                        c = 0;
                    }
                } else {
                    clsArr = parseMethodPars(classLoader, method4);
                    try {
                        if (value.equals(decode)) {
                            try {
                                method2 = cls.getConstructor(clsArr);
                            } catch (NoSuchMethodException unused2) {
                                method2 = cls.getDeclaredConstructor(clsArr);
                            }
                        } else {
                            try {
                                method2 = cls.getMethod(value, clsArr);
                            } catch (NoSuchMethodException unused3) {
                                method2 = cls.getDeclaredMethod(value, clsArr);
                            }
                        }
                    } catch (NoSuchMethodException unused4) {
                        if (method4.getAnnotation(MethodAnyParams.class) != null) {
                            System.out.println(jaggu.base.decode("17141C5F590343445710045B0F011412524606521516090047580C56165140100D0708530D44") + value);
                            Method[] methods = cls.getMethods();
                            int length2 = methods.length;
                            int i3 = 0;
                            while (true) {
                                if (i3 >= length2) {
                                    method3 = null;
                                    break;
                                }
                                method3 = methods[i3];
                                if (value.equals(method3.getName())) {
                                    clsArr = method3.getParameterTypes();
                                    break;
                                }
                                i3++;
                            }
                            if (method3 == null) {
                                Method[] declaredMethods = cls.getDeclaredMethods();
                                int length3 = declaredMethods.length;
                                int i4 = 0;
                                while (true) {
                                    if (i4 >= length3) {
                                        break;
                                    }
                                    Method method6 = declaredMethods[i4];
                                    if (value.equals(method6.getName())) {
                                        clsArr = method6.getParameterTypes();
                                        method3 = method6;
                                        break;
                                    }
                                    i4++;
                                }
                            }
                        } else {
                            method3 = null;
                        }
                        if (method3 == null) {
                            System.err.println(jaggu.base.decode("00070B16590B17105E590C5641115514545115170C53100D5C545912") + value);
                        } else {
                            method2 = method3;
                        }
                    }
                    if (!method4.isAnnotationPresent(SkipParamCheck.class)) {
                        checkSignature(method2, method4, clsArr);
                    }
                }
                HookEntity hookEntity = (HookEntity) hashMap.get(method2);
                if (hookEntity == null) {
                    hookEntity = new HookEntity(method2);
                    hashMap.put(method2, hookEntity);
                }
                hookEntity.pars = clsArr;
                hookEntity.hook = method4;
            } else if (hookMethodBackup != null) {
                String value2 = hookMethodBackup.value();
                Class<?>[] parseMethodPars = parseMethodPars(classLoader, method4);
                try {
                    if (value2.equals(decode)) {
                        try {
                            member = cls.getConstructor(parseMethodPars);
                        } catch (NoSuchMethodException unused5) {
                            member = cls.getDeclaredConstructor(parseMethodPars);
                        }
                    } else {
                        try {
                            member = cls.getMethod(value2, parseMethodPars);
                        } catch (NoSuchMethodException unused6) {
                            member = cls.getDeclaredMethod(value2, parseMethodPars);
                        }
                    }
                } catch (NoSuchMethodException unused7) {
                    if (method4.getAnnotation(MethodAnyParams.class) != null) {
                        System.out.println(jaggu.base.decode("17141C5F590343445710045B0F0114125246065215160604505B1642165E5C440B090116551D435E595D070841") + value2);
                        Method[] methods2 = cls.getMethods();
                        int length4 = methods2.length;
                        int i5 = 0;
                        while (true) {
                            if (i5 >= length4) {
                                method = null;
                                break;
                            }
                            method = methods2[i5];
                            if (value2.equals(method.getName())) {
                                parseMethodPars = method.getParameterTypes();
                                break;
                            }
                            i5++;
                        }
                        if (method == null) {
                            Method[] declaredMethods2 = cls.getDeclaredMethods();
                            int length5 = declaredMethods2.length;
                            int i6 = 0;
                            while (true) {
                                if (i6 >= length5) {
                                    break;
                                }
                                Method method7 = declaredMethods2[i6];
                                if (value2.equals(method7.getName())) {
                                    parseMethodPars = method7.getParameterTypes();
                                    method = method7;
                                    break;
                                }
                                i6++;
                            }
                        }
                    } else {
                        method = null;
                    }
                    if (method == null) {
                        System.err.println(jaggu.base.decode("00070B16590B17105E590C5641115514545115170357070E4640435F5347515F075C45") + value2);
                    } else {
                        member = method;
                    }
                }
                if (!method4.isAnnotationPresent(SkipParamCheck.class)) {
                    checkSignature(member, method4, parseMethodPars);
                }
                HookEntity hookEntity2 = (HookEntity) hashMap.get(member);
                if (hookEntity2 == null) {
                    hookEntity2 = new HookEntity(member);
                    hashMap.put(member, hookEntity2);
                }
                hookEntity2.pars = parseMethodPars;
                hookEntity2.backup = method4;
            }
            i2++;
            obj = null;
            c = 0;
        }
        return hashMap;
    }

    private static int getParsCount(Method method) {
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes == null) {
            return 0;
        }
        return parameterTypes.length;
    }

    private static Class getRealParType(ClassLoader classLoader, Class cls, Annotation[] annotationArr, boolean z) throws Exception {
        if (!(annotationArr == null || annotationArr.length == 0)) {
            for (Annotation annotation : annotationArr) {
                if (annotation instanceof Param) {
                    Param param = (Param) annotation;
                    if (TextUtils.isEmpty(param.value())) {
                        return cls;
                    }
                    Class<?> classNameToClass = classNameToClass(param.value(), classLoader);
                    if (z || classNameToClass.equals(cls) || cls.isAssignableFrom(classNameToClass)) {
                        return classNameToClass;
                    }
                    throw new ClassCastException(jaggu.base.decode("0B090A5D17090644505F06121104464650551243415316175C4242"));
                }
            }
        }
        return cls;
    }

    public static Class getTargetHookClass(ClassLoader classLoader, Class<?> cls) {
        HookClass hookClass = (HookClass) cls.getAnnotation(HookClass.class);
        if (hookClass != null) {
            return hookClass.value();
        }
        HookObject hookObject = (HookObject) cls.getAnnotation(HookObject.class);
        if (hookObject != null) {
            return hookObject.value();
        }
        HookReflectClass hookReflectClass = (HookReflectClass) cls.getAnnotation(HookReflectClass.class);
        if (hookReflectClass != null) {
            return getTargetHookClass(classLoader, cls, hookReflectClass.value());
        }
        HookReflectObject hookReflectObject = (HookReflectObject) cls.getAnnotation(HookReflectObject.class);
        if (hookReflectObject != null) {
            return getTargetHookClass(classLoader, cls, hookReflectObject.value());
        }
        return null;
    }

    private static Class getTargetHookClass(ClassLoader classLoader, Class<?> cls, String str) {
        try {
            if (str.length() == 0) {
                try {
                    return (Class) cls.getMethod(jaggu.base.decode("0403117E580B087354511141"), new Class[0]).invoke(null, new Object[0]);
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            } else {
                if (str.startsWith(jaggu.base.decode("4D"))) {
                    str = SandHookConfig.SELF_PACKAGE_NAME + str;
                }
                return classLoader == null ? Class.forName(str) : Class.forName(str, true, classLoader);
            }
        } catch (ClassNotFoundException e2) {
            System.err.println(e2);
            return null;
        }
    }

    private static boolean hasThisObject(Method method) {
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        if (parameterAnnotations == null || parameterAnnotations.length == 0) {
            return false;
        }
        return isThisObject(parameterAnnotations[0]);
    }

    private static boolean isThisObject(Annotation[] annotationArr) {
        if (!(annotationArr == null || annotationArr.length == 0)) {
            for (Annotation annotation : annotationArr) {
                if (annotation instanceof ThisObject) {
                    return true;
                }
            }
        }
        return false;
    }

    private static Class[] parseMethodPars(ClassLoader classLoader, Field field) throws HookErrorException {
        MethodParams methodParams = (MethodParams) field.getAnnotation(MethodParams.class);
        MethodReflectParams methodReflectParams = (MethodReflectParams) field.getAnnotation(MethodReflectParams.class);
        if (methodParams != null) {
            return methodParams.value();
        }
        Class[] clsArr = null;
        if (methodReflectParams != null) {
            if (methodReflectParams.value().length == 0) {
                return null;
            }
            clsArr = new Class[methodReflectParams.value().length];
            for (int i2 = 0; i2 < methodReflectParams.value().length; i2++) {
                try {
                    clsArr[i2] = classNameToClass(methodReflectParams.value()[i2], classLoader);
                } catch (ClassNotFoundException e) {
                    throw new HookErrorException(jaggu.base.decode("0B090A5D17090644505F061211044615135113450E445E45") + field.getName(), e);
                }
            }
        }
        return clsArr;
    }

    private static Class[] parseMethodPars(ClassLoader classLoader, Method method) throws HookErrorException {
        MethodParams methodParams = (MethodParams) method.getAnnotation(MethodParams.class);
        MethodReflectParams methodReflectParams = (MethodReflectParams) method.getAnnotation(MethodReflectParams.class);
        if (methodParams != null) {
            return methodParams.value();
        }
        if (methodReflectParams != null) {
            if (methodReflectParams.value().length == 0) {
                return null;
            }
            Class[] clsArr = new Class[methodReflectParams.value().length];
            for (int i2 = 0; i2 < methodReflectParams.value().length; i2++) {
                try {
                    clsArr[i2] = classNameToClass(methodReflectParams.value()[i2], classLoader);
                } catch (ClassNotFoundException e) {
                    throw new HookErrorException(jaggu.base.decode("0B090A5D17090644505F061211044615135113450E445E45") + method.getName(), e);
                }
            }
            return clsArr;
        } else if (getParsCount(method) <= 0) {
            return null;
        } else {
            if (getParsCount(method) != 1) {
                return parseMethodParsNew(classLoader, method);
            }
            if (hasThisObject(method)) {
                return parseMethodParsNew(classLoader, method);
            }
            return null;
        }
    }

    private static Class[] parseMethodParsNew(ClassLoader classLoader, Method method) throws HookErrorException {
        Class<?>[] parameterTypes = method.getParameterTypes();
        Class[] clsArr = null;
        if (!(parameterTypes == null || parameterTypes.length == 0)) {
            Annotation[][] parameterAnnotations = method.getParameterAnnotations();
            int i2 = 0;
            for (int i3 = 0; i3 < parameterAnnotations.length; i3++) {
                Class<?> cls = parameterTypes[i3];
                Annotation[] annotationArr = parameterAnnotations[i3];
                if (i3 == 0) {
                    if (isThisObject(annotationArr)) {
                        clsArr = new Class[(parameterAnnotations.length - 1)];
                    } else {
                        clsArr = new Class[parameterAnnotations.length];
                    }
                }
                try {
                    clsArr[i2] = getRealParType(classLoader, cls, annotationArr, method.isAnnotationPresent(SkipParamCheck.class));
                    i2++;
                } catch (Exception e) {
                    throw new HookErrorException(jaggu.base.decode("0B090A5D17090644505F06125D") + method.getName() + jaggu.base.decode("5D461557451706421840034012455114415B13"), e);
                }
            }
        }
        return clsArr;
    }

    private static boolean samePars(ClassLoader classLoader, Field field, Class[] clsArr) {
        try {
            Class[] parseMethodPars = parseMethodPars(classLoader, field);
            if (parseMethodPars == null && field.isAnnotationPresent(SkipParamCheck.class)) {
                return true;
            }
            if (clsArr == null) {
                clsArr = new Class[0];
            }
            if (parseMethodPars == null) {
                parseMethodPars = new Class[0];
            }
            if (clsArr.length != parseMethodPars.length) {
                return false;
            }
            for (int i2 = 0; i2 < clsArr.length; i2++) {
                if (clsArr[i2] != parseMethodPars[i2]) {
                    return false;
                }
            }
            return true;
        } catch (HookErrorException unused) {
            return false;
        }
    }
}