-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservidor.py
More file actions
227 lines (192 loc) · 6.78 KB
/
Copy pathservidor.py
File metadata and controls
227 lines (192 loc) · 6.78 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'''
PiChat - Chat Corporativo Almacenamiento-Básico en Red
Copyright (C) 2025 Santiago Potes Giraldo
SPDX-License-Identifier: GPL-3.0-or-later
Este archivo es parte de PiChat.
PiChat is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import os
import json
from argon2 import PasswordHasher
from flask import (
Flask, request, jsonify, redirect, url_for,
send_from_directory, render_template
)
from flask_socketio import SocketIO, join_room, leave_room, send
from flask_login import (
LoginManager, UserMixin, login_user, logout_user,
login_required, current_user
)
from werkzeug.utils import secure_filename
from flask_argon2 import Argon2
# --- CONFIGURACIÓN INICIAL ---
app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*") # SocketIO envuelve a Flask
app.secret_key = os.environ.get("SECRET_KEY", "a-very-secret-key-for-dev")
argon2 = Argon2(app)
ph = PasswordHasher()
print("configuracion inicial completada ...")
# --- CONFIGURACIÓN DE CARPETAS ---
UPLOAD_FOLDER = './cuarentena'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
print("verificacion de archivos demo ...")
# --- USUARIOS BASE ---
users = {
os.getenv("ADMIN_USER", "admin"): {
"password": ph.hash(os.getenv("ADMIN_PASS", "admin123")),
"role": "administrator"
},
os.getenv("CLIENT_USER", "cliente"): {
"password": ph.hash(os.getenv("CLIENT_PASS", "cliente123")),
"role": "cliente"
},
os.getenv("USR_USER", "usuario"): {
"password": ph.hash(os.getenv("USR_PASS", "usuario123")),
"role": "usuario"
}
}
print("usuarios base generados...")
'''
# --- DEMO USERS DESDE ENV ---
try:
demo_users_env = os.getenv("DEMO_USERS", "[]")
demo_users = json.loads(demo_users_env)
for u in demo_users:
users[u["username"]] = {
"password": ph.hash(u["password"]),
"role": u.get("role", "usuario")
}
except Exception as e:
print(f"[WARN] No se pudieron cargar demo_users: {e}")
print("usuarios demo creados ....")
'''
# --- LOGIN MANAGER ---
login_manager = LoginManager(app)
login_manager.login_view = 'login'
class Usuario(UserMixin):
def __init__(self, username, role):
self.id = username
self.rol = role
@login_manager.user_loader
def load_user(user_id):
if user_id in users:
return Usuario(user_id, users[user_id]['role'])
return None
print("login initializied... Starting app....")
# --- RUTAS ---
@app.route('/')
def home():
if current_user.is_authenticated:
return redirect(url_for('inicio'))
return redirect(url_for('login'))
@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('inicio'))
if request.method == 'POST':
user = request.form['usuario']
password = request.form['clave']
if user in users:
try:
ph.verify(users[user]['password'], password)
login_user(Usuario(user, users[user]['role']))
return redirect(url_for('inicio'))
except Exception:
pass
return render_template("login.html", error="Credenciales inválidas.")
return render_template("login.html")
@app.route('/logout',methods=['GET','POST'])
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
@app.route('/inicio')
@login_required
def inicio():
return render_template('inicio.html', current_user=current_user)
# --- FUNCIONALIDAD DE ARCHIVOS ---
@app.route('/subir', methods=['GET', 'POST'])
@login_required
def subir():
if current_user.rol == 'usuario':
return 'No tienes permiso para subir archivos', 403
if request.method == 'POST':
if 'archivo' not in request.files:
return 'No se encontró el archivo', 400
archivo = request.files['archivo']
if archivo.filename == '':
return 'No se seleccionó ningún archivo', 400
filename = secure_filename(archivo.filename)
archivo.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('listar'))
return render_template("subir.html")
@app.route('/listar')
@login_required
def listar():
archivos = os.listdir(UPLOAD_FOLDER)
return render_template("listar.html", archivos=archivos)
@app.route('/descargar/<nombre>')
@login_required
def descargar(nombre):
return send_from_directory(UPLOAD_FOLDER, nombre, as_attachment=True)
@app.route('/eliminar/<nombre>')
@login_required
def eliminar(nombre):
if current_user.rol != 'administrator':
return 'No tienes permiso para eliminar archivos', 403
try:
os.remove(os.path.join(UPLOAD_FOLDER, secure_filename(nombre)))
except FileNotFoundError:
pass
return redirect(url_for('listar'))
@app.route('/chat')
@login_required
def chat():
return render_template('chat.html', current_user=current_user)
# --- SOCKET.IO ---
chat_rooms = {}
@socketio.on('join')
def on_join(data):
username = current_user.id
room_code = data['room']
password = data['password']
is_group = data.get('is_group', False)
if room_code not in chat_rooms:
chat_rooms[room_code] = ph.hash(password)
else:
try:
ph.verify(chat_rooms[room_code], password)
except Exception: # argon2.exceptions.VerifyMismatchError
send({'msg': 'Contraseña incorrecta.', 'type': 'error'})
return
join_room(room_code)
send({'msg': f"👋 {username} se ha unido.", 'user': 'Servidor', 'is_group': is_group}, to=room_code)
@socketio.on('leave')
def on_leave(data):
username = current_user.id
room_code = data['room']
leave_room(room_code)
send({'msg': f"🚪 {username} ha salido.", 'user': 'Servidor'}, to=room_code)
@socketio.on('message')
def handle_message(data):
username = current_user.id
room = data['room']
msg = data['msg']
is_group = data.get('is_group', False)
send({'msg': msg, 'user': username, 'is_group': is_group}, to=room)
# --- INICIO ---
if __name__ == '__main__':
port = int(os.environ.get("PORT", 8080))
socketio.run(app, host='0.0.0.0', port=port) # 👉 Para gunicorn/render
print(f"app running at host : 0.0.0.0 and port {port}")
application = app