# README
WebExt
A collection of website middleware to extend basic tasks for gofiber.
Note: this module assumes you are using gofiber/v2
Installation
go get github.com/AspieSoft/webext
# dependencies
go get github.com/gofiber/fiber/v2
Usage
package main
import (
"github.com/AspieSoft/webext"
"github.com/gofiber/fiber/v2"
)
func main(){
app := fiber.New()
origins := []string{
"localhost",
"example.com",
}
proxies := []string{
"127.0.0.1",
"192.168.0.1",
}
// enforce specific domain and ip origins
app.Use(webext.VerifyOrigin(origins, proxies, func(c *fiber.Ctx, err error) error {
c.SendStatus(403)
return c.SendString(err.Error())
}))
// auto redirect http to https
app.Use(webext.RedirectSSL(8080, 8443))
// do anything with gofiber
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
// listen to both http and https ports and
// auto generate a self signed ssl certificate
// (will also auto renew every year)
webext.ListenAutoTLS(app, 8080, 8443, "db/ssl/auto_ssl", proxies)
// by using self signed certs, you can use a proxy like cloudflare and
// not have to worry about verifying a certificate athority like lets encrypt
}
# Functions
DelCron removes a named cron job.
GenRsaKey generates a new ssl certificate and key pair - expires: 3 years - rsa: 4096 - x509 - sha256 - recommended renewal: once a year.
GenRsaKeyIfNeeded auto detects if the certificates generated by the GenRsaKey method are either - not synchronized by date modified - are possibly expired (assuming a 1 year renewal) If it detects this is true, it will automatically regenerate a new certificate.
GetLoginSession will populate c.Locals("uuid") with a user uuid if a login session is verified.
HasCron checks if a named cron job exists.
ListenAutoTLS will automatically generate a self signed tls certificate if needed and listen to both http and https ports
@httpPort: 80, @sslPort: 443
@certPath: file path to store ssl certificates to (this will generate a my/path.crt and my/path.key file)
@proxy: optional, if only one proxy is specified, the app will only listen to that ip address.
NewCron adds a new, unnamed cron job to the queue
minimum interval: 1 minute
in the callback, return true to keep the job running, and return false to end the job.
PrintMsg prints to console and auto inserts spaces.
RedirectSSL can be added to `app.Use` to auto redirect http to https
@httpPort: 80, @sslPort: 443.
SetCron adds or overwrites a named cron job.
TryPerm attempts to set a directory permission to @perm only if it can access that directory
if it fails due to permission restrictions, and if IsRoot returns false, it will instead return @nonrootPerm as a fallback.
VerifyLogin will verify if a user is loggedin or present them with a login form on GET requests.
VerifyOrigin can be added to `app.Use` to enforce that all connections are coming through a specified domain and proxy ip
@origin: list of valid domains
@proxy: list of valid ip proxies
@handleErr: optional, allows you to define a function for handling invalid origins, instead of returning the default http error.