qwj / python-proxy

HTTP/HTTP2/HTTP3/Socks4/Socks5/Shadowsocks/ShadowsocksR/SSH/Redirect/Pf TCP/UDP asynchronous tunnel proxy implemented in Python 3 asyncio.
MIT License
1.93k stars 323 forks source link

Monitor bandwidth usage ? #164

Open lucydjo opened 1 year ago

lucydjo commented 1 year ago

Hello, Is it possible to calculate the bandwidth usage? I can't find anything about this in the documentation.

Thanks :)

strangeh21 commented 1 year ago

Add verbose :) pproxy -r socks5://{proxy['host']}:{proxy['port']}#{proxy['username']}:{proxy['password']} -l socks5://:{server_port} -vv

This prints out the current usage to stdout. What I did is sent stdout to a class, and parsed the data. Search for data_usage as such:

    class OutputHandler:
        def __init__(self):
            pass

        def write(self, message):
            self.parse_and_handle_output(message.strip())

        def flush(self):
            pass

        def print_to_output(self, message):
            print(message, file=sys.__stdout__)

        def parse_and_handle_output(self, output):
            websites = re.findall(r'->\s(?:.*)->\s(.*):\d+', output)
            data_usage = re.search(
                "DIRECT: \d+ \(([\d.]+[KM])/s,([\d.]+[KM])/s\)   PROXY: \d+ \(([\d.]+[KM])/s,([\d.]+[KM])/s\)",
                output)
            if websites:  # Parse the websites
                for website in websites:
                    self.print_to_output(f"URL: {website}")
            elif data_usage and data_usage != "":  # Parse the data usage
                try:
                    direct, direct2, proxy, proxy2 = data_usage.groups()
                    direct_combined = self.to_megabytes(direct) + self.to_megabytes(direct2)
                    proxy_combined = self.to_megabytes(proxy) + self.to_megabytes(proxy2)
                    combined = float(direct_combined + proxy_combined)
                    self.print_to_output(round(combined, 4))
            else:
                if output and output != "":
                    self.print_to_output(output)

        @staticmethod
        def to_megabytes(value):
            if value[-1] == 'M':
                return float(value[:-1])
            elif value[-1] == 'K':
                return float(value[:-1]) / 1024
            else:
                return 0
sys.stdout = self.OutputHandler()
maxiedaniels commented 1 year ago

@strangeh21 Sorry, can you explain further? Are you running python proxy within a Python script? I'm running via command-line so I'm unclear on how I would adjust your solution.

strangeh21 commented 1 year ago

@strangeh21 Sorry, can you explain further? Are you running python proxy within a Python script? I'm running via command-line so I'm unclear on how I would adjust your solution.

In short, verbose level 2 within commandline adds traffic logging. This is done by adding "-vv" When it is running you can press enter at any time to get a complete list of data usage.