1# This file is part of Xpra.
2# Copyright (C) 2013-2020 Antoine Martin <antoine@xpra.org>
3# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
4# later version. See the file COPYING for details.
5
6import os
7import threading
8
9from xpra.util import repr_ellipsized, envint, envbool
10from xpra.log import Logger
11log = Logger("network")
12
13class ConnectionClosedException(Exception):
14    pass
15
16MAX_PACKET_SIZE = envint("XPRA_MAX_PACKET_SIZE", 16*1024*1024)
17FLUSH_HEADER = envbool("XPRA_FLUSH_HEADER", True)
18
19SOCKET_TYPES = ("tcp", "ws", "wss", "ssl", "ssh", "rfb", "vsock")
20
21IP_SOCKTYPES = ("tcp", "ssl", "ws", "wss", "ssh")
22TCP_SOCKTYPES = ("tcp", "ssl", "ws", "wss", "ssh")
23
24
25#this is used for generating aliases:
26PACKET_TYPES = [
27    "hello", "info",
28    "open-url", "send-file", "send-data-request", "send-data-response", "ack-file-chunk", "send-file-chunk",
29    "sound-data", "new-stream", "state-changed", "new-buffer", "cleanup", "add_data", "stop",
30    "ping", "ping_echo",
31    "info-response", "server-event",
32    "disconnect", "set_deflate", "connection-lost", "gibberish", "invalid",
33    "show-desktop", "desktop_size",
34    "new-window", "new-override-redirect", "new-tray",
35    "raise-window", "initiate-moveresize", "window-move-resize", "window-resized", "window-metadata",
36    "configure-override-redirect", "lost-window", "window-icon",
37    "draw",
38    "eos", "cursor", "bell",
39    "pointer-position", "pointer-grab", "pointer-ungrab",
40    "webcam-stop", "webcam-ack",
41    "set-clipboard-enabled", "clipboard-token", "clipboard-request",
42    "clipboard-contents", "clipboard-contents-none", "clipboard-pending-requests", "clipboard-enable-selections",
43    "notify_show", "notify_close",
44    "rpc-reply", "startup-complete", "setting-change", "control",
45    "encodings",
46    ]
47
48def get_log_packets(exclude=False):
49    lp = os.environ.get("XPRA_LOG_PACKETS")
50    if not lp:
51        return None
52    pt = []
53    for x in lp.split(","):
54        if x.startswith("-")==exclude:
55            pt.append(x[int(exclude):])
56    return tuple(pt)
57
58def _may_log_packet(sending, packet_type, packet):
59    if LOG_PACKET_TYPE:
60        log.info("%s %s (thread=%s)", "sending  " if sending else "receiving", packet_type, threading.current_thread())
61    if LOG_PACKETS or NOLOG_PACKETS:
62        if packet_type in NOLOG_PACKETS:
63            return
64        if packet_type in LOG_PACKETS or "*" in LOG_PACKETS:
65            s = str(packet)
66            if len(s)>PACKET_LOG_MAX_SIZE:
67                s = repr_ellipsized(s, PACKET_LOG_MAX_SIZE)
68            log.info(s)
69
70LOG_PACKETS = None
71NOLOG_PACKETS = None
72LOG_PACKET_TYPE = False
73PACKET_LOG_MAX_SIZE = 500
74
75def noop(*_args):
76    """ the default implementation is to do nothing """
77may_log_packet = noop
78
79def init():
80    global LOG_PACKETS, NOLOG_PACKETS, LOG_PACKET_TYPE, PACKET_LOG_MAX_SIZE
81    LOG_PACKETS = get_log_packets()
82    NOLOG_PACKETS = get_log_packets(True)
83    LOG_PACKET_TYPE = envbool("XPRA_LOG_PACKET_TYPE", False)
84
85    PACKET_LOG_MAX_SIZE = envint("XPRA_PACKET_LOG_MAX_SIZE", 500)
86
87    global may_log_packet
88    if LOG_PACKETS or NOLOG_PACKETS or LOG_PACKET_TYPE:
89        may_log_packet = _may_log_packet
90    else:
91        may_log_packet = noop
92
93init()
94