Ftp/FtpServer.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * FtpServer.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include "../TcpServer.h"
14 #include <WHashMap.h>
15 #include <FileSystem.h>
16 
18 
23 class CustomFtpServer : public TcpServer
24 {
25  friend class FtpServerConnection;
26 
27 public:
28  CustomFtpServer(IFS::FileSystem* fileSystem = nullptr) : fileSystem(fileSystem)
29  {
30  setTimeOut(900);
31  }
32 
39  virtual IFS::UserRole validateUser(const char* login, const char* pass) = 0;
40 
41 protected:
42  TcpConnection* createClient(tcp_pcb* clientTcp) override;
43 
51  virtual bool onCommand([[maybe_unused]] String cmd, [[maybe_unused]] String data,
52  [[maybe_unused]] FtpServerConnection& connection)
53  {
54  return false;
55  }
56 
58  {
59  return fileSystem ?: ::getFileSystem();
60  }
61 
62 private:
63  IFS::FileSystem* fileSystem;
64 };
65 
70 class FtpServer : public CustomFtpServer
71 {
72 public:
73  void addUser(const String& login, const String& pass, IFS::UserRole userRole = IFS::UserRole::Admin);
74  IFS::UserRole validateUser(const char* login, const char* pass) override;
75 
76 protected:
77  bool onCommand(String cmd, String data, FtpServerConnection& connection) override;
78 
79 private:
80  struct User {
81  String password;
82  IFS::UserRole role;
83  };
84  using UserList = HashMap<String, User>;
85  UserList users;
86 };
Definition: Ftp/FtpServer.h:24
IFS::FileSystem * getFileSystem() const
Definition: Ftp/FtpServer.h:57
virtual IFS::UserRole validateUser(const char *login, const char *pass)=0
Validate user.
TcpConnection * createClient(tcp_pcb *clientTcp) override
virtual bool onCommand([[maybe_unused]] String cmd, [[maybe_unused]] String data, [[maybe_unused]] FtpServerConnection &connection)
Handle an incoming command.
Definition: Ftp/FtpServer.h:51
CustomFtpServer(IFS::FileSystem *fileSystem=nullptr)
Definition: Ftp/FtpServer.h:28
Definition: FtpServerConnection.h:28
Provides FTP server.
Definition: Ftp/FtpServer.h:71
bool onCommand(String cmd, String data, FtpServerConnection &connection) override
void addUser(const String &login, const String &pass, IFS::UserRole userRole=IFS::UserRole::Admin)
IFS::UserRole validateUser(const char *login, const char *pass) override
Validate user.
Installable File System base class.
Definition: Components/IFS/src/include/IFS/FileSystem.h:40
The String class.
Definition: WString.h:133
Definition: TcpConnection.h:40
void setTimeOut(uint16_t waitTimeOut)
Definition: TcpServer.h:31
UserRole
Definition: UserRole.h:36