# requirements :
# pip install proxy.py requests
import re
from typing import Optional
import requests
import proxy
from proxy.http.parser import HttpParser
from proxy.http.proxy import HttpProxyBasePlugin
from proxy.http.responses import okResponse
class SFVIP(HttpProxyBasePlugin):
def handle_client_request(self, req: HttpParser) -> Optional[HttpParser]:
# intercept the series or vod categories requests
intercept = req.body if req.method == b"POST" else req.path
if intercept and re.findall(rb"get_(series|vod)_categories", intercept):
resp = requests.request(
req.method,
f"{req._url.scheme.decode()}://{req.host.decode()}:{req.port}/{req.path.decode()}",
params=req.body,
timeout=None,
)
# inject the 'All' category with id=0
self.client.queue(
okResponse(
headers={b"Content-Type": b"application/json"},
content=(
b'[{"category_id":"0","category_name":"All","parent_id":0},'
+ resp.content[1:]
),
),
)
else:
# handle the id=0 'All' category by returning the whole catalog
if req.body: # for a post request
req.body = req.body.replace(b"&category_id=0", b"")
if req.path: # for a get request
req.path = req.path.replace(b"&category_id=0", b"")
return req
if __name__ == "__main__":
PORT = 7777
with proxy.Proxy(["--log-level", "e"], port=PORT, plugins=[SFVIP]):
print(f"SF VIP Proxy started @ http://127.0.0.1:{PORT}")
proxy.sleep_loop()