Skip to content

Commit

Permalink
fix: captcha issue & perf: humanized input
Browse files Browse the repository at this point in the history
  • Loading branch information
Yudaotor committed Aug 21, 2023
1 parent e955ef0 commit d75ab59
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 29 deletions.
74 changes: 50 additions & 24 deletions Handler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import random
import time
from traceback import format_exc

Expand Down Expand Up @@ -79,13 +80,29 @@ def automaticLogIn(self, username, password) -> bool:
wait = WebDriverWait(self.driver, 10)
usernameInput = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "input[name=username]")))
usernameInput.send_keys(Keys.CONTROL, 'a')
usernameInput.send_keys(username)
for character in username:
usernameInput.send_keys(character)
time.sleep(random.uniform(0.02, 0.1))
time.sleep(1)
passwordInput = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "input[name=password]")))
passwordInput.send_keys(Keys.CONTROL, 'a')
passwordInput.send_keys(password)
submitButton = wait.until(ec.presence_of_element_located((By.XPATH, "/html/body/div[2]/div/div/div[2]/div/div/button")))
for character in password:
passwordInput.send_keys(character)
time.sleep(random.uniform(0.02, 0.1))
time.sleep(1)
submitButton = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "button[data-testid=btn-signin-submit]")))
self.driver.execute_script("arguments[0].click();", submitButton)
try:
captchaIframe = WebDriverWait(self.driver, 5).until(ec.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src*='captcha']")))
captchaDiv = WebDriverWait(self.driver, 5).until(ec.presence_of_element_located((By.CSS_SELECTOR, "div.interface-wrapper")))
print(_("遇到图片验证码", "red", self.config.language))
if captchaIframe:
self.driver.switch_to.default_content()
print(username + _(" 失败", "red", self.config.language))
self.log.error(username + _log(" 失败", self.config.language))
return False
except Exception:
pass
return True
except Exception:
self.log.error(username + _log(" 失败", self.config.language))
Expand All @@ -111,16 +128,23 @@ def changePassword(self, username, password, newPassword, delimiter) -> bool:
bool: True if the password change is successful, False otherwise.
"""
try:
self.driver.find_element(by=By.XPATH, value='//*[@id="riot-account"]/div/div[2]/div/div[2]/div[1]/div/div/input').send_keys(password)
time.sleep(1)
self.driver.find_element(by=By.XPATH, value='//*[@id="riot-account"]/div/div[2]/div/div[2]/div[3]/div/input').send_keys(newPassword)
time.sleep(1)
self.driver.find_element(by=By.XPATH, value='//*[@id="riot-account"]/div/div[2]/div/div[2]/div[2]/div/input').send_keys(newPassword)
time.sleep(1)
self.driver.find_element(by=By.XPATH, value='//*[@id="riot-account"]/div/div[2]/div/div[2]/div[1]/div/div/input').click()
currentPasswordInput = self.driver.find_element(by=By.CSS_SELECTOR, value='input[data-testid=password-card__currentPassword]')
for character in password:
currentPasswordInput.send_keys(character)
time.sleep(random.uniform(0.02, 0.1))
time.sleep(0.5)
newPasswordInput = self.driver.find_element(by=By.CSS_SELECTOR, value='input[data-testid=password-card__newPassword]')
for character in newPassword:
newPasswordInput.send_keys(character)
time.sleep(random.uniform(0.02, 0.1))
time.sleep(0.5)
confirmNewPasswordInput = self.driver.find_element(by=By.CSS_SELECTOR, value='input[data-testid=password-card__confirmNewPassword]')
for character in newPassword:
confirmNewPasswordInput.send_keys(character)
time.sleep(random.uniform(0.02, 0.1))
time.sleep(0.5)
self.driver.find_element(by=By.CSS_SELECTOR, value='button[data-testid=password-card__submit-btn]').click()
time.sleep(1)
self.driver.find_element(by=By.XPATH, value='//*[@id="riot-account"]/div/div[2]/div/div[3]/button[2]').click()
time.sleep(2)
Export(delimiter).writeSuccAcc(username, newPassword)
self.log.info(username + _log(" 成功", self.config.language))
print(username + _(" 成功", "green", self.config.language))
Expand All @@ -141,10 +165,13 @@ def accountLogOut(self):
None
"""
try:
self.driver.find_element(by=By.XPATH, value='//*[@id="riotbar-account-bar"]/div/div').click()
time.sleep(1)
self.driver.find_element(by=By.XPATH, value='//*[@id="riotbar-account-dropdown-links"]/a[3]').click()
time.sleep(2)
# self.driver.find_element(by=By.XPATH, value='//*[@id="riotbar-account-bar"]/div/div').click()
# time.sleep(1)
# self.driver.find_element(by=By.XPATH, value='//*[@id="riotbar-account-dropdown-links"]/a[3]').click()
# time.sleep(2)
# self.driver.delete_all_cookies()
# self.driver.refresh()
pass
except Exception:
print(_("登出时发生错误", "red", self.config.language))
self.log.error(_log("登出时发生错误", self.config.language))
Expand All @@ -167,19 +194,18 @@ def imapLogIn(self, imapUsername, imapPassword, imapServer, imapDelay) -> bool:
time.sleep(imapDelay)
req = self.IMAPHook(imapUsername, imapPassword, imapServer)
if req is None or req.code == "":
self.log.error(_log("IMAP获取验证码失败", self.config.language))
print(_("IMAP获取验证码失败", "red", self.config.language))
self.log.error(_log("IMAP获取验证码失败 请检查是否打开了IMAP", self.config.language))
print(_("IMAP获取验证码失败 请检查是否打开了IMAP", "red", self.config.language))
return False
self.driver.find_element(by=By.XPATH, value='/html/body/div[2]/div/div/div[2]/div/div/div[2]/div/div/div[1]/div/input').send_keys(req.code)
self.driver.find_element(by=By.XPATH, value='/html/body/div[2]/div/div/div[2]/div/div/button').click()
twoFaCodeInput = self.driver.find_element(by=By.CSS_SELECTOR, value='input[data-testid=input-mfa]')
twoFaCodeInput.send_keys(req.code)
time.sleep(1)
self.driver.find_element(by=By.CSS_SELECTOR, value='button[type=submit]').click()
time.sleep(3)
self.driver.implicitly_wait(10)
buttonNumber = self.driver.find_elements(By.XPATH, '/html/body/div[2]/div[1]/div[2]/div[2]/div[2]/div/div[1]')
if len(req.code) == 6 and len(buttonNumber) > 0:
if len(req.code) == 6:
return True
else:
self.driver.delete_all_cookies()
self.driver.refresh()
self.log.error(imapUsername + _log(" 邮箱验证码获取失败", self.config.language))
print(imapUsername + _(" 邮箱验证码获取失败", "red", self.config.language))
return False
Expand Down
4 changes: 4 additions & 0 deletions I18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"改密时发生错误": "An error occurred while changing password",
"登出时发生错误": "An error occurred while logging out",
"IMAP获取验证码失败": "IMAP failed to get verification code",
"IMAP获取验证码失败 请检查是否打开了IMAP": "IMAP failed to get verification code.Please check imap is on?",
" 邮箱验证码获取失败": " Email verification code acquisition failed",
"webDriver创建失败!": "webDriver creation failed!",
"按回车键退出...": "Press Enter to exit...",
Expand All @@ -30,6 +31,7 @@
"创建webdriver失败,请检查是否对应浏览器是否已经是最新版本.": "Failed to create webdriver, please check whether the corresponding browser is the latest version.",
"检测到您使用的是网易邮箱, 请注意网易邮箱的IMAP功能需要手动开启,以及需要的是授权码而非密码": "It is detected that you are using NetEase mailbox. Please note that the IMAP function of NetEase mailbox needs to be turned on manually, and the authorization code is required instead of the password",
"延迟格式错误, 已重置为默认值10": "Delay format error, reset to default value 10",
"遇到图片验证码": "Encounter image captcha",
}
zhTWI18n = {
"语言格式错误, 已重置为默认值zh_CN": "語言格式錯誤,已重置為默認值zh_CN",
Expand All @@ -47,6 +49,7 @@
'请我喝杯咖啡吧~https://github.com/Yudaotor': '請我喝杯咖啡吧~https://github.com/Yudaotor',
" 成功": " 成功",
"改密时发生错误": "改密時發生錯誤",
"IMAP获取验证码失败 请检查是否打开了IMAP": "IMAP獲取驗證碼失敗 請檢查是否打開了IMAP",
"登出时发生错误": "登出時發生錯誤",
"IMAP获取验证码失败": "IMAP獲取驗證碼失敗",
" 邮箱验证码获取失败": " 郵箱驗證碼獲取失敗",
Expand All @@ -63,6 +66,7 @@
"检测到您使用的是网易邮箱, 请注意网易邮箱的IMAP功能需要手动开启,以及需要的是授权码而非密码": "檢測到您使用的是網易郵箱,請注意網易郵箱的IMAP功能需要手動開啟,以及需要的是授權碼而非密碼",
"延迟格式错误, 已重置为默认值10": "延遲格式錯誤,已重置為默認值10",
"密码强度不足": "密碼強度不足",
"遇到图片验证码": "遇到圖片驗證碼",
}


