|
|
|
@ -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) |
|
|
|
|
|
|
|
|
|
defer proxyLog.Close() |
|
|
|
|
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, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//proxy
|
|
|
|
|
transport := &http.Transport{ |
|
|
|
|
DialContext: (&net.Dialer{ |
|
|
|
|
Timeout: 5 * time.Second, |
|
|
|
|
}).DialContext, |
|
|
|
|
go func() { |
|
|
|
|
if err := proxysrv.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
|
|
|
|
logger.PrintError(err, map[string]string{ |
|
|
|
|
"err": "failed to start proxy server", |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
*/ |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
management.Settings.Initialize() |
|
|
|
|
|
|
|
|
|
management.StartWebserver("0.0.0.0:8080") |
|
|
|
|
fmt.Println(runtime.NumGoroutine()) |
|
|
|
|
|
|
|
|
|
go management.StartWebserver("0.0.0.0:8080") |
|
|
|
|
|
|
|
|
|
closer.Hold() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func cleanup() { |
|
|
|
|
fmt.Println("Closing Application") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func workingProxyForDomain(logger *Utils.Logger, domain string, realServer string, port string) *proxy.ReverseProxy { |
|
|
|
|
transport := &http.Transport{ |
|
|
|
|
DialContext: (&net.Dialer{ |
|
|
|
|
Timeout: 5 * time.Second, |
|
|
|
|
}).DialContext, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
conf := &proxy.Config{ |
|
|
|
|
Path: domain, |
|
|
|
|
Host: realServer, |
|
|
|
|
Override: proxy.Override{}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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" |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
} |
|
|
|
|