调用淘宝查询ip的 api,已经筛选出来一堆ip
package main
import (
"fmt"
"net/http"
"time"
"encoding/json"
)
type IPInfo struct {
Code int `json:"code"`
Data IP `json:"data`
}
type IP struct {
IP string `json:"ip"`
Country string `json:"country"`
Region string `json:"region"`
City string `json:"city"`
Count int `json:"count"`
}
func WebServerBase() {
fmt.Println("server base go!")
//第一个参数为客户端发起http请求时的接口名,第二个参数是一个func,负责处理这个请求。
http.HandleFunc("/result", handleTask)
//服务器要监听的主机地址和端口号
err := http.ListenAndServe(":8081", nil)
if err != nil {
fmt.Println("ListenAndServe error: ", err.Error())
}
}
func handleTask(w http.ResponseWriter, req *http.Request) {
fmt.Println("handleTask is running...")
//添加响应头,解决跨域请求问题
w.Header().Set("Access-Control-Allow-Origin", "*")
//模拟延时
time.Sleep(time.Second * 2)
var count= count_go()
m := make(map[string]interface{})
arrList := make([]interface{}, 0)
for k, v := range count {
var a = TabaoAPI(k) //a是一个struct对象,ipInfo
a.Data.Count = v ************报错
fmt.Println(&a)
arr1 := make(map[string]interface{})
arr1["ip"] = a.Data.IP ************报错
arr1["country"] = a.Data.Country************报错
arr1["count"] = a.Data.Count ************报错
arrList = append(arrList, arr1)
m["mydata"] = arrList
}
bt, _ := json.Marshal(m)
fmt.Println(string(bt))
fmt.Fprintf(w,string(bt)[:])
}
ip提取整理
package main
import (
"net"
"fmt"
"bytes"
"net/http"
"io/ioutil"
"encoding/json"
"strings"
"strconv"
"regexp"
"math/big"
"io"
"os"
"bufio"
"reflect"
)
type IPInfo struct {
Code int `json:"code"`
Data IP `json:"data`
}
type IP struct {
IP string `json:"ip"`
Country string `json:"country"`
Region string `json:"region"`
City string `json:"city"`
Count int `json:"count"`
}
//string转int
func InetAtoN(ip string) int64 {
ret := big.NewInt(0)
ret.SetBytes(net.ParseIP(ip).To4())
return ret.Int64()
}
//struct转map
func Struct2Map(obj interface{}) map[string] interface{} {
t := reflect.TypeOf(obj)
v := reflect.ValueOf(obj)
var data = make(map[string]interface{})
for i := 0; i < t.NumField(); i++ {
data[t.Field(i).Name] = v.Field(i).Interface()
}
return data
}
//int转ip
func inet_ntoa(ipnr int64) net.IP {
var bytes [4]byte
bytes[0] = byte(ipnr & 0xFF)
bytes[1] = byte((ipnr >> 8) & 0xFF)
bytes[2] = byte((ipnr >> 16) & 0xFF)
bytes[3] = byte((ipnr >> 24) & 0xFF)
return net.IPv4(bytes[3], bytes[2], bytes[1], bytes[0])
}
//ip(string)转int
func inet_aton(ipnr net.IP) int64 {
bits := strings.Split(ipnr.String(), ".")
b0, _ := strconv.Atoi(bits[0])
b1, _ := strconv.Atoi(bits[1])
b2, _ := strconv.Atoi(bits[2])
b3, _ := strconv.Atoi(bits[3])
var sum int64
sum += int64(b0) << 24
sum += int64(b1) << 16
sum += int64(b2) << 8
sum += int64(b3)
return sum
}
//判断是否为公网IP
func IsPublicIP(IP net.IP) bool {
if IP.IsLoopback() || IP.IsLinkLocalMulticast() || IP.IsLinkLocalUnicast() {
return false
}
if ip4 := IP.To4(); ip4 != nil {
switch true {
case ip4[0] == 10:
return false
case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31:
return false
case ip4[0] == 192 && ip4[1] == 168:
return false
default:
return true
}
}
return false
}
//判断ip地址区间
func IpBetween(from net.IP, to net.IP, test net.IP) bool {
if from == nil || to == nil || test == nil {
fmt.Println("An ip input is nil") // or return an error!?
return false
}
from16 := from.To16()
to16 := to.To16()
test16 := test.To16()
if from16 == nil || to16 == nil || test16 == nil {
fmt.Println("An ip did not convert to a 16 byte") // or return an error!?
return false
}
if bytes.Compare(test16, from16) >= 0 && bytes.Compare(test16, to16) <= 0 {
return true
}
return false
}
// https://ip.awk.sh/api.php?ip=
//通过淘宝接口查询相关信息
func TabaoAPI(ip string) *IPInfo {
//url := "http://ip.taobao.com/service/getIpInfo.php?ip="
url := "https://ip.awk.sh/api.php?ip="
url += ip
resp, err := http.Get(url)
if err != nil {
return nil
}
defer resp.Body.Close()
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}
var result IPInfo
if err := json.Unmarshal(out, &result); err != nil {
return nil
}
return &result
}
func JudgeIp(ip string) bool {
if m, _ := regexp.MatchString("^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$", ip); !m {
err := fmt.Errorf("Not validate IP address :"+string(ip) )
fmt.Println(err)
return false
}
return true
}
// 通过map主键唯一的特性过滤重复元素
func RemoveRepByMap(slc []string) []string {
result := []string{}
tempMap := map[string]string{} // 存放不重复主键
for _, e := range slc{
l := len(tempMap)
tempMap[e] = ""
if len(tempMap) != l{ // 加入map后,map长度变化,则元素不重复
result = append(result, e)
}
}
return result
}
var ipv4count = 0
var intranetcount =0
var notipcount =0
//从文本获取ip进行整理
func ReadIPStatic(filename string,ipMap map[string]int) map[string]int {
f, err := os.Open(filename)
if err != nil {
fmt.Print(err)
}
buf := bufio.NewReader(f)
for {
line,err := buf.ReadString('\n')
if err!=nil|| io.EOF == err{
break
}
//line = strings.TrimSpace(line)
if line=="\n" {continue}
ip := strings.Split(line,"/")[2]
if IsPublicIP(inet_ntoa(InetAtoN(ip))) { //判断是否为ipv4
_, ok := ipMap[ip]
if ok == true {
ipMap[ip]++ //如果已经存在,则value+1
} else {
ipMap[ip] = 1 //如果没有存在,则创建
}
ipv4count++
}else {
intranetcount++
}
}
return ipMap
}
//从counter上获取到的ip,动态分析(未完成) iplong为
func ReadIPdynamic(iplong []string ,ipMap map[string]int) map[string]int {
ipMap["0"] = 0
for i:=0;i< len(iplong);i++ {
ipline := iplong[i]
ip := strings.Split(ipline, "/")[2]
if IsPublicIP(inet_ntoa(InetAtoN(ip))) { //判断是否为ipv4
//fmt.Println(ip)
_, ok := ipMap[ip]
if ok == true {
ipMap[ip]++ //如果已经存在,则value+1
} else {
ipMap[ip] = 1 //如果没有存在,则创建
}
} else {
//fmt.Println(ip)
}
}
return ipMap
}
错误如下http: panic serving 127.0.0.1:65522: runtime error: invalid memory address or nil pointer dereference
goroutine 16 [running]:
net/http.(*conn).serve.func1(0xc0423e2640)
C:/Go/src/net/http/server.go:1697 +0xd7
panic(0x996060, 0xf255b0)
C:/Go/src/runtime/panic.go:491 +0x291
main.handleTask(0xe64140, 0xc04221e0e0, 0xc04211a400)
C:/Users/Administrator.erica-PC/Desktop/IPFS/ipfs-counter-master/server.go:35 +0x2a2
net/http.HandlerFunc.ServeHTTP(0xb88ad8, 0xe64140, 0xc04221e0e0, 0xc04211a400)
C:/Go/src/net/http/server.go:1918 +0x4b
net/http.(*ServeMux).ServeHTTP(0xf4ab20, 0xe64140, 0xc04221e0e0, 0xc04211a400)
C:/Go/src/net/http/server.go:2254 +0x137
net/http.serverHandler.ServeHTTP(0xc04206aea0, 0xe64140, 0xc04221e0e0, 0xc04211a400)
C:/Go/src/net/http/server.go:2619 +0xbb
net/http.(*conn).serve(0xc0423e2640, 0xe64b80, 0xc042106440)
C:/Go/src/net/http/server.go:1801 +0x724
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2720 +0x28f
有疑问加站长微信联系(非本文作者)