DIVY118 commited on
Commit
d8e86bd
1 Parent(s): 5b6f162

Upload 2 files

Browse files
Files changed (2) hide show
  1. Dockerfile +15 -0
  2. main.py +110 -0
Dockerfile ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.12
2
+
3
+ WORKDIR /code
4
+
5
+ # Install necessary dependencies
6
+ RUN pip install fastapi pygithub uvicorn rich
7
+
8
+ # Grant permission for Python to create files in the entire /code directory
9
+ RUN chmod -R 777 /code
10
+
11
+ # Copy project files
12
+ COPY . .
13
+
14
+ # Run the FastAPI application
15
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, WebSocket, WebSocketDisconnect
2
+ from fastapi.responses import HTMLResponse
3
+ import json
4
+ import hashlib
5
+ import sqlite3
6
+ from github import Github
7
+ from time import time as t
8
+ import os
9
+
10
+ app = FastAPI()
11
+
12
+ tokens = {"divydon", "yatteshdon", "subhdon"}
13
+
14
+ def sha256(string: str) -> str:
15
+ return hashlib.sha256(string.encode()).hexdigest()
16
+
17
+ @app.get("/")
18
+ async def get():
19
+ return HTMLResponse("Hello, world!")
20
+
21
+ @app.websocket("/ws")
22
+ async def websocket_endpoint(websocket: WebSocket):
23
+ await websocket.accept()
24
+ try:
25
+ authuri = await websocket.receive_text()
26
+ if authuri not in tokens:
27
+ await websocket.close()
28
+ return
29
+
30
+ fileName = f"{sha256(authuri)}.db"
31
+ conn = sqlite3.connect(fileName)
32
+ cursor = conn.cursor()
33
+ while True:
34
+ jsonScript = await websocket.receive_text()
35
+ jsonScript = json.loads(jsonScript)
36
+ jsonOutput = []
37
+ clss = ["conn", "cursor"]
38
+ notallowed = ["conn.close", "cursor.close"]
39
+ for i in jsonScript:
40
+ functionName = i["funcname"]
41
+ if functionName in notallowed:
42
+ continue
43
+ if len([c for c in clss if functionName.startswith(c)]) == 0:
44
+ continue
45
+
46
+ args = i.get("args", [])
47
+ kwargs = i.get("kwargs", {})
48
+ try:
49
+ out = eval(functionName + "(*args, **kwargs)")
50
+ except Exception as e:
51
+ out = str(e)
52
+ i["eval"] = f"{out}"
53
+ i.pop("args", None)
54
+ i.pop("kwargs", None)
55
+ jsonOutput.append(i)
56
+ await websocket.send_text(json.dumps(jsonOutput))
57
+ except WebSocketDisconnect:
58
+ await websocket.close()
59
+ except Exception as e:
60
+ ...
61
+ finally:
62
+ pushToDb(fileName)
63
+ conn.close()
64
+
65
+ def pushToDb(token: str):
66
+ token = token.removesuffix(".db")
67
+ g = Github(os.getenv("GITHUB_TOKEN"))
68
+ user = g.get_user()
69
+ try:
70
+ repo = user.get_repo(token)
71
+ except:
72
+ repo = user.create_repo(token)
73
+ try:
74
+ with open(f"{token}.db", "rb") as f:
75
+ contents = f.read()
76
+ try:
77
+ repo_file = repo.get_contents(f"{token}.db")
78
+ repo.update_file(repo_file.path, f"Updated at {t()}", contents, repo_file.sha)
79
+ except:
80
+ repo.create_file(f"{token}.db", f"Created at {t()}", contents)
81
+ except Exception as e:
82
+ print(f"Error pushing to GitHub: {e}")
83
+
84
+
85
+ def downloadDbIfExist(token: str):
86
+ token = token.removesuffix(".db")
87
+ g = Github(os.getenv("GITHUB_TOKEN"))
88
+ user = g.get_user()
89
+ try:
90
+ repo = user.get_repo(token)
91
+ except:
92
+ repo = user.create_repo(token)
93
+ try:
94
+ repo_file = repo.get_contents(f"{token}.db")
95
+ with open(f"{token}.db", "wb") as f:
96
+ f.write(repo_file.decoded_content)
97
+ except Exception as e:
98
+ print(f"Error downloading from GitHub: {e}")
99
+
100
+
101
+ @app.on_event("startup")
102
+ def on_startup():
103
+ for token in tokens:
104
+ downloadDbIfExist(sha256(token))
105
+
106
+ if __name__ == "__main__":
107
+ import uvicorn
108
+
109
+ uvicorn.run(app=app, host="127.0.0.1")
110
+