package ee; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.MapListHandler; import java.io.*; import java.math.BigInteger; import java.sql.SQLException; import java.util.*; import java.util.regex.MatchResult; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Hello world! */ public class App { public static HashMap<Integer, String> citys = new HashMap<Integer, String>() {{ put(2, "beijing"); put(3, "anhui"); put(4, "fujian"); put(5, "gansu"); put(6, "guangdong"); put(7, "guangxi"); put(8, "guiyang"); put(9, "hainan"); put(10, "hebei"); put(11, "henan"); put(12, "heilongjiang"); put(13, "hubei"); put(14, "hunan"); put(15, "jilin"); put(16, "jiangsu"); put(17, "jiangxi"); put(18, "liaoning"); put(19, "neimenggu"); put(20, "ningxia"); put(21, "qinghai"); put(22, "shandong"); put(23, "shanxi23"); put(24, "sanxi24"); put(25, "shanghai"); put(26, "sichuan"); put(27, "tianjing"); put(28, "xizang"); put(29, "xinjiang"); put(30, "yunnan"); put(31, "zhejiang"); put(32, "chongqing"); put(33, "xianggang"); put(34, "aoming"); put(35, "tanwan"); }}; public static HashMap<Integer, String> isp = new HashMap<Integer, String>() {{ put(0, "qita"); put(1, "dianxin"); put(2, "liantong"); put(3, "yidong"); }}; public static HashSet<String> sipset = new HashSet<>(); public static HashSet<String> dipset = new HashSet<>(); public static void main(String[] args) throws IOException, SQLException { List<String> argsList = Arrays.asList(args); if (argsList.size() == 2) { read(new Integer(argsList.get(0)), new Integer(argsList.get(1))); } // Integer sum = 0; // for (String arg : args) { // sum += logSum(arg); // } // System.out.println(sum); } public static Integer logSum(String patten) throws IOException { String filePath = "log.log"; File file = new File(filePath); if (file.isFile() && file.exists()) { InputStreamReader read = new InputStreamReader(new FileInputStream(file)); BufferedReader bufferedReader = new BufferedReader(read); String lineTxt; StringBuilder sb = new StringBuilder(); while ((lineTxt = bufferedReader.readLine()) != null) { sb.append(lineTxt); } read.close(); Pattern r = Pattern.compile(patten); Matcher m = r.matcher(sb.toString()); List<String> matchList = new ArrayList<>(); while (m.find()) { matchList.add(m.group(1)); } Integer sum = 0; for (String s : matchList) { sum += new Integer(s); } return sum; } return 0; } public static void read(Integer flow, Integer num) throws IOException, SQLException { HashMap<String, List<Integer>> sresult = new HashMap<>(); HashMap<String, List<Integer>> dresult = new HashMap<>(); String filePath = "s.pcap"; File file = new File(filePath); BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); byte[] a = new byte[1024]; int count; StringBuilder sb = new StringBuilder(); while ((count = in.read(a)) != -1) { for (int i = 0; i < count; i++) { convertByte2Hex(a[i], sb); } } in.close(); ComboPooledDataSource dataSource = DBPool.getInstance().getDataSource(); QueryRunner run = new QueryRunner(dataSource); MapListHandler mapHandler = new MapListHandler(); List<Map<String, Object>> dips; List<Map<String, Object>> sips; if (flow == 1) { dips = run.query("SELECT INET_ATON(ip_begin) AS b, INET_ATON(ip_end) AS e, province, isp FROM _ips", mapHandler); sips = run.query("SELECT INET_ATON(ip_begin) AS b, INET_ATON(ip_end) AS e, province, isp FROM _ips where province = 18 and isp = 1", mapHandler); } else { sips = run.query("SELECT INET_ATON(ip_begin) AS b, INET_ATON(ip_end) AS e, province, isp FROM _ips", mapHandler); dips = run.query("SELECT INET_ATON(ip_begin) AS b, INET_ATON(ip_end) AS e, province, isp FROM _ips where province = 18 and isp = 1", mapHandler); } String fileString = sb.toString(); for (Integer i = 0; i < num; i++) { Map<String, Object> sobj = sips.get(rand(sips.size())); Long b = ((BigInteger) sobj.get("b")).longValue(); Long e = ((BigInteger) sobj.get("e")).longValue(); String sip = longToIp2(b + rand((new Long(e - b).intValue()))); String sipToHex = strIpToHex(sip); if (sipset.contains(sipToHex)) { continue; } else { sipset.add(sipToHex); } String replacedS = fileString.replace("3b2d6eaa", sipToHex); Map<String, Object> dobj = dips.get(rand(dips.size())); b = ((BigInteger) dobj.get("b")).longValue(); e = ((BigInteger) dobj.get("e")).longValue(); String dip = longToIp2(b + rand((new Long(e - b).intValue()))); sipToHex = strIpToHex(dip); if (dipset.contains(sipToHex)) { continue; } else { dipset.add(sipToHex); } String replacedD = replacedS.replace("ca6a0014", sipToHex); String sprovince = citys.get(new Integer((String) sobj.get("province"))); Integer sisp1 = new Integer((String) sobj.get("isp")); String sisp = App.isp.get(sisp1); String dprovince = citys.get(new Integer((String) dobj.get("province"))); Integer disp1 = new Integer((String) dobj.get("isp")); String disp = App.isp.get(disp1); writeNew2Binary(join(i.toString(), "-", sprovince, "_", sisp, "-", dprovince, "_", disp ), replacedD); if (sresult.get(sprovince) == null) { List<Integer> list = Arrays.asList(0, 0, 0, 0); sresult.put(sprovince, list); } sresult.get(sprovince).set(sisp1, sresult.get(sprovince).get(sisp1) + 1); if (dresult.get(dprovince) == null) { List<Integer> list = Arrays.asList(0, 0, 0, 0); dresult.put(dprovince, list); } dresult.get(dprovince).set(disp1, dresult.get(dprovince).get(disp1) + 1); } System.out.println(sresult); System.out.println(dresult); } public static String join(String... strings) { StringBuilder sb = new StringBuilder(); for (String string : strings) { sb.append(string); } return sb.toString(); } private static void convertByte2Hex(byte b, StringBuilder sb) { int number = b & 0x00ff; if (number < 16) { sb.append(0); } sb.append(Integer.toHexString(number)); } public static void writeNew2Binary(String fileName, String replaced) throws NumberFormatException, IOException { System.out.println(fileName); FileOutputStream fout = new FileOutputStream(".\\out\\" + fileName + "_.pcap"); for (int i = 0; i < replaced.length(); i = i + 2) { fout.write(Integer.parseInt(replaced.substring(i, i + 2), 16)); } } public static String strIpToHex(String string) { String[] split = string.split("\\."); StringBuilder stringBuilder = new StringBuilder(); for (String s : split) { Integer integer = new Integer(s); if (integer < 16) { stringBuilder.append("0"); } stringBuilder.append(Integer.toHexString(integer)); } return stringBuilder.toString(); } public static Integer rand(Integer max) { if (max == 0) { return max; } Random random = new Random(); return random.nextInt(max); } public static String longToIp2(long ip) { StringBuilder sb = new StringBuilder(15); for (int i = 0; i < 4; i++) { sb.insert(0, Long.toString(ip & 0xff)); if (i < 3) { sb.insert(0, '.'); } ip = ip >> 8; } return sb.toString(); } }