优化主进程子进程获取参数
This commit is contained in:
22
cap.go
22
cap.go
@@ -29,7 +29,7 @@ func printAvailableInterfaces() {
|
||||
|
||||
// 判断 IP 是否在链表中
|
||||
func isIPInList(ip net.IP) bool {
|
||||
for e := ipList.Front(); e != nil; e = e.Next() {
|
||||
for e := IpList.Front(); e != nil; e = e.Next() {
|
||||
if e.Value.(net.IP).Equal(ip) {
|
||||
return true
|
||||
}
|
||||
@@ -43,10 +43,10 @@ func printPacketInfo(packet gopacket.Packet) {
|
||||
|
||||
if ipLayer != nil {
|
||||
ip, _ := ipLayer.(*layers.IPv4)
|
||||
ipMutex.Lock()
|
||||
defer ipMutex.Unlock()
|
||||
IpMutex.Lock()
|
||||
defer IpMutex.Unlock()
|
||||
if !isIPInList(ip.SrcIP) {
|
||||
ipList.PushBack(ip.SrcIP)
|
||||
IpList.PushBack(ip.SrcIP)
|
||||
log.Printf("\033[31m 已添加源 IP: %s 到链表 \033[0m\n", ip.SrcIP)
|
||||
|
||||
}
|
||||
@@ -54,12 +54,10 @@ func printPacketInfo(packet gopacket.Packet) {
|
||||
}
|
||||
|
||||
func startPacketCapture() {
|
||||
handle, err := pcap.OpenLive(*interfaceName, 65535, true, pcap.BlockForever)
|
||||
handle, err := pcap.OpenLive(*InterfaceName, 65535, true, pcap.BlockForever)
|
||||
if err != nil {
|
||||
log.Fatalf("打开网络接口 %s 出错: %v", *interfaceName, err)
|
||||
log.Fatalf("打开网络接口 %s 出错: %v", *InterfaceName, err)
|
||||
}
|
||||
defer handle.Close()
|
||||
|
||||
defer func() {
|
||||
fmt.Println("清理资源...")
|
||||
if handle != nil {
|
||||
@@ -67,14 +65,14 @@ func startPacketCapture() {
|
||||
}
|
||||
}()
|
||||
|
||||
err = handle.SetBPFFilter(*filter)
|
||||
err = handle.SetBPFFilter(*Protocol)
|
||||
if err != nil {
|
||||
log.Fatalf("设置 BPF 过滤器出错: %v", err)
|
||||
}
|
||||
|
||||
var pcapWriter *pcapgo.Writer
|
||||
if *outputFile != "" {
|
||||
file, err := os.Create(*outputFile)
|
||||
if *PcapFile != "" {
|
||||
file, err := os.Create(*PcapFile)
|
||||
if err != nil {
|
||||
log.Fatalf("创建输出文件出错: %v", err)
|
||||
}
|
||||
@@ -88,7 +86,7 @@ func startPacketCapture() {
|
||||
}
|
||||
|
||||
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
|
||||
fmt.Printf("正在监听网络接口 %s,使用过滤器 '%s'...\n", *interfaceName, *filter)
|
||||
fmt.Printf("正在监听网络接口 %s,使用过滤器 '%s'...\n", *InterfaceName, *Protocol)
|
||||
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
Reference in New Issue
Block a user