packagedatabaseimport("errors""honeypot/internal/types""strings")func(db*Database)GetBlockedAddresses()([]types.BlocklistEntry,error){ifdb.DB==nil{returnnil,errors.New("database is not connected")}query:=` SELECT id, address, timestamp, expires, reason
FROM blocklist
WHERE expires > CURRENT_TIMESTAMP
ORDER BY expires DESC
`rows,err:=db.DB.Query(query)iferr!=nil{returnnil,err}deferrows.Close()varblocklist[]types.BlocklistEntryforrows.Next(){varblocklistEntrytypes.BlocklistEntryiferr:=rows.Scan(&blocklistEntry.ID,&blocklistEntry.Address,&blocklistEntry.Timestamp,&blocklistEntry.Expires,&blocklistEntry.Reason,);err!=nil{returnnil,err}blocklist=append(blocklist,blocklistEntry)}returnblocklist,nil}func(db*Database)GetBlocklistForNet(networkstring,limitint)([]types.BlocklistEntry,error){ifdb.DB==nil{returnnil,errors.New("database is not connected")}_,_,err:=GetIpWhere(network)iferr!=nil{returnnil,err}varargs[]any// We join with the same logic as GetIpWhere but for the 'address' column in blocklist table.// Since GetIpWhere uses 'remote_ip_int' for IP/Subnet matching in honeypot_events,// we need to adapt it for the 'address' column in 'blocklist'.// In 'blocklist' table, 'address' is TEXT.varquerystringifstrings.Contains(network,"/"){// Subnet matching for blocklist entriesquery=` SELECT id, address, timestamp, expires, reason
FROM blocklist
WHERE address::INET <<= ?::INET
ORDER BY timestamp DESC
LIMIT ?
`args=[]any{network,limit}}else{// Single IP matchingquery=` SELECT id, address, timestamp, expires, reason
FROM blocklist
WHERE address = ?
ORDER BY timestamp DESC
LIMIT ?
`args=[]any{network,limit}}rows,err:=db.DB.Query(query,args...)iferr!=nil{returnnil,err}deferrows.Close()varblocklist[]types.BlocklistEntryforrows.Next(){varblocklistEntrytypes.BlocklistEntryiferr:=rows.Scan(&blocklistEntry.ID,&blocklistEntry.Address,&blocklistEntry.Timestamp,&blocklistEntry.Expires,&blocklistEntry.Reason,);err!=nil{returnnil,err}blocklist=append(blocklist,blocklistEntry)}returnblocklist,nil}func(db*Database)GetBlockCounts()(map[string]int,error){query:=` SELECT address, COUNT(*) as count
FROM blocklist
GROUP BY address
ORDER BY count DESC
`rows,err:=db.DB.Query(query)iferr!=nil{returnnil,err}deferrows.Close()counts:=make(map[string]int)forrows.Next(){varaddressstringvarcountintiferr:=rows.Scan(&address,&count);err!=nil{returnnil,err}counts[address]=count}returncounts,nil}