package top.octopusyan.utils;

import top.octopusyan.config.ProxyConfig;
import top.octopusyan.http.request.ProxySetup;
import top.octopusyan.model.ProxySetupModel;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author : octopus yan
 * @email : octopus_yan@foxmail.com
 * @description :
 * @create : 2022-4-6 20:57
 */
public class DomainUtil {

    public static String getCustomize(ProxySetup setup) {
        String domain = setup.getDomain();
        String serverPath = getServerPath(setup);
        int end = domain.indexOf("." + serverPath);
        if (domain.length() <= 0 || end == -1) return "";
        return domain.substring(0, end);
    }

    public static String getSuffix(ProxySetup setup) {
        String serverPath = getServerPath(setup);
        return "." + serverPath;
    }

    public static boolean isHttp(ProxySetupModel model){
        return ProxyConfig.getTypeIndex(model.getProxyType()) < 2;
    }

    /**
     * 是否自定义隧道
     */
    public static boolean isCustomize(ProxySetup setup) {
        return !setup.getDomain().contains(getServerPath(setup));
    }

    private static String getServerPath(ProxySetup setup) {
        return ProxyConfig.getServerPath(ProxyConfig.getServerName(setup.getNode()));
    }

    public static String getDomainAddress(String domain) {
        try {
            String hostAddress = InetAddress.getByName(domain).getHostAddress();
            return hostAddress;
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return null;
        }
    }
}
