@@ -2,10 +2,103 @@ const Fastify = require('fastify');
22const cors = require ( '@fastify/cors' ) ;
33const { readFileSync } = require ( 'fs' ) ;
44const { join, dirname } = require ( 'path' ) ;
5+ const { createServer } = require ( 'net' ) ;
56const { Sniffer } = require ( './sniffer.cjs' ) ;
67
78// __filename 和 __dirname 在 CommonJS 中是内置的全局变量
89
10+ // 解析命令行参数
11+ function parseCommandLineArgs ( ) {
12+ const args = process . argv . slice ( 2 ) ;
13+ const options = {
14+ port : null ,
15+ help : false
16+ } ;
17+
18+ for ( let i = 0 ; i < args . length ; i ++ ) {
19+ const arg = args [ i ] ;
20+
21+ if ( arg === '-port' || arg === '--port' ) {
22+ if ( i + 1 < args . length ) {
23+ const portValue = parseInt ( args [ i + 1 ] ) ;
24+ if ( ! isNaN ( portValue ) && portValue > 0 && portValue <= 65535 ) {
25+ options . port = portValue ;
26+ i ++ ; // 跳过端口值参数
27+ } else {
28+ console . error ( '错误:端口号必须是1-65535之间的数字' ) ;
29+ process . exit ( 1 ) ;
30+ }
31+ } else {
32+ console . error ( '错误:-port 参数需要指定端口号' ) ;
33+ process . exit ( 1 ) ;
34+ }
35+ } else if ( arg === '-h' || arg === '--help' ) {
36+ options . help = true ;
37+ } else if ( arg . startsWith ( '-' ) ) {
38+ console . error ( `错误:未知参数 ${ arg } ` ) ;
39+ console . log ( '使用 -h 或 --help 查看帮助信息' ) ;
40+ process . exit ( 1 ) ;
41+ }
42+ }
43+
44+ return options ;
45+ }
46+
47+ // 显示帮助信息
48+ function showHelp ( ) {
49+ console . log ( `
50+ Pup Sniffer - 视频资源嗅探器
51+
52+ 使用方法:
53+ node server.cjs [选项]
54+ pup-sniffer-win.exe [选项]
55+
56+ 选项:
57+ -port <端口号> 指定服务器端口号 (1-65535)
58+ -h, --help 显示此帮助信息
59+
60+ 示例:
61+ node server.cjs -port 8080
62+ pup-sniffer-win.exe -port 3000
63+
64+ 如果不指定端口号,服务器将从57573开始自动查找可用端口。
65+ ` ) ;
66+ }
67+
68+ // 检查端口是否可用
69+ function checkPortAvailable ( port ) {
70+ return new Promise ( ( resolve ) => {
71+ const server = createServer ( ) ;
72+
73+ server . listen ( port , '0.0.0.0' , ( ) => {
74+ server . once ( 'close' , ( ) => {
75+ resolve ( true ) ;
76+ } ) ;
77+ server . close ( ) ;
78+ } ) ;
79+
80+ server . on ( 'error' , ( err ) => {
81+ resolve ( false ) ;
82+ } ) ;
83+ } ) ;
84+ }
85+
86+ // 查找可用端口
87+ async function findAvailablePort ( startPort = 57573 ) {
88+ let port = startPort ;
89+ const maxAttempts = 100 ; // 最多尝试100个端口
90+
91+ for ( let i = 0 ; i < maxAttempts ; i ++ ) {
92+ const isAvailable = await checkPortAvailable ( port ) ;
93+ if ( isAvailable ) {
94+ return port ;
95+ }
96+ port ++ ;
97+ }
98+
99+ throw new Error ( `无法找到可用端口,已尝试从 ${ startPort } 到 ${ startPort + maxAttempts - 1 } ` ) ;
100+ }
101+
9102// 获取资源文件路径(兼容打包后的环境)
10103function getResourcePath ( filename ) {
11104 // 在打包环境中,资源文件被打包到可执行文件内部
@@ -408,14 +501,40 @@ process.on('SIGINT', async () => {
408501// 启动服务器
409502const start = async ( ) => {
410503 try {
504+ // 解析命令行参数
505+ const options = parseCommandLineArgs ( ) ;
506+
507+ // 如果请求帮助,显示帮助信息并退出
508+ if ( options . help ) {
509+ showHelp ( ) ;
510+ process . exit ( 0 ) ;
511+ }
512+
411513 // 注册 CORS 插件
412514 await fastify . register ( cors , {
413515 origin : true ,
414516 credentials : true
415517 } ) ;
416518
417- const port = process . env . PORT || 57573 ;
418519 const host = process . env . HOST || '0.0.0.0' ;
520+ let port ;
521+
522+ // 确定使用的端口
523+ if ( options . port ) {
524+ // 使用指定的端口
525+ const isAvailable = await checkPortAvailable ( options . port ) ;
526+ if ( ! isAvailable ) {
527+ console . error ( `错误:指定的端口 ${ options . port } 已被占用` ) ;
528+ process . exit ( 1 ) ;
529+ }
530+ port = options . port ;
531+ console . log ( `使用指定端口: ${ port } ` ) ;
532+ } else {
533+ // 自动查找可用端口
534+ console . log ( '正在查找可用端口...' ) ;
535+ port = await findAvailablePort ( ) ;
536+ console . log ( `找到可用端口: ${ port } ` ) ;
537+ }
419538
420539 await fastify . listen ( { port, host } ) ;
421540 console . log ( `🚀 服务器已启动,监听地址: http://${ host } :${ port } ` ) ;
0 commit comments