diff --git a/.gitignore b/.gitignore index 66fd13c..7d06fd5 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +# Ignore IDE folders +.idea \ No newline at end of file diff --git a/cmd/bruter/main.go b/cmd/bruter/main.go index 3a57c4d..8f50e59 100644 --- a/cmd/bruter/main.go +++ b/cmd/bruter/main.go @@ -8,6 +8,7 @@ import ( "github.com/CyberRoute/bruter/pkg/handlers" "github.com/CyberRoute/bruter/pkg/network" "github.com/CyberRoute/bruter/pkg/render" + "github.com/CyberRoute/bruter/pkg/server" "github.com/alexedwards/scs/v2" "github.com/evilsocket/islazy/async" "github.com/fatih/color" @@ -98,7 +99,7 @@ func main() { srv := &http.Server{ Addr: portNumber, - Handler: routes(&app), + Handler: server.NewServer(&app), } go func() { diff --git a/cmd/bruter/concurrency.go b/pkg/server/concurrency.go similarity index 85% rename from cmd/bruter/concurrency.go rename to pkg/server/concurrency.go index ae30b9d..3a84e23 100644 --- a/cmd/bruter/concurrency.go +++ b/pkg/server/concurrency.go @@ -1,8 +1,6 @@ -package main +package server -import ( - "sync" -) +import "sync" func RunConcurrently(tasks ...func()) { var wg sync.WaitGroup diff --git a/cmd/bruter/routes.go b/pkg/server/config.go similarity index 68% rename from cmd/bruter/routes.go rename to pkg/server/config.go index d603713..00ca570 100644 --- a/cmd/bruter/routes.go +++ b/pkg/server/config.go @@ -1,40 +1,45 @@ -package main +package server import ( "crypto/tls" - "html/template" - "net/http" - "github.com/CyberRoute/bruter/pkg/config" "github.com/CyberRoute/bruter/pkg/grabber" - "github.com/CyberRoute/bruter/pkg/handlers" "github.com/CyberRoute/bruter/pkg/models" "github.com/CyberRoute/bruter/pkg/network" "github.com/CyberRoute/bruter/pkg/shodan" "github.com/CyberRoute/bruter/pkg/ssl" - "github.com/go-chi/chi/v5" - "github.com/go-chi/chi/v5/middleware" + "html/template" + "net/http" ) -func checkError(err error) { - if err != nil { - app.ZeroLog.Error().Err(err).Msg("") - } +type ConfigServer struct { + App *config.AppConfig } -func routes(app *config.AppConfig) http.Handler { - mux := chi.NewRouter() +func NewConfigServer(app *config.AppConfig) *ConfigServer { + return &ConfigServer{ + App: app, + } +} - mux.Use(middleware.Recoverer) - mux.Use(SessionLoad) +func (sc *ConfigServer) checkError(err error) { + if err != nil { + sc.App.ZeroLog.Error().Err(err).Msg("") + } +} +// RunConfiguration runs for NewServer +func (sc *ConfigServer) RunConfiguration(app *config.AppConfig) (models.HomeArgs, models.TemplateData, models.TemplateData) { ipv4, err := network.ResolveByName(app.Domain) - checkError(err) + sc.checkError(err) + ipv6, err := network.ResolveByNameipv6(app.Domain) - checkError(err) + sc.checkError(err) + customTransport := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } + client := &http.Client{Transport: customTransport} sh := shodan.NewClient(client, ipv4, app.ShodanAPIKey) @@ -56,23 +61,23 @@ func routes(app *config.AppConfig) http.Handler { RunConcurrently( func() { hostinfo, err = sh.HostInfo(app) - checkError(err) + sc.checkError(err) }, func() { headers, err = sh.Head("http://" + app.Domain) - checkError(err) + sc.checkError(err) }, func() { mx_records, err = network.FindMX(app.Domain) - checkError(err) + sc.checkError(err) }, func() { whoisinfo, err = network.WhoisLookup(app.Domain) - checkError(err) + sc.checkError(err) }, func() { sslinfo, err = ssl.FetchCrtData(app.Domain) - checkError(err) + sc.checkError(err) }, ) @@ -80,30 +85,31 @@ func routes(app *config.AppConfig) http.Handler { RunConcurrently( func() { mysql, err = grabber.GrabMysqlBanner(app.Domain, hostinfo.Ports) - checkError(err) + sc.checkError(err) }, func() { ssh, err = grabber.GrabSSHBanner(app.Domain, hostinfo.Ports) - checkError(err) + sc.checkError(err) }, func() { ftp, err = grabber.GrabFTPBanner(app.Domain, hostinfo.Ports) - checkError(err) + sc.checkError(err) }, func() { smtp, err = grabber.GrabSMTPBanner(app.Domain, hostinfo.Ports) - checkError(err) + sc.checkError(err) }, func() { pop, err = grabber.GrabPOPBanner(app.Domain, hostinfo.Ports) - checkError(err) + sc.checkError(err) }, func() { irc, err = grabber.GrabIRCBanner(app.Domain, hostinfo.Ports) - checkError(err) + sc.checkError(err) }, ) - homeargs := models.HomeArgs{ + + homeArgs := models.HomeArgs{ Ipv4: ipv4, Ipv6: ipv6, Host: hostinfo, @@ -117,20 +123,13 @@ func routes(app *config.AppConfig) http.Handler { Irc: irc, } - sslargs := models.TemplateData{ + sslArgs := models.TemplateData{ SSLInfo: sslinfo, } - whoisargs := models.TemplateData{ + whoIsArgs := models.TemplateData{ WhoisInfo: whoisinfo, } - mux.Get("/", handlers.Repo.Home(homeargs)) - mux.Get("/ssl", handlers.Repo.SSLInfo(sslargs)) - mux.Get("/whois", handlers.Repo.WhoisInfo(whoisargs)) - mux.Get("/consumer", handlers.Repo.Consumer) - fileServer := http.FileServer(http.Dir("./static/")) - mux.Handle("/static/*", http.StripPrefix("/static", fileServer)) - - return mux + return homeArgs, sslArgs, whoIsArgs } diff --git a/cmd/bruter/middleware.go b/pkg/server/middleware/session.go similarity index 66% rename from cmd/bruter/middleware.go rename to pkg/server/middleware/session.go index 8bda046..465411f 100644 --- a/cmd/bruter/middleware.go +++ b/pkg/server/middleware/session.go @@ -1,9 +1,12 @@ -package main +package middleware import ( + "github.com/alexedwards/scs/v2" "net/http" ) +var session *scs.SessionManager + // SessionLoad loads and saves the session on every request func SessionLoad(next http.Handler) http.Handler { return session.LoadAndSave(next) diff --git a/pkg/server/routes/routes.go b/pkg/server/routes/routes.go new file mode 100644 index 0000000..1083009 --- /dev/null +++ b/pkg/server/routes/routes.go @@ -0,0 +1,17 @@ +package routes + +import ( + "github.com/CyberRoute/bruter/pkg/handlers" + "github.com/CyberRoute/bruter/pkg/models" + "github.com/go-chi/chi/v5" + "net/http" +) + +func Routes(mux *chi.Mux, homeArgs models.HomeArgs, sslArgs, whoIsArgs models.TemplateData) { + mux.Get("/", handlers.Repo.Home(homeArgs)) + mux.Get("/ssl", handlers.Repo.SSLInfo(sslArgs)) + mux.Get("/whois", handlers.Repo.WhoisInfo(whoIsArgs)) + mux.Get("/consumer", handlers.Repo.Consumer) + fileServer := http.FileServer(http.Dir("./static/")) + mux.Handle("/static/*", http.StripPrefix("/static", fileServer)) +} diff --git a/pkg/server/server.go b/pkg/server/server.go new file mode 100644 index 0000000..e6a89a1 --- /dev/null +++ b/pkg/server/server.go @@ -0,0 +1,24 @@ +package server + +import ( + "github.com/CyberRoute/bruter/pkg/config" + midd "github.com/CyberRoute/bruter/pkg/server/middleware" + "github.com/CyberRoute/bruter/pkg/server/routes" + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" +) + +func NewServer(app *config.AppConfig) *chi.Mux { + mux := chi.NewRouter() + + mux.Use(middleware.Recoverer) + mux.Use(midd.SessionLoad) + + sc := NewConfigServer(app) + + homeArgs, sslArgs, whoIsArgs := sc.RunConfiguration(app) + + routes.Routes(mux, homeArgs, sslArgs, whoIsArgs) + + return mux +}