Add config File and example conf

This commit is contained in:
Pablu23
2024-06-09 20:36:30 +02:00
parent 49a21c4233
commit 7f8718d609
2 changed files with 46 additions and 7 deletions

3
domains.conf Normal file
View File

@@ -0,0 +1,3 @@
test.localhost;8181
test2.localhost;8282
localhost;8080

50
main.go
View File

@@ -1,17 +1,28 @@
package main package main
import ( import (
"bufio"
"errors" "errors"
"flag"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"os"
"strconv"
"strings"
)
var (
configFileFlag = flag.String("config", "domains.conf", "Path to Domain config file")
) )
func main() { func main() {
domains := make(map[string]int) flag.Parse()
domains["test.localhost"] = 8181 domains, err := loadConfig(*configFileFlag)
domains["test2.localhost"] = 8282 if err != nil {
panic(err)
}
client := &http.Client{ client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error { CheckRedirect: func(req *http.Request, via []*http.Request) error {
@@ -21,10 +32,10 @@ func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
port, ok := domains[r.Host] port, ok := domains[r.Host]
if !ok { if !ok {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
return return
} }
rDump, err := httputil.DumpRequest(r, true) rDump, err := httputil.DumpRequest(r, true)
if err != nil { if err != nil {
@@ -104,3 +115,28 @@ func main() {
fmt.Println("Starting server on :443") fmt.Println("Starting server on :443")
http.ListenAndServeTLS(":443", "server.crt", "server.key", nil) http.ListenAndServeTLS(":443", "server.crt", "server.key", nil)
} }
func loadConfig(path string) (map[string]int, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
m := make(map[string]int)
for scanner.Scan() {
line := scanner.Text()
params := strings.Split(line, ";")
if len(params) <= 1 {
return nil, errors.New("Line does not contain enough Parameters")
}
port, err := strconv.Atoi(params[1])
if err != nil {
return nil, err
}
m[params[0]] = port
}
return m, nil
}