discord.jse.io" itemprop="description"/>

Jonah's Blog

Jonah's blog is here

Web/Technology

Web development and Technology related topics.

So mempler (join our discord server if you don't know him!) is working on a currently undisclosed project on one of my servers and wanted to do some rewriting to a php file.

I never have done much with rewrites ever with anything. I never really cared about ugly URLs anyway with anything I ran in PHP so I just didn't care. On the occasion I needed to run something that did, someone always provided it to me in a .htaccess with it, but I want to use lighttpd. It's just more efficient for what we want. Fight me.

Anyway he wanted something like example.com/path/here to rewrite to example.com/index.php?path=WhateverCameAfterSlashHere as long as no ? appears in the URL (more on that later).

So that is pretty easy. Just do something like "([^?]*)" => "index.php?path=$1"

So what does this do? Well. I used this place and the lighttpd documentation to figure this out. Basically, a * will match anything. Quoting autohotkey.com:

An asterisk matches zero or more of the preceding character, class, or subpattern. For example, a* matches ab and aaab. It also matches at the very beginning of any string that contains no "a" at all.

Anything in the parentheses are a regex group for lighttpd, so anything it matches is provided to $1. Quoting the lighttpd documentation:

If the matched regex contains groups in parentheses, $1..$9 in the replacement refer to the captured text in the matching group "$1" meaning the first group, "$2" the second, and so on.

More on that later when we want to have a second group.

I also wanted it to stop at a ?, which eventually will be used to look like you're really accessing an index.php on the path in the URL and you're going to specify the first parameter that of course is passed to $_GET, even though the path is one too but unsuspecting people won't know that.

With the [^?] before the * we're telling regex to match everything EXCEPT for a ?.

Again, to quote autohotkey.com:

Matches any single character that is not in the class. For example, [^/]* matches zero or more occurrences of any character that is not a forward-slash, such as http://. Similarly, [^0-9xyz] matches any single character that isn't a digit and isn't the letter x, y, or z.

Now we also want to handle other parameters. This is where our second group comes in. Of course, we already included the ? for the path parameter in the rewrite rule where $1 is passed. When people supply a ? it should actually be after an & and the ? shouldn't actually be passed to the requested php file. I did this:

url.rewrite = (
                "([^?|&]*)\?(.*)" => "index.php?path=$1&$2"
        )

As you see, since the ? in regex actually means something (A question mark matches zero or one of the preceding character[...]) I need to escape it with a backslash which isn't a foreign concept to anyone who has dabbled in scripting and programming even a little bit. So this means expect an actual ? character in the URL and then I made a second group that will match *literally *everything with the .*. That's fine though, he can handle it in the PHP code for whatever he wants to do. This means that any other parameters in the URL will be followed with an actual & character in the URL and not something else of course, but that's fine, nothing for lighttpd to handle.

You'll also notice that in my first group where I told it to not match a ? that I specified a | (which means OR, also not a foreign concept for most) followed by an & symbol, so people can't specify additional parameters after an & sign before the ?.

This means we're now resulted with: url.rewrite = ( "([^?|&]*)\?(.*)" => "index.php?path=$1&$2" )

Now the problem is that sometimes he doesn't want to specify other parameters after the ? in the URL, but if you try the rewrite example I provided you'll notice if no ? character is specified in the URL you get a 404 thrown at you as the rule has no match.

I initially then decided to do

url.rewrite = (
                "([^?|&]*)" => "index.php?path=$1"
                "([^?|&]*)\?(.*)" => "index.php?path=$1&$2"
  )

Now as you might expect if you're not retarded like me that won't work. Now parameters are never provided to the PHP file when they are in the URL because the first rule I specified always applies (and of course, doesn't match anything with a? character or an & character)!

The fix is simple. Just reverse them like so:

url.rewrite = (
                "([^?|&]*)\?(.*)" => "index.php?path=$1&$2",
                "([^?|&]*)" => "index.php?path=$1"
)

This way if a ? exists in the URL then of course, the first rewrite rule applies since it's the first that appears. Otherwise, the second will.

That's for going along with me on my regex learning journey. I know this is probably simple for most people but took me a while to figure out as I've never done it before (aside from fixing my htmly rewrites for lighttpd a while ago which doesn't count!)

Moving

- Posted in Web/Technology by with comments

So the server this blog is hosted on is bad.

I mean, I love the thought of a low end box and never have I pushed it to it's fullest potential, but I frequently get emails from statuscake telling me it's down, yet again. It seems to be network, the vm doesn't actually go down, but whatever. I'll say more about that another time.

Anyway, I am moving this site to a Kimsufi dedicated server in time. It's an i3-2130 for $12.99/mo on flash sale. Not an amazing "server" but the price is nice.

I'll still keep this LEB, it will probably host a mirror for stuff and backups, but nothing I will really care much about beyond that.

Perhaps I'll write a more in depth review about it all later.

Anyway since I scooped up a couple of these I'm going to move everything and mirror the data across them, along with publishing some downloads to the BuyVM anycast "CDN" I have so there will be a lot of redundancy when I'm done for everything.

UPDATE: 3:30AM on the 20th and I'm going to bed. https://jseb.ca is moved though (which probably doesn't mean much as it's mostly a screenshot server I use with sharex aside from mirroring some other files, but it had a lot of files (including the recovery bins, some images on this blog and one of the drivers for blackweb), and I'm slow as I talked a lot on discord in between, but it's moved.

Tomorrow is a busy day but perhaps in the evening I'll move some other stuff then actually get onto moving this website. Meanwhile mempler.de has been working on something with all that storage I've gotten my hands onto ;) Hopefully I can also write more about that as time goes on and get more involved into it (and learn more about node.js!!)

Oh yeah.... it's also an opportunity for me to move 100% of my stuff to HTTPS with HSTS. So there's that. Say goodbye to port 80.

Update 2 Nov 21 also early in the morning:

Moved jsebean.com (this site over). Should have been seamless since I'm not hosting any dynamic content here. Just a quick DNS change.

UPDATE August 10th, 2017 Much thanks to Jared Trotter for submitting the Blackweb BWA17 drivers. Download links updated below.

UPDATE June 11, 2017

This should help clarify things for people who have issues with this driver for their mouse and perhaps could help solve future issues for people down the road if they want to contribute to hosting their mouse driver.

While I initially thought there was only one model of a Blackweb gaming mouse it's been brought to my attention though comments (here and on youtube), discord, and emails that people can't get this to work. Apparently Walmart has several models (figures) of this mouse and some (if not all of them lol) use a different drive. Additionally, turns out the particular gaming mouse my brother got was one like this, not the older BWG66 model, so I cannot say if the BWG66 will work or not with this driver.

Anyhow if you have a different model and this driver does not work, but you have the ability to upload the particular driver for your model from the CD you got, along with a link or description (or a walmart store link) of the particular model you have please email it to me at [email protected] and we can talk about hosting it here for others with a similar model (if you're trustworthy of course ;) ) Screenshot of the walmart store link I provided above if they decide to remove it down the road: https://jseb.ca/blackwebmousepage.png

The one we have essentially has two buttons on the side in addition to the standard mouse buttons along with a "DPI" button that also changes colours with the mouse DPI. It has a black top, all one piece on the top (unlike the pictures I saw in the original BWG66 post I linked).

So I hate optical drives, and in both the PC I built for myself and the one my brother uses doesn't even have one inside it. Seriously, I want optical discs to just die.

But recently for my brothers birthday he got a "Blackweb Gaming Mouse", it has 5 buttons not counting the DPI button and is supposedly RGB, which changes colour when you change the DPI if the driver is not installed and configured otherwise. It was purchased at Walmart. A CD came with the mouse, supposedly with the driver/utility, but generally we just head online to find the driver and download it from there.

Well not this time. People have asked on Reddit, and YouTube, and on Walmart.ca itself, nobody knowing an answer on where to get the drivers for Blackweb.

So I'm not sure if they just didn't get the CD, they just refuse to insert one into their PC, or they don't have an optical drive, but I took the time to fetch the setup file (It's literally the only file on the CD) and upload it here. It's only ~5MB, and to be honest it's way more clunky than Logitech/corsair software I've used, but it gets the job done. It definitely does have "cheap" written all over it but hey... if it works, at least you got the full functionality that was advertised on the box.

The driver lets you change the colour profiles, change button functions (including changing the function of the "DPI" button) and DPI itself from a graphical interface among other things.

If you have this mouse... then look no further than here. EDIT: Additionally, I have provided a download link for other Blackweb models (namely, BWA17 at least). If you have drivers to submit if your model is not supported, please email them to me ([email protected]) to share here.

Download links:

Click here (Blackweb Gaming Mouse Driver 5 button + DPI button)

Click here (Blackweb RGB Gaming Mouse BWA17 Driver) (Thanks to Jared Trotter for submitting this!)

Screenshot: screenshot screenshotbwa17

If any nerds out there want to mirror this that would be great just so I don't lose it down the road if I decide to make server changes, but hopefully I won't ;)

In addition, this reverse engineered python utility is available for the Blackweb AYA(?) mouse (looks similar to the mouse my brother has?) for linux! If you're using debian/ubuntu run sudo apt install python2.4 python-qt4 pyusb and then run sudo ./ayatool_qt.py (remember to chmod it!) to launch the graphical configuration tool (have not tested so if you need help post back especially if I'm wrong about dependencies!). If you can install all that stuff on a Mac it should also work for Mac users. Kudos to commenter Jigoku for writing this!

Screenshot: linux blackweb utility

TL;DR: If you just want the rewrite rules for Lighttpd, scroll to the bottom code snippet.

So like most every other PHP app I've worked with, everybody seems to support both apache and nginx but poor old lighttpd gets left out in the cold and has no support on how to configure it in documentation.

Well have no fear, because you'll have to pry lighttpd out of my cold dead hands!

A while back I setup a VHOST for jseb.ca on Lighttpd with an FTP server so I could integrate it with Sharex. One other feature sharex seems to support is "short URLs" using an API from a PHP script called YOURLS. So I wanted to install it on my low end box.

Now if you caught on earlier, this blog is hosted using a flat-file database platform called htmly, because my lowendbox with only 128MB RAM isn't really ideal for running something like wordpress. MySQL can sure eat the RAM, so I sort of decided to not use MySQL on this server at all.

SQLite on the other hand is a perfect candidate. While it locks the entire database when you're writing to it, something like a URL shortener is a perfect application, since it's pretty much read only. Only time it writes is when I decide to add a URL, which is a fast query and so few and far between, it's a way better candidate for a personal URL shortener over MySQL for a site my size anyway.

Unfortunately, YOURLS doesn't support SQLite out of the box. Luckily there's a great project here on github that adds it. Sweet! That was easy! Install php5-sqlite on my Debian install, chown/chmod your stuff so PHP can write to the directory YOURLS is in, install it by going to /admin/ and we're all set.

However, first thing you'll notice if you're running lighttpd is you'll get 404s since lighttpd doesn't support .htaccess, which YOURLS generates when you first install it. A search around on the internet will find stuff like this, but all the snippets in the examples causes issues for me.

While the last post from gsdefender (from Apr 24, 2016) works, my index.html on jseb.ca doesn't! Now I know it's not special, but it triggered me that if you visit the root, rather than showing my index.html it resulted in a redirect loop which freaked out Chrome.

Now like you probably figured out by now if you read anything here, I'm not much of a sysadmin or programmer, but I know my way around enough to be dangerous. I never got my head around regex stuff, but a quick look at lighttpd documentation says the ?

match zero or one of the previous symbol

Previous being keyword here... I just removed it.

So, now, with a vhost setup and mod_rewrite enabled for lighttpd, my lighttpd.conf looks like this for jseb.ca:

$HTTP["host"] == "jseb.ca" {
    server.document-root = "/var/www/upload/"
    url.rewrite-once = (
#        "^/([0-9A-Za-z-]+)?$" => "/yourls-go.php?id=$1",
#        "^/([0-9A-Za-z-]+)?\+$" => "/yourls-infos.php?id=$1",

        "^/([0-9A-Za-z-]+)$" => "/yourls-go.php?id=$1",
        "([0-9A-Za-z-]+)\+$" => "/yourls-infos.php?id=$1",
)

As you can see, the original copy-pasta from the github issue is commented out so you can note the difference between the two. It works for me... now my uploads and shorturls both reside on jseb.ca and coexists pefectly along with an index.html. So hopefully this helps anybody else who wants to run YOURLS on lighttpd.

Why did they put the ? to begin with? I don't know? Perhaps you can tell me? enter image description here

I couldn't upload images to htmly with my lighttpd config. Further, the lighttpd config blocked my favicon.ico file. All images would result in a 404 when uploaded, despite ls-ing to the directory shows it uploaded successfully. I'm not using vhost with lighttpd... and honestly ... I blindly copied and pasted the config directly from the htmly documentation found on the github readme (and of course... enabled the rewrite mod in lighttpd.conf which is as simple as uncommenting. The verbatim code was:

$HTTP["url"] =~ "^/config" {
  url.access-deny = ( "" )
}
$HTTP["url"] =~ "^/system/includes" {
  url.access-deny = ( "" )
}
$HTTP["url"] =~ "^/system/admin/views" {
  url.access-deny = ( "" )
}

url.rewrite-once = (
  "^/(themes|system|vendor)/(.*)" => "$0",
  "^/(.*\.php)" => "$0",

  # Everything else is handles by htmly
  "^/(.*)$" => "/index.php/$1"
)

My issue was I would get 404s on images I upload. So I modified the line "^/(themes|system|vendor)/(.*)" => "$0", to show "^/(themes|system|backup|vendor|content/images)/(.*)" => "$0", since content/images is where the images reside when you upload them with htmly. Additionally, backup was added so you can download backups direct from the interface.

Next, I added a line directly below it "^/(favicon\.ico|robots\.txt|humans\.txt|sitemap\.xml)$" => "$0", which of course, ensures favicon.ico, robots.txt, humans.txt and sitemap.xml actually works.

It works. It's not perfect. I want to be able to create other directories without rewrite screwing me over for hosting random files and whatever else I feel like. Perhaps I'll setup proper vhosts and elaborate on that down the road as I work on this site. For now the config is in /etc/lighttpd/lighttpd.conf at the bottom, and it gets the job done, so I'm happy.

FWIW... here is my entire config I ended up modifying to for those of you who also want to blindly copy and paste :P

$HTTP["url"] =~ "^/config" {
  url.access-deny = ( "" )
}
$HTTP["url"] =~ "^/system/includes" {
  url.access-deny = ( "" )
}
$HTTP["url"] =~ "^/system/admin/views" {
  url.access-deny = ( "" )
}

url.rewrite-once = (
  "^/(themes|system|backup|vendor|content/images)/(.*)" => "$0",
  "^/(favicon\.ico|robots\.txt|humans\.txt|sitemap\.xml)$" => "$0",
  "^/(.*\.php)" => "$0",

  # Everything else is handles by htmly
  "^/(.*)$" => "/index.php/$1"
)

But yeah.... fixes that issue for you lighttpd users out there who can't upload images with the htmly platform :)

Why am I using lighttpd? Well certainly apache is out of the picture for a 128MB Low End Box. Lighttpd was the choice mainly because I've used it more than nginx. I like nginx and have used it a bit but lighttpd gets the job done for me where I have more experience with it (it use to serve the chickentalk.org forums I used to run a few years ago well so you can say I used it in "production") and is rock stable as far as I can tell for all the memory leak complaining I've heard about in the past, so I use it.

Perhaps one day I'll switch to nginx since that's where the community is at, but old habits die hard. It's always been my goto apache alternative.