-
Notifications
You must be signed in to change notification settings - Fork 0
/
SzpieGwiazdor.py
217 lines (186 loc) · 9.25 KB
/
SzpieGwiazdor.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
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
ver 2016.09.13.2248 / @author: s
=============================================================================
zapamiętuje zawartości gwiazdek graczy parsując gałęzie "playersOnMap".
wyniki lądują w tabeli 'gwiazda'
def sample():
import cStringIO as StringIO
from amfast import decoder
trade= '/home/s/s3tt-sorted/smarTSO/amf-1001-visitfriend'
dane = StringIO.StringIO(open(trade, 'r').read()).read()
mess = decoder.decode_packet(dane)
sample = mess.messages[0].body.body['data']['data']
return sample
sam=sample()
#####
* ##
## *
________________________________________________________________________#####
WYMAGANIA:
amfast, mitmproxy, re2, pymysql, threading2, pysqlite, attrdict, lru-dict
"""
from mitmproxy.script import concurrent
from mitmproxy.models import decoded
from amfast import decoder as amfdec
from nested_lookup import nested_lookup
from threading2 import Thread
from re2 import search
from attrdict import AttrDict
from pymysql import connect
from os import path
from sys import stdout
from expiringdict import ExpiringDict
from time import time
from collections import Counter
import logging
global log, BASEDIR
if '__file__' in locals().keys():
BASEDIR= path.abspath(path.dirname(__file__))
else:
BASEDIR='/home/s/smarTSO/'
print ("'__file__' not defined, assuming hardcoded BASEDIR: %s"%BASEDIR)
def getloggerinstance():
""" https://docs.python.org/2/howto/logging-cookbook.html """
log =logging.getLogger()
hdlr = logging.FileHandler(path.join(BASEDIR,'SzpieGwiazdor.log'))
formatter = logging.Formatter('%(asctime)s %(filename)s %(lineno)s %(funcName)15s() %(levelname)s : %(message)s')
hdlr.setFormatter(formatter)
#log.addHandler(hdlr)
ch = logging.StreamHandler(stdout)
ch.setFormatter(formatter)
log.addHandler(ch)
log.setLevel(logging.DEBUG)
FORMAT="[%(msecs)s;%(filename)s:%(lineno)s:%(funcName)15s()] %(message)s"
logging.basicConfig(format=FORMAT)
return log
log=getloggerinstance()
class SGD(object):
""" SzpieGwiazdor """
def __init__(self, **kwargs):
cfg_defaults= { 'mysql_user' : None,
'mysql_pass' : None,
'mysql_db' : None,
'mysql_host' : None }
self.c = AttrDict()
for k in cfg_defaults.keys():
if kwargs.has_key(k):
self.c[k]=kwargs[k]
elif cfg_defaults[k]:
self.c[k]=cfg_defaults[k]
log.info("przyjęto domyślne ustawienie dla %s"%k)
else:
raise Exception("ustawienia niekompletne, brak %s"%k)
self.c.db = connect(self.c.mysql_host,
self.c.mysql_user,
self.c.mysql_pass,
self.c.mysql_db )
self.c.db.autocommit(False)
self.c.cur = self.c.db.cursor()
self.recent_act = ExpiringDict(256,300)
def _amf_raw_response_parser(self, rawamf):
""" przetwarza surową odpowiedź AMF (iteruje po zawartych wiadomościach
(message), wybiera tą o numerze 1061 (trade update), zwraca body pierwszej
pasującej lub None) """
decresp = amfdec.decode_packet( rawamf )
for m in decresp.messages:
if m.body.body.has_key('type') and int(m.body.body['type'])== 1001 \
and m.body.body.has_key('data') and m.body.body['data'].has_key('data')\
and isinstance(m.body.body['data']['data'], dict):
if m.body.body['data']['errorCode']==0:
return m.body.body
else:
errcode = m.body.body['data']['errorCode']
log.error('amf error, code:%s') %errcode
return None
def _pom_remove_if_exists(self, pID, pN):
sql = """DELETE FROM gwiazda WHERE playerID = %d AND playerName = '%s';"""%(pID, pN)
self.c.cur.execute(sql)
log.info('gracz/ID %s/%d: usunięto %d pozycji'%(pN, pID, self.c.cur.rowcount))
self.c.db.commit()
def _pom_generate_objects(self, pID, pN, buffsvector):
return [self.StarItem( pID, pN, **pd ) for pd in buffsvector]
def _pom_insert_star_list(self, pID, pN, star_menu):
sqllines = [b._sql_ins_data for b in star_menu]
query= """ INSERT IGNORE INTO gwiazda
( playerID, playerName, amount,
buffName_string, resourceName_string, kiedyZlapanoTS )
VALUES ( %s,%s,%s,%s,%s,%s ) """
self.c.cur.executemany(query, sqllines)
log.info('gracz/ID %s/%d: dodano %d pozycji'%(pN, pID, self.c.cur.rowcount))
self.c.db.commit()
def _incoming_traffic_handler(self, response):
""" logika postępowania z przechwyconym ruchem (1001 response) """
resp_1001= self._amf_raw_response_parser(response)
pom = nested_lookup('playersOnMap', resp_1001)
for pd in pom[0]:
bv = pd.get('availableBuffs_vector')
if bv:
pID = pd['userID']
pN = pd['username_string']
recent_key = ":".join([str(pID),pN])
ittl= self.recent_act.ttl( recent_key )
if ittl:
log.info('gracz/ID %s/%d: ignorowanie jeszcze %d sekund'%(pN, pID, ittl))
return None
else:
adventure_cntr = dict(Counter(b['resourceName_string'] for b in [a for a in bv if a['buffName_string'] == "Adventure"]))
adventure_list = [self.StarItem(pID, pN, "Adventure", i[0], i[1]) for i in adventure_cntr.iteritems()]
otheritms_list = [self.StarItem(pID, pN, i['resourceName_string'],i['buffName_string'], i['amount']) for i in bv if i['buffName_string'] <> "Adventure"]
star_menu = adventure_list + otheritms_list
self._pom_remove_if_exists(pID, pN)
self._pom_insert_star_list(pID, pN, star_menu)
log.info('gracz/ID %s/%d: zapisano menu gwiazdy, będzie ignorowany kolejne %d sekund'%(pN, pID, self.recent_act.max_age))
self.recent_act[ recent_key ] = True
###############################################################################
class StarItem(object):
""" pozycja w menu gwiazdy """
def __init__(self, playerID, playerName, resourceName_string, \
buffName_string, amount ):
self.playerID = playerID
self.playerName = playerName
self.resourceName_string= resourceName_string
self.buffName_string= buffName_string
self.amount = amount
self.kiedyZlapanoTS = time().__int__()
def __eq__(self, o):
return self.playerID == o.playerID \
and self.playerName == o.playerName \
and self.resourceName_string == o.resourceName_string \
and self.buffName_string == o.buffName_string \
and self.amount == o.amount
@property
def _sql_ins_data(self):
""" wypluwa sql insert values:
( playerID, playerName, resourceName_string,
buffName_string, amount, kiedyZlapanoTS )
"""
return self.playerID, self.playerName, \
self.amount, self.buffName_string, \
self.resourceName_string, self.kiedyZlapanoTS
###############################################################################
from .config import dbcfg as cfg_sgd
sgd=SGD(**cfg_sgd)
###############################################################################
@concurrent
def response(context, flow):
"""========================================================================
==========================================================================="""
if flow.request.host.endswith('.thesettlersonline.pl'):
if "application/x-amf" in flow.response.headers.get("Content-Type", "_"):
with decoded(flow.response):
res = flow.response.content
if search( 'defaultGame.Communication.VO.dZoneVO', res )\
and search( 'defaultGame.Communication.VO.dBuffVO', res )\
and search( 'defaultGame.Communication.VO.dPlayerVO',res ):
log.debug("got type 1001 response... wysyłam szpiega...")
try:
t= Thread(target=sgd._incoming_traffic_handler, args=(flow.response.content,))
t.setDaemon(True)
t.start()
except (KeyboardInterrupt, SystemExit):
log.info('caught either KeyboardInterrupt or SystemExit, quitting threads')
t.__stop()
import thread
thread.interrupt_main()