-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconnection.py
80 lines (68 loc) · 2.57 KB
/
connection.py
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
################################################################################
#
# Stand-alone VoIP honeypot client (preparation for Dionaea integration)
# Copyright (c) 2010 Tobias Wulff (twu200 at gmail)
#
#
# 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 2 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, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
################################################################################
import socket
import asyncore
import time
import logging
# Setup logging mechanism
logger = logging.getLogger('connection')
logger.setLevel(logging.DEBUG)
logConsole = logging.StreamHandler()
logConsole.setLevel(logging.DEBUG)
logConsole.setFormatter(logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
logger.addHandler(logConsole)
class connection(asyncore.dispatcher):
"""Connection class mockup (from connection.pyx in dionaea src)"""
def __init__(self, proto=None, sock=None):
"""Creates a new connection with TCP as its default transport
protocol"""
asyncore.dispatcher.__init__(self, sock)
if sock == None:
# Use TCP by default, and UDP if stated
type = socket.SOCK_STREAM
if proto and proto.lower() == 'udp':
type = socket.SOCK_DGRAM
# Create non-blocking socket
self.create_socket(socket.AF_INET, type)
def handle_established(self):
"""Callback for a newly established connection (client or server)"""
logger.info('Session established')
logger.debug('Session socket: {}'.format(
self.getsockname()))
def handle_read(self):
"""Callback for incoming data (dionaea: handle_io_in)"""
pass
def handle_write(self):
"""Callback for outgoing data (dionaea: handle_io_out)"""
pass
def handle_connect(self):
"""Callback for successful connect (client)"""
self.handle_established()
def handle_close(self):
"""Callback for a closed connection"""
self.close()
logger.info('Session closed')
def handle_accept(self):
"""Callback for successful accept (server)"""
self.__conn, self.__address = self.accept()
self.handle_established()