Tools

NetCat

NetCat (nc) is used to read and write to TCP and UDP network connections. It is an excellent tool for diagnosing network connectivity issues.

Installation

sudo apt-get install netcat

Usage

To listen to a specific port.

nc -l -p 8000

To write to a specific port

nc 127.0.0.1 8000
nc -v 127.0.0.1 8000

phpMyAdmin

About

The phpMyAdmin tool is an SQL database manager written in PHP. It resides in the web root on your server, making it easily accessed from a web browser.

There are a number important security considerations when using software like phpMyAdmin, since it:

  • Communicates directly with your MySQL installation.
  • Handles authentication using MySQL credentials.
  • Executes and returns results for arbitrary SQL queries.

The phpMyAdmin tool is available for a number of different operating systems. This article will focus solely on the Linux installation.

Manual Installation

While there are a number of ways to install phpMyAdmin, there is an easy quick install method. This involves downloading and extracting it directly to your web root directory. While you will need at least basic bash terminal knowledge, it is relatively trivial to set up. However, you will require sudo privileges or access to the web-user-account.

Following are the condensed steps for the quick install found in the phpMyAdmin documentation.

cd /www
sudo wget https://files.phpmyadmin.net/phpMyAdmin/5.1.1/phpMyAdmin-5.1.1-english.tar.gz -O phpMyAdmin.tar.gz
sudo tar -xvf phpMyAdmin.tar.gz
sudo chown -R wp-user:wp-user phpMyAdmin-5.1.1-english/
sudo cp config.sample.inc.php config.inc.php
  1. Change directory to your web root.
  2. Download the tar file. You can choose from a number of options here. We rename it here with the -O flag
  3. Unpack the tar, it will be placed in it’s own directory.
  4. Give ownership of the directory to the web user account.
  5. Create the configuration file by copying the sample configuration file.

Usage

When a user logs into phpMyAdmin the username and password are sent directly to the SQL database. It is just an interface to the database, and any operation it does can be done on the command line. As such, all users must be valid database users.

Securing

You should use cookie mode authentication so that your user/password pair are not kept in the configuration file. The variable may be set in the example config file as:

$cfg['Servers'][$i]['auth_type'] = 'cookie';

You will need to also add a 'blowfish secret' value to the config file.

$cfg['blowfish_secret'] = 'anyrandomtextyouwant';

Deny access to the temp, libraries, and templates subdirectories. Put the following in the server directive of your nginx enabled sites file.

location /phpmyadmin/libraries { deny all; }
location /phpmyadmin/templates { deny all; }
location /phpmyadmin/tmp { deny all; }

References

https://www.phpmyadmin.net/

https://docs.phpmyadmin.net/en/latest

NodeJS Code Coverage With C8

Code coverage allows you to determine how well unit-tests cover your codebase. The standard code coverage suite for JavaScript is the NYC interface for Istanbul. However, this package doesn’t play well with NodeJS modules. Instead we are going to use the C8 package. C8 uses functionality native to NodeJS to create output that is compatible with Istanbul reporters.

Installation

npm i --save-dev c8

Execution

You can run c8 on any source file by simply passing in the appropriate node command.

$ npx c8 node src/index.js
-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files  |   88.23 |      100 |      50 |   88.23 |
 MooYou.js |   84.61 |      100 |      50 |   84.61 | 9-10
 index.js  |     100 |      100 |     100 |     100 |
-----------|---------|----------|---------|---------|-------------------

You can also easily run it with your Mocha test files.

$ npx c8 mocha
  MooYou Class Test
    #who
      ✔ returns 'you'


  1 passing (3ms)

-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files  |   84.61 |      100 |      50 |   84.61 |
 MooYou.js |   84.61 |      100 |      50 |   84.61 | 9-10
-----------|---------|----------|---------|---------|-------------------

Configuration

C8 can be configured from the command line, in your package.json file, or in a configuration file in your project directory. In particular you can instruct C8 to report one files as opposed to just those in your tests (npx c8 --all mocha). You can also specify which files to include and/or ignore. See the configuration documentation for specific details.

Generating Reports

By default the coverage data will be located in the coverage/tmp directory. You can change this directory with the --temp-directory flag. Don’t forget to add this to your .gitignore file.

Run npx c8 report to regenerate reports after c8 has already been run. Use the -r flag to specify which reporter to use, and the -o flag to specify where to output the report. By default generated reports can be found in the coverage/ dirctory.

Vanilla C8 command with HTML report:

npx c8 -r html mocha

Generate C8 report after the fact:

npx c8 -r html report

Two-part C8 command with custom directories and HTML report:

npx c8 --temp-directory .c8 mocha
npx c8 report --temp-directory .c8 -o html_coverage -r html

In Code Markup

You can add comments into your source code that tells c8 to ignore certain portions of code.

Ignoring all lines until told to stop.

/* c8 ignore start */
function foo() {
  ...
}
/* c8 ignore stop */

Ignore next line.

/* c8 ignore next */

External References

https://www.npmjs.com/package/c8

https://github.com/bcoe/c8

Git: Checkout a Single File from a Previous Commit

Often we alter or delete a file that we didn’t really want to. Sometimes this goes unnoticed for a few commits. It’s fairly easy in git to retrieve a specific file from a specific commit.

git checkout <commit> <path>
git checkout f08b32 ./src/main.c
git checkout HEAD~2 ./src/main.c

How to reattach a GIT detached head.

$ git status
HEAD detached at 36bc359
nothing to commit, working tree clean

$ git branch fix-detached
$ git checkout main
$ git merge fix-detached -Xtheirs
$ git branch -d fix-detached