Click here to Skip to main content
15,880,972 members
Articles / Web Development / HTML

Advanced FTP Server

Rate me:
Please Sign up or sign in to vote.
4.78/5 (26 votes)
24 Oct 2009CPOL7 min read 130K   20K   94   30
Enables remote access to your files and folders via FTP

Introduction

I always come across situations like I will be outside home and I may need some files or reference materials from my system. So I can ask any body at home to switch on the system through a call. Now the FTP server starts and I will be able to access my files and folders from anywhere. This FTP server is designed using C# .NET which enables full control over the application from remote locations.

Unique Feature

Their are lots of FTP server applications that are available free of cost. But those applications do not provide the facility to control the server remotely and some provide this facility by installing the required software in the remote machine which may not be possible all the times. But this provides a simple, user friendly web interface, where you can connect from a remote machine through a browser. This enables control over the application from remote machines. You can add new Users by setting their root paths remotely. This enables to allow others to use your system with restrictions for certain actions and specific path.

Concept/Code Behind AdvancedFTPServer

As soon windows starts up, a service named AdvFtpSvr starts and it executes FTP Server.exe from the installed path. Once the application is started, the process ends by itself as there is no work to do with the process. Here you may ask why use a Windows Service as the application can be started directly? The reason is the Windows service starts as soon as Windows is started and any process started by a Windows service has full access over the system which enables access to all the files and folders. There is an another way which we can use instead of Windows service which is Windows Scheduled task, but the disadvantage with this is that the scheduled task might had been disabled in some systems and is not the fully automated way of doing things.

Let us first see some of the basic and interesting methods. Most of us know that for FTP server programming, we need to listen on some ports (21) using a listener class. Generally when a listener is started, the firewall installed in the system blocks the application from receiving any incoming connections. So, for the application to work properly, the firewall must be configured to add an exception to the application. Adding exception to the firewall had been automated with the custom action in the installer.

A method from InstallerService.cs is as follows:

C#
void AddToFirewallException(string ApplicationName, string FilePath)
{
    // Add exception to windows firewall after installation
    string RegPath =
        @"SYSTEM\ControlSet001\Services\SharedAccess\Parameters\
		FirewallPolicy\StandardProfile\AuthorizedApplications\List";
    string KeyValue = FilePath + ":*:Enabled:" + ApplicationName;

    RegistryKey Key = Registry.LocalMachine.OpenSubKey(RegPath, true);
    Key.SetValue(FilePath, KeyValue);
    Key.Close();
    Key = null;
}

The above method takes two string params, (i.e. ApplicationName which is displayed in the exceptions list of the Windows firewall and FilePath which is the physical path to the executable).

The settings file (Settings.DAT) is just an encrypted XML file. Both the encryption and decryption are done using a single method described below.

A method from ApplicationSettings class in Settings.cs - A class which stores all the application settings in static methods and properties.

C#
static byte[] Crypt(byte[] Buffer)
{
    for (int i = 0; i < Buffer.Length; i++)
    {
        Buffer[i] ^= 36;
    }
    return Buffer;
}

The above method takes a byte array as input parameter and then processes each byte and then finally returns the encrypted byte array back. This method uses a very silly way of encrypting data but it is more than enough to protect a file from manual access.

Their are lot such petty interesting methods implemented in the application.

Let us now get deep into the implementation. As soon as the application starts first of all, the "Settings.dat" will be decrypted and loaded into memory. Then according to the settings, the FTP and HTTP listener will be started. Once the listener is started, it's now ready to serve the client's request.

First we shall discuss a little bit about FTP implementation.

There are three classes for the FTP server which are:

  • FTPServer in FTPServer.cs - This is the class which holds the listener.
  • FTPClient in FTPClient.cs - This is the class which holds all the methods required to service the FTP client.
  • FTPUser in FTPUser.cs - This is the class which holds the permission settings of all the users.

Now let us discuss about the methods in FTPServer class.

  • Start - This is the method which starts the listener to listen to the specified port for clients and fires the NewFTPClientArrived method whenever a new client arrives.
  • Stop - Stops the listener class from listening to the clients
  • NewFTPClientArrived - This is fired automatically whenever the new client is arrived. This creates a new object of the FTPClient class and stores the object in a local array list.

