调用shell脚本,IP处理

时间:2021-03-17 01:58:35
//调用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;
}
}
}