Category Archives: Open source

Allowing digest auth with CORS on Chrome and LIGHTTPD

LigHTTPD is a very popular web server to use nowadays and it powers a lot of devices. I’ve mainly seen it in rasberry pi’s and other smaller servers but it’s powerful nonetheless.

Today I did some fiddling around with CORS to get data passed through another domain than the site was residing on. It all seemed fairly easy as it’s just a simple header to add to your server response, as described on enable cors

Turned out it wasn’t that easy. The server with the APIs are armed with Digest authentication for authorization and chrome has made it a must to send HTTP OPTIONS to check some parameters from the server.

Chrome needs in order to authenticate:

  • Access-Control-Allow-Origin: *
  • Access-Control-Expose-Headers: WWW-Authenticate
  • Access-Control-Allow-Headers: Authorization
  • Access-Control-Allow-Methods: POST, GET, OPTIONS

That’s easy enough with LigHTTPDs extra environment header in the server configuration:

setenv.add-response-header = (
"Access-Control-Allow-Origin" => "*",
"Access-Control-Expose-Headers" => "WWW-Authenticate",
"Access-Control-Allow-Headers" => "Authorization",
"Access-Control-Allow-Methods" => "POST, GET, OPTIONS"

)

But, that isn’t enough. If you have set up a rule in your server configuration that you require to authenticate, Chrome will fail when it gets 401 on it’s OPTION request and will not continue. In order to get that worked around I had to do some modifications in mod_auth to make sure it doesn’t authenticate on HTTP OPTIONS. It was an easy hack to implement once you figured out the functions and where to look.

All the needed code was:

//Hack to make OPTIONS pass through AUTH filter with just sending the headers needed by CORS on Chrome
const char *htm = get_http_method_name(con->request.http_method);
if(strcmp(htm,"OPTIONS") == 0){
con->http_status = 200;
con->mode = DIRECT;
return HANDLER_FINISHED;
}

I’ve attached the modified .c file for some convenience. I used the latest (to date) official release 1.4.39. Hope this helps anyone out there who needs to use digest over CORS with LigHTTPD.

mod_auth

/Marcus

Running Python on Axis devices

Axis’ platform for running applications on the camera itself is really great. For you who haven’t seen ACAP share I really suggest you click yourself over there to see what it’s all about.

As mentioned in other posts, Axis have created an embedded platform where you can install applications on the different devices to add features to the edge. This can be analytics that is looking for something unique like a people counter or advanced cross line-type detection. It can also be for simpler tasks like sending a message using a service platform similar to the pushbullet integration that I published here some time ago.

The only downside is that to develop these applications you have to know C which can be tough for the average script kiddie who just wants to perform something small. So, in order to solve some of the more simple scripting that you can be interested in doing. On ACAP Share there’s a PHP application that allows someone to write a PHP script that can be executed on a camera. This is a great application if you’re looking to use smaller APIs for your task.

PHP is extremely popular and allows you to do a number of great things. Python is another extremely popular language. Soo.. Enough said, below is the compiled ACAP and how to build it if you have the environment.

Build script:

buildpython

The python binary itself:

Python_2.7.1_1_0_mipsisa32r2el

Recommended FW: 5.80+. The reason is that the package is pretty big and will need some space. We’ve seen problems with 5.60 and 5.70 firmware that doesn’t allow the package to go through due to it’s size. After 5.80 it seems fine though. (Yes, python is a big library)

It’s build to run on the MIPS architecture, meaning it will work on ARTPEC-4 and ARTPEC-5 devices but that’s it. Python version is 2.7.1 with some limitation built to it (see buildpython.sh)

When you upload the binary there’s an attached python script in that folder. Edit that and make sure you run a continuous loop on it to perform whatever you want to achieve.

ACAP is actually so powerful that you can create your own applications that are essentially script that runs off another application. To start a python script from the command line, use:

LD_LIBRARY_PATH=/usr/local/packages/python_2_7_1/lib /usr/local/packages/python_2_7_1/bin/python /path/to/script.py

This can be called from system(); as well in your C application.

Hopefully this can save some time for people who’re interested in developing in python on Axis devices.

/Marcus

Axis PTZ and Swiping

I got a funny request today on how to control Axis PTZ on a webpage that was running on a tablet. And by controlling the PTZ i mean by the gestures that are supported in many different tablets and smartphones. The Swipe!

It’s fairly easy to control the Axis PTZ devices since they all have a unified API. From Axis website: http://www.axis.com/files/manuals/vapix_ptz_52933_en_1307.pdf

 

