Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CREATE] server package for HTTP server (#33) #33

Merged
merged 3 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# Ignore IDE folders
.idea
3 changes: 2 additions & 1 deletion cmd/bruter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -98,7 +99,7 @@ func main() {

srv := &http.Server{
Addr: portNumber,
Handler: routes(&app),
Handler: server.NewServer(&app),
}

go func() {
Expand Down
6 changes: 2 additions & 4 deletions cmd/bruter/concurrency.go → pkg/server/concurrency.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main
package server

import (
"sync"
)
import "sync"

func RunConcurrently(tasks ...func()) {
var wg sync.WaitGroup
Expand Down
77 changes: 38 additions & 39 deletions cmd/bruter/routes.go → pkg/server/config.go
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -56,54 +61,55 @@ 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)
},
)

// Step 2: Execute functions that depend on hostinfo.Ports
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,
Expand All @@ -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
}
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
17 changes: 17 additions & 0 deletions pkg/server/routes/routes.go
Original file line number Diff line number Diff line change
@@ -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))
}
24 changes: 24 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading