Skip to content
Snippets Groups Projects
Commit 4596bac3 authored by Nicolas Fernandes's avatar Nicolas Fernandes
Browse files

authent anonymous

parent 0bf26ea7
No related branches found
No related tags found
1 merge request!2Refacto/commands
node_modules/
.idea/codeStyles/
const messages = {
220: 'FTP server (vsftpd)',
230: '230 Already logged in.',
331 : 'Please specify the password.',
530 : 'Please FTPConnection with USER and PASS.'
}
\ No newline at end of file
import { write } from "../utils/responseUtils";
export default class AuthentFTP {
constructor(){
this.login = null;
this.password = null;
}
async serverResponse(data, socket) {
let response;
const dataArray = data.toString().replace(/\n|\r/g, '').split(" ");
switch (dataArray[0]) {
case "AUTH":
response = "530 Please FTPConnection with USER and PASS.";
break;
case "USER":
response = this.userCmd(dataArray);
break;
case "PASS":
response = this.passCmd();
if (response === true)
return;
break;
default:
response = "530 Please FTPConnection with USER and PASS.";
}
const clientResponse = await write(socket, response);
this.serverResponse(clientResponse, socket);
}
userCmd(data) {
if (this.connected) {
return `530 Can't change from guest user.`;
} else if (data[1] && data[1].toLowerCase() === 'anonymous') {
this.login = data[1].toLowerCase();
return '331 Please specify the password.';
} else {
return '530 This FTP server is anonymous only.';
}
}
passCmd() {
if (this.connected) {
return '230 Already logged in.';
} else if (this.login == null) {
return '503 Login with USER first.';
} else if (this.login == 'anonymous') {
this.connected = true;
this.password = "";
return '230 Login successful.';
}
}
}
\ No newline at end of file
import { createServer } from 'net';
import FileSystem from './FileSystem.js';
import { write } from "../utils/responseUtils";
import authent from './AuthentFTP.js'
export default class Server {
constructor() {
this.server = createServer(socket => this.init(socket));
this.server.listen(process.env.PORT, () => console.log('Server created'));
this.pasv();
}
async init(socket) {
console.log('Client connected');
this.login = null;
this.password = null;
this.connected = false;
const data = await this.write(socket, '220 FTP server (vsftpd)');
const data = await write(socket, '220 FTP server (vsftpd)');
const fs = new FileSystem();
this.connected = await authent.serverResponse(data, socket)
this.serverResponse(data, socket, fs);
socket.on('end', () => console.log('Closed'));
}
......@@ -25,7 +24,7 @@ export default class Server {
const dataArray = data.toString().replace(/\n|\r/g,'').split(" ");
switch (dataArray[0]) {
case "AUTH":
response = "530 Please login with USER and PASS.";
response = "530 Please FTPConnection with USER and PASS.";
break;
case "USER":
response = this.userCmd(dataArray);
......@@ -46,48 +45,13 @@ export default class Server {
break;
}
const clientResponse = await this.write(socket, response);
const clientResponse = await write(socket, response);
this.serverResponse(clientResponse, socket);
}
/**
* Write data to the socket buffer to execute the provided command
* @param {string} command
* @return {Promise<String>} Resolving on response from the server
*/
write(socket, command) {
return new Promise(resolve => {
socket.once('data', buffer => resolve(buffer.toString()));
socket.write(`${command}\r\n`);
});
}
userCmd(data) {
if (this.connected) {
return `530 Can't change from guest user.`;
} else if (data[1] && data[1].toLowerCase() === 'anonymous') {
this.login = data[1].toLowerCase();
return '331 Please specify the password.';
} else {
return '530 This FTP server is anonymous only.';
}
}
passCmd() {
if (this.connected) {
return '230 Already logged in.';
} else if (this.login == null) {
return '503 Login with USER first.';
} else if (this.login == 'anonymous') {
this.connected = true;
this.password = "";
return '230 Login successful.';
}
}
pasv() {
//serveur qui ecoute sur le portData
const serverData = createServer(socket => this.write(socket, this.data));
const serverData = createServer(socket => write(socket, this.data));
serverData.listen(0, () => console.log('Server data created'));
const portData = serverData.address().port;
const response = `227 Entering passive mode (127,0,0,1,${Math.floor(portData / 256).toString()},${Math.floor(portData % 256).toString()})`
......
/**
* Write data to the socket buffer to execute the provided command
* @param {string} command
* @return {Promise<String>} Resolving on response from the server
*/
export function write(socket, command) {
return new Promise(resolve => {
socket.once('data', buffer => resolve(buffer.toString()));
socket.write(`${command}\r\n`);
});
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment