Wednesday, October 19, 2016

Overwrite response body with mitmproxy

It is possible to run python script with mitmproxy. For example:
mitmproxy -s ""
The modification process is very simple. Take a look at the code:
from mitmproxy.models import decoded

def response(context, flow):
    if "site.custom.min.js" in flow.request.path:
        with decoded(flow.response):  # automatically decode gzipped responses.
            flow.response.content = flow.response.content.replace(
The script will look for a request with path that contains site.custom.min.js and replace all occurences of with

mitmproxy and websocket connections

mitmproxy does not really supports websocket connections. However, there is a workaround: it is possible to just ignore them. For example all your websocket conections are encrypted and go to the special subdomain:
mitmproxy --ignore

Thursday, October 13, 2016

Slow performance and no sound with pulseaudio-equalizer

I installed pulseaudio-equalizer on my laptop, but after 3 days my laptop became slow and there wes no sound at all. It happened because of the pulseaudio-equalizer package. Even if you remove it, the problem won't go because it adds some configuration to the pulseaudio itself and doesn't remove it.
Open pulseaudio configuration file, find pulseaudio-equalizer section and remove it completely:
sudo nano ~/.pulse/
The section should look like this:
### Generated from: pulseaudio-equalizer
load-module module-ladspa-sink sink_name=ladspa_output.mbeq_1197.mbeq master=alsa_output.pci-0000_05_00.0.analog-stereo plugin=mbeq_1197 label=mbeq #control=-0.2,-0.2,-0.2,-0.2,3.5,3.5,3.5,3.5,3.5,3.5,3.5,2.5,2.5,0.0,0.0
set-default-sink ladspa_output.mbeq_1197.mbeq
set-sink-volume alsa_output.pci-0000_05_00.0.analog-stereo 65536
set-sink-mute alsa_output.pci-0000_05_00.0.analog-stereo 0
### END: Equalized audio configuration
Reboot your computer and enjoy restored performance and sound!

Monday, October 10, 2016

fedora: Install latest VirtualBox

The latest virtualbox can be installed from the Oracle repository. First of all we need to install oracle repository:
cd /etc/yum.repos.d/
sudo wget
Install dependencies:
sudo dnf install binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-PAE-devel dkms
Install virtualbox. Note: pointing 5.1 is important as at the moment that is how it is called in the oracle's repository:
sudo dnf install VirtualBox-5.1
You might need to rebuild kernel modules with command:
sudo /usr/lib/virtualbox/ setup
And add current user to the xboxusers group:
sudo usermod -a -G vboxusers user_name
I had a problem reconfiguring kernel modules on my laptop. The error wasn't indicating anything specific. The problem was in the BIOS settings. Secure Boot should be disabled to allow module reconfiguration.

Monday, October 3, 2016

Speeding up Django tests: Disable migrations during tests

Running all migrations before the testrun can take a lot of time. For some reason, they are very slow. Running 50 migrations could easily take more than 1 minute! Fourtunately, disabling migrations is very easy. Just add the following code to the test's settings file:
# Disable migrations when running tests
    app[app.rfind('.') + 1:]: 'your_app.migrations_not_used_in_tests'
    for app in INSTALLED_APPS
Some of our migrations add data to the database and tests expect this data to be there. To fix that problem we will need to overwrite test runner. Add to the settings file:
TEST_RUNNER = 'your_app.test_runner.CustomDiscoverRunner'
Then create in the project root with the following content:
from django.test.runner import DiscoverRunner

from import create_groups

class CustomDiscoverRunner(DiscoverRunner):
    def setup_databases(self, **kwargs):
        result = super(CustomDiscoverRunner, self).setup_databases(**kwargs)
        return result
After recreating test database directly from the models (what is really fast), test runner will execute create_groups() function which populates database with all necessary data.

Wednesday, September 14, 2016

Analyze http2 with Wireshark

Most of the time http2 requests are sent over TLS, which means that Wireshark needs to decrypt information before we can analyze it. Fortunately, Chrome allows to dump cryptographic keys in the file. To do that, before you start Chrome, export environmental variable SSLKEYLOGFILE:
export SSLKEYLOGFILE=/home/jsn/Downloads/sslkeys/sslkeys.log
Firefox also allows to do it, but as for now (48) it is disabled by default. Get more information Now we need to configure Wireshark to use exported keys. Go to Edit - Preferences - Protocols - SSL and point (Pre)-Master-Secret log filename to the file with keys (/home/jsn/Downloads/sslkeys/sslkeys.log) Now you can filter records by http2 and analyze them.

Wednesday, August 24, 2016

tar: Archives cheatsheet

Create archive:
tar -czvf compressed_file.tar.gz original_file.sql
Extract files:
tar -xzvf compressed_file.tar.gz

Tuesday, August 2, 2016

Number of connections to the specific port

To check how many connections are exists to the port 3306:
watch -n 1 "sudo netstat -anp | grep 3306 | wc -l"

Monday, June 27, 2016

fedora: Increase watches limit

Open configuration file:
sudo vim /etc/sysctl.d/90-override.conf
Add the following line to the end of the file to increase watches limit:
fs.inotify.max_user_watches = 524288

Saturday, June 25, 2016

postgresql: How to fix ERROR: new encoding (UTF8) is incompatible

Sometimes I'm unable to create a database in UTF8 encoding because the template database is in the ASCII encoding. To fix it we need to recreate template database in UTF8. Start psql console:
psql -U postgres
Run the following commands:
UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
DROP DATABASE template1;
UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
\c template1

Tuesday, May 31, 2016

Preview docs

It is possible to preview apiary docs before pushing to the repository. To do that, install apiaryio CLI:
gem install apiaryio
The executable file of the installed application is located somewhere there:
Generate html file to preview docs and open it in the browser:
/home/jsn/.gem/ruby/gems/apiaryio-0.3.3/bin/apiary preview --output="preview.html"

Friday, May 13, 2016

fedora: Install HipChat 4

UPDATED: Atlassian team has a rpm repository which they do not mention on the official download page. To install hipchat4 from it, create a repository file:
sudo gedit /etc/yum.repos.d/hipchat4.repo
With the following content:
name=Atlassian Hipchat4
Install hipchat:
sudo dnf install hipchat4 --refresh
Atlassian team wasn't able to release rpm package for the latest hipchat for more than 2 months. Fortunately, it is possible to intsall hipchat from the debian package. Download the latest available debian package from here.

Install alien package which will convert deb package to the rpm package:
sudo dnf install alien
Convert hipchat debian package to rpm package:
sudo alien HipChat4-4.0.1635-Linux.deb -r
Install hipchat:
sudo dnf install hipchat4-4.0.1635-2.x86_64.rpm

fedora: X11 forwarding issue

Here I have a nice post how to open browser window on the remote machine. On fedora 23 that method stopped to work. It was possible to start simple application like xclock, but firefox or chrome.
The problem is in the xorg-x11-drv-intel package. The latest version which is available in the official fedora repo is xorg-x11-drv-intel-2.99.917-19.20151206.fc23 (as for 13 May 2016). Downgrading the package to the previous version fixes the issue.
The latest version of the package which is known to work is xorg-x11-drv-intel-2.99.917-19.20151206.fc23. Let's install it:
sudo dnf install xorg-x11-drv-intel-2.99.917-16.20150729.fc23.x86_64
It would be also nice to lock the package version, so the next time you start updating the system it won't be updated:
sudo dnf install 'dnf-command(versionlock)'
sudo dnf versionlock add xorg-x11-drv-intel

Monday, May 2, 2016

How to format USB stick as FAT32

First step is to find the name of the USB stick in the system:
sudo fdisk -l
In my case the name of the device is /dev/sdc If the USB stick is mounted, unmount it with command:
sudo unmount /dev/sdc
Finally, run the command to format USB stick:
mkdosfs -F 32 -I /dev/sdc

Monday, April 18, 2016

apt-get: Install package with specific version and hold it on the next upgrade

If you need to install specific version of the package in your system, you can list all available package versions with the command:
apt-cache madison varnish
After you know the package version you can install it with:
sudo apt-get install varnish=4.0.2-1
To hold the package on the selected version during the next upgrade:
sudo apt-mark hold varnish
You can also "unhold it" with:
sudo apt-mark unhold varnish