]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/account.go
Replace hardcoded version with ldflag usage
[rbdr/mobius] / hotline / account.go
index b041f3cd6c4eb025381d961285eb3bacd7aefddf..3ad068797294c91e5b8e159a64364d0cdf77ac48 100644 (file)
@@ -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
@@ -13,10 +16,12 @@ type Account struct {
        Name     string       `yaml:"Name"`
        Password string       `yaml:"Password"`
        Access   accessBitmap `yaml:"Access"`
+
+       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, encodeString([]byte(a.Login))),
@@ -30,13 +35,24 @@ func (a *Account) Read(p []byte) (n int, err error) {
        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