Merge pull request #30 from rayanfer32/master

fixed username bug + added consistent aliases using hashes
This commit is contained in:
Christy Roys 2021-06-10 08:57:44 +05:30 committed by GitHub
commit f6e6bd11cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 31 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ logo/
app.json app.json
Procfile Procfile
.vscode .vscode
.gitignore

View File

@ -47,8 +47,9 @@ pip3 install -U -r requirements.txt
| `DEBUG` (optional) | Give `true` to set logging level to debug, info by default. | `DEBUG` (optional) | Give `true` to set logging level to debug, info by default.
| `BLOCK_DOWNLOADS` (optional) | Enable downloads or not. If any value is provided, downloads will be disabled. | `BLOCK_DOWNLOADS` (optional) | Enable downloads or not. If any value is provided, downloads will be disabled.
| `RESULTS_PER_PAGE` (optional) | Number of results to be returned per page defaults to 20. | `RESULTS_PER_PAGE` (optional) | Number of results to be returned per page defaults to 20.
| `USERNAME` (optional) | Username for authentication, defaults to `''`. | `TGINDEX_USERNAME` (optional) | Username for authentication, defaults to `''`.
| `PASSWORD` (optional) | Username for authentication, defaults to `''`. | `PASSWORD` (optional) | Username for authentication, defaults to `''`.
| `SHORT_URL_LEN` (optional) | Url length for aliases
| `SESSION_COOKIE_LIFETIME` (optional) | Number of minutes, for which authenticated session is valid for, after which user has to login again. defaults to 60. | `SESSION_COOKIE_LIFETIME` (optional) | Number of minutes, for which authenticated session is valid for, after which user has to login again. defaults to 60.
| `SECRET_KEY` (optional) | Long string for signing the session cookies, required if authentication is enabled. | `SECRET_KEY` (optional) | Long string for signing the session cookies, required if authentication is enabled.

View File

@ -44,10 +44,12 @@ host = os.environ.get("HOST", "0.0.0.0")
debug = bool(os.environ.get("DEBUG")) debug = bool(os.environ.get("DEBUG"))
block_downloads = bool(os.environ.get("BLOCK_DOWNLOADS")) block_downloads = bool(os.environ.get("BLOCK_DOWNLOADS"))
results_per_page = int(os.environ.get("RESULTS_PER_PAGE", "20")) results_per_page = int(os.environ.get("RESULTS_PER_PAGE", "20"))
logo_folder = Path("/Temp/logo/" if platform.system() == "Windows" else "/tmp/logo") logo_folder = Path("./Temp/logo/" if platform.system() == "Windows" else "/tmp/logo")
logo_folder.mkdir(exist_ok=True) if not logo_folder.exists():
username = os.environ.get("USERNAME", "") logo_folder.mkdir(parents=True)
username = os.environ.get("TGINDEX_USERNAME", "")
password = os.environ.get("PASSWORD", "") password = os.environ.get("PASSWORD", "")
SHORT_URL_LEN = int(os.environ.get("SHORT_URL_LEN", 3))
authenticated = username and password authenticated = username and password
SESSION_COOKIE_LIFETIME = int(os.environ.get("SESSION_COOKIE_LIFETIME") or "60") SESSION_COOKIE_LIFETIME = int(os.environ.get("SESSION_COOKIE_LIFETIME") or "60")
try: try:

View File

