Skip to content

Instantly share code, notes, and snippets.

View ankur22's full-sized avatar

Ankur ankur22

View GitHub Profile
@ankur22
ankur22 / iptables.sh
Last active January 1, 2023 03:16
This lists rules to lock down network with iptables to the specified ports
#!/bin/bash
# OpenVPN Server
iptables -t nat -I POSTROUTING 1 -s 10.8.0.0/24 -o eth0 -j MASQUERADE
iptables -I INPUT 1 -i tun0 -j ACCEPT
iptables -I FORWARD 1 -i eth0 -o tun0 -j ACCEPT
iptables -I FORWARD 1 -i tun0 -o eth0 -j ACCEPT
iptables -I INPUT 1 -i eth0 -p udp --dport 1194 -j ACCEPT
iptables -I OUTPUT 1 -o eth0 -p udp --sport 1194 -j ACCEPT
@ankur22
ankur22 / isoTime.go
Created March 7, 2022 10:50
Parse an iso timestamp from json straight into a time type
package main
import (
"encoding/json"
"fmt"
"time"
)
type someType struct {
Time *time.Time `json:"time"`
@ankur22
ankur22 / set_timeout.go
Created March 4, 2022 11:11
Checking whether the underlying client and context have a context and setting one on the context if no timeout set in either
// Set a timeout of 10secs if no timeout in the underlying
// http client and no timeout set in the context.
_, ok := ctx.Deadline()
if c.c.Timeout == 0 && !ok {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Second*10)
defer cancel()
}
@ankur22
ankur22 / goRESTHelloWorldMain.go
Last active April 24, 2021 11:16
Hello world for rest service in go
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
@ankur22
ankur22 / structEmbeddingSameFields.go
Created May 30, 2020 14:21
Example of embedded structs with overlapping string fields
type logger struct {
s string
s2 string
}
func (l *logger) do(in string) {
log.Println(in)
}
type checker struct {
@ankur22
ankur22 / structEmbeddingOwnField.go
Created May 30, 2020 14:06
Example of an embedded struct with its own field too
type loggerAndChecker struct {
*logger
*checker
s string
}
@ankur22
ankur22 / interfaceEmbeddingMoreFunc.go
Created May 30, 2020 14:02
Example of a embedded interface which defines its own functions too
type doerAndValidator interface {
doer
validator
doSomething(string)
}
@ankur22
ankur22 / structEmbeddingImplSameFunc.go
Created May 30, 2020 13:53
Example of struct embedding where there's two functions with same name, so we override that function
type logger struct{}
func (l *logger) do(in string) {
if l.validate(in) {
log.Println(in)
}
}
func (l *logger) validate(in string) bool {
if len(in) == 0 {
@ankur22
ankur22 / structEmbeddingSameFunc.go
Last active May 30, 2020 13:45
Example of a embedded structs where the structs implement the same func
type logger struct{}
func (l *logger) do(in string) {
if l.validate(in) {
log.Println(in)
}
}
func (l *logger) validate(in string) bool {
if len(in) == 0 {
@ankur22
ankur22 / notStructEmbedding.go
Created May 30, 2020 13:36
An example of a type which does not embed the struct but declares fields instead
type loggerAndChecker struct {
l *logger
c *checker
}
func main() {
var lc loggerAndChecker
in := "Hello World"
if lc.c.validate(in) {
lc.l.do(in)