Sunday 6 April 2014

Plex Dual Monitor on Ubuntu

I had a lot of problems with the Plex client on Ubuntu and my dual monitor setup.

If you run plex in fullscreen mode it detects the screens incorrectly (because of TwinView and xrandr).

The best way I could get to fix this is by running Plex in windowed mode but making it full screen (so it looks like it is in fullscreen mode). This has the added benefit of still allowing you the use of your mouse and keyboard on your other screen.

Unfortunately plex detects the resolution of your screen automatically for windowed mode, and did this incorrectly for me (it set the resolution to 720p, that of my small monitor instead of 1080p), so my plex did not fill the entire screen and had big black borders on two sides.

I fixed this with a bit of a hack, by lying to plex about the xrandr output.

Feel free to give any ideas for better fixes in the comments.

So here is how I got my Plex to work correctly on my two monitors.

Part 1 - Full screen windowed mode:


1. Set Plex to windowed mode:


Preferences > System > Advanced > Display Mode > Windowed


2. Install wmctrl:


sudo apt-get install wmctrl
(or use software center if you like)

3. copy the shell script for plex to your own bin folder and make some changes:


mkdir ~/bin
cp /usr/bin/plexhometheater.sh ~/bin/plexhometheater.sh

change the content in your own bin folder to:

#!/bin/sh
export XBMC_HOME=/opt/plexhometheater/share/XBMC
/opt/plexhometheater/bin/plexhometheater &
sleep 1
wmctrl -x -r plexhometheater.plexhometheater -e 0,0,-1,-1,-1
wmctrl -x -r plexhometheater.plexhometheater -b add,fullscreen

Make sure this is executed when you launch plex instead of the original.
The first 2 line are the same as the original file,
The 3rd line has an added '&' which allow the other lines to be executed
The 4th line sleeps one second while plex launches
The 5th line puts plex on the correct screen (the second 0 is the x-coordinate of the plex window), so change that to something like 3000 if you want plex on your second monitor, ex.

wmctrl -x -r plexhometheater.plexhometheater -e 0,3000,-1,-1,-1

The 6th line makes the window full screen.
Hopefully this is all you need. If your Plex has the wrong resolution then follow part two

Part 2 - Change windowed mode resolution:


Plex sets the resolution using its own xrandr binary, so to force the resolution I lied to it.

1. Turn off your second screen, keeping only the one you want plex to run on:


In Ubuntu and not Plex go to Settings > System Settings... > Displays and turn off the display you to not want plex on, leaving only the want you want plex to be displayed on.

This lets you get the correct xrandr output for the next step.

2. Run Plex's xrandr and save the output:


cd /opt/plexhometheater/bin
sudo ./xbmc-xrandr > xrandr.txt

3. Turn on your second screen again:


Settings > System Settings... > Displays and turn on your second display

4. Move xrandr:


sudo mv xbmc-xrandr xbmc-xrandr_bac

5. Create your own xrandr that calls xrandr when it wants to, otherwise returns your txt file:


Use your favourite editor (in my case vim) to create your own xrandr file
sudo vim xbmc-xrandr
And paste the following

#!/bin/bash

if [ "$#" == 0 ]; then
    cat /opt/plexhometheater/bin/xrandr.txt
else
    /opt/plexhometheater/bin/xbmc-xrandr_bac "$@"
fi
save the file and make it executable
sudo chmod +x xbmc-xrandr

Plex will now call your xrandr instead of the normal one, and you will tell it you only have the high resolution screen connected.

You might want to make a copy of these files somewhere, I do not know if Plex will overwrite the xbmc-xrandr file if it updates.

And that's it, a bit of a hack but it works.


Wednesday 12 February 2014

Amazon E-Book Specials Checker

I recently cleaned up my Amazon E-Book Specials Checker and put it up on github.

If you are like me and like to know when e-books from your favourite authors are on special, check it out.

You can set the price to look for, add specific authors to a list to watch for, or just specific books.

The program can log-in to your amazon account if you choose to let it (this ensures that you get the correct pricing for the book, as the price differs per region).

How this program came to be:

A while ago I decided to write a script for checking e-book specials, so I tried to use the amazon API.
I got this working, but they did not have pricing for e-books (only physical books).

Plan B was to scrape the website. I wrote some code in python, scraping the website with urllib2 and using Beautiful Soup to parse the html. This did not always give the correct prices though, because my server running the script was not in the same country as I and amazon has different prices in different countries.

The next problem was to figure out how to log-in to my amazon account, this makes sure I always have the correct prices.

So with that done, here it is. Feedback is appreciated.

link to program

Tuesday 3 December 2013

Flat Network 1: USB Internet Dongle

The steps to set up your dongle is explained on the OpenWRT wiki. But I had some problems because of the Specific dongle I was using (The Nokia dongle you get from CellC). So here is how to get it working.

ssh into the router then install packages with the following command:
opkg install comgt usbutils kmod-usb2 kmod-usb-ohci kmod-usb-serial \
kmod-usb-serial-option kmod-usb-acm usb-modeswitch usb-modeswitch-data
optionally also
opkg install luci-proto-3g
for the gui. Run lsusb, plug the dongle into the USB port of the router and run lsusb again. You should see a new device. Take note of the ID. The first part is the Vendor and the second part is the Product ID. Run:
ls /dev/
You will see that no new tty devices appear when you insert the dongle, this is because the dongle is not in the correct mode. This product's mode needs to be changed and it is not known to modeswitch, so it does not know what to do with it. Run:
ls /etc/usb_modeswitch.d
You will see that your ID is not there. But there are other devices from the same vendor (Nokia). A few of these devices run as the same product, so just copy one of these files to a file with your ID as the name and edit the product ID field to match yours.

Remove the dongle and plug it in again and you will see that the ID lsusb gives you is now different and you have two new devices in /dev/, ttyACM0 and ttyACM1. If you run:
gcom -d /dev/ttyACM0
You should see something like:
SIM ready
Waiting for Registration..(120 sec max)
Registered on Home network: "Cell C",2
Signal Quality: 9,0
So you know the device is communicating correctly. Now edit your /etc/config/network file and edit the wan section:
config interface wan
        option ifname  ppp0
        option pincode 1234
        option device  /dev/ttyACM0
        option apn     internet
        option service umts
        option proto   3g
        option pppd_options "noipdefault refuse-chap refuse-mschap \
                refuse-mschap-v2 refuse-eap"

Most websites recommend that you set a pin code for the dongle's SIM if it does not have one already, as some OpenWrt builds do not connect correctly without it. Run:
ifup wan
If it runs successfully it should give the output:
SIM ready
PIN set successfully
You should now have a working internet connection.

Sunday 27 October 2013

Using Netgear N300 as a modem only

Telkom gives a Netgear N300 modem to their new customers. If you want to use it as a modem only and dial from another router (I use a TP-link router running OpenWRT), follow these easy steps.

Force a 10.0.0.0/8 range IP on your computer (I used 10.0.0.3) and connect to 10.0.0.2 in your browser, the default username and password is usually admin:admin or admin:<blank>.


Then go to 'Setup > Basic Settings', at 'Does Your Internet Connection Require A Login?', set it to 'No', make sure 'Enable' is ticked under 'NAT' and select 'Apply'.


Next go to 'Advanced > WAN Setup', make sure 'Enable PPPoE Relay' is ticked and 'Apply'.


Then go to 'Advanced > LAN Setup', untick 'Use Router as DHCP Server' and 'Apply'.


All that is left is to plug a cable from the modem into the WAN port of your chosen router and dial a PPPoE connection with your Telkom login details.

Setting up iTunes in Linux

