Skip to content
Snippets Groups Projects
Commit 2e8ddbb6 authored by Ferhat Hocine's avatar Ferhat Hocine
Browse files

rendu intermédiaire ajout de commandes

parent 9475eded
Branches
Tags
Loading
Showing
with 514 additions and 24 deletions
# Created by https://www.toptal.com/developers/gitignore/api/maven,java,eclipse
# Edit at https://www.toptal.com/developers/gitignore?templates=maven,java,eclipse
### Eclipse ###
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# PyDev specific (Python IDE for Eclipse)
*.pydevproject
# CDT-specific (C/C++ Development Tooling)
.cproject
# CDT- autotools
.autotools
# Java annotation processor (APT)
.factorypath
# PDT-specific (PHP Development Tools)
.buildpath
# sbteclipse plugin
.target
# Tern plugin
.tern-project
# TeXlipse plugin
.texlipse
# STS (Spring Tool Suite)
.springBeans
# Code Recommenders
.recommenders/
# Annotation Processing
.apt_generated/
.apt_generated_test/
# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet
# Uncomment this line if you wish to ignore the project description file.
# Typically, this file would be tracked if it contains build/dependency configurations:
#.project
### Eclipse Patch ###
# Spring Boot Tooling
.sts4-cache/
### Java ###
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
### Maven ###
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar
# End of https://www.toptal.com/developers/gitignore/api/maven,java,eclipse
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>serveurFTP</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding/<project>=UTF-8
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sr.projet2</groupId>
<artifactId>serveurFTP</artifactId>
<version>1.0-SNAPSHOT</version>
<name>serveurFTP</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
File added
File added
...@@ -35,6 +35,16 @@ ...@@ -35,6 +35,16 @@
<artifactId>maven-clean-plugin</artifactId> <artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version> <version>3.1.0</version>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<reportOutputDirectory>${project.build.directory}/docs</reportOutputDirectory>
<destDir>docs</destDir>
<nohelp>true</nohelp>
</configuration>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin> <plugin>
<artifactId>maven-resources-plugin</artifactId> <artifactId>maven-resources-plugin</artifactId>
...@@ -51,6 +61,7 @@ ...@@ -51,6 +61,7 @@
<plugin> <plugin>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version> <version>3.0.2</version>
<configuration><archive><manifest><mainClass>sr.projet2.Main</mainClass></manifest></archive></configuration>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-install-plugin</artifactId> <artifactId>maven-install-plugin</artifactId>
......
package sr.projet2.Request;
import java.io.IOException;
import sr.projet2.Server.FtpConnexion;
public class FEAT implements request {
private FtpConnexion ftp;
public FEAT(FtpConnexion ftp) {
this.ftp =ftp;
}
@Override
public void send() throws IOException{
if (this.ftp.getSocket() == null ) {
throw new IOException("Ftp server error");
}
try {
this.ftp.getBufferedWriter().write("211-Features.\r\n");
this.ftp.getBufferedWriter().write("211 End.\r\n");
this.ftp.getBufferedWriter().flush();
}
catch(IOException e){
throw new IOException("Connexion failed");
}
}
}
package sr.projet2.Request;
import java.io.IOException;
import sr.projet2.Server.FtpConnexion;
public class LIST implements request {
private FtpConnexion ftp;
public LIST(FtpConnexion ftp) {
this.ftp =ftp;
}
@Override
public void send() throws IOException{
if (this.ftp.getSocket() == null ) {
throw new IOException("Ftp server error");
}
try {
this.ftp.getBufferedWriter().write("530 please login with USER AND PASS.\r\n");
this.ftp.getBufferedWriter().flush();
}
catch(IOException e){
throw new IOException("Connexion failed");
}
}
}
...@@ -6,8 +6,10 @@ import sr.projet2.Server.FtpConnexion; ...@@ -6,8 +6,10 @@ import sr.projet2.Server.FtpConnexion;
public class PASS implements request { public class PASS implements request {
private FtpConnexion ftp; private FtpConnexion ftp;
public PASS(FtpConnexion ftp) { private String pass;
public PASS(FtpConnexion ftp, String pass) {
this.ftp =ftp; this.ftp =ftp;
this.pass=pass;
} }
@Override @Override
...@@ -17,8 +19,14 @@ public class PASS implements request { ...@@ -17,8 +19,14 @@ public class PASS implements request {
} }
try { try {
this.ftp.getBufferedWriter().write("Login successful.\r\n"); if (this.pass.equals("anonymous")){
this.ftp.getBufferedWriter().flush(); this.ftp.getBufferedWriter().write("230 Login successful.\r\n");
this.ftp.getBufferedWriter().flush();
this.ftp.connect();
}else{
this.ftp.getBufferedWriter().write("530 unknown Password\r\n");
this.ftp.getBufferedWriter().flush();
}
} }
catch(IOException e){ catch(IOException e){
throw new IOException("Connexion failed"); throw new IOException("Connexion failed");
......
package sr.projet2.Request;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import sr.projet2.Server.FtpConnexion;
public class PASV implements request {
private FtpConnexion ftp;
public PASV(FtpConnexion ftp) {
this.ftp =ftp;
}
@Override
public void send() throws IOException{
if (this.ftp.getSocket() == null ) {
throw new IOException("Ftp server error");
}
try {
ServerSocket socket= new ServerSocket(0);
int newPort = socket.getLocalPort();
int port1 = newPort / 256;
int port2 = newPort % 256;
String ips[] = this.ftp.getSocket().getLocalAddress().getHostAddress().split("\\.");
String ip = ips[0] + "," + ips[1] + "," + ips[2] + "," + ips[3] + "," + port1 + "," + port2;
this.ftp.getBufferedWriter().write("227 Entering Passive Mode ("+ip+").\r\n");
this.ftp.getBufferedWriter().flush();
}
catch(IOException e){
throw new IOException("Connexion failed");
}
}
}
package sr.projet2.Request;
import java.io.IOException;
import sr.projet2.Server.FtpConnexion;
public class PWD implements request {
private FtpConnexion ftp;
public PWD(FtpConnexion ftp) {
this.ftp =ftp;
}
@Override
public void send() throws IOException{
if (this.ftp.getSocket() == null ) {
throw new IOException("Ftp server error");
}
try {
this.ftp.getBufferedWriter().write("257 \""+this.ftp.getDirectory()+"\" is the current directory.\r\n");
this.ftp.getBufferedWriter().flush();
}
catch(IOException e){
throw new IOException("Connexion failed");
}
}
}
package sr.projet2.Request;
import java.io.IOException;
import sr.projet2.Server.FtpConnexion;
public class SYST implements request {
private FtpConnexion ftp;
public SYST(FtpConnexion ftp) {
this.ftp =ftp;
}
@Override
public void send() throws IOException{
if (this.ftp.getSocket() == null ) {
throw new IOException("Ftp server error");
}
try {
this.ftp.getBufferedWriter().write("215 UNIX Type: L8.\r\n");
this.ftp.getBufferedWriter().flush();
}
catch(IOException e){
throw new IOException("Connexion failed");
}
}
}
package sr.projet2.Request;
import java.io.IOException;
import sr.projet2.Server.FtpConnexion;
public class TYPE implements request {
private FtpConnexion ftp;
private String type;
public TYPE(FtpConnexion ftp,String type) {
this.ftp =ftp;
this.type=type;
}
@Override
public void send() throws IOException{
if (this.ftp.getSocket() == null ) {
throw new IOException("Ftp server error");
}
try {
switch(this.type) {
case "A" :
this.type = "ASCII";
break;
case "E" :
this.type = "EBCDIC";
break;
case "I" :
this.type = "Switching to Binary mode";
break;
case "L" :
this.type = "local";
break;
default:
this.ftp.getBufferedWriter().write("400 incorrect type.\r\n");
this.ftp.getBufferedWriter().flush();
return;
}
this.ftp.getBufferedWriter().write("200 Type "+this.type+".\r\n");
this.ftp.getBufferedWriter().flush();
}
catch(IOException e){
throw new IOException("Connexion failed");
}
}
}
...@@ -6,18 +6,26 @@ import sr.projet2.Server.FtpConnexion; ...@@ -6,18 +6,26 @@ import sr.projet2.Server.FtpConnexion;
public class USER implements request { public class USER implements request {
private FtpConnexion ftp; private FtpConnexion ftp;
public USER(FtpConnexion ftp) { private String user;
public USER(FtpConnexion ftp,String user) {
this.ftp =ftp; this.ftp =ftp;
this.user=user;
} }
@Override @Override
public void send() throws IOException{ public void send() throws IOException{
if (this.ftp.getSocket() == null ) { if (this.ftp.getSocket() == null ) {
throw new IOException("Ftp server error");
} }
try { try {
this.ftp.getBufferedWriter().write("331 PLEASE SPECIFY THE PASSWORD."); if (this.user.equals("anonymous")){
this.ftp.getBufferedWriter().flush(); this.ftp.getBufferedWriter().write("331 PLEASE SPECIFY THE PASSWORD\r\n");
this.ftp.getBufferedWriter().flush();
}
else{
this.ftp.getBufferedWriter().write("530 unknown User\r\n");
this.ftp.getBufferedWriter().flush();
}
} }
catch(IOException e){ catch(IOException e){
throw new IOException("Connexion failed"); throw new IOException("Connexion failed");
......
...@@ -7,13 +7,21 @@ import java.io.InputStreamReader; ...@@ -7,13 +7,21 @@ import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.Random;
public class ConnectionServer { public class ConnectionServer {
private int PORT; private int PORT;
private ServerSocket srvSckt; private ServerSocket srvSckt;
public ConnectionServer (int PORT) throws IOException { public ConnectionServer (int PORT) throws IOException {
if(PORT>1024 && PORT<8080){
this.PORT=PORT; this.PORT=PORT;
}
else {
Random rand = new Random();
int nombreAleatoire = rand.nextInt(8079 - 1025) + 1024;
this.PORT=nombreAleatoire;
}
server(); server();
} }
...@@ -22,6 +30,7 @@ public class ConnectionServer { ...@@ -22,6 +30,7 @@ public class ConnectionServer {
public void server() throws IOException { public void server() throws IOException {
try { try {
this.srvSckt=new ServerSocket(this.PORT); this.srvSckt=new ServerSocket(this.PORT);
System.out.println("server started");
} }
catch (IOException e) { catch (IOException e) {
throw new IOException("Connexion failed"); throw new IOException("Connexion failed");
...@@ -31,10 +40,8 @@ public class ConnectionServer { ...@@ -31,10 +40,8 @@ public class ConnectionServer {
public void serverConnect() throws IOException { public void serverConnect() throws IOException {
try { try {
Socket socket=this.srvSckt.accept(); Socket socket=this.srvSckt.accept();
BufferedReader bfR =new BufferedReader(new InputStreamReader(socket.getInputStream())); FtpConnexion ftp= new FtpConnexion(socket);
BufferedWriter bfW = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); ftp.run();
FtpConnexion ftp= new FtpConnexion(socket, bfR, bfW);
ftp.start();
} }
catch(IOException e){ catch(IOException e){
e.printStackTrace(); e.printStackTrace();
......
...@@ -3,22 +3,32 @@ package sr.projet2.Server; ...@@ -3,22 +3,32 @@ package sr.projet2.Server;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket; import java.net.Socket;
import sr.projet2.Request.AUTH_SSL; import sr.projet2.Request.AUTH_SSL;
import sr.projet2.Request.AUTH_TLS; import sr.projet2.Request.AUTH_TLS;
import sr.projet2.Request.FEAT;
import sr.projet2.Request.PASS; import sr.projet2.Request.PASS;
import sr.projet2.Request.PASV;
import sr.projet2.Request.PWD;
import sr.projet2.Request.SYST;
import sr.projet2.Request.TYPE;
import sr.projet2.Request.USER; import sr.projet2.Request.USER;
public class FtpConnexion extends Thread { public class FtpConnexion extends Thread {
private Socket socket; private Socket socket;
private BufferedReader bfR; private BufferedReader bfR;
private BufferedWriter bfW; private BufferedWriter bfW;
public FtpConnexion(Socket socket,BufferedReader bfR,BufferedWriter bfW) { private boolean Connected;
private String directory;
public FtpConnexion(Socket socket) throws IOException {
this.socket=socket; this.socket=socket;
this.bfR=bfR; this.bfR =new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
this.bfW=bfW; this.bfW = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
this.Connected=false;
this.directory="/";
} }
public void auth() throws IOException{ public void auth() throws IOException{
...@@ -47,23 +57,54 @@ public class FtpConnexion extends Thread { ...@@ -47,23 +57,54 @@ public class FtpConnexion extends Thread {
} }
public void read(String s) throws IOException { public void read(String readLine) throws IOException {
if(s.startsWith("AUTH TLS")) { if(readLine.startsWith("AUTH TLS")) {
AUTH_TLS request= new AUTH_TLS(this); AUTH_TLS request= new AUTH_TLS(this);
request.send(); request.send();
} }
else if (s.startsWith("AUTH SSL")){ else if (readLine.startsWith("AUTH SSL")){
AUTH_SSL request= new AUTH_SSL(this); AUTH_SSL request= new AUTH_SSL(this);
request.send(); request.send();
} }
else if (s.startsWith("USER")){ else if (readLine.startsWith("USER")){
USER request= new USER(this); String[] mystring=readLine.split(" ");
USER request= new USER(this,mystring[1].trim());
request.send(); request.send();
} }
else if (s.startsWith("PASS")){ else if (readLine.startsWith("PASS")){
PASS request= new PASS(this); String[] mystring=readLine.split(" ");
PASS request= new PASS(this,mystring[1].trim());
request.send(); request.send();
} }
else {
if(this.Connected) {
if(readLine.startsWith("PWD")) {
PWD request= new PWD(this);
request.send();
}
else if(readLine.startsWith("SYST")) {
SYST request= new SYST(this);
request.send();
}
else if(readLine.startsWith("FEAT")) {
FEAT request= new FEAT(this);
request.send();
}
else if (readLine.startsWith("TYPE")){
String[] mystring=readLine.split(" ");
TYPE request= new TYPE(this,mystring[1].trim());
request.send();
}
else if (readLine.startsWith("PASV")){
PASV request= new PASV(this);
request.send();
}
}
else {
this.bfW.write("530 server is not connected\r\n");
this.bfW.flush();
}
}
} }
...@@ -76,6 +117,16 @@ public class FtpConnexion extends Thread { ...@@ -76,6 +117,16 @@ public class FtpConnexion extends Thread {
public BufferedReader getBufferedReader() { public BufferedReader getBufferedReader() {
return this.bfR; return this.bfR;
} }
public boolean getConnectedState(){
return this.Connected;
}
public void connect() {
this.Connected=true;
}
public void disconnect(){
this.Connected=false;
}
public String getDirectory(){
return this.directory;
}
} }
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment