github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/example/main.go (about)

     1  package main
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/sirupsen/logrus"
     8  
     9  	"github.com/zartbot/goflow/service/ciscoavc"
    10  	"github.com/zartbot/goflow/service/ciscoeta"
    11  	"github.com/zartbot/goflow/service/flowinfo"
    12  	"github.com/zartbot/goflow/service/geoipmap"
    13  	"github.com/zartbot/goflow/service/identity"
    14  	"github.com/zartbot/goflow/service/optiontemplatemap"
    15  
    16  	"github.com/zartbot/goflow/datarecord"
    17  	"github.com/zartbot/goflow/iedb"
    18  	"github.com/zartbot/goflow/ipfix"
    19  	"github.com/zartbot/goflow/netflowv9"
    20  )
    21  
    22  func main() {
    23  
    24  	ipaddr := "0.0.0.0"
    25  
    26  	iedb.ReadIANA("./config/database/informationElement/iana_ie.csv")
    27  	iedb.ReadCiscoIE("./config/database/informationElement/cisco_ie.csv")
    28  	iedb.ReadCiscoIE("./config/database/informationElement/key_ie.csv")
    29  	iedb.ShowIEDB()
    30  
    31  	logrus.Info("Starting Identification service....")
    32  	identity.LoadServerInfo("./config/database/server/server.csv")
    33  	identity.LoadReputationInfo("./config/database/security/reputation.csv")
    34  	identity.LoadBotnetInfo("./config/database/security/spam_bot_net.csv")
    35  	go identity.Run()
    36  
    37  	//g, geoServiceErr := geoipmap.NewGeoIPCollector("./config/database/geolocation/city.mmdb", "./config/database/geolocation/asn.mmdb", 31.123, 111.11)
    38  
    39  	logrus.Info("Starting Netflow Collectors.....")
    40  
    41  	//IPFIXDataFrameChan is dataframe output channel for future streaming process.
    42  	var IPFIXDataFrameChan = make(chan *datarecord.DataFrame, 10000)
    43  	var NFv9DataFrameChan = make(chan *datarecord.DataFrame, 10000)
    44  
    45  	//input Collector
    46  	var ipfixCollector []*ipfix.IPFIXCollector
    47  	for _, port := range []int{4739, 4740} {
    48  		c := ipfix.NewIPFIXCollector(ipaddr, port, IPFIXDataFrameChan, 1)
    49  		ipfixCollector = append(ipfixCollector, c)
    50  	}
    51  
    52  	var nfv9Collector []*netflowv9.NetflowV9Collector
    53  	for _, port := range []int{2055, 2056} {
    54  		c := netflowv9.NewNFV9Collector(ipaddr, port, NFv9DataFrameChan, 1)
    55  		nfv9Collector = append(nfv9Collector, c)
    56  	}
    57  
    58  	var wg sync.WaitGroup
    59  	wg.Add(2)
    60  
    61  	for _, c := range ipfixCollector {
    62  		go c.Run()
    63  	}
    64  
    65  	for _, c := range nfv9Collector {
    66  		go c.Run()
    67  	}
    68  
    69  	for {
    70  		select {
    71  		case e1 := <-IPFIXDataFrameChan:
    72  			RecordMap(e1)
    73  			e1.Print("IPFIX")
    74  
    75  		case e2 := <-NFv9DataFrameChan:
    76  			RecordMap(e2)
    77  			e2.Print("NFv9")
    78  		}
    79  	}
    80  
    81  	wg.Wait()
    82  
    83  }
    84  
    85  func RecordMap(d *datarecord.DataFrame) {
    86  	d.TypeAssertion()
    87  	createAt := time.Unix(int64(d.ExportTime), 0)
    88  	for _, value := range d.Record {
    89  		value["CreateAt"] = createAt
    90  		value["AgentID"] = d.AgentID
    91  		value["Type"] = d.Type
    92  		//Optional Template Correlation
    93  		optiontemplatemap.UpdateInterfaceMap(value, d.AgentID)
    94  		optiontemplatemap.UpdateAppMap(value, d.AgentID)
    95  		optiontemplatemap.UpdateCiscoVarString(value, d.AgentID)
    96  		optiontemplatemap.UpdateC3PLMap(value, d.AgentID)
    97  		optiontemplatemap.UpdateDropCauseMap(value, d.AgentID)
    98  		optiontemplatemap.UpdateViptelaTLOCMap(value, d.AgentID)
    99  		optiontemplatemap.UpdateFWEventMap(value, d.AgentID)
   100  		optiontemplatemap.UpdateFWZonePairMap(value, d.AgentID)
   101  		optiontemplatemap.UpdateFWClassMapMap(value, d.AgentID)
   102  
   103  		//Additional Service
   104  		flowinfo.UpdateFlowInfo(value, true)
   105  		identity.AddressLookUP(value, true)
   106  		ciscoavc.ExtendField(value)
   107  		ciscoeta.ExtendField(value)
   108  	}
   109  }
   110  
   111  func GeoRecordMap(d *datarecord.DataFrame, g *geoipmap.GeoIPCollector) {
   112  	d.TypeAssertion()
   113  	createAt := time.Unix(int64(d.ExportTime), 0)
   114  	for _, value := range d.Record {
   115  		value["CreateAt"] = createAt
   116  		value["AgentID"] = d.AgentID
   117  		value["Type"] = d.Type
   118  		//Optional Template Correlation
   119  		optiontemplatemap.UpdateInterfaceMap(value, d.AgentID)
   120  		optiontemplatemap.UpdateAppMap(value, d.AgentID)
   121  		optiontemplatemap.UpdateCiscoVarString(value, d.AgentID)
   122  		optiontemplatemap.UpdateC3PLMap(value, d.AgentID)
   123  		optiontemplatemap.UpdateDropCauseMap(value, d.AgentID)
   124  		optiontemplatemap.UpdateViptelaTLOCMap(value, d.AgentID)
   125  		optiontemplatemap.UpdateFWEventMap(value, d.AgentID)
   126  		optiontemplatemap.UpdateFWZonePairMap(value, d.AgentID)
   127  		optiontemplatemap.UpdateFWClassMapMap(value, d.AgentID)
   128  
   129  		//Additional Service
   130  		flowinfo.UpdateFlowInfo(value, true)
   131  		identity.AddressLookUP(value, true)
   132  		geoipmap.UpdateGeoLocationInfo(value, g)
   133  		ciscoavc.ExtendField(value)
   134  		ciscoeta.ExtendField(value)
   135  	}
   136  }