Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
df0e5e354b | ||
|
|
23b5a5d9b7 | ||
|
|
46770ed980 | ||
|
|
6179c5f6b6 |
+64
-37
@@ -9,32 +9,40 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/admin-else/strom/crypto"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Servers struct {
|
||||
ServicesHost string
|
||||
SessionHost string
|
||||
ProfilesHost string
|
||||
}
|
||||
|
||||
var DefaultServers = &Servers{
|
||||
ServicesHost: "https://api.minecraftservices.com",
|
||||
SessionHost: "https://sessionserver.mojang.com",
|
||||
ProfilesHost: "https://api.mojang.com",
|
||||
}
|
||||
|
||||
type Account struct {
|
||||
*Servers
|
||||
Name string
|
||||
Uuid uuid.UUID
|
||||
Ygg string
|
||||
}
|
||||
|
||||
const (
|
||||
BaseUrlApi = "https://api.minecraftservices.com"
|
||||
)
|
||||
|
||||
func (a *Account) doMcApiRequest(method string, url string, from any, to any) (err error) {
|
||||
var b []byte
|
||||
if method != "GET" {
|
||||
b, err = json.Marshal(from)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
r, err := http.NewRequest(method, url, bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
@@ -44,21 +52,22 @@ func (a *Account) doMcApiRequest(method string, url string, from any, to any) (e
|
||||
|
||||
resp, err := http.DefaultClient.Do(r)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if !(resp.StatusCode >= 200 && resp.StatusCode <= 299) {
|
||||
return fmt.Errorf("bad status code %v", resp.StatusCode)
|
||||
}
|
||||
b, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
err = json.Unmarshal(b, to)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
type PlayerKeys struct {
|
||||
@@ -73,7 +82,7 @@ type PlayerKeys struct {
|
||||
}
|
||||
|
||||
func (a *Account) FetchKeys() (keys PlayerKeys, err error) {
|
||||
err = a.doMcApiRequest("POST", BaseUrlApi+"/player/certificates", nil, &keys)
|
||||
err = a.doMcApiRequest("POST", a.ServicesHost+"/player/certificates", nil, &keys)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -90,10 +99,12 @@ func (a *Account) JoinServer(serverId string) (err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
resp, err := http.Post("https://sessionserver.mojang.com/session/minecraft/join", "application/json", bytes.NewReader(body))
|
||||
resp, err := http.Post(a.SessionHost+"/session/minecraft/join", "application/json", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 204 {
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
@@ -112,17 +123,10 @@ type NameChangResp struct {
|
||||
}
|
||||
|
||||
func (a *Account) NameChangeInfo() (r NameChangResp, err error) {
|
||||
err = a.doMcApiRequest("GET", "https://api.minecraftservices.com/minecraft/profile/namechange", nil, &r)
|
||||
err = a.doMcApiRequest("GET", a.ServicesHost+"/minecraft/profile/namechange", nil, &r)
|
||||
return
|
||||
}
|
||||
|
||||
func NewOfflineAccount(name string) *Account {
|
||||
return &Account{
|
||||
Name: name,
|
||||
Uuid: crypto.FromOfflinePlayer(name),
|
||||
}
|
||||
}
|
||||
|
||||
// PlayerProfile represents the player's profile information
|
||||
type PlayerProfile struct {
|
||||
ID string `json:"id"`
|
||||
@@ -143,7 +147,7 @@ type PlayerProfile struct {
|
||||
|
||||
// QueryProfile queries the player's profile
|
||||
func (a *Account) QueryProfile() (profile PlayerProfile, err error) {
|
||||
err = a.doMcApiRequest("GET", BaseUrlApi+"/minecraft/profile", nil, &profile)
|
||||
err = a.doMcApiRequest("GET", a.ServicesHost+"/minecraft/profile", nil, &profile)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -181,7 +185,7 @@ type PlayerAttributes struct {
|
||||
|
||||
// QueryAttributes queries the player's attributes
|
||||
func (a *Account) QueryAttributes() (attrs PlayerAttributes, err error) {
|
||||
err = a.doMcApiRequest("GET", BaseUrlApi+"/player/attributes", nil, &attrs)
|
||||
err = a.doMcApiRequest("GET", a.ServicesHost+"/player/attributes", nil, &attrs)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -194,7 +198,7 @@ type ModifyAttributesRequest struct {
|
||||
|
||||
// ModifyAttributes modifies the player's attributes
|
||||
func (a *Account) ModifyAttributes(req ModifyAttributesRequest) (attrs PlayerAttributes, err error) {
|
||||
err = a.doMcApiRequest("POST", BaseUrlApi+"/player/attributes", req, &attrs)
|
||||
err = a.doMcApiRequest("POST", a.ServicesHost+"/player/attributes", req, &attrs)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -205,24 +209,23 @@ type BlockedUsers struct {
|
||||
|
||||
// GetBlocklist gets the list of blocked users
|
||||
func (a *Account) GetBlocklist() (blocklist BlockedUsers, err error) {
|
||||
err = a.doMcApiRequest("GET", BaseUrlApi+"/privacy/blocklist", nil, &blocklist)
|
||||
err = a.doMcApiRequest("GET", a.ServicesHost+"/privacy/blocklist", nil, &blocklist)
|
||||
return
|
||||
}
|
||||
|
||||
// CheckNameAvailability checks if a name is available
|
||||
type NameAvailability struct {
|
||||
Status string `json:"status"` // DUPLICATE, AVAILABLE, NOT_ALLOWED
|
||||
}
|
||||
|
||||
// CheckNameAvailability checks if a name is available
|
||||
func (a *Account) CheckNameAvailability(name string) (avail NameAvailability, err error) {
|
||||
err = a.doMcApiRequest("GET", BaseUrlApi+"/minecraft/profile/name/"+name+"/available", nil, &avail)
|
||||
err = a.doMcApiRequest("GET", a.ServicesHost+"/minecraft/profile/name/"+name+"/available", nil, &avail)
|
||||
return
|
||||
}
|
||||
|
||||
// ChangeName changes the player's name
|
||||
func (a *Account) ChangeName(newName string) (profile PlayerProfile, err error) {
|
||||
err = a.doMcApiRequest("PUT", BaseUrlApi+"/minecraft/profile/name/"+newName, nil, &profile)
|
||||
err = a.doMcApiRequest("PUT", a.ServicesHost+"/minecraft/profile/name/"+newName, nil, &profile)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -234,19 +237,19 @@ type ChangeSkinRequest struct {
|
||||
|
||||
// ChangeSkin changes the player's skin
|
||||
func (a *Account) ChangeSkin(req ChangeSkinRequest) (profile PlayerProfile, err error) {
|
||||
err = a.doMcApiRequest("POST", BaseUrlApi+"/minecraft/profile/skins", req, &profile)
|
||||
err = a.doMcApiRequest("POST", a.ServicesHost+"/minecraft/profile/skins", req, &profile)
|
||||
return
|
||||
}
|
||||
|
||||
// ResetSkin resets the player's skin to default
|
||||
func (a *Account) ResetSkin() (profile PlayerProfile, err error) {
|
||||
err = a.doMcApiRequest("DELETE", BaseUrlApi+"/minecraft/profile/skins/active", nil, &profile)
|
||||
err = a.doMcApiRequest("DELETE", a.ServicesHost+"/minecraft/profile/skins/active", nil, &profile)
|
||||
return
|
||||
}
|
||||
|
||||
// HideCape hides the player's active cape
|
||||
func (a *Account) HideCape() (profile PlayerProfile, err error) {
|
||||
err = a.doMcApiRequest("DELETE", BaseUrlApi+"/minecraft/profile/capes/active", nil, &profile)
|
||||
err = a.doMcApiRequest("DELETE", a.ServicesHost+"/minecraft/profile/capes/active", nil, &profile)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -258,29 +261,53 @@ type ShowCapeRequest struct {
|
||||
// ShowCape shows a specific cape
|
||||
func (a *Account) ShowCape(capeID string) (profile PlayerProfile, err error) {
|
||||
req := ShowCapeRequest{CapeID: capeID}
|
||||
err = a.doMcApiRequest("PUT", BaseUrlApi+"/minecraft/profile/capes/active", req, &profile)
|
||||
err = a.doMcApiRequest("PUT", a.ServicesHost+"/minecraft/profile/capes/active", req, &profile)
|
||||
return
|
||||
}
|
||||
|
||||
// CheckGiftCodeValidity checks if a gift code is valid
|
||||
func (a *Account) CheckGiftCodeValidity(giftCode string) (valid bool, err error) {
|
||||
url := BaseUrlApi + "/productvoucher/giftcode"
|
||||
url := a.ServicesHost + "/productvoucher/" + giftCode
|
||||
r, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return
|
||||
}
|
||||
|
||||
r.Header.Set("Authorization", "Bearer "+a.Ygg)
|
||||
|
||||
resp, err := http.DefaultClient.Do(r)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Returns HTTP 200 or 204 if valid, 404 if invalid
|
||||
if resp.StatusCode == 200 || resp.StatusCode == 204 {
|
||||
return true, nil
|
||||
valid = true
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func NewAccountFromYGG(ygg string) (a *Account, err error) {
|
||||
a = &Account{
|
||||
Ygg: ygg,
|
||||
Servers: DefaultServers,
|
||||
}
|
||||
p, err := a.QueryProfile()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
a.Name = p.Name
|
||||
a.Uuid, err = uuid.Parse(strings.ReplaceAll(p.ID, "-", ""))
|
||||
return
|
||||
}
|
||||
|
||||
func NewAccount(name string, uuid uuid.UUID, ygg string) (a *Account) {
|
||||
return &Account{
|
||||
Name: name,
|
||||
Uuid: uuid,
|
||||
Ygg: ygg,
|
||||
Servers: DefaultServers,
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package api
|
||||
|
||||
import "github.com/admin-else/strom/crypto"
|
||||
|
||||
func NewOfflineAccount(name string) *Account {
|
||||
return &Account{
|
||||
Name: name,
|
||||
Uuid: crypto.FromOfflinePlayer(name),
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ func MakeHandshakePacket(c *proto.Conn, nextState proto_base.State) (p *v1_21_8.
|
||||
return MakeHandshakePacketAddr(c, nextState, c.RemoteAddr().String())
|
||||
}
|
||||
|
||||
// MakeHandshakePacketAddr gets a custom addr
|
||||
func MakeHandshakePacketAddr(s *proto.Conn, nextState proto_base.State, addr string) (p *v1_21_8.HandshakingToServerPacketSetProtocol, err error) {
|
||||
p = &v1_21_8.HandshakingToServerPacketSetProtocol{NextState: int32(nextState), ProtocolVersion: s.ProtocolVersion}
|
||||
var portStr string
|
||||
|
||||
+16
-3
@@ -127,7 +127,7 @@ func (s *LoginClient) OnStart() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func Login(c *proto.Conn, account *api.Account) (err error) {
|
||||
func LoginRaw(c *proto.Conn, account *api.Account, addr string) (err error) {
|
||||
lc := &LoginClient{
|
||||
Conn: c,
|
||||
Account: account,
|
||||
@@ -159,11 +159,24 @@ func Login(c *proto.Conn, account *api.Account) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func ConnectAndLogin(connectTo string, account *api.Account) (c *proto.Conn, err error) {
|
||||
func LoginNoSrv(connectTo string, account *api.Account) (c *proto.Conn, err error) {
|
||||
c, err = Connect(connectTo)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = Login(c, account)
|
||||
err = LoginRaw(c, account, connectTo)
|
||||
return
|
||||
}
|
||||
|
||||
func Login(connectTo string, account *api.Account) (c *proto.Conn, err error) {
|
||||
connectTo, err = DoDnsSimple(connectTo)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
c, err = Connect(connectTo)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = LoginRaw(c, account, connectTo)
|
||||
return
|
||||
}
|
||||
|
||||
+13
-2
@@ -37,7 +37,8 @@ func (s *StatusClient) OnPong(p *v1_21_8.StatusToClientPacketPing) (err error) {
|
||||
}
|
||||
|
||||
// StatusRaw returns a StatusClient that is not connected to a server.
|
||||
// So ignore the resource leak warning. And maybe attach a warn ignore comment
|
||||
// So ignore the resource leak warning. And maybe attach a warn ignore comment.
|
||||
// It does not resolve SRV records.
|
||||
func StatusRaw(addr string) (s *StatusClient, err error) {
|
||||
c, err := ConnectVersionLess(addr)
|
||||
if err != nil {
|
||||
@@ -69,7 +70,8 @@ func StatusRaw(addr string) (s *StatusClient, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func Status(addr string) (status server.StatusResponse, err error) {
|
||||
// StatusNoDns is like Status but does not resolve SRV records.
|
||||
func StatusNoDns(addr string) (status server.StatusResponse, err error) {
|
||||
s, err := StatusRaw(addr)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -77,3 +79,12 @@ func Status(addr string) (status server.StatusResponse, err error) {
|
||||
err = json.Unmarshal([]byte(s.Status), &status)
|
||||
return
|
||||
}
|
||||
|
||||
// Status resolves the status of the minecraft server at the given address.
|
||||
func Status(addr string) (status server.StatusResponse, err error) {
|
||||
addr, err = DoDnsSimple(addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return StatusNoDns(addr)
|
||||
}
|
||||
|
||||
@@ -8,9 +8,10 @@ import (
|
||||
"os"
|
||||
"slices"
|
||||
|
||||
"github.com/admin-else/strom/tools/cli/offline_uuid"
|
||||
"github.com/admin-else/strom/tools/cli/print_nbt"
|
||||
"github.com/admin-else/strom/tools/cli/status"
|
||||
"github.com/admin-else/strom/cmd/strom/offline_uuid"
|
||||
"github.com/admin-else/strom/cmd/strom/packet_inspector"
|
||||
"github.com/admin-else/strom/cmd/strom/print_nbt"
|
||||
"github.com/admin-else/strom/cmd/strom/status"
|
||||
)
|
||||
|
||||
var ExpectedASubcommandErr = errors.New("expected a subcommand")
|
||||
@@ -20,6 +21,7 @@ var subcommands = map[string]func(args []string) error{
|
||||
"offline-uuid": offline_uuid.Run,
|
||||
"print-nbt": print_nbt.Run,
|
||||
"status": status.Run,
|
||||
"packet-spy": packet_inspector.Run,
|
||||
}
|
||||
|
||||
func Help(args []string) (err error) {
|
||||
@@ -0,0 +1,118 @@
|
||||
package packet_inspector
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log/slog"
|
||||
|
||||
"github.com/admin-else/strom/api"
|
||||
"github.com/admin-else/strom/client"
|
||||
"github.com/admin-else/strom/event"
|
||||
"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_11"
|
||||
"github.com/admin-else/strom/server"
|
||||
"github.com/admin-else/strom/text"
|
||||
)
|
||||
|
||||
var Cmd = flag.NewFlagSet("packet-inspector", flag.ContinueOnError)
|
||||
|
||||
var OfflineNameFlag = Cmd.String("name", "packet_inspector", "offline player name")
|
||||
var Token = Cmd.String("token", "", "token to use")
|
||||
|
||||
var TargetAddr = Cmd.String("addr", "127.0.0.1:25565", "address to connect to")
|
||||
var ListenAddr = Cmd.String("listen", "127.0.0.1:25566", "address to listen on")
|
||||
|
||||
type Proxy struct {
|
||||
Client, Servee *proto.Conn
|
||||
Data nbt.Tag
|
||||
}
|
||||
|
||||
func (p *Proxy) Start() (err error) {
|
||||
p.Client.RegisterCritical(p.OnAnything)
|
||||
p.Servee.RegisterCritical(p.OnAnything)
|
||||
|
||||
p.Servee.RegisterCriticalUntilLatest(p.OnFinishConfiguration)
|
||||
|
||||
errChan := make(chan error)
|
||||
go func() {
|
||||
errChan <- p.Client.StartConn()
|
||||
}()
|
||||
go func() {
|
||||
errChan <- p.Servee.StartConn()
|
||||
}()
|
||||
err = <-errChan
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Proxy) OnAnything(e event.Anything) (err error) {
|
||||
packet, ok := e.Val.(proto_base.EncodeDecodeAble)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
packetInfo, ok := proto.LookupPacketInfoByType(packet)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
switch packetInfo.Direction {
|
||||
case proto_base.ToServer:
|
||||
err = p.Client.Send(packet)
|
||||
case proto_base.ToClient:
|
||||
err = p.Servee.Send(packet)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Proxy) OnFinishConfiguration(packet *v1_21_11.ConfigurationToServerPacketFinishConfiguration) (err error) {
|
||||
err = p.Client.Send(packet)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
p.Servee.SetState(proto_base.Play)
|
||||
p.Client.SetState(proto_base.Play)
|
||||
err = event.DontForwardErr
|
||||
return
|
||||
}
|
||||
|
||||
var StatusResponse = server.StatusResponse{
|
||||
Version: server.StatusResponseVersion{
|
||||
Name: text.Pretty("STROM"),
|
||||
Protocol: 772,
|
||||
},
|
||||
Players: server.StatusResponsePlayers{},
|
||||
Description: text.Pretty("Packet spy forwards to " + *TargetAddr),
|
||||
Favicon: "",
|
||||
EnforcesSecureChat: false,
|
||||
}
|
||||
|
||||
func Run(args []string) (err error) {
|
||||
err = Cmd.Parse(args)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
slog.SetLogLoggerLevel(slog.LevelDebug)
|
||||
slog.Info("Starting packet inspector", "listen", *ListenAddr)
|
||||
err = server.StartServerWithOnConn(*ListenAddr, func(serveeConn *proto.Conn) (err error) {
|
||||
p := &Proxy{Servee: serveeConn, Client: nil}
|
||||
var acc = api.NewOfflineAccount(*OfflineNameFlag)
|
||||
if *Token != "" {
|
||||
acc, err = api.NewAccountFromYGG(*Token)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
_, err = server.ServeLogin(serveeConn, server.WithOtherAccount(acc), server.WithStatus(StatusResponse))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
c, err := client.Login(*TargetAddr, acc)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer c.Close()
|
||||
p.Client = c
|
||||
err = p.Start()
|
||||
return
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -125,6 +125,10 @@ func (l *Loop) RegisterDirect(k reflect.Type, h reflect.Value) {
|
||||
l.HandlerFunctions[k] = append(l.HandlerFunctions[k], h)
|
||||
}
|
||||
|
||||
func (l *Loop) RegisterIgnore(t any) {
|
||||
l.RegisterDirect(reflect.TypeOf(t), reflect.ValueOf(func(any) error { return nil }))
|
||||
}
|
||||
|
||||
// FireFound triggers all handlers registered for the specified event type and returns if handlers were found or an error occurred.
|
||||
func (l *Loop) FireFound(event any) (found bool, err error) {
|
||||
var handlers []reflect.Value
|
||||
|
||||
@@ -63,6 +63,7 @@ func (l *LoginServer) OnHandshake(packet *v1_21_8.HandshakingToServerPacketSetPr
|
||||
return
|
||||
}
|
||||
|
||||
err = l.SetProtocolVersion(packet.ProtocolVersion)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -159,6 +160,7 @@ func WithCompatibleVersions(versions ...int32) LoginServerSetting {
|
||||
func ServeLogin(c *proto.Conn, settings ...LoginServerSetting) (ret *LoginServer, err error) {
|
||||
ret = &LoginServer{Conn: c}
|
||||
ret.RegisterCritical(ret.OnDefault)
|
||||
ret.RegisterIgnore(event.Close{})
|
||||
ret.RegisterUntilLatest(ret.OnHandshake)
|
||||
ret.RegisterUntilLatest(ret.OnLoginStart)
|
||||
ret.RegisterUntilLatest(ret.OnLoginAcknowledged)
|
||||
@@ -168,6 +170,6 @@ func ServeLogin(c *proto.Conn, settings ...LoginServerSetting) (ret *LoginServer
|
||||
s(ret)
|
||||
}
|
||||
ret.CompressionThreshold = 256
|
||||
err = ret.StartLoop()
|
||||
err = ret.StartConn()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"log/slog"
|
||||
"net"
|
||||
|
||||
"github.com/admin-else/strom/event"
|
||||
"github.com/admin-else/strom/proto"
|
||||
"github.com/admin-else/strom/proto_base"
|
||||
"github.com/admin-else/strom/text"
|
||||
@@ -19,6 +20,7 @@ func Servee(c net.Conn) (ret *proto.Conn) {
|
||||
ret.Conn = c
|
||||
ret.R = c
|
||||
ret.W = c
|
||||
ret.Loop = event.NewLoop()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package failed_packets_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/admin-else/strom/proto_generated/v1_21_8"
|
||||
)
|
||||
|
||||
// Error: %v
|
||||
func TestFailedPacket%10X(t *testing.T) {
|
||||
p := v1_21_8.Play%vPacket{}
|
||||
b := bytes.NewBuffer(%#v)
|
||||
err := p.Decode(b)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
@@ -9,105 +10,76 @@ import (
|
||||
"github.com/admin-else/strom/api"
|
||||
"github.com/admin-else/strom/client"
|
||||
"github.com/admin-else/strom/event"
|
||||
"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/proto_generated/v1_21_11"
|
||||
"github.com/admin-else/strom/server"
|
||||
"github.com/admin-else/strom/text"
|
||||
)
|
||||
|
||||
type ProxyClient struct {
|
||||
*proto.Conn
|
||||
Server *Proxy
|
||||
}
|
||||
|
||||
func (p *ProxyClient) OnDefault(event event.Unhandled) (err error) {
|
||||
packet, isPacket := event.Val.(proto_base.EncodeDecodeAble)
|
||||
if !isPacket {
|
||||
return
|
||||
}
|
||||
err = p.Server.Send(packet)
|
||||
return
|
||||
}
|
||||
|
||||
func (p *ProxyClient) OnCycle(_ event.Tick) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (p *ProxyClient) OnStart() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (p *ProxyClient) OnUnCodeAble(packet *proto.UnCodablePacket) (err error) {
|
||||
err = p.OnDefault(event.Unhandled{Val: packet})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
SaveUnCodeAbleAsTest(proto_base.ToServer, packet)
|
||||
return
|
||||
}
|
||||
//go:embed failed_packet.go.tmpl
|
||||
var TestSrcF string
|
||||
|
||||
type Proxy struct {
|
||||
*proto.Conn
|
||||
Client *ProxyClient
|
||||
Client, Servee *proto.Conn
|
||||
Data nbt.Tag
|
||||
}
|
||||
|
||||
func (p *Proxy) OnDefault(event event.Unhandled) (err error) {
|
||||
packet, isPacket := event.Val.(proto_base.EncodeDecodeAble)
|
||||
if !isPacket {
|
||||
func (p *Proxy) Start() (err error) {
|
||||
p.Client.RegisterCritical(p.OnAnything)
|
||||
p.Servee.RegisterCritical(p.OnAnything)
|
||||
|
||||
p.Servee.Register(p.OnUnCodeAble)
|
||||
p.Client.Register(p.OnUnCodeAble)
|
||||
|
||||
p.Servee.RegisterCriticalUntilLatest(p.OnFinishConfiguration)
|
||||
|
||||
errChan := make(chan error)
|
||||
go func() {
|
||||
errChan <- p.Client.StartConn()
|
||||
}()
|
||||
go func() {
|
||||
errChan <- p.Servee.StartConn()
|
||||
}()
|
||||
err = <-errChan
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Proxy) OnAnything(e event.Anything) (err error) {
|
||||
packet, ok := e.Val.(proto_base.EncodeDecodeAble)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
packetInfo, ok := proto.LookupPacketInfoByType(packet)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
switch packetInfo.Direction {
|
||||
case proto_base.ToServer:
|
||||
err = p.Client.Send(packet)
|
||||
case proto_base.ToClient:
|
||||
err = p.Servee.Send(packet)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Proxy) OnCycle(_ event.Tick) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Proxy) OnStart() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Proxy) OnFinishConfiguration(packet *v1_21_8.ConfigurationToServerPacketFinishConfiguration) (err error) {
|
||||
err = p.OnDefault(event.Unhandled{Val: packet})
|
||||
func (p *Proxy) OnFinishConfiguration(packet *v1_21_11.ConfigurationToServerPacketFinishConfiguration) (err error) {
|
||||
err = p.Client.Send(packet)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
p.SetState(proto_base.Play)
|
||||
p.Servee.SetState(proto_base.Play)
|
||||
p.Client.SetState(proto_base.Play)
|
||||
err = event.DontForwardErr
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Proxy) OnUnCodeAble(packet *proto.UnCodablePacket) (err error) {
|
||||
err = p.OnDefault(event.Unhandled{Val: packet})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
SaveUnCodeAbleAsTest(proto_base.ToClient, packet)
|
||||
SaveUnCodeAbleAsTest(packet.Direction, packet)
|
||||
return
|
||||
}
|
||||
|
||||
const TestSrcF = `package failed_packets_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/admin-else/strom/proto_generated/v1_21_8"
|
||||
)
|
||||
|
||||
// Error: %v
|
||||
func TestFailedPacket%10X(t *testing.T) {
|
||||
p := v1_21_8.Play%vPacket{}
|
||||
b := bytes.NewBuffer(%#v)
|
||||
err := p.Decode(b)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
func SaveUnCodeAbleAsTest(d proto_base.Direction, packet *proto.UnCodablePacket) {
|
||||
hUntrimmed := sha256.Sum256(packet.Data)
|
||||
h := hUntrimmed[:8]
|
||||
@@ -138,28 +110,20 @@ var StatusResponse = server.StatusResponse{
|
||||
|
||||
func main() {
|
||||
slog.SetLogLoggerLevel(slog.LevelDebug)
|
||||
err := server.StartServerWithOnConn(":25565", func(serveeConn *proto.Conn) (err error) {
|
||||
p := &Proxy{Conn: serveeConn, Client: nil}
|
||||
err := server.StartServerWithOnConn(":25566", func(serveeConn *proto.Conn) (err error) {
|
||||
p := &Proxy{Servee: serveeConn, Client: nil}
|
||||
acc := api.NewOfflineAccount("sigma")
|
||||
_, err = server.ServeLogin(serveeConn, server.WithOtherAccount(acc), server.WithStatus(StatusResponse))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
c, err := client.ConnectAndLogin("127.0.0.1:25566", acc)
|
||||
c, err := client.LoginNoSrv("127.0.0.1:25565", acc)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
pc := &ProxyClient{Conn: c, Server: p}
|
||||
p.Client = pc
|
||||
errChan := make(chan error)
|
||||
defer pc.Close()
|
||||
go func() {
|
||||
errChan <- pc.StartConn()
|
||||
}()
|
||||
go func() {
|
||||
errChan <- p.StartConn()
|
||||
}()
|
||||
err = <-errChan
|
||||
defer c.Close()
|
||||
p.Client = c
|
||||
err = p.Start()
|
||||
return
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user