Last active
July 9, 2024 08:01
-
-
Save goodarzi/7695b176d1efc84e0fa3f2a3c5b82b30 to your computer and use it in GitHub Desktop.
nftables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# ] | |
# { | |
# "val": "192.168.1.23", | |
# "upload": { | |
# "packets": 613376, | |
# "bytes": 111798380, | |
# "megabytes": 106 | |
# }, | |
# "download": { | |
# "packets": 165962, | |
# "bytes": 21587933, | |
# "megabytes": 20 | |
# } | |
# } | |
# ] | |
nft -j list sets inet | jq ' | |
[.nftables[].set | [select(.name == "download").elem[].elem | | |
{val, download: { packets: .counter.packets, bytes: .counter.bytes, megabytes: (.counter.bytes/1024/1024 | floor)}}] as $download | | |
[select(.name == "upload").elem[].elem | { val, upload: { packets: .counter.packets, bytes: .counter.bytes, megabytes: (.counter.bytes/1024/1024 | floor)}}] | ($download + .)[]] | [group_by(.val)[] | add]' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# output schema | |
# [ | |
# { | |
# "val": "192.168.1.1", | |
# "upload": { | |
# "packets": 1, | |
# "bytes": 8 | |
# }, | |
# "download": { | |
# "packets": 1, | |
# "bytes": 8 | |
# } | |
# } | |
# ] | |
nft -j list sets inet | jq ' | |
[.nftables[].set | | |
[select(.name == "download").elem[].elem | {val, download: .counter}] as $download | | |
[select(.name == "upload").elem[].elem | { val, upload: .counter}] | ($download + .)[]] | | |
[group_by(.val)[] | add]' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# output schema: | |
# [ | |
# { | |
# "192.168.1.1": { | |
# "upload": { | |
# "packets": 1, | |
# "bytes": 8 | |
# }, | |
# "download": { | |
# "packets": 1, | |
# "bytes": 8 | |
# } | |
# } | |
# } | |
# ] | |
nft -j list sets inet | jq ' | |
[.nftables[].set | | |
[select(.name == "download").elem[].elem | {val, download: .counter}] as $download | | |
[select(.name == "upload").elem[].elem | { val, upload: .counter}] | ($download + .)[]] | | |
[group_by(.val)[] | add] | [.[] | {(.val| tostring): {upload, download}}]' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/sbin/nft -f | |
define DEV_LAN = br0 | |
define NET_LAN = 192.168.1.1/24 | |
define NET_LAN6 = fe80::/10 | |
add table inet global | |
flush table inet global | |
table inet global { | |
set upload { | |
typeof ip saddr | |
flags dynamic | |
counter | |
} | |
set download { | |
typeof ip daddr | |
flags dynamic | |
counter | |
} | |
set upload6 { | |
typeof ip6 saddr | |
flags dynamic | |
counter | |
} | |
set download6 { | |
typeof ip6 daddr | |
flags dynamic | |
counter | |
} | |
chain mangle_output { | |
type route hook output priority mangle; policy accept; | |
oifname $DEV_LAN ip daddr $NET_LAN update @download { ip daddr counter } | |
oifname $DEV_LAN ip6 daddr $NET_LAN6 update @download6 { ip6 daddr counter } | |
} | |
chain mangle_prerouting { | |
type filter hook prerouting priority mangle; policy accept; | |
iifname $DEV_LAN ip saddr $NET_LAN update @upload { ip saddr counter } | |
iifname $DEV_LAN ip6 saddr $NET_LAN6 update @upload6 { ip6 saddr counter } | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment