mirror of
https://github.com/Ysurac/openmptcprouter-feeds.git
synced 2025-03-09 15:40:03 +00:00
Add luci-app-packet-capture
This commit is contained in:
parent
19dde3a38a
commit
93694b5983
9 changed files with 489 additions and 2 deletions
64
luci-app-packet-capture/root/usr/libexec/packet_capture
Executable file
64
luci-app-packet-capture/root/usr/libexec/packet_capture
Executable file
|
@ -0,0 +1,64 @@
|
|||
#!/usr/bin/env lua
|
||||
|
||||
local ubus = require "ubus"
|
||||
local fs = require "nixio.fs"
|
||||
|
||||
local conn = ubus.connect()
|
||||
if not conn then
|
||||
error("Failed to connect to ubus")
|
||||
return
|
||||
end
|
||||
|
||||
local args = "-n"
|
||||
local duration = ""
|
||||
|
||||
if arg[1] ~= nil then
|
||||
args = arg[1]
|
||||
if arg[2] ~= "" then
|
||||
duration = arg[2]
|
||||
end
|
||||
end
|
||||
|
||||
local filter = fs.stat("/tmp/tcpdump_filter")
|
||||
if filter then
|
||||
args = args .. " -F /tmp/tcpdump_filter"
|
||||
end
|
||||
|
||||
local ubus_objects = {
|
||||
tcpdump = {
|
||||
}
|
||||
}
|
||||
|
||||
conn:add( ubus_objects )
|
||||
|
||||
os.execute("sleep 1")
|
||||
|
||||
local command = "tcpdump -l " .. args .. " 2>&1"
|
||||
|
||||
if duration ~= "" then
|
||||
command = "timeout " .. duration .. " " .. command
|
||||
end
|
||||
|
||||
local pipe = io.popen(command)
|
||||
|
||||
for line in pipe:lines() do
|
||||
local params = {
|
||||
data = line
|
||||
}
|
||||
conn:notify(ubus_objects.tcpdump.__ubusobj, "tcpdump.data", params)
|
||||
end
|
||||
|
||||
local pcap = fs.stat("/tmp/capture.pcap0")
|
||||
if pcap then
|
||||
fs.move("/tmp/capture.pcap0","/tmp/capture.pcap")
|
||||
fs.remove("/tmp/capture.pcap1")
|
||||
end
|
||||
|
||||
if filter then
|
||||
fs.remove("/tmp/tcpdump_filter")
|
||||
end
|
||||
|
||||
conn:close()
|
||||
pipe:close()
|
||||
|
||||
fs.remove("/var/run/packet_capture.pid")
|
69
luci-app-packet-capture/root/usr/libexec/packet_capture_start
Executable file
69
luci-app-packet-capture/root/usr/libexec/packet_capture_start
Executable file
|
@ -0,0 +1,69 @@
|
|||
#!/bin/sh
|
||||
|
||||
. /usr/share/libubox/jshn.sh
|
||||
|
||||
PIDFILE="/var/run/packet_capture.pid"
|
||||
|
||||
if [ -f "$PIDFILE"];then
|
||||
echo "error: Packet capture is running"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
json_load "$1"
|
||||
json_get_var interface interface
|
||||
json_get_var filter filter
|
||||
json_get_var duration duration
|
||||
json_get_var packets packets
|
||||
json_get_var verbose verbose
|
||||
json_get_var domains domains
|
||||
json_get_var file file
|
||||
|
||||
args="-n"
|
||||
|
||||
if [ "$domains" == "1" ];then
|
||||
args=""
|
||||
fi
|
||||
|
||||
if [ -n "$interface" ];then
|
||||
ip a show "$interface" > /dev/null 2>&1
|
||||
if [ "$?" == "1" ]; then
|
||||
echo "error: Incorrect format of an interface"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
args="$args -i $interface"
|
||||
fi
|
||||
|
||||
if [ -n "$packets" ];then
|
||||
echo "$packets" | egrep '^[0-9]*$'
|
||||
if [ "$?" -eq 0 ];then
|
||||
args="$args -c $packets"
|
||||
else
|
||||
echo "error: Incorrect packets argument"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$verbose" == "1" ];then
|
||||
args="$args -e"
|
||||
fi
|
||||
|
||||
if [ "$file" == "1" ];then
|
||||
mem=$(awk '/MemTotal/ {print $2}' /proc/meminfo)
|
||||
args="$args -W 2 -C $((mem/(1024 * 10))) -w /tmp/capture.pcap -z /usr/libexec/packet_capture_stop"
|
||||
fi
|
||||
|
||||
if [ -n "$filter" ];then
|
||||
tcpdump -i lo -d "$filter" >/dev/null 2>/dev/null
|
||||
if [ $? -eq 1 ];then
|
||||
echo "error: Incorrect filter argument"
|
||||
exit 1
|
||||
fi
|
||||
echo "$filter" > /tmp/tcpdump_filter
|
||||
fi
|
||||
|
||||
(/usr/libexec/packet_capture "$args" "$duration")&
|
||||
|
||||
echo $! > /var/run/packet_capture.pid
|
||||
|
||||
exit 0
|
9
luci-app-packet-capture/root/usr/libexec/packet_capture_stop
Executable file
9
luci-app-packet-capture/root/usr/libexec/packet_capture_stop
Executable file
|
@ -0,0 +1,9 @@
|
|||
#!/bin/sh
|
||||
|
||||
pid=$(cat /var/run/packet_capture.pid)
|
||||
if [ -n "$pid" ] && grep -sq packet_capture "/proc/$pid/cmdline"; then
|
||||
ppid=$(pgrep -P $pid)
|
||||
kill -TERM $ppid
|
||||
fi
|
||||
|
||||
exit 0
|
Loading…
Add table
Add a link
Reference in a new issue