Thursday, October 29, 2015

How to permanently disable Windows Defender

By default in Windows 10, Windows defender will turn back on again after being disabled after some time. However, if you are doing something that requires not having a AV (like creating a honeypot VM). You might find this handy.

Run the command gpedit.msc
This will bring up local group policy handling.
Navigate to:
Administrative templates - Windows components - Windows defender - Turn off Windows defender

Hit 'enable', and Windows defender should be turned off indefinently.


Friday, September 25, 2015

Counting all nodes and items in a tree structure (JS)

It's been a long time since last post, and altho this is rather mundane, here is an update:

Case: Im recieving data from the server as a gigantic object tree structure (like shown below). The data is displayed on a webpage in a suitable AngularJS directive. However, when the number of objects are more than 100 the view has to be set to only show a summary. So, how do you count a tree?
Solution:

var CountAllNodesInTree = function (treeObject) {
    this.counter = 0;
    self.countTree = function (treeObj) {
        self.counter = 0;
        self.countLeaves(treeObj);
        return self.counter;
    }

    self.countLeaves = function (treeObj) {
        if (treeObj) {
            self.counter++; // count department
            if (treeObj.Items) {
                self.counter += treeObj.Items.length; 
            }
            if (treeObj.Children) {
                if (treeObj.Children[0]) {
                    for (var i = 0; i < treeObj.Children.length; i++) {
                        if (treeObj.Children[i]) 
                            self.countLeaves(treeObj.Children[i]);
                    }
                }
            }
        }
    }
    return self.countTree(treeObject);
}

Monday, July 20, 2015

Multiple lines of code in chrome console

There are times when you need to run some code on your page to see how some changes would look. Or maybe just mess about on a friends computer. Nevertheless you might not be able to do all your console operations with just one line at a time.

So how do you run multiple lines of code in the Chrome debugger console?
Turns out you can just create a function:
And once you have declared your function, you run it.

Saturday, July 18, 2015

Watch that UTF8 when serving JSON

Im currently messing about with Node.JS after deploying a droplet on DigitalOcean.  Heard alot of people talking about DigitalOcean before, but never tried it untill now. That server was so incredibly simple to set up, i'm speechless.


I will post a guide on how to quickly get multiple node sessions running in a bit, but for now i will just mention this little detail i ran across. I had a bit of a head-scratcher when writing my first Node service for serving up some Json data. It worked nice and dandy, but coming from Norway my Json data contains alot of special characters, like Æ, Ø and Å.

These characters went missing when my file was being served. Turns out it was just bad encoding. Setting the encoding to UTF8 solved the problem. So check that!

Friday, July 17, 2015

MSMQ - Bare minimum example

Sometimes you need a project to be split into two programs but still pass data between eachother. A normal way of doing this would be with a webserver, like a WCF service.
There is however a simpler way of achieving this functionality, using a windows preinstalled component called MSMQ.

MSMQ is a message queue system. An application can push messages to a queue and another application can read them out. MSMQ also has the advantage of keeping queue messages even if a client goes offline. The messages can then be retrieved the next time the client comes online.

The code:
This first code snippet initializes the queue:
public static MessageQueue mq;
private void btnInitQueue_Click(object sender, EventArgs e)
{
 // Create queue or connect to an existing queue
 if (MessageQueue.Exists(@".\Private$\MyQueue"))
 {
  mq = new MessageQueue(@".\Private$\MyQueue");
 }
 else
 {
  mq = MessageQueue.Create(@".\Private$\MyQueue");
 }
}

This next snippet adds a string message to the queue:
private void btnSendMessage_Click(object sender, EventArgs e)
{
 // This adds a message to the queue
 System.Messaging.Message mm = new System.Messaging.Message();
 mm.Body = textBox1.Text;
 mq.Send(mm);
}

And this last snippet is used to read out the messages from the queue:
private void btnReadOutMessages_Click(object sender, EventArgs e)
{
 // This retrieves all messages in the queue, processes them and deletes them 
 System.Messaging.Message[] messages = mq.GetAllMessages();
 foreach (System.Messaging.Message m in messages)
 {
  // Need to read the messages with a formatter
  m.Formatter = new XmlMessageFormatter(new String[] { "System.String,mscorlib" });
  richTextBox1.AppendText(m.Body.ToString());
 }
 mq.Purge();
}

Notice we need to apply a formatter when reading data out from the queue.

Wednesday, June 17, 2015

Learning JavaScript the hard way: 1 - dont minify in beta

Being a seasoned vet with C# i find the world of javascript rather sensible, although there are a few pitfalls. One difference is the use of minified files. Since javascript is parsed, rather than compiled there is a great push to make the source as small as possible, thus allowing for faster loading sites.

One way of achieving this would be to write javascript the same way C++ devs write their code; with truncated and unreadable variable names, and without any comments except the ever so loved "\\change this".

The good news is that javascript devs are not C++ autists, and the prefered solution is a fair bit more sane.

The common practice is called minifying. And it consists of parsing the source files thru a program that changes out every var with a short-as-possible name, and removes all unneded whitespace and comments. This makes the code unreadable for sane humans, but saves space.

 Below is an example of code minified using http://jscompress.com/

This is a normal un-minified javascript function (388 bytes)
This is the same function minified (188 bytes)
But back to the problems: Being new to javascript i just downloaded the minified versions of all my libs (angularJS, jQuery etc.) and this lead to problems during debugging, as all the error messages were less than readable.

Switching out the minified files with the large uncompressed ones made things alot easier.

Friday, June 12, 2015

WCF on IIS: configure permissions

I modified my WCF service to get/set data from a SQL database, in order to make it a usefull service. And after adding the functions i needed i debugged it and everything looked fine. But then upon uploading it to my IIS server i was unable to get access.

The problem was inadequate permissions, and so i started down a long road of error checking.

First off all i checked these two off my list:

  • Newcomer advice 1: Install WCF support for IIS
  • Newcomer advice 2: Add the service as an application in IIS Manager


Having done those the first step i had to do was changing the permissions to use the ApplicationPool:
After doing this i was allowed to view my site, or at least the non descriptive error messages it gave me.


The second step i did was change my web.config a little, so i could get descriptive error messages:

Now I could see there was something wrong with my SQL permissions, as IIS APPPOOL\ASP.NET v4.5 was denied access. So next step was to open SQL Management Studio and add this name as a user:


After adding the user, and setting my table 'arbeidslog' as the default for the user i added the permission 'db_owner' on the User Mapping dialog. (Security -> Logins -> IIS APPPOOL\ASP.NET v4.5)

And that was it, Service is now spitting JSON from my database just like it's supposed to.