Skip to content

Commit

Permalink
net.Listener.ApprovePlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
oq-x committed Aug 13, 2024
1 parent a1a8de7 commit b0eeaae
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 5 deletions.
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ func main() {

cfg := loadConfig()

w := world.NewWorld(cfg.LevelName)
w, err := world.NewWorld(cfg.LevelName)
if err != nil {
log.Errorlnf("Error preparing level: %v", w)
return
}

log.Infof("Binding server to %s:%d\n", cfg.ServerIp, cfg.ServerPort)

Expand Down
4 changes: 4 additions & 0 deletions net/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"

"github.com/zeppelinmc/zeppelin/net/packet/status"
"github.com/zeppelinmc/zeppelin/text"
)

type Config struct {
Expand Down Expand Up @@ -34,6 +35,9 @@ func (c Config) New() (*Listener, error) {

newConns: make(chan *Conn),
err: make(chan error),
ApprovePlayer: func(c *Conn) (ok bool, disconnectionReason *text.TextComponent) {
return true, nil
},
}
lis.privKey, err = rsa.GenerateKey(rand.Reader, 1024)

Expand Down
13 changes: 11 additions & 2 deletions net/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ var pkpool = sync.Pool{
}

func (conn *Conn) WritePacket(pk packet.Packet) error {
var packetBuf = pkbufpool.Get().(*bytes.Buffer)
var packetBuf = pkpool.Get().(*bytes.Buffer)
packetBuf.Reset()
defer pkbufpool.Put(packetBuf)
defer pkpool.Put(packetBuf)

w := io.NewWriter(packetBuf)

Expand Down Expand Up @@ -402,6 +402,15 @@ func (conn *Conn) handleHandshake() bool {
if !ok {
return false
}
if ok, r := conn.listener.ApprovePlayer(conn); !ok {
var reason = text.Sprint("Disconnected")
if r != nil {
reason = *r
}
conn.WritePacket(&login.Disconnect{Reason: reason})
conn.Close()
return false
}
conn.username = loginStart.Name
conn.uuid = loginStart.PlayerUUID

Expand Down
4 changes: 4 additions & 0 deletions net/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"crypto/rsa"
"fmt"
"net"

"github.com/zeppelinmc/zeppelin/text"
)

const (
Expand All @@ -22,6 +24,8 @@ type Listener struct {
net.Listener
cfg Config

ApprovePlayer func(*Conn) (ok bool, disconnectionReason *text.TextComponent)

newConns chan *Conn
err chan error

Expand Down
15 changes: 15 additions & 0 deletions server/player/list/playerlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Package list provides parsing of playerlist files (whitelist.json, ops.json etc)

package list

type WhitelistPlayer struct {
UUID string `json:"uuid"`
Name string `json:"name"`
}

type OperatorPlayer struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Level int32 `json:"level"`
BypassesPlayerLimit bool `json:"bypassesPlayerLimit"`
}
2 changes: 2 additions & 0 deletions server/world/level/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/zeppelinmc/zeppelin/server/world/level/seed"
)

var SessionLock = []byte{0xE2, 0x98, 0x83}

// A game rule is a string containing an integer or a boolean
type GameRule string

Expand Down
23 changes: 21 additions & 2 deletions server/world/world.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package world

import (
"fmt"
"os"
"strings"

Expand All @@ -19,11 +20,13 @@ type World struct {

levelPrepared bool

lock *os.File

path string
worldAge, dayTime atomic.AtomicValue[int64]
}

func NewWorld(path string) *World {
func NewWorld(path string) (*World, error) {
var err error
w := &World{
path: path,
Expand All @@ -33,6 +36,11 @@ func NewWorld(path string) *World {
if err != nil {
w.prepareLevel()
}

if w.obtainLock() != nil {
return nil, fmt.Errorf("failed to obtain session.lock")
}

w.worldAge = atomic.Value(w.Level.Data.Time)
w.dayTime = atomic.Value(w.Level.Data.DayTime)
w.dimensions = map[string]*dimension.Dimension{
Expand All @@ -46,7 +54,7 @@ func NewWorld(path string) *World {
),
}

return w
return w, nil
}

func (w *World) prepareLevel() {
Expand All @@ -60,6 +68,16 @@ func (w *World) prepareLevel() {
os.MkdirAll(w.path+"/DIM1/region", 0755)
}

func (w *World) obtainLock() error {
f, err := os.OpenFile(w.path+"/session.lock", os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
}
f.Write(level.SessionLock)
w.lock = f
return nil
}

// returns the dimension struct for the dimension name
func (w *World) Dimension(name string) *dimension.Dimension {
if !strings.Contains(name, ":") {
Expand All @@ -74,6 +92,7 @@ func (w *World) Save() {
dim.Save()
log.Infoln("Saved dimension", dim.Name())
}
w.lock.Close()
}

func (w *World) RegisterDimension(name string, dim *dimension.Dimension) {
Expand Down

0 comments on commit b0eeaae

Please sign in to comment.