Skip to content

Commit

Permalink
secure chat
Browse files Browse the repository at this point in the history
  • Loading branch information
aimjel committed Oct 22, 2023
1 parent e5f679e commit 8f0d451
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 20 deletions.
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func start(cfg *server.Config) {
stopProfile()
}
srv.Close()

f, _ := os.OpenFile("config.toml", os.O_RDWR|os.O_CREATE, 0666)
_ = toml.NewEncoder(f).Encode(cfg)
os.Exit(0)
}()

Expand All @@ -62,14 +65,15 @@ func start(cfg *server.Config) {
}
}

var cfg server.Config

func main() {
log.Info("Starting Dynamite 1.20.1 server")
if util.HasArg("-prof") {
log.Info("Starting CPU/RAM profiler")
startProfile()
}

var cfg server.Config
if err := server.LoadConfig("config.toml", &cfg); err != nil {
log.Info("%v loading config.toml. Using default config", err)
cfg = server.DefaultConfig
Expand Down
2 changes: 1 addition & 1 deletion server/network/handlers/chat_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Controller interface {
BreakBlock(pos uint64)
//BroadcastDigging(pos uint64)
SetClientSettings(p *packet.ClientSettings)
SetSessionID(id [16]byte, pk []byte)
SetSessionID(id [16]byte, pk, ks []byte, expires int64)
}

func ChatCommandPacket(controller Controller, graph *commands.Graph, content string) {
Expand Down
4 changes: 2 additions & 2 deletions server/network/handlers/player_session.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package handlers

func PlayerSession(controller Controller, id [16]byte, pk []byte) {
controller.SetSessionID(id, pk)
func PlayerSession(controller Controller, id [16]byte, pk, ks []byte, expires int64) {
controller.SetSessionID(id, pk, ks, expires)
}
37 changes: 35 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"errors"
"github.com/aimjel/minecraft/protocol/types"
"os"
"strings"
"sync"
Expand Down Expand Up @@ -122,13 +123,45 @@ func (srv *Server) handleNewConn(conn *minecraft.Conn) {
}

func (srv *Server) addPlayer(p *Session) {
players := make([]types.PlayerInfo, 0, len(srv.Players)+1)
srv.mu.Lock()
for _, pl := range srv.Players {
players = append(players, types.PlayerInfo{
UUID: pl.conn.UUID(),
Name: pl.conn.Name(),
Properties: pl.conn.Properties(),
Listed: true,
})
}
srv.mu.Unlock()

//updates the new session's player list
p.SendPacket(&packet.PlayerInfoUpdate{
Actions: 0x01 | 0x08,
Players: players,
})

srv.mu.Lock()
srv.Players[p.UUID] = p
srv.mu.Unlock()

srv.PlayerlistUpdate()
newPlayer := types.PlayerInfo{
UUID: p.conn.UUID(),
Name: p.conn.Name(),
Properties: p.conn.Properties(),
Listed: true,
}

srv.mu.RLock()
for _, sesh := range srv.Players {
sesh.SendPacket(&packet.PlayerInfoUpdate{
Actions: 0x01 | 0x08,
Players: []types.PlayerInfo{newPlayer},
})
}
srv.mu.RUnlock()

//gui.AddPlayer(p.session.Info().Name, p.UUID)
//gui.AddPlayer(pl.session.Info().Name, pl.UUID)

srv.Logger.Info("[%s] Player %s (%s) has joined the server", p.conn.RemoteAddr(), p.conn.Name(), p.UUID)
srv.GlobalMessage(srv.Translate(srv.Config.Messages.PlayerJoin, map[string]string{"player": p.Name()}), nil)
Expand Down
47 changes: 33 additions & 14 deletions server/session.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"github.com/aimjel/minecraft/protocol/types"
"math"
"math/rand"
"slices"
Expand Down Expand Up @@ -38,12 +39,10 @@ var tags = &packet.UpdateTags{
}

type Session struct {
mu sync.RWMutex
player *player.Player
conn *minecraft.Conn
Server *Server
sessionID [16]byte
publicKey []byte
mu sync.RWMutex
player *player.Player
conn *minecraft.Conn
Server *Server

spawnedEntities []int32
loadedChunks map[[2]int32]struct{}
Expand All @@ -65,6 +64,11 @@ type Session struct {

//playReady means the player is ready to receive packets regarding other players
playReady bool

sessionID [16]byte
publicKey []byte
keySignature []byte
expires uint64
}

func (p *Session) HandlePackets() error {
Expand Down Expand Up @@ -106,7 +110,7 @@ func (p *Session) HandlePackets() error {
case *packet.PlayerAbilitiesServer:
handlers.PlayerAbilities(p.player, pk.Flags)
case *packet.PlayerSessionServer:
handlers.PlayerSession(p, pk.SessionID, pk.PublicKey.PublicKey)
handlers.PlayerSession(p, pk.SessionID, pk.PublicKey, pk.KeySignature, pk.ExpiresAt)

Check failure on line 113 in server/session.go

View workflow job for this annotation

GitHub Actions / build (1.21.x, ubuntu-latest, amd64)

cannot use pk.PublicKey (variable of type packet.PublicKey) as []byte value in argument to handlers.PlayerSession

Check failure on line 113 in server/session.go

View workflow job for this annotation

GitHub Actions / build (1.21.x, ubuntu-latest, amd64)

pk.KeySignature undefined (type *packet.PlayerSessionServer has no field or method KeySignature)

Check failure on line 113 in server/session.go

View workflow job for this annotation

GitHub Actions / build (1.21.x, ubuntu-latest, amd64)

pk.ExpiresAt undefined (type *packet.PlayerSessionServer has no field or method ExpiresAt)

Check failure on line 113 in server/session.go

View workflow job for this annotation

GitHub Actions / build (1.21.x, ubuntu-latest, arm64)

cannot use pk.PublicKey (variable of type packet.PublicKey) as []byte value in argument to handlers.PlayerSession

Check failure on line 113 in server/session.go

View workflow job for this annotation

GitHub Actions / build (1.21.x, ubuntu-latest, arm64)

pk.KeySignature undefined (type *packet.PlayerSessionServer has no field or method KeySignature)

Check failure on line 113 in server/session.go

View workflow job for this annotation

GitHub Actions / build (1.21.x, ubuntu-latest, arm64)

pk.ExpiresAt undefined (type *packet.PlayerSessionServer has no field or method ExpiresAt)
}
}
}
Expand Down Expand Up @@ -293,11 +297,26 @@ func (p *Session) Kill(message string) {
})
}

func (p *Session) SetSessionID(id [16]byte, pk []byte) {
func (p *Session) SetSessionID(id [16]byte, pk, ks []byte, expires int64) {
p.mu.Lock()
defer p.mu.Unlock()
p.sessionID = id
p.publicKey = pk
p.keySignature = ks
p.expires = uint64(expires)

p.Server.GlobalBroadcast(&packet.PlayerInfoUpdate{
Actions: 0x02,
Players: []types.PlayerInfo{
{
UUID: p.conn.UUID(),
ChatSessionID: id,

Check failure on line 313 in server/session.go

View workflow job for this annotation

GitHub Actions / build (1.21.x, ubuntu-latest, amd64)

unknown field ChatSessionID in struct literal of type struct{UUID [16]byte; Name string; Properties []types.Property; GameMode int32; Ping int32; HasDisplayName bool; DisplayName string; ExpiresAt int64; PublicKey []byte; KeySignature []byte; Listed bool}

Check failure on line 313 in server/session.go

View workflow job for this annotation

GitHub Actions / build (1.21.x, ubuntu-latest, arm64)

unknown field ChatSessionID in struct literal of type struct{UUID [16]byte; Name string; Properties []types.Property; GameMode int32; Ping int32; HasDisplayName bool; DisplayName string; ExpiresAt int64; PublicKey []byte; KeySignature []byte; Listed bool}
PublicKey: pk,
KeySignature: ks,
ExpiresAt: expires,
},
},
})
}

func (p *Session) Position() (x, y, z float64) {
Expand Down Expand Up @@ -543,12 +562,12 @@ func (p *Session) Chat(pk *packet.ChatMessageServer) {
p.Server.GlobalMessage(msg, p)
} else {
p.Server.GlobalBroadcast(&packet.PlayerChatMessage{
Sender: p.conn.UUID(),
//MessageSignature: pk.Signature,
Message: pk.Message,
Timestamp: pk.Timestamp,
Salt: pk.Salt,
NetworkName: p.Name(),
Sender: p.conn.UUID(),
MessageSignature: pk.Signature,
Message: pk.Message,
Timestamp: pk.Timestamp,
Salt: pk.Salt,
NetworkName: p.Name(),
})
}
}
Expand Down

0 comments on commit 8f0d451

Please sign in to comment.