|
|
|
@ -3,9 +3,17 @@ package main |
|
|
|
|
import ( |
|
|
|
|
"fmt" |
|
|
|
|
"log" |
|
|
|
|
"net" |
|
|
|
|
"net/http" |
|
|
|
|
"net/http/httputil" |
|
|
|
|
"os" |
|
|
|
|
"runtime" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
management "github.com/cr3a70r/shield/Management" |
|
|
|
|
proxy "github.com/cr3a70r/shield/Proxy" |
|
|
|
|
"github.com/cr3a70r/shield/Utils" |
|
|
|
|
"github.com/xlab/closer" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
@ -17,28 +25,70 @@ func main() { |
|
|
|
|
} |
|
|
|
|
defer logFile.Close() |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
proxyLog, err := os.OpenFile("proxy.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal(err) |
|
|
|
|
closer.Bind(cleanup) |
|
|
|
|
|
|
|
|
|
logger := Utils.New(os.Stdout, Utils.LevelInfo) |
|
|
|
|
|
|
|
|
|
test := workingProxyForDomain(logger, "test.dev.corp.daydev.org", "10.10.20.40", "9090") |
|
|
|
|
http.HandleFunc("/", test.Handler) |
|
|
|
|
proxysrv := &http.Server{ |
|
|
|
|
Addr: ":8085", |
|
|
|
|
Handler: nil, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
go func() { |
|
|
|
|
if err := proxysrv.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
|
|
|
|
logger.PrintError(err, map[string]string{ |
|
|
|
|
"err": "failed to start proxy server", |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
defer proxyLog.Close() |
|
|
|
|
management.Settings.Initialize() |
|
|
|
|
|
|
|
|
|
fmt.Println(runtime.NumGoroutine()) |
|
|
|
|
|
|
|
|
|
go management.StartWebserver("0.0.0.0:8080") |
|
|
|
|
|
|
|
|
|
closer.Hold() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func cleanup() { |
|
|
|
|
fmt.Println("Closing Application") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//proxy
|
|
|
|
|
func workingProxyForDomain(logger *Utils.Logger, domain string, realServer string, port string) *proxy.ReverseProxy { |
|
|
|
|
transport := &http.Transport{ |
|
|
|
|
DialContext: (&net.Dialer{ |
|
|
|
|
Timeout: 5 * time.Second, |
|
|
|
|
}).DialContext, |
|
|
|
|
} |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
management.Settings.Initialize() |
|
|
|
|
conf := &proxy.Config{ |
|
|
|
|
Path: domain, |
|
|
|
|
Host: realServer, |
|
|
|
|
Override: proxy.Override{}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
management.StartWebserver("0.0.0.0:8080") |
|
|
|
|
} |
|
|
|
|
director := &httputil.ReverseProxy{Director: func(req *http.Request) { |
|
|
|
|
originHost := conf.Host |
|
|
|
|
logger.PrintInfo("provided host", map[string]string{ |
|
|
|
|
"host": originHost, |
|
|
|
|
}) |
|
|
|
|
req.Header.Add("X-Forwarded-Host", req.Host) |
|
|
|
|
req.Header.Add("X-Origin-Host", originHost) |
|
|
|
|
req.Header.Add("Accept-Encoding", "") |
|
|
|
|
req.Host = originHost |
|
|
|
|
req.URL.Host = originHost |
|
|
|
|
req.URL.Scheme = "http" |
|
|
|
|
|
|
|
|
|
func cleanup() { |
|
|
|
|
fmt.Println("Closing Application") |
|
|
|
|
if conf.Override.Header != "" && conf.Override.Match != "" { |
|
|
|
|
if req.Header.Get(conf.Override.Header) == conf.Override.Match { |
|
|
|
|
req.URL.Path = conf.Override.Path |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}} |
|
|
|
|
|
|
|
|
|
prv := proxy.NewProxy(conf.Host, transport, director, logger, port) |
|
|
|
|
return prv |
|
|
|
|
} |
|
|
|
|