調用shell腳本,IP處理

//調用shell腳本,IP處理

package com.letv.sdns.web.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 */
@Service
public class DnsApiUtils {
    public static final String ASIA = "亞洲";
    public static final String EUROPE = "歐洲";
    public static final String SOUTHAMERICA = "南美洲";
    public static final String NOTHAMERICA = "北美洲";
    public static final String AFRICA = "非洲";
    public static final String OCEANIA = "大洋洲";
    public static final String ANTRACTICA = "南極洲";
    public static final String VIEW_ALL = "ALL";
    public static final String VIEW_ALL_NAME = "全部";
    public static final String VIEW_CN = "CN";
    public static final String VIEW_CN_NAME = "中國";
    public static final String REQUEST_SUCCESS = "200";
    public static final String REQUEST_CS_FAIL = "403";
    public static final String REQUEST_IP_FAIL = "405";
    public static final String REQUEST_VAL_IP_FAIL = "413";
    public static final String REQUEST_VAL_DOMAIN_FAIL = "415";
    public static final String REQUEST_VAL_VIEWCODE_FAIL = "412";
    public static final String REQUEST_VAL_TTL_FAIL = "417";
    public static final String REQUEST_DATA_NULL = "411";
    public static final String CACHE_NULL = "418";
    public static final String RESPONSE_DATA_NULL = "414";
    public static final String MX_VALUE_FAIL = "419";
    public static final String SRV_VALUE_NUBBER = "421";
    public static final String CHECK_IP_FAIL = "426";
    public static final String CHECK_IP_RESULT_FAIL = "427";
    public static final String ROOTDOMAIN_SUBDOAMIN_FAIL = "422";
    public static final String REQUEST_API_FAIL = "423";
    public static final String ETCD_FAIL = "416";
    public static final String PATH_COUNTRY = "/country.properties";
    public static final String KEY = "dns_ip";
    public static final int CS_TIME = -20;
    public static final String SUB_DOMAIN = "subdomain";
    public static final String VIEW_CODE = "viewCode";
    public static final int IP_NUMBERS = 7;
    public static final String REQUEST_IP_VAL_FAIL = "權限不夠,請求被阻止!";
    public static final int IP_LENGTH = 64;
    public static final int TTL_MIN_SOA = 0;
    public static final int TTL_MAX_SOA = 86400;
    public static final int TTL_MIN = 0;
    public static final int TTL_MAX = 3600;
    public static final String INIT_IP_TIMER_STATUS = "init_ip_timer_status_V1";
    public static final String ACLLIB_TIMER_STATUS = "acllib_timer_status_V1";
    public static final String INIT_VIEWLIST_TIMER_STATUS = "init_viewlist_timer_status_V1";
    public static final String STATE_SUCCESS = "success";
    public static final String STATE_FAIL = "error";
    public static final String MEMCACHED_WHITE_IP = "MEMCACHED_WHITE_IP_V1";
    public static final String MEMCACHED_VIEW_LIST = "MEMCACHED_VIEW_LIST_V1";
    public static final String MEMCACHED_ERROR_IP_ISP = "memcached_error_ip_isp";
    public static final String MEMCACHED_LAST_SUCCESS_DOAMIN_VIEW = "MEMCACHED_LAST_SUCCESS_DOAMIN_VIEW_V1";
    public static final String FILE_RESULT = "FILE_RESULT_V1";
    public static final String LAST_POST_TIME = "LAST_POST_TIME_V1";
    public static final String LAST_PARSE_VIEW_STATE = "LAST_PARSE_VIEW_STATE_V1";
    private static final Logger LOG = LoggerFactory.getLogger(DnsApiUtils.class);
    private static final Pattern IPV4_PATTERN = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");

    public static final String VIEW_SERIAL = "3";
    public static final String VIEW_TTL = "600";
    public static final String VIEW_MAX_HOSTS = "8";
    public static final String VIEW_TARGETING = "country continent @ * regiongroup region city isp ip asn";
    public static final String VIEW_CONTACT = "support.bitnames.com";
    public static final String VIEW_WEIGHT = "100";
    public static final String VIEW_TYPE = "a";

