/
connectionmanager.py
245 lines (203 loc) · 8.49 KB
/
connectionmanager.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#Embedded file name: scripts/client/ConnectionManager.py
import json
import hashlib
import ResMgr
import BigWorld
from account_shared import isValidClientVersion
import constants
from Event import Event
from functools import partial
from Singleton import Singleton
from enumerations import Enumeration
from helpers import getClientLanguage
from predefined_hosts import g_preDefinedHosts
from debug_utils import LOG_DEBUG, LOG_UNEXPECTED, LOG_MX, LOG_NOTE
CONNECTION_STATUS = Enumeration('Connection status', ('disconnected', 'connected', 'connectionInProgress', 'disconnectingInProgress', 'kicked'))
def getHardwareID():
import Settings
up = Settings.g_instance.userPrefs
if up.has_key(Settings.KEY_LOGIN_INFO):
loginInfo = up[Settings.KEY_LOGIN_INFO]
else:
loginInfo = up.write(Settings.KEY_LOGIN_INFO, '')
prevSalt = loginInfo.readString('salt', '')
newSalt = BigWorld.wg_cpsalt(prevSalt)
loginInfo.writeString('salt', newSalt)
return newSalt
def md5hex(concealed_value):
m = hashlib.md5()
m.update(concealed_value)
return m.hexdigest()
class AUTH_METHODS:
BASIC = 'basic'
EBANK = 'ebank'
TOKEN2 = 'token2'
TOKEN = 'token'
class ConnectionManager(Singleton):
@staticmethod
def instance():
return ConnectionManager()
def _singleton_init(self):
self.__host = g_preDefinedHosts._makeHostItem('', '', '')
self.searchServersCallbacks = Event()
self.connectionStatusCallbacks = Event()
self.__connectionStatus = CONNECTION_STATUS.disconnected
self.onConnected = Event()
self.onDisconnected = Event()
self.onKickedFromServer = Event()
self.onRejected = Event()
self.__rawStatus = ''
self.__loginName = None
self.__isVersionsDiffered = False
BigWorld.serverDiscovery.changeNotifier = self._searchServersHandler
def startSearchServers(self):
if constants.IS_DEVELOPMENT:
BigWorld.serverDiscovery.searching = 1
def stopSearchServers(self):
if constants.IS_DEVELOPMENT:
BigWorld.serverDiscovery.searching = 0
def _searchServersHandler(self):
def _serverDottedHost(ip):
return '%d.%d.%d.%d' % (ip >> 24 & 255,
ip >> 16 & 255,
ip >> 8 & 255,
ip >> 0 & 255)
def _serverNetName(details):
name = _serverDottedHost(details.ip)
if details.port:
name += ':%d' % details.port
return name
def _serverNiceName(details):
name = details.hostName
if not name:
name = _serverNetName(details)
elif details.port:
name += ':%d' % details.port
if details.ownerName:
name += ' (' + details.ownerName + ')'
return name
servers = [ (_serverNiceName(server), server.serverString) for server in BigWorld.serverDiscovery.servers ]
self.searchServersCallbacks(servers)
def connect(self, url, login, password, publicKeyPath = None, nickName = None, token2 = '', isNeedSavingPwd = False, tokenLoginParams = None):
self.disconnect()
self.__isVersionsDiffered = False
self.__setConnectionStatus(CONNECTION_STATUS.connectionInProgress)
basicLoginParams = {'login': login,
'auth_method': AUTH_METHODS.BASIC}
loginParams = tokenLoginParams if tokenLoginParams is not None else basicLoginParams
loginParams['session'] = md5hex(getHardwareID())
loginParams['auth_realm'] = constants.AUTH_REALM
loginParams['game'] = 'wot'
loginParams['temporary'] = str(int(not isNeedSavingPwd))
if nickName is not None:
loginParams['nickname'] = nickName
loginParams['auto_registration'] = 'true'
if constants.IS_IGR_ENABLED:
loginParams['is_igr'] = '1'
if token2:
loginParams['token2'] = token2
loginParams['auth_method'] = AUTH_METHODS.TOKEN2
LOG_NOTE('User authentication method: {0}'.format(loginParams['auth_method']))
clientContext = {'lang_id': getClientLanguage()}
class LoginInfo:
pass
loginInfo = LoginInfo()
loginInfo.username = json.dumps(loginParams).encode('utf8')
loginInfo.password = password
loginInfo.clientContext = json.dumps(clientContext)
loginInfo.inactivityTimeout = constants.CLIENT_INACTIVITY_TIMEOUT
if publicKeyPath is not None:
loginInfo.publicKeyPath = publicKeyPath
if login and constants.IS_DEVELOPMENT and login[0] == '@':
try:
loginInfo.username = login[1:]
except IndexError:
loginInfo.username = login
BigWorld.connect(url, loginInfo, partial(self.connectionWatcher, nickName is not None))
self.__setConnectionStatus(CONNECTION_STATUS.connectionInProgress)
self.__loginName = login
if g_preDefinedHosts.predefined(url) or g_preDefinedHosts.roaming(url):
self.__host = g_preDefinedHosts.byUrl(url)
else:
for server in BigWorld.serverDiscovery.servers:
if server.serverString == url:
self.__host = self.__host._replace(name=server.ownerName)
break
def disconnect(self):
BigWorld.disconnect()
if self.isConnected():
self.__setConnectionStatus(CONNECTION_STATUS.disconnectingInProgress)
@property
def serverUserName(self):
return self.__host.name
@property
def serverUserNameShort(self):
return self.__host.shortName
@property
def peripheryID(self):
return self.__host.peripheryID
@property
def areaID(self):
if not self.isDisconnected():
return self.__host.areaID
@property
def loginName(self):
if not self.isDisconnected():
return self.__loginName
@property
def lastLoginName(self):
return self.__loginName
@property
def databaseID(self):
if not self.isDisconnected():
return BigWorld.player().databaseID
@property
def isVersionsDiffered(self):
return self.__isVersionsDiffered
def setKickedFromServer(self, reason, isBan, expiryTime):
self.disconnect()
self.__setConnectionStatus(CONNECTION_STATUS.kicked)
self.onKickedFromServer(reason, isBan, expiryTime)
def connectionWatcher(self, isAutoRegister, stage, status, serverMsg):
self.__connectionStatusCallback(stage, status, serverMsg, isAutoRegister)
self.connectionStatusCallbacks(stage, status, serverMsg, isAutoRegister)
def checkClientServerVersions(self, clientVersion, serverVersion):
if not isValidClientVersion(clientVersion, serverVersion):
LOG_DEBUG('Version mismatch. Client is "%s", server needs "%s".' % (clientVersion, serverVersion))
self.__isVersionsDiffered = True
BigWorld.callback(0.001, BigWorld.disconnect)
else:
self.__isVersionsDiffered = False
def __connectionStatusCallback(self, stage, status, serverMsg, isAutoRegister):
LOG_MX('__connectionStatusCallback', stage, status)
self.__rawStatus = status
if stage == 0:
pass
elif stage == 1:
if status != 'LOGGED_ON':
self.__setConnectionStatus(CONNECTION_STATUS.disconnected)
self.onRejected()
else:
self.__setConnectionStatus(CONNECTION_STATUS.connected)
self.onConnected()
elif stage == 2:
pass
elif stage == 6:
self.__setConnectionStatus(CONNECTION_STATUS.disconnected)
self.onDisconnected()
else:
LOG_UNEXPECTED('stage:%d, status:%s, serverMsg:%s' % (stage, status, serverMsg))
def __setConnectionStatus(self, status):
self.__connectionStatus = status
def isDisconnected(self):
return self.__connectionStatus != CONNECTION_STATUS.connected
def isConnected(self):
return self.__connectionStatus == CONNECTION_STATUS.connected
def isUpdateClientSoftwareNeeded(self):
return ResMgr.activeContentType() in (3, 4) or self.__rawStatus in ('LOGIN_BAD_PROTOCOL_VERSION', 'LOGIN_REJECTED_BAD_DIGEST')
def isStandalone(self):
return self.peripheryID == 0
def _getClientUpdateUrl():
import Settings
return Settings.g_instance.scriptConfig.readString(Settings.KEY_UPDATE_URL)
connectionManager = ConnectionManager.instance()