Iptables port forwarding

iptables -t nat -I OUTPUT -p tcp -o lo --dport 80 -j REDIRECT --to-ports 8080
7 ноября 2019, 21:00

Linux get mac address

ifconfig -a | grep ether | awk '{print $2}'
15 октября 2019, 12:34

NodeJs natsort with Intl

const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
array.sort(collator.compare);
13 сентября 2019, 16:10

Install JIRA 8 on docker

version: '3.1'
services:
  jira:
    image: zhangguanzhang/atlassian-jira-software:8.0.0
    container_name: jira8
    restart: always
    environment:
      - "CATALINA_OPTS= -Xms1024m -Xmx4096m"
    links:
      - database
    volumes:
      - /home/jira8.0/data:/var/atlassian/jira
      - /home/jira8.0/logs:/opt/atlassian/jira/logs
      - /etc/localtime:/etc/localtime:ro
    ports:
      - '80:8080'

  database:
    restart: always
    container_name: jira8db
    image: percona:5.7
    environment:
      - MYSQL_DATABASE=jira
      - MYSQL_USER=jira
      - MYSQL_PASSWORD=jira
      - MYSQL_ROOT_PASSWORD=jira
    volumes:
      - /home/jira8.0/db:/var/lib/mysql
    ports:
      - "127.0.0.1:3306:3306"
    command:
      - '--collation-server=utf8_bin'
      - '--character-set-server=utf8'
      - '--max_allowed_packet=34M'
      - '--innodb_log_file_size=256M'
13 сентября 2019, 14:39

Rebuild all containers in docker-compose

 docker-compose up -d --force-recreate --build
11 сентября 2019, 11:43

Websocket broadcasting

Broadcasting All
const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(data) {
    wss.clients.forEach(function each(client) {
      if (client.readyState === WebSocket.OPEN) {
        client.send(data);
      }
    });
  });
});
Broadcasting excluding itself.
const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(data) {
    wss.clients.forEach(function each(client) {
      if (client !== ws && client.readyState === WebSocket.OPEN) {
        client.send(data);
      }
    });
  });
});
7 сентября 2019, 18:09

FadeIn without Jquery

el.classList.add('show');
el.classList.remove('hide');
.show {
  transition: opacity 400ms;
}
.hide {
  opacity: 0;
}
source
7 сентября 2019, 17:53

FFMPEG playlist

Create file playlist.txt
file '/path/to/video/video.mp4'
file '/path/to/video/video.mp4'
file '/path/to/video/video.mp4'
file '/path/to/video/video.mp4'
file '/path/to/video/video.mp4'
Run ffmpeg
ffmpeg -f concat -i playlist.txt -c copy output.mp4
7 сентября 2019, 17:50

Get docker host internal ip

docker inspect <container-id-or-name> | grep Gateway
7 сентября 2019, 10:08

Map, for, spread javascript benchmark for copy array

copy for: 2508.226ms
copy map: 1188.982ms
copy spread: 1898.890ms
const map = [
  [1, 2, 3, 4, 5],
  [6, 7, 8, 9, 10],
  [11, 12, 13, 14, 15]
];
const n  = 10e6;

console.time('copy for');
for (let i = 0; i < n; i++) {
  const m = [];
  for (let row = 0; row < map.length; row++) {
    m[row] = [];
    for (let reel = 0; reel < map[row].length; reel++) {
      m[row][reel] = map[row][reel];
    }
  }
}
console.timeEnd('copy for');

console.time('copy map');
for (let i = 0; i < n; i++) {
  const m = map.map(a => a.map(item => item));
}
console.timeEnd('copy map');

console.time('copy spread');
for (let i = 0; i < n; i++) {
  const m = map.map(a => [...a]);
}
console.timeEnd('copy spread');
13 августа 2019, 13:29

Javascript chrome full screen mode

document.documentElement.webkitRequestFullscreen();
25 июля 2019, 10:20

Javascript break(2)

loop:
for(let i=0; i<10; i++) {
  for(let j=0; j<10; j++){
    console.log(i,j);
    if(j === 5) break loop;
  }
}
22 июля 2019, 15:22

Nodejs clone object

const v8 = require('v8');

const structuredClone = obj => {
  return v8.deserialize(v8.serialize(obj));
};
19 июля 2019, 09:53

Install docker on Ubuntu

sudo apt-get update

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io

17 июня 2019, 16:31

Install node11 on Ubuntu

Node.js v11.x:
# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
sudo apt-get install -y nodejs

17 июня 2019, 15:50

Laravel persistent connection to mysql

database.php
'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
            'options' => [
                \PDO::ATTR_PERSISTENT => true
            ]
        ],
6 июня 2019, 16:51

Recommended innodb pool size

SELECT CONCAT(ROUND(KBS/POWER(1024, 
IF(PowerOf1024<0,0,IF(PowerOf1024>3,0,PowerOf1024)))+0.49999), 
SUBSTR(' KMG',IF(PowerOf1024<0,0, 
IF(PowerOf1024>3,0,PowerOf1024))+1,1)) recommended_innodb_buffer_pool_size 
FROM (SELECT SUM(data_length+index_length) KBS FROM information_schema.tables 
WHERE engine='InnoDB') A, 
(SELECT 2 PowerOf1024) B; 
5 июня 2019, 10:19

Increase maxproc on OSX

sudo nano /Library/LaunchDaemons/limit.maxproc.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN"
          "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
      <string>limit.maxproc</string>
    <key>ProgramArguments</key>
      <array>
        <string>launchctl</string>
        <string>limit</string>
        <string>maxproc</string>
        <string>2048</string>
        <string>2048</string>
      </array>
    <key>RunAtLoad</key>
      <true />
    <key>ServiceIPC</key>
      <false />
  </dict>
</plist>
sudo chown root:wheel /Library/LaunchDaemons/limit.maxproc.plist
15 мая 2019, 12:24

Increase ulimit on OSX

sudo nano /Library/LaunchDaemons/limit.maxfiles.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
          "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>limit.maxfiles</string>
    <key>ProgramArguments</key>
    <array>
      <string>launchctl</string>
      <string>limit</string>
      <string>maxfiles</string>
      <string>524288</string>
      <string>524288</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceIPC</key>
    <false/>
  </dict>
</plist>
sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist
reboot
ulimit -n
15 мая 2019, 12:22

Resize /dev/shm

shm
To increase or decrease /dev/shm filesystem size

1) Open /etc/fstab with vi or any text editor of your choice,

2) Locate the line of /dev/shm and use the tmpfs size option to specify your expected size,

e.g. 512MB:
tmpfs      /dev/shm      tmpfs   defaults,size=512m   0   0

e.g. 2GB:
tmpfs      /dev/shm      tmpfs   defaults,size=2g   0   0

The /etc/fstab content format is documented in man fstab and the tmpfs filesystem options can be found in man mount

3) To make change effective immediately, run this mount command to remount the /dev/shm filesystem:
mount -o remount /dev/shm

12 февраля 2019, 22:00