Add option to hex encode the source IP of a client and use it as the ident, closes #24

This commit is contained in:
Ken-Håvard Lieng 2018-08-10 20:24:29 +02:00
parent e2c6cedc27
commit c975c5d120
17 changed files with 163 additions and 41 deletions

View file

@ -54,13 +54,11 @@ func (s *BoltStore) GetUsers() ([]*storage.User, error) {
s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketUsers)
return b.ForEach(func(k, _ []byte) error {
id := idFromBytes(k)
return b.ForEach(func(k, v []byte) error {
user := storage.User{
ID: id,
IDBytes: make([]byte, 8),
Username: strconv.FormatUint(id, 10),
IDBytes: make([]byte, 8),
}
user.Unmarshal(v)
copy(user.IDBytes, k)
users = append(users, &user)
@ -76,7 +74,10 @@ func (s *BoltStore) SaveUser(user *storage.User) error {
return s.db.Batch(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketUsers)
user.ID, _ = b.NextSequence()
if user.ID == 0 {
user.ID, _ = b.NextSequence()
user.IDBytes = idToBytes(user.ID)
}
user.Username = strconv.FormatUint(user.ID, 10)
data, err := user.Marshal(nil)
@ -84,7 +85,6 @@ func (s *BoltStore) SaveUser(user *storage.User) error {
return err
}
user.IDBytes = idToBytes(user.ID)
return b.Put(user.IDBytes, data)
})
}

View file

@ -1,6 +1,7 @@
struct User {
ID uint64
Username string
lastIP []byte
}
struct Server {

View file

@ -29,6 +29,21 @@ func (d *User) Size() (s uint64) {
}
s += l
}
{
l := uint64(len(d.lastIP))
{
t := l
for t >= 0x80 {
t >>= 7
s++
}
s++
}
s += l
}
s += 8
return
}
@ -67,6 +82,25 @@ func (d *User) Marshal(buf []byte) ([]byte, error) {
copy(buf[i+8:], d.Username)
i += l
}
{
l := uint64(len(d.lastIP))
{
t := uint64(l)
for t >= 0x80 {
buf[i+8] = byte(t) | 0x80
t >>= 7
i++
}
buf[i+8] = byte(t)
i++
}
copy(buf[i+8:], d.lastIP)
i += l
}
return buf[:i+8], nil
}
@ -98,6 +132,31 @@ func (d *User) Unmarshal(buf []byte) (uint64, error) {
d.Username = string(buf[i+8 : i+8+l])
i += l
}
{
l := uint64(0)
{
bs := uint8(7)
t := uint64(buf[i+8] & 0x7F)
for buf[i+8]&0x80 == 0x80 {
i++
t |= uint64(buf[i+8]&0x7F) << bs
bs += 7
}
i++
l = t
}
if uint64(cap(d.lastIP)) >= l {
d.lastIP = d.lastIP[:l]
} else {
d.lastIP = make([]byte, l)
}
copy(d.lastIP, buf[i+8:])
i += l
}
return i + 8, nil
}

View file

@ -15,6 +15,7 @@ type User struct {
store Store
messageLog MessageStore
messageIndex MessageSearchProvider
lastIP []byte
certificate *tls.Certificate
lock sync.Mutex
}
@ -68,6 +69,21 @@ func (u *User) Remove() {
os.RemoveAll(Path.User(u.Username))
}
func (u *User) GetLastIP() []byte {
u.lock.Lock()
ip := u.lastIP
u.lock.Unlock()
return ip
}
func (u *User) SetLastIP(ip []byte) error {
u.lock.Lock()
u.lastIP = ip
u.lock.Unlock()
return u.store.SaveUser(u)
}
type Server struct {
Name string
Host string