That's all with the FTPServer class. Let us now start with the methods in FTPClient class.

As soon as the object for the FTPClient class is created in the FTPServer class, it listens for the commands from the clients. There is a huge list of predefined commands for the FTP implementation. View the reference section at the bottom of this article for the links which describe each method and its implementation. Once the client sends the user name and password, the CommandReceived method checks for the username and password in the list and creates an object for the FTPUser class which holds all the rights information of the specific user. That's all to deal with the FTP implementation classes.

Let us now have a small discussion with the ApplicationLog class.

ApplicationLog class is a simple class holding a single static method named Write. This is used to write the application error events into the XML file for bug fixing purposes and for tracking some application changes. This write method is called throughout the project from inside a catch block passing the exception caught over there.

Finally let us have a short discussion on the HTTP implementation classes and then move on with a guide to use the application. The HTTP implementation class is the same as that of the FTP implementation class whereas all the commands used are not pre-defined and were defined by me. Unlink FTP classes instead of dealing with files and folders. This just deals with HTML codes, which is sent as response to each of the commands. All the pages for response are stored in the resource section of the application and are requested whenever needed.

A method from HTTPClient class is given below:

C#
void SendData(byte[] ResponseBody)
{
    if (ClientSocket.Connected)
        ClientSocket.Send(ResponseBody, ResponseBody.Length, SocketFlags.None);
}

The above method sends the response to the clients in byte array.

C#
void SendHeader(int TotalBytes, string StatusCode, string ContentType)
{
    string ResponseHeader = HttpVersion + " " + StatusCode + "\r\n";
    ResponseHeader += "Server: AdvancedFTPServer/2.5\r\n";
    ResponseHeader += "Date: " + DateTime.Now.ToString("r") + "\r\n";
    ResponseHeader += "Content-Type: " + ContentType + "\r\n";
    ResponseHeader += "Accept-Ranges: bytes\r\n";
    ResponseHeader += "Content-Length: " + TotalBytes + "\r\n\r\n";

    SendData(Encoding.ASCII.GetBytes(ResponseHeader));
}

This method sends the client a response header and once the response header is sent, the following bytes must contain the response body (i.e., HTML code). Response body can be sent using the same SendData method which takes a byte array as a parameter.

Now we are done with all the major classes and methods, and let us move on.

Using the Setup

First download and install the Setup from the link provided above, then restart your system. You will find a new icon in your system tray. Right click on the Tray Icon to display the context menu options as shown below. I have added the screen shots of the application. The code is self explanatory and I don't find much time to explain the features or code in detail. The default User Name and Password for the web authentication is "admin" and "admin" and the default port for the HTTP server is 9090. To connect to the web interface from the local system, just click on the Open HTTP menu from the context menu shown below.

Context Menu

Image 1

User Account Screen

Image 2

Add/Edit User Screen

Image 3

Web Interface

Image 4

For more help, just click on the help menu in the above screen after authenticating with the User Name and Password (default is "admin" and "admin").

Reference: List of raw FTP commands

Other Supportive Tools

When you use this from remote locations, you require an External IP Address. In case your External IP is generated dynamically, then you need an automated system to intimate your IP instantly. I use IPMon for this purpose.

Note

  • After the installation of the setup, the system needs to be restarted for the application to work properly.
  • The setup link provided above also installs a service as the server must be started as soon as Windows starts.
  • The installer automatically adds an exception for the application in Windows Firewall. If you had installed any other firewall, you need to manually add an exception or add to allowed/trusted list of the firewall for the application to work properly.
  • All the settings are stored in DAT file with the simple encryption, restricting people from manually viewing the information.

Points of Interest

This source code also contains:

  • A code piece for adding firewall exception
  • Developing Windows Services using C#
  • Deploying Windows Service using MSI
  • Adding Custom actions to MSI
  • A simple way of encryption

History

  • October 24, 2009 - Missing Resource file [Settings.xml] has been added

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India


