-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch.py
289 lines (243 loc) · 10.7 KB
/
launch.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os, sys
from colorama import init
init(autoreset=True)
import requests
import json
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import hashlib
from getmac import get_mac_address
import wmi
config = {}
account_info = {}
def reboot(text = "The application will reboot and try to login again."):
print_warning(text)
os.execl(sys.executable, os.path.abspath(__file__), *sys.argv)
def print_warning(text):
print("\033[0;33m" + "WARNING: "+ text + "\033[0m")
def print_error(text, serious = False, reboot = True):
print("\033[0;31m" + "ERROR: " + text + "\033[0m")
if serious:
exit(0)
if reboot:
reboot()
def print_green(text):
print("\033[0;32m" + text + "\033[0m")
def read_setting(file_name):
with open(file_name, "r", encoding="UTF-8") as f:
j = json.load(f)
return j
def read_config_setting(setting, serious = False, print_error = True):
global config
if setting not in config:
if print_error:
print_error("Config setting not found: " + setting, serious=serious, reboot=False)
return 0
return config[setting]
def get_user_cookie(login_id, password):
print("Loading login environment...")
# GET login url
session = requests.session()
# try to get login_url in 5 times
if read_config_setting("proxy", print_error=False) == 0:
proxy = {}
else:
proxy = {
"https":read_config_setting("proxy"),
"http":read_config_setting("proxy")
}
for i in range(5):
try:
req = session.get("https://apidgp-gameplayer.games.dmm.com/v5/loginurl", timeout=5, proxies=proxy)
break
except:
time.sleep(0.5)
continue
else:
print_error("Get login url failed.")
loginurl = json.loads(req.text)["data"]["url"]
# Initialize webdriver
try:
print("Setting up login browser...")
browser = read_config_setting("browser", True)
driver_path = read_config_setting("driver_path", True)
if_headless = not read_config_setting("show_browser_when_login")
if_stable_login = read_config_setting("stable_login")
def set_options(option):
if not if_stable_login:
if if_headless:
option.add_argument("--headless")
option.add_argument("--disable-gpu")
option.add_argument("--no-sandbox")
option.add_argument("--disable-dev-shm-usage")
option.add_argument("--disable-extensions")
option.add_argument("blink-settings=imagesEnabled=false")
prefs = {
"profile.managed_default_content_settings.images": 2,
"permissions.default.stylesheet": 2
}
option.add_experimental_option("prefs", prefs)
option.add_experimental_option("excludeSwitches", ["enable-logging"])
if read_config_setting("proxy",print_error=False) != 0:
print("Browser will use proxy: "+read_config_setting("proxy"))
option.add_argument("--proxy-server="+read_config_setting("proxy"))
return option
if browser == "Chrome":
from selenium.webdriver.chrome.service import Service
driver = webdriver.Edge(service=Service(driver_path), options=set_options(webdriver.ChromeOptions()))
elif browser == "Firefox":
from selenium.webdriver.firefox.service import Service
driver = webdriver.Edge(service=Service(driver_path), options=set_options(webdriver.FirefoxOptions()))
elif browser == "Edge":
from selenium.webdriver.edge.service import Service
driver = webdriver.Edge(service=Service(driver_path), options=set_options(webdriver.EdgeOptions()))
else:
print_error("Browser not supported: " + browser, serious=True)
except Exception as e:
print_error("Initialize webdriver failed: " + str(e), serious=True)
# Simulate manual login
print("Connecting to the login website...")
driver.get(loginurl)
if driver.page_source.find("not available in your region") != -1:
print_error("Your IP address is forbidden. Please use Japan IP to login.", serious=True)
driver.find_element(by=By.ID,value='login_id').send_keys(login_id)
driver.find_element(by=By.ID,value='password').send_keys(password)
driver.find_element(by=By.XPATH,value='//*[@id="loginbutton_script_on"]/span/input').click()
# Wait until get login cookies
print("Waiting for cookies...")
while True:
if driver.get_cookie("login_session_id") != None:
driver_cookies = driver.get_cookies()
break
for cookies in driver_cookies:
if cookies['name'] == 'login_session_id':
break
else:
print_error("Fail to get login_session_id")
driver.close()
user_cookies = {c['name']:c['value'] for c in driver_cookies}
return user_cookies
def get_game_launch_args(game_info,user_cookies,mac_address,hdd_serial,motherboard):
print("Get game launching arguments...")
if read_config_setting("proxy",print_error=False) == 0:
proxy = {}
else:
proxy = {
"https":read_config_setting("proxy"),
"http":read_config_setting("proxy")
}
header = {
"Accept-Encoding" : "gzip, deflate, br",
"User-Agent": "DMMGamePlayer5-Win/5.0.119 Electron/17.2.0",
"Client-App": "DMMGamePlayer5",
"Client-version": "5.0.119",
"Sec-Fetch-Dest":"empty",
"Sec-Fetch-Mode":"no-cors",
"Sec-Fetch-Site":"none"
}
get_game_info_json = {
"product_id": game_info["product_id"],
"game_type": game_info["game_type"],
"game_os": "win",
"launch_type": game_info["launch_type"],
"mac_address": mac_address,
"hdd_serial": hdd_serial,
"motherboard": motherboard,
"user_os": "win"
}
def cope_request_fail(response,text):
if response["result_code"] != 100:
if response["error"] == "DMM session has expired":
with open("account_info.json", "w", encoding="UTF-8") as f:
account_info["cookies"] = {}
json.dump(account_info, f, indent=4)
print_error("DMM session has expired.", reboot = False)
reboot("The application will reboot and try to login without cookies.")
raise print_error(response["error"])
session = requests.session()
req = session.post(
"https://apidgp-gameplayer.games.dmm.com/v5/gameinfo",
json = get_game_info_json,
headers = header,
cookies = user_cookies,
proxies = proxy
)
cope_request_fail(json.loads(req.text),"Get game info failed: ")
args = session.post(
"https://apidgp-gameplayer.games.dmm.com/v5/launch/cl",
json = get_game_info_json,
headers = header,
cookies = user_cookies,
proxies = proxy
)
cope_request_fail(json.loads(args.text),"Get game start argument failed: ")
req = session.post(
"https://apidgp-gameplayer.games.dmm.com/v5/report",
json=
{
"type":"start",
"product_id":game_info["product_id"],
"game_type":game_info["game_type"]
},
headers = header,
cookies = user_cookies,
proxies = proxy
)
cope_request_fail(json.loads(req.text),"Report failed: ")
return json.loads(args.text)["data"]["execute_args"]
if __name__ == "__main__":
try:
os.chdir(sys.path[0])
config = read_setting("config.json")
account_info = read_setting("account_info.json")
if "user" not in account_info:
print_error("Cannot read user info in account_info.json.")
if "game_exe_path" not in config:
print_error("Cannot read the game exe path in config.json.",serious=True)
account = account_info["user"]
if "mac_address" not in account:
account_info["user"]["mac_address"] = get_mac_address()
if "hdd_serial" not in account:
account_info["user"]["hdd_serial"] = hashlib.sha256((",".join(
item.SerialNumber.strip(" ") for item in wmi.WMI().Win32_PhysicalMedia()
)).encode('UTF-8')).hexdigest() # not real hdd serial
if "motherboard" not in account:
account_info["user"]["motherboard"] = hashlib.sha256((account["hdd_serial"] + account["mac_address"])
.encode('UTF-8')).hexdigest() # not real motherboard
use_cookies = False
if "cookies" in account_info:
cookies = account_info["cookies"]
if "login_session_id" in cookies and "login_secure_id" in cookies:
print("Program will use cookies in account_info.json")
use_cookies = True
else:
print_warning("Cookies is missing or invalid. Program will generate new cookies.")
else:
print_warning("Cookies is not exist. Program will generate new cookies.")
def load_cookies():
if "login_id" not in account:
print_error("Account info is not exist or incompleted: Missing login_id",serious=True)
if "password" not in account:
print_error("Account info is not exist or incompleted: Missing password",serious=True)
user_cookies = get_user_cookie(account_info["user"]["login_id"], account_info["user"]["password"])
account_info["cookies"] = user_cookies
if not read_config_setting("not_store_cookies"):
with open("account_info.json", "w", encoding="UTF-8") as f:
json.dump(account_info, f, indent=4)
if not use_cookies:
load_cookies()
print_green("Succesfully get user cookies.")
args = get_game_launch_args(read_config_setting("game_info", True),
account_info["cookies"],
account_info["user"]["mac_address"],
account_info["user"]["hdd_serial"],
account_info["user"]["motherboard"])
os.system('start "" {} {} '.format(config["game_exe_path"], args))
print_green("Succesfully get game launching arguments.")
print("Program will exit 3 seconds later.")
time.sleep(3)
exit(0)
except Exception as e:
print_error(str(e))
exit(1)