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 1 commit
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
8 changes: 8 additions & 0 deletions .idea/.gitignore
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may want to remove the .idea folder, the gitignore is not complete since I develop on linux I didn't bother to add that :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will put .idea folder in .gitignore for future

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/bruter.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added build/bruter
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also the build folder to be wiped, that comes from running the make commands

Binary file not shown.
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
39 changes: 15 additions & 24 deletions cmd/bruter/routes.go → pkg/server/config.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
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"
)

var app config.AppConfig

func checkError(err error) {
if err != nil {
app.ZeroLog.Error().Err(err).Msg("")
}
}

func routes(app *config.AppConfig) http.Handler {
mux := chi.NewRouter()

mux.Use(middleware.Recoverer)
mux.Use(SessionLoad)

// RunConfiguration runs for NewServer
func RunConfiguration(app *config.AppConfig) (models.HomeArgs, models.TemplateData, models.TemplateData) {
ipv4, err := network.ResolveByName(app.Domain)
checkError(err)

ipv6, err := network.ResolveByNameipv6(app.Domain)
checkError(err)

customTransport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

client := &http.Client{Transport: customTransport}
sh := shodan.NewClient(client, ipv4, app.ShodanAPIKey)

Expand Down Expand Up @@ -103,7 +100,8 @@ func routes(app *config.AppConfig) http.Handler {
checkError(err)
},
)
homeargs := models.HomeArgs{

homeArgs := models.HomeArgs{
Ipv4: ipv4,
Ipv6: ipv6,
Host: hostinfo,
Expand All @@ -117,20 +115,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))
}
22 changes: 22 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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)

homeArgs, sslArgs, whoIsArgs := RunConfiguration(app)

routes.Routes(mux, homeArgs, sslArgs, whoIsArgs)

return mux
}