示例#1
0
func resolveIPHandler(w http.ResponseWriter, r *http.Request) {
	path := r.URL.Path
	splits := strings.Split(path, "/")
	ip := net.ParseIP(splits[len(splits)-1])
	// FIXME check error
	hostname := lookup.Resolve(ip, false)
	io.WriteString(w, hostname)
}
示例#2
0
func natJsonHandler(w http.ResponseWriter, r *http.Request) {
	natEntries := getConntrackFlows().FilterByType(conntrack.SNATFilter | conntrack.DNATFilter)
	natEntriesDumbedDown := make([]NATEntry, len(natEntries))

	for index, entry := range natEntries {
		natEntriesDumbedDown[index] = NATEntry{
			entry.Protocol.Name,
			lookup.Resolve(entry.Original.Source, false),
			entry.Original.SPort,
			lookup.Resolve(entry.Original.Destination, false),
			entry.Original.DPort,
			entry.State,
		}
	}

	b, err := json.Marshal(natEntriesDumbedDown)
	if err != nil {
		panic(err)
	}

	w.Write(b)
}
示例#3
0
// FIXME pass on errors
func Clients() ([]Client, error) {
	clients := make([]Client, 0)
	done := make(map[string]bool)

	leases, err := dhcp.Leases()
	if err != nil {
		return nil, err
	}
	for _, lease := range leases {
		if lease.BindingState != "active" {
			continue
		}

		if lease.Hostname == "" {
			lease.Hostname = lookup.Resolve(lease.IP, false)
		}
		clients = append(clients, Client{lease.IP, lease.HardwareAddress, lease.Hostname})
		done[lease.IP.String()] = true
	}

	natEntriesS, _ := nat.GetSNAT(false)
	natEntriesD, _ := nat.GetDNAT(false)
	natEntriesDSwapped := make([]nat.Entry, len(natEntriesD), len(natEntriesD))

	for index, natEntry := range natEntriesD {
		natEntry.SourceAddress, natEntry.SourcePort, natEntry.DestinationAddress, natEntry.DestinationPort = natEntry.DestinationAddress, natEntry.DestinationPort, natEntry.SourceAddress, natEntry.SourcePort
		natEntriesDSwapped[index] = natEntry
	}

	natEntriesCombined := append(natEntriesS, natEntriesDSwapped...)
	for _, natEntry := range natEntriesCombined {
		if done[natEntry.SourceAddress] {
			continue
		}

		ip := natEntry.SourceAddress
		hw, err := lookup.IPToHardwareAddress(net.ParseIP(ip))
		if err != nil {
			return nil, err
		}

		hostname, err := lookup.IPToHostname(net.ParseIP(ip))
		if err != nil {
			return nil, err
		}

		clients = append(clients, Client{net.ParseIP(ip), hw, hostname})

		done[natEntry.SourceAddress] = true
	}

	arpEntries, _ := arp.ARP()
	for _, arpEntry := range arpEntries {
		if done[arpEntry.IP.String()] {
			continue
		}

		hostname, err := lookup.IPToHostname(arpEntry.IP)
		if err != nil {
			return nil, err
		}

		clients = append(clients, Client{arpEntry.IP, arpEntry.HardwareAddress, hostname})

		done[arpEntry.IP.String()] = true
	}
	return clients, nil
}
示例#4
0
func main() {
	tm := monitor.NewMonitor(&traffic.Monitor{}, 500*time.Millisecond)
	ch := make(chan interface{})
	tm.RegisterChannel(ch)
	go tm.Start()

	hosts := make(map[string]string)
	for {
		lines := &lineColorizer{}
		stats := (<-ch).([]traffic.ProgressiveStat)

		os.Stdout.WriteString("\033[2J\033[;H")

		tabWriter := &tabwriter.Writer{}
		tabWriter.Init(lines, 0, 0, 2, ' ', 0)
		// The \b are a messy hack to remove the spaces that the
		// tabwriter inserts because it counts ansi colors as actual
		// characters
		tabWriter.Write([]byte("Host\t         Down\t           Up\t│ Total down\t\b\b\bTotal up\n"))
		lines.colors = append(lines.colors, 255)
		sort.Sort(statSlice(stats))

		for index, stat := range stats {
			var color_index int
			if stat.BPSIn == 0 && stat.BPSOut == 0 {
				color_index = 0
			} else if float64(stat.BPSIn)/maxIn > float64(stat.BPSOut)/maxOut {
				// IN is the culprit
				color_index = int(stat.BPSIn/(maxIn/uint64(len(colors))-1) + 1)
			} else {
				// OUT is the culprit
				color_index = int(stat.BPSOut/(maxOut/uint64(len(colors))-1) + 1)
			}

			if color_index >= len(colors) {
				color_index = len(colors) - 1
			}

			if index == len(stats)-1 {
				lines.colors = append(lines.colors, 255)
				tabWriter.Write([]byte("──────────────────────────────────────────────────────────────┼───────────────────────\n"))
			}

			lines.colors = append(lines.colors, colors[color_index])
			host, ok := hosts[stat.Host]
			if !ok {
				if stat.Host == "total" {
					host = "total"
				} else {
					host = lookup.Resolve(net.ParseIP(stat.Host), false)
					hosts[stat.Host] = host
				}
			}

			tabWriter.Write([]byte(fmt.Sprintf("%-30.30s\t%11s/s\t%11s/s\t\033[39m│ %10s\t%10s\n",
				host,
				formatByteCount(stat.BPSIn),
				formatByteCount(stat.BPSOut),
				formatByteCount(stat.In),
				formatByteCount(stat.Out))))
		}

		// fmt.Print(buf.String())
		tabWriter.Flush()
		os.Stdout.WriteString(lines.String())
	}
}