I got iTunes working on Ubuntu 13.04 after some googling, but it should work on any Debian based distro. I tested buying and downloading an album and that works. I am not interested in using it as a music player, so you can test that if you want and let me know in a comment.

You will need:

  • iTunes 10 - 32 bit (iTunes 11 works, but cannot connect to the internet, so it is useless and 64 bit does not work)
  • The newest PlayOnLinux (At the time of writing it was version 4.2.1)
  • Wine 1.4.1
iTunes 10.3.1 is available from the Apple support site, so download it (direct dl link).
For the newest PlayOnLinux you will need to add the ppa, instructions are here. There is not an option for Raring Ringtail, Ubuntu 13.04, but I just used the Quantal ppa and that worked fine.
You will need to install wine:
sudo apt-get install wine

Now, run PlayOnLinux and choose 'Install'.


Tick the 'Testing' box and search for iTunes. Select iTunes 10 and press Install.


Follow the steps, they will ask you to select an iTunes installer, select the one you downloaded previously. When you finish, you should have a working iTunes in Linux.

Monday 23 September 2013

Reset Locked Chinese Tablet

Someone gave me this cheap Chinese tablet:


He forgot the unlock pattern for the tablet and trying to guess it, we got totally locked out. You could still reset it with your google account, but the wifi was off and there is no way to turn it on whilst locked out. So +Nicholas Thompson and I spent some hours figuring out how to reset this tablet.

I found out this tablet uses a rk2918 (Rockchip) SoC (you can open it up or if you know someone else with the same tablet, go to Settings > About Device and check the 'Model Number', mine was 'Full AOSP on Rk29sdk')

First I had to get the tablet into recovery mode. The tablet has no volume buttons, so it has a different key combination from most tablets. So to get into recovery mode make sure the data cable is disconnected, turn off the tablet, then hold the power and menu buttons until the android logo appears. It should now be in recovery mode. Connect the data cable to the computer and run:
lsusb
You will get something like:
Bus 001 Device 011: ID 18d1:d001 Google Inc.
For some reason this is a Google Inc device because of its Vendor ID (list of vendors for those interested). You will need to get adb somewhere, I got it from the android website but it is a big download. You can download just the platform-tools here or run:
wget http://dl.google.com/android/repository/platform-tools_r18.0.1-linux.zip
unzip platform-tools_r18.0.1-linux.zip
cd platform-tools/
You should see a device with the command:
./adb devices
listed as:
???????????? no permissions
Add the tablet's vendor to the udev file to get the correct permissions:
echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="18d1" , MODE="0666", GROUP="plugdev"' >> /etc/udev/rules.d/51-android.rules
disconnect and reconnect the data cables and run ./adb devices again and you should get:
0123456789ABCDEF recovery
To get the tablet info flash mode run:
./adb reboot bootloader
The tablet screen will go black, but it is not off, you will see it if you run lsusb again. It should be listed as something like Bus 001 Device 013: ID 2207:281a. Add this device to the udev rules as well:
echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="2207", MODE="0666", GROUP="plugdev"' >> /etc/udev/rules.d/51-android.rules
For the next step you will need Rockbatchtool. I found some instructions and a linux version here.

