feat(arp): 自 go-hua 迁入 ARP 表查询与定时刷新缓存
- IP→MAC 查询(AutoRefresh 后台刷新/Search/Table), 新增依赖 mostlygeek/arp - 附测试、examples/arp 例程与包 README
This commit is contained in:
+57
@@ -0,0 +1,57 @@
|
||||
// Package arp 提供系统 ARP 表读取与 IP→MAC 查询,内置定时刷新缓存。
|
||||
package arp
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// ArpTable ARP 表:IP → MAC 地址映射
|
||||
type ArpTable map[string]string
|
||||
|
||||
var (
|
||||
stop = make(chan struct{})
|
||||
arpCache = &cache{
|
||||
table: make(ArpTable),
|
||||
}
|
||||
)
|
||||
|
||||
// AutoRefresh 开启定时自动刷新 ARP 缓存
|
||||
// t 为刷新间隔
|
||||
func AutoRefresh(t time.Duration) {
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-time.After(t):
|
||||
arpCache.Refresh()
|
||||
case <-stop:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// StopAutoRefresh 停止定时自动刷新
|
||||
func StopAutoRefresh() {
|
||||
stop <- struct{}{}
|
||||
}
|
||||
|
||||
// CacheUpdate 手动刷新一次 ARP 缓存
|
||||
func CacheUpdate() {
|
||||
arpCache.Refresh()
|
||||
}
|
||||
|
||||
// CacheLastUpdate 返回缓存最近一次刷新时间
|
||||
func CacheLastUpdate() time.Time {
|
||||
return arpCache.Updated
|
||||
}
|
||||
|
||||
// CacheUpdateCount 返回缓存累计刷新次数
|
||||
func CacheUpdateCount() int {
|
||||
return arpCache.UpdatedCount
|
||||
}
|
||||
|
||||
// Search looks up the MAC address for an IP address
|
||||
// in the arp table
|
||||
func Search(ip string) string {
|
||||
return arpCache.Search(ip)
|
||||
}
|
||||
Reference in New Issue
Block a user