separate FLAP from session

This commit is contained in:
Artem Titoulenko 2021-12-16 18:37:27 -05:00
parent 268cff0963
commit d4d7f71c66
6 changed files with 17 additions and 18 deletions

View file

@ -50,7 +50,7 @@ func (a *AuthorizationRegistrationService) HandleSNAC(db *bun.DB, session *Sessi
snac := NewSNAC(0x17, 0x03) snac := NewSNAC(0x17, 0x03)
snac.Data.WriteBinary(usernameTLV) snac.Data.WriteBinary(usernameTLV)
snac.Data.WriteBinary(NewTLV(0x08, []byte{0, 4})) snac.Data.WriteBinary(NewTLV(0x08, []byte{0, 4}))
resp := NewFLAP(session, 2) resp := NewFLAP(2)
resp.Data.WriteBinary(snac) resp.Data.WriteBinary(snac)
return session.Send(resp) return session.Send(resp)
} }
@ -65,7 +65,7 @@ func (a *AuthorizationRegistrationService) HandleSNAC(db *bun.DB, session *Sessi
snac.Data.WriteUint16(uint16(len(user.Cipher))) snac.Data.WriteUint16(uint16(len(user.Cipher)))
snac.Data.WriteString(user.Cipher) snac.Data.WriteString(user.Cipher)
resp := NewFLAP(session, 2) resp := NewFLAP(2)
resp.Data.WriteBinary(snac) resp.Data.WriteBinary(snac)
return session.Send(resp) return session.Send(resp)
@ -90,7 +90,7 @@ func (a *AuthorizationRegistrationService) HandleSNAC(db *bun.DB, session *Sessi
snac := NewSNAC(0x17, 0x03) snac := NewSNAC(0x17, 0x03)
snac.Data.WriteBinary(usernameTLV) snac.Data.WriteBinary(usernameTLV)
snac.Data.WriteBinary(NewTLV(0x08, []byte{0, 4})) snac.Data.WriteBinary(NewTLV(0x08, []byte{0, 4}))
resp := NewFLAP(session, 2) resp := NewFLAP(2)
resp.Data.WriteBinary(snac) resp.Data.WriteBinary(snac)
return session.Send(resp) return session.Send(resp)
} }
@ -111,12 +111,12 @@ func (a *AuthorizationRegistrationService) HandleSNAC(db *bun.DB, session *Sessi
badPasswordSnac := NewSNAC(0x17, 0x03) badPasswordSnac := NewSNAC(0x17, 0x03)
badPasswordSnac.Data.WriteBinary(usernameTLV) badPasswordSnac.Data.WriteBinary(usernameTLV)
badPasswordSnac.Data.WriteBinary(NewTLV(0x08, []byte{0, 4})) badPasswordSnac.Data.WriteBinary(NewTLV(0x08, []byte{0, 4}))
badPasswordFlap := NewFLAP(session, 2) badPasswordFlap := NewFLAP(2)
badPasswordFlap.Data.WriteBinary(badPasswordSnac) badPasswordFlap.Data.WriteBinary(badPasswordSnac)
session.Send(badPasswordFlap) session.Send(badPasswordFlap)
// Tell tem to leave // Tell tem to leave
discoFlap := NewFLAP(session, 4) discoFlap := NewFLAP(4)
return session.Send(discoFlap) return session.Send(discoFlap)
} }
@ -125,12 +125,12 @@ func (a *AuthorizationRegistrationService) HandleSNAC(db *bun.DB, session *Sessi
authSnac.Data.WriteBinary(usernameTLV) authSnac.Data.WriteBinary(usernameTLV)
authSnac.Data.WriteBinary(NewTLV(0x5, []byte("10.0.1.2:5191"))) authSnac.Data.WriteBinary(NewTLV(0x5, []byte("10.0.1.2:5191")))
authSnac.Data.WriteBinary(NewTLV(0x6, []byte(`{"hello": "uwu"}`))) authSnac.Data.WriteBinary(NewTLV(0x6, []byte(`{"hello": "uwu"}`)))
authFlap := NewFLAP(session, 2) authFlap := NewFLAP(2)
authFlap.Data.WriteBinary(authSnac) authFlap.Data.WriteBinary(authSnac)
session.Send(authFlap) session.Send(authFlap)
// Tell tem to leave // Tell tem to leave
discoFlap := NewFLAP(session, 4) discoFlap := NewFLAP(4)
return session.Send(discoFlap) return session.Send(discoFlap)
} }

View file

@ -21,13 +21,12 @@ type FLAP struct {
Data Buffer Data Buffer
} }
func NewFLAP(session *Session, channel uint8) *FLAP { func NewFLAP(channel uint8) *FLAP {
session.SequenceNumber += 1
return &FLAP{ return &FLAP{
Header: FLAPHeader{ Header: FLAPHeader{
Channel: channel, Channel: channel,
SequenceNumber: uint16(session.SequenceNumber), SequenceNumber: 0,
DataLength: 0,
}, },
} }
} }

View file

@ -6,8 +6,7 @@ import (
) )
func TestFLAP(t *testing.T) { func TestFLAP(t *testing.T) {
session := Session{} hello := NewFLAP(1)
hello := NewFLAP(&session, 1)
hello.Data.Write([]byte{0, 0, 0, 1}) hello.Data.Write([]byte{0, 0, 0, 1})
b, err := hello.MarshalBinary() b, err := hello.MarshalBinary()

View file

@ -94,7 +94,7 @@ func handleTCPConnection(db *bun.DB, session *Session, conn net.Conn) {
for { for {
if !session.GreetedClient { if !session.GreetedClient {
// send a hello // send a hello
hello := NewFLAP(session, 1) hello := NewFLAP(1)
hello.Data.Write([]byte{0, 0, 0, 1}) hello.Data.Write([]byte{0, 0, 0, 1})
err := session.Send(hello) err := session.Send(hello)
panicIfError(err) panicIfError(err)

View file

@ -2,7 +2,6 @@ package main
import ( import (
"context" "context"
"encoding"
"fmt" "fmt"
"net" "net"
@ -46,8 +45,10 @@ func CurrentSession(ctx context.Context) (session *Session, err error) {
return s.(*Session), nil return s.(*Session), nil
} }
func (s *Session) Send(m encoding.BinaryMarshaler) error { func (s *Session) Send(flap *FLAP) error {
bytes, err := m.MarshalBinary() s.SequenceNumber += 1
flap.Header.SequenceNumber = s.SequenceNumber
bytes, err := flap.MarshalBinary()
if err != nil { if err != nil {
return errors.Wrap(err, "could not marshal message") return errors.Wrap(err, "could not marshal message")
} }

View file

@ -38,7 +38,7 @@ func prettyBytes(bytes []byte) string {
if err != nil || (n < 32 || n > 126) { if err != nil || (n < 32 || n > 126) {
res += "." res += "."
} else { } else {
res += string(n) res += string(rune(n))
} }
} }
res += "|\n" res += "|\n"