-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
143 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"flag" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/evg4b/uncors/inernal/handler" | ||
) | ||
|
||
func hello(w http.ResponseWriter, req *http.Request) { | ||
fmt.Fprintf(w, "hello\n") | ||
} | ||
var ( | ||
target = "github.com" | ||
protocol = "https" | ||
origin = "localhost:4200" | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/", hello) | ||
flag.StringVar(&target, "target", target, "host:port to proxy requests to") | ||
flag.StringVar(&protocol, "protocol", protocol, "protocol used by the target") | ||
flag.StringVar(&origin, "origin", origin, "origin header to be used for the proxy request") | ||
|
||
flag.Parse() | ||
|
||
reqHandler := handler.NewRequestHandler( | ||
handler.WithOrigin(origin), | ||
handler.WithProtocol(protocol), | ||
handler.WithTarget(target), | ||
) | ||
|
||
http.HandleFunc("/", reqHandler.HandleRequest) | ||
|
||
http.ListenAndServe(":8080", nil) | ||
log.Println("localhost:3000", "=>", target) | ||
http.ListenAndServe(":3000", nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package handler | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type RequestHandeler struct { | ||
target string | ||
protocol string | ||
origin string | ||
} | ||
|
||
func NewRequestHandler(options ...RequestHandelerOptions) *RequestHandeler { | ||
handler := &RequestHandeler{} | ||
|
||
for _, option := range options { | ||
option(handler) | ||
} | ||
|
||
return handler | ||
} | ||
|
||
func (rh *RequestHandeler) HandleRequest(w http.ResponseWriter, req *http.Request) { | ||
url := fmt.Sprintf("%s://%s%s", rh.protocol, rh.target, req.URL.String()) | ||
log.Println("Requset: ", url) | ||
|
||
for n, h := range req.Header { | ||
if strings.Contains(n, "Origin") { | ||
for _, h := range h { | ||
rh.origin = h | ||
} | ||
} | ||
} | ||
header := w.Header() | ||
|
||
header.Add("Access-Control-Allow-Origin", rh.origin) | ||
header.Add("Access-Control-Allow-Credentials", "true") | ||
header.Add("Access-Control-Allow-Methods", "GET, PUT, POST, HEAD, TRACE, DELETE, PATCH, COPY, HEAD, LINK, OPTIONS") | ||
|
||
if req.Method == "OPTIONS" { | ||
log.Print("CORS asked for ", url) | ||
for n, h := range req.Header { | ||
if strings.Contains(n, "Access-Control-Request") { | ||
for _, h := range h { | ||
k := strings.Replace(n, "Request", "Allow", 1) | ||
header.Add(k, h) | ||
} | ||
} | ||
} | ||
return | ||
} | ||
|
||
req, err := http.NewRequest(req.Method, url, req.Body) | ||
if err != nil { | ||
log.Print(err) | ||
return | ||
} | ||
|
||
for n, h := range req.Header { | ||
for _, h := range h { | ||
req.Header.Add(n, h) | ||
} | ||
} | ||
|
||
client := http.Client{} | ||
if req.TLS != nil { | ||
client.Transport = &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
} | ||
} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Println(err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
for h, v := range resp.Header { | ||
for _, v := range v { | ||
w.Header().Add(h, v) | ||
} | ||
} | ||
|
||
w.WriteHeader(resp.StatusCode) | ||
|
||
wr, err := io.Copy(w, resp.Body) | ||
if err != nil { | ||
log.Println(wr, err) | ||
} else { | ||
log.Print("Written", wr, "bytes") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package handler | ||
|
||
type RequestHandelerOptions = func(*RequestHandeler) | ||
|
||
func WithTarget(target string) RequestHandelerOptions { | ||
return func(rh *RequestHandeler) { | ||
rh.target = target | ||
} | ||
} | ||
|
||
func WithProtocol(protocol string) RequestHandelerOptions { | ||
return func(rh *RequestHandeler) { | ||
rh.protocol = protocol | ||
} | ||
} | ||
|
||
func WithOrigin(origin string) RequestHandelerOptions { | ||
return func(rh *RequestHandeler) { | ||
rh.origin = origin | ||
} | ||
} |