2024-10-27 06:10:27 +01:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Copyright 2024 Jonas Kaninda.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may get a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*/
|
|
|
|
|
import (
|
2024-11-05 10:34:47 +01:00
|
|
|
"crypto/tls"
|
2024-10-27 06:10:27 +01:00
|
|
|
"fmt"
|
2024-10-27 07:57:52 +01:00
|
|
|
"github.com/jedib0t/go-pretty/v6/table"
|
2024-11-05 10:34:47 +01:00
|
|
|
"github.com/jkaninda/goma-gateway/pkg/logger"
|
2024-10-28 02:26:02 +01:00
|
|
|
"net/http"
|
2024-10-27 06:10:27 +01:00
|
|
|
)
|
|
|
|
|
|
2024-11-05 10:34:47 +01:00
|
|
|
// printRoute prints routes
|
2024-10-27 07:57:52 +01:00
|
|
|
func printRoute(routes []Route) {
|
|
|
|
|
t := table.NewWriter()
|
|
|
|
|
t.AppendHeader(table.Row{"Name", "Route", "Rewrite", "Destination"})
|
|
|
|
|
for _, route := range routes {
|
|
|
|
|
t.AppendRow(table.Row{route.Name, route.Path, route.Rewrite, route.Destination})
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(t.Render())
|
|
|
|
|
}
|
2024-11-05 10:34:47 +01:00
|
|
|
|
|
|
|
|
// getRealIP gets user real IP
|
2024-10-28 02:26:02 +01:00
|
|
|
func getRealIP(r *http.Request) string {
|
|
|
|
|
if ip := r.Header.Get("X-Real-IP"); ip != "" {
|
|
|
|
|
return ip
|
|
|
|
|
}
|
|
|
|
|
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {
|
|
|
|
|
return ip
|
|
|
|
|
}
|
|
|
|
|
return r.RemoteAddr
|
|
|
|
|
}
|
2024-11-05 10:34:47 +01:00
|
|
|
|
|
|
|
|
// loadTLS loads TLS Certificate
|
|
|
|
|
func loadTLS(cert, key string) (*tls.Config, error) {
|
|
|
|
|
if cert == "" && key == "" {
|
|
|
|
|
return nil, fmt.Errorf("no certificate or key file provided")
|
|
|
|
|
}
|
|
|
|
|
serverCert, err := tls.LoadX509KeyPair(cert, key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error("Error loading server certificate: %v", err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
|
Certificates: []tls.Certificate{serverCert},
|
|
|
|
|
}
|
|
|
|
|
return tlsConfig, nil
|
|
|
|
|
}
|