Skip to content

Commit cccefdc

Browse files
committed
2 parents 2fbc427 + 4d3b5ff commit cccefdc

File tree

14 files changed

+253
-122
lines changed

14 files changed

+253
-122
lines changed

README.md

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,40 @@
102102
## 配置文件
103103

104104
```yml
105-
# 是否不使用 BMCLAPI 分发的证书, 同 CLUSTER_BYOC
106-
byoc: false
107-
# OpenBMCLAPI 的 CLUSTER_ID
108-
cluster_id: ''
109-
# OpenBMCLAPI 的 CLUSTER_SECRET
110-
cluster_secret: ''
111-
# 同步文件时最多打开的连接数量
112-
download_threads: 64
113-
# 超时时间
114-
timeout: 30
115-
# 实际开放的公网主机名, 同 CLUSTER_IP
116-
web_host: ''
117-
# 要监听的本地端口, 同 CLUSTER_PORT
118-
web_port: 8800
119-
# 实际开放的公网端口, 同 CLUSTER_PUBLIC_PORT
120-
web_publicport: 8800
121-
io_buffer: 16777216
122-
max_download: 64
123-
min_rate: 500
124-
min_rate_timestamp: 1000
125-
port: 8800
126-
public_host: ''
127-
public_port: null
128-
server_name: TTB-Network
105+
advanced:
106+
# 新连接读取数据头大小
107+
header_bytes: 4096
108+
# 数据传输缓存大小
109+
io_buffer: 16777216
110+
# 最小读取速率(Bytes)
111+
min_rate: 500
112+
# 最小读取速率时间
113+
min_rate_timestamp: 1000
114+
# 请求缓存大小
115+
request_buffer: 8192
116+
# 超时时间
117+
timeout: 30
118+
cluster:
119+
# 是否不使用 BMCLAPI 分发的证书, 同 CLUSTER_BYOC
120+
byoc: false
121+
# OpenBMCLAPI 的 CLUSTER_ID
122+
id: ''
123+
# 实际开放的公网主机名, 同 CLUSTER_IP
124+
public_host: ''
125+
# 实际开放的公网端口, 同 CLUSTER_PUBLIC_PORT
126+
public_port: 8800
127+
# OpenBMCLAPI 的 CLUSTER_SECRET
128+
secret: ''
129+
download:
130+
# 最高下载线程
131+
threads: 64
132+
web:
133+
# 要监听的本地端口, 同 CLUSTER_PORT
134+
port: 80
135+
# 服务器名字
136+
server_name: TTB-Network
137+
# SSL 端口
138+
ssl_port: 8800
129139
```
130140

131141
# 贡献

bmclapi_dashboard/static/js/index.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bmclapi_dashboard/static/js/ttb.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/__init__.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ def disconnect(self, client: ProxyClient):
9292
return
9393
self._tables.remove(client)
9494

95+
def get_origin_from_ip(self, ip: tuple[str, int]):
96+
# ip is connected client
97+
for target in self._tables:
98+
if target.target.get_sock_address() == ip:
99+
return target.origin.get_address()
100+
return None
101+
95102

96103
ssl_server: Optional[asyncio.Server] = None
97104
server: Optional[asyncio.Server] = None
@@ -106,7 +113,14 @@ def disconnect(self, client: ProxyClient):
106113

107114

108115
async def _handle_ssl(reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
109-
return await _handle_process(Client(reader, writer), True)
116+
return await _handle_process(
117+
Client(
118+
reader,
119+
writer,
120+
peername=proxy.get_origin_from_ip(writer.get_extra_info("peername")),
121+
),
122+
True,
123+
)
110124

111125

112126
async def _handle(reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
@@ -131,7 +145,8 @@ async def _handle_process(client: Client, ssl: bool = False):
131145
await asyncio.open_connection(
132146
"127.0.0.1", ssl_server.sockets[0].getsockname()[1]
133147
)
134-
)
148+
),
149+
peername=client.get_address(),
135150
)
136151
proxying = True
137152
await proxy.connect(client, target, header)
@@ -156,7 +171,7 @@ async def check_ports():
156171
ports: list[tuple[asyncio.Server, ssl.SSLContext | None]] = []
157172
for service in (
158173
(server, None),
159-
(ssl_server, client_side_ssl if get_loads() != 0 else None),
174+
(ssl_server, client_side_ssl if get_loaded() else None),
160175
):
161176
if not service[0]:
162177
continue
@@ -192,20 +207,20 @@ async def check_ports():
192207
async def main():
193208
global ssl_server, server, server_side_ssl, restart
194209
await web.init()
210+
certificate.load_cert(Path(".ssl/cert"), Path(".ssl/key"))
195211
Timer.delay(check_ports, (), 5)
196212
while 1:
197213
try:
198214
server = await asyncio.start_server(_handle, port=PORT)
199215
ssl_server = await asyncio.start_server(
200216
_handle_ssl,
201217
port=0 if SSL_PORT == PORT else SSL_PORT,
202-
ssl=server_side_ssl if get_loads() != 0 else None,
218+
ssl=server_side_ssl if get_loaded() else None,
203219
)
204220
logger.info(f"Listening server on port {PORT}.")
205221
logger.info(
206222
f"Listening server on {ssl_server.sockets[0].getsockname()[1]}."
207223
)
208-
logger.info(f"Loaded {get_loads()} certificates!")
209224
async with server, ssl_server:
210225
await asyncio.gather(server.serve_forever(), ssl_server.serve_forever())
211226
except asyncio.CancelledError:

