【毕设日记】(8)go语言基于gopacket+fyne简单抓包工具实现之包MetaData显示UI

PkgInfo.go

package ui

import (
	"fyne.io/fyne/v2"
	fyne2 "fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/google/gopacket"

	//"github.com/google/gopacket"
)
var (
	PkgInfoWidget fyne2.Widget
	PkgCharWidget fyne2.Widget
	PkgMetaData [][]string
	PkgCharData [][]string
)
func loadPkgInfo() (*fyne2.Container) {
	speparator := widget.NewSeparator()
	PkgInfoWidget=widget.NewTable(func() (int, int) {
		if len(PkgMetaData)!=0{
			return len(PkgMetaData),len(PkgMetaData[0])
		}
		return 0,0
	},
		func() fyne.CanvasObject {
			return widget.NewLabel("00")
		},
		func(tableCellID widget.TableCellID, object fyne.CanvasObject) {
			l:=object.(*widget.Label)
			l.SetText(PkgMetaData[tableCellID.Row][tableCellID.Col])
		})
	PkgCharWidget=widget.NewTable(func() (int, int) {
		if len(PkgCharData)!=0{
			return len(PkgCharData),len(PkgCharData[0])
		}
		return 0,0
	},
		func() fyne.CanvasObject {
			return widget.NewLabel("00")
		},
		func(tableCellID widget.TableCellID, object fyne.CanvasObject) {
			l:=object.(*widget.Label)
			l.SetText(PkgCharData[tableCellID.Row][tableCellID.Col])
		})
	PkgInfoWidget.(*widget.Table).OnSelected= func(id widget.TableCellID) {
		PkgCharWidget.(*widget.Table).Select(id)
	}
	PkgCharWidget.(*widget.Table).OnSelected= func(id widget.TableCellID) {
		PkgInfoWidget.(*widget.Table).Select(id)
	}
	InfoContainer:=container.NewBorder(nil,nil,nil,nil,PkgInfoWidget)
	CharContainer:=container.NewBorder(nil,nil,nil,nil,PkgCharWidget)
	InfoContainer.Resize(fyne2.NewSize(560,280))
	CharContainer.Resize(fyne2.NewSize(560,280))
	InfoContainer.Move(fyne2.NewPos(0,0))
	CharContainer.Move(fyne2.NewPos(600,0))
	InfoAndCharContainer:=container.NewWithoutLayout(InfoContainer,CharContainer)
	PkgInfoContainer := container.NewBorder(nil, speparator, nil,nil,InfoAndCharContainer)
	PkgInfoContainer.Resize(fyne2.NewSize(1600, 240))
	return PkgInfoContainer
}
func NewPkgInfoData(packet gopacket.Packet)  {
	PkgMetaData=PkgBytes2StringSlice(packet.Data())
	PkgCharData=PkgBytes2AsciiSlice(packet.Data())
	PkgInfoWidget.Refresh()
	PkgCharWidget.Refresh()
}
func PkgBytes2String(PkgBytes []byte) string {
	res := ""
	for i, v := range PkgBytes {
		if i != 0 && i%8 == 0 {
			res += "   "
		}
		if i != 0 && i%16 == 0 {
			res += "\n"
		}
		res += byte2Hex(v) + " "
	}
	return res
}
func PkgBytes2AsciiSlice(PkgBytes []byte) [][]string {
	res:=[][]string{}
	for i:=0;i<len(PkgBytes);{
		temp:=[]string{}
		c:=0
		for i<len(PkgBytes)&&c!=16{
			temp= append(temp, Byte2AscllString(PkgBytes[i]))
			if i%16==7{
				temp= append(temp, " ")
			}
			i++
			c++
		}
		if c==16{
			res= append(res, temp)
			c=0
		}else if i==len(PkgBytes){  //如果最后不足一行则补齐空string
			for len(temp)<17{
				temp= append(temp, " ")
			}
			res= append(res, temp)
		}
	}
	return res
}

func Byte2AscllString(b byte) string {
	bint:=int(b)
	if bint>=33&&bint<=126{
		return string(b)

	}
	return "."

}
func  PkgBytes2StringSlice(PkgBytes []byte)[][]string  {
	res:=[][]string{}
	for i:=0;i<len(PkgBytes);{
		temp:=[]string{}
		c:=0
		for i<len(PkgBytes)&&c!=16{
			temp= append(temp, byte2Hex(PkgBytes[i]))
			if i%16==7{
				temp= append(temp, " ")
			}
			i++
			c++
		}
		if c==16{
			res= append(res, temp)
			c=0
		}else if i==len(PkgBytes){  //如果最后不足一行则补齐空string
			for len(temp)<17{
				temp= append(temp, " ")
			}
			res= append(res, temp)
		}
	}
	return res
}
func byte2Hex(b byte) string {
	care := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}
	bb := int(b)
	res := ""
	if b == 0 {
		return "00"
	}
	if b < 16 {
		return "0" + care[bb%16]
	}
	for bb > 0 {
		res = care[bb%16] + res
		bb /= 16
	}
	return res
}

效果图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值