Compare commits

..

2 Commits

Author SHA1 Message Date
8607b4aa2d feat: core-config -> core config 2025-11-07 10:32:20 +08:00
a60d12a059 feat: 增加 subscription set-url 方法 2025-11-07 10:23:26 +08:00
4 changed files with 74 additions and 56 deletions

View File

@ -40,6 +40,11 @@ def create_parser() -> argparse.ArgumentParser:
rename_parser.add_argument('name', help='Current name of the subscription')
rename_parser.add_argument('new_name', help='New name for the subscription')
# Set URL subscription command
set_url_parser = subscription_subparsers.add_parser('set-url', help='Update the URL for a subscription')
set_url_parser.add_argument('name', help='Name of the subscription')
set_url_parser.add_argument('url', help='New URL for the subscription')
# Activate subscription command
activate_parser = subscription_subparsers.add_parser('activate', help='Activate a subscription')
activate_parser.add_argument('name', help='Name of the subscription to activate')
@ -50,31 +55,6 @@ def create_parser() -> argparse.ArgumentParser:
# Storage info command
storage_parser = subscription_subparsers.add_parser('storage', help='Show storage information')
# Core config commands
core_config_parser = subparsers.add_parser('core-config', help='Manage core configuration')
core_config_subparsers = core_config_parser.add_subparsers(dest='core_config_command', help='Configuration operations')
# Import config
import_parser = core_config_subparsers.add_parser('import', help='Import configuration from file')
import_parser.add_argument('source', help='Path to configuration file to import')
# Export config
export_parser = core_config_subparsers.add_parser('export', help='Export configuration to file')
export_parser.add_argument('destination', help='Path to save configuration file')
# Edit config
edit_parser = core_config_subparsers.add_parser('edit', help='Edit configuration with system editor')
# Reset config
reset_parser = core_config_subparsers.add_parser('reset', help='Reset configuration to default values')
# Show config
show_parser = core_config_subparsers.add_parser('show', help='Show current configuration')
# Apply config
apply_parser = core_config_subparsers.add_parser('apply', help='Apply active subscription to generate final config')
# Core commands
core_parser = subparsers.add_parser('core', help='Manage scientific-surfing core components')
core_subparsers = core_parser.add_subparsers(dest='core_command', help='Core operations')
@ -84,6 +64,30 @@ 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')
# Config commands
config_parser = core_subparsers.add_parser('config', help='Manage core configuration')
config_subparsers = config_parser.add_subparsers(dest='config_command', help='Configuration operations')
# Import config
import_parser = config_subparsers.add_parser('import', help='Import configuration from file')
import_parser.add_argument('source', help='Path to configuration file to import')
# Export config
export_parser = config_subparsers.add_parser('export', help='Export configuration to file')
export_parser.add_argument('destination', help='Path to save configuration file')
# Edit config
edit_parser = config_subparsers.add_parser('edit', help='Edit configuration with system editor')
# Reset config
reset_parser = config_subparsers.add_parser('reset', help='Reset configuration to default values')
# Show config
show_parser = config_subparsers.add_parser('show', help='Show current configuration')
# Apply config
apply_parser = config_subparsers.add_parser('apply', help='Apply active subscription to generate final config')
# 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')
@ -164,6 +168,8 @@ def main() -> None:
subscription_manager.delete_subscription(args.name)
elif args.subcommand == 'rename':
subscription_manager.rename_subscription(args.name, args.new_name)
elif args.subcommand == 'set-url':
subscription_manager.set_subscription_url(args.name, args.url)
elif args.subcommand == 'activate':
subscription_manager.activate_subscription(args.name)
elif args.subcommand == 'list':
@ -173,27 +179,6 @@ def main() -> None:
else:
parser.parse_args(['subscription', '--help'])
elif args.command == 'core-config':
if not hasattr(args, 'core_config_command') or not args.core_config_command:
parser.parse_args(['core-config', '--help'])
return
if args.core_config_command == 'import':
core_config_manager.import_config(args.source)
elif args.core_config_command == 'export':
core_config_manager.export_config(args.destination)
elif args.core_config_command == 'edit':
core_config_manager.edit_config()
elif args.core_config_command == 'reset':
core_config_manager.reset_config()
elif args.core_config_command == 'show':
core_config_manager.show_config()
elif args.core_config_command == 'apply':
core_config_manager.apply()
else:
parser.parse_args(['core-config', '--help'])
elif args.command == 'core':
if not hasattr(args, 'core_command') or not args.core_command:
parser.parse_args(['core', '--help'])
@ -201,6 +186,25 @@ def main() -> None:
if args.core_command == 'update':
core_manager.update(version=args.version, force=args.force)
elif args.core_command == 'config':
if not hasattr(args, 'config_command') or not args.config_command:
parser.parse_args(['core', 'config', '--help'])
return
if args.config_command == 'import':
core_config_manager.import_config(args.source)
elif args.config_command == 'export':
core_config_manager.export_config(args.destination)
elif args.config_command == 'edit':
core_config_manager.edit_config()
elif args.config_command == 'reset':
core_config_manager.reset_config()
elif args.config_command == 'show':
core_config_manager.show_config()
elif args.config_command == 'apply':
core_config_manager.apply()
else:
parser.parse_args(['core', 'config', '--help'])
elif args.core_command == 'service':
if not hasattr(args, 'service_command') or not args.service_command:
parser.parse_args(['core', 'service', '--help'])
@ -262,7 +266,6 @@ def main() -> None:
except Exception as e:
print(f"❌ Error: {e}")
raise
sys.exit(1)
if __name__ == '__main__':

