feat(snmp,ui): устройства за портом (MAC/IP) в таблице 2
Добавил сбор FDB/ARP по SNMP (BRIDGE-MIB + ipNetToMedia), хранение и API /port-devices. В UI Таблица 2 теперь показывает MAC/IP за выбранным портом после клика по строке Таблицы 1. Made-with: Cursor
This commit is contained in:
@@ -69,6 +69,8 @@ type Store interface {
|
||||
ListLLDPResults(scanID string, limit int) []LLDPResult
|
||||
SaveInterfaceResult(scanID string, result InterfaceResult) error
|
||||
ListInterfaceResults(scanID string, filterIP string, limit int) []InterfaceResult
|
||||
SavePortDeviceResult(scanID string, result PortDeviceResult) error
|
||||
ListPortDeviceResults(scanID string, filterIP string, filterIfIndex int, limit int) []PortDeviceResult
|
||||
// PurgeAllScanData удаляет все сканы и связанные строки (хосты, порты, SNMP, LLDP, интерфейсы).
|
||||
PurgeAllScanData() error
|
||||
}
|
||||
@@ -115,6 +117,15 @@ type InterfaceResult struct {
|
||||
CheckedAt time.Time `json:"checked_at"`
|
||||
}
|
||||
|
||||
// PortDeviceResult — MAC/IP, обнаруженные за конкретным портом (ifIndex) через BRIDGE-MIB + ARP.
|
||||
type PortDeviceResult struct {
|
||||
IP string `json:"ip"`
|
||||
IfIndex int `json:"if_index"`
|
||||
MAC string `json:"mac"`
|
||||
LearnedIP string `json:"learned_ip"`
|
||||
CheckedAt time.Time `json:"checked_at"`
|
||||
}
|
||||
|
||||
type MemoryStore struct {
|
||||
mu sync.RWMutex
|
||||
jobs map[string]ScanJob
|
||||
@@ -123,6 +134,7 @@ type MemoryStore struct {
|
||||
snmp map[string][]SNMPResult
|
||||
lldp map[string][]LLDPResult
|
||||
ifaces map[string][]InterfaceResult
|
||||
fdb map[string][]PortDeviceResult
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
@@ -133,6 +145,7 @@ func NewMemoryStore() *MemoryStore {
|
||||
snmp: make(map[string][]SNMPResult),
|
||||
lldp: make(map[string][]LLDPResult),
|
||||
ifaces: make(map[string][]InterfaceResult),
|
||||
fdb: make(map[string][]PortDeviceResult),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -341,6 +354,7 @@ func (s *MemoryStore) PurgeAllScanData() error {
|
||||
s.snmp = make(map[string][]SNMPResult)
|
||||
s.lldp = make(map[string][]LLDPResult)
|
||||
s.ifaces = make(map[string][]InterfaceResult)
|
||||
s.fdb = make(map[string][]PortDeviceResult)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -373,6 +387,52 @@ func (s *MemoryStore) ListInterfaceResults(scanID string, filterIP string, limit
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *MemoryStore) SavePortDeviceResult(scanID string, result PortDeviceResult) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if _, ok := s.jobs[scanID]; !ok {
|
||||
return errors.New("scan not found")
|
||||
}
|
||||
s.fdb[scanID] = append(s.fdb[scanID], result)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) ListPortDeviceResults(scanID string, filterIP string, filterIfIndex int, limit int) []PortDeviceResult {
|
||||
if limit <= 0 {
|
||||
limit = 20000
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
items := s.fdb[scanID]
|
||||
filtered := make([]PortDeviceResult, 0, len(items))
|
||||
for _, r := range items {
|
||||
if filterIP != "" && r.IP != filterIP {
|
||||
continue
|
||||
}
|
||||
if filterIfIndex > 0 && r.IfIndex != filterIfIndex {
|
||||
continue
|
||||
}
|
||||
filtered = append(filtered, r)
|
||||
}
|
||||
sort.Slice(filtered, func(i, j int) bool {
|
||||
if filtered[i].IfIndex != filtered[j].IfIndex {
|
||||
return filtered[i].IfIndex < filtered[j].IfIndex
|
||||
}
|
||||
if filtered[i].MAC != filtered[j].MAC {
|
||||
return filtered[i].MAC < filtered[j].MAC
|
||||
}
|
||||
return filtered[i].LearnedIP < filtered[j].LearnedIP
|
||||
})
|
||||
if len(filtered) <= limit {
|
||||
out := make([]PortDeviceResult, len(filtered))
|
||||
copy(out, filtered)
|
||||
return out
|
||||
}
|
||||
out := make([]PortDeviceResult, limit)
|
||||
copy(out, filtered[:limit])
|
||||
return out
|
||||
}
|
||||
|
||||
func applyDefaultOptions(opts *ScanOptions) {
|
||||
if opts.PingTimeoutMS <= 0 {
|
||||
opts.PingTimeoutMS = 700
|
||||
|
||||
Reference in New Issue
Block a user