A collection of my gists, quick guides, and snippets curated over time.
November 25, 2024
// 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...
November 22, 2024
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 {
...
July 18, 2022
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
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()
February 7, 2022
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.
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.
$ sudo apt udapte
$ sudo apt install fail2ban
Check status of fail2ban
$ sudo systemctl status fail2ban
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
ipignore = 192.168.43.1 // These IPs are ignored from banlist
bantime = 1d ...
January 10, 2022
$ 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
/etc/netplan/00-installer-config.yaml$ sudo vim 00-installer-config.yaml
network:
ethernets:
ens00:
dhcp4: true
ens01:
dhcp4: true
version: 2
$ netplan apply
July 16, 2021
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.
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 ...
May 25, 2021
Image Conversion
This script will traverse your filesystem and convert all Images to desired format.
REQUIRED PARAMETERS
Source directory path. (This is the path from where images will be converted)
Output format. default = .jpg
Note - All the converted images will be stored in a new directory in the source directory named as output
May 18, 2021
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...
March 23, 2020
$ sudo apt install apt-transport-https ca-certificates curl 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 bionic stable"
$ sudo apt install docker-ce
$ sudo systemctl status docker
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
$ sudo usermod -aG docker username