package com.tsi.api.server.util;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ApiUtils {

    private ApiUtils() {}

    public static String getRemoteIP(HttpServletRequest request) {
        if (request == null) {
            return "";
        }

        String ipAddr = request.getHeader("X-FORWARDED-FOR");

        // proxy 환경일 경우
        if (ipAddr == null || ipAddr.length() == 0) {
            ipAddr = request.getHeader("Proxy-Client-IP");
        }

        // 웹로직 서버일 경우
        if (ipAddr == null || ipAddr.length() == 0) {
            ipAddr = request.getHeader("WL-Proxy-Client-IP");
        }

        // 기타
        if (ipAddr == null || ipAddr.length() == 0) {
            ipAddr = request.getRemoteAddr() ;
        }

        //-Djava.net.preferIPv4Stack=true
        if (ipAddr.equals("0:0:0:0:0:0:0:1"))   //==> ipv6 <== default
        {
            ipAddr = "127.0.0.1";   //==> localhost
        }
        return ipAddr;
    }

    public static String now() {
        Date today = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(today);
    }
}
