File: mutetcp.py

package info (click to toggle)
python-aioxmpp 0.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 6,244 kB
  • sloc: python: 97,761; xml: 215; makefile: 155; sh: 63
file content (202 lines) | stat: -rw-r--r-- 5,810 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/python3
########################################################################
# File name: mutetcp.py
# This file is part of: aioxmpp
#
# LICENSE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
########################################################################
import collections
import math
import subprocess
import textwrap
import time

Conn = collections.namedtuple(
    "Conn",
    [
        "proto",
        "laddr",
        "raddr",
        "state",
        "pid",
        "prog"
    ])

def get_connections(search_for_pid=None, search_for_prog=None):
    rows = subprocess.check_output(["netstat", "-tnWp"]).split(b"\n")
    for line in rows:
        line = line.strip()
        parts = line.split()
        try:
            proto, _, _, laddr, raddr, state, pid_prog = parts
        except ValueError:
            # print("skipping input: {}".format(parts))
            continue

        proto = proto.decode()
        laddr = laddr.decode()
        raddr = raddr.decode()
        state = state.decode()

        if pid_prog == b"-":
            pid = None
            prog = None
        else:
            pid, prog = pid_prog.split(b"/", 1)
            pid = int(pid)
            prog = prog.decode("utf8")

        if search_for_pid is not None and pid != search_for_pid:
            continue
        if search_for_prog is not None and prog != search_for_prog:
            continue

        yield Conn(proto, laddr, raddr, state, pid, prog)

def get_connections_by_pid(**kwargs):
    pidmap = {}
    for conn in get_connections(**kwargs):
        pidmap.setdefault(conn.pid, (conn.prog, []))[1].append(conn)

    return pidmap

def select_from_list(keylist, items, formatter, prompt, *, allow_all=False):
    for key in keylist:
        item = items[key]
        print(formatter(key, item))

    while True:
        try:
            s = input(prompt)
            key = int(s)
            value = items[key]
        except ValueError as err:
            if s == "a" and allow_all:
                return None
            print(str(err))
            continue
        except (IndexError, KeyError) as err:
            print("not a valid entry: {}".format(err))
            continue
        break

    return value

def select_process(pidmap):
    pids = sorted(pidmap.keys())
    width = math.ceil(math.log(max(pids), 10))
    formatter = ("({{pid:>{width}d}})  {{progname}} "
                 "({{nconn}} connection{{nconn_pls}})").format(width=width)

    def fmt(pid, progconns):
        nonlocal formatter
        prog, conns = progconns
        return formatter.format(
            pid=pid,
            progname=prog,
            nconn=len(conns),
            nconn_pls="s" if len(conns) != 1 else ""
        )

    print("select a process from the list below, by typing its pid")
    print("({{pid:<{width}s}})  {{info}}".format(width=width).format(
        pid="pid",
        info="info"))
    return select_from_list(pids, pidmap, fmt, "(pid)> ")

def select_conn(conns):
    width = math.ceil(math.log(len(conns), 10))
    formatter = (
        "({{connno:>{width}d}})  {{laddr:30s}} <-> {{raddr:30s}}"
    ).format(width=width)

    def fmt(connno, conn):
        nonlocal formatter
        return formatter.format(
            connno=connno,
            laddr=conn.laddr,
            raddr=conn.raddr
        )

    numbers = list(range(len(conns)))
    print("select a connection from the list below")
    print("({{connno:<{width}s}})  {{info}}".format(width=width).format(
        connno="connno",
        info="info"))
    return select_from_list(numbers, conns, fmt, "(connno)> ")

if __name__ == "__main__":
    import argparse
    import sys

    parser = argparse.ArgumentParser()

    parser.add_argument(
        "-p", "--pid",
        default=None)
    parser.add_argument(
        "-P", "--prog",
        default=None)

    args = parser.parse_args()

    pidmap = get_connections_by_pid(search_for_pid=args.pid,
                                    search_for_prog=args.prog)

    if len(pidmap) > 1:
        print("process selector was ambiguous")
        _, conns = select_process(pidmap)
    elif pidmap:
        _, conns = list(pidmap.values())[0]
    else:
        print("no matching process found")
        sys.exit(1)

    if len(conns) > 1:
        conn = select_conn(conns)
    else:
        conn = conns.pop()

    laddr, lport = conn.laddr.rsplit(":", 1)
    raddr, rport = conn.raddr.rsplit(":", 1)

    print("\n".join(
        textwrap.wrap("muting connection between [{}]:{} (local) and [{}]:{} "
                      "(remote), on process {} (pid={})".format(
                          laddr, lport, raddr, rport, conn.prog, conn.pid))))

    if conn.proto.endswith("6"):
        # ipv6
        iptables = "ip6tables"
    else:
        iptables = "iptables"

    subprocess.check_call(
        [iptables, "-I", "INPUT",
         "-s", raddr,
         "-d", laddr,
         "-p", "tcp",
         "--sport", rport,
         "--dport", lport,
         "-j", "DROP"])

    print("use ^C (SIGINT) to un-mute")
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        subprocess.check_call([iptables, "-D", "INPUT", "1"])