File: traffic.py

package info (click to toggle)
python-pure-python-adb 0.3.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,500 kB
  • sloc: python: 2,597; makefile: 8; sh: 1
file content (62 lines) | stat: -rw-r--r-- 1,421 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import re
from collections import namedtuple

from ppadb.plugins import Plugin

State = namedtuple("TrafficState", [
    'idx',
    'iface',
    'acct_tag_hex',
    'uid_tag_int',
    'cnt_set',
    'rx_bytes',
    'rx_packets',
    'tx_bytes',
    'tx_packets',
    'rx_tcp_bytes',
    'rx_tcp_packets',
    'rx_udp_bytes',
    'rx_udp_packets',
    'rx_other_bytes',
    'rx_other_packets',
    'tx_tcp_bytes',
    'tx_tcp_packets',
    'tx_udp_bytes',
    'tx_udp_packets',
    'tx_other_bytes',
    'tx_other_packets',
])


class Traffic(Plugin):
    def get_traffic(self, package_name):
        cmd = 'dumpsys package {} | grep userId'.format(package_name)
        result = self.shell(cmd).strip()

        pattern = r"userId=([\d]+)"

        if result:
            match = re.search(pattern, result)
            uid = match.group(1)
        else:
            # This package is not existing
            return None

        cmd = 'cat /proc/net/xt_qtaguid/stats | grep {}'.format(uid)
        result = self.shell(cmd)

        def convert(token):
            if token.isdigit():
                return int(token)
            else:
                return token

        states = []
        if result:
            for line in result.strip().split('\n'):
                values = map(convert, line.split())
                states.append(State(*values))

            return states
        else:
            return None