gistfile1.txt
· 1.1 KiB · Text
原始檔案
ed -n '/^def fw_events/,/^def [a-z]/p' /etc/wireguard/wgctl/core/json_helper.py
def fw_events(file, filter_ip, filter_type, clients_dir, net_file, limit):
"""
Format firewall drop events with dedup, counts, and service annotation.
Output per line: ts|client|dest_ip|dest_port|proto|service_name|count
"""
import glob
from datetime import datetime
proto_map = {1: 'icmp', 6: 'tcp', 17: 'udp'}
# Build ip->name map
ip_to_name = {}
for conf in glob.glob(f"{clients_dir}/*.conf"):
name = os.path.basename(conf).replace('.conf', '')
try:
with open(conf) as f:
for line in f:
if line.startswith('Address'):
ip = line.split('=')[1].strip().split('/')[0]
ip_to_name[ip] = name
except Exception:
pass
# Load net services for reverse lookup
net_data = {}
if os.path.exists(net_file):
try:
with open(net_file) as f:
net_data = json.load(f)
except Exception:
pass
| 1 | ed -n '/^def fw_events/,/^def [a-z]/p' /etc/wireguard/wgctl/core/json_helper.py |
| 2 | def fw_events(file, filter_ip, filter_type, clients_dir, net_file, limit): |
| 3 | """ |
| 4 | Format firewall drop events with dedup, counts, and service annotation. |
| 5 | Output per line: ts|client|dest_ip|dest_port|proto|service_name|count |
| 6 | """ |
| 7 | import glob |
| 8 | from datetime import datetime |
| 9 | proto_map = {1: 'icmp', 6: 'tcp', 17: 'udp'} |
| 10 | |
| 11 | # Build ip->name map |
| 12 | ip_to_name = {} |
| 13 | for conf in glob.glob(f"{clients_dir}/*.conf"): |
| 14 | name = os.path.basename(conf).replace('.conf', '') |
| 15 | try: |
| 16 | with open(conf) as f: |
| 17 | for line in f: |
| 18 | if line.startswith('Address'): |
| 19 | ip = line.split('=')[1].strip().split('/')[0] |
| 20 | ip_to_name[ip] = name |
| 21 | except Exception: |
| 22 | pass |
| 23 | |
| 24 | # Load net services for reverse lookup |
| 25 | net_data = {} |
| 26 | if os.path.exists(net_file): |
| 27 | try: |
| 28 | with open(net_file) as f: |
| 29 | net_data = json.load(f) |
| 30 | except Exception: |
| 31 | pass |
| 32 | |
| 33 |