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

implementing DB driver #25

Closed
wants to merge 2 commits into from
Closed
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
55 changes: 38 additions & 17 deletions cmd/bruter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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/driver"
"github.com/alexedwards/scs/v2"
"github.com/evilsocket/islazy/async"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -55,13 +56,49 @@ func main() {
IP, _ := network.ResolveByName(*Domain)
logger.Info().Msg(fmt.Sprintf("Scanning IP %s %s", IP, "OK"))

db, err := run(&logger)
if err != nil {
logger.Info().Msg(err.Error())
}
defer db.SQL.Close()




file, err := os.Open(*Dictionary)
if err != nil {
logger.Fatal().Err(err).Msg("")
}
defer file.Close()

list := readDictionary(file)
total := len(list)
shift := 1

queue := createQueue(&app.Mu, *Domain, list, shift, total, *Verbose)

queue.WaitDone()

fmt.Println("\nAll tasks completed, press Ctrl-C to quit.")
select {}
}

func run(logger *zerolog.Logger) (*driver.DB, error) {
app.InProduction = false
session = scs.New()
session.Lifetime = 24 * time.Hour
session.Cookie.Persist = true
session.Cookie.SameSite = http.SameSiteLaxMode
session.Cookie.Secure = app.InProduction
app.Session = session
// conct to database
logger.Info().Msg("Connecting to Postgres")
db, err := driver.ConnectSQL("host=localhost port=5432 dbname= user= password=")
if err != nil {
return nil, err
}
logger.Info().Msg("Connected to Postgres")


tc, err := render.CreateTemplateCache()
if err != nil {
Expand All @@ -87,23 +124,7 @@ func main() {
panic(err)
}
}()

file, err := os.Open(*Dictionary)
if err != nil {
logger.Fatal().Err(err).Msg("")
}
defer file.Close()

list := readDictionary(file)
total := len(list)
shift := 1

queue := createQueue(&app.Mu, *Domain, list, shift, total, *Verbose)

queue.WaitDone()

fmt.Println("\nAll tasks completed, press Ctrl-C to quit.")
select {}
return db, nil
}

func readDictionary(file *os.File) []string {
Expand Down
16 changes: 11 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ require (
github.com/alexedwards/scs/v2 v2.5.1
github.com/evilsocket/islazy v1.11.0
github.com/go-chi/chi/v5 v5.0.8
github.com/jackc/pgconn v1.14.1
github.com/jackc/pgx/v5 v5.4.3
github.com/rs/zerolog v1.29.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
)
152 changes: 149 additions & 3 deletions go.sum

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package driver

import (
"database/sql"
"time"
_ "github.com/jackc/pgconn"
_ "github.com/jackc/pgx/v5"
_ "github.com/jackc/pgx/v5/stdlib"


)

// DB holds the database connection pool
type DB struct {
SQL *sql.DB
}

var dbConn = &DB{}

const maxOpenDbConn = 10
const MaxIdleDbConn = 5
const MaxDbLifetime = 5 * time.Minute

// creates database pool for postgres
func ConnectSQL(dsn string) (*DB, error) {
d, err := NewDatabase(dsn)
if err != nil {
panic(err)
}
d.SetMaxOpenConns(maxOpenDbConn)
d.SetMaxIdleConns(MaxIdleDbConn)
d.SetConnMaxLifetime(MaxDbLifetime)
dbConn.SQL = d
err = testDB(d)
if err != nil {
return nil, err
}
return dbConn, nil

}

// ping db
func testDB(d *sql.DB) error {
err := d.Ping()
if err != nil {
return err
}
return nil
}

// open new db
func NewDatabase(dsn string) (*sql.DB, error) {
db, err := sql.Open("pgx", dsn)
if err != nil {
return nil, err
}
if err = db.Ping(); err != nil {
return nil, err
}
return db, nil
}
21 changes: 21 additions & 0 deletions pkg/repository/dbrepo/dbrepo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dbrepo

import (
"database/sql"

"github.com/CyberRoute/bruter/pkg/config"
"github.com/CyberRoute/bruter/pkg/repository"
)


type postgresDBRepo struct {
App *config.AppConfig
DB *sql.DB
}

func NewPostgresRepo(conn *sql.DB, a *config.AppConfig) repository.DatabaseRepo {
return &postgresDBRepo{
App: a,
DB: conn,
}
}
5 changes: 5 additions & 0 deletions pkg/repository/dbrepo/postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dbrepo

func (m *postgresDBRepo) AllUrls() bool {
return true
}
5 changes: 5 additions & 0 deletions pkg/repository/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package repository

type DatabaseRepo interface {
AllUrls() bool
}