Gorelay

From UVOO Tech Wiki
Revision as of 01:34, 3 May 2024 by Busk (talk | contribs) (Created page with "# Simple HTTP Relay written in go ``` package main import ( "crypto/tls" "github.com/labstack/echo/v4" "io" "net/http" ) func main() {...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Simple HTTP Relay written in go

package main

import (
        "crypto/tls"
        "github.com/labstack/echo/v4"
        "io"
        "net/http"
)

func main() {
        e := echo.New()

        e.Any("/*", func(c echo.Context) error {
                req := c.Request()

                client := &http.Client{}
                client.Transport = &http.Transport{
                        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
                }

                // Create a new request
                newReq, err := http.NewRequest(req.Method, "https://192.168.1.1:8443"+req.RequestURI, req.Body)
                if err != nil {
                        return err
                }

                // Copy the headers
                for k, v := range req.Header {
                        newReq.Header[k] = v
                }

                // Send the request
                resp, err := client.Do(newReq)
                if err != nil {
                        return err
                }
                defer resp.Body.Close()

                // Copy the response headers
                for k, v := range resp.Header {
                        c.Response().Header().Set(k, v[0])
                }

                // Copy the response body
                io.Copy(c.Response().Writer, resp.Body)

                return nil
        })

        e.Start(":8080")
}