func detect_ip(result chan string, hk *temp_data, newip string) {
head:
	startIpInfo, e1 := iputil.ParseUrlToMap(hk.url)
	endIpInfo, e2 := iputil.ParseUrlToMap(hk.endIPUrl)
	if startIpInfo["country_id"] == "" {
		println("NULL VALUE", startIpInfo)
		time.Sleep(1 * 1e9)
		goto head
	}
	if e2 && e1 && startIpInfo["country_id"] == endIpInfo["country_id"] && startIpInfo["isp_id"] == endIpInfo["isp_id"] && startIpInfo["city_id"] == endIpInfo["city_id"] {
		fmt.Println(startIpInfo)
		info := iputil.KeyinfoFormatToOutput(startIpInfo)
		address := fmt.Sprintf("%s|%s|%d|%s\n", startIpInfo["ip"], newip, (1 + iputil.InetAtonInt(newip) - iputil.InetAtonInt(startIpInfo["ip"])), info)
		fmt.Printf("!!!ok!!!", address)
		result <- address
	} else {
		info1 := iputil.KeyinfoFormatToOutput(startIpInfo) + "|" + newip
		info1 = fmt.Sprintf("%s|lineno:%d\n", info1, hk.fileno)
		info2 := iputil.KeyinfoFormatToOutput(endIpInfo) + "\n"
		fmt.Printf("$$$", info1)
		fmt.Printf("$$$", info2)
		hk.fp.WriteString(info1)
		hk.fp.WriteString(info2)
		hk.fp.Sync()
		result <- "NOTSAME"
	}
}
示例#2
0
func SaveSameNetwork(startip, endip string, ipinfoMap interface{}, fileFP *os.File) {
	ipmap := ipinfoMap.(map[string]string)
	if ipmap == nil {
		url := fmt.Sprintf("%s%s", taobaoURL, endip)
		ipmap, _ = iputil.ParseUrlToMap(url)
	}
	lens := iputil.InetAtonInt(endip) - iputil.InetAtonInt(startip) + 1
	result := iputil.KeyinfoFormatToOutput(ipmap)
	fileFP.WriteString(startip + "|" + endip + "|" + strconv.Itoa(int(lens)) + "|" + result + "\n")
	fileFP.Sync()
}
示例#3
0
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
		}

	}
}