core/api.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class File:
4040
last_hit: float = 0
4141
last_access: float = 0
4242
data: Optional[io.BytesIO] = None
43+
cache: bool = False
4344

4445
def is_url(self):
4546
if not isinstance(self.path, str):
@@ -60,6 +61,12 @@ def set_data(self, data: io.BytesIO | memoryview | bytes):
6061
self.data = io.BytesIO(zlib.compress(data.getbuffer()))
6162

6263

64+
@dataclass
65+
class StatsCache:
66+
total: int = 0
67+
bytes: int = 0
68+
69+
6370
class Storage(metaclass=abc.ABCMeta):
6471
@abc.abstractmethod
6572
async def get(self, file: str) -> File:
@@ -79,6 +86,10 @@ async def exists(self, hash: str) -> bool:
7986
async def get_size(self, hash: str) -> int:
8087
raise NotImplementedError
8188

89+
@abc.abstractmethod
90+
async def copy(self, origin: Path, hash: str) -> int:
91+
raise NotImplementedError
92+
8293
@abc.abstractmethod
8394
async def write(self, hash: str, io: io.BytesIO) -> int:
8495
raise NotImplementedError
@@ -101,6 +112,10 @@ async def get_files_size(self, dir: str) -> int:
101112
async def removes(self, hashs: list[str]) -> int:
102113
raise NotImplementedError
103114

115+
@abc.abstractmethod
116+
async def get_cache_stats(self) -> StatsCache:
117+
raise NotImplementedError
118+
104119

105120
def get_hash(org):
106121
if len(org) == 32:
@@ -112,7 +127,7 @@ def get_hash(org):
112127
async def get_file_hash(org: str, path: Path):
113128
hash = get_hash(org)
114129
async with aiofiles.open(path, "rb") as r:
115-
while data := await r.read(Config.get("io_buffer")):
130+
while data := await r.read(Config.get("advanced.io_buffer")):
116131
if not data:
117132
break
118133
hash.update(data)

core/certificate.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from pathlib import Path
23
import ssl
34
import time
@@ -13,24 +14,25 @@
1314
client_side_ssl = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
1415
client_side_ssl.check_hostname = False
1516

16-
_loads: int = 0
17+
_loaded: bool = False
1718

1819

1920
def load_cert(cert, key):
20-
global server_side_ssl, client_side_ssl, _loads
21+
global server_side_ssl, client_side_ssl, _loaded
22+
if not os.path.exists(cert) or not os.path.exists(key):
23+
return False
2124
try:
2225
server_side_ssl.load_cert_chain(cert, key)
2326
client_side_ssl.load_verify_locations(cert)
24-
_loads += 1
27+
_loaded = True
2528
return True
2629
except:
2730
logger.error(f"Failed to load certificate: {traceback.format_exc()}.")
2831
return False
2932

3033

31-
def get_loads() -> int:
32-
global _loads
33-
return _loads
34+
def get_loaded() -> bool:
35+
return _loaded
3436

3537

3638
def load_text(cert: str, key: str):
@@ -41,9 +43,7 @@ def load_text(cert: str, key: str):
4143
c.write(cert)
4244
k.write(key)
4345
if load_cert(cert_file, key_file):
44-
logger.info(
45-
f"Loaded certificate from local files! Current certificate: {get_loads()}."
46-
)
46+
logger.info("Loaded certificate from local files!")
4747
core.restart = True
4848
if core.server:
4949
core.server.close()

0 commit comments

Comments
 (0)