I’m also a big fan of jQuery have used that a lot while I’m doing anything related to web development. It’s so easy to use and has a ton of plugins. A quick search on google today gave me TouchSwipe, a jQuery plugin for tablets. Awesome, this shouldn’t be any issue to hack together.

Requirements

  1. The webpage / webapp does not run directly on the camera so both MJPEG streaming and PTZ controlling must be done from within the app.
  2. It cannot run on any plugins but what Javascript and HTML5 allows.

Alright, lets start looking into the streaming part. Streaming MJPEG is super easy with the Axis devices on a web page all that is needed is:

<img src="http://username:password@axis-device/axis-cgi/mjpeg/video.cgi?resolution=640x480" alt="" />

Note: This will not work on IE, but who cares? We want this on tablets.

The next part is a little trickier. Nothing else but HTML and Javascript. Since the app is not running on the same webpage Cross domain scripting is not allowed. So how do we translate our swipe movement into a HTTP command that can be sent to the Axis device? The answer is an ugly but lovely hack called the <iframe>. Iframes can load whatever content you like from whatever source  you like. Without even asking why.  Why is this so good? Because the PTZ api is all HTTP GET. Boom! Easy enough.

Ok, we have all our pieces for our solution. MJPEG will work with our browser, SwipeTouch will trigger when a user Swipes and the PTZ can be controlled from an iframe.

SwipeTouch is super easy to implement, just take a look at this piece

 $("html").swipe( {
 //Generic swipe handler for all directions
 swipe:function(event, direction, distance, duration, fingerCount, fingerData) {
 
 alert('Swiped '+direction)
 },
 //Default is 75px, set to 0 for demo so any distance triggers swipe
 threshold:75
 });
 });

So, here we are listening on swipe events on all of the webpage, we could do it on the image tag as well if we wanted but lets keep it on the HTML for now. When the swipe event is triggered we get the direction, distance, finger count and finger data. The direction is particulary interesting, it comes in up,down,left,right. It’s super convenient since the PTZ API also allows commands like ?move=[left,right,up,down].

This means that we can translate this super easy, have a look:

$('#swipeFrame').attr('src', 'http://root:pass@[ip]/axis-cgi/com/ptz.cgi?move='+direction);

Put that between in the swipe event and create an iframe what has the id swipeFrame and you’ll have Axis PTZ control that moves 25% of its field of view on each swipe. It’s not enterprice vms integration but certainly good enough for some basic checks from your Tablet!

This blog post requires some basic HTML knowledge to understand what’s going on as I didnt tell how to include the JS files or how to set up the actual html frame. If I was too messy in my write up, have a look at the source code 🙂

Server Report Grabber

Axis Communications offer a great SDK for all their application development partners that allows anyone to create applications that runs on the cameras.

The SDK itself comes with a set of libraries and compilers for the different chipsets in the Axis devices that let you:

  • Capture frames from the devices
  • Control device parameters
  • Create web pages/CGIs
  • Create event triggers
  • License key management (if you considered commercial use)

Axis also created the RAPP (Rasterizing Processing Primitives library) that allow the developer to do low level processing image operations. It’s licensed under GPL v3.

All Axis devices runs Linux as the operating system, so it’s really up to you what you want to do in the operating system and there’s really no limit what you can do. The main language to develop the applications in is C but C++ is supported as well.

I had a task where i had to monitor changes that was being made on the camera over time and Axis already have a great feature to get information about the whole system that can be grabbed from their APIs, this is called the “Server report”. So what I came up with is an application that grabs a Server report of the camera and saves it to the preferred location. The default is the SD card slot in the camera but any directory or even mounted network devices can be used. It’s not a fancy app that does all this kinds of analytics. But it’s working as expected.

Download

Axis Parameter Class for Python

A couple of days ago I was in need of making some scripts that removed duplicate parameter groups on a couple of Axis devices. For those of you who don’t know, Axis creates Digital Surveillance Cameras with IP technology, which in practice means that It’s surveillance cameras running Linux.

All Axis devices offers an open API for developers who want to develop  that uses Axis devices. The most frequent API used is the Parameter API, that allows to change basic settings, create motion detection windows, events with triggers and so on. And this is the API that I had to implement a class for.

The class is written in Python and is pretty much an object oriented interface. Example

param1 = AxisParameter("Motion.M0.Left","1120")
#param1.name = Motion.M0.Left, param1.value = "1120"
param2 = AxisParameter("Properties.Resolution")
#param2.name = Properties.Resolution, param2.value = None