View File

@ -175,11 +175,7 @@ class CoreConfigManager:
def show_config(self) -> None:
"""Display current configuration."""
config = self.load_config()
print("⚙️ Current Configuration:")
print(f" Auto-refresh: {config.auto_refresh}")
print(f" Refresh interval: {config.refresh_interval_hours} hours")
print(f" User-Agent: {config.default_user_agent}")
print(f" Timeout: {config.timeout_seconds} seconds")
print(yaml.dump(config, indent=2))
def get_config(self) -> Config:
"""Get current configuration."""

View File

@ -107,3 +107,11 @@ class SubscriptionsData(BaseModel):
subscription.name = new_name
self.subscriptions[new_name] = subscription
return True
def set_subscription_url(self, name: str, url: str) -> bool:
"""Update the URL for a subscription."""
if name not in self.subscriptions:
return False
self.subscriptions[name].url = url
return True

View File

@ -21,7 +21,7 @@ class SubscriptionManager:
self.subscriptions_dir = self.storage.config_dir / "subscriptions"
self.subscriptions_dir.mkdir(exist_ok=True)
def add_subscription(self, url: str, name: str) -> None:
def add_subscription(self, name: str, url: str) -> None:
"""Add a new subscription."""
subscription = self.subscriptions_data.add_subscription(name, url)
@ -45,11 +45,12 @@ class SubscriptionManager:
try:
# Download the subscription content
headers = {
'User-Agent': self.config.default_user_agent
# 'User-Agent': self.config.default_user_agent
}
timeout = self.config.timeout_seconds
# timeout = self.config.timeout_seconds
response = requests.get(url, headers=headers, timeout=timeout)
# response = requests.get(url, headers=headers, timeout=timeout)
response = requests.get(url)
response.raise_for_status()
# File path without timestamp
@ -121,6 +122,16 @@ class SubscriptionManager:
else:
print(f"❌ Failed to rename subscription: '{old_name}' not found or '{new_name}' already exists")
def set_subscription_url(self, name: str, url: str) -> None:
"""Update the URL for a subscription."""
if self.subscriptions_data.set_subscription_url(name, url):
if self.storage.save_subscriptions(self.subscriptions_data):
print(f"✅ Updated URL for subscription '{name}': {url}")
else:
print("❌ Failed to save subscription")
else:
print(f"❌ Subscription '{name}' not found")
def activate_subscription(self, name: str) -> None:
"""Activate a subscription."""
if self.subscriptions_data.set_active(name):