-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaFtpServer.java
More file actions
256 lines (228 loc) · 10.5 KB
/
JavaFtpServer.java
File metadata and controls
256 lines (228 loc) · 10.5 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package FTP.Server;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import FTP.Util.Util;
/**
* Servidor FTP principal que maneja conexiones concurrentes de clientes.
* <p>
* Este servidor implementa el protocolo FTP con soporte para:
* <ul>
* <li>Autenticación de usuarios basada en archivo</li>
* <li>Control de acceso basado en roles (RBAC)</li>
* <li>Modos de transferencia ACTIVE y PASSIVE</li>
* <li>Manejo concurrente de múltiples clientes mediante ExecutorService</li>
* </ul>
*
* @author Eduardo Díaz Sánchez
* @version 1.0
*/
public class JavaFtpServer {
/** Directorio raíz desde donde el servidor sirve archivos */
protected static String dirRoot;
/** Ruta al archivo de usuarios (modo legacy; puede sobreescribirse desde config) */
protected static String usersFilePath;
/** Almacén de usuarios (SQLite o fichero); compartido por todos los handlers */
protected static UserStore userStore;
/** Directorio base para archivos del sistema */
protected static final String FILES_DIR = "files";
/** Directorio que contiene información de usuarios */
protected static final String USERS_DIR = FILES_DIR + File.separator + "users";
/** Archivo que almacena las credenciales de usuario (formato: username:bcryptHash:profile) */
protected static final String USERS_FILE = USERS_DIR + File.separator + "users.txt";
/** Puerto de control FTP estándar */
protected static final int CONTROL_PORT = 21;
/** Flag para cierre ordenado (graceful shutdown) */
private static volatile boolean shuttingDown = false;
/** Referencia al ServerSocket para que el shutdown hook pueda cerrarlo */
private static volatile ServerSocket serverRef = null;
/**
* Verifica la existencia del directorio base 'files'.
*/
private static void checkFilesDir() {
File dir = new File(FILES_DIR);
if (!dir.exists() || !dir.isDirectory()) {
Util.printRedColor("\nERROR CRÍTICO: La carpeta 'files' no existe en la raiz del programa");
}
}
/**
* Solicita y configura el directorio raíz del servidor FTP.
* Valida que el directorio exista antes de aceptarlo.
*
* @param sc Scanner para leer entrada del usuario
*/
private static void setRoot(Scanner sc) {
String input;
File file;
do {
do {
System.out.println("\nIntroduzca el directorio raiz del servidor FTP");
System.out.print(":> ");
input = sc.nextLine();
if (input == null || input.trim().isEmpty()) {
Util.printRedColor("\nLa dirección root del servidor no puede estar vacía");
input = null;
}
} while (input == null);
dirRoot = input;
file = new File(dirRoot);
if (!file.exists())
Util.printRedColor("\nLa dirección root proporcionada para el servidor no existe, pruebe de nuevo.");
} while (!file.exists());
}
/**
* Punto de entrada principal del servidor FTP.
* Inicializa el servidor, muestra el banner, configura el directorio raíz
* y comienza a escuchar conexiones de clientes en el puerto 21.
*
* @param args Argumentos de línea de comandos (no utilizados)
* @throws InterruptedException Si el servidor es interrumpido
*/
public static void main(String[] args) throws InterruptedException {
ExecutorService execute = null;
Scanner sc = null;
ServerConfig config = new ServerConfig();
int serverPort;
System.out.println(" _____ _ _ _____ _____ _____");
System.out.println("/ ___| (_) | | | ___|_ _| ___ \\");
System.out.println("\\ `--. ___ _ ____ ___ __| | ___ _ __ | |_ | | | |_/ /");
System.out.println(" `--. \\/ _ \\ '__\\ \\ / / |/ _` |/ _ \\| '__| | _| | | | __/ ");
System.out.println("/\\__/ / __/ | \\ V /| | (_| | (_) | | | | | | | | ");
System.out.println("\\____/ \\___|_| \\_/ |_|\\__,_|\\___/|_| \\_| \\_/ \\_| ");
System.out.println("\nBy Eduardo Díaz");
FTP.Util.FileLogger.initialize();
FTP.Util.FileLogger.info("========== SERVIDOR FTP INICIADO ==========");
usersFilePath = USERS_FILE;
try {
config.loadFromFile("server.properties");
Util.printGreenColor("\n✓ Configuración cargada desde server.properties");
serverPort = config.getControlPort();
FTP.Util.FileLogger.setVerbose(config.isVerboseLogging());
FTP.Util.FileLogger.setRotation(config.getLogMaxSizeBytes(), config.getLogMaxBackupFiles());
} catch (IOException e) {
Util.printRedColor("\n⚠ No se pudo cargar server.properties, usando configuración por defecto");
serverPort = CONTROL_PORT;
}
// 1) Root directory: from config or ask interactively
if (!config.getRootDirectory().isEmpty()) {
dirRoot = config.getRootDirectory();
File rootFile = new File(dirRoot);
if (!rootFile.exists() || !rootFile.isDirectory()) {
Util.printRedColor("\nERROR: Directorio raíz no existe: " + dirRoot);
FTP.Util.FileLogger.error("Directorio raíz no existe: " + dirRoot);
return;
}
Util.printGreenColor("✓ Directorio raíz: " + dirRoot);
} else {
checkFilesDir();
sc = new Scanner(System.in);
setRoot(sc);
}
// 2) User store: SQLite if configured, otherwise TXT file
String dbPath = config.getUsersDatabase();
if (dbPath != null && !dbPath.isEmpty()) {
try {
File dbFile = new File(dbPath);
if (!dbFile.exists()) {
File parent = dbFile.getParentFile();
if (parent != null) parent.mkdirs();
}
SqliteUserStore sqliteStore = new SqliteUserStore(dbPath);
sqliteStore.initSchema();
userStore = sqliteStore;
Util.printGreenColor("✓ Usuarios: SQLite (" + dbPath + ")");
} catch (SQLException e) {
Util.printRedColor("\nERROR: No se pudo inicializar la base de usuarios: " + e.getMessage());
FTP.Util.FileLogger.error("SQLite usuarios: " + e.getMessage());
return;
}
} else {
usersFilePath = config.getUsersFile();
File usersFile = new File(usersFilePath);
if (!usersFile.exists() || !usersFile.isFile()) {
Util.printRedColor("\nERROR: Archivo de usuarios no existe: " + usersFilePath);
FTP.Util.FileLogger.error("Archivo de usuarios no existe: " + usersFilePath);
return;
}
userStore = new FileUserStore(usersFilePath);
Util.printGreenColor("✓ Usuarios: fichero (" + usersFilePath + ")");
}
int maxConn = config.getMaxConnections();
execute = Executors.newFixedThreadPool(maxConn);
Semaphore connectionLimit = new Semaphore(maxConn);
LoginThrottle loginThrottle = new LoginThrottle(config.getAuthMaxAttempts(), config.getAuthLockoutMinutes());
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
shuttingDown = true;
try {
if (serverRef != null) serverRef.close();
} catch (IOException ignored) { }
}));
ServerSocket server = null;
try {
server = new ServerSocket(serverPort);
serverRef = server;
Util.printGreenColor("\nServidor FTP iniciado en el puerto de control " + serverPort);
FTP.Util.FileLogger.info("Servidor escuchando en puerto " + serverPort + ", max conexiones: " + maxConn);
while (!shuttingDown) {
try {
Socket client = server.accept();
if (!connectionLimit.tryAcquire()) {
try {
client.getOutputStream().write("421 Too many connections. Try again later.\r\n".getBytes());
client.close();
} catch (IOException ignored) { }
FTP.Util.FileLogger.warning("Conexión rechazada: límite alcanzado desde " + client.getInetAddress());
continue;
}
FTP.Util.FileLogger.logConnection(client.getInetAddress().toString());
final Semaphore permit = connectionLimit;
final UserStore store = userStore;
execute.execute(() -> {
try {
FtpClientHandler handler = new FtpClientHandler(client, config, loginThrottle, store);
handler.run();
} catch (IOException e) {
Util.printRedColor("Error iniciando handler: " + e.getMessage());
} finally {
permit.release();
}
});
} catch (SocketException e) {
if (shuttingDown) break;
throw e;
}
}
} catch (IOException e) {
if (!shuttingDown) {
Util.printRedColor("\nError en el servidor: " + e.getMessage());
FTP.Util.FileLogger.error("Error en servidor: " + e.getMessage());
}
} catch (Exception e) {
Util.printRedColor("\nError: " + e.getMessage());
FTP.Util.FileLogger.error("Error inesperado: " + e.getMessage());
} finally {
serverRef = null;
if (server != null) try { server.close(); } catch (IOException ignored) { }
if (execute != null) {
execute.shutdown();
try {
if (!execute.awaitTermination(10, TimeUnit.SECONDS))
execute.shutdownNow();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
execute.shutdownNow();
}
}
if (sc != null) sc.close();
}
FTP.Util.FileLogger.info("========== SERVIDOR FTP DETENIDO ==========");
}
}