Here we have two parameters that can be accessed from the class. param1 contains a parameter with an assigned value to it, this can be used to modify an already existing parameter. param2 contains a parameter name, but have no idea of the value of it, better used to receive the parameters

To use the class, you must first import the data from it.

from AxisParameterClass import * #import both the working class and the information class

paramclass = AxisParameterClass("some.ip","username","password","vapixversion")

All arguments are optional and can be set later with the following functions

  • setIP
  • setCredentials
  • setVapixVersion

Axis currently have two API’s for handling vapix parameters. The only big difference is the URL, but still. The class will automatically try to determine the vapix version if not set before.

Receive parameters

To get parameters from a device, simply create a parameter that you’re interesting, both groups and subgroups are fine to use:

paramclass.getParameters([AxisParameter('Motion'), AxisParameter('Properties'), AxisParameter('NonExistent')])

The class will return a list of all parameters that matches the search. Is a parameter not found, the Parameter will be returned with the value None. All parameters that are found have their value as a string.

Remove parameters

Just as add. Create your list, and send to

paramclass.removeParameters([AxisParameter('Motion')])

Parameters returned have a status parameter that refers if the parameter was removed or not:

AxisParameter.status = "OK" #removed
AxisParameter.status = "Error" #not removed.

Update Parameters (Implementation still needs some work)

To update, pass the list into

paramclass.updateParameters([AxisParameter('Motion.M0.Left','1123')] )

If all works, the parameters returned will contain the same result as what you passed in.

 

Download

It can all be downloaded from github, licensed under MIT, so feel free to do whatever you like with it 🙂
https://github.com/marcusfollrud/AxisParameterPython 

dopingkollen.se

Hej Vänner!

För några dagar sedan hade jag och några vänner en diskussion kring kosttillskott och “misstagsdoping”. Finns misstagsdoping egentligen? Behöver man kosttillskott? Frågorna var många och vi hade väl ungefär lika många svar. Men samtidigt som jag var mitt uppe i diskussionen pågick där parallellt en härlig problemlösning i bakhuvudet som bara kommer fram vid programmeringstillfälle. Hur skulle det vara ifall man hade en databas som hade koll på vilka produkter som var stämplade som dopingklassade?Med innehållsförteckning, länkar mot nyheter osv osv?

Av att döma utav Riksidrottsförbundets lista på medel som inte är tillåtna blir man lätt blind, och t.om ger upp. Där finns helt enkelt inget bra sätt att se ifall ett kosttillskott är dopingklassat eller inte.

Samma dag som diskussionen satte igång var jag fullt igång med att göra något av det. Tekniskt intresserad som jag är lade jag nog mest tyngd på vilka komponenter jag skulle använda. Valen blev följande:

DooPHP blev valt av den anledningen att det är väldigt komplett, men ändå förvånansvärt snabbt och enkelt att använda.
jQuery för enkelheten med JavaScript och dynamiska webbsidor.
mySQL var lika enkelt att välja, jag behöver bara en enkel tabell med korsreferenser. sqlite hade säkert funkat, men DooPHP har grymt stöd för CRUD och tabellrelationer, som inte verkade vara helt stött med sqlite.

Så, jag satte mig ner och började skrida till verket. Efter några timmar har jag en sida uppe som ligger på http://dopingkollen.se.

Dopingkollen är en kollaborativ sida där alla kan söka, ändra och lägga till produkter. Det som behövs är produktens titel samt dess innehållsförteckning. När produkten finns i databasen jämförs den direkt med RF’s lista och ger information ifall produkten gick igenom eller inte.

Enkelt och smidigt, men designen behöver jobbas på, och det gör jag :).

Förutom sidan i sigsjälv är där ett API, se http://dopingkollen.se/api, gjort i REST + JSON, för enkel och bandbreddssparande implementering.

Så, nästa steg är att gå igång sidan i sin enklaste form, vara nöjd med designen. Därefter blir det ett nytt spännande projekt som jag tänkte låta vara Open Source, en Android app som implementerar zxing, där man kan använda sin mobilkamera för att skanna en EAN-kod och därefter får resultatet direkt i luren när man står i affären och ska bestämma sig för vilket kosttillskott man funderar på att köpa.

 

 

Goals of 2011

Haha, just read my old post regarding the goals I had set in 2010. Did any of them come true? – No.

This is how it went:

#1 MonoTouch app
I did start an iPhone application with MonoTouch, but the API for the application is was developing on never got completed so it just died. Would’ve been a great app that’s for sure

#2 Payson drupal module.
Also got started, even got quite far actually, but my leaving of the drupal site I was part of (linuxportalen.se) resulted in a non-complete code. Besides, I have a shallow memory that there were some problems with drupal when it came to directly hook into POST parameters, which were needed by the Payson API. When i think about it I should’ve just made a hacky php that connected to the database with the proper result, but if I’m not mistaken mind was set to do it “the proper” way, which resulted in a no go.

#3 Community site based on Django
Ah, i really loved python back in early 2010, and I still do, but when I did some research I found tornado server of better use, as I started to more and more like to writer proper API’s, instead of views with hooks of code.

I stopped with the Django project after some time, got back to PHP, created a small framework and started developing the new community site, which also kind of died along with my interest in the Swedish Linux community (yeah, it was meant to replace linuxportalen) whom were too much interested of internal conflicts plus distro war.

But, through the year I looked up some different Web frameworks for PHP, and I found DooPHP, a wonderful and fast framework which is easy to learn and use. There’s a small project going on there, without a release date though, just something I do when I’m bored.

So to sum it up, none of my plans happened with the spare time I had part from my ordinary job, but it’s ok, none of them were too important, even though it looked like it when I think back about it.

 

Goals of 2011
Ha ha, right, I won’t set up any goals this year. I spend far too little time in front of the computer when I’m not working and the truth is, there are other things in life far more important to cherish and enjoy besides techie stuff. But hey, do I find something not too big and fun, I’ll probably do it :-).

Take care,
Marcus

Adding bit.ly support to mu-feeder

My twitterfeed haven’t been working for  a while now. The last update was 2009-11-25, so I went out googling on “twitterfeed replacement” and found mu-feeder. A small python script that publish your blog entrys (or whatever RSS feed that you might have) to twitter. It’s very sweet but i missed the bit.ly support.

But, since it’s open source, there’s nothing that you can’t do about it :). I went out hunting and i found python-bitly. A library for the bit.ly API!

After some tweaking in the bitly file I got it working.

Step 1: Download mu-feeder and unpack it.
Step 2: Download the modified bitly file and put it in mu-feeder/shorteners (diff here)
Step 3:  Edit your mu-feeder settings.py. set URL_SHORTENING_SERVICE to “bitly” and URL_SHORTENER_LOGIN plus URL_SHORTENER_PASSWORD to your bit.ly username and the API key that you have on bit.ly.

That’s it. Now it should work just fine :).

Have a nice weekend.
Marcus

Debian in Virtualbox

In the Swedish linux community, it’s quite common to experience linux by:

  1. Install ubuntu/opensuse/mint
  2. Fix the lack of mp3/mkv/h264/xvid blabla support
  3. Install virtualbox
  4. Test distributions
  5. Brag about the ones you installed (especially Debian)

It’s always good to try out different flavors, till you find the one the match your needs and requirements. It is also always good for people that’s creating the different distributions as it often generates feedback. If not directly towards the developers, then at least a comment about it on the web will be made in most cases. This means indirect feedback plus the growth of Linux overall.

When it comes to myself, I’ve never been much of a distribution tester. Sure, I’ve installed quite many over my 6 years running Linux. Such as Ubuntu, OpenSuSE, Fedora, Slackware, Arch, Crux and so on. But I have always tried them out fairly, in the meaning that none of them have been in a Virtualbox just to test the installation and the mp3 support in the chosen distribution. To me, the most importance thing is how productive I am.

Anyways, what I was going to say is that I made my first Virtualbox+Debian installation today (yay, my time to shine!!). To me Debian has always seemed to be this quite advanced installation to get it up and running, according to what Swedish users express on the web. . But today, I realized that it’s just as simple as installing it’s buddy Ubuntu. Why do people brag about this?

The true reason for why I chosed Debian, and more important installing via Virtualbox is because of the recent Appartment change me and my darling made I’ve only set up the MacMini as my working station. The PC is now standing doing nothing, and I’m too lazy to insert all the cables, hehe :). Also, it’s the recommended distribution by the company that I work for. Therefor it felt naturally to install Debian!

This entry was never to start a Flame war between distributions, they are all good. It all just depends on your needs.

And if any Virtualbox distribution tester feels pointed out or accused inappropriately, then I apologize. I never meant to sound negative, rude or wiseacre.

Best,
Marcus