Example #1
0
File: main.go Project: qgweb/gopro
func getIp(w http.ResponseWriter, r *http.Request) {
	ip := r.URL.Query().Get("sspip")
	if ip == "" {
		w.Write([]byte(""))
		return
	}
	loc, err := ip17mon.Find(ip)
	if err != nil {
		log.Println(err)
		w.Write([]byte(""))
		return
	}

	region := ""
	city := ""
	if v, ok := area[loc.Region]; ok {
		region = v
	}

	if v, ok := area[loc.City]; ok {
		city = v
	} else {
		for k, v := range area {
			if strings.Contains(loc.City, k) {
				city = v
				break
			}
		}
	}

	w.Write([]byte(region + "_" + city))
}
Example #2
0
func (th *TailHandler) Ip(ip string) (string, string) {
	th.lock.Lock()
	defer th.lock.Unlock()
	if loc, err := ip17mon.Find(ip); err == nil {
		return loc.Region, loc.City
	}
	return "", ""
}
Example #3
0
func main() {
	if len(os.Args) > 1 {
		if loc, err := ip17mon.Find(os.Args[1]); err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", os.Args[1], err)
			os.Exit(1)
		} else {
			fmt.Println(loc.Country, loc.Region, loc.City, loc.Isp)
		}
	} else {
		stdin()
	}
}
Example #4
0
func main() {
	flag.Parse()

	f, err := os.Open(file)
	if err != nil {
		fmt.Printf("打开日志[%s]出错: %s\n", file, err.Error())
		return
	}

	if err = ip17mon.Init(path); err != nil {
		fmt.Printf("初始化 ip 库失败: %s\n", err.Error())
		return
	}

	buf, output := bufio.NewReader(f), make([]string, 0, batch)
	for {
		ip, err := buf.ReadString('\n')
		if err != nil {
			if err != io.EOF {
				fmt.Printf("读取日志[%s]出错: %s\n", file, err.Error())
				return
			}
			break
		}

		ip = strings.TrimSpace(ip)
		loc, err := ip17mon.Find(ip)
		if err != nil {
			output = append(output, fmt.Sprintf("%s,%s,%s,%s,%s", ip, err.Error(), "", "", ""))
			continue
		}

		output = append(output, fmt.Sprintf("%s,%s,%s,%s,%s", ip, loc.Country, loc.Region, loc.City, loc.Isp))
		if len(output) < batch {
			continue
		}

		fmt.Println(strings.Join(output, "\n"))
		output = output[:0]
	}

	if len(output) > 0 {
		fmt.Println(strings.Join(output, "\n"))
	}
}
Example #5
0
func printHop(hop traceroute.TracerouteHop) {
	addr := fmt.Sprintf("%v.%v.%v.%v", hop.Address[0], hop.Address[1], hop.Address[2], hop.Address[3])
	hostOrAddr := addr
	if hop.Host != "" {
		hostOrAddr = hop.Host
	}

	addrLoc, err := ip17mon.Find(fmt.Sprintf("%v", addr))
	if err != nil {
		fmt.Println("ip17mon error:", err)
		return
	}

	if hop.Success {
		fmt.Printf("%-3d %v (%v) %s  %v\n", hop.TTL, hostOrAddr, addr, LocString(addrLoc), hop.ElapsedTime)
	} else {
		fmt.Printf("%-3d *\n", hop.TTL)
	}
}
Example #6
0
func stdin() {
	scanner := bufio.NewScanner(os.Stdin)

	for scanner.Scan() {
		ip := scanner.Text()
		if loc, err := ip17mon.Find(ip); err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", ip, err)
		} else {
			fmt.Println(ip, loc.Country, loc.Region, loc.City, loc.Isp)
		}
	}

	if err := scanner.Err(); err != nil {
		fmt.Fprintln(os.Stderr, "Failed:", err)
		os.Exit(1)
	}

	return
}
Example #7
0
func main() {
	var m = flag.Int("m", traceroute.DEFAULT_MAX_HOPS, `Set the max time-to-live (max number of hops) used in outgoing probe packets (default is 64)`)
	var q = flag.Int("q", 1, `Set the number of probes per "ttl" to nqueries (default is one probe).`)

	flag.Parse()
	host := flag.Arg(0)
	options := traceroute.TracerouteOptions{}
	options.SetRetries(*q - 1)
	options.SetMaxHops(*m + 1)

	ipAddr, err := net.ResolveIPAddr("ip", host)
	if err != nil {
		return
	}

	loc, err := ip17mon.Find(fmt.Sprintf("%v", ipAddr))
	if err != nil {
		fmt.Println("ip17mon error:", err)
		return
	}

	fmt.Printf("traceroute to %v (%v) %s, %v hops max, %v byte packets Location\n", host, ipAddr, LocString(loc), options.MaxHops(), options.PacketSize())

	c := make(chan traceroute.TracerouteHop, 0)
	go func() {
		for {
			hop, ok := <-c
			if !ok {
				fmt.Println()
				return
			}
			printHop(hop)
		}
	}()

	_, err = traceroute.Traceroute(host, &options, c)
	if err != nil {
		fmt.Printf("Error: ", err)
	}
}
Example #8
0
func (b *BaseData) loadData(typ string) {
	b.lock.RLock()
	defer b.lock.RUnlock()
	ms, c := db.Connect(DB, C_REQUEST)
	c.EnsureIndexKey("time")
	defer ms.Close()

	now := tm.New(time.Now())
	var Begin, End time.Time
	if typ == TODAY {
		Begin = now.BeginningOfDay()
		End = now.EndOfDay()
	} else if typ == YESTERDAY {
		Begin = now.BeginningOfDay().Add(-24 * time.Hour)
		End = now.EndOfDay().Add(-24 * time.Hour)
	}
	if typ == TODAY {
		err := c.Find(nil).Sort("-time").Skip(0).Limit(pageCount).All(&b.Latest)
		if err != nil {
			log.Error(err)
		}
	}
	count, err := c.Find(bson.M{"time": bson.M{"$gte": Begin, "$lt": End}}).Count()
	if err != nil {
		log.Error(err)
	}
	b.PV[typ] = count
	var sessions []string
	err = c.Find(bson.M{"time": bson.M{"$gte": Begin, "$lt": End}}).Distinct("sessionid", &sessions)
	if err != nil {
		log.Error(err)
	}
	b.UV[typ] = len(sessions)
	var ips []string
	err = c.Find(bson.M{"time": bson.M{"$gte": Begin, "$lt": End}}).Distinct("remoteaddr", &ips)
	if err != nil {
		log.Error(err)
	}
	b.China = make(map[string]*Area)
	b.World = make(map[string]*Area)
	for _, v := range ips {
		info, err := ip17mon.Find(v)
		if err != nil {
			log.Warn(err)
			continue
		}
		if info.Country == "中国" {
			if city := b.China[info.City]; city == nil {
				if info.Region == "台湾" {
					b.China[info.Region] = &Area{Name: info.Region, Value: 1}
					continue
				}
				b.China[info.City] = &Area{Name: info.Region, Value: 1}
			} else {
				city.Value++
			}
		} else {
			if country := b.World[info.Country]; country == nil {
				b.World[info.Country] = &Area{Name: info.Country, Value: 1}
			} else {
				country.Value++
			}
		}
	}

	b.IP[typ] = len(ips)
	var ts []*Request
	err = c.Find(bson.M{"time": bson.M{"$gte": Begin, "$lt": End}}).Select(bson.M{"time": 1}).All(&ts)
	if err != nil {
		log.Error(err)
	}
	b.TimePV[typ] = make([]int, 145)
	for _, v := range ts {
		b.TimePV[typ][ParseTime(v.Time)]++
	}
}