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

query: allow multiple session IDs per connection #2

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 7 additions & 6 deletions src/query/packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
"errors"
"fmt"
"io"
"net"
"main/src/util"
"math/rand"
"strconv"
"strings"
)

func writeHandshakePacket(w io.Writer, sessionID int32) error {
func writeHandshakePacket(w io.Writer, addr net.Addr, sessionID int32) error {
challengeToken := strconv.FormatInt(int64(rand.Int31()), 10)

sessionsMutex.Lock()
sessions[sessionID] = challengeToken
sessions[addr.String()] = challengeToken
sessionsMutex.Unlock()

// Type - byte
Expand All @@ -36,13 +37,13 @@ func writeHandshakePacket(w io.Writer, sessionID int32) error {
return nil
}

func readRequestPacket(r io.Reader, w io.Writer, sessionID int32) (bool, error) {
func readRequestPacket(r io.Reader, w io.Writer, addr net.Addr, sessionID int32) (bool, error) {
sessionsMutex.Lock()

defer sessionsMutex.Unlock()

if _, ok := sessions[sessionID]; !ok {
return false, fmt.Errorf("query: invalid or expired session ID: %X", sessionID)
if _, ok := sessions[addr.String()]; !ok {
return false, fmt.Errorf("query: no currently active challenges for %s", addr.String())
}

// Challenge Token - int32
Expand All @@ -53,7 +54,7 @@ func readRequestPacket(r io.Reader, w io.Writer, sessionID int32) (bool, error)
return false, err
}

if sessions[sessionID] != strconv.FormatInt(int64(challengeToken), 10) {
if sessions[addr.String()] != strconv.FormatInt(int64(challengeToken), 10) {
return false, fmt.Errorf("query: received challenge token did not match stored")
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/query/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
var (
socket net.PacketConn = nil
conf *config.Config = nil
sessions map[int32]string = make(map[int32]string)
sessions map[string]string = make(map[string]string) // Map of net.Addr.String() to challenge string
sessionsMutex *sync.Mutex = &sync.Mutex{}
)

Expand Down Expand Up @@ -76,15 +76,15 @@ func handlePacket(data []byte, addr net.Addr) {
switch packetType {
case 0x09: // Generate challenge token
{
if err = writeHandshakePacket(buf, sessionID); err != nil {
if err = writeHandshakePacket(buf, addr, sessionID); err != nil {
return
}

break
}
case 0x00: // Request
{
isFullStat, err := readRequestPacket(r, buf, sessionID)
isFullStat, err := readRequestPacket(r, buf, addr, sessionID)

if err != nil {
return
Expand Down