FileServer Class
Namespace: Rebex.Net.Servers
Assembly: Rebex.FileServer.dll (version 8.0.9673)
Implements the SFTP/SSH server.
Syntax
public class FileServer : Server, IDisposable, IAsyncDisposable
Inherited Members
Examples
See https://www.rebex.net/file-server/samples.aspx for ready-to-use samples.
Starting the file server
// initialize new file server instance
var server = new Rebex.Net.Servers.FileServer();
// attach logger for diagnostic purposes
server.LogWriter = new Rebex.FileLogWriter(@"C:\MyData\sftp-server.log", LogLevel.Debug);
// bind FTP and SFTP protocols
server.Bind(21, FileServerProtocol.Ftp); // port 21 (default FTP port)
server.Bind(22, FileServerProtocol.Sftp); // port 22 (runs over SSH)
// configure server behavior
server.Settings.Banner = "Welcome to the server.";
server.Settings.MaxIdleDuration = (int)TimeSpan.FromMinutes(30).TotalSeconds;
server.Settings.FtpControlProtection = FtpProtection.Optional; // allow plain FTP
server.Settings.FtpDataProtection = FtpProtection.Optional; // allow unsecured transfers
// add server key (for SSH)
server.Keys.Add(new SshPrivateKey("key.ppk", "password"));
// add server certificate (for FTP)
server.Certificates.Add(CertificateChain.LoadPfx("cert.pfx", "password"));
// add some users
server.Users.Add("admin", "password", @"C:\MyData");
server.Users.Add("user01", "password", @"C:\MyData\user01");
server.Users.Add("user02", "password", @"C:\MyData\user02");
server.Users.Add("guest", "password", @"C:\MyData\public");
// start server in the background
server.Start();
Custom authentication
// load SSH public key for later validation
var adminKey = new SshPublicKey(@"C:\MyData\keys\admin.pub");
// configure server to allow 'password' and 'public-key' authentication
server.Settings.AllowedAuthenticationMethods =
AuthenticationMethods.Password | AuthenticationMethods.PublicKey;
// register pre-authentication event to setup authentication methods
server.PreAuthentication += (s, e) =>
{
// require 'key' authentication for admin and 'password' for others
if (e.UserName == "admin")
e.Accept(AuthenticationMethods.PublicKey);
else
e.Accept(AuthenticationMethods.Password);
};
// register authentication event to check login
server.Authentication += (s, e) =>
{
var user = e.Users[e.UserName];
if (user == null)
return;
if (user.Name == "admin")
{
// check the key
if (e.Key != null && adminKey.Equals(e.Key))
{
e.Accept(user);
return;
}
}
else
{
// check the password
if (user.CheckPassword(e.Password))
{
e.Accept(user);
return;
}
}
};
Custom file access
server.PathAccessAuthorization += (s, e) =>
{
// guest user has read-only access
if (e.User.Name == "guest")
{
// allow 'read' and 'list', deny the rest
e.Allow(FileSystemOperation.Read | FileSystemOperation.List);
}
else
{
// allow all operations for other users
e.Allow();
}
};
Applications using Rebex File Server
- Rebex Buru SFTP Server - fully featured SSH/SFTP/FTP server.
- Rebex Tiny SFTP Server - simple local SSH/SFTP server.
- Rebex Tiny FTP Server - simple local FTP server.
- test.rebex.net - free read-only SSH/SFTP/FTP service.
Constructors
| Name | Description |
|---|---|
| FileServer() | Initializes a new instance of this object. See https://www.rebex.net/file-server/features/ for code snippets of common use cases. |
Properties
| Name | Description |
|---|---|
| Bindings | Gets a read-only collection of server bindings. Inherited from Server. |
| Certificates | Gets a collection of server certificates. Inherited from Server. |
| IsRunning | Gets a value indicating whether the server is currently running. Inherited from Server. |
| Keys | Gets a collection of server keys. Inherited from Server. |
| LogWriter | Gets or sets the log writer used by this object. Inherited from Server. |
| Modules | Gets an enumerator for currently bound modules. Inherited from Server. |
| Sessions | Gets an enumerator for currently active sessions. Inherited from Server. |
| Settings | Gets server settings. |
| Users | Gets a user collection that provide a simple default user database which is used when no custom authentication has been defined. |
Methods
| Name | Description |
|---|---|
| Bind(ServerModule) | Binds the specified module to its default port on all network interfaces. Inherited from Server. |
| Bind(FileServerProtocol) | Binds the specified protocol to its default port on all network interfaces. |
| Bind(Int32, ServerModule) | Binds the specified module to the specified port on all network interfaces. Inherited from Server. |
| Bind(Int32, FileServerProtocol) | Binds the specified protocol to the specified port on all network interfaces. |
| Bind(IPEndPoint, ServerModule) | Binds the specified module to the specified endpoint. Inherited from Server. |
| Bind(IPEndPoint, FileServerProtocol) | Binds the specified protocol to the specified endpoint. |
| BindAsync(ServerModule) | Asynchronously binds the specified module to its default port on all network interfaces. Inherited from Server. |
| BindAsync(FileServerProtocol) | Asynchronously binds the specified protocol to its default port on all network interfaces. |
| BindAsync(Int32, ServerModule) | Asynchronously binds the specified module to the specified port on all network interfaces. Inherited from Server. |
| BindAsync(Int32, FileServerProtocol) | Binds the specified protocol to the specified port on all network interfaces. |
| BindAsync(IPEndPoint, ServerModule) | Asynchronously binds the specified module to the specified endpoint. Inherited from Server. |
| BindAsync(IPEndPoint, FileServerProtocol) | Binds the specified protocol to the specified endpoint. |
| CreateModuleHost(FileServerProtocol[]) | Creates SSH server module host. |
| Dispose() | Disposes the server object, releasing all listeners and sessions. Inherited from Server. |
| DisposeAsync() | Disposes the server object, releasing all listeners and sessions. Inherited from Server. |
| Start() | Starts the server. Inherited from Server. |
| StartAsync() | Asynchronously starts the server. Inherited from Server. |
| Stop() | Stops the server. Inherited from Server. |
| StopAsync() | Asynchronously stops the server. Inherited from Server. |
| Unbind() | Unbinds all modules. Inherited from Server. |
| Unbind(ServerModule) | Unbinds the specified module from all endpoints. Inherited from Server. |
| Unbind(Int32) | Unbinds all modules bound to the specified port. Inherited from Server. |
| Unbind(IPAddress) | Unbinds all modules bound to the specified address. Inherited from Server. |
| Unbind(IPEndPoint) | Unbinds all modules bound to the specified endpoint. Inherited from Server. |
| UnbindAsync() | Unbinds all modules. Inherited from Server. |
| UnbindAsync(ServerModule) | Unbinds the specified module from all endpoints. Inherited from Server. |
| UnbindAsync(Int32) | Unbinds all modules bound to the specified port. Inherited from Server. |
| UnbindAsync(IPAddress) | Unbinds all modules bound to the specified address. Inherited from Server. |
| UnbindAsync(IPEndPoint) | Unbinds all modules bound to the specified endpoint. Inherited from Server. |
Events
| Name | Description |
|---|---|
| Authentication | Occurs when client is being authenticated. Inherited from Server. |
| ChangePassword | Occurs when a client sends a request to change their password. Inherited from Server. |
| Connecting | Occurs when a client is connecting. Inherited from Server. |
| Disconnected | Occurs when a client has disconnected. Inherited from Server. |
| ErrorOccurred | Occurs when an error occurs while processing requests. Inherited from Server. |
| FileDownloaded | Occurs after a file (or a part of a file) has been downloaded. |
| FileUploaded | Occurs after a file (or a part of a file) has been uploaded. |
| PathAccessAuthorization | Occurs when path access authorization is required. |
| PreAuthentication | Occurs when client authentication is about to start. Inherited from Server. |
| PreNegotiation | Occurs when SSH negotiation is about to start. |
| ShellCommand | Occurs when a shell command is executed. |
| TunnelClosed | Occurs when a tunnel has been closed. |
| TunnelRequested | Occurs when a tunnel is requested. |