Skip to content

Commit

Permalink
git - Merge pull request #312 from DinoTools/remove_pyev
Browse files Browse the repository at this point in the history
Remove pyev
  • Loading branch information
phibos committed Nov 29, 2020
2 parents 0cd2692 + a9688b0 commit f7c4c3a
Show file tree
Hide file tree
Showing 42 changed files with 199 additions and 3,786 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,5 @@ Licenses
--------

* dionaea: GPLv2+
* pyev(modules/python/pyev): GPLv3+
* tftp service(modules/python/tftp.py): CNRI Python License (incompatible with GPL)
* parts of ftp service(modules/python/ftp.py): MIT (compatible with GPL)
52 changes: 26 additions & 26 deletions modules/python/binding.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ cdef class connection_speed:
def __get__(self):
return c_connection_stats_speed_get(self.thisptr)

cdef connection_speed connection_speed_from(c_connection_stats *info):
cdef connection_speed connection_speed_from(c_connection_stats *info) with gil:
cdef connection_speed instance
instance = NEW_C_NODE_INFO_CLASS(connection_speed)
instance.thisptr = info
Expand Down Expand Up @@ -255,7 +255,7 @@ cdef class connection_accounting:
def __get__(self):
return c_connection_stats_accounting_get(self.thisptr)

cdef connection_accounting connection_accounting_from(c_connection_stats *info):
cdef connection_accounting connection_accounting_from(c_connection_stats *info) with gil:
cdef connection_accounting instance
instance = NEW_C_NODE_INFO_CLASS(connection_accounting)
instance.thisptr = info
Expand All @@ -282,7 +282,7 @@ cdef class connection_stats:
def __get__(self):
return connection_accounting_from(self.thisptr)

cdef connection_stats connection_stats_from(c_connection_stats *info):
cdef connection_stats connection_stats_from(c_connection_stats *info) with gil:
cdef connection_stats instance
instance = NEW_C_NODE_INFO_CLASS(connection_stats)
instance.thisptr = info
Expand All @@ -294,7 +294,7 @@ cdef extern from "./module.h":
cdef void DECREF "Py_DECREF"(object)
void c_log_wrap "log_wrap" (char *, int, char *, int, char *)

cdef node_info node_info_from(c_node_info *node):
cdef node_info node_info_from(c_node_info *node) with gil:
cdef node_info instance
instance = NEW_C_NODE_INFO_CLASS(node_info)
instance.thisptr = node
Expand Down Expand Up @@ -380,7 +380,7 @@ cdef class connection_timeouts:
cdef extern from "./module.h":
cdef connection_timeouts NEW_C_CONNECTION_TIMEOUTS_CLASS "PY_NEW"(object T)

cdef connection_timeouts connection_timeouts_from(c_connection *con):
cdef connection_timeouts connection_timeouts_from(c_connection *con) with gil:
cdef connection_timeouts instance
instance = NEW_C_CONNECTION_TIMEOUTS_CLASS(connection_timeouts)
instance.thisptr = con
Expand Down Expand Up @@ -738,7 +738,7 @@ cdef extern from "./module.h":
# cdef int PRINT_REFCOUNT "REFCOUNT"(object T)


cdef connection _factory(c_connection *con):
cdef connection _factory(c_connection *con) with gil:
cdef connection instance
cdef connection parent = <object>c_connection_protocol_ctx_get(con)
instance = CLONE_C_CONNECTION_CLASS(parent)
Expand All @@ -749,27 +749,27 @@ cdef connection _factory(c_connection *con):
instance.apply_parent_config(parent)
return instance

cdef void _garbage(void *context):
cdef void _garbage(void *context) with gil:
# print "get out the garbage !"
cdef connection instance
instance = <connection>context;
instance.thisptr = NULL
DECREF(instance)

cdef void handle_origin_cb(c_connection *con, c_connection *origin) except *:
cdef void handle_origin_cb(c_connection *con, c_connection *origin) except * with gil:
# print "origin_cb"
cdef connection instance
instance = <connection>c_connection_protocol_ctx_get(con)
parent = <connection>c_connection_protocol_ctx_get(origin)
instance.handle_origin(parent)

