first commit

This commit is contained in:
2021-11-10 20:06:12 +01:00
commit 2fcfa91545
29 changed files with 2724 additions and 0 deletions

3
internal/core/version.go Normal file
View File

@ -0,0 +1,3 @@
package core
var Version = "tobedefined"

View File

@ -0,0 +1,66 @@
package httputils
import (
"fmt"
"net/http"
"sort"
"strings"
"github.com/gin-gonic/gin"
)
func HeadersToSortedString(headers http.Header) string {
var output string
keys := make([]string, 0, len(headers))
for k := range headers {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
if len(headers[k]) > 1 {
for _, h := range headers[k] {
output += k + ": " + h + "\n"
}
} else {
output += k + ": " + headers[k][0] + "\n"
}
}
return output
}
func GetLogFormatter(param gin.LogFormatterParams) string {
return fmt.Sprintf("%s - [%s] \"%s %s %s\" %d %d %d %s \"%s\" \"%s\" \"%s\"\n",
param.ClientIP,
param.TimeStamp.Format("02/Nov/2006:15:04:05 -0700"),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.BodySize,
param.Latency.Nanoseconds(),
normalizeLog(param.Request.Referer()),
normalizeLog(param.Request.UserAgent()),
normalizeLog(param.Request.Header["X-Forwarded-For"]),
normalizeLog(param.ErrorMessage),
)
}
func normalizeLog(log interface{}) interface{} {
switch v := log.(type) {
case string:
if v == "" {
return "-"
}
case []string:
if len(v) == 0 {
return "-"
} else {
return strings.Join(v, ", ")
}
}
return log
}

View File

@ -0,0 +1,61 @@
package httputils
import (
"net/http"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestHeaderToString(t *testing.T) {
expected := `Header1: One
Header2: 2
Header2: Two
Header3: Three
`
header := http.Header{
"Header2": []string{"2", "Two"},
"Header1": []string{"One"},
"Header3": []string{"Three"},
}
assert.Equal(t, expected, HeadersToSortedString(header))
}
func TestGetLogFormatter(t *testing.T) {
expected := "127.0.0.1 - [01/Nov/0001:00:00:00 +0000] \"GET / HTTP/1.1\" 200 100 1000 local \"golang test 1.0\" \"1.1.1.1, 2.2.2.2\" \"-\"\n"
h := http.Header{}
h.Set("User-Agent", "golang test 1.0")
h.Set("Referer", "local")
h.Set("X-Forwarded-For", "1.1.1.1, 2.2.2.2")
r := http.Request{
Proto: "HTTP/1.1",
Header: h,
}
p := gin.LogFormatterParams{
ClientIP: "127.0.0.1",
TimeStamp: time.Time{},
Method: "GET",
Path: "/",
StatusCode: 200,
BodySize: 100,
Latency: 1000,
ErrorMessage: "",
Request: &r,
}
assert.Equal(t, expected, GetLogFormatter(p))
}
func TestNormalizeLog(t *testing.T) {
assert.Equal(t, "-", normalizeLog(""))
assert.Equal(t, "string", normalizeLog("string"))
assert.Equal(t, "-", normalizeLog([]string{}))
assert.Equal(t, "1.1.1.1", normalizeLog([]string{"1.1.1.1"}))
assert.Equal(t, "1.1.1.1, 2.2.2.2", normalizeLog([]string{"1.1.1.1", "2.2.2.2"}))
}

87
internal/setting/app.go Normal file
View File

@ -0,0 +1,87 @@
package setting
import (
"flag"
"fmt"
"os"
"time"
"github.com/dcarrillo/whatismyip/internal/core"
)
type geodbPath struct {
City string
ASN string
}
type serverSettings struct {
ReadTimeout time.Duration
WriteTimeout time.Duration
}
type settings struct {
GeodbPath geodbPath
TemplatePath string
BindAddress string
TLSAddress string
TLSCrtPath string
TLSKeyPath string
TrustedHeader string
Server serverSettings
}
const defaultAddress = ":8080"
var App *settings
func Setup() {
city := flag.String("geoip2-city", "", "Path to GeoIP2 city database")
asn := flag.String("geoip2-asn", "", "Path to GeoIP2 ASN database")
template := flag.String("template", "", "Path to template file")
address := flag.String("bind", defaultAddress, "Listening address (see https://pkg.go.dev/net?#Listen)")
addressTLS := flag.String("tls-bind", "", "Listening address for TLS (see https://pkg.go.dev/net?#Listen)")
tlsCrtPath := flag.String("tls-crt", "", "When using TLS, path to certificate file")
tlsKeyPath := flag.String("tls-key", "", "When using TLS, path to private key file")
trustedHeader := flag.String("trusted-header", "", "Trusted request header for remote IP (e.g. X-Real-IP)")
ver := flag.Bool("version", false, "Output version information and exit")
flag.Parse()
if *ver {
fmt.Printf("whaismyip version %s", core.Version)
os.Exit(0)
}
if *city == "" || *asn == "" {
exitWithError("geoip2-city and geoip2-asn parameters are mandatory")
}
if (*addressTLS != "") && (*tlsCrtPath == "" || *tlsKeyPath == "") {
exitWithError("In order to use TLS -tls-crt and -tls-key flags are mandatory")
}
if *template != "" {
info, err := os.Stat(*template)
if os.IsNotExist(err) || info.IsDir() {
exitWithError(*template + " doesn't exist or it's not a file")
}
}
App = &settings{
GeodbPath: geodbPath{City: *city, ASN: *asn},
TemplatePath: *template,
BindAddress: *address,
TLSAddress: *addressTLS,
TLSCrtPath: *tlsCrtPath,
TLSKeyPath: *tlsKeyPath,
TrustedHeader: *trustedHeader,
Server: serverSettings{
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
},
}
}
func exitWithError(error string) {
fmt.Printf("%s\n\n", error)
flag.Usage()
os.Exit(1)
}