← Back to Home

Informatics (My Brain)

A collection of my gists, quick guides, and snippets curated over time.

// import {SESClient, SendEmailCommand} from '@aws-sdk/client-ses';
const AWS = require("/var/runtime/node_modules/aws-sdk/lib/aws.js")

exports.handler = async event => {
  const ses = new AWS.SES()
  const emailBody =
    "<div style='font-family: Helvetica,Arial,sans-serif;min-width:1000px;overflow:auto;line-height:2'> <div style='margin:50px auto;width:70%;padding:20px 0'> <div style='border-bottom:1px solid #eee'> <a href='' style='font-size:1.4em;color: #00466a;text-decoration:none;font-weight:600'>Your Brand</a> </div> <p style='font-size:1.1em'>Hi,</p> <p>Thank you for choosing Your Brand. Use the following OTP to complete your Sign Up procedures. OTP is valid for 5 minutes</p> <h2 style='background: #00466a;margin: 0 auto;width: max-content;padding: 0 10px;color: #fff;border-radius: 4px;'>324457</h2> <p style='font-size:0.9em;'>Regards,<br />Your Brand</p> <hr style='border:none;border-top:1px solid #eee' /> <div style='float:right;padding:8px 0;color:#aaa;font-s...
package sub

import (
	"config/constants"
	"encoding/json"
	"fmt"
	"sync"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/sqs"
)

type Consumer struct {
	sqs         *sqs.SQS
	mux         *sync.Mutex
	threadCount int
}

func NewConsumer() Consumer {
	sess := session.Must(session.NewSessionWithOptions(session.Options{
		Config: aws.Config{
			Credentials: credentials.NewEnvCredentials(),
			Region:      aws.String(constants.AWS_REGION),
		},
	}))

	return Consumer{
		sqs:         sqs.New(sess),
		mux:         &sync.Mutex{},
		threadCount: 0,
	}
}

func (c *Consumer) Consume() {
	queue := &sqs.ReceiveMessageInput{
		QueueUrl:            aws.String(constants.QUEUE_URL),
		MaxNumberOfMessages: aws.Int64(constants.FIFO_QUEUE_BATCH_SIZE), // Do not increase, this is directly proportional to number of goroutines
	}

	for {
		if c.threadCount >= constants.MAX_CONSUMER_LISTENERS {
	...

Add Alias in Next.js Imports

Update your tsconfig.json or jsconfig.json

"compilerOptions": {
  ...
  "baseUrl": ".",
  "paths": {
    "@alias": ["components"],
    "@alias/*": ["components/*"]
  }
}

Now, restart you TS server

On Mac Cmd+Shift+P > Typescript: Restart TS Server

On PC Ctrl+Shift+P > Typescript: Restart TS Server

RabbitMQ Pub-Sub

May 2, 2022

const amqp = require('amqplib')

const connect = async () => {
  try {
    const connection = await amqp.connect('amqp://localhost:5672')
    const channel = await connection.createChannel()

    const result = await channel.assertQueue('jobs')

    channel.consume('jobs', (message) => {
      const data = JSON.parse(message.content.toString())
      console.log(data.number)

      if (data.number == 10) {
        console.log(`Consumed ${data.number}`)
        channel.ack(message)
      }
    })

    return console.log('Waiting for Messages')
  } catch (err) {
    console.log(err)
  }
}

connect()

Installing fail2ban on Debian Based Systems

Fail2Ban prevents linux server from brute force ssh attacks. It bans the IP Address from where multiple failed login attemps are made withing short duration of time.

How does it work

fail2ban reads /var/log/auth.log file to gether all the IP Addresses which have made failed login attemps to the server. Based on this data fail2ban creates a ban list.

Installation

$ sudo apt udapte
$ sudo apt install fail2ban

Check status of fail2ban

$ sudo systemctl status fail2ban

Configuration

Fail2ban comes with some configuration files, which we will not edit directly as they might get overwritten when the package is updated.

Hence, we create a copy of config files:

$ sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
$ sudo vim /etc/fail2ban/jail.local

Configuration Options

ipignore = 192.168.43.1      // These IPs are ignored from banlist
bantime = 1d                 ...

linux-networking

January 10, 2022

Adding New Network Interface in Ubuntu

1. Check if the network adapter is connected

$ ifconfig -a
ens00: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.1  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:feb8:214e  prefixlen 64  scopeid 0x20<link>

ens01: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.74.1  netmask 255.255.255.0  broadcast 192.168.74.255
        inet6 fe80::20c:29ff:feb8:2158  prefixlen 64  scopeid 0x20<link>

NOTE: To list all the connected network adapters run: ls /sys/class/net

2. Add the config of new interface into /etc/netplan/00-installer-config.yaml

$ sudo vim 00-installer-config.yaml
network:
  ethernets:
     ens00:
        dhcp4: true
     ens01:
        dhcp4: true
version: 2

3. Apply the changes

$ netplan apply

Allow Remote Access to MySQL

Edit Configurations

First thing that needs to be done is to allow connections from all hosts to MySQL. mysqld.cnf file needs to updated as follows.

$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Update the value to bind-address which by default will be 127.0.0.1. Which means only local host connect to MySQL.

bind-address      =  0.0.0.0

After updating this, restart MySQL server using the following command.

$ sudo systemctl restart mysql

Now, MySQL will allow connections from any host. But yet we need to do some more configurations to actually connect to MySQL from a remote server.

Create User Account

We need an account which will connect to MySQL Server remotely. If already created an account you will need to reconfigure that account to connect from the remote server instead of localhost.

Login to MySQL

$ mysql -u root -p

If you are updating an existing account ...

Image Conversion

This script will traverse your filesystem and convert all Images to desired format.

REQUIRED PARAMETERS

  1. Source directory path. (This is the path from where images will be converted)

  2. Output format. default = .jpg

Note - All the converted images will be stored in a new directory in the source directory named as output

const crypto = require('crypto')
const express = require('express')
const bodyParser = require('body-parser')

const app = express()

// To include rawBody in req parameter
app.use(bodyParser.json({
  verify: (req, res, buf) => {
    req.rawBody = buf
  }
}))

app.post('/github', (req, res) => {
  const secret = "YOUR_SECRET_HERE"
  
  const signature = Buffer.from('sha256=' + crypto
          .createHmac('sha256', secret)
          .update(req.rawBody)
          .digest('hex'), 'utf-8')
  
  // For secure compare use a secure comaprision method 
  if(signature.length !== req.headers['x-hub-signature-256'].length ||
          req.headers['x-hub-signature-256'] !==  signature.toString()){
          console.log("Invalid Signature");
          return res.status(401).end();
  }

  return res.status(200).send('Authentication complete')
})

app.listen(5000, () => console.log("Server running on port 5000"))


/**
  References
  1. https://www.digitalocean.com/community/tutorials...

install_docker_ce.md

March 23, 2020

INSTALL DOCKER CE ON LINUX

Install Prerequisite Packages (optional)

$ sudo apt install apt-transport-https ca-certificates curl software-properties-common

Add Docker GPG Key

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

Add Docker Repository

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

Install Docker

$ sudo apt install docker-ce
$ sudo systemctl status docker

Add Docker as Sudo

To Execute the Docker Command Without Sudo:

$ sudo usermod -aG docker ${USER}

Apply user group changes using:

$ su - ${USER}

To confirm that docker has been added to user group use:

$ id -nG

Add docker to as sudo for a different user

$ sudo usermod -aG docker username
Page 1Next →