@ -5,10 +5,8 @@ from telethon.tl.types import Channel, Chat, User
from .config import index_settings from .config import index_settings
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
async def setup_routes(app, handler): async def setup_routes(app, handler):
h = handler h = handler
client = h.client client = h.client
@ -24,6 +22,15 @@ async def setup_routes(app, handler):
web.post("/login", h.login_post, name="login_handle"), web.post("/login", h.login_post, name="login_handle"),
web.get("/logout", h.logout_get, name="logout"), web.get("/logout", h.logout_get, name="logout"),
] ]
def get_common_routes(p):
return [
web.get(p, h.index),
web.get(p + r"/logo", h.logo),
web.get(p + r"/{id:\d+}/view", h.info),
web.get(p + r"/{id:\d+}/download", h.download_get),
web.head(p + r"/{id:\d+}/download", h.download_head),
web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get),
]
if index_all: if index_all:
# print(await client.get_dialogs()) # print(await client.get_dialogs())
# dialogs = await client.get_dialogs() # dialogs = await client.get_dialogs()
@ -48,14 +55,7 @@ async def setup_routes(app, handler):
alias_id = h.generate_alias_id(chat) alias_id = h.generate_alias_id(chat)
p = "/{chat:" + alias_id + "}" p = "/{chat:" + alias_id + "}"
routes.extend( routes.extend(
[ get_common_routes(p)
web.get(p, h.index),
web.get(p + r"/logo", h.logo),
web.get(p + r"/{id:\d+}/view", h.info),
web.get(p + r"/{id:\d+}/download", h.download_get),
web.head(p + r"/{id:\d+}/download", h.download_head),
web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get),
]
) )
log.debug(f"Index added for {chat.id} at /{alias_id}") log.debug(f"Index added for {chat.id} at /{alias_id}")
@ -65,14 +65,7 @@ async def setup_routes(app, handler):
alias_id = h.generate_alias_id(chat) alias_id = h.generate_alias_id(chat)
p = "/{chat:" + alias_id + "}" p = "/{chat:" + alias_id + "}"
routes.extend( routes.extend(
[ get_common_routes(p) # returns list() of common routes
web.get(p, h.index),
web.get(p + r"/logo", h.logo),
web.get(p + r"/{id:\d+}/view", h.info),
web.get(p + r"/{id:\d+}/download", h.download_get),
web.head(p + r"/{id:\d+}/download", h.download_head),
web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get),
]
) )
log.debug(f"Index added for {chat.id} at /{alias_id}") log.debug(f"Index added for {chat.id} at /{alias_id}")
routes.append(web.view(r"/{wildcard:.*}", h.wildcard)) routes.append(web.view(r"/{wildcard:.*}", h.wildcard))

View File

@ -4,11 +4,11 @@
Available Sources Available Sources
</h1> </h1>
<div class="mx-auto my-2 p-2 w-full"> <div class="mx-auto my-1 p-1 w-full">
<div class="block p-4 md:flex md:flex-wrap md:justify-items-center w-full text-center break-words"> <div class="block p-4 md:flex md:flex-wrap md:justify-items-center w-full text-center break-words">
{% for chat in chats %} {% for chat in chats %}
<a title="{{chat.name}}" href="/{{chat.page_id}}" <a title="{{chat.name}}" href="/{{chat.page_id}}"
class="mx-auto flex flex-col justify-items-center w-2/3 min-h-full md:w-2/5 lg:w-1/4 rounded my-2 p-2 shadow-md hover:shadow-lg hover:border hover:border-blue-300 hover:bg-blue-100"> class="mx-auto flex flex-col justify-items-center w-2/3 min-h-full md:w-2/5 lg:w-1/6 rounded my-2 p-2 shadow-md hover:shadow-lg hover:border hover:border-blue-300 hover:bg-blue-100">
<img src="/{{chat.page_id}}/logo?big=1" class="w-full rounded-full "> <img src="/{{chat.page_id}}/logo?big=1" class="w-full rounded-full ">
<div class="p-4 rounded">{{chat.name}}</div> <div class="p-4 rounded">{{chat.name}}</div>

View File

@ -1,6 +1,10 @@
import base64
import hashlib
import random import random
import string import string
from ..config import SHORT_URL_LEN
from .home_view import HomeView from .home_view import HomeView
from .wildcard_view import WildcardView from .wildcard_view import WildcardView
from .download import Download from .download import Download
@ -13,6 +17,7 @@ from .logout_view import LogoutView
from .middlewhere import middleware_factory from .middlewhere import middleware_factory
class Views( class Views(
HomeView, HomeView,
Download, Download,
@ -33,12 +38,15 @@ class Views(
chat_id = chat.id chat_id = chat.id
title = chat.title title = chat.title
while True: while True:
alias_id = "".join( # alias_id = "".join(
[ # [
random.choice(string.ascii_letters + string.digits) # random.choice(string.ascii_letters + string.digits)
for _ in range(len(str(chat_id))) # for _ in range(len(str(chat_id)))
] # ]
) # )
orig_id = f"{title}{chat_id}" # the original id
alias_id = base64.urlsafe_b64encode(hashlib.md5(orig_id.encode()).digest())[:SHORT_URL_LEN].decode()
if alias_id in self.chat_ids: if alias_id in self.chat_ids:
continue continue

29
run-dev.py Normal file
View File

@ -0,0 +1,29 @@
import os
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env.
# Code of your application, which uses environment variables (e.g. from `os.environ` or
# `os.getenv`) as if they came from the actual environment.
# os.system("alias python3=python")
def runSetup():
def alert(missing="API_ID , API_HASH"):
print(f"\nCopy your {missing} and save it into Secrets(Environment variables) Sidebar!\n")
api_id = os.getenv("API_ID")
if api_id == None:
alert()
return
session_string = os.getenv("SESSION_STRING")
if session_string == None:
os.system("python app/generate_session_string.py")
alert(missing = "SESSION_STRING")
return
os.system("python -m app")
if __name__ == '__main__':
runSetup()