samhocevar / rinetd

📡 TCP/UDP port redirector
GNU General Public License v2.0
859 stars 184 forks source link

Can you add a Http or Socket query interface for total upstream and downstream traffic? #24

Open fivetime opened 3 years ago

fivetime commented 3 years ago

Can you add a Http or Socket query interface for total upstream and downstream traffic?

samhocevar commented 3 years ago

Sure, why not! Can you give examples of software you know that do similar things, so that I don’t reinvent the wheel?

fivetime commented 3 years ago

I don't find any similar software, my requirement is to query all the upstream and downstream traffic of rinetd through API.

fivetime commented 3 years ago

For example: rinetd.conf configure a line of manager addr and port, and then rinetd starts udp monitoring to receive the query command. After receiving the query command, the upstream and downlink traffic data is sent to the requestor by udp.

rinetd.conf manager 127.0.0.1:8000

command protocol: Send udp request: stat Respond to Json: stat: {"up": 11370, "down": 12345}

fivetime commented 3 years ago

Python client example code

import socket

cli = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
cli.sendto('stat', '127.0.0.1:8000')
print(cli.recv(8000))  # You'll receive 'stat: {"up": 11370, "down": 12345}'

Linux socat program example

echo "stat" | socat - udp4-datagram:127.0.0.1:8000

The console will output stat: {"up": 11370, "down": 12345}

fivetime commented 3 years ago

It is recommended to use udp protocol to provide traffic query. If you use http api, then you need to import the http framework, the program is too large, it will affect performance.

lyda commented 3 years ago

You might want to look at exposing this information via a prometheus metrics page. There's a C library for this.