fewfef
This commit is contained in:
187
test2.py
187
test2.py
@@ -1,144 +1,63 @@
|
||||
import time
|
||||
import random
|
||||
import oss2
|
||||
import os
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
import datetime
|
||||
from loguru import *
|
||||
from curl_cffi import requests
|
||||
def upload_file_to_oss(local_file_path):
|
||||
# 建议使用环境变量存储敏感信息,这里为了示例方便,假设已通过环境变量设置
|
||||
endpoint = 'https://oss-cn-beijing.aliyuncs.com'
|
||||
access_key_id = "LTAI5tRMxrM95Pi8JEEmqRcg"
|
||||
access_key_secret = "8vueGCsRVeFyQMcAA7sysO7LSnuJDG"
|
||||
|
||||
from models.combined_table import CombinedTable
|
||||
from models.concrete_wallet import ConcreteWallet
|
||||
from models.ips import Ips
|
||||
from tools import get_evm_wallet_singed_message
|
||||
if not access_key_id or not access_key_secret:
|
||||
print('❌ 错误: 未找到有效的 OSS 访问密钥,请检查环境变量。')
|
||||
return None
|
||||
|
||||
# 生成唯一 Bucket 名称
|
||||
bucket_name = f'oss-bucket-yj'
|
||||
print(f"创建 Bucket: {bucket_name}")
|
||||
|
||||
class Credentials:
|
||||
def __init__(self, sql_info):
|
||||
# 初始化 Bucket 对象
|
||||
auth = oss2.Auth(access_key_id, access_key_secret)
|
||||
bucket = oss2.Bucket(auth, endpoint, bucket_name)
|
||||
|
||||
self.sql_info = sql_info
|
||||
try:
|
||||
# 1. 创建 Bucket
|
||||
bucket.create_bucket(oss2.models.BUCKET_ACL_PUBLIC_READ) # 设置 Bucket 为公共读权限
|
||||
print(f'✅ 成功创建 Bucket: {bucket_name}')
|
||||
except oss2.exceptions.BucketAlreadyExists:
|
||||
print(f'⚠️ Bucket {bucket_name} 已经存在')
|
||||
print('提示:请使用不同的 Bucket 名称或使用现有 Bucket')
|
||||
except oss2.exceptions.OssError as e:
|
||||
print(f'❌ OSS 错误: {e}')
|
||||
print(f' 错误码: {e.code}')
|
||||
print(f' 请求 ID: {e.request_id}')
|
||||
return None
|
||||
|
||||
self.headers = {
|
||||
'accept': '*/*',
|
||||
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
|
||||
'cache-control': 'no-cache',
|
||||
'content-type': 'application/json',
|
||||
'pragma': 'no-cache',
|
||||
'priority': 'u=1, i',
|
||||
'referer': 'https://points.concrete.xyz/home',
|
||||
'sec-ch-ua': '"Microsoft Edge";v="143", "Chromium";v="143", "Not A(Brand";v="24"',
|
||||
'sec-ch-ua-mobile': '?0',
|
||||
'sec-ch-ua-platform': '"Windows"',
|
||||
'sec-fetch-dest': 'empty',
|
||||
'sec-fetch-mode': 'cors',
|
||||
'sec-fetch-site': 'same-origin',
|
||||
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0',
|
||||
# 'cookie': 'client-season=z2zi-tzc2; domain=https%3A%2F%2Fpoints.concrete.xyz; __Host-authjs.csrf-token=afc782765e3380ca973101d02f54c9516d317120fe83faea733390bfc2e46cb4%7Cdca5d6028cadbe52f05820005ed29862e6bd8fff72ae5514142c25bf0d0ad97f; __Secure-authjs.callback-url=https%3A%2F%2Fboost.absinthe.network; redirect-pathname=%2Fhome',
|
||||
}
|
||||
# 2. 验证本地文件是否存在
|
||||
if not Path(local_file_path).exists():
|
||||
print(f'❌ 文件错误: 本地文件 {local_file_path} 不存在')
|
||||
return None
|
||||
|
||||
self.session = requests.Session()
|
||||
self.session.headers.update(self.headers)
|
||||
file_name = os.path.basename(local_file_path)
|
||||
oss_object_name = uuid.uuid4().hex[:12] + file_name
|
||||
result = bucket.put_object_from_file(oss_object_name, local_file_path)
|
||||
|
||||
def csrf(self):
|
||||
for i in range(3):
|
||||
try:
|
||||
response = self.session.get('https://points.concrete.xyz/api/auth/csrf', )
|
||||
if result.status == 200:
|
||||
print(f'✅ 文件上传成功')
|
||||
print(f' ETag: {result.etag}')
|
||||
print(f' 文件大小: {result.resp.headers.get("content-length")} bytes')
|
||||
|
||||
return response.json()["csrfToken"]
|
||||
except:
|
||||
time.sleep(random.randint(1, 5))
|
||||
# 生成长期可访问的 URL
|
||||
public_url = f'https://{bucket_name}.{endpoint.replace("https://", "")}/{oss_object_name}'
|
||||
print(f' 文件公共 URL(长期有效): {public_url}')
|
||||
return public_url
|
||||
else:
|
||||
print(f'❌ 文件上传失败,状态码: {result.status}')
|
||||
return None
|
||||
|
||||
return False
|
||||
|
||||
def credentials(self, message, signature, csrfToken):
|
||||
params = ''
|
||||
|
||||
data = {
|
||||
'message': message,
|
||||
'redirect': 'false',
|
||||
'signature': "0x" + signature,
|
||||
'csrfToken': csrfToken,
|
||||
'callbackUrl': 'https://points.concrete.xyz/home',
|
||||
}
|
||||
|
||||
for i in range(3):
|
||||
try:
|
||||
response = self.session.post(
|
||||
'https://points.concrete.xyz/api/auth/callback/credentials',
|
||||
params=params,
|
||||
data=data,
|
||||
)
|
||||
print(response.json())
|
||||
|
||||
return True
|
||||
except:
|
||||
time.sleep(random.randint(1, 5))
|
||||
|
||||
return False
|
||||
|
||||
def add_ips_to_combined_table(self, ):
|
||||
# 获取 ips 表中的所有记录
|
||||
all_ips_records = Ips.select()
|
||||
for ips_record in all_ips_records:
|
||||
# 检查该记录是否已经存在于 CombinedTable 中
|
||||
existing_record = CombinedTable.get_or_none(
|
||||
CombinedTable.host == ips_record.host,
|
||||
CombinedTable.port == ips_record.port,
|
||||
CombinedTable.username == ips_record.username,
|
||||
CombinedTable.pwd == ips_record.pwd
|
||||
)
|
||||
if not existing_record:
|
||||
# 如果记录不存在,则将其添加到 CombinedTable 中
|
||||
# 这里假设 mnemonic 字段有默认值或者你可以根据需求传入具体值
|
||||
|
||||
self.com_info.host = ips_record.host
|
||||
self.com_info.port = ips_record.port
|
||||
self.com_info.username = ips_record.username
|
||||
self.com_info.pwd = ips_record.pwd
|
||||
self.com_info.save()
|
||||
|
||||
print("成功添加记录到 CombinedTable。")
|
||||
return True
|
||||
|
||||
print("ips 表中所有数据在 CombinedTable 中都已存在,结束操作。")
|
||||
return False
|
||||
|
||||
def action(self):
|
||||
|
||||
self.com_info, type1 = CombinedTable.get_or_create(
|
||||
mnemonic=self.sql_info.mnemonic,
|
||||
)
|
||||
|
||||
if not self.com_info.host:
|
||||
if self.add_ips_to_combined_table():
|
||||
logger.info("添加ip成功!!!")
|
||||
else:
|
||||
logger.error("没有ip了")
|
||||
return
|
||||
|
||||
proxies = {
|
||||
'http': f'socks5://{self.com_info.username}:{self.com_info.pwd}@{self.com_info.host}:{self.com_info.port}',
|
||||
'https': f'socks5://{self.com_info.username}:{self.com_info.pwd}@{self.com_info.host}:{self.com_info.port}',
|
||||
}
|
||||
self.session.proxies.update(proxies)
|
||||
|
||||
csrf_token = self.csrf()
|
||||
if csrf_token:
|
||||
logger.info("获取签名信息成功!!!")
|
||||
else:
|
||||
logger.error("获取签名信息失败!!!")
|
||||
|
||||
# 获取当前 UTC 时间
|
||||
current_utc_time = datetime.datetime.now(datetime.timezone.utc)
|
||||
original_msg = f"points.concrete.xyz wants you to sign in with your Ethereum account:\n{self.sql_info.address}\n\nPlease sign with your account\n\nURI: https://points.concrete.xyz\nVersion: 1\nChain ID: 1\nNonce: {csrf_token}\nIssued At: {current_utc_time.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + 'Z'}\nResources:\n- connector://metaMask"
|
||||
signature = get_evm_wallet_singed_message(
|
||||
original_msg=original_msg,
|
||||
private_key=self.sql_info.private_key,
|
||||
|
||||
)
|
||||
|
||||
self.credentials(message=original_msg, signature=signature, csrfToken=csrf_token)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
for concrete_info in ConcreteWallet.select():
|
||||
credentials = Credentials(sql_info=concrete_info)
|
||||
credentials.action()
|
||||
# 示例调用
|
||||
local_file = r'C:\Users\27942\Desktop\codes\lm_code\test.py'
|
||||
signed_url = upload_file_to_oss(local_file)
|
||||
if signed_url:
|
||||
print(f"可访问文件的 URL: {signed_url}")
|
||||
Reference in New Issue
Block a user