X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/69af8ddbdff8e6d0dd551e9bdc72284469a86fc0..0ed5132769e88cb385b5240986b706430f0ccd72:/hotline/account.go diff --git a/hotline/account.go b/hotline/account.go index 9a0fb7d..18965ed 100644 --- a/hotline/account.go +++ b/hotline/account.go @@ -2,8 +2,11 @@ package hotline import ( "encoding/binary" + "fmt" "golang.org/x/crypto/bcrypt" + "io" "log" + "slices" ) const GuestAccount = "guest" // default account used when no login is provided for a connection @@ -12,31 +15,44 @@ type Account struct { Login string `yaml:"Login"` Name string `yaml:"Name"` Password string `yaml:"Password"` - Access accessBitmap `yaml:"Access"` + Access accessBitmap `yaml:"Access,flow"` + + readOffset int // Internal offset to track read progress } // Read implements io.Reader interface for Account -func (a *Account) Read(p []byte) (n int, err error) { +func (a *Account) Read(p []byte) (int, error) { fields := []Field{ - NewField(fieldUserName, []byte(a.Name)), - NewField(fieldUserLogin, negateString([]byte(a.Login))), - NewField(fieldUserAccess, a.Access[:]), + NewField(FieldUserName, []byte(a.Name)), + NewField(FieldUserLogin, encodeString([]byte(a.Login))), + NewField(FieldUserAccess, a.Access[:]), } if bcrypt.CompareHashAndPassword([]byte(a.Password), []byte("")) != nil { - fields = append(fields, NewField(fieldUserPassword, []byte("x"))) + fields = append(fields, NewField(FieldUserPassword, []byte("x"))) } fieldCount := make([]byte, 2) binary.BigEndian.PutUint16(fieldCount, uint16(len(fields))) - p = append(p, fieldCount...) - + var fieldBytes []byte for _, field := range fields { - p = append(p, field.Payload()...) + b, err := io.ReadAll(&field) + if err != nil { + return 0, fmt.Errorf("error reading field: %w", err) + } + fieldBytes = append(fieldBytes, b...) } - return len(p), nil + buf := slices.Concat(fieldCount, fieldBytes) + if a.readOffset >= len(buf) { + return 0, io.EOF // All bytes have been read + } + + n := copy(p, buf[a.readOffset:]) + a.readOffset += n + + return n, nil } // hashAndSalt generates a password hash from a users obfuscated plaintext password