代理列表表
| 356 | |
| 357 | |
| 358 | class Proxy(Base): |
| 359 | """代理列表表""" |
| 360 | __tablename__ = 'proxies' |
| 361 | |
| 362 | id = Column(Integer, primary_key=True, autoincrement=True) |
| 363 | name = Column(String(100), nullable=False) # 代理名称 |
| 364 | type = Column(String(20), nullable=False, default='http') # http, socks5 |
| 365 | host = Column(String(255), nullable=False) |
| 366 | port = Column(Integer, nullable=False) |
| 367 | username = Column(String(100)) |
| 368 | password = Column(String(255)) |
| 369 | enabled = Column(Boolean, default=True) |
| 370 | is_default = Column(Boolean, default=False) # 是否为默认代理 |
| 371 | priority = Column(Integer, default=0) # 优先级(保留字段) |
| 372 | last_used = Column(DateTime) # 最后使用时间 |
| 373 | created_at = Column(DateTime, default=utcnow_naive) |
| 374 | updated_at = Column(DateTime, default=utcnow_naive, onupdate=utcnow_naive) |
| 375 | |
| 376 | def to_dict(self, include_password: bool = False) -> Dict[str, Any]: |
| 377 | """转换为字典""" |
| 378 | result = { |
| 379 | 'id': self.id, |
| 380 | 'name': self.name, |
| 381 | 'type': self.type, |
| 382 | 'host': self.host, |
| 383 | 'port': self.port, |
| 384 | 'username': self.username, |
| 385 | 'enabled': self.enabled, |
| 386 | 'is_default': self.is_default or False, |
| 387 | 'priority': self.priority, |
| 388 | 'last_used': self.last_used.isoformat() if self.last_used else None, |
| 389 | 'created_at': self.created_at.isoformat() if self.created_at else None, |
| 390 | 'updated_at': self.updated_at.isoformat() if self.updated_at else None, |
| 391 | } |
| 392 | if include_password: |
| 393 | result['password'] = self.password |
| 394 | else: |
| 395 | result['has_password'] = bool(self.password) |
| 396 | return result |
| 397 | |
| 398 | @property |
| 399 | def proxy_url(self) -> str: |
| 400 | """获取完整的代理 URL""" |
| 401 | if self.type == "http": |
| 402 | scheme = "http" |
| 403 | elif self.type == "socks5": |
| 404 | scheme = "socks5" |
| 405 | else: |
| 406 | scheme = self.type |
| 407 | |
| 408 | auth = "" |
| 409 | if self.username and self.password: |
| 410 | auth = f"{self.username}:{self.password}@" |
| 411 | |
| 412 | return f"{scheme}://{auth}{self.host}:{self.port}" |