feat: Windows 服务 安装,启动,停止 功能

This commit is contained in:
2025-10-18 22:16:54 +08:00
parent b4ce2046a9
commit 6a54381310
14 changed files with 1882 additions and 504 deletions

View File

@ -84,6 +84,35 @@ def create_parser() -> argparse.ArgumentParser:
update_parser.add_argument('--version', help='Specific version to download (e.g., v1.18.5). If not specified, downloads latest')
update_parser.add_argument('--force', action='store_true', help='Force update even if binary already exists')
# Service management commands
service_parser = core_subparsers.add_parser('service', help='Manage mihomo as a system service')
service_subparsers = service_parser.add_subparsers(dest='service_command', help='Service operations')
# Install service command
install_service_parser = service_subparsers.add_parser('install', help='Install mihomo as a system service')
install_service_parser.add_argument('--name', default='mihomo', help='Service name (default: mihomo)')
install_service_parser.add_argument('--description', default='Mihomo proxy service', help='Service description')
# Uninstall service command
uninstall_service_parser = service_subparsers.add_parser('uninstall', help='Uninstall mihomo system service')
uninstall_service_parser.add_argument('--name', default='mihomo', help='Service name (default: mihomo)')
# Start service command
start_service_parser = service_subparsers.add_parser('start', help='Start mihomo system service')
start_service_parser.add_argument('--name', default='mihomo', help='Service name (default: mihomo)')
# Stop service command
stop_service_parser = service_subparsers.add_parser('stop', help='Stop mihomo system service')
stop_service_parser.add_argument('--name', default='mihomo', help='Service name (default: mihomo)')
# Restart service command
restart_service_parser = service_subparsers.add_parser('restart', help='Restart mihomo system service')
restart_service_parser.add_argument('--name', default='mihomo', help='Service name (default: mihomo)')
# Status service command
status_service_parser = service_subparsers.add_parser('status', help='Check mihomo system service status')
status_service_parser.add_argument('--name', default='mihomo', help='Service name (default: mihomo)')
# Hook commands
hook_parser = subparsers.add_parser('hook', help='Manage hook scripts')
hook_subparsers = hook_parser.add_subparsers(dest='hook_command', help='Hook operations')
@ -170,9 +199,41 @@ def main() -> None:
parser.parse_args(['core', '--help'])
return
if args.core_command == 'update':
core_manager.update(version=args.version, force=args.force)
elif args.core_command == 'service':
if not hasattr(args, 'service_command') or not args.service_command:
parser.parse_args(['core', 'service', '--help'])
return
if args.service_command == 'install':
success = core_manager.install_service(
service_name=args.name,
description=args.description
)
if not success:
sys.exit(1)
elif args.service_command == 'uninstall':
success = core_manager.uninstall_service(service_name=args.name)
if not success:
sys.exit(1)
elif args.service_command == 'start':
success = core_manager.start_service(service_name=args.name)
if not success:
sys.exit(1)
elif args.service_command == 'stop':
success = core_manager.stop_service(service_name=args.name)
if not success:
sys.exit(1)
elif args.service_command == 'restart':
success = core_manager.restart_service(service_name=args.name)
if not success:
sys.exit(1)
elif args.service_command == 'status':
status = core_manager.get_service_status(service_name=args.name)
print(f"Service '{args.name}' status: {status}")
else:
parser.parse_args(['core', 'service', '--help'])
else:
parser.parse_args(['core', '--help'])