cdef void handle_established_cb(c_connection *con) except *:
cdef void handle_established_cb(c_connection *con) except * with gil:
# print "established_cb"
cdef connection instance
instance = <connection>c_connection_protocol_ctx_get(con)
instance.handle_established()

cdef int handle_io_in_cb(c_connection *con, void *context, void *data, int size) except *:
cdef int handle_io_in_cb(c_connection *con, void *context, void *data, int size) except * with gil:
# print "io_in_cb"
cdef connection instance
instance = <connection>context
Expand All @@ -782,7 +782,7 @@ cdef int handle_io_in_cb(c_connection *con, void *context, void *data, int size)
return len(bdata)
return l

cdef void handle_io_out_cb(c_connection *con, void *context) except *:
cdef void handle_io_out_cb(c_connection *con, void *context) except * with gil:
# print "io_out_cb"
cdef connection instance
instance = <connection>context
Expand All @@ -792,7 +792,7 @@ cdef void handle_io_out_cb(c_connection *con, void *context) except *:
logging.error("There was an error in the Python service", exc_info=True)
instance.close()

cdef c_bool handle_disconnect_cb(c_connection *con, void *context) except *:
cdef c_bool handle_disconnect_cb(c_connection *con, void *context) except * with gil:
# print "disconnect_cb"
cdef connection instance
instance = <connection>context
Expand All @@ -801,7 +801,7 @@ cdef c_bool handle_disconnect_cb(c_connection *con, void *context) except *:
instance.thisptr = NULL
return <bint>r

cdef c_bool handle_error_cb(c_connection *con, c_connection_error err) except *:
cdef c_bool handle_error_cb(c_connection *con, c_connection_error err) except * with gil:
# print "connect_error_cb"
cdef connection instance
instance = <connection>c_connection_protocol_ctx_get(con)
Expand Down Expand Up @@ -836,27 +836,27 @@ cdef c_bool handle_error_cb(c_connection *con, c_connection_error err) except *:
r = instance.handle_error(i)
return <bint>r

cdef c_bool handle_timeout_sustain_cb(c_connection *con, void *ctx) except *:
cdef c_bool handle_timeout_sustain_cb(c_connection *con, void *ctx) except * with gil:
# print "timeout_sustain_cb"
cdef connection instance
instance = <connection>ctx
return <bint> instance.handle_timeout_sustain()

cdef c_bool handle_timeout_listen_cb(c_connection *con, void *ctx) except *:
cdef c_bool handle_timeout_listen_cb(c_connection *con, void *ctx) except * with gil:
# print "timeout_listen_cb"
cdef connection instance
instance = <connection>ctx
return <bint> instance.handle_timeout_listen()


cdef c_bool handle_timeout_idle_cb(c_connection *con, void *ctx) except *:
cdef c_bool handle_timeout_idle_cb(c_connection *con, void *ctx) except * with gil:
# print "timeout_idle_cb"
cdef connection instance
instance = <connection>ctx
return <bint> instance.handle_timeout_idle()


