Ver código fonte

commiting before the server blows up lol

svcs 1 ano atrás
pai
commit
1545137b89
3 arquivos alterados com 73 adições e 53 exclusões
  1. 8 1
      cmd/route.go
  2. 2 1
      pkg/include.go
  3. 63 51
      pkg/server.go

+ 8 - 1
cmd/route.go

@@ -27,10 +27,17 @@
 package main
 
 import (
+	"log"
+	"os"
+
 	httpserver "git.aetherial.dev/aeth/http-proxy/pkg"
 )
 
 func main() {
-	httpserver.AddCookiePrompt()
+	cfg, err := httpserver.ReadConfig(os.Args[1])
+	if err != nil {
+		log.Fatal("couldnt read the config file", err)
+	}
+	httpserver.AddCookiePrompt(cfg)
 
 }

+ 2 - 1
pkg/include.go

@@ -51,6 +51,7 @@ type HttpServerConfig struct {
 	ProxyAddr            string   `json:"proxy_addr"`
 	RouteMapPath         string   `json:"route_map_path"`
 	PageModPath          string   `json:"page_mod_path"`
+	CookieFile           string   `json:"cookie_file"`
 	FullProxyDomain      string   // the domain name of the proxied site with the protocol
 	KnownHosts           []string `json:"known_hosts"`
 	CookieJar            []*http.Cookie
@@ -162,7 +163,7 @@ func ReadConfig(loc string) (*HttpServerConfig, error) {
 	if err != nil {
 		return nil, err
 	}
-	cf, err := os.ReadFile("./cookies.json")
+	cf, err := os.ReadFile(cfg.CookieFile)
 	if err != nil {
 		return nil, err
 	}

+ 63 - 51
pkg/server.go

@@ -28,10 +28,12 @@ package httpserver
 
 import (
 	"bufio"
+	"encoding/json"
 	"fmt"
-	"io"
 	"log"
+	"net/http"
 	"os"
+	"strconv"
 	"strings"
 )
 
@@ -45,58 +47,21 @@ var prompts = [...]string{
 	"Include it in subdomains? (default=y) y/n: ",
 }
 
-type CookiePrompt struct {
-	Name   map[string]string
-	Value  map[string]string
-	Age    map[string]string
-	Uri    map[string]string
-	Domain map[string]string
-	Https  map[string]bool
-	Subd   map[string]bool
-}
-
-func NewCookiePrompt() *CookiePrompt {
-	return &CookiePrompt{
-		Name: map[string]string{
-			"Name of the cookie: ": "",
-		},
-		Value: map[string]string{
-			"Cookie value: ": "",
-		},
-		Age: map[string]string{
-			"Max age of the cookie (default=0, for no expiry): ": "0",
-		},
-		Uri: map[string]string{
-			"URI path to include it for (default=/): ": "/",
-		},
-		Domain: map[string]string{
-			"Domain that the cookie belongs to: ": "",
-		},
-		Https: map[string]bool{
-			"HTTPS only? (default yes) y/n: ": true,
-		},
-		Subd: map[string]bool{
-			"Include it in subdomains? (default=y) y/n: ": true,
-		},
-	}
-}
-
-func (c *CookiePrompt) SetName(reader *io.Reader) {
-	fmt.Print(c.Name[0])
-	ans, err := reader.ReadString('\n')
-	if err != nil {
-		log.Fatal("couldnt read input: ", err)
-	}
-}
-
 /*
 Open an interactive prompt for a user to save cookies to
 */
-func AddCookiePrompt() {
-	answers := NewCookiePrompt()
+func AddCookiePrompt(path string, cookies []*http.Cookie) {
+	var answers []string
+	var https bool
+	var subd bool
+	var name string
+	var value string
+	var domain string
+	maxAge := 0
+	path := "/"
 	reader := bufio.NewReader(os.Stdin)
 	for idx := range prompts {
-		fmt.Print()
+		fmt.Print(prompts[idx])
 		ans, err := reader.ReadString('\n')
 		if err != nil {
 			log.Fatal("Fatal error when reading cookie creation input: ", err)
@@ -104,11 +69,58 @@ func AddCookiePrompt() {
 		ans = strings.TrimSuffix(ans, "\n")
 		if prompts[idx] == "HTTPS only? (default yes) y/n: " {
 			if ans == "y" {
-
+				https = true
+			}
+			if ans == "n" {
+				https = false
 			}
 		}
+		if prompts[idx] == "Max age of the cookie (default=0, for no expiry): " {
+			if ans != "" {
+				maxAge, err = strconv.Atoi(answers[idx])
+				if err != nil {
+					log.Fatal("Not an integer!")
+				}
+			}
+		}
+		if prompts[idx] == "Include it in subdomains? (default=y) y/n: " {
+			if ans == "y" {
+				subd = true
+			}
+			if ans == "n" {
+				subd = false
+			}
+		}
+		if prompts[idx] == "URI path to include it for (default=/): " {
+			if ans != "" {
+				path = prompts[idx]
+			}
+		}
+		if prompts[idx] == "Name of the cookie: " {
+			name = ans
+		}
+		if prompts[idx] == "Value to supply it: " {
+			value = ans
+		}
+		if prompts[idx] == "Domain that the cookie belongs to:  " {
+			domain = ans
+		}
+
+	}
+	ck := &http.Cookie{
+		Name:   name,
+		Value:  value,
+		MaxAge: maxAge,
+		Path:   path,
+		Domain: domain,
+		Secure: https,
 	}
-	for i := range answers {
-		fmt.Println(answers[i])
+	cookies = append(cookies, ck)
+	b, err := json.Marshal(cookies)
+	if err != nil {
+		log.Fatal("couldnt marshal the cookie: ", err)
 	}
+
+	os.WriteFile(path, b, os.ModePerm)
+
 }