1. 程式人生 > 其它 >類載入器隔離樸實案例(三)logback實戰加密

類載入器隔離樸實案例(三)logback實戰加密

背景:

公司框架日誌包經常衝突太亂了,經常打不出來,擬搞一套私有的日誌

框架 tomcat appclassloader

package com.test.privatelogclass;

/**
 * Created by joyce on 2022/5/19.
 */
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;


public class PrivateLogClassLoader extends ClassLoader {

    private static final String ALGORITHM = "DESede";
    private static final byte [] key = "privatelogprivatelogpriv".getBytes();

    JarInputStream[] list = null;
    private HashMap<String, byte[]> classes = new HashMap<>();

    private static volatile PrivateLogClassLoader privateLogClassLoader;

    private static final String [] JAR_URLS = {"api", "classic", "core"};

    static {
        try {
            JarInputStream [] inputStreams = new JarInputStream[JAR_URLS.length];
            for(int i=0; i<JAR_URLS.length; ++i) {
                InputStream inputStream = PrivateLogClassLoader.class.getClassLoader().getResourceAsStream("privatelog/"+JAR_URLS[i]);
                byte [] bytes = toByteArray(inputStream);
                inputStreams[i] = new JarInputStream(new ByteArrayInputStream(decrypt(key, bytes)));
            }
            privateLogClassLoader = new PrivateLogClassLoader(inputStreams);
        } catch (Exception e) {
            printError(e);
        }
    }

    public static PrivateLogClassLoader getPrivateLogClassLoader() {
        return privateLogClassLoader;
    }

    private PrivateLogClassLoader(JarInputStream [] jarInputStream) {
        this.list = jarInputStream;

        for(JarInputStream jar : list) {
            JarEntry entry;
            try {
                while ((entry = jar.getNextJarEntry()) != null) {
                    String name = entry.getName();
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    int len = -1;
                    byte [] tmp = new byte[1024];
                    while ((len = jar.read(tmp)) != -1) {
                        out.write(tmp, 0, len);
                    }
                    byte[] bytes = out.toByteArray();
                    classes.put(name, bytes);
                }
            } catch (Exception e) {
                printError(e);
            }
        }
        printInfo("total classes - " + classes.size());

    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        printInfo("start to find class " + name);
        try {
            InputStream in = getResourceAsStream(name.replace('.', '/') + ".class");
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int len = -1;
            byte[] tmp = new byte[1024];
            while ((len = in.read(tmp)) != -1) {
                out.write(tmp, 0, len);
            }
            byte[] bytes = out.toByteArray();
            return defineClass(name, bytes, 0, bytes.length);
        } catch (Exception e) {
            printError(e);
        }

        return super.findClass(name);
    }

    @Override
    public InputStream getResourceAsStream(String name) {
        printInfo("start to find resource stream " + name);
        if(classes.containsKey(name)) {
            byte [] res = classes.get(name);
            classes.remove(name);
            return new ByteArrayInputStream(res);
        }
        printInfo("getResourceAsStream - error - " + name);
        return super.getResourceAsStream(name);
    }

    @Override
    public URL getResource(String name) {
        printInfo("start to find resource file " + name);
        if(name.equals("logback.xml")) {
            return this.getClass().getClassLoader().getResource("privatelog/privatelog.xml");
        }
        return null;
    }

    private static void printInfo(String msg) {
        System.out.println(msg);
    }

    private static void printError(Exception e) {
        e.printStackTrace();
    }

    private static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024*4];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }

    private static byte[] decrypt(byte[] key, byte[] src) {
        byte[] value = null;
        SecretKey deskey = new SecretKeySpec(key, ALGORITHM);
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, deskey);
            value = cipher.doFinal(src);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
}
package com.test.privatelogclass;

import java.lang.reflect.Method;

/**
 * Created by joyce on 2020/3/28.
 */
public class PrivateLogFactory {
    public static PrivateLogger getLogger(Class c) {
        try {
            Class cl = PrivateLogClassLoader.getPrivateLogClassLoader().loadClass("org.slf4j.LoggerFactory");
            Method method = cl.getMethod("getLogger", Class.class);
            Object log = method.invoke(null, c);
            PrivateLogger privateLogger = new PrivateLogger();
            privateLogger.setLogger(log);
            return privateLogger;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static class PrivateLogger {

        public void setLogger(Object logger) {
            this.logger = logger;
        }

        Object logger;

        public void error(String var1, Throwable var2) {
            try {
                Class logcl = PrivateLogClassLoader.getPrivateLogClassLoader().loadClass("org.slf4j.Logger");
                Method info = logcl.getMethod("error", String.class, Throwable.class);
                info.invoke(this.logger, var1, var2);
            } catch (Exception e) {
                throw new RuntimeException(e) ;
            }
        }

        public void error(Throwable var1) {
            try {
                Class logcl = PrivateLogClassLoader.getPrivateLogClassLoader().loadClass("org.slf4j.Logger");
                Method info = logcl.getMethod("error", String.class, Throwable.class);
                info.invoke(this.logger, var1.getMessage(), var1);
            } catch (Exception e) {
                throw new RuntimeException(e) ;
            }
        }

        public void info(String var1, Object... var2) {
            try {
                Class logcl = PrivateLogClassLoader.getPrivateLogClassLoader().loadClass("org.slf4j.Logger");
                Method info = logcl.getMethod("info", String.class, Object[].class);
                info.invoke(this.logger, var1, var2);
            } catch (Exception e) {
                throw new RuntimeException(e) ;
            }
        }
    }
}
package com.test.privatelogclass;

/**
 * Created by joyce on 2022/5/19.
 */
public class TestMain {
    public static void main(String []f) {
        PrivateLogFactory.PrivateLogger logger = PrivateLogFactory.getLogger(TestMain.class);
        logger.error("eeexxx", new Exception("ex22"));
        logger.error(new Exception("ex33333"));
        logger.info("infoinfo");
        logger.info("infoinfo {}", 11);
        logger.info("infoinfo {} {}", 21, 31);
    }
}
package com.test.privatelogclass;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;

/**
 * Created by joyce on 2022/5/19.
 */
public class Encoder {

    private static final String ALGORITHM = "DESede";
    private static final byte [] key = "privatelogprivatelogpriv".getBytes();

    public static void main(String []f) throws Exception {
        InputStream inputStream = Encoder.class.getClassLoader().getResourceAsStream("privatelog/core-1.2.3.jar");
        byte [] bytes = toByteArray(inputStream);
        File ff = new File("/Users/joyce/work/MyTest/MyMock/src/main/resources/privatelog/core");
        FileOutputStream fop = new FileOutputStream(ff);
        fop.write(encrypt(key, bytes));
        fop.close();
    }

    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024*4];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }

    public static byte[] encrypt(byte[] key, byte[] src) {
        byte[] value = null;
        SecretKey deskey = new SecretKeySpec(key, ALGORITHM);
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, deskey);
            value = cipher.doFinal(src);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

}