cdef void process_io_in(c_connection *con, c_processor_data *pd, void *data, int size) except *:
cdef void process_io_in(c_connection *con, c_processor_data *pd, void *data, int size) except * with gil:
bdata = bytesfrom(<char *>data, size)
cdef connection instance
instance = <connection>c_connection_protocol_ctx_get(con)
Expand All @@ -868,7 +868,7 @@ cdef void process_io_in(c_connection *con, c_processor_data *pd, void *data, int
instance.bistream.append((u'in',bdata))
return

cdef void process_io_out(c_connection *con, c_processor_data *pd, void *data, int size) except *:
cdef void process_io_out(c_connection *con, c_processor_data *pd, void *data, int size) except * with gil:
cdef connection instance
instance = <connection>c_connection_protocol_ctx_get(con)
if instance.thisptr.processor_data != NULL:
Expand All @@ -879,7 +879,7 @@ cdef void process_io_out(c_connection *con, c_processor_data *pd, void *data, in
instance.bistream.append((u'out',bdata))
return

cdef c_bool process_process(c_connection *con, void *config) except *:
cdef c_bool process_process(c_connection *con, void *config) except * with gil:
cdef connection instance
instance = <connection>c_connection_protocol_ctx_get(con)
instance.bistream = []
Expand Down Expand Up @@ -1001,7 +1001,7 @@ cdef extern from "../../include/incident.h":



cdef c_GList *py_to_glist(l):
cdef c_GList *py_to_glist(l) with gil:
cdef c_GList *gl
cdef c_opaque_data *op
gl = NULL
Expand All @@ -1010,7 +1010,7 @@ cdef c_GList *py_to_glist(l):
gl = c_g_list_append(gl, op)
return gl

cdef py_from_glist(c_GList *l):
cdef py_from_glist(c_GList *l) with gil:
cdef c_GList *it
it = c_g_list_first(l)
pl = []
Expand All @@ -1019,7 +1019,7 @@ cdef py_from_glist(c_GList *l):
it = c_g_list_next(it)
return pl

cdef GHashTable *py_to_ghashtable(d):
cdef GHashTable *py_to_ghashtable(d) with gil:
cdef GHashTable *gd
cdef char *kn
cdef c_opaque_data *op
Expand All @@ -1032,7 +1032,7 @@ cdef GHashTable *py_to_ghashtable(d):
g_hash_table_insert(gd, op.name, op)
return gd

cdef py_from_ghashtable(GHashTable *h):
cdef py_from_ghashtable(GHashTable *h) with gil:
cdef GHashTableIter iter
cdef gpointer key
cdef gpointer value
Expand All @@ -1044,7 +1044,7 @@ cdef py_from_ghashtable(GHashTable *h):
a[skey] = py_from_opaque(<c_opaque_data *>value)
return a

cdef c_opaque_data *py_to_opaque(value):
cdef c_opaque_data *py_to_opaque(value) with gil:
cdef c_opaque_data *o
o = c_opaque_data_new()
if isinstance(value, connection):
Expand Down Expand Up @@ -1073,7 +1073,7 @@ cdef c_opaque_data *py_to_opaque(value):
return NULL
return o

cdef py_from_opaque(c_opaque_data *value):
cdef py_from_opaque(c_opaque_data *value) with gil:
cdef c_uintptr_t x
cdef connection c
cdef c_connection *cc
Expand Down Expand Up @@ -1217,7 +1217,7 @@ cdef extern from "../../include/incident.h":
cdef extern from "module.h":
void c_traceable_ihandler_cb "traceable_ihandler_cb" (c_incident *, void *)

cdef void c_python_ihandler_cb (c_incident *i, void *ctx) except *:
cdef void c_python_ihandler_cb (c_incident *i, void *ctx) except * with gil:
cdef ihandler handler
cdef incident pi
handler = <ihandler>ctx
Expand Down
87 changes: 86 additions & 1 deletion modules/python/dionaea/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# This file is part of the dionaea honeypot
#
# SPDX-FileCopyrightText: 2009 Markus Koetter
# SPDX-FileCopyrightText: 2016-2018 PhiBo (DinoTools)
# SPDX-FileCopyrightText: 2016-2020 PhiBo (DinoTools)
#
# SPDX-License-Identifier: GPL-2.0-or-later

import glob
import logging
import pkgutil
import traceback
from threading import Event, Thread
from typing import Callable, Optional

import yaml

Expand Down Expand Up @@ -51,6 +53,89 @@ def stop(cls, ihandler):
ihandler.stop()


class SubTimer(Thread):
"""
Our own Timer class because some attributes we have to user are undocumented in the Python stub files.
:param interval: Wait interval sconds until the callback is called.
:param function: The callback function.
:param delay: Time in seconds before the callback is called for the first time. It not set interval is used.
:param repeat: Call function every $interval seconds.
:param args: Optional arguments passed to the callback function.
:param kwargs: Opional arguments passed to the callback function.
"""
def __init__(self, interval: float, function: Callable, delay: Optional[float] = None, repeat=False,
args: Optional[list] = None, kwargs: Optional[dict] = None):
Thread.__init__(self)
self.interval = interval
self.function = function
self.delay = delay
if self.delay is None:
self.delay = self.interval
self.repeat = repeat
self.args = args if args is not None else []
self.kwargs = kwargs if kwargs is not None else {}
self.finished = Event()

def cancel(self):
"""Stop the timer if it hasn't finished yet."""
self.finished.set()

def run(self) -> None:
self.finished.wait(self.delay)
if not self.finished.is_set():
self.function(*self.args, **self.kwargs)
while self.repeat and not self.finished.wait(self.interval):
if not self.finished.is_set():
self.function(*self.args, **self.kwargs)


class Timer(object):
"""
Extend Timer with additional functions like cancel and reset it. It uses the SubTimer() internally.
:param interval: Wait interval sconds until the callback is called.
:param function: The callback function.
:param delay: Time in seconds before the callback is called for the first time. It not set interval is used.
:param repeat: Call function every $interval seconds.
:param args: Optional arguments passed to the callback function.
:param kwargs: Opional arguments passed to the callback function.
"""
def __init__(self, interval: float, function: Callable, delay: Optional[float] = None, repeat=False,
args: Optional[list] = None, kwargs: Optional[dict] = None):
self.interval = interval
self.function = function
self.delay = delay
if self.delay is None:
self.delay = self.interval
self.repeat = repeat
self.args = args if args is not None else []
self.kwargs = kwargs if kwargs is not None else {}
self._timer: Optional[SubTimer] = None

def start(self) -> None:
"""Start the Timer"""
self._timer = SubTimer(
interval=self.interval,
function=self.function,
delay=self.delay,
repeat=self.repeat,
args=self.args,
kwargs=self.kwargs,
)
self._timer.start()

def cancel(self) -> None:
"""Cancel the Timer"""
if self._timer:
self._timer.cancel()

def reset(self) -> None:
"""Restart the Timer"""
self.cancel()
self.start()


def load_submodules(base_pkg=None):
if base_pkg is None:
import dionaea as base_pkg
Expand Down
24 changes: 10 additions & 14 deletions modules/python/dionaea/hpfeeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later

from dionaea import IHandlerLoader
from dionaea import IHandlerLoader, Timer
from dionaea.core import ihandler, incident, g_dionaea, connection
from dionaea.util import sha512file

Expand All @@ -16,10 +16,6 @@
import datetime
from time import gmtime, strftime

try:
from dionaea import pyev
except ImportError:
pyev = None

logger = logging.getLogger('hpfeeds')
logger.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -281,22 +277,22 @@ def __init__(self, path, config=None):
self.dynip_timer = None
self.ownip = None
if isinstance(self.dynip_resolve, str) and self.dynip_resolve.startswith("http"):
if pyev is None:
logger.debug('You are missing the python pyev binding in your dionaea installation.')
else:
logger.debug('hpfeedihandler will use dynamic IP resolving!')
self.loop = pyev.default_loop()
self.dynip_timer = pyev.Timer(2., 300, self.loop, self._dynip_resolve)
self.dynip_timer.start()
logger.debug('hpfeedihandler will use dynamic IP resolving!')
self.dynip_timer = Timer(
300,
self._dynip_resolve,
delay=2,
repeat=True,
)
self.dynip_timer.start()

def stop(self):
if self.dynip_timer:
self.dynip_timer.stop()
self.dynip_timer = None
self.loop = None

def _ownip(self, icd):
if self.dynip_resolve and 'http' in self.dynip_resolve and pyev is not None:
if self.dynip_resolve and 'http' in self.dynip_resolve:
if self.ownip:
return self.ownip
else:
Expand Down

0 comments on commit f7c4c3a

Please sign in to comment.