Completed B.Com(CS) at DGVC and GNIIT Software Engineering at NIIT. Resident at Chennai and working as a Software Engineer.



 Language / Technology :

C#, ADO.NET, ASP.NET, MVC, WCF, ASP, PHP, XML, Java, J2EE, HTML, JavaScript, JQuery, AngularJS, VB Script, C++, MS SQL Server, SSRS, MySql, Oracle, Oracle Forms Development, Windows, Linux.



Click here to view other articles.


Mail Me at:  shridhar_tl@ymail.com


Visit my Site:  www.iCodeIt.in


Comments and Discussions

 
QuestionHow can I send file size from FTP server Pin
htk10-Aug-15 1:25
htk10-Aug-15 1:25 
QuestionGetExactPath Pin
Member 398617216-Jun-15 6:36
Member 398617216-Jun-15 6:36 
QuestionGetExactPath Pin
Member 398617216-Jun-15 6:27
Member 398617216-Jun-15 6:27 
QuestionIPMon article missing Pin
Wayne Linner27-Apr-15 15:37
Wayne Linner27-Apr-15 15:37 
QuestionWhat is going on in ftpclient.cs GetExactPath? Pin
Member 9390925-Feb-15 8:48
Member 9390925-Feb-15 8:48 
AnswerRe: What is going on in ftpclient.cs GetExactPath? Pin
Member 9390925-Feb-15 17:29
Member 9390925-Feb-15 17:29 
GeneralRe: What is going on in ftpclient.cs GetExactPath? Pin
Member 1150563912-Mar-15 3:08
Member 1150563912-Mar-15 3:08 
QuestionA nice piece of work Pin
jinnon9-Aug-14 22:41
jinnon9-Aug-14 22:41 
QuestionWorking directory Pin
Phạm Mạnh Hưng27-Apr-14 16:40
Phạm Mạnh Hưng27-Apr-14 16:40 
GeneralMy vote of 5 Pin
Elizalde G. Baguinon2-May-13 21:58
Elizalde G. Baguinon2-May-13 21:58 
GeneralMy vote of 5 Pin
dl4gbe29-Nov-12 2:20
dl4gbe29-Nov-12 2:20 
nice Smile | :)
GeneralMy vote of 5 Pin
Bjerke3-Nov-12 12:01
Bjerke3-Nov-12 12:01 
BugProblem! Pin
Petoj873-Aug-12 23:46
Petoj873-Aug-12 23:46 
GeneralMy vote of 5 Pin
yesliu10-May-12 22:24
yesliu10-May-12 22:24 
GeneralMy vote of 5 Pin
Alberto Armando29-Sep-11 20:46
Alberto Armando29-Sep-11 20:46 
QuestionAccessing to server from Mozilla Firefox Pin
juanico1713-Sep-11 14:37
juanico1713-Sep-11 14:37 
GeneralI Have extended and reformatted the server side of these excellent classes Pin
Smurf IV (Simon Coghlan)4-May-11 9:03
Smurf IV (Simon Coghlan)4-May-11 9:03 
GeneralThanks! You just saved me a few days of hard work. Pin
Member 405730031-Mar-11 15:13
Member 405730031-Mar-11 15:13 
QuestionNeed your great help!! Pin
ITHelperServerStudio28-Dec-10 8:38
ITHelperServerStudio28-Dec-10 8:38 
GeneralYou are perfect person [modified] Pin
geniusce14-Sep-10 1:41
geniusce14-Sep-10 1:41 
Generalabout of permission Pin
makaryos2-Apr-10 4:17
makaryos2-Apr-10 4:17 
GeneralMy vote of 1 Pin
CARPETBURNER21-Oct-09 23:15
CARPETBURNER21-Oct-09 23:15 
GeneralRe: My vote of 1 PinPopular
SHRIDHAR TL24-Oct-09 1:54
SHRIDHAR TL24-Oct-09 1:54 
GeneralRe: My vote of 1 PinPopular
jgauffin26-Oct-09 20:53
jgauffin26-Oct-09 20:53 
GeneralRe: My vote of 1 Pin
Member 9390924-Feb-15 19:03
Member 9390924-Feb-15 19:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.