-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmain.py
More file actions
177 lines (153 loc) · 6.01 KB
/
Copy pathmain.py
File metadata and controls
177 lines (153 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3
"""
m3u-proxy - Main Entry Point
A high-performance IPTV streaming proxy with client management and failover support.
"""
import uvicorn
import logging
import logging.handlers
import sys
import os
import asyncio
# Add the src directory to Python path so local modules in `src/` can be imported
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
# Import configs AFTER setting up the path
from redis_config import get_redis_config, should_use_pooling
from config import settings, VERSION
def _build_uvicorn_log_config(log_level: str, anonymize: bool) -> dict:
"""Return a dictConfig-compatible logging config for uvicorn.
When anonymize=True the AnonymizingFilter is added to both the default
and access handlers so access-log request lines are also scrubbed.
"""
config: dict = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"default": {
"()": "uvicorn.logging.DefaultFormatter",
"fmt": "%(levelprefix)s %(message)s",
"use_colors": None,
},
"access": {
"()": "uvicorn.logging.AccessFormatter",
"fmt": '%(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s',
},
},
"handlers": {
"default": {
"formatter": "default",
"class": "logging.StreamHandler",
"stream": "ext://sys.stderr",
},
"access": {
"formatter": "access",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
},
},
"loggers": {
"uvicorn": {"handlers": ["default"], "level": log_level.upper(), "propagate": False},
"uvicorn.error": {"level": log_level.upper()},
"uvicorn.access": {"handlers": ["access"], "level": log_level.upper(), "propagate": False},
},
}
if anonymize:
config["filters"] = {
"anonymizing": {"()": "log_anonymizer.AnonymizingFilter"},
}
for handler in config["handlers"].values():
handler["filters"] = ["anonymizing"]
return config
def main():
"""Main function to start the m3u-proxy server."""
# Try to use uvloop for better async performance (2-4x faster)
try:
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
use_uvloop = True
except ImportError:
use_uvloop = False
# Configure logging
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
logging.basicConfig(
level=settings.LOG_LEVEL.upper(),
format=log_format
)
# Add rotating file handler so logs are persisted to the mounted volume
log_dir = settings.LOG_DIR
os.makedirs(log_dir, exist_ok=True)
file_handler = logging.handlers.RotatingFileHandler(
os.path.join(log_dir, settings.LOG_FILE),
maxBytes=10 * 1024 * 1024, # 10 MB per file
backupCount=5,
encoding="utf-8",
)
file_handler.setLevel(settings.LOG_LEVEL.upper())
file_handler.setFormatter(logging.Formatter(log_format))
logging.getLogger().addHandler(file_handler)
if settings.LOG_ANONYMIZE:
from log_anonymizer import AnonymizingFilter
anon_filter = AnonymizingFilter()
for handler in logging.getLogger().handlers:
handler.addFilter(anon_filter)
logger = logging.getLogger(__name__)
logger.info("="*60)
logger.info(
f"⚡️ Starting m3u-proxy v{VERSION} on {settings.HOST}:{settings.PORT}")
logger.info("="*60)
logger.info(f"ℹ️ Log level set to: {settings.LOG_LEVEL}")
if use_uvloop:
logger.info("✅ Using uvloop for optimized async I/O performance")
else:
logger.info(
"✅ Using standard asyncio (install uvloop for better performance)")
logger.info("✅ Connection pooling enabled (HLS, Transcoded, and live Direct/TS streams)")
logger.info("✅ Transcoding support via FFmpeg")
logger.info("✅ Streamlink and yt-dlp resolver support")
logger.info("✅ Seamless failover support")
if settings.RELOAD:
logger.info("🔄 Auto-reload is enabled.")
# If pooling is enabled, perform a quick Redis connectivity check and log result
if should_use_pooling():
try:
import redis.asyncio as redis_async
redis_cfg = get_redis_config()
redis_url = redis_cfg.get('redis_url')
async def _check_redis():
try:
client = redis_async.from_url(
redis_url, decode_responses=True)
await client.ping()
await client.aclose()
return True
except Exception:
try:
if 'client' in locals() and client:
await client.aclose()
except Exception:
pass
return False
ok = asyncio.run(_check_redis())
if ok:
logger.info(
f"✅ Redis available and reachable for pooling")
else:
logger.warning(
f"❌ Redis configured but ping failed for: {redis_url}; pooling will be unavailable")
except ImportError:
logger.warning(
"❌ Redis async library not installed; pooling requested but unavailable")
except Exception as e:
logger.warning(f"❌ Redis pooling check failed: {e}")
# Start the server using settings from the config object
uvicorn.run(
"api:app",
host=settings.HOST,
port=settings.PORT,
reload=settings.RELOAD,
log_level=settings.LOG_LEVEL.lower(),
log_config=_build_uvicorn_log_config(settings.LOG_LEVEL, settings.LOG_ANONYMIZE),
loop="uvloop" if use_uvloop and not settings.RELOAD else "asyncio"
)
if __name__ == "__main__":
main()