luci-peeraddr 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/sh
  2. NL="
  3. "
  4. function ifaces_by_device() {
  5. ubus call network.interface dump 2>/dev/null | \
  6. jsonfilter -e "@.interface[@.device='$1' || @.l3_device='$1'].interface"
  7. }
  8. function device_by_addr() {
  9. set -- $(ip route get "$1" ${2:+from "$2"} 2>/dev/null)
  10. echo "$5"
  11. }
  12. for inbound_device in $(device_by_addr "$REMOTE_ADDR" "$SERVER_ADDR"); do
  13. inbound_devices="$inbound_device"
  14. inbound_interfaces=""
  15. for iface in $(ifaces_by_device "$inbound_device"); do
  16. inbound_interfaces="${inbound_interfaces:+$inbound_interfaces$NL}$iface"
  17. for peeraddr in $(uci get "network.$iface.peeraddr"); do
  18. for ipaddr in $(resolveip -t 1 "$peeraddr" 2>/dev/null); do
  19. for peerdev in $(device_by_addr "$ipaddr"); do
  20. for iface in $(ifaces_by_device "$peerdev"); do
  21. inbound_devices="${inbound_devices:+$inbound_devices$NL}$peerdev"
  22. inbound_interfaces="${inbound_interfaces:+$inbound_interfaces$NL}$iface"
  23. done
  24. done
  25. done
  26. done
  27. done
  28. done
  29. inbound_devices="$(echo "$inbound_devices" | sort -u | sed ':a;N;$!ba;s/\n/", "/g')"
  30. inbound_interfaces="$(echo "$inbound_interfaces" | sort -u | sed ':a;N;$!ba;s/\n/", "/g')"
  31. cat <<JSON
  32. {
  33. "remote_addr": "$REMOTE_ADDR",
  34. "server_addr": "$SERVER_ADDR",
  35. "inbound_devices": [ ${inbound_devices:+\"$inbound_devices\"} ],
  36. "inbound_interfaces": [ ${inbound_interfaces:+\"$inbound_interfaces\"} ]
  37. }
  38. JSON