Expand Down
2 changes: 1 addition & 1 deletion Logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
BACKUP_COUNT = 5
PROGRAM_NAME = "AutoChangePassword"
GITHUB_ADDRESS = "https://github.com/Yudaotor/Riot-Accounts-AutoChangePassword"
version = "2.5"
version = "2.6"


class Logger:
Expand Down
3 changes: 3 additions & 0 deletions Webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def addWebdriverOptions(options):
options.add_argument("--disable-gpu")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
options.add_argument('disable-infobars')
options.add_argument("--disable-gpu-shader-disk-cache")
options.add_argument("--disable-application-cache")
options.add_argument("--disable-cache")
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.41"
options.add_argument(f'user-agent={user_agent}')
return options
Expand Down
33 changes: 29 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def init() -> tuple[logging.Logger, Config, webdriver.Edge, Handler]:
return log, config, driver, handler


CURRENT_VERSION = 2.5
CURRENT_VERSION = 2.6
log, config, driver, handler = init()

if not VersionManager.isLatestVersion(CURRENT_VERSION):
Expand All @@ -62,7 +62,7 @@ def init() -> tuple[logging.Logger, Config, webdriver.Edge, Handler]:
print("[yellow]--------------------------------------------------------------------[/]")


def main(config: Config):
def main(config, handler):
print(f"[bold yellow]{'-' * 15}[/]"
f"v{CURRENT_VERSION} "
f"{_('程序启动', 'bold yellow', config.language)} "
Expand All @@ -83,14 +83,39 @@ def main(config: Config):
handler.accountLogOut()
elif handler.changePassword(sp[0], sp[1], config.newPassword, config.accountDelimiter):
handler.accountLogOut()
line = f.readline()
line = f.readline()
if line:
try:
handler.driver.quit()
driver = Webdriver(browser=config.browser, config=config, log=log).createWebdriver()
driver.get('https://auth.riotgames.com/login#acr_values=urn%3Ariot%3Agold&client_id=accountodactyl-prod&redirect_uri'
'=https%3A%2F%2Faccount.riotgames.com%2Foauth2%2Flog-in&response_type=code&scope=openid%20email%20profile'
'%20riot%3A%2F%2Friot.atlas%2Faccounts.edit%20riot%3A%2F%2Friot.atlas%2Faccounts%2Fpassword.edit%20riot'
'%3A%2F%2Friot.atlas%2Faccounts%2Femail.edit%20riot%3A%2F%2Friot.atlas%2Faccounts.auth%20riot%3A%2F'
'%2Fthird_party.revoke%20riot%3A%2F%2Fthird_party.query%20riot%3A%2F%2Fforgetme%2Fnotify.write%20riot%3A'
'%2F%2Friot.authenticator%2Fauth.code%20riot%3A%2F%2Friot.authenticator%2Fauthz.edit%20riot%3A%2F%2Frso'
'%2Fmfa%2Fdevice.write%20riot%3A%2F%2Friot.authenticator%2Fidentity.add&state=547c8cd2-9eb0-4302-b9b2'
'-f29ee843a4bd&ui_locales=zh-Hans')
except Exception:
print(_("webDriver创建失败!", "red", config.language))
print(sp[0] + " " + _("失败", "red", config.language))
log.error(_log("webDriver创建失败!"))
log.error(sp[0] + " " + _log("失败", config.language))
log.error(format_exc())
if driver:
driver.quit()
# 载入网页
driver.implicitly_wait(10) # 隐式等待时间
driver.maximize_window() # 最大化窗口
handler = Handler(log=log, driver=driver, config=config)
handler.acceptCookies()
except Exception:
log.error(format_exc())


if __name__ == '__main__':
try:
main(config)
main(config, handler)
except (KeyboardInterrupt, SystemExit):
print(f"[bold yellow]{'-' * 15}[/]"
f"{_('程序结束', 'bold yellow', config.language)} "
Expand Down

0 comments on commit d75ab59

Please sign in to comment.