version compat system start
This commit is contained in:
+14
-3
@@ -7,13 +7,24 @@ import (
|
||||
"github.com/admin-else/strom/proto_base"
|
||||
)
|
||||
|
||||
// Connect connects to the given address note that it does not resolve the SRV records its just a raw net.Dial
|
||||
// Connect connects to the given address note that it does not resolve the SRV records it's just a raw net.Dial
|
||||
func Connect(addr string) (ret *proto.Conn, err error) {
|
||||
ret = &proto.Conn{}
|
||||
err = ret.SetVersion("1.21.8")
|
||||
s, err := Status(addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ret, err = ConnectVersionLess(addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = ret.SetProtocolVersion(s.Version.Protocol)
|
||||
return
|
||||
}
|
||||
|
||||
// ConnectVersionLess connects to the given address without setting the version you should set it
|
||||
// when using this function.
|
||||
func ConnectVersionLess(addr string) (ret *proto.Conn, err error) {
|
||||
ret = &proto.Conn{}
|
||||
ret.SetState(proto_base.Handshaking)
|
||||
ret.SetCompressionThreshold(-1)
|
||||
ret.Actor = proto_base.Client
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/admin-else/strom/proto"
|
||||
"github.com/admin-else/strom/proto_base"
|
||||
"github.com/admin-else/strom/proto_generated/v1_21_8"
|
||||
)
|
||||
|
||||
func MakeHandshakePacket(s *proto.Conn, nextState proto_base.State) (p *v1_21_8.HandshakingToServerPacketSetProtocol, err error) {
|
||||
p = &v1_21_8.HandshakingToServerPacketSetProtocol{NextState: int32(nextState), ProtocolVersion: s.ProtocolVersion}
|
||||
var portStr string
|
||||
p.ServerHost, portStr, err = net.SplitHostPort(s.RemoteAddr().String())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var portUint uint64
|
||||
portUint, err = strconv.ParseUint(portStr, 10, 16)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
p.ServerPort = uint16(portUint)
|
||||
return
|
||||
}
|
||||
+7
-19
@@ -7,8 +7,6 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/admin-else/strom/api"
|
||||
"github.com/admin-else/strom/crypto"
|
||||
@@ -51,26 +49,16 @@ func (s *LoginClient) OnClose(_ event.Close) (err error) {
|
||||
}
|
||||
|
||||
func (s *LoginClient) OnStart(_ event.Start) (err error) {
|
||||
p, err := MakeHandshakePacket(s.Conn, proto_base.Login)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if s.ServerHost == "" && s.ServerPort == 0 {
|
||||
var portStr string
|
||||
s.ServerHost, portStr, err = net.SplitHostPort(s.RemoteAddr().String())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var portUint uint64
|
||||
portUint, err = strconv.ParseUint(portStr, 10, 16)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
s.ServerPort = uint16(portUint)
|
||||
p.ServerHost = s.ServerHost
|
||||
p.ServerPort = s.ServerPort
|
||||
}
|
||||
|
||||
err = s.Send(&v1_21_8.HandshakingToServerPacketSetProtocol{
|
||||
ProtocolVersion: s.ProtocolVersion,
|
||||
ServerHost: s.ServerHost,
|
||||
ServerPort: s.ServerPort,
|
||||
NextState: int32(proto_base.Login),
|
||||
})
|
||||
err = s.Send(p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
+41
-17
@@ -1,21 +1,22 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/admin-else/strom/event"
|
||||
"github.com/admin-else/strom/proto"
|
||||
"github.com/admin-else/strom/proto_base"
|
||||
"github.com/admin-else/strom/server"
|
||||
|
||||
"github.com/admin-else/strom/data"
|
||||
"github.com/admin-else/strom/proto_generated/v1_21_8"
|
||||
)
|
||||
|
||||
type StatusClient struct {
|
||||
*proto.Conn
|
||||
Status string
|
||||
Status string
|
||||
PingSendTime, PingReceiveTime time.Time
|
||||
}
|
||||
|
||||
func (s *StatusClient) Default(event any) (err error) {
|
||||
@@ -24,21 +25,11 @@ func (s *StatusClient) Default(event any) (err error) {
|
||||
}
|
||||
|
||||
func (s *StatusClient) OnStart(_ event.Start) (err error) {
|
||||
parts := strings.Split(s.RemoteAddr().String(), ":")
|
||||
versionData, err := data.LookUpVersionByName(s.Version)
|
||||
p, err := MakeHandshakePacket(s.Conn, proto_base.Status)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
port, err := strconv.Atoi(parts[1])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = s.Send(&v1_21_8.HandshakingToServerPacketSetProtocol{
|
||||
ProtocolVersion: versionData.Version,
|
||||
ServerHost: parts[0],
|
||||
ServerPort: uint16(port),
|
||||
NextState: int32(proto_base.Status),
|
||||
})
|
||||
err = s.Send(p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -47,8 +38,41 @@ func (s *StatusClient) OnStart(_ event.Start) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *StatusClient) OnStatus(p v1_21_8.StatusToClientPacketServerInfo) (err error) {
|
||||
func (s *StatusClient) OnStatus(p *v1_21_8.StatusToClientPacketServerInfo) (err error) {
|
||||
s.Status = p.Response
|
||||
s.PingSendTime = time.Now()
|
||||
err = s.Send(&v1_21_8.StatusToServerPacketPing{Time: s.PingSendTime.UnixMilli()})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *StatusClient) OnPong(p *v1_21_8.StatusToClientPacketPing) (err error) {
|
||||
s.PingReceiveTime = time.Now()
|
||||
err = event.HandlerDoneErr{}
|
||||
return
|
||||
}
|
||||
|
||||
func StatusRaw(addr string) (s *StatusClient, err error) {
|
||||
c, err := ConnectVersionLess(addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = c.SetVersion("1.21.8")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
s = &StatusClient{
|
||||
Conn: c,
|
||||
}
|
||||
err = s.Start(s)
|
||||
defer c.Close()
|
||||
return
|
||||
}
|
||||
|
||||
func Status(addr string) (status server.StatusResponse, err error) {
|
||||
s, err := StatusRaw(addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = json.Unmarshal([]byte(s.Status), &status)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user