package com.drops.poc;

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.drops.entity.ControllersFactory;
import com.drops.ui.MainController;
import com.drops.utils.HTTPUtils;
import com.drops.utils.PropertiesBean;
import com.drops.utils.URLUtil;
import com.drops.utils.Utils;

import java.util.Map;
import java.util.Scanner;

/**
 * @ClassName: SpringBootUtil
 * @Description: TODO
 * @Author: Summer
 * @Date: 2021/7/28 14:14
 * @Version: v1.0.0
 * @Description:
 **/
public class SpringBootUtil {
//    private static HTTPUtils httpUtils;
    static HTTPUtils httpUtils = new HTTPUtils();

    public MainController mainController ;
    public static boolean flag = false;
//    SpringBootInfoCheck infoCheck = new SpringBootInfoCheck();


    public SpringBootUtil() {
        this.mainController = (MainController) ControllersFactory.controllers.get(MainController.class.getSimpleName());
    }

    public boolean checkPoint(String target, String point){
        String url = URLUtil.normalizeURL(target) + point;
        System.out.println("正在检测：\t" + url);
        HttpResponse response = HTTPUtils.getRequest(url);
        if (response.getStatus() == 200){
            if (this.checkPoint(url, response)){
                this.mainController.logTextArea.appendText(Utils.log("检测到 " + point + " 端点存在，已做验证：" + url));
                flag = true;
            }else{
                this.mainController.logTextArea.appendText(Utils.log("检测到 "+ point +" 端点，请手工验证: "+ url));

            }
        }
        return flag;
    }
    // https://blog.csdn.net/testcs_dn/article/details/79033009/
    public  boolean checkPoint(String url, HttpResponse resp){
        String s = resp.body();
        // hystrix.stream
        if (url.contains("hystrix.stream")){
            return "ping:".contains(s)||"data:".contains(s);
            // health
        }else if (url.contains("health")){
            try {
                Map res = (Map) JSON.parse(s);
                return res.containsKey("status")||res.containsKey("diskSpace");
            }catch (Exception e){
                return false;
            }
            // beans
        }else if (url.contains("beans")){
            Map res;
            try {
                JSONArray objects = JSON.parseArray(s);
                for (Object o:objects){
                    res = (Map)o;
                    return res.containsKey("bean")||res.containsKey("scope")||res.containsKey("dependencies");
                }
            }catch (Exception e){
                return false;
            }
            // configprops
        }else if (url.contains("configprops")){
            try {
                Map res = (Map)JSON.parse(s);
                return res.containsKey("configurationPropertiesReportEndpoint");
            }catch (Exception e){
                return false;
            }
        }else if (url.contains("mappings")){
            return s.contains("bean")||s.contains("method");
        }else if (url.contains("metrics")){
            return s.contains("threads")||s.contains("heap");
        }
        return false;
    }

}
