Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
refactor: Change store to sqlite and UI improvements (#36)
Browse files Browse the repository at this point in the history
- Move backend to sqlite (from badgerdb)
- Update dashboard UI
- Delete users
- Add connection status (online, lastActiveAt)
  • Loading branch information
amalshaji authored Mar 12, 2023
1 parent ad01e77 commit 084ee40
Show file tree
Hide file tree
Showing 34 changed files with 1,980 additions and 667 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ dist/
!beaver_server/
data/
testdata/
!tests/data/
!tests/data/
internal/server/admin/test_beaver.db
22 changes: 5 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,23 @@ require (
github.com/shirou/gopsutil/v3 v3.23.1
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.1
github.com/timshannon/badgerhold/v4 v4.0.2
golang.org/x/crypto v0.6.0
golang.org/x/term v0.5.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/sqlite v1.4.4
gorm.io/gorm v1.24.6
)

require (
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgraph-io/badger/v3 v3.2103.1 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v2.0.0+incompatible // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.13.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand All @@ -46,10 +36,8 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
205 changes: 13 additions & 192 deletions go.sum

Large diffs are not rendered by default.

11 changes: 0 additions & 11 deletions internal/server/admin/dashboard.go

This file was deleted.

53 changes: 27 additions & 26 deletions internal/server/admin/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@ import (

"github.com/amalshaji/beaver/internal/utils"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)

var ErrWrongPassword = errors.New("wrong password")

type BaseModel struct {
ID uint64 `badgerhold:"key" json:"id"`
CreatedAt time.Time `json:"created_at"`
}

func (b *BaseModel) MarkAsNew() {
b.CreatedAt = time.Now()
}
var (
ErrWrongPassword = errors.New("wrong password")
ErrWrongSecretKey = errors.New("wrong secret key")
)

type AdminUser struct {
BaseModel
gorm.Model

Email string `badgerhold:"unique" json:"email"`
PasswordHash string `json:"-"`
SessionToken string `json:"-"`
IsSuperUser bool `json:"is_super_user"`
Email string `gorm:"index,unique"`
PasswordHash string `gorm:"unique" json:"-"`
SuperUser bool

Session Session
}

func (a *AdminUser) SetPassword(rawPassword string) error {
Expand All @@ -47,24 +43,29 @@ func (a *AdminUser) CheckPassword(rawPassword string) error {
return nil
}

func (a *AdminUser) GenerateSessionToken() error {
a.SessionToken = utils.GenerateUUIDV4().String()
return nil
type Session struct {
gorm.Model

Token string `gorm:"index, unique"`
AdminUserId uint
}

func (a *AdminUser) ResetSessionToken() error {
a.SessionToken = ""
func (s *Session) GenerateSessionToken() error {
s.Token = utils.GenerateSessionToken()
return nil
}

type TunnelUser struct {
BaseModel
gorm.Model

Email string `badgerhold:"unique" json:"email"`
SecretKey string `json:"secret_key"`
Email string `gorm:"index,unique"`
SecretKey *string `gorm:"index,unique" json:"-"`
Active bool
LastActiveAt *time.Time
}

func (t *TunnelUser) RotateSecretKey() error {
t.SecretKey = utils.GenerateUUIDV4().String()
return nil
func (t *TunnelUser) RotateSecretKey() string {
newSecretKey := utils.GenerateSecretKey()
t.SecretKey = &newSecretKey
return newSecretKey
}
Loading

0 comments on commit 084ee40

Please sign in to comment.