    static final int VIEW_TIMEOUT = 10000;
    static final boolean VIEW_OUTPUT = true;
    static final String VIEW_METHOD = "PUT";
    static final String ETCD_METHOD = "GET";
    static final String VIEW_CHAR_CODE = "UTF-8";
    public static final String PASSWORD = "sdns-api";
    public static final String SERIAL = "serial";
    public static final String BWSERIAL = "bwserial";
    public static final String FORWARD_SERIAL = "fwserial";
    public static final String CHECK_IP = "checkip";
    public static final String CHECK_IP_DATA = "checkipdata";
    public static final String NOT_SENT_MSG="notsentmsg";
    public static Properties getProperties(String path) throws IOException {
        Properties props = new Properties();
        InputStream in = new BufferedInputStream(new FileInputStream(
                DnsApiUtils.class.getResource(path).getFile()));
        props.load(in);
        in.close();
        return props;
    }

    /**
     * 驗證請求頻次
     *
     * @param lastPostTime
     * @param csTime
     * @return true 能夠放問 false 不能夠
     */
    public static boolean timeAllow(Calendar lastPostTime, int csTime) {
        if (lastPostTime != null) {
            Calendar cl = Calendar.getInstance();
            cl.add(Calendar.SECOND, csTime);
            boolean Str = cl.compareTo(lastPostTime) == -1;
            if (Str) {
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }

    public static boolean valNum(String val) {
        try {
            new Integer(val);
            return true;
        } catch (Exception ce) {
            return false;
        }
    }

    public static boolean isIp(String input) {
        return IPV4_PATTERN.matcher(input.trim()).matches();
    }

    /**
     * 將127.0.0.1形式的IP地址轉換成十進制整數,這裏沒有進行任何錯誤處理
     * 經過左移位操做(<<)給每一段的數字加權,第一段的權爲2的24次方,第二段的權爲2的16次方,第三段的權爲2的8次方,最後一段的權爲1
     */
    public static long ip42Long(String ipaddress) {
        long[] ip = new long[4];
        //先找到IP地址字符串中.的位置
        int position1 = ipaddress.indexOf(".");
        int position2 = ipaddress.indexOf(".", position1 + 1);
        int position3 = ipaddress.indexOf(".", position2 + 1);
        //將每一個.之間的字符串轉換成整型
        ip[0] = Long.parseLong(ipaddress.substring(0, position1));
        ip[1] = Long.parseLong(ipaddress.substring(position1 + 1, position2));
        ip[2] = Long.parseLong(ipaddress.substring(position2 + 1, position3));
        ip[3] = Long.parseLong(ipaddress.substring(position3 + 1));
        return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
    }

    /**
     * 驗證IP
     *
     * @param iP
     * @return
     */
    public static boolean valIp(String iP) {
        if (iP == null || "".equals(iP)) {
            return false;
        }
        if (DnsApiUtils.isIp(iP) && iP.length() <= IP_LENGTH) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 驗證Ttl
     *
     * @param ttl
     * @return
     */
    public static boolean valTtl(int ttl) {
        try {
            if (ttl <= TTL_MAX && ttl >= TTL_MIN) {
                return true;
            } else {
                return false;
            }
        }catch (Exception ce){
            return false;
        }
    }
    public static boolean valTtlSoa(String ttl) {
        try {
            int ttlNum=Integer.parseInt(ttl);
            if (ttlNum <= TTL_MAX_SOA && ttlNum >= TTL_MIN_SOA) {
                return true;
            } else {
                return false;
            }
        }catch (Exception ce){
            return false;
        }
    }
    /**
     * 驗證子域爲數字、字母、* 、點,點不能兩個以上相連,不能爲空
     *
     * @param addressArr
     * @return
     */
    public static boolean isDomain(String addressArr) {

        if ("".equals(addressArr)) {
            return false;
        }
        Pattern p = Pattern.compile("(\\.)\\1{1,}");
        java.util.regex.Matcher m = p.matcher(addressArr);
        if (m.find()) {
            return false;
        }
        String Str = "\\.";
        addressArr = addressArr.replaceAll(Str, "");
        addressArr = addressArr.replaceAll("\\*", "");
        addressArr = addressArr.replaceAll("-", "");
        addressArr = addressArr.replaceAll("_", "");
        boolean valDomain = addressArr.matches("[0-9A-Za-z]*");
        if (valDomain) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 從配置文件獲取大洲、國家、ALL
     *
     * @return
     * @throws Exception
     */
    public static List<String> getCountry() throws IOException {
        Properties properties = getProperties(PATH_COUNTRY);
        Enumeration<?> enu = properties.propertyNames();
        List<String> list = new ArrayList<String>();
        while (enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            String value = (String) properties.get(key);
            list.add(key);
            String[] counts = value.split(",");
            list.addAll(Arrays.asList(counts));
        }
        list.add(VIEW_ALL);
        return list;
    }

    /**
     * 調用shell腳本
     *
     * @param cmdSh
     * @param fromPath
     * @param toPath
     * @param toIp
     * @return
     */
    public static int exeShell(String cmdSh, String fromPath, String toPath, String toIp) {
        LineNumberReader input = null;
        InputStreamReader ir = null;
        try {
            StringBuilder cmd = new StringBuilder();
            cmd.append("sh ");
            cmd.append(cmdSh);
            cmd.append(" ");
            cmd.append(fromPath);
            cmd.append(" ");
            cmd.append(toIp);
            cmd.append(" ");
            cmd.append(toPath);
            Process pos = Runtime.getRuntime().exec(cmd.toString());
            ir = new InputStreamReader(pos.getInputStream(), "UTF-8");
            input = new LineNumberReader(ir);
            String line;
            while ((line = input.readLine()) != null) {
                LOG.debug(line);
            }
            pos.waitFor();
            return pos.exitValue();
        } catch (InterruptedException e) {
            LOG.error("shell腳本執行異常!" + e.getMessage());
            return 1;
        } catch (IOException e) {
            LOG.error("shell腳本執行異常!" + e.getMessage());
            return 1;
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
                if (ir != null) {
                    ir.close();
                }
            } catch (IOException ioE) {
                LOG.error("流關閉異常!");
            }
        }
    }

    /**
     * 取得服務器文件差別列表
     *
     * @param cmdSh
     * @param fromPath
     * @param toPath
     * @param ip
     * @return
     * @throws Exception
     */
    public static List<String> getDiffFile(String cmdSh, String fromPath, String toPath, String ip) throws IOException {
        StringBuilder cmd = new StringBuilder();
        cmd.append("sh ");
        cmd.append(cmdSh);
        cmd.append(" ");
        cmd.append(fromPath);
        cmd.append(" ");
        cmd.append(ip);
        cmd.append(" ");
        cmd.append(toPath);
        LOG.info("檢查文件*********" + cmd.toString());
        Process pos = Runtime.getRuntime().exec(cmd.toString());
        InputStreamReader ir = new InputStreamReader(pos.getInputStream(), "UTF-8");
        LineNumberReader input = new LineNumberReader(ir);
        String line;
        List<String> list = new ArrayList<String>();
        while ((line = input.readLine()) != null) {
            if (!"".equals(line)) {
                if (line.startsWith("deleting")) {
                    list.add(line.substring("deleting".length(), line.length()).trim());
                } else {
                    list.add(line);
                }
            } else {
                break;
            }
        }
        if (!list.isEmpty()) {
            LOG.info("移除文件差別列表頭:" + list.get(0));
            list.remove(0);
        }
        input.close();
        ir.close();
        return list;
    }

    /**
     * 檢查zone文件
     *
     * @param cmdSh
     * @param domain
     * @param zoneConfig
     * @return
     * @throws Exception
     */
    public static String checkZone(String cmdSh, String domain, String zoneConfig) throws IOException {
        StringBuilder cmd = new StringBuilder();
        cmd.append("sh ");
        cmd.append(cmdSh);
        cmd.append(" ");
        cmd.append(domain);
        cmd.append(" ");
        cmd.append(zoneConfig);
        LOG.debug("檢查zone文件" + cmd.toString());
        Process pos = Runtime.getRuntime().exec(cmd.toString());
        InputStreamReader ir = new InputStreamReader(pos.getInputStream(), "UTF-8");
        LineNumberReader input = new LineNumberReader(ir);
        String line;
        StringBuilder checkZoneStr = new StringBuilder();
        while ((line = input.readLine()) != null) {
            checkZoneStr.append(line);
        }
        input.close();
        ir.close();
        return checkZoneStr.toString();
    }

    /**
     * 傳入shell文件名,取得shell文件路徑
     *
     * @param shellName
     * @return
     * @throws Exception
     */
    public static String getShellPath(String shellName) throws URISyntaxException {
        return DnsApiUtils.class.getClassLoader().getResource("").toURI().getPath() + "shell/" + shellName;
    }

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

    static void setSystem() throws IOException {
        Properties ps = getProperties("/env.properties");
        String filePath = DnsApiUtils.class.getResource("/").getPath() + "keystore/";
        String jks = filePath + "keystore.jks";
        String password = (String) ps.get("https_password");
        String ts = filePath + "truststore.ts";
        System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");
        System.setProperty("javax.net.ssl.keyStoreType", "JKS");
        System.setProperty("javax.net.ssl.trustStore", ts);
        System.setProperty("javax.net.ssl.keyStore", jks);
        System.setProperty("javax.net.ssl.keyStorePassword", password);
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
    }

    public static int sendMessage(String data, String urlStr, String type) {
        try {
            setSystem();
            String msg = data;
           // StringBuffer sb = new StringBuffer(urlStr);
          //  sb.replace(sb.length() - 6, sb.length(), "security/conf");
           // String requestUrl = sb.toString();
            URL url = new URL(urlStr);
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setConnectTimeout(VIEW_TIMEOUT);
            connection.setDoOutput(VIEW_OUTPUT);
            connection.setRequestMethod(VIEW_METHOD);
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());
            String fileMsg = "value=" + URLEncoder.encode(msg, VIEW_CHAR_CODE);
            System.out.println(urlStr + "內容:" + msg);
            InputStream fileInputStream = new ByteArrayInputStream(fileMsg.getBytes(VIEW_CHAR_CODE));
            byte[] bytes = new byte[1024];
            int numReadByte = 0;
            while ((numReadByte = fileInputStream.read(bytes, 0, 1024)) > 0) {
                out.write(bytes, 0, numReadByte);
            }
            out.flush();
            fileInputStream.close();
            int code = connection.getResponseCode();
            LOG.info("返回碼:" + code + "文件名:" + urlStr);
            return code;
        } catch (Exception ce) {
            LOG.error(ce.getMessage());
            return 500;
        }

    }
//    public static int sendFD(String data, String urlStr) {
//        try {
//            setSystem();
//            String msg = data;
//            StringBuffer sb = new StringBuffer(urlStr);
//            sb.replace(sb.length() - 6, sb.length(), "forward/conf");
//            String requestUrl = sb.toString();
//            URL url = new URL(requestUrl);
//            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
//            connection.setConnectTimeout(VIEW_TIMEOUT);
//            connection.setDoOutput(VIEW_OUTPUT);
//            connection.setRequestMethod(VIEW_METHOD);
//            connection.setRequestProperty("Accept", "application/json");
//            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//            BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());
//            String fileMsg = "value=" + URLEncoder.encode(msg, VIEW_CHAR_CODE);
//            System.out.println(requestUrl + "內容:" + msg);
//            InputStream fileInputStream = new ByteArrayInputStream(fileMsg.getBytes(VIEW_CHAR_CODE));
//            byte[] bytes = new byte[1024];
//            int numReadByte = 0;
//            while ((numReadByte = fileInputStream.read(bytes, 0, 1024)) > 0) {
//                out.write(bytes, 0, numReadByte);
//            }
//            out.flush();
//            fileInputStream.close();
//            int code = connection.getResponseCode();
//            LOG.info("返回碼:" + code + "文件名:" + requestUrl);
//            return code;
//        } catch (Exception ce) {
//            LOG.error(ce.getMessage());
//            return 500;
//        }
//
//    }
    public static String format(JsonResult jsonResult) {
        return com.alibaba.fastjson.JSONObject.toJSONString(jsonResult, true);
    }

    public static String   getLeader(String etcd_leader)throws IOException {


        URL url = new URL(etcd_leader+":4001/v2/leader");
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setConnectTimeout(VIEW_TIMEOUT);
        connection.setDoOutput(VIEW_OUTPUT);
        connection.setRequestMethod(ETCD_METHOD);
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        int code = connection.getResponseCode();
        if (code != 200&&code!=201) {
            return "";
        } else {
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            String lines;
            StringBuffer sb = new StringBuffer();
            while ((lines = reader.readLine()) != null) {
                sb.append(lines);
            }
            reader.close();
            connection.disconnect();
            return sb.toString().replaceAll("http","https").replaceAll(":7001","");
        }
    }

    public static int uploadFile(JsonResult jsonResult, String urlStr, String key) {
        try {
            Properties ps = getProperties("/env.properties");
            String etcd_leader =(String) ps.get("etcd_leader");
            setSystem();
            String leader =getLeader(etcd_leader);
            String msg = format(jsonResult);
            String requestUrl = urlStr + key.replaceAll("\\*\\.", "") + ".json";
            requestUrl=requestUrl.replaceAll(etcd_leader,leader) ;
            LOG.info("send etcd to leader "+requestUrl);
            URL url = new URL(requestUrl);
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setConnectTimeout(VIEW_TIMEOUT);
            connection.setDoOutput(VIEW_OUTPUT);
            connection.setRequestMethod(VIEW_METHOD);
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());
            String fileMsg = "value=" + URLEncoder.encode(msg, VIEW_CHAR_CODE);
//            System.out.println(requestUrl + "內容:" + msg);
            InputStream fileInputStream = new ByteArrayInputStream(fileMsg.getBytes(VIEW_CHAR_CODE));
            byte[] bytes = new byte[1024];
            int numReadByte = 0;
            while ((numReadByte = fileInputStream.read(bytes, 0, 1024)) > 0) {
                out.write(bytes, 0, numReadByte);
            }
            out.flush();
            fileInputStream.close();
            int code = connection.getResponseCode();
            LOG.info("返回碼:" + code + "文件名:" + requestUrl);
            connection.disconnect();
            return code;
        } catch (Exception ce) {
            LOG.error(ce.getMessage());
            ce.printStackTrace();
            return 500;
        }

    }

    public static String getSubString(String str, String strKey) {
        if (str.indexOf("(") != -1) {
            return str.substring(0, str.indexOf(strKey));
        } else {
            return str;
        }
    }

    public static String getMd5Url(String url, String domain, String ip) {
        StringBuffer sb = new StringBuffer(url);
        sb.insert(url.lastIndexOf("sdns") + 4, "/zones/" + domain);
        sb.replace(sb.length() - 6, sb.length(), ip);
        return sb.toString();
    }

    public static String getEtcdMd5(String urlEtcd) {
        try {
            setSystem();
            URL getUrl = new URL(urlEtcd);
            HttpsURLConnection connection = (HttpsURLConnection) getUrl
                    .openConnection();
            connection.setConnectTimeout(VIEW_TIMEOUT);
            connection.setDoOutput(VIEW_OUTPUT);
            connection.setRequestMethod(ETCD_METHOD);
            int code = connection.getResponseCode();
            if (code != 200&&code!=201) {
                return "";
            } else {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
                String lines;
                StringBuffer sb = new StringBuffer();
                while ((lines = reader.readLine()) != null) {
                    sb.append(lines);
                }
                reader.close();
                return sb.toString();
            }
        } catch (Exception ce) {
            return "";
        }
    }
    public static Map<String,Object> getCheckIpResult(String urlEtcd) {
        Map<String,Object> map=new HashMap<String, Object>();
        try {
            setSystem();
            URL getUrl = new URL(urlEtcd);
            HttpsURLConnection connection = (HttpsURLConnection) getUrl
                    .openConnection();
            connection.setConnectTimeout(VIEW_TIMEOUT);
            connection.setDoOutput(VIEW_OUTPUT);
            connection.setRequestMethod(ETCD_METHOD);
            int code = connection.getResponseCode();
            map.put("status",code);
            if (code != 200&&code!=201) {
                map.put("msg","");
            } else {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
                String lines;
                StringBuffer sb = new StringBuffer();
                while ((lines = reader.readLine()) != null) {
                    sb.append(lines);
                }
                reader.close();
                map.put("msg",sb.toString());
            }
            return map;
        } catch (Exception ce) {
            map.put("status","500");
            map.put("msg","");
            return map;
        }
    }

    public static String StringFilter(String str) {
        String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\]<>/?~!@#¥%……&*()——+|{}【】‘;:」「’。,、?]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    public static String getNewDomain(String mapRKey) {
        if (mapRKey != null) {
            return mapRKey.replaceAll("_", ".").toLowerCase().replaceAll("\\.all", "");
        } else {
            return "";
        }
    }
    public static long ip2long(String ip) {
        String[] ips = ip.split("[.]");
        long num =  16777216L*Long.parseLong(ips[0]) + 65536L*Long.parseLong(ips[1]) + 256*Long.parseLong(ips[2]) + Long.parseLong(ips[3]);
        return num;
    }
    public static int deleteDomain(String requestUrl) {
        try {
            setSystem();
            URL url = new URL(requestUrl);
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setConnectTimeout(DnsApiUtils.VIEW_TIMEOUT);
            connection.setDoOutput(DnsApiUtils.VIEW_OUTPUT);
            connection.setRequestMethod("DELETE");
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            int code = connection.getResponseCode();
            return code;
        } catch (Exception ce) {
            LOG.error(ce.getMessage());
            return 500;
        }
    }
}
相關文章
相關標籤/搜索