Skip to content

Commit

Permalink
fix: China login failure (#268)
Browse files Browse the repository at this point in the history
* fix: China login failure

* Fixed undefined server_region

---------

Co-authored-by: Mandar Patil <[email protected]>
  • Loading branch information
tete1030 and mandarons authored Nov 27, 2024
1 parent a3ff357 commit 1e22ae0
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ celerybeat.pid
*.sage.py

# Environments
.env
.venv
env/
venv/
Expand Down
7 changes: 5 additions & 2 deletions src/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,16 @@ def notify_discord(config, message, last_send=None, dry_run=False):
return sent_on


def send(config, username, last_send=None, dry_run=False):
def send(config, username, last_send=None, dry_run=False, region="global"):
"""Send notifications."""
sent_on = None
region_opt = ""
if region != "global":
region_opt = f"--region={region} "
message = f"""Two-step authentication for iCloud Drive, Photos (Docker) is required.
Please login to your server and authenticate. Please run -
`docker exec -it --user=abc icloud /bin/sh -c
"icloud --session-directory=/config/session_data --username={username}"`."""
"icloud --session-directory=/config/session_data {region_opt}--username={username}"`."""
subject = f"icloud-docker: Two step authentication is required for {username}"
notify_telegram(config=config, message=message, last_send=last_send, dry_run=dry_run)
notify_discord(config=config, message=message, last_send=last_send, dry_run=dry_run)
Expand Down
8 changes: 4 additions & 4 deletions src/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_api_instance(
apple_id=username,
password=password,
cookie_directory=cookie_directory,
auth_endpoint="https://www.icloud.com.cn",
home_endpoint="https://www.icloud.com.cn",
setup_endpoint="https://setup.icloud.com.cn/setup/ws/1",
)
if server_region == "china"
Expand All @@ -63,12 +63,12 @@ def sync():
username = config_parser.get_username(config=config)
if username:
try:
server_region = config_parser.get_region(config=config)
if ENV_ICLOUD_PASSWORD_KEY in os.environ:
password = os.environ.get(ENV_ICLOUD_PASSWORD_KEY)
utils.store_password_in_keyring(username=username, password=password)
else:
password = utils.get_password_from_keyring(username=username)
server_region = config_parser.get_region(config=config)
api = get_api_instance(username=username, password=password, server_region=server_region)
if not api.requires_2sa:
if "drive" in config and enable_sync_drive:
Expand All @@ -92,7 +92,7 @@ def sync():
break
next_sync = (datetime.datetime.now() + datetime.timedelta(seconds=sleep_for)).strftime("%c")
LOGGER.info(f"Retrying login at {next_sync} ...")
last_send = notify.send(config=config, username=username, last_send=last_send)
last_send = notify.send(config=config, username=username, last_send=last_send, region=server_region)
sleep(sleep_for)
continue
except exceptions.ICloudPyNoStoredPasswordAvailableException:
Expand All @@ -103,7 +103,7 @@ def sync():
break
next_sync = (datetime.datetime.now() + datetime.timedelta(seconds=sleep_for)).strftime("%c")
LOGGER.info(f"Retrying login at {next_sync} ...")
last_send = notify.send(config=config, username=username, last_send=last_send)
last_send = notify.send(config=config, username=username, last_send=last_send, region=server_region)
sleep(sleep_for)
continue

Expand Down
1 change: 1 addition & 0 deletions tests/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENV_CONFIG_FILE_PATH=./tests/data/test_config.yaml
10 changes: 10 additions & 0 deletions tests/test_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ def test_send_with_username(self):
"Subject: icloud-docker: Two step authentication is required",
instance.sendmail.mock_calls[0][2]["msg"],
)
self.assertNotIn("--region=", instance.sendmail.mock_calls[0][2]["msg"])

def test_send_with_region(self):
"""Test for email send with region."""
username = "[email protected]"
with patch("smtplib.SMTP") as smtp:
notify.send(self.config, username, region="some_region")

instance = smtp.return_value
self.assertIn("--region=some_region", instance.sendmail.mock_calls[0][2]["msg"])

def test_send_fail(self):
"""Test for failed send."""
Expand Down

0 comments on commit 1e22ae0

Please sign in to comment.