|
1 | 1 | package Server.FTPServer; |
2 | 2 |
|
| 3 | +import Server.StaticVariables; |
3 | 4 | import org.apache.ftpserver.FtpServer; |
| 5 | +import org.apache.ftpserver.FtpServerConfigurationException; |
4 | 6 | import org.apache.ftpserver.FtpServerFactory; |
5 | 7 | import org.apache.ftpserver.ftplet.*; |
6 | 8 | import org.apache.ftpserver.listener.ListenerFactory; |
| 9 | +import org.apache.ftpserver.ssl.SslConfigurationFactory; |
7 | 10 | import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory; |
| 11 | +import org.apache.ftpserver.usermanager.UserManagerFactory; |
8 | 12 | import org.apache.ftpserver.usermanager.impl.BaseUser; |
9 | 13 | import org.apache.ftpserver.usermanager.impl.WritePermission; |
10 | 14 |
|
11 | 15 | import java.io.File; |
| 16 | +import java.io.FileOutputStream; |
12 | 17 | import java.io.IOException; |
13 | | -import java.util.ArrayList; |
14 | | -import java.util.HashMap; |
15 | | -import java.util.List; |
16 | | -import java.util.Map; |
| 18 | +import java.io.PrintStream; |
| 19 | +import java.text.FieldPosition; |
| 20 | +import java.text.SimpleDateFormat; |
| 21 | +import java.util.*; |
| 22 | + |
| 23 | +import static Server.StaticVariables.*; |
| 24 | + |
| 25 | +public class FTPService implements Runnable { |
| 26 | + |
| 27 | + private FtpServer server; |
| 28 | + |
| 29 | + private FtpServerFactory serverFactory; |
| 30 | + private ListenerFactory listenerFactory; |
| 31 | + |
| 32 | + private UserManager um; |
| 33 | + private PropertiesUserManagerFactory userManagerFactory; |
| 34 | + |
| 35 | + private boolean LOGGING = true; |
| 36 | + private File logFile = new File(ftpLogFile); |
| 37 | + private PrintStream logStream; |
| 38 | + |
| 39 | + private String LOG_TAG = "FTPService"; |
| 40 | + |
| 41 | + @Override |
| 42 | + public void run() { |
| 43 | + try { |
| 44 | + logFile.createNewFile(); |
| 45 | + logStream = new PrintStream(new FileOutputStream(logFile)); |
| 46 | + } catch (IOException e) { |
| 47 | + e.printStackTrace(); |
| 48 | + } |
| 49 | + |
| 50 | + try { |
| 51 | + ftpServerSetUp(); |
| 52 | + } catch (IOException e) { |
| 53 | + e.printStackTrace(); |
| 54 | + } |
| 55 | + |
| 56 | + server = serverFactory.createServer(); |
| 57 | + try { |
| 58 | + server.start(); //Your FTP server starts listening for incoming FTP-connections, using the configuration options previously set |
| 59 | + } catch (FtpException ex) { |
| 60 | + ex.printStackTrace(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void ftpServerSetUp() throws IOException { |
17 | 65 |
|
18 | | -public class FTPService { |
19 | | - public void ftpServer() { |
20 | 66 | //setting up a server Factory |
21 | | - FtpServerFactory serverFactory = new FtpServerFactory(); |
22 | | - ListenerFactory factory = new ListenerFactory(); |
| 67 | + serverFactory = new FtpServerFactory(); |
| 68 | + listenerFactory = new ListenerFactory(); |
| 69 | + |
| 70 | + //defineSSLConf(); |
| 71 | + defineListener(); |
| 72 | + setUpUser(); |
23 | 73 |
|
24 | | - factory.setPort(5000);// set the port of the listener (choose your desired port, not 1234) |
25 | | - serverFactory.addListener("default", factory.createListener()); // adding a "default Listener" |
26 | | - PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); // adding a new UserManagementClass |
27 | | - userManagerFactory.setFile(new File("~/ftp/userList.properties"));//choose any. We're telling the FTP-server where to read its user list |
| 74 | + serverFactory.setUserManager(um); |
| 75 | + Map<String, Ftplet> m = new HashMap<>(); |
| 76 | + m.put("miaFtplet", new FtpLetImpl()); |
| 77 | + serverFactory.setFtplets(m); |
| 78 | + |
| 79 | + Map<String, Ftplet> mappa = serverFactory.getFtplets(); |
| 80 | + |
| 81 | + log(String.valueOf(mappa.size())); |
| 82 | + log("Thread #" + Thread.currentThread().getId()); |
| 83 | + log(mappa.toString()); |
| 84 | + } |
| 85 | + |
| 86 | + private void defineSSLConf() { |
| 87 | + // define SSL configuration |
| 88 | + SslConfigurationFactory ssl = new SslConfigurationFactory(); |
| 89 | + ssl.setKeystoreFile(new File("src/main/resources/ftpServer.jks")); |
| 90 | + ssl.setKeystorePassword("password"); |
| 91 | + |
| 92 | + // set the SSL configuration for the listener |
| 93 | + listenerFactory.setSslConfiguration(ssl.createSslConfiguration()); |
| 94 | + listenerFactory.setImplicitSsl(true); |
| 95 | + |
| 96 | + // replace the default listener |
| 97 | + serverFactory.addListener("default", listenerFactory.createListener()); |
| 98 | + PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); |
| 99 | + userManagerFactory.setFile(new File("myusers.properties")); |
| 100 | + serverFactory.setUserManager(userManagerFactory.createUserManager()); |
| 101 | + } |
| 102 | + |
| 103 | + private void defineListener() { |
| 104 | + listenerFactory.setPort(ftpPort);// set the port of the listener (choose your desired port, not 1234) |
| 105 | + serverFactory.addListener("default", listenerFactory.createListener()); // adding a "default Listener" |
| 106 | + userManagerFactory = new PropertiesUserManagerFactory(); // adding a new UserManagementClass |
| 107 | + userManagerFactory.setFile(new File(ftpServiceUserPropertiesFile));//choose any. We're telling the FTP-server where to read its user list |
28 | 108 | userManagerFactory.setPasswordEncryptor(new PasswordEncryptorsImpl()); // encrypts passwords of users by using the EncrImpl |
| 109 | + serverFactory.setUserManager(userManagerFactory.createUserManager()); |
| 110 | + } |
29 | 111 |
|
| 112 | + private void setUpUser() throws IOException { |
30 | 113 | //Let's add a user, since our userList.properties file is empty on our first test run |
31 | 114 | BaseUser user = new BaseUser(); |
32 | 115 |
|
33 | | - user.setName("ohdmOffViewer"); |
34 | | - user.setPassword("H!3r0glyph Sat3llite Era$er"); |
35 | | - user.setHomeDirectory("/map"); |
| 116 | + user.setName(standardUserName); |
| 117 | + user.setPassword(standardUserPassword); |
| 118 | + user.setHomeDirectory(ftpServiceMapDir); |
36 | 119 |
|
37 | 120 | List<Authority> authorities = new ArrayList<>(); |
38 | 121 | authorities.add(new WritePermission()); |
39 | 122 | user.setAuthorities(authorities); |
40 | | - UserManager um = userManagerFactory.createUserManager(); |
41 | 123 |
|
| 124 | + try { |
| 125 | + um = userManagerFactory.createUserManager(); |
| 126 | + } catch (FtpServerConfigurationException e) { |
| 127 | + try { |
| 128 | + new File(ftpServiceUserPropertiesFile).createNewFile(); |
| 129 | + } catch (IOException ex) { |
| 130 | + new File("./ftp").mkdir(); |
| 131 | + new File(ftpServiceUserPropertiesFile).createNewFile(); |
| 132 | + } |
| 133 | + um = userManagerFactory.createUserManager(); |
| 134 | + } |
42 | 135 | try { |
43 | 136 | um.save(user);//Save the user to the user list on the filesystem |
44 | 137 | } catch (FtpException e1) { |
45 | 138 | //Deal with exception as you need |
46 | 139 | } |
| 140 | + } |
47 | 141 |
|
48 | | - serverFactory.setUserManager(um); |
49 | | - Map<String, Ftplet> m = new HashMap<>(); |
50 | | - m.put("miaFtplet", new FtpLetImpl()); |
| 142 | + public void log(String msg) { |
| 143 | + if (!LOGGING) |
| 144 | + return; |
51 | 145 |
|
52 | | - serverFactory.setFtplets(m); |
| 146 | + if (logStream == null) |
| 147 | + throw new NullPointerException("ERROR: PrintStream for log not initialized"); |
53 | 148 |
|
54 | | - //Map<String, Ftplet> mappa = serverFactory.getFtplets(); |
55 | | - //System.out.println(mappa.size()); |
56 | | - //System.out.println("Thread #" + Thread.currentThread().getId()); |
57 | | - //System.out.println(mappa.toString()); |
58 | | - |
59 | | - FtpServer server = serverFactory.createServer(); |
60 | | - try { |
61 | | - server.start();//Your FTP server starts listening for incoming FTP-connections, using the configuration options previously set |
62 | | - } catch (FtpException ex) { |
63 | | - //Deal with exception as you need |
64 | | - } |
| 149 | + SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z"); |
| 150 | + Date date = new Date(System.currentTimeMillis()); |
| 151 | + logStream.println(LOG_TAG + formatter.format(date) + " | " + msg); |
65 | 152 | } |
66 | 153 | } |
0 commit comments