Explorar o código

trying to clean up

aeth hai 8 meses
pai
achega
2b18c8ae5c
Modificáronse 4 ficheiros con 19 adicións e 22 borrados
  1. 4 4
      cmd/http-wokou/http-wokou.go
  2. 3 1
      pkg/client.go
  3. 3 2
      pkg/configuration.go
  4. 9 15
      pkg/controller.go

+ 4 - 4
cmd/http-wokou/http-wokou.go

@@ -60,7 +60,9 @@ func main() {
 		fmt.Println(redistMsg)
 		os.Exit(0)
 	}
-	fmt.Println(licenseMsg)
+	numFlags := flag.NFlag()
+	proxyDomain := os.Args[numFlags+1]
+	targetDomain := os.Args[numFlags+2]
 
 	if *makeConfig {
 		out := httpserver.HttpServerConfig{}
@@ -74,15 +76,13 @@ func main() {
 		os.WriteFile("./routemap.json", b, os.ModePerm)
 	}
 
-	cfg, err := httpserver.ReadConfig(*configFile)
+	cfg, err := httpserver.ReadConfig(*configFile, proxyDomain, targetDomain)
 	if err != nil {
 		log.Fatal("Couldnt read config: ", err)
 	}
 	cfg.Caching = *caching
 	e := gin.Default()
 	e.SetTrustedProxies(nil)
-	//config := cors.DefaultConfig()
-	//e.Use(cors.New(config))
 	httpserver.RegisterRoutes(e, cfg)
 	e.Run(fmt.Sprintf("%s:%v", cfg.ListeningIp, cfg.HttpPort))
 }

+ 3 - 1
pkg/client.go

@@ -27,6 +27,7 @@
 package httpserver
 
 import (
+	"bytes"
 	"fmt"
 	"io"
 	"log"
@@ -46,7 +47,7 @@ type ResponseCarrier struct {
 Generic site call to an upstream server
 */
 func (c *Controller) RequestGeneric(method string, host string, path string, hdrs *http.Header, body io.Reader) ResponseCarrier {
-	reqUrl := fmt.Sprintf("http://%s%s", host, path)
+	reqUrl := fmt.Sprintf("%s://%s%s", "http", host, path)
 	req, err := http.NewRequest(method, reqUrl, body)
 	if err != nil {
 		return ResponseCarrier{Data: nil, Headers: nil, StatusCode: 500, Error: err}
@@ -60,6 +61,7 @@ func (c *Controller) RequestGeneric(method string, host string, path string, hdr
 	if err != nil {
 		return ResponseCarrier{Data: nil, Headers: resp.Header, StatusCode: resp.StatusCode, Error: err}
 	}
+	bytes.ReplaceAll(b, []byte(c.Config.TargetDomain), []byte(c.Config.ProxyDomain))
 
 	if c.Config.Caching {
 		if !strings.Contains(path, "?") {

+ 3 - 2
pkg/configuration.go

@@ -39,7 +39,7 @@ import (
 type HttpServerConfig struct {
 	HttpPort     int      `json:"http_port"`
 	HttpsPort    int      `json:"https_port"`
-	ServerDomain string   // This is the domain that will be used by the client
+	TargetDomain string   // This is the domain that will be used by the client
 	ProxyDomain  string   // This will be the domain that is being proxied, i.e. the downstream domain
 	SslPemFile   string   `json:"ssl_pem_file"`
 	SslKeyFile   string   `json:"ssl_key_file"`
@@ -68,13 +68,14 @@ accessed through the proxy
 
 	:param loc: the location of the config file
 */
-func ReadConfig(loc string) (HttpServerConfig, error) {
+func ReadConfig(loc string, proxyDomain string, targetDomain string) (HttpServerConfig, error) {
 
 	f, err := os.ReadFile(loc)
 	if err != nil {
 		return HttpServerConfig{}, err
 	}
 	var cfg HttpServerConfig
+	cfg = HttpServerConfig{ProxyDomain: proxyDomain, TargetDomain: targetDomain}
 	err = json.Unmarshal(f, &cfg)
 	if err != nil {
 		return HttpServerConfig{}, err

+ 9 - 15
pkg/controller.go

@@ -49,16 +49,13 @@ var NonmutableHeaders = map[string]struct{}{
 	"Host":            struct{}{},
 }
 
-type TokenUpdate struct {
-	Code    string `form:"code"`
-	Content string `form:"content"`
-}
-
 type Controller struct {
-	Config    HttpServerConfig
-	RouteMaps *RouteMap
-	Client    *http.Client
-	cache     *cache.Cache
+	Config     HttpServerConfig
+	ProxyAddr  string
+	TargetAddr string
+	RouteMaps  *RouteMap
+	Client     *http.Client
+	cache      *cache.Cache
 }
 
 type ProxyCookies struct {
@@ -118,22 +115,19 @@ func (c *Controller) HandleAny(ctx *gin.Context) {
 		}
 	}
 
-	//dname, ok := c.RouteMaps.GetMappedDomain(incomingPath)
-	//if ok { // below, RequestURI() returns the whole URI with the query
-	resp := c.RequestGeneric(ctx.Request.Method, c.Config.ProxyDomain, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body)
+	resp := c.RequestGeneric(ctx.Request.Method, c.Config.TargetDomain, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body)
 	if resp.Error != nil {
 		log.Fatal(resp.Error, " failed to route the request: ", incomingPath, " to the target domain: ", ctx.Request.Host, " Error: ", resp.Error)
 	}
 	for k, v := range resp.Headers {
 		_, ok := NonmutableHeaders[k]
 		if !ok {
+			// ctx.Header(k, strings.ReplaceAll(v[0], c.Config.TargetDomain, c.Config.ProxyDomain))
 			ctx.Header(k, v[0])
 		}
 	}
-	ctx.Header("access-control-allow-origin", c.Config.ServerDomain)
+	ctx.Header("access-control-allow-origin", c.Config.ProxyDomain)
 	ctx.Data(resp.StatusCode, resp.Headers.Get("content-type"), resp.Data)
 	return
-	//}
 
-	//c.TryHosts(ctx.Request.Method, ctx.Request.URL.RequestURI(), &ctx.Request.Header, ctx.Request.Body, c.Config.KnownHosts)
 }