Skip to content

Commit e5504b2

Browse files
committed
修复无法加载本地证书的情况,加快统计显示
1 parent 0286457 commit e5504b2

File tree

6 files changed

+113
-75
lines changed

6 files changed

+113
-75
lines changed

bmclapi_dashboard/static/js/index.min.js

Lines changed: 69 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,14 +2779,29 @@ app.$Menu.add("dashboard", new class {
27792779
this.data_day_option = parseInt(index + "")
27802780
if (this.get_globals_stats) {
27812781
$progress.set(60)
2782+
var percent = 0
2783+
var total = 6
2784+
var percent_func = (n) => {
2785+
percent += n
2786+
$progress.set(60 + (percent / total) * 100)
2787+
}
27822788
$MainSocket.send("pro_stats", { type: this.data_day_option }).then((data) => {
2783-
$progress.set(90)
2789+
percent_func(1)
27842790
this.pro_stats = data
27852791
this.drawGlobals()
27862792
this.drawGEO()
27872793
}).finally(() => {
2788-
$progress.set(100)
2794+
percent_func(2)
2795+
})
2796+
$MainSocket.send("geo_stats", { type: this.data_day_option }).then((data) => {
2797+
percent_func(1)
2798+
this.geo_stats = data
2799+
this.drawGlobals()
2800+
this.drawGEO()
2801+
}).finally(() => {
2802+
percent_func(2)
27892803
})
2804+
$progress.set(100)
27902805
}
27912806
})
27922807
this._be_qps = app.createEcharts().style("min-height: 162px;")
@@ -3474,6 +3489,11 @@ app.$Menu.add("dashboard", new class {
34743489
this.drawGlobals()
34753490
this.drawGEO()
34763491
})
3492+
$MainSocket.send("geo_stats", { type: this.data_day_option }).then((data) => {
3493+
this.geo_stats = data
3494+
this.drawGlobals()
3495+
this.drawGEO()
3496+
})
34773497
}, 0, 360000)
34783498
this.detailSystemTimer = app.runTaskRepeat(() => {
34793499
$MainSocket.send("system_details").then((data) => {
@@ -3751,12 +3771,12 @@ app.$Menu.add("dashboard", new class {
37513771
}
37523772
}
37533773
drawGEO() {
3754-
if (this.pro_stats == null) return
3774+
if (this.geo_stats == null) return
37553775
var data = []
37563776
if (this.geo_type == "world") {
37573777
var other = []
37583778
var cn_data = 0
3759-
this.pro_stats.addresses.forEach(e => {
3779+
this.geo_stats.addresses.forEach(e => {
37603780
if (e.country == "CN") cn_data += e.value
37613781
else other.push(e)
37623782
})
@@ -3836,6 +3856,49 @@ app.$Menu.add("dashboard", new class {
38363856
value: e.value
38373857
}
38383858
}))
3859+
var visits = (this.data_day_option <= 1 ? fillDateTimes(this.geo_stats.distinct_ip, this.data_day_option == 1 ? 7 : 1) : fillDates(this.geo_stats.distinct_ip))
3860+
this._e_visits.setOption({
3861+
color: app.getThemeVar("main-color"),
3862+
xAxis: {
3863+
data: Object.keys(visits)
3864+
},
3865+
series: [
3866+
{
3867+
name: $I18N.t("dashboard.distincts"),
3868+
data: Object.values(visits),
3869+
type: 'line',
3870+
smooth: true,
3871+
}
3872+
]
3873+
})
3874+
this.pro[1].getChildren()[1].getChildren()[0].getChildren()[0].getChildren()[1].t18n({ peer: Math.max(...Object.values(visits)) })
3875+
this.pro[0].getChildren()[2].getChildren()[1].setText(this.geo_stats.distinct_ip_count + "")
3876+
var data = []
3877+
for (const key in this.geo_stats.useragents) {
3878+
data.push({
3879+
label: key,
3880+
value: this.geo_stats.useragents[key]
3881+
})
3882+
}
3883+
data = data.sort((a, b) => b.value - a.value)
3884+
this._e_ua.setOption({
3885+
color: [
3886+
app.getThemeVar("echarts-color-0"),
3887+
app.getThemeVar("echarts-color-1"),
3888+
app.getThemeVar("echarts-color-2"),
3889+
app.getThemeVar("echarts-color-3"),
3890+
app.getThemeVar("echarts-color-4")
3891+
],
3892+
series: [
3893+
{
3894+
itemStyle:{
3895+
borderColor:app.getThemeVar("dark-color"),
3896+
},
3897+
data: data.slice(0, 5)
3898+
}
3899+
]
3900+
})
3901+
this.setRank_UA();
38393902
}
38403903
drawSystem() {
38413904
var hits, bytes;
@@ -3919,48 +3982,6 @@ app.$Menu.add("dashboard", new class {
39193982
this.pro[3].getChildren()[2].getChildren()[0].getChildren()[1].t18n({ peer: Math.max(...Object.values(this.detail_system.connections), 0)})
39203983
}
39213984
drawGlobals() {
3922-
var visits = (this.data_day_option <= 1 ? fillDateTimes(this.pro_stats.distinct_ip, this.data_day_option == 1 ? 7 : 1) : fillDates(this.pro_stats.distinct_ip))
3923-
this._e_visits.setOption({
3924-
color: app.getThemeVar("main-color"),
3925-
xAxis: {
3926-
data: Object.keys(visits)
3927-
},
3928-
series: [
3929-
{
3930-
name: $I18N.t("dashboard.distincts"),
3931-
data: Object.values(visits),
3932-
type: 'line',
3933-
smooth: true,
3934-
}
3935-
]
3936-
})
3937-
this.pro[1].getChildren()[1].getChildren()[0].getChildren()[0].getChildren()[1].t18n({ peer: Math.max(...Object.values(visits)) })
3938-
this.pro[0].getChildren()[2].getChildren()[1].setText(this.pro_stats.distinct_ip_count + "")
3939-
var data = []
3940-
for (const key in this.pro_stats.useragents) {
3941-
data.push({
3942-
label: key,
3943-
value: this.pro_stats.useragents[key]
3944-
})
3945-
}
3946-
data = data.sort((a, b) => b.value - a.value)
3947-
this._e_ua.setOption({
3948-
color: [
3949-
app.getThemeVar("echarts-color-0"),
3950-
app.getThemeVar("echarts-color-1"),
3951-
app.getThemeVar("echarts-color-2"),
3952-
app.getThemeVar("echarts-color-3"),
3953-
app.getThemeVar("echarts-color-4")
3954-
],
3955-
series: [
3956-
{
3957-
itemStyle:{
3958-
borderColor:app.getThemeVar("dark-color"),
3959-
},
3960-
data: data.slice(0, 5)
3961-
}
3962-
]
3963-
})
39643985
this.pro[0].getChildren()[0].getChildren()[1].setText(this._format_number_unit(this.pro_stats.downloads))
39653986
this.pro[0].getChildren()[1].getChildren()[1].setText(this._format_bytes(this.pro_stats.bytes))
39663987
var data = []
@@ -3988,15 +4009,14 @@ app.$Menu.add("dashboard", new class {
39884009
}
39894010
]
39904011
})
3991-
this.setRank_UA();
39924012
this.setRank_Status();
39934013
}
39944014
setRank_UA() {
39954015
var data = [];
3996-
for (const key in this.pro_stats != null && this.pro_stats.useragents != null ? this.pro_stats.useragents : {}) {
4016+
for (const key in this.geo_stats != null && this.geo_stats.useragents != null ? this.geo_stats.useragents : {}) {
39974017
data.push({
39984018
label: key,
3999-
value: this.pro_stats.useragents[key]
4019+
value: this.geo_stats.useragents[key]
40004020
})
40014021
}
40024022
data = data.sort((a, b) => b.value - a.value)

core/certificate.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
_loaded: bool = False
1717

1818

19-
def load_cert(cert, key):
19+
def _load_cert(cert, key):
2020
global server_side_ssl, client_side_ssl, _loaded
2121
if not os.path.exists(cert) or not os.path.exists(key):
22+
logger.terror("cert.error.failed.file", cert=cert, key=key)
2223
return False
2324
try:
2425
server_side_ssl.load_cert_chain(cert, key)
@@ -28,6 +29,13 @@ def load_cert(cert, key):
2829
except:
2930
logger.terror("cert.error.failed", failure=traceback.format_exc())
3031
return False
32+
33+
def load_cert(cert: str, key: str):
34+
if _load_cert(cert, key):
35+
logger.tsuccess("cert.success.loaded_cert")
36+
from core.network import restart
37+
38+
restart()
3139

3240

3341
def get_loaded() -> bool:
@@ -41,7 +49,7 @@ def load_text(cert: str, key: str):
4149
with open(cert_file, "w") as c, open(key_file, "w") as k:
4250
c.write(cert)
4351
k.write(key)
44-
if load_cert(cert_file, key_file):
52+
if _load_cert(cert_file, key_file):
4553
logger.tsuccess("cert.success.loaded_cert")
4654
from core.network import restart
4755

core/const.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ class Certificate:
146146
cert: str = ""
147147
path: str = ""
148148

149+
def empty(self):
150+
return self.cert == self.path == ""
151+
149152
if Config.get("storages") is not None:
150153
for name in Config.get("storages"):
151154
storage = Config.get(f"storages.{name}")
@@ -156,7 +159,8 @@ class Certificate:
156159
)
157160

158161
CERTIFICATE = Certificate(Config.get("certificate.cert"), Config.get("certificate.key"))
159-
162+
SSL_PORT: int = Config.get("web.ssl_port")
163+
PROTOCOL_HEADER_BYTES = Config.get("advanced.header_bytes")
160164
# xdb 默认参数
161165
XDB_HeaderInfoLength = 256
162166
XDB_VectorIndexRows = 256

core/dashboard.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ async def process(type: str, data: Any):
170170
"python": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
171171
"os": platform.platform(),
172172
}
173-
if type == "pro_stats":
173+
if type == "pro_stats" or type == "geo_stats":
174174
day = 1
175175
if isinstance(data, dict):
176176
t = data.get("type", 0)
@@ -180,7 +180,9 @@ async def process(type: str, data: Any):
180180
day = 30
181181
elif t >= 3:
182182
day = -1
183-
return await asyncio.get_event_loop().run_in_executor(None, statistics.stats_pro, day)
183+
func = statistics.stats_pro if type == "pro_stats" else statistics.geo_pro
184+
return await asyncio.get_event_loop().run_in_executor(None, func, day)
185+
184186
if type == "system_details":
185187
return system.get_loads_detail()
186188

core/network.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import ssl
66
import traceback
77
from typing import Optional
8-
from core.certificate import get_loaded, client_side_ssl, server_side_ssl
8+
from core.certificate import get_loaded, client_side_ssl, server_side_ssl, load_cert
99
from core.config import Config
1010
from core.utils import Client
11-
from core.i18n import locale
11+
from core.const import *
1212
from core import env, logger, scheduler, web
1313

1414

@@ -106,11 +106,6 @@ def get_origin_from_ip(self, ip: tuple[str, int]):
106106
server: Optional[asyncio.Server] = None
107107
proxy: Proxy = Proxy()
108108
check_port_key = os.urandom(8)
109-
PORT: int = Config.get("web.port")
110-
TIMEOUT: int = Config.get("advanced.timeout")
111-
SSL_PORT: int = Config.get("web.ssl_port")
112-
PROTOCOL_HEADER_BYTES = Config.get("advanced.header_bytes")
113-
IO_BUFFER: int = Config.get("advanced.io_buffer")
114109

115110
async def _handle_ssl(reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
116111
return await _handle_process(
@@ -215,6 +210,8 @@ async def check_ports():
215210

216211
async def start():
217212
global server, ssl_server
213+
if not CERTIFICATE.empty():
214+
load_cert(CERTIFICATE.cert, CERTIFICATE.path)
218215
while "EXIT" not in env:
219216
close()
220217
try:

core/statistics.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -361,26 +361,15 @@ def daily():
361361
return data
362362

363363

364-
def stats_pro(day: int):
364+
def geo_pro(day: int):
365365
format_day = day == 30
366366
t = get_query_hour_tohour(0) - (day * 24)
367-
status_arr = list(status.value for status in Status)
368-
status: defaultdict[str, int] = defaultdict(int)
369367
d_address: defaultdict[int, defaultdict[str, int]] = defaultdict(lambda: defaultdict(int))
370368
d_ip: set[str] = set()
371369
d_geo: defaultdict[IPInfo, int] = defaultdict(int)
372370
d_useragent: defaultdict[UserAgent, int] = defaultdict(int)
373-
d_hit, d_bytes, d_sync_hit, d_sync_bytes = 0, 0, 0, 0
374371
stats_t = StatsTiming()
375-
stats_t.start("storage")
376-
for q in queryAllData(f"select sum(hit + cache_hit) as hit, sum(bytes + cache_bytes) as bytes, sync_hit, sync_bytes, {', '.join((f'sum({arr})' for arr in status_arr))} from access_storage where hour >= ?", t):
377-
d_hit += q[0] or 0
378-
d_bytes += q[1] or 0
379-
d_sync_hit += q[2] or 0
380-
d_sync_bytes += q[3] or 0
381-
for i, status_val in enumerate(status_arr):
382-
status[status_val] += q[4 + i] or 0
383-
stats_t.start("globals", "storage")
372+
stats_t.start("globals")
384373
for q in queryAllData(f"select hour, addresses, useragents from access_globals where hour >= ?", t):
385374
hour = (q[0] + get_utc_offset()) if not format_day else (q[0] + get_utc_offset()) // 24
386375
data_address = q[1]
@@ -402,7 +391,6 @@ def stats_pro(day: int):
402391
d_geo[location.query(address)] += count
403392
stats_t.end("location")
404393
stats_t.print()
405-
406394
return {
407395
"useragents": {key.value: value for key, value in d_useragent.items() if value},
408396
"addresses": [
@@ -418,6 +406,25 @@ def stats_pro(day: int):
418406
for hour, ip in sorted(d_address.items())
419407
},
420408
"distinct_ip_count": len(d_ip),
409+
}
410+
def stats_pro(day: int):
411+
t = get_query_hour_tohour(0) - (day * 24)
412+
status_arr = list(status.value for status in Status)
413+
status: defaultdict[str, int] = defaultdict(int)
414+
d_hit, d_bytes, d_sync_hit, d_sync_bytes = 0, 0, 0, 0
415+
stats_t = StatsTiming()
416+
stats_t.start("storage")
417+
for q in queryAllData(f"select sum(hit + cache_hit) as hit, sum(bytes + cache_bytes) as bytes, sync_hit, sync_bytes, {', '.join((f'sum({arr})' for arr in status_arr))} from access_storage where hour >= ?", t):
418+
d_hit += q[0] or 0
419+
d_bytes += q[1] or 0
420+
d_sync_hit += q[2] or 0
421+
d_sync_bytes += q[3] or 0
422+
for i, status_val in enumerate(status_arr):
423+
status[status_val] += q[4 + i] or 0
424+
stats_t.end("storage")
425+
stats_t.print()
426+
427+
return {
421428
"status": {k: v for k, v in status.items() if v != 0},
422429
"bytes": d_bytes,
423430
"downloads": d_hit,

0 commit comments

Comments
 (0)