-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetal.py
208 lines (177 loc) · 6.91 KB
/
metal.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# metal is a simple python network library I wrote years ago.
# Copyright (c) 2003, 2011 by Samuel Williams.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import asyncore, socket, time, sys, string, struct, random, time;
from array import array;
def hexdump(Data):
Counter = 0;
Div = 4;
Buffer = "";
LineData = ""
for Ch in Data:
if(Counter % 16) == 0:
Buffer +=(hex(Counter) + " ").rjust(7) + " ";
Div -= 1;
Buffer += string.zfill((hex( ord(Ch)) [2:]), 2) + " ";
if(ord(Ch) >= 32) &(ord(Ch) < 127):
LineData += Ch;
else:
LineData += ".";
if Div == 0:
Div = 4;
Buffer += " ";
if(Counter % 16) == 15:
Buffer += " " + LineData + "\n";
LineData = "";
Counter += 1;
Length = Counter % 16;
return Buffer + LineData.rjust(53 -((Length * 3) + int(Length / 4)) + len(LineData));
def debug(Object, Message):
Pieces = string.split(`Object`[1:-1], ' ');
Module, Name = string.split(Pieces [0], '.');
print time.strftime("%a, %d %b %Y %H:%M:%S %Z", time.localtime()) + " " + Name + "@" + Pieces [len(Pieces) - 1] + ": " + Message;
def address(Addr):
return Addr [0] + ":" + `Addr [1]`;
def resolve(Addr):
return(socket.gethostbyname(Addr[0]), Addr [1]);
class protocol:
def __init__(self):
self.Packets = {};
self.Transformations = {"uint16":"H", "int16":"h", "str8":"8s", "str4": "4s", "str2": "2s"};
def load_protocol(self, Packets):
self.Packets = Packets;
def make(self, Message, Parameters):
Buffer = Message + ":";
Bits = string.split(self.Packets [Message], ' ');
for Bit in Bits:
Name, Type = string.split(Bit, ':');
if Type == '':
Buffer += Name
else:
Buffer += struct.pack("!" + self.Transformations [Type], Parameters [Name])
return Buffer + "\x00";
def interpret(self, Message):
Msg = Message [0: string.find(Message, ":")];
Data = Message [string.find(Message, ":") + 1: len(Message) - 1];
UnpackString = "";
Names = [];
Bits = string.split(self.Packets [Msg], ' ');
for Bit in Bits:
Name, Type = string.split(Bit, ':');
UnpackString += self.Transformations [Type];
Names.append(Name)
Parameters = {};
Unpackage = struct.unpack("!" + UnpackString, Data);
if len(Names) != len(Unpackage):
debug(self, "oh my god something crazy has happened!");
Ln = 0;
while(Ln < len(Unpackage)):
Parameters [Names [Ln]] = Unpackage [Ln];
Ln += 1
return(Msg, Parameters);
class interaction(asyncore.dispatcher):
STREAM = socket.SOCK_STREAM;
PACKET = socket.SOCK_DGRAM;
def __init__(self, Typ, OnRead, OnWrite = 0, OnConnect = 0, OnDisconnect = 0):
self.Host =();
self.Connected = 0;
if Typ is interaction.STREAM or Typ is interaction.PACKET:
asyncore.dispatcher.__init__(self);
self.create_socket(socket.AF_INET, Typ);
else:
asyncore.dispatcher.__init__(self, Typ);
try:
self.Host = Typ.getpeername();
self.Connected = 1;
except:
pass;
self.SocketBuffer = "";
self.OnRead = OnRead;
self.OnWrite = OnWrite;
self.OnConnect = OnConnect;
self.OnDisconnect = OnDisconnect;
self.BufferSize = 256;
def host(self, Address = 0):
if(self.Connected == 0) &(Address != 0):
self.Host = Address;
self.Host =(socket.gethostbyname(self.Host [0]), self.Host [1]);
return self.Host;
def listen(self, Addr, Port, N = 1):
self.bind((Addr, Port));
asyncore.dispatcher.listen(self, N);
def write(self, Buffer):
self.SocketBuffer += Buffer;
def writable(self):
if(self.OnWrite != 0) | len(self.SocketBuffer):
return 1;
return 0;
def readable(self):
if self.OnRead or self.OnConnect:
return 1;
return 0;
def connect_state(self, OnConnect):
self.OnConnect = OnConnect;
def disconnect_state(self, OnDisconnect):
self.OnDisconnect = OnDisconnect;
def write_state(self, State):
self.OnWrite = State;
def read_state(self, State):
self.OnRead = State;
def connect(self, To):
asyncore.dispatcher.connect(self, To);
self.Connected = 1;
self.Host = To;
def handle_write(self):
if len(self.SocketBuffer) > 0:
if self.Connected:
Sent = self.send(self.SocketBuffer);
else:
Sent = self.sendto(self.SocketBuffer, self.Host);
self.SocketBuffer = self.SocketBuffer [Sent:];
if(self.OnWrite != 0):
New = self.OnWrite();
if New.__class__.__name__ != "NoneType":
self.OnWrite = New;
def handle_read(self):
if(self.Connected):
Data = self.recv(self.BufferSize);
Address = self.Host;
else:
Data, Address = self.recvfrom(self.BufferSize);
New = self.OnRead(Data, Address);
if New.__class__.__name__ != "NoneType":
self.OnRead = New;
def close(self):
if len(self.SocketBuffer) > 0:
if self.Connected:
Sent = self.send(self.SocketBuffer);
else:
Sent = self.sendto(self.SocketBuffer, self.Host);
asyncore.dispatcher.close(self);
def handle_close(self):
self.close();
if(self.OnDisconnect):
self.OnDisconnect();
def handle_accept(self):
if self.OnConnect:
Con, Addr = self.accept();
self.OnConnect(Con, Addr);
def run():
asyncore.loop();