package com.drops.poc;

import com.drops.entity.ControllersFactory;
import com.drops.ui.MainController;
import com.drops.utils.PropertiesBean;
import com.drops.utils.URLUtil;
import com.drops.utils.Utils;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: SpringBootInfo
 * @Description: TODO
 * @Author: Summer
 * @Date: 2021/7/27 16:58
 * @Version: v1.0.0
 * @Description: Spring Boot Info
 **/
public class SpringBootInfo {
    List<String> pointListV1 = new ArrayList<>();
    List<String> pointListV2 = new ArrayList<>();
    static boolean SpringbootVersionV1 = false;
    String[] basicPoint = new String[]{"cloudfoundryapplication","hystrix.stream" };

    MainController mainController;
    SpringBootInfoCheck infoCheck ;
    public List<String> gadget = new ArrayList<>();
    List<String> gadgetExp = new ArrayList<>();

//    public SpringBootInfo(List<String> pointListV1, List<String> pointListV2, String[] basicPoint, PropertiesBean properties, MainController mainController) {
//        this.pointListV1 = pointListV1;
//        this.pointListV2 = pointListV2;
//        this.basicPoint = basicPoint;
//        this.properties = properties;
//        this.mainController = (MainController) ControllersFactory.controllers.get(MainController.class.getSimpleName());
//    }

//    public SpringBootInfo() {
//    }

    public SpringBootInfo(){
        this.mainController = (MainController) ControllersFactory.controllers.get(MainController.class.getSimpleName());
        this.infoCheck = new SpringBootInfoCheck();

//        pointListV1.add("autoconfig");
//        pointListV1.add("heapdump");
//        pointListV1.add("dump");
//        pointListV1.add("mappings");
        pointListV1.add("auditevents");
        pointListV1.add("beans");
//        pointListV1.add("health");
        pointListV1.add("configprops");
        pointListV1.add("info");
        pointListV1.add("loggers");
//        pointListV1.add("threaddump");
        pointListV1.add("metrics");
        pointListV1.add("trace");
        pointListV1.add("env/spring.jmx.enabled");
        pointListV1.add("refresh");
//        pointListV1.add("trace");
        pointListV1.add("jolokia");
        pointListV1.add("env");
        pointListV1.add("restart");



        pointListV2.add("actuator/auditevents");
        pointListV2.add("actuator/env");
        pointListV2.add("actuator/restart");
        pointListV2.add("actuator/refresh");
        pointListV2.add("actuator/beans");
//        pointListV2.add("actuator/health");
        pointListV2.add("actuator/conditions");
        pointListV2.add("actuator/configprops");
        pointListV2.add("actuator/info");
        pointListV2.add("actuator/loggers");
//        pointListV2.add("actuator/httptrace");
//        pointListV2.add("actuator/threaddump");
        pointListV2.add("actuator/metrics");
//        pointListV2.add("actuator/httptrace");
//        pointListV2.add("actuator/mappings");
        pointListV2.add("actuator/jolokia");
        pointListV2.add("actuator/jolokia/list");
//        pointListV2.add("actuator/hystrix.stream");
        pointListV2.add("actuator/env/spring.jmx.enabled");

        pointListV2.add("monitor/auditevents");
        pointListV2.add("monitor/beans");
        pointListV2.add("monitor/conditions");
        pointListV2.add("monitor/configprops");
        pointListV2.add("monitor/env");
        pointListV2.add("monitor/info");
        pointListV2.add("monitor/loggers");
//        pointListV2.add("monitor/heapdump");
//        pointListV2.add("monitor/threaddump");
        pointListV2.add("monitor/metrics");
        pointListV2.add("monitor/scheduledtasks");
//        pointListV2.add("monitor/httptrace");
//        pointListV2.add("monitor/mappings");
        pointListV2.add("monitor/jolokia");
//        pointListV2.add("monitor/hystrix.stream");

        gadget.add("SnakeYAMLRCEPOC");


        gadgetExp.add("SnakeYAMLRCEEXP");

    }



    public boolean doCheck(String target) throws MalformedURLException {
        String rootaddr = URLUtil.normalizeURL(target);
        try {
            if (this.infoCheck.CheckPointInfo(rootaddr)){
                this.mainController.logTextArea.appendText(Utils.log("检测到 Spring Boot 404 特征！"));
            }
            if (this.infoCheck.check404(rootaddr)){
                this.mainController.logTextArea.appendText(Utils.log("默认404页面返回200，无法准确爆破！"));
            }
        }catch (Exception e){
            this.mainController.logTextArea.appendText(Utils.log(e.getMessage()));
        }
        try {
            this.infoCheck.checkActuatorPointV1(rootaddr);
            this.infoCheck.checkEnvPointV1(rootaddr);

            if (!SpringbootVersionV1){
                this.infoCheck.checkActuatorPointV2(rootaddr);
                this.infoCheck.checkEnvPointV2(rootaddr);
            }
            this.mainController.logTextArea.appendText(Utils.log("检测流程结束! "));
        }catch (Exception e){
            this.mainController.logTextArea.appendText(Utils.log("遇到异常退出！"));
            this.mainController.logTextArea.appendText(Utils.log(e.getMessage()));
        }
        return true;
    }




    void parseProperties(PropertiesBean properties){
        if (properties.getHaveInfo()){
//            this.mainController.logTextArea.appendText();
//            infoCheck.
            this.mainController.logTextArea.appendText(Utils.log("\tJVM信息:\t\t"+properties.getJvmName()));
            this.mainController.logTextArea.appendText(Utils.log("\t端口信息:\t\t"+properties.getServerPort()));
            this.mainController.logTextArea.appendText(Utils.log("\tJava版本:\t\t"+properties.getJavaVersion()));
            this.mainController.logTextArea.appendText(Utils.log("\t用户名:\t\t"+properties.getUserName()));
        }
    }


}
