Client controller – Custom binaries

I found some time this afternoon to work on the Client Controller. My aim was to allow the uploading of custom dll files for use in the code seen on the page.

There are a few steps involved here. Firstly, some HTML on the main controller page for file uploading. A little tweak to the form tag was required, that was a small headache:

<form method="post" action="index.php" enctype="multipart/form-data">

A nice simple input element for the file, only 1 at a time at the moment!

<input type="file" name="custom_dll" />

Now we need some PHP to handle this on the server. Nice and simple really, I’m just saving the file to a folder called ‘custom_dll’. If this were to be used by many people the next obvious thing would be some more validation!

if(isset($_FILES['custom_dll']))
{
	$uploaddir = 'custom_dll/';
	$uploadfile = $uploaddir . basename($_FILES['custom_dll']['name']);
	
	if (move_uploaded_file($_FILES['custom_dll']['tmp_name'], $uploadfile)) {
		$uploaded_files = $uploaded_files. "<strong>". $_FILES['custom_dll']['name']. "</strong>";
	} else {
		echo "<strong>There was an error uploading the file.</strong>";
	}
}

Now that’s done, I need to actually download it via the console app itself. Before the app can download any files, it needs to  know what files to download. I created a simple PHP page for this with some delimiters on the file names:

$custom_dlls_location = "custom_dll";
$site_root_url = "http://resource.greynet.co.uk/clientController/custom_dll/";

$files = scandir($custom_dlls_location. '/');

foreach($files as $file) {
	if($file != ".." && $file != ".")
	{
		echo $site_root_url. $file. "|";
	}
}

This returns a nice string for us, if we check the source of the page’s URL in a browser, we get this:

http://resource.greynet.co.uk/clientController/custom_dll/Badcustoms.dll|http://resource.greynet.co.uk/clientController/custom_dll/RCWebservice.dll|http://resource.greynet.co.uk/clientController/custom_dll/System.dll|

As you can see, I’m using the pipe (|) symbol to separate my files. This is simply because it’s an easy symbol that you can’t use in a file name in Windows. Now we have a reference it’s time to download the files in the console.

I’ve written all of this in to a nice and simple method:

public static void GetCustomDlls()
{
	string downloadServiceURL = string.Format("{0}/return-file-urls.php", serverCommandURL);
	WebClient wc = new WebClient();

	//download the list of files we need to get as a string
	string filesString = wc.DownloadString(downloadServiceURL);

	string[] filesToGet = filesString.Split('|');

	Console.WriteLine("Custom references retrieved, downloading binaries...");
	//loop through the array and download each file
	foreach (string file in filesToGet)
	{
		if (file != "")
		{
			//split the string in to our url + our file name
			string[] filedata = file.Split('/');
			string fileName = filedata[filedata.Length - 1];

			//Adding our special file names to the assemblies array
			assembliesList.Add(fileName);

			wc.DownloadFile(file, fileName);
		}
	}
}

You can see the idea behind this is quite simple. It didn’t take a huge amount of effort to get each custom dll downloaded ready for use. I’m using the CSharpCodeProvider to compile the downloaded code. Before everything get’s compiled and run we need to add our references:

CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();

foreach (string asmbly in assemblies)
{
	parameters.ReferencedAssemblies.Add(asmbly);
}

That was just about it if we’re sticking to the interesting bits. The whole thing is quite simple really. That’s the idea at the end of the day, a simple, dynamic tool one can use for updating applications or anything else they see it fit for.

Client Controller – The client

So, tonight I managed to find the time to get the client working and deployed. At the moment it’s probably not of great use to anyone as it’s compiled source code, so you won’t be able to use it in your own project.

I’m not sure whether to just publish a zip of the source code & project for all to play with, or make it more of an API based thing. I’m thinking the latter.

Anyway, with no further delay, you can install the console here:

http://www.greynet.co.uk/deploy/clientcontroller/publish.htm

Once installed, on run-time, the console will download, compile and invoke the initial method set out on the controller’s web page. That bit can be found and edited here:

http://greynet.co.uk/clientController/

One thing you might find interesting on there is the ‘assemblies’ reference. If you’re using something like this:

System.Diagnostics.Process.Start(run);

Then you need to reference “system.dll” on the controller page. Depending on how this goes I may add the facility to go a little further with referencing, you might want to bring in a third party dll and then work with it. So perhaps the ability to upload it ready for download & compilation. It would take some thinking at the client end but I think it could make things a bit more powerful.

Dynamic Client Controller

Strange name you say? Well, this is something I came up with a while ago now. We’ve all worked with software that updates itself and does various things with your computer. A lot of us have used Windows, a lot of us have been subject to a Windows update breaking our whole SDK on our PC.

So I thought, rather than build an app and put stuff in for runtime to check for updated files, why not be a tiny bit more dynamic. The idea is that your C# .Net based application connects up to a web URL and downloads what is essentially a string of text. That then gets parsed, along with an instance and starting method. Once we’ve got all of that and we’re happy with it, the code can be run.

So in theory, this could be anything from:

Console.Writeline("Your software is out of date, please download new version at greynet.co.uk");

To some crazy stuff that starts downloading files and ripping apart the software. There’s obviously some security things we need to consider but as a simple idea I think it’s worth working on.

I’ve got a console version running on my local set up which I’ll be deploying once I make the download URLs in to settings. I’ve only really been testing with this code at the moment:

namespace RCCSystem 
{ 
    public class BeginWork
    {
        public void start()
        {
			string messageToShow = "Test message";
          	System.Console.WriteLine(messageToShow);
        }
    } 
}

When I get the time I’ll do some testing with some slightly more ambiguous code!

For now, you can see the page that controls the downloaded code here: http://greynet.co.uk/clientcontroller/