forked from kakaroto/SWProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWProxy.py
executable file
·78 lines (66 loc) · 2.78 KB
/
SWProxy.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
#!/usr/bin/python
import logging
from SWParser import parse_login_data, parse_visit_data
from SWParser.smon_decryptor import decrypt_request, decrypt_response
import json
import proxy
import socket
import sys
VERSION = "0.94"
logger = logging.getLogger(__name__)
class ProxyCallback(object):
def __init__(self):
self.host = None
self.port = 0
self.request = None
self.response = None
def onRequest(self, proxy, host, port, request):
self.host = host
self.port = port
self.request = request
def onResponse(self, proxy, response):
self.response = response
if self.host and self.host.startswith("summonerswar") and \
self.host.endswith("com2us.net") and \
self.request and self.request.url.path.startswith("/api/"):
try:
req_plain = decrypt_request(self.request.body)
req_json = json.loads(req_plain)
resp_plain = decrypt_response(response.body)
resp_json = json.loads(resp_plain)
print "Found Summoners War API request : %s" % req_json['command']
if resp_json['command'] == 'HubUserLogin':
print "Monsters and Runes data generated"
parse_login_data(resp_json)
elif resp_json['command'] == 'VisitFriend':
print "Visit Friend data generated"
parse_visit_data(resp_json)
elif resp_json['command'] == 'GetUnitCollection':
collection = resp_json['collection']
print "Your collection has %d/%d monsters" % (sum([y['open'] for y in collection]), len(collection))
except:
pass
def onDone(self, proxy):
pass
class HTTP(proxy.TCP):
"""HTTP proxy server implementation.
Spawns new process to proxy accepted client connection.
"""
def handle(self, client):
callback = ProxyCallback()
proc = proxy.Proxy(client, callback)
proc.daemon = True
proc.start()
logger.debug('Started process %r to handle connection %r' % (proc, client.conn))
if __name__ == "__main__":
print "SWParser v%s - Summoners War Proxy" % VERSION
print "\tWritten by KaKaRoTo\n\nLicensed under GPLv3 and available at : \n\thttps://github.com/kakaroto/SWParser\n"
logging.basicConfig(level="ERROR", format='%(levelname)s - %(message)s')
port = 8080 if len(sys.argv) < 2 else int(sys.argv[1])
my_ip = [[(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]][0]
try:
print "Running Proxy server at %s on port %s" % (my_ip, port)
p = HTTP(my_ip, port)
p.run()
except KeyboardInterrupt:
pass