1
1
import express from '@/vendor/express' ;
2
2
import $ from '@/core/app' ;
3
3
import migrate from '@/utils/migration' ;
4
- import download from '@/utils/download' ;
4
+ import download , { downloadFile } from '@/utils/download' ;
5
5
import { syncArtifacts , produceArtifact } from '@/restful/sync' ;
6
6
import { gistBackupAction } from '@/restful/miscs' ;
7
7
import { TOKENS_KEY } from '@/constants' ;
@@ -35,7 +35,7 @@ export default function serve() {
35
35
const fe_be_path = eval ( 'process.env.SUB_STORE_FRONTEND_BACKEND_PATH' ) ;
36
36
const fe_path = eval ( 'process.env.SUB_STORE_FRONTEND_PATH' ) ;
37
37
if ( be_prefix || be_merge ) {
38
- if ( ! fe_be_path . startsWith ( '/' ) ) {
38
+ if ( ! fe_be_path . startsWith ( '/' ) ) {
39
39
throw new Error (
40
40
'SUB_STORE_FRONTEND_BACKEND_PATH should start with /' ,
41
41
) ;
@@ -48,15 +48,20 @@ export default function serve() {
48
48
$app . use ( ( req , res , next ) => {
49
49
if ( req . path . startsWith ( fe_be_path ) ) {
50
50
req . url = req . url . replace ( fe_be_path , '' ) || '/' ;
51
- if ( be_merge && req . url . startsWith ( '/api/' ) ) {
51
+ if ( be_merge && req . url . startsWith ( '/api/' ) ) {
52
52
req . query [ 'share' ] = 'true' ;
53
53
}
54
54
next ( ) ;
55
55
return ;
56
56
}
57
- const pathname = decodeURIComponent ( req . _parsedUrl . pathname ) || '/' ;
58
- if ( be_merge && req . path . startsWith ( '/share/' ) && req . query . token ) {
59
- if ( req . method . toLowerCase ( ) !== 'get' ) {
57
+ const pathname =
58
+ decodeURIComponent ( req . _parsedUrl . pathname ) || '/' ;
59
+ if (
60
+ be_merge &&
61
+ req . path . startsWith ( '/share/' ) &&
62
+ req . query . token
63
+ ) {
64
+ if ( req . method . toLowerCase ( ) !== 'get' ) {
60
65
res . status ( 405 ) . send ( 'Method not allowed' ) ;
61
66
return ;
62
67
}
@@ -67,14 +72,14 @@ export default function serve() {
67
72
`/share/${ t . type } /${ t . name } ` === pathname &&
68
73
( t . exp == null || t . exp > Date . now ( ) ) ,
69
74
) ;
70
- if ( token ) {
75
+ if ( token ) {
71
76
next ( ) ;
72
77
return ;
73
78
}
74
79
}
75
- if ( be_merge && fe_path && req . path . indexOf ( '/' , 1 ) == - 1 ) {
76
- if ( req . path . indexOf ( '.' ) == - 1 ) {
77
- req . url = " /index.html"
80
+ if ( be_merge && fe_path && req . path . indexOf ( '/' , 1 ) == - 1 ) {
81
+ if ( req . path . indexOf ( '.' ) == - 1 ) {
82
+ req . url = ' /index.html' ;
78
83
}
79
84
const express_ = eval ( `require("express")` ) ;
80
85
const mime_ = eval ( `require("mime-types")` ) ;
@@ -85,7 +90,7 @@ export default function serve() {
85
90
if ( type ) {
86
91
res . set ( 'Content-Type' , type ) ;
87
92
}
88
- }
93
+ } ,
89
94
} ) ;
90
95
staticFileMiddleware ( req , res , next ) ;
91
96
return ;
@@ -230,6 +235,60 @@ export default function serve() {
230
235
// 'Asia/Shanghai' // timeZone
231
236
) ;
232
237
}
238
+ const mmdb_cron = eval ( 'process.env.SUB_STORE_MMDB_CRON' ) ;
239
+ const countryFile = eval ( 'process.env.SUB_STORE_MMDB_COUNTRY_PATH' ) ;
240
+ const countryUrl = eval ( 'process.env.SUB_STORE_MMDB_COUNTRY_URL' ) ;
241
+ const asnFile = eval ( 'process.env.SUB_STORE_MMDB_ASN_PATH' ) ;
242
+ const asnUrl = eval ( 'process.env.SUB_STORE_MMDB_ASN_URL' ) ;
243
+ if ( mmdb_cron && ( ( countryFile && countryUrl ) || ( asnFile && asnUrl ) ) ) {
244
+ $ . info ( `[MMDB CRON] ${ mmdb_cron } enabled` ) ;
245
+ const { CronJob } = eval ( `require("cron")` ) ;
246
+ new CronJob (
247
+ mmdb_cron ,
248
+ async function ( ) {
249
+ try {
250
+ $ . info ( `[MMDB CRON] ${ mmdb_cron } started` ) ;
251
+ if ( countryFile && countryUrl ) {
252
+ try {
253
+ $ . info (
254
+ `[MMDB CRON] downloading ${ countryUrl } to ${ countryFile } ` ,
255
+ ) ;
256
+ await downloadFile ( countryUrl , countryFile ) ;
257
+ } catch ( e ) {
258
+ $ . error (
259
+ `[MMDB CRON] ${ countryUrl } download failed: ${
260
+ e . message ?? e
261
+ } `,
262
+ ) ;
263
+ }
264
+ }
265
+ if ( asnFile && asnUrl ) {
266
+ try {
267
+ $ . info (
268
+ `[MMDB CRON] downloading ${ asnUrl } to ${ asnFile } ` ,
269
+ ) ;
270
+ await downloadFile ( asnUrl , asnFile ) ;
271
+ } catch ( e ) {
272
+ $ . error (
273
+ `[MMDB CRON] ${ asnUrl } download failed: ${
274
+ e . message ?? e
275
+ } `,
276
+ ) ;
277
+ }
278
+ }
279
+
280
+ $ . info ( `[MMDB CRON] ${ mmdb_cron } finished` ) ;
281
+ } catch ( e ) {
282
+ $ . error (
283
+ `[MMDB CRON] ${ mmdb_cron } error: ${ e . message ?? e } ` ,
284
+ ) ;
285
+ }
286
+ } , // onTick
287
+ null , // onComplete
288
+ true , // start
289
+ // 'Asia/Shanghai' // timeZone
290
+ ) ;
291
+ }
233
292
const path = eval ( `require("path")` ) ;
234
293
const fs = eval ( `require("fs")` ) ;
235
294
const data_url = eval ( 'process.env.SUB_STORE_DATA_URL' ) ;
0 commit comments