yes a lot of stuff
This commit is contained in:
+12
-4
@@ -1,11 +1,13 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"log"
|
||||
"log/slog"
|
||||
"net"
|
||||
|
||||
"github.com/admin-else/strom/proto"
|
||||
"github.com/admin-else/strom/proto_base"
|
||||
"github.com/admin-else/strom/server/server_modules"
|
||||
"github.com/admin-else/strom/text"
|
||||
)
|
||||
|
||||
func Servee(c net.Conn) (ret *proto.Conn) {
|
||||
@@ -13,7 +15,7 @@ func Servee(c net.Conn) (ret *proto.Conn) {
|
||||
_ = ret.SetVersion("1.21.8") // cant fail
|
||||
ret.SetState(proto_base.Handshaking)
|
||||
ret.CompressionThreshold = -1
|
||||
ret.Actor = proto_base.Server
|
||||
ret.Actor = proto_base.Servee
|
||||
ret.Conn = c
|
||||
ret.R = c
|
||||
ret.W = c
|
||||
@@ -57,9 +59,15 @@ func StartServerWithOnConn(listenAddr string, onConn func(c *proto.Conn) (err er
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
connErr := onConn(Servee(cNet))
|
||||
c := Servee(cNet)
|
||||
defer c.Close()
|
||||
connErr := onConn(c)
|
||||
if connErr != nil {
|
||||
log.Println(connErr)
|
||||
slog.Error("Error while handling client", "error", connErr, "client", c.Conn.RemoteAddr())
|
||||
connErr = server_modules.Kick(c, text.Pretty(connErr.Error()))
|
||||
if connErr != nil {
|
||||
slog.Error("Error while kicking client", "error", connErr, "client", c.Conn.RemoteAddr())
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package server_modules
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/admin-else/strom/nbt"
|
||||
"github.com/admin-else/strom/proto"
|
||||
"github.com/admin-else/strom/proto_base"
|
||||
"github.com/admin-else/strom/proto_generated/v1_21_8"
|
||||
"github.com/admin-else/strom/text"
|
||||
)
|
||||
|
||||
var (
|
||||
NotClientErr = errors.New("not a servee")
|
||||
CantKickInStateErr = errors.New("cant kick in this state")
|
||||
)
|
||||
|
||||
func Kick(c *proto.Conn, reason *text.Component) (err error) {
|
||||
if c.Actor != proto_base.Servee {
|
||||
err = NotClientErr
|
||||
return
|
||||
}
|
||||
switch c.State() {
|
||||
case proto_base.Handshaking, proto_base.Status:
|
||||
err = CantKickInStateErr
|
||||
case proto_base.Login:
|
||||
var b []byte
|
||||
b, err = reason.MarshalJSON()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = c.Send(&v1_21_8.LoginToClientPacketDisconnect{Reason: string(b)})
|
||||
case proto_base.Configuration:
|
||||
err = c.Send(&v1_21_8.ConfigurationToClientPacketDisconnect{Reason: nbt.Anon{Value: reason.ToNBT()}})
|
||||
case proto_base.Play:
|
||||
err = c.Send(&v1_21_8.PlayToClientPacketKickDisconnect{Reason: nbt.Anon{Value: reason.ToNBT()}})
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
package server_modules
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"slices"
|
||||
|
||||
"github.com/admin-else/strom/api"
|
||||
"github.com/admin-else/strom/event"
|
||||
@@ -11,6 +15,8 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var StatusServedError = errors.New("status served")
|
||||
|
||||
var UnexpectedStatusRequest = UnexpectedNextStateError{proto_base.Status}
|
||||
|
||||
type NameAndUUID struct {
|
||||
@@ -26,25 +32,51 @@ func (u UnexpectedNextStateError) Error() string {
|
||||
return fmt.Sprintf("unexpected next state: %v", u.NextState)
|
||||
}
|
||||
|
||||
var IncompatibleProtocolVersionError = fmt.Errorf("incompatible protocol version")
|
||||
|
||||
type LoginServer struct {
|
||||
*proto.Conn
|
||||
//OnlineMode bool FIXME: implement this
|
||||
ServerHost string
|
||||
ServerPort uint16
|
||||
Requested NameAndUUID
|
||||
Given *NameAndUUID
|
||||
CompressionThreshold int32
|
||||
Status []byte
|
||||
CompatibleVersions []int32
|
||||
}
|
||||
|
||||
func (l *LoginServer) OnHandshake(packet *v1_21_8.HandshakingToServerPacketSetProtocol) (err error) {
|
||||
l.SetState(proto_base.State(packet.NextState))
|
||||
l.ServerHost = packet.ServerHost
|
||||
if packet.NextState != int32(proto_base.Login) {
|
||||
l.ServerPort = packet.ServerPort
|
||||
|
||||
if len(l.CompatibleVersions) != 0 {
|
||||
if !slices.Contains(l.CompatibleVersions, packet.ProtocolVersion) {
|
||||
err = IncompatibleProtocolVersionError
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if packet.NextState != int32(proto_base.Login) && !(l.Status != nil && packet.NextState == int32(proto_base.Status)) {
|
||||
err = UnexpectedNextStateError{proto_base.State(packet.NextState)}
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (l *LoginServer) OnStatusRequest(_ *v1_21_8.StatusToServerPacketPingStart) (err error) {
|
||||
err = l.Send(&v1_21_8.StatusToClientPacketServerInfo{Response: string(l.Status)})
|
||||
return
|
||||
}
|
||||
|
||||
func (l *LoginServer) OnStatusPing(packet *v1_21_8.StatusToServerPacketPing) (err error) {
|
||||
err = l.Send(&v1_21_8.StatusToClientPacketPing{Time: packet.Time})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return event.HandlerDoneErr{Return: StatusServedError}
|
||||
}
|
||||
|
||||
func (l *LoginServer) SetCompressionThreshold(threshold int32) (err error) {
|
||||
err = l.Send(&v1_21_8.LoginToClientPacketCompress{Threshold: threshold})
|
||||
if err != nil {
|
||||
@@ -73,7 +105,7 @@ func (l *LoginServer) OnLoginStart(packet *v1_21_8.LoginToServerPacketLoginStart
|
||||
|
||||
func (l *LoginServer) OnLoginAcknowledged(_ *v1_21_8.LoginToServerPacketLoginAcknowledged) (err error) {
|
||||
l.SetState(proto_base.Configuration)
|
||||
err = event.ErrHandlerDone
|
||||
err = event.HandlerDoneErr{}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -91,18 +123,59 @@ func (l *LoginServer) OnCycle(_ event.Tick) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func ServeLogin(c *proto.Conn) (ret *LoginServer, err error) {
|
||||
ret = &LoginServer{Conn: c}
|
||||
ret.CompressionThreshold = 256
|
||||
err = ret.Start(ret)
|
||||
return
|
||||
type LoginServerSetting func(*LoginServer)
|
||||
|
||||
func WithOtherAccount(a *api.Account) LoginServerSetting {
|
||||
return func(s *LoginServer) {
|
||||
if s.Given == nil {
|
||||
return
|
||||
}
|
||||
s.Given = &NameAndUUID{
|
||||
Name: a.Name,
|
||||
UUID: a.Uuid,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ServeLoginWithOtherAccount(c *proto.Conn, a *api.Account) (ret *LoginServer, err error) {
|
||||
ret = &LoginServer{Conn: c, Given: &NameAndUUID{
|
||||
Name: a.Name,
|
||||
UUID: a.Uuid,
|
||||
}}
|
||||
func WithRawStatus(status []byte) LoginServerSetting {
|
||||
return func(s *LoginServer) {
|
||||
s.Status = status
|
||||
}
|
||||
}
|
||||
|
||||
func WithStatus(response StatusResponse) LoginServerSetting {
|
||||
var status []byte
|
||||
status, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
slog.Debug("With status", "status", status)
|
||||
return WithRawStatus(status)
|
||||
}
|
||||
|
||||
func WithCompatibleVersion(versions int32) LoginServerSetting {
|
||||
return WithCompatibleVersions([]int32{versions})
|
||||
}
|
||||
|
||||
func WithCompatibleVersionsRange(start, end int32) LoginServerSetting {
|
||||
var versions []int32
|
||||
for i := start; i <= end; i++ {
|
||||
versions = append(versions, i)
|
||||
}
|
||||
return WithCompatibleVersions(versions)
|
||||
}
|
||||
|
||||
func WithCompatibleVersions(versions []int32) LoginServerSetting {
|
||||
return func(s *LoginServer) {
|
||||
s.CompatibleVersions = versions
|
||||
}
|
||||
}
|
||||
|
||||
func ServeLogin(c *proto.Conn, settings ...LoginServerSetting) (ret *LoginServer, err error) {
|
||||
ret = &LoginServer{Conn: c}
|
||||
for _, s := range settings {
|
||||
s(ret)
|
||||
}
|
||||
ret.CompressionThreshold = 256
|
||||
err = ret.Start(ret)
|
||||
return
|
||||
|
||||
@@ -5,27 +5,26 @@ import (
|
||||
|
||||
"github.com/admin-else/strom/proto"
|
||||
"github.com/admin-else/strom/proto_generated/v1_21_8"
|
||||
"github.com/admin-else/strom/text"
|
||||
)
|
||||
|
||||
// StatusResponse here is the structure I used: https://minecraft.wiki/w/Java_Edition_protocol/Server_List_Ping#Status_Response
|
||||
type StatusResponse struct {
|
||||
Version struct {
|
||||
Name string `json:"name"`
|
||||
Protocol int `json:"protocol"`
|
||||
Name *text.Component `json:"name"`
|
||||
Protocol int `json:"protocol"`
|
||||
} `json:"version"`
|
||||
Players struct {
|
||||
Max int `json:"max"`
|
||||
Online int `json:"online"`
|
||||
Sample []struct {
|
||||
Name string `json:"name"`
|
||||
ID string `json:"id"`
|
||||
Name *text.Component `json:"name"`
|
||||
ID string `json:"id"`
|
||||
} `json:"sample"`
|
||||
} `json:"players"`
|
||||
Description struct {
|
||||
Text string `json:"text"`
|
||||
} `json:"description"`
|
||||
Favicon string `json:"favicon"`
|
||||
EnforcesSecureChat bool `json:"enforcesSecureChat"`
|
||||
Description *text.Component `json:"description"`
|
||||
Favicon string `json:"favicon"`
|
||||
EnforcesSecureChat bool `json:"enforcesSecureChat"`
|
||||
}
|
||||
|
||||
type StatusServer struct {
|
||||
|
||||
Reference in New Issue
Block a user