From 8f0d4512615194e75733a393d5c30a2629a43946 Mon Sep 17 00:00:00 2001 From: Angel Date: Sat, 21 Oct 2023 22:52:19 -0400 Subject: [PATCH] secure chat --- main.go | 6 ++- server/network/handlers/chat_command.go | 2 +- server/network/handlers/player_session.go | 4 +- server/server.go | 37 +++++++++++++++++- server/session.go | 47 ++++++++++++++++------- 5 files changed, 76 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index 9091eb1..e552e32 100644 --- a/main.go +++ b/main.go @@ -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) }() @@ -62,6 +65,8 @@ func start(cfg *server.Config) { } } +var cfg server.Config + func main() { log.Info("Starting Dynamite 1.20.1 server") if util.HasArg("-prof") { @@ -69,7 +74,6 @@ func main() { 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 diff --git a/server/network/handlers/chat_command.go b/server/network/handlers/chat_command.go index 53f72cf..dcc863b 100644 --- a/server/network/handlers/chat_command.go +++ b/server/network/handlers/chat_command.go @@ -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) { diff --git a/server/network/handlers/player_session.go b/server/network/handlers/player_session.go index a8afad4..b11d970 100644 --- a/server/network/handlers/player_session.go +++ b/server/network/handlers/player_session.go @@ -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) } diff --git a/server/server.go b/server/server.go index 39bd28c..70b94d8 100644 --- a/server/server.go +++ b/server/server.go @@ -2,6 +2,7 @@ package server import ( "errors" + "github.com/aimjel/minecraft/protocol/types" "os" "strings" "sync" @@ -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) diff --git a/server/session.go b/server/session.go index 1554fb5..3162026 100644 --- a/server/session.go +++ b/server/session.go @@ -1,6 +1,7 @@ package server import ( + "github.com/aimjel/minecraft/protocol/types" "math" "math/rand" "slices" @@ -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{} @@ -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 { @@ -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) } } } @@ -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, + PublicKey: pk, + KeySignature: ks, + ExpiresAt: expires, + }, + }, + }) } func (p *Session) Position() (x, y, z float64) { @@ -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(), }) } }