🎉 Initial Commit
This commit is contained in:
49
pkg/cmd/main.go
Normal file
49
pkg/cmd/main.go
Normal file
@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
roadsign "code.smartsheep.studio/goatworks/roadsign/pkg"
|
||||
"code.smartsheep.studio/goatworks/roadsign/pkg/configurator"
|
||||
"code.smartsheep.studio/goatworks/roadsign/pkg/hypertext"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func init() {
|
||||
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Configure settings
|
||||
viper.AddConfigPath(".")
|
||||
viper.AddConfigPath("..")
|
||||
viper.SetConfigName("settings")
|
||||
viper.SetConfigType("yaml")
|
||||
|
||||
// Load settings
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
log.Panic().Err(err).Msg("An error occurred when loading settings.")
|
||||
}
|
||||
|
||||
// Load configurations
|
||||
if err := configurator.ReadInConfig(viper.GetString("paths.configs")); err != nil {
|
||||
log.Panic().Err(err).Msg("An error occurred when loading configurations.")
|
||||
} else {
|
||||
log.Debug().Any("sites", configurator.C).Msg("All configuration has been loaded.")
|
||||
}
|
||||
|
||||
// Init hypertext server
|
||||
hypertext.RunServer(hypertext.InitServer())
|
||||
|
||||
log.Info().Msgf("RoadSign v%s is started...", roadsign.AppVersion)
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
|
||||
log.Info().Msgf("RoadSign v%s is quitting...", roadsign.AppVersion)
|
||||
}
|
58
pkg/configurator/main.go
Normal file
58
pkg/configurator/main.go
Normal file
@ -0,0 +1,58 @@
|
||||
package configurator
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var C *AppConfig
|
||||
|
||||
func ReadInConfig(root string) error {
|
||||
cfg := &AppConfig{
|
||||
Sites: []SiteConfig{},
|
||||
}
|
||||
|
||||
if err := filepath.Walk(root, func(fp string, info os.FileInfo, err error) error {
|
||||
var site SiteConfig
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
} else if file, err := os.OpenFile(fp, os.O_RDONLY, 0755); err != nil {
|
||||
return err
|
||||
} else if data, err := io.ReadAll(file); err != nil {
|
||||
return err
|
||||
} else if err := json.Unmarshal(data, &site); err != nil {
|
||||
return err
|
||||
} else {
|
||||
// Extract file name as site id
|
||||
site.ID = strings.SplitN(filepath.Base(fp), ".", 2)[0]
|
||||
|
||||
cfg.Sites = append(cfg.Sites, site)
|
||||
}
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
C = cfg
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SaveInConfig(root string, cfg *AppConfig) error {
|
||||
for _, site := range cfg.Sites {
|
||||
data, _ := json.Marshal(site)
|
||||
|
||||
fp := filepath.Join(root, site.ID)
|
||||
if file, err := os.OpenFile(fp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755); err != nil {
|
||||
return err
|
||||
} else if _, err := file.Write(data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
46
pkg/configurator/struct.go
Normal file
46
pkg/configurator/struct.go
Normal file
@ -0,0 +1,46 @@
|
||||
package configurator
|
||||
|
||||
import "strings"
|
||||
|
||||
type AppConfig struct {
|
||||
Sites []SiteConfig `json:"sites"`
|
||||
}
|
||||
|
||||
type SiteConfig struct {
|
||||
ID string `json:"id"`
|
||||
|
||||
Name string `json:"name"`
|
||||
Rules []RouterRuleConfig `json:"rules"`
|
||||
Upstreams []UpstreamConfig `json:"upstreams"`
|
||||
}
|
||||
|
||||
type RouterRuleConfig struct {
|
||||
Host []string `json:"host"`
|
||||
Path []string `json:"path"`
|
||||
Queries map[string]string `json:"query"`
|
||||
Headers map[string][]string `json:"headers"`
|
||||
}
|
||||
|
||||
const (
|
||||
UpstreamTypeFile = "file"
|
||||
UpstreamTypeHypertext = "hypertext"
|
||||
UpstreamTypeUnknown = "unknown"
|
||||
)
|
||||
|
||||
type UpstreamConfig struct {
|
||||
Name string `json:"name"`
|
||||
URI string `json:"uri"`
|
||||
}
|
||||
|
||||
func (v *UpstreamConfig) GetType() string {
|
||||
protocol := strings.SplitN(v.URI, "://", 2)[0]
|
||||
switch protocol {
|
||||
case "file":
|
||||
return UpstreamTypeFile
|
||||
case "http":
|
||||
case "https":
|
||||
return UpstreamTypeHypertext
|
||||
}
|
||||
|
||||
return UpstreamTypeUnknown
|
||||
}
|
56
pkg/hypertext/init.go
Normal file
56
pkg/hypertext/init.go
Normal file
@ -0,0 +1,56 @@
|
||||
package hypertext
|
||||
|
||||
import (
|
||||
roadsign "code.smartsheep.studio/goatworks/roadsign/pkg"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/limiter"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
"time"
|
||||
)
|
||||
|
||||
func InitServer() *fiber.App {
|
||||
app := fiber.New(fiber.Config{
|
||||
AppName: "RoadSign",
|
||||
ServerHeader: fmt.Sprintf("RoadSign v%s", roadsign.AppVersion),
|
||||
DisableStartupMessage: true,
|
||||
EnableIPValidation: true,
|
||||
EnablePrintRoutes: viper.GetBool("debug.print_routes"),
|
||||
Prefork: viper.GetBool("performance.prefork"),
|
||||
BodyLimit: viper.GetInt("hypertext.limitation.max_body_size"),
|
||||
})
|
||||
|
||||
if viper.GetInt("hypertext.limitation.max_qps") > 0 {
|
||||
app.Use(limiter.New(limiter.Config{
|
||||
Max: viper.GetInt("hypertext.limitation.max_qps"),
|
||||
Expiration: 1 * time.Second,
|
||||
}))
|
||||
}
|
||||
|
||||
UseProxies(app)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func RunServer(app *fiber.App) {
|
||||
for _, port := range viper.GetStringSlice("hypertext.ports") {
|
||||
port := port
|
||||
go func() {
|
||||
if err := app.Listen(port); err != nil {
|
||||
log.Panic().Err(err).Msg("An error occurred when listening hypertext tls ports.")
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
for _, port := range viper.GetStringSlice("hypertext.secured_ports") {
|
||||
port := port
|
||||
pem := viper.GetString("hypertext.certificate.pem")
|
||||
key := viper.GetString("hypertext.certificate.key")
|
||||
go func() {
|
||||
if err := app.ListenTLS(port, pem, key); err != nil {
|
||||
log.Panic().Err(err).Msg("An error occurred when listening hypertext tls ports.")
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
140
pkg/hypertext/proxies.go
Normal file
140
pkg/hypertext/proxies.go
Normal file
@ -0,0 +1,140 @@
|
||||
package hypertext
|
||||
|
||||
import (
|
||||
"code.smartsheep.studio/goatworks/roadsign/pkg/configurator"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/proxy"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/valyala/fasthttp"
|
||||
"math/rand"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func UseProxies(app *fiber.App) {
|
||||
app.All("/", func(ctx *fiber.Ctx) error {
|
||||
host := ctx.Hostname()
|
||||
path := ctx.Path()
|
||||
queries := ctx.Queries()
|
||||
headers := ctx.GetReqHeaders()
|
||||
|
||||
log.Debug().
|
||||
Any("host", host).
|
||||
Any("path", path).
|
||||
Any("queries", queries).
|
||||
Any("headers", headers).
|
||||
Msg("A new request received")
|
||||
|
||||
// Filtering sites
|
||||
for _, site := range configurator.C.Sites {
|
||||
// Matching rules
|
||||
for _, rule := range site.Rules {
|
||||
if !lo.Contains(rule.Host, host) {
|
||||
continue
|
||||
} else if !lo.ContainsBy(rule.Path, func(item string) bool {
|
||||
matched, err := regexp.MatchString(item, path)
|
||||
return matched && err == nil
|
||||
}) {
|
||||
continue
|
||||
}
|
||||
|
||||
flag := true
|
||||
|
||||
// Filter query strings
|
||||
for rk, rv := range rule.Queries {
|
||||
for ik, iv := range queries {
|
||||
if rk != ik && rv != iv {
|
||||
flag = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if !flag {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !flag {
|
||||
continue
|
||||
}
|
||||
|
||||
// Filter headers
|
||||
for rk, rv := range rule.Headers {
|
||||
for ik, iv := range headers {
|
||||
if rk == ik {
|
||||
for _, ov := range iv {
|
||||
if !lo.Contains(rv, ov) {
|
||||
flag = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !flag {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !flag {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !flag {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Passing all the rules means the site is what we are looking for.
|
||||
// Let us respond to our client!
|
||||
return makeResponse(ctx, site)
|
||||
}
|
||||
|
||||
// There is no site available for this request.
|
||||
// Just ignore it and give our client a not found status.
|
||||
// Do not care about the user experience, we can do it in custom error handler.
|
||||
return fiber.ErrNotFound
|
||||
})
|
||||
}
|
||||
|
||||
func doLoadBalance(site configurator.SiteConfig) *configurator.UpstreamConfig {
|
||||
idx := rand.Intn(len(site.Upstreams))
|
||||
|
||||
switch site.Upstreams[idx].GetType() {
|
||||
case configurator.UpstreamTypeHypertext:
|
||||
return &site.Upstreams[idx]
|
||||
case configurator.UpstreamTypeFile:
|
||||
// TODO Make this into hypertext configuration
|
||||
return &site.Upstreams[idx]
|
||||
default:
|
||||
// Give him the null value when this configuration is invalid.
|
||||
// Then we can print a log in the console to warm him.
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func makeRequestUri(ctx *fiber.Ctx, upstream configurator.UpstreamConfig) string {
|
||||
var queries []string
|
||||
for k, v := range ctx.Queries() {
|
||||
queries = append(queries, fmt.Sprintf("%s=%s", k, v))
|
||||
}
|
||||
|
||||
hash := string(ctx.Request().URI().Hash())
|
||||
|
||||
return upstream.URI +
|
||||
lo.Ternary(len(queries) > 0, "?"+strings.Join(queries, "&"), "") +
|
||||
lo.Ternary(len(hash) > 0, "#"+hash, "")
|
||||
}
|
||||
|
||||
func makeResponse(ctx *fiber.Ctx, site configurator.SiteConfig) error {
|
||||
upstream := doLoadBalance(site)
|
||||
if upstream == nil {
|
||||
log.Warn().Str("id", site.ID).Msg("There is no available upstream for this request.")
|
||||
return fiber.ErrBadGateway
|
||||
}
|
||||
|
||||
timeout := time.Duration(viper.GetInt64("performance.network_timeout")) * time.Millisecond
|
||||
return proxy.Do(ctx, makeRequestUri(ctx, *upstream), &fasthttp.Client{
|
||||
ReadTimeout: timeout,
|
||||
WriteTimeout: timeout,
|
||||
})
|
||||
}
|
5
pkg/meta.go
Normal file
5
pkg/meta.go
Normal file
@ -0,0 +1,5 @@
|
||||
package roadsign
|
||||
|
||||
const (
|
||||
AppVersion = "1.0.0"
|
||||
)
|
Reference in New Issue
Block a user