forked from ysfchn/SamFetch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
136 lines (104 loc) · 6.38 KB
/
main.py
File metadata and controls
136 lines (104 loc) · 6.38 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
import os
from sanic import Sanic, Request, HTTPResponse
from sanic.exceptions import SanicException
from sanic.response import redirect, text, empty
from httpx import HTTPError, NetworkError
from web import bp
def get_env_int(name : str, default):
if name not in os.environ:
return default
_val = os.environ[name]
if _val.isnumeric():
return int(_val)
else:
return default
app = Sanic("SamFetch")
app.config.SAMFETCH_HIDE_TEXT = bool(get_env_int("SAMFETCH_HIDE_TEXT", 0))
app.config.SAMFETCH_ALLOW_ORIGIN = os.environ.get("SAMFETCH_ALLOW_ORIGIN", None) or "*"
app.config.SAMFETCH_CHUNK_SIZE = get_env_int("SAMFETCH_CHUNK_SIZE", 1485760)
app.config.FALLBACK_ERROR_FORMAT = "json"
NOTICE = \
"""
_____ ______ _ _
/ ____| | ____| | | | |
| (___ __ _ _ __ ___ | |__ ___| |_ ___| |__
\___ \ / _` | '_ ` _ \| __/ _ \ __/ __| '_ \
____) | (_| | | | | | | | | __/ || (__| | | |
|_____/ \__,_|_| |_| |_|_| \___|\__\___|_| |_|
A simple HTTP API to download Samsung Stock ROMs from Samsung's own servers without any restriction.
It doesn't have any analytics, rate-limits, download speed limit, authorization or any crap that you don't want.
SamFetch pulls firmware files from Samsung's servers and sends back to you. Oh, Samsung's own firmware
files are encrypted, however, SamFetch can decrypt the firmware while sending it to you! But if you want to
speed up the download process, you can prefer to not to decrypt the firmware and get the encrypted archive.
This project is licensed with AGPLv3.
https://github.com/ysfchn/SamFetch
## Credits
This is a Web API variant of samloader (https://github.com/nlscc/samloader).
SamFetch wouldn't be possible without Samloader.
## Endpoints
/csc Lists all available CSC.
Note that the list may be incomplete.
/list/<REGION>/<MODEL> Lists all firmware versions for a specific
device and region. Region examples can be found
on /csc endpoint. Note that some firmwares may
be only available to specific regions.
/binary/<REGION>/<MODEL>/<FIRMWARE> Gets details for a firmware such as download
size, file name and decryption key. You can
get firmware from /list endpoint.
/download/<PATH>/<FILE>?decrypt=<DECRYPT_KEY> Downloads a firmware while decrypting it. You
can get decrypt key, path and file from
/binary endpoint.
/download/<PATH>/<FILE> Downloads a firmware. But it doesn't decrypt
while downloading, so you need to decrypt
yourself. Downloading without decrypting
can speed up the download a bit. You
can get path and file from /binary endpoint.
/direct/<REGION>/<MODEL> Fetches all endpoints and downloads the latest
/<REGION>/<MODEL> firmware file with decrypting.
## Global Configuration
You can set environment variables to change configuration of your SamFetch instance.
SAMFETCH_HIDE_TEXT Only 0 or 1. Set the value to 1 if you don't
want this help text.
SAMFETCH_ALLOW_ORIGIN Sets the "Access-Control-Allow-Origin" header
value. Settings this to "*" (wildcard) allows
all domains to access this SamFetch instance.
Default is set to "*".
SAMFETCH_CHUNK_SIZE Specifies how many bytes must read in
a single iteration when downloading the firmware.
Default is set to 1485760 (1 megabytes)
"""
@app.middleware("response")
async def set_cors(request : Request, response : HTTPResponse):
response.headers["Access-Control-Allow-Origin"] = request.app.config.SAMFETCH_ALLOW_ORIGIN
response.headers["Access-Control-Allow-Credentials"] = "true"
response.headers["Access-Control-Allow-Methods"] = "DELETE, GET, POST, PUT, OPTIONS"
response.headers["Access-Control-Allow-Headers"] = "*"
@app.exception(HTTPError)
async def http_error(request : Request, exception : HTTPError):
if isinstance(exception, NetworkError):
raise SanicException(message = \
"SamFetch has lost connection with Kies servers. If you are running SamFetch locally, make sure you " + \
"have an internet connection. If you are currently hosting SamFetch somewhere, you can also check " + \
"if something (such as firewall) blocking the connection. If you need help, create a new Issue in " + \
"https://git.io/JPAbu",
status_code = 500
)
else:
raise SanicException(message = \
"SamFetch couldn't connect to Kies servers. This is probably not related to you. " + \
"Please try again if you didn't. Make sure you reported that in the SamFetch repository " + \
"by creating a new Issue in " + \
"https://git.io/JPAbu",
status_code = 500
)
@app.get("/")
async def home(request : Request):
return empty() if request.app.config.SAMFETCH_HIDE_TEXT else \
text(
"\n".join(x.replace(" ", " ", 1) for x in NOTICE.splitlines()) + "\n\n"
)
@app.get("/github")
async def github(request : Request):
return redirect("https://github.com/ysfchn/SamFetch")
# Register blueprint.
app.blueprint(bp)