Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增了代理功能 && 新增加了一站式服务 #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
更新
格式化部分文件(utf-8以及格式)
优化proxy封装
修正MajSoulWeb错误的代理
mitmdump启动前显示等待页面
自动检测mitmdump状态后启动游戏
浏览器存储自动保存及恢复(实现一次登录后免登录,游戏设置等)
bilibili12433014 committed Jun 25, 2024
commit 6a9ce459dcb3d3871952b786d768617adc736462
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -157,4 +157,5 @@ cython_debug/

config
mahjong-helper.exe
log
log
config.json
4 changes: 2 additions & 2 deletions START.py
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ def update_proxy_config(proxy):
yaml.dump(config_data, file, default_flow_style=False)

parser = argparse.ArgumentParser(description='设置代理和端口配置')
parser.add_argument('-proxy', type=str, default=None, help='指定代理上网,流量出接口,http://host:port')
parser.add_argument('-proxy', type=str, default=None, help='指定代理上网,流量出接口,格式http://host:port,例如http://localhost:10809')
parser.add_argument('-mit_port', type=int, default=23410, help='指定中间代理监听端口')
parser.add_argument('-helper_port', type=int, default=12121, help='指定小助手端监听口')

@@ -36,5 +36,5 @@ def update_proxy_config(proxy):
update_proxy_config(args.proxy)

helper = MahjongHelper(port=args.helper_port)
web = MajSoulWeb(proxy_port=args.mit_port)
web = MajSoulWeb(proxy_port=args.mit_port, helper=helper)
mitmdump = MajSoulMitmdump(proxy=args.proxy,port=args.mit_port)
4 changes: 3 additions & 1 deletion auto_play/MahjongHelper.py
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
def download_and_extract(url, extract_to="."):
local_filename = url.split("/")[-1]
if not os.path.exists("mahjong-helper.exe"):
print(f"mahjong-helper.exe不存在,正在下载\n")
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(local_filename, "wb") as f:
@@ -20,8 +21,9 @@ def download_and_extract(url, extract_to="."):
zip_ref.extractall(extract_to)

os.remove(local_filename)
print(f"mahjong-helper.exe下载完成\n")
else:
print(f"mahjong-helper.exe already exists, skipping download.")
print(f"mahjong-helper.exe已存在,跳过下载\n")


url = "https://github.com/EndlessCheng/mahjong-helper/releases/download/v0.2.8/mahjong-helper-v0.2.8-win64-x64.zip"
69 changes: 60 additions & 9 deletions auto_play/MajSoulWeb.py
Original file line number Diff line number Diff line change
@@ -10,17 +10,27 @@
import cv2
import threading
import time
import traceback
import os
from auto_play.RequestsProxy import RequestsProxy
from auto_play.MahjongHelper import MahjongHelper
import json
import atexit


class MajSoulWeb:
def __init__(
self,
proxy_port:str|int|None="23410",
proxy_port: str | int | None = "23410",
helper: MahjongHelper = None,
chrome_path="C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
):
self.proxy_server = "127.0.0.1:"+str(proxy_port) if None else None
self.helper = helper
self.proxy_server = "127.0.0.1:" + str(proxy_port) if proxy_port else None
self.proxy = RequestsProxy("http://" + self.proxy_server)
self.chrome_path = chrome_path
self.driver = self._setup_driver()
self.open_wait_page()
threading.Thread(target=self.main).start()

def _setup_driver(self):
@@ -35,12 +45,40 @@ def _setup_driver(self):
)
return driver

def open_wait_page(self, url="auto_play/wait.html"):
self.driver.get(os.path.abspath(url))

def open_game_page(self, url="https://game.maj-soul.com/1/"):
self.driver.get(url)
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.ID, "layaCanvas"))
)


def save_data(self):
# 保存Cookies
with open('config/cookies.json', 'w') as f:
json.dump(self.driver.get_cookies(), f, indent=4)

# 保存localStorage数据
local_storage = self.driver.execute_script("return JSON.stringify(localStorage);")
local_storage_data = json.loads(local_storage)
with open('config/local_storage.json', 'w') as local_file:
json.dump(local_storage_data, local_file, indent=4)

def read_data(self):
# 读取并添加Cookies
if os.path.exists('config/cookies.json'):
with open('config/cookies.json', 'r') as f:
cookies = json.load(f)
for cookie in cookies:
self.driver.add_cookie(cookie)
self.driver.refresh()

# 读取并设置localStorage数据
if os.path.exists('config/local_storage.json'):
with open('config/local_storage.json', 'r') as local_file:
local_storage_data = json.load(local_file)
for key, value in local_storage_data.items():
self.driver.execute_script(f"localStorage.setItem('{key}', '{value}');")


def get_image(self):
screenshot = self.driver.get_screenshot_as_png()
image = Image.open(BytesIO(screenshot))
@@ -87,7 +125,20 @@ def process_and_display(self):
cv2.imshow("maj-soul", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

def main(self):
time.sleep(10)
self.open_game_page()
while True:
try:
self.proxy.get("http://www.baidu.com")
self.open_game_page()
self.read_data()
atexit.register(self.save_data)
break
except:
pass

while True:
try:
exec(input())
except:
print(traceback.format_exc())
38 changes: 20 additions & 18 deletions auto_play/RequestsProxy.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
import requests
import yaml


def init_proxy_config():
config_data = {
'config': {
'proxy': None
}
}

with open('config/settings.proxy.yaml', 'w') as file:
config_data = {"config": {"proxy": None}}

with open("config/settings.proxy.yaml", "w") as file:
yaml.dump(config_data, file, default_flow_style=False)



def get_proxy():
try:
with open('config/settings.proxy.yaml', 'r') as file:
with open("config/settings.proxy.yaml", "r") as file:
proxy = yaml.load(file, Loader=yaml.FullLoader)["config"]["proxy"]
return proxy
except:
init_proxy_config()
return None


class RequestsProxy:
def __init__(self):
proxy = get_proxy()
def __init__(self, proxy=None):
if not proxy:
proxy = get_proxy()
if proxy:
self.proxies = {
'http': proxy,
'https': proxy,
"http": proxy,
"https": proxy,
}
else:
self.proxies = None

def get(self, url, **kwargs):
kwargs['proxies'] = self.proxies
kwargs["proxies"] = self.proxies
return requests.get(url, **kwargs)

def post(self, url, data=None, json=None, **kwargs):
kwargs['proxies'] = self.proxies
kwargs["proxies"] = self.proxies
return requests.post(url, data=data, json=json, **kwargs)

def put(self, url, data=None, **kwargs):
kwargs['proxies'] = self.proxies
kwargs["proxies"] = self.proxies
return requests.put(url, data=data, **kwargs)

def delete(self, url, **kwargs):
kwargs['proxies'] = self.proxies
kwargs["proxies"] = self.proxies
return requests.delete(url, **kwargs)

# ����һ��RequestsProxy��ʵ������ֵ��requests

# 创建一个RequestsProxy的实例并赋值给requests
proxy = RequestsProxy()
Binary file modified auto_play/requirements.txt
Binary file not shown.
31 changes: 31 additions & 0 deletions auto_play/wait.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>代理启动中</title>
<style>
html, body {
height: 100%;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}

.message {
text-align: center;
font-size: 24px;
line-height: 1.5;
color: #333;
}
</style>
</head>
<body>
<div class="message">
等待代理启动中<br>请稍候
</div>
</body>
</html>