Exemple #1
0
func integrityVerify(filename string) bool {
	iplist := iputil.GetDetectedIpInfoSlice(filename)
	bIntegrity := true
	value := iplist[0]
	fmt.Println(value)
	for i, ipMap := range iplist {
		if i == 0 {
			continue
		}
		if ipMap["end"] == "" {
			ipMap["end"] = ipMap["ip"]
			ipMap["len"] = "1"
		}
		testip1 := iputil.InetAtonInt(value["end"])
		testip2 := iputil.InetAtonInt(ipMap["ip"])
		if isSequentail(testip1, testip2) == false {
			ip11 := iputil.InetAtonInt(value["end"]) + 1
			ip22 := iputil.InetAtonInt(ipMap["ip"]) - 1
			sip1 := iputil.InetNtoaStr(ip11)
			sip2 := iputil.InetNtoaStr(ip22)
			fmt.Println(sip1 + "|" + sip2)

			bIntegrity = false
		}
		value = ipMap
	}
	return bIntegrity
}
func CalcuAndSplit(startip, endip string, ipinfoMap map[string]interface{}, resultFP, middleresultFP *os.File) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("PPPPPPPPPPPPPPP get panic", r)
		}
	}()
	var startipMap map[string]string
	var endipMap map[string]string

	fmt.Println("-------------------------------------------------")
	fmt.Println("startip|endip|" + startip + "|" + endip)
	fmt.Println("-------------------------------------------------")

	info1, b1 := ipinfoMap[startip]
	if b1 == false {
		url1 := fmt.Sprintf("%s%s", taobaoURL, startip)
		startipMap, _ = iputil.ParseUrlToMap(url1)
	} else {
		startipMap = info1.(map[string]string)
	}
	ipinfoMap[startip] = startipMap

	result1 := iputil.KeyinfoFormatToOutput(startipMap)
	middleresultFP.WriteString(startip + "|" + startip + "|1|" + result1 + "\n")

	if startip == endip {
		resultFP.WriteString(startip + "|" + endip + "\n")
		return
	}

	info2, b2 := ipinfoMap[endip]
	if b2 == false {
		url2 := fmt.Sprintf("%s%s", taobaoURL, endip)
		endipMap, _ = iputil.ParseUrlToMap(url2)
	} else {
		endipMap = info2.(map[string]string)
	}
	ipinfoMap[endip] = endipMap

	result2 := iputil.KeyinfoFormatToOutput(endipMap)
	middleresultFP.WriteString(endip + "|" + endip + "|1|" + result2 + "\n")

	ip1 := iputil.InetAtonInt(startip)
	ip2 := iputil.InetAtonInt(endip)

	for ip1 < ip2 {
		m := (ip1 + ip2) / 2
		ip1_str := iputil.InetNtoaStr(ip1)
		ip2_str := iputil.InetNtoaStr(ip2)
		mip := iputil.InetNtoaStr(m)
		mip_rfirst := iputil.InetNtoaStr(m + 1)
		fmt.Println("start|middle-ip|end", ip1_str, mip, ip2_str)
		url1 := fmt.Sprintf("%s%s", taobaoURL, mip)
		url2 := fmt.Sprintf("%s%s", taobaoURL, mip_rfirst)
		var mipinfo1, mipinfo2 map[string]string

		mipInfo1, exist1 := ipinfoMap[mip]
		if exist1 == false {
			mipinfo1, _ = iputil.ParseUrlToMap(url1)
			ipinfoMap[mip] = mipinfo1
		} else {
			mipinfo1 = mipInfo1.(map[string]string)
		}

		mipInfo2, exist2 := ipinfoMap[mip_rfirst]
		if exist2 == false {
			mipinfo2, _ = iputil.ParseUrlToMap(url2)
			ipinfoMap[mip_rfirst] = mipinfo2
		} else {
			mipinfo2 = mipInfo2.(map[string]string)
		}

		/*store middle detect result*/
		result1 := iputil.KeyinfoFormatToOutput(mipinfo1)
		result2 := iputil.KeyinfoFormatToOutput(mipinfo2)
		middleresultFP.WriteString(mip + "|" + mip + "|1|" + result1 + "\n")
		middleresultFP.WriteString(mip_rfirst + "|" + mip_rfirst + "|1|" + result2 + "\n")
		middleresultFP.Sync()

		var finded string
		finded = QualifiedIpAtLevel("country", mipinfo1, startipMap, endipMap)
		fmt.Println("country finded:", finded)
		switch finded {
		case goon:
			finded = QualifiedIpAtLevel("isp", mipinfo1, startipMap, endipMap)
			fmt.Println("isp finded:", finded)
			switch finded {
			case goon:
				finded = QualifiedIpAtLevel("region", mipinfo1, startipMap, endipMap)
				fmt.Println("province finded:", finded)
				switch finded {
				case goon:
					fmt.Println("this is same network")
					SaveSameNetwork(ip1_str, ip2_str, ipinfoMap[ip1_str], resultFP)
					return
				case leftmove:
					ip1 = m + 1
					SaveSameNetwork(ip1_str, mip, ipinfoMap[ip1_str], resultFP)
				case rightmove:
					ip2 = m
					SaveSameNetwork(mip_rfirst, ip2_str, ipinfoMap[mip_rfirst], resultFP)
				case morenetwork:
					CalcuAndSplit(ip1_str, mip, ipinfoMap, resultFP, middleresultFP)
					CalcuAndSplit(mip_rfirst, ip2_str, ipinfoMap, resultFP, middleresultFP)
					return
				}

			case leftmove:
				SaveSameNetwork(ip1_str, mip, ipinfoMap[ip1_str], resultFP)
				ip1 = m + 1
			case rightmove:
				SaveSameNetwork(mip_rfirst, ip2_str, ipinfoMap[mip_rfirst], resultFP)
				ip2 = m
			case morenetwork:
				CalcuAndSplit(ip1_str, mip, ipinfoMap, resultFP, middleresultFP)
				CalcuAndSplit(mip_rfirst, ip2_str, ipinfoMap, resultFP, middleresultFP)
				return
			}

		case leftmove:
			SaveSameNetwork(ip1_str, mip, ipinfoMap[ip1_str], resultFP)
			ip1 = m + 1
		case rightmove:
			SaveSameNetwork(mip_rfirst, ip2_str, ipinfoMap[mip_rfirst], resultFP)
			ip2 = m
		case morenetwork:
			CalcuAndSplit(ip1_str, mip, ipinfoMap, resultFP, middleresultFP)
			CalcuAndSplit(mip_rfirst, ip2_str, ipinfoMap, resultFP, middleresultFP)
			return
		}

	}
}
Exemple #3
0
func MergeIP(filename, mergedFile, breakFile string) bool {
	mergeFP, err := os.Create(mergedFile)
	if err != nil {
		fmt.Println("open file failed")
		return false
	}
	defer mergeFP.Close()

	breakFP, err := os.Create(breakFile)
	if err != nil {
		fmt.Println("open file failed")
		return false
	}
	defer breakFP.Close()

	iplist := iputil.GetDetectedIpInfoSlice(filename)
	bIntegrity := true
	current := iplist[0]
	for i, ipMap := range iplist {
		if i == 0 {
			continue
		}
		if ipMap["end"] == "" {
			ipMap["end"] = ipMap["ip"]
			ipMap["len"] = "1"
		}
		testip1 := iputil.InetAtonInt(current["end"])
		testip2 := iputil.InetAtonInt(ipMap["ip"])
		if testip1 == testip2 {
			if EqualOfTwoNetwork(current, ipMap) == true {
				current = ipMap
			} else {
				fmt.Println("ERROR CURR:", current)
				fmt.Println("ERROR IPMAP:", ipMap)
			}
			current = ipMap
		} else if testip1+1 < testip2 {
			newgInfo := iputil.AllKeyinfoFormatToOutput(current)
			mergeFP.WriteString(newgInfo + "\n")
			ip11 := iputil.InetAtonInt(current["end"]) + 1
			ip22 := iputil.InetAtonInt(ipMap["ip"]) - 1
			sip1 := iputil.InetNtoaStr(ip11)
			sip2 := iputil.InetNtoaStr(ip22)
			breakFP.WriteString(sip1 + "|" + sip2 + "\n")

			bIntegrity = false
			current = ipMap
		} else if testip1+1 > testip2 {
			current = ipMap
		} else if testip1+1 == testip2 {
			if EqualOfTwoNetwork(current, ipMap) == true {
				//info1 := iputil.AllKeyinfoFormatToOutput(current)
				//info2 := iputil.AllKeyinfoFormatToOutput(ipMap)
				//fmt.Println(info1)
				//fmt.Println(info2)
				current["end"] = ipMap["end"]
				l1, _ := strconv.Atoi(current["len"])
				l2, _ := strconv.Atoi(ipMap["len"])
				current["len"] = strconv.Itoa(l1 + l2)
			} else {
				newgInfo := iputil.AllKeyinfoFormatToOutput(current)
				mergeFP.WriteString(newgInfo + "\n")
				//fmt.Println("11111 not equal start!!!")
				//fmt.Println(current)
				//fmt.Println(ipMap)
				//fmt.Println("11111 not equal end!!!")
				current = ipMap
			}
		}
	}
	return bIntegrity
}