You need to install the needed package and build the executable:
wget -O rkflashtool-v2.zip http://forum.xda-developers.com/attachment.php?attachmentid=785592&d=1321547155
unzip rkflashtool-v2.zip
sudo apt-get install libusb-1.0-0-dev
gcc -o rkflashtool rkflashtool.c -lusb-1.0 -O2 -W -Wall -s
Find the tablet's partition table by running:
./rkflashtool r 0x0000 0x2000 >parm
then check the start of this file with a hex dump:
xxd parm | less
mine looked like this:
0000000: 5041 524d 5302 0000 4649 524d 5741 5245  PARMS...FIRMWARE
0000010: 5f56 4552 3a30 2e32 2e33 0d0a 4d41 4348  _VER:0.2.3..MACH
0000020: 494e 455f 4d4f 4445 4c3a 4675 6c6c 2041  INE_MODEL:Full A
0000030: 4f53 5020 6f6e 2052 6b32 3973 646b 200d  OSP on Rk29sdk .
0000040: 0a4d 4143 4849 4e45 5f49 443a 3030 370d  .MACHINE_ID:007.
0000050: 0a4d 414e 5546 4143 5455 5245 523a 524b  .MANUFACTURER:RK
0000060: 3239 5344 4b0d 0a4d 4147 4943 3a20 3078  29SDK..MAGIC: 0x
0000070: 3530 3431 3532 3442 0d0a 4154 4147 3a20  5041524B..ATAG: 
0000080: 3078 3630 3030 3038 3030 0d0a 4d41 4348  0x60000800..MACH
0000090: 494e 453a 2032 3932 390d 0a43 4845 434b  INE: 2929..CHECK
00000a0: 5f4d 4153 4b3a 2030 7838 300d 0a4b 4552  _MASK: 0x80..KER
00000b0: 4e45 4c5f 494d 473a 2030 7836 3034 3038  NEL_IMG: 0x60408
00000c0: 3030 300d 0a43 4d44 4c49 4e45 3a20 636f  000..CMDLINE: co
00000d0: 6e73 6f6c 653d 7474 7953 312c 3131 3532  nsole=ttyS1,1152
00000e0: 3030 6e38 6e20 616e 6472 6f69 6462 6f6f  00n8n androidboo
00000f0: 742e 636f 6e73 6f6c 653d 7474 7953 3120  t.console=ttyS1 
0000100: 696e 6974 3d2f 696e 6974 2069 6e69 7472  init=/init initr
0000110: 643d 3078 3632 3030 3030 3030 2c30 7838  d=0x62000000,0x8
0000120: 3030 3030 3020 6d74 6470 6172 7473 3d72  00000 mtdparts=r
0000130: 6b32 3978 786e 616e 643a 3078 3030 3030  k29xxnand:0x0000
0000140: 3230 3030 4030 7830 3030 3032 3030 3028  2000@0x00002000(
0000150: 6d69 7363 292c 3078 3030 3030 3430 3030  misc),0x00004000
0000160: 4030 7830 3030 3034 3030 3028 6b65 726e  @0x00004000(kern
0000170: 656c 292c 3078 3030 3030 3830 3030 4030  el),0x00008000@0
0000180: 7830 3030 3038 3030 3028 626f 6f74 292c  x00008000(boot),
0000190: 3078 3030 3030 3830 3030 4030 7830 3030  0x00008000@0x000
00001a0: 3130 3030 3028 7265 636f 7665 7279 292c  10000(recovery),
00001b0: 3078 3030 3046 3030 3030 4030 7830 3030  0x000F0000@0x000
00001c0: 3138 3030 3028 6261 636b 7570 292c 3078  18000(backup),0x
00001d0: 3030 3033 6130 3030 4030 7830 3031 3038  0003a000@0x00108
00001e0: 3030 3028 6361 6368 6529 2c30 7830 3031  000(cache),0x001
00001f0: 3030 3030 3040 3078 3030 3134 3230 3030  00000@0x00142000
0000200: 2875 7365 7264 6174 6129 2c30 7830 3030  (userdata),0x000
0000210: 3032 3030 3040 3078 3030 3234 3230 3030  02000@0x00242000
0000220: 286b 7061 6e69 6329 2c30 7830 3030 4536  (kpanic),0x000E6
0000230: 3030 3040 3078 3030 3234 3430 3030 2873  000@0x00244000(s
0000240: 7973 7465 6d29 2c2d 4030 7830 3033 3241  ystem),-@0x0032A
0000250: 3030 3028 7573 6572 290d 0a3b ff43 e300  000(user)..;.C..
The relevant parts are:
0x0003a000@0x00108000(cache) and 0x00100000@0x00142000(userdata).

This means the cache partition has a size of 0x0003a000 and starts at 0x00108000 and the userdata a size of 0x00100000 and starts at 0x00142000.

Firstly backup both these partitions (in case this does not work):
./rkflashtool r 0x108000 0x3a000 >cache_backup
./rkflashtool r 0x142000 0x100000 >userdata_backup
Then overwrite both partitions with 0's:
./rkflashtool w 0x108000 0x3a000 </dev/zero
./rkflashtool w 0x142000 0x100000 </dev/zero
Lastly reboot the tablet with:
./rkflashtool b
The tablet should now boot as though it was factory reset. I went to the settings menu anyway and did another factory reset from there, just in case.

Wednesday 7 November 2012

Home Network 2: CTWUG

The Network

This post is about the CTWUG setup. I bought all the needed hardware from Scoop and followed the instructions on the CTWUG wiki to get everything working. I then spent a while on their IRC server to find the correct person to connect to, and get an IP range allocated. They allocated 172.18.28.80/29 to me (172.18.28.80-172.18.28.86), with .80 allocated to the wireless radio card and .86 to the MikroTik router. At first I forced IP's and a gateway on each PC on my network, but I wanted a more elegant solution.


The way I decided to go was to put the CTWUG connections on a separate VLAN and to do source and destination NATing.

I connected the MikroTik to port 5 of the TP-LINK router and set that port on a separate VLAN.


Then had to create a separate interface for it with only the VLAN that was just created.


And to get the whole thing working a static route is needed which routes all ctwug traffic (172.18.0.0/16) through the MikroTik.


At this stage I could ping the CTWUG network, but had a problem with the hostname lookups. So under the advanced DHCP options, I entered option 6, which advertises custom DNS servers. And advertised the ctwug DNS server and Telkom DNS server with it. The full command is "6,172.18.1.1,196.43.45.190".


I am now able to access everything on the CTWUG network, but all my devices are behind a masquerade NAT, so I would not be able to access my individual devices from the CTWUG network. I need this if I decide to host a website or any other service on the WUG.

The first part (and I am still looking for a better solution) is to create a few IP aliases so the router responds to ARP requests for those IP's.


And the final part is for the router to actually forward request for those IP's to the correct PC on the local network. For this you need SNAT for outgoing packages and DNAT for incoming packages. I want this to be 1 to 1 NATing, so instead of adding a DNAT and SNAT for each IP, I used the NETMAP iptables option. I first needed to install the iptables-mod-nat-extra package.



I then added two firewall rules to do the NATing. For the destination NAT

iptables -v -t nat -A PREROUTING -i eth0.3 -s 172.18.0.0/16 -d 172.18.28.80/29 -j NETMAP --to 192.168.0.0/29

for the source NAT

iptables -v -t nat -A POSTROUTING -o eth0.3 -s 192.168.0.0/29 -d 172.18.0.0/16 -j NETMAP --to 172.18.28.80/29

This would mean that 192.168.0.1 would be seen as 172.18.28.81 to other people on CTWUG, and similarly up to 192.168.0.6. I also made sure masquerading was enabled (for any IP's not in this range).



That is it! You now have this part of the network:



Torrenting Tip

One problem I still had was that torrents that I wanted to download from CTWUG sometimes used my Internet. The client I use is Transmission and it can use block lists. So I blocked all IP's that are not on the CTWUG network on the Transmission client of the PC I use for my CTWUG torrents.

I did this by adding the lines

other:1.0.0.0-172.17.255.255
other2:172.19.0.0-254.255.255.255

to

~/.config/transmission/blocklists/list.txt

and made sure blocklists were enabled in

~/.config/transmission/settings.json

with the line

"blocklist-enabled": true,

tl;dr
Set up the CTWUG network connection with 1 to 1 NATing and gave a handy torrenting tip.