Esempio n. 1
0
def sockfam_to_enum(num):
    """Convert a numeric socket family value to an IntEnum member.
    If it's not a known member, return the numeric value itself.
    """
    if enum is None:
        return num
    else:  # pragma: no cover
        try:
            return socket.AddressFamily(num)
        except (ValueError, AttributeError):
            return num
Esempio n. 2
0
def pid_connections(pid: int, kind: str) -> Iterator[Connection]:
    allowed_combos = _util.conn_kind_to_combos(kind)
    if not allowed_combos:
        return

    kfiles = {
        kfile.ki_fdata: (kfile.ki_pid, kfile.ki_fd)
        for kfile in _list_kinfo_files(pid)
        if kfile.ki_fd >= 0 and kfile.ki_ftype == DTYPE_SOCKET
    }
    if not kfiles:
        return

    for (family, stype) in allowed_combos:
        base_mib = _bsd.sysctlnametomib(
            "net.{}.pcblist".format(_SOCK_TYPES[family][stype]),
            maxlen=4,
        )

        mib = [
            *base_mib,
            PCB_ALL,
            0,
            ctypes.sizeof(KinfoPcb),
            1000000,
        ]

        kinfo_pcb_data = _bsd.sysctl_bytes_retry(mib, None)
        for kpcb in (KinfoPcb *
                     (len(kinfo_pcb_data) // ctypes.sizeof(KinfoPcb))
                     ).from_buffer_copy(kinfo_pcb_data):
            try:
                pid, fd = kfiles.pop(kpcb.ki_sockaddr)
            except KeyError:
                continue

            yield Connection(
                family=socket.AddressFamily(kpcb.ki_family),  # pylint: disable=no-member
                type=socket.SocketKind(kpcb.ki_type),  # pylint: disable=no-member
                laddr=kpcb.ki_s.to_addr(kpcb.ki_family),
                raddr=kpcb.ki_d.to_addr(kpcb.ki_family),
                status=(_TCP_STATES[kpcb.ki_tstate]
                        if kpcb.ki_type == socket.SOCK_STREAM
                        and kpcb.ki_family != socket.AF_UNIX else None),
                fd=fd,
                pid=pid,
            )

            if not kfiles:
                return
Esempio n. 3
0
 def __init__(
     self,
     family: socket.AddressFamily,
     address: Tuple[HostAddress, int],
     timeout: float,
     encryption_settings: Dict[str, str],
 ):
     # type (...) -> None
     super(TCPDataFetcher, self).__init__()
     self._family = socket.AddressFamily(family)
     self._address = address
     self._timeout = timeout
     self._encryption_settings = encryption_settings
     self._logger = logging.getLogger("cmk.fetchers.tcp")
     self._socket: Optional[socket.socket] = None
Esempio n. 4
0
 def __init__(
     self,
     file_cache: AgentFileCache,
     family: socket.AddressFamily,
     address: Tuple[HostAddress, int],
     timeout: float,
     encryption_settings: Dict[str, str],
 ) -> None:
     super().__init__(file_cache, logging.getLogger("cmk.fetchers.tcp"))
     self._family = socket.AddressFamily(family)
     # json has no builtin tuple, we have to convert
     self._address = tuple(address) if isinstance(address,
                                                  list) else address
     self._timeout = timeout
     self._encryption_settings = encryption_settings
     self._socket: Optional[socket.socket] = None
Esempio n. 5
0
 def sessions(self, filters=None, options=None):
     """
     Get currently open websocket sessions.
     """
     return filter_list([{
         'id':
         i.session_id,
         'socket_type':
         socket.AddressFamily(
             i.request.transport.get_extra_info('socket').family).name,
         'address':
         i.request.transport.get_extra_info('sockname'),
         'authenticated':
         i.authenticated,
         'call_count':
         i._softhardsemaphore.counter,
     } for i in self.middleware.get_wsclients().values()], filters, options)
Esempio n. 6
0
 def sessions(self, filters=None, options=None):
     """
     Get currently open websocket sessions.
     """
     return filter_list([{
         'id':
         i.session_id,
         'socket_family':
         socket.AddressFamily(
             i.request.transport.get_extra_info('socket').family).name,
         'address': ((i.request.headers.get('X-Real-Remote-Addr'),
                      i.request.headers.get('X-Real-Remote-Port'))
                     if i.request.headers.get('X-Real-Remote-Addr') else
                     (i.request.transport.get_extra_info("peername"))),
         'authenticated':
         i.authenticated,
         'call_count':
         i._softhardsemaphore.counter,
     } for i in self.middleware.get_wsclients().values()], filters, options)
Esempio n. 7
0
 def __init__(
     self,
     file_cache: DefaultAgentFileCache,
     *,
     family: socket.AddressFamily,
     address: Tuple[Optional[HostAddress], int],
     timeout: float,
     encryption_settings: Mapping[str, str],
     use_only_cache: bool,
 ) -> None:
     super().__init__(file_cache, logging.getLogger("cmk.fetchers.tcp"))
     self.family: Final = socket.AddressFamily(family)
     # json has no builtin tuple, we have to convert
     assert address[0] is not None
     self.address: Final[Tuple[HostAddress, int]] = (address[0], address[1])
     self.timeout: Final = timeout
     self.encryption_settings: Final = encryption_settings
     self.use_only_cache: Final = use_only_cache
     self._socket: Optional[socket.socket] = None
Esempio n. 8
0
 def __init__(
     self,
     file_cache: AgentFileCache,
     *,
     family: socket.AddressFamily,
     address: Tuple[Optional[HostAddress], int],
     timeout: float,
     host_name: HostName,
     encryption_settings: Mapping[str, str],
 ) -> None:
     super().__init__(file_cache, logging.getLogger("cmk.helper.tcp"))
     self.family: Final = socket.AddressFamily(family)
     # json has no builtin tuple, we have to convert
     self.address: Final[Tuple[Optional[HostAddress],
                               int]] = (address[0], address[1])
     self.timeout: Final = timeout
     self.host_name: Final = host_name
     self.encryption_settings: Final = encryption_settings
     self._opt_socket: Optional[socket.socket] = None
Esempio n. 9
0
async def test_tunnel_rule():
    rule = conf.TunnelRule({
        "id": "test",
        "priority": 100,
        "addr": "*.baidu.com;www.qq.com",
        "port": "80;443;5555-5566",
        "tunnel": "web",
    })
    assert rule.id == "test"
    assert rule.priority == 100
    with patch("socket.getaddrinfo") as mocked_getaddrinfo:
        mocked_getaddrinfo.side_effect = lambda *args: [(socket.AddressFamily(
            2), ("1.1.1.1", 0))]
        assert await rule.is_hit(("www.baidu.com", 80)) == True
        assert await rule.is_hit(("baidu.com", 80)) == False
        assert await rule.is_hit(("www.baidu.com", 801)) == False
        assert await rule.is_hit(("www.qq.com", 443)) == True
        assert await rule.is_hit(("www.qq.com", 5555)) == True
        assert await rule.is_hit(("www.qq.com", 5566)) == True
        assert await rule.is_hit(("www.qq.com", 5567)) == False
        assert await rule.is_hit(("wwww.qq.com", 5555)) == False
Esempio n. 10
0
import os
import aenum
import socket
import asyncio as aio
import ctypes as ct


class sockaddr_un(ct.Structure):
    _fields_ = [("sun_family", ct.c_ushort), ("sun_path", ct.c_char * 108)]


aenum.extend_enum(socket.AddressFamily, "AF_UNIX", 1)
AF_UNIX = socket.AddressFamily(1)
NULL = 0


def iocp_connect(proactor, conn, address):
    # _overlapped.WSAConnect(conn.fileno(), address)
    addr = sockaddr_un(AF_UNIX.value, address.encode() + b"\0")
    winsock = ct.windll.ws2_32
    winsock.connect(conn.fileno(), addr, 110)

    fut = proactor._loop.create_future()
    fut.set_result(None)
    return fut


async def create_unix_connection(loop,
                                 protocol_factory,
                                 path=None,
                                 *,
    def __init__(self, log_ts, start_ts, hb):
        self._log_timestamp = log_ts  # timestamp when the logevent occurred on the test system
        self._sut_timestamp = hb[
            'ts']  # timestamp of the event on the system under test
        self._sut_start_ts = start_ts
        self._pid = hb[
            'pid']  # TODO: should never change, still required here?
        # self._process_name = hb['name']   # should never change
        self._status = hb['status']
        # self._username = hb['username']   # should never change
        self._cpu_percent = hb['cpu_percent']
        self._mem_percent = hb['memory_percent']
        self._num_threads = hb['num_threads']
        self._num_fds = hb['num_fds']

        # network connections
        # https://psutil.readthedocs.io/en/latest/#psutil.Process.connections
        self._connections = list()
        for con in hb['connections']:
            fd = con[0]

            # fix: psutils encodes AF_INET6 with 10, corresponds with 30 in socket.AddressFamily
            af_inet_idx = con[1] if (con[1] != 10) else 30
            address_family = socket.AddressFamily(af_inet_idx).name
            socket_kind = socket.SocketKind(con[2]).name

            listen_addr = con[3]
            if len(listen_addr) > 0:
                laddr = {'ip': listen_addr[0], 'port': listen_addr[1]}
            else:
                laddr = {'ip': '', 'port': ''}

            remote_addr = con[4]
            if len(remote_addr) > 0:
                raddr = {'ip': remote_addr[0], 'port': remote_addr[1]}
            else:
                raddr = {'ip': '', 'port': ''}

            status = con[5]

            con_dict = {
                'fd': fd,
                'family': address_family,
                'type': socket_kind,
                'laddr': laddr,
                'raddr': raddr,
                'status': status
            }
            self._connections.append(con_dict)

        # Memory consumption
        # https://psutil.readthedocs.io/en/latest/#psutil.Process.memory_full_info
        # http://grodola.blogspot.com/2016/02/psutil-4-real-process-memory-and-environ.html
        mem_info = hb['memory_info']
        mem_rss = mem_info[0] / (1024 * 1024)
        mem_vms = mem_info[1] / (1024 * 1024)
        mem_shared = mem_info[2] / (1024 * 1024)
        mem_text = mem_info[3] / (1024 * 1024)
        mem_lib = mem_info[4] / (1024 * 1024)
        mem_data = mem_info[5] / (1024 * 1024)
        mem_dirty = mem_info[6] / (1024 * 1024)
        self._memory = {
            'rss': mem_rss,
            'vms': mem_vms,
            'shared': mem_shared,
            'text': mem_text,
            'lib': mem_lib,
            'data': mem_data,
            'dirty': mem_dirty
        }

        # I/O
        # https://psutil.readthedocs.io/en/latest/#psutil.Process.io_counters
        io_counters = hb['io_counters']
        io_read_count = io_counters[0]
        io_write_count = io_counters[1]
        io_read_chars = io_counters[4]  # bytes read via syscalls
        io_write_chars = io_counters[5]  # bytes written via syscalls
        self._io_counters = {
            'read_count': io_read_count,
            'write_count': io_write_count,
            'read_chars': io_read_chars,
            'write_chars': io_write_chars
        }

        # CPU times
        # https://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_times
        cpu_times = hb['cpu_times']
        self._cpu_times = {
            'user': cpu_times[0],
            'system': cpu_times[1],
            'children_user': cpu_times[2],
            'children_system': cpu_times[3]
        }

        # Context switches
        # https://psutil.readthedocs.io/en/latest/#psutil.Process.num_ctx_switches
        ctx_switches = hb['num_ctx_switches']
        self._ctx_switches = {
            'voluntary': ctx_switches[0],
            'involuntary': ctx_switches[1]
        }

        # Open files
        # https://psutil.readthedocs.io/en/latest/#psutil.Process.open_files
        open_files = hb['open_files']
        self._open_files = list()
        for f in open_files:
            self._open_files.append({
                'path': f[0],
                'fd': f[1],
                'pos': f[2],
                'mode': f[3],
                'flags': f[4]
            })