Ejemplo n.º 1
0
func main() {
	flows, err := conntrack.Flows()
	if err != nil {
		panic(err)
	}

	encoder := gob.NewEncoder(os.Stdout)
	encoder.Encode(flows)
}
Ejemplo n.º 2
0
func main() {
	flag.Parse()

	if *displayVersion {
		fmt.Println("Version " + Version)
		os.Exit(0)
	}

	which := conntrack.SNATFilter | conntrack.DNATFilter

	if *onlySNAT {
		which = conntrack.SNATFilter
	}

	if *onlyDNAT {
		which = conntrack.DNATFilter
	}

	if *onlyLocal {
		which = conntrack.LocalFilter
	}

	if *onlyRouted {
		which = conntrack.RoutedFilter
	}

	flows, err := conntrack.Flows()
	if err != nil {
		panic(err)
	}

	tabWriter := &tabwriter.Writer{}
	tabWriter.Init(os.Stdout, 0, 0, 4, ' ', 0)

	if !*noHeader {
		fmt.Fprintln(tabWriter, "Proto\tSource Address\tDestination Address\tState")
	}

	filteredFlows := flows.FilterByType(which)
	if *protocol != "" {
		protoent, ok := netdb.GetProtoByName(*protocol)
		if !ok {
			// TODO descriptive error message
			panic("Unknown protocol")
		}
		filteredFlows = filteredFlows.FilterByProtocol(protoent)
	}

	if *sourceHost != "" {
		sourceIP := net.ParseIP(*sourceHost) // TODO support hostnames
		filteredFlows = filteredFlows.Filter(func(flow conntrack.Flow) bool {
			return flow.Original.Source.Equal(sourceIP)
		})
	}

	if *destinationHost != "" {
		destinationIP := net.ParseIP(*destinationHost) // TODO support hostnames
		filteredFlows = filteredFlows.Filter(func(flow conntrack.Flow) bool {
			return flow.Original.Destination.Equal(destinationIP)
		})
	}

	for _, flow := range filteredFlows {
		sHostname := resolve(flow.Original.Source, *noResolve)
		dHostname := resolve(flow.Original.Destination, *noResolve)
		sPortName := portToName(int(flow.Original.SPort), flow.Protocol.Name)
		dPortName := portToName(int(flow.Original.DPort), flow.Protocol.Name)
		fmt.Fprintf(tabWriter, "%s\t%s:%s\t%s:%s\t%s\n",
			flow.Protocol.Name,
			sHostname,
			sPortName,
			dHostname,
			dPortName,
			flow.State,
		)
	}
	tabWriter.Flush()
}