diff --git a/login-bing/Dockerfile b/login-bing/Dockerfile new file mode 100644 index 0000000..3aaf5f2 --- /dev/null +++ b/login-bing/Dockerfile @@ -0,0 +1,21 @@ +FROM python:3.11.3-slim-buster + +ENV DEBIAN_FRONTEND noninteractive + +WORKDIR /bing + +COPY chrome-linux/chromedriver chrome-linux/google-chrome-stable_current_amd64.deb requirements.txt login.py send_mail.py /bing/ + +RUN apt update && apt install -y cron + +RUN pip install -r requirements.txt + +RUN dpkg -i google-chrome-stable_current_amd64.deb;exit 0 + +RUN apt install -f -y + +RUN rm -rf google-chrome-stable_current_amd64.deb requirements.txt /var/lib/apt + +RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime + +CMD crontab /etc/cron.d/loginbing && cron && tail -f /bing/cron.log diff --git a/login-bing/README.md b/login-bing/README.md new file mode 100644 index 0000000..2dc6fe2 --- /dev/null +++ b/login-bing/README.md @@ -0,0 +1 @@ +## 定时自动刷新New Bing Cookie diff --git a/login-bing/chrome-linux/chromedriver b/login-bing/chrome-linux/chromedriver new file mode 100755 index 0000000..93d13bf Binary files /dev/null and b/login-bing/chrome-linux/chromedriver differ diff --git a/login-bing/chrome-linux/google-chrome-stable_current_amd64.deb b/login-bing/chrome-linux/google-chrome-stable_current_amd64.deb new file mode 100644 index 0000000..3669f4e Binary files /dev/null and b/login-bing/chrome-linux/google-chrome-stable_current_amd64.deb differ diff --git a/login-bing/cronfile b/login-bing/cronfile new file mode 100644 index 0000000..d2381bb --- /dev/null +++ b/login-bing/cronfile @@ -0,0 +1 @@ +20 23 7,17,27 * * . /bing/env && /usr/local/bin/python /bing/login.py >> /bing/cron.log 2>&1 diff --git a/login-bing/env.example b/login-bing/env.example new file mode 100644 index 0000000..8e5a649 --- /dev/null +++ b/login-bing/env.example @@ -0,0 +1,3 @@ +export BING_ACCOUNT_LIST=[{"user": "user", "password": "passwd"}] +export MAIL_SENDER= +export MAIL_SENDER_PASSWD= diff --git a/login-bing/login.py b/login-bing/login.py new file mode 100644 index 0000000..b01f84c --- /dev/null +++ b/login-bing/login.py @@ -0,0 +1,74 @@ +# coding=utf-8 + +import json +import os +import time +import traceback +from datetime import datetime + +from selenium import webdriver +from selenium.webdriver.chrome.options import Options +from selenium.webdriver.common.by import By +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.support.wait import WebDriverWait + +from send_mail import send_mail + +LOGN_URL = 'https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=13&id=264960&wreply=https%3a%2f%2fwww.bing.com%2fsecure%2fPassport.aspx%3fedge_suppress_profile_switch%3d1%26requrl%3dhttps%253a%252f%252fwww.bing.com%252f%253fwlexpsignin%253d1%26sig%3d3D7C913181BC62133D8C83DD80F96371&wp=MBI_SSL&lc=2052&CSRFToken=8af3d639-be36-45c6-a5b6-0c50b7d74e12&aadredir=1' + +BING_ACCOUNT_LIST = json.loads(os.environ.get('BING_ACCOUNT_LIST', '[]')) + +MAIL_SENDER = os.environ.get('MAIL_SENDER') +MAIL_SENDER_PASSWD = os.environ.get('MAIL_SENDER_PASSWD') +MAIL_RECEIVER = os.environ.get('MAIL_RECEIVER') +MAIL_RECEIVER = MAIL_RECEIVER or MAIL_SENDER + + +def login(index, user_name, passwd): + print('[%s] login: %s' % (datetime.now(), user_name)) + options = Options() + options.add_argument('--headless=new') + options.add_argument('--no-sandbox') + options.add_argument('--disable-dev-shm-usage') + driver = webdriver.Chrome(options=options, executable_path='/bing/chromedriver') + driver.get(LOGN_URL) + emali_input = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.NAME, 'loginfmt'))) + emali_input.send_keys(user_name) + next_bt = driver.find_element(By.ID, 'idSIButton9') + next_bt.click() + time.sleep(30) + passwd_input = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.ID, 'i0118'))) + passwd_input.send_keys(passwd) + submit_bt = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.ID, 'idSIButton9'))) + submit_bt.click() + time.sleep(30) + submit_bt = driver.find_element(By.ID, 'idSIButton9') + submit_bt.click() + # wait for redirect to www.bing.com + time.sleep(30) + cookies = driver.get_cookies() + with open('/bing/cookies/cookie{}.json'.format(index), 'w') as f: + json.dump(cookies, f) + f.flush() + print('[%s] %s login success!' % (datetime.now(), user_name)) + + +if __name__ == '__main__': + try: + for index in range(len(BING_ACCOUNT_LIST)): + login(index, BING_ACCOUNT_LIST[index]['user'], BING_ACCOUNT_LIST[index]['password']) + send_mail( + MAIL_SENDER, + MAIL_SENDER_PASSWD, + MAIL_RECEIVER, + 'Bing Cookie刷新', + '成功刷新以下账号的cookie: \n' + '\n'.join([x['user'] for x in BING_ACCOUNT_LIST]), + ) + except Exception as e: + send_mail( + MAIL_SENDER, + MAIL_SENDER_PASSWD, + MAIL_RECEIVER, + 'Bing Cookie刷新', + '刷新遇到异常: \n' + traceback.format_exc(), + ) diff --git a/login-bing/requirements.txt b/login-bing/requirements.txt new file mode 100644 index 0000000..dfbdc6d --- /dev/null +++ b/login-bing/requirements.txt @@ -0,0 +1 @@ +selenium==4.8.2 diff --git a/login-bing/send_mail.py b/login-bing/send_mail.py new file mode 100644 index 0000000..0bbd88b --- /dev/null +++ b/login-bing/send_mail.py @@ -0,0 +1,20 @@ +# coding=utf-8 + +from email.header import Header +from email.mime.text import MIMEText +from smtplib import SMTP_SSL + +HOST_SERVER = 'smtp.qq.com' + + +def send_mail(sender, sender_passwd, receiver, subject, body): + smtp = SMTP_SSL(HOST_SERVER, port=465) + smtp.ehlo(HOST_SERVER) + smtp.login(sender, sender_passwd) + + msg = MIMEText(body, "plain", 'utf-8') + msg["Subject"] = Header(subject, 'utf-8') + msg["From"] = sender + msg["To"] = receiver + smtp.sendmail(sender, receiver, msg.as_string()) + smtp.quit() diff --git a/login-bing/start.sh b/login-bing/start.sh new file mode 100644 index 0000000..e822468 --- /dev/null +++ b/login-bing/start.sh @@ -0,0 +1,10 @@ +count=$(docker ps -a | grep login-bing | wc -l) +if [ $count -gt 0 ]; then + docker stop login-bing && docker rm login-bing +fi + +if [ -f cron.log ];then + touch cron.log +fi + +docker run --name login-bing -d -v $(pwd)/cronfile:/etc/cron.d/loginbing -v $(pwd)/cookies:/bing/cookies -v $(pwd)/cron.log:/bing/cron.log -v $(pwd)/env:/bing/env yy194131/login-bing:$1