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

fix: debug and optimize reloading of database file #22

Merged
merged 3 commits into from
Oct 26, 2023
Merged
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
32 changes: 18 additions & 14 deletions offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ var (
type (
// An OfflineDatabase is a client for querying Pwned Passwords locally.
OfflineDatabase struct {
file *DatabaseFile
logger zap.Logger
cron *cron.Cron
}

DatabaseFile struct {
database readCloserAt
logger zap.Logger
cron *cron.Cron
}

// readCloserAt is an io.ReaderAt that can be Closed and whose
Expand Down Expand Up @@ -135,9 +139,9 @@ func NewOfflineDatabase(dbFile string, updatedDbFile string) (*OfflineDatabase,
logger.Info("Database opened")
c := cron.New()
odb := &OfflineDatabase{
database: db,
logger: logger,
cron: c,
file: &DatabaseFile{db},
logger: logger,
cron: c,
}

c.AddFunc("@hourly", func() {
Expand All @@ -156,7 +160,7 @@ func NewOfflineDatabase(dbFile string, updatedDbFile string) (*OfflineDatabase,
if err != nil {
log.Panic(err)
}
odb.database = db
odb.file = &DatabaseFile{db}
logger.Info("Database updated")
}
}
Expand All @@ -169,7 +173,7 @@ func NewOfflineDatabase(dbFile string, updatedDbFile string) (*OfflineDatabase,
// Close frees resources associated with the database.
func (od *OfflineDatabase) Close() error {
od.cron.Stop()
return od.database.Close()
return od.file.database.Close()
}

// Pwned checks how frequently the given hash is included in the Pwned Passwords
Expand Down Expand Up @@ -211,7 +215,7 @@ func (od *OfflineDatabase) Pwned(hash [20]byte) (frequency int, err error) {
test = lo + ((hi - lo) / 2)

// lookup
if _, err := od.database.ReadAt(rbuf[:], DataSegmentOffset+start+int64(test*19)); err != nil {
if _, err := od.file.database.ReadAt(rbuf[:], DataSegmentOffset+start+int64(test*19)); err != nil {
return 0, err
}

Expand Down Expand Up @@ -270,7 +274,7 @@ func (od *OfflineDatabase) Scan(startPrefix, endPrefix [3]byte, hash []byte, cb
}

// read from the data file
if _, err := od.database.ReadAt(buffer[0:length], DataSegmentOffset+start); err != nil {
if _, err := od.file.database.ReadAt(buffer[0:length], DataSegmentOffset+start); err != nil {
return err
}

Expand Down Expand Up @@ -325,18 +329,18 @@ func (od *OfflineDatabase) lookup(start [3]byte) (location, length int64, err er
case [3]byte{0xFF, 0xFF, 0xFF}:

// read the required index
if _, err := od.database.ReadAt(buffer[0:8], int64(prefixIndex)*8); err != nil {
if _, err := od.file.database.ReadAt(buffer[0:8], int64(prefixIndex)*8); err != nil {
return 0, 0, err
}

// look up locations and calculate length
loc = int64(binary.BigEndian.Uint64(buffer[0:8]))
dataLen = int64(od.database.Len()-IndexSegmentSize) - loc
dataLen = int64(od.file.database.Len()-IndexSegmentSize) - loc

default:

// read the required index, and the next one (to calculate length)
if _, err := od.database.ReadAt(buffer[0:16], int64(prefixIndex)*8); err != nil {
if _, err := od.file.database.ReadAt(buffer[0:16], int64(prefixIndex)*8); err != nil {
return 0, 0, err
}

Expand Down Expand Up @@ -377,9 +381,9 @@ func (od *OfflineDatabase) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// not a hash, hash it now
hash = sha1.Sum([]byte(pw))
}

od.logger.Sugar().Infof("using database file at %p", od.file)
od.logger.Sugar().Infof("checking password: %s", hex.EncodeToString(hash[:]))
frequency, err := od.Pwned(hash)
od.logger.Sugar().Infof("checking password: %v", hash)
if err != nil {
od.logger.Sugar().Warnf("error checking password: %v", err)
w.WriteHeader(http.StatusInternalServerError)
Expand Down