mirror of
https://github.com/1f349/http-dev.git
synced 2024-12-30 11:26:26 +00:00
Rename to http-dev
This commit is contained in:
parent
8f888cddac
commit
1a6d416b03
2
go.mod
2
go.mod
@ -1,3 +1,3 @@
|
||||
module github.com/MrMelon54/host-rewriter
|
||||
module github.com/1f349/http-dev
|
||||
|
||||
go 1.21.3
|
||||
|
@ -1,46 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
var listenFlag, targetFlag, hostFlag string
|
||||
|
||||
func main() {
|
||||
flag.StringVar(&listenFlag, "listen", ":8080", "Listen address")
|
||||
flag.StringVar(&targetFlag, "target", "", "Set target URL to forward to")
|
||||
flag.StringVar(&hostFlag, "host", "", "Set the host value")
|
||||
flag.Parse()
|
||||
|
||||
target, err := url.Parse(targetFlag)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to parse target URL:", err)
|
||||
}
|
||||
|
||||
proxy := &httputil.ReverseProxy{
|
||||
Rewrite: func(r *httputil.ProxyRequest) {
|
||||
r.SetURL(target)
|
||||
r.Out.Host = hostFlag
|
||||
},
|
||||
}
|
||||
s := http.Server{
|
||||
Addr: listenFlag,
|
||||
Handler: proxy,
|
||||
ReadTimeout: 150 * time.Second,
|
||||
ReadHeaderTimeout: 150 * time.Second,
|
||||
WriteTimeout: 150 * time.Second,
|
||||
IdleTimeout: 150 * time.Second,
|
||||
MaxHeaderBytes: 4096000,
|
||||
}
|
||||
|
||||
log.Printf("Host Rewriter listening on %s\n", listenFlag)
|
||||
log.Printf("Rewriting host to '%s' and forwarding to '%s'\n", hostFlag, targetFlag)
|
||||
if err := s.ListenAndServe(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
78
http-dev.go
Normal file
78
http-dev.go
Normal file
@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var listenFlag, targetFlag, hostFlag string
|
||||
var allowCors bool
|
||||
|
||||
func main() {
|
||||
flag.StringVar(&listenFlag, "listen", ":8080", "Listen address")
|
||||
flag.StringVar(&targetFlag, "target", "", "Set target URL to forward to")
|
||||
flag.StringVar(&hostFlag, "host", "", "Set the host value")
|
||||
flag.BoolVar(&allowCors, "cors", false, "Allow all cross origin requests")
|
||||
flag.Parse()
|
||||
|
||||
target, err := url.Parse(targetFlag)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to parse target URL:", err)
|
||||
}
|
||||
|
||||
proxy := &httputil.ReverseProxy{
|
||||
Rewrite: func(r *httputil.ProxyRequest) {
|
||||
r.SetURL(target)
|
||||
if hostFlag != "" {
|
||||
r.Out.Host = hostFlag
|
||||
}
|
||||
},
|
||||
ModifyResponse: func(response *http.Response) error {
|
||||
if !allowCors {
|
||||
return nil
|
||||
}
|
||||
|
||||
response.Header.Add("Access-Control-Allow-Origin", "*")
|
||||
response.Header.Add("Access-Control-Allow-Credentials", "true")
|
||||
response.Header.Add("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
|
||||
response.Header.Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
||||
|
||||
if response.Request.Method == "OPTIONS" {
|
||||
_ = response.Body.Close()
|
||||
response.Header.Set("Content-Type", "text/plain; charset=utf-8")
|
||||
response.Header.Set("X-Content-Type-Options", "nosniff")
|
||||
response.StatusCode = http.StatusNoContent
|
||||
response.Status = http.StatusText(http.StatusNoContent)
|
||||
response.Body = io.NopCloser(strings.NewReader("No Content\n"))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
s := http.Server{
|
||||
Addr: listenFlag,
|
||||
Handler: proxy,
|
||||
ReadTimeout: 150 * time.Second,
|
||||
ReadHeaderTimeout: 150 * time.Second,
|
||||
WriteTimeout: 150 * time.Second,
|
||||
IdleTimeout: 150 * time.Second,
|
||||
MaxHeaderBytes: 4096000,
|
||||
}
|
||||
|
||||
log.Printf("HTTP Dev listening on %s\n", listenFlag)
|
||||
log.Printf("- Forwarding to '%s'\n", targetFlag)
|
||||
if hostFlag != "" {
|
||||
log.Printf("- Rewriting host to '%s'\n", hostFlag)
|
||||
}
|
||||
if allowCors {
|
||||
log.Println("- Allowing all cross-origin requests")
|
||||
}
|
||||
if err := s.ListenAndServe(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user