-
Notifications
You must be signed in to change notification settings - Fork 6
/
NJProtocol.py
105 lines (82 loc) · 3.06 KB
/
NJProtocol.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
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
import asyncio
class NJRatProtocol(asyncio.StreamReaderProtocol):
def __init__(self, controller, handler_cls, args):
asyncio.StreamReaderProtocol.__init__(self, asyncio.StreamReader(), self._pseudo_connected)
self.controller = controller
self.handler = handler_cls(self, controller, args)
#self.sockname = None
#self.sockip = None
#self.sockport = None
#self.peername = None
def connection_made(self, transport):
super().connection_made(transport)
#self.sockname = self.writer.get_extra_info('sockname')
#self.sockip = self.sockname[0]
#self.sockport = self.sockname[1]
#self.peername = self.writer.get_extra_info('peername')
self.controller.output("Connected to {}:{}".format(self.peername[0], self.peername[1]), color="green")
f = asyncio.async(self.handler.run())
f.add_done_callback(self.terminated)
def _pseudo_connected(self, reader, writer):
pass
def connection_lost(self, exc):
super().connection_lost(exc)
def terminated(self, f):
if f.done() and not f.cancelled():
ex = f.exception()
if ex:
#print(ex)
#self.controller.output("Connection has been terminated.", color="green")
self.handler.close_connection()
@property
def writer(self):
return self._stream_writer
@property
def reader(self):
return self._stream_reader
@property
def sockname(self):
return self.writer.get_extra_info('sockname')
@property
def peername(self):
return self.writer.get_extra_info('peername')
@property
def sockip(self):
return self.sockname[0]
@property
def sockport(self):
return self.sockname[1]
class NJRat_064_Protocol(NJRatProtocol):
@asyncio.coroutine
def read_nj_msg(self):
marker = b"[endof]"
data = yield from self.reader.readuntil(separator=marker)
return data[:-len(marker)] #readuntil return data including the seperator, we don't want the seperator
@asyncio.coroutine
def send_nj_msg(self, data):
msg = data + b'[endof]'
self.writer.write(msg)
class NJRat_07d_Protocol(NJRatProtocol):
@asyncio.coroutine
def read_nj_msg(self):
size = yield from self.reader.readuntil(separator=b'\x00')
data = yield from self.reader.readexactly(int(size.decode()[:-1]))
return data
@asyncio.coroutine
def send_nj_msg(self, data):
size = len(data)
header = str(size).encode() + b'\x00'
msg = header + data
self.writer.write(msg)
class NJRat_07dg_Protocol(NJRatProtocol):
@asyncio.coroutine
def read_nj_msg(self):
size = yield from self.reader.readuntil(separator=b'\x00')
data = yield from self.reader.readexactly(int(size.decode()[:-1]))
return data
@asyncio.coroutine
def send_nj_msg(self, data):
size = len(data)
header = str(size).encode() + b'\x00'
msg = header + data
self.writer.write(msg)