mirror of
https://github.com/MeowLynxSea/pocketmine-rcon.git
synced 2025-07-09 19:04:38 +00:00
Update connection.go
This commit is contained in:
parent
f8c81edbb6
commit
a78205163c
@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,12 +13,11 @@ type Connection struct {
|
|||||||
addr string
|
addr string
|
||||||
}
|
}
|
||||||
|
|
||||||
var uniqueId int32 = 0
|
var uniqueID int32 = 0
|
||||||
|
|
||||||
func NewConnection(addr, pass string) (*Connection, error) {
|
func NewConnection(addr, pass string) (*Connection, error) {
|
||||||
conn, err := net.Dial("tcp", addr)
|
conn, err := net.Dial("tcp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c := &Connection{conn: conn, pass: pass, addr: addr}
|
c := &Connection{conn: conn, pass: pass, addr: addr}
|
||||||
@ -29,25 +27,36 @@ func NewConnection(addr, pass string) (*Connection, error) {
|
|||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) SendCommand(cmd string) string {
|
func (c *Connection) SendCommand(cmd string) (string, error) {
|
||||||
c.sendCommand(2, []byte(cmd))
|
err := c.sendCommand(2, []byte(cmd))
|
||||||
pkg := c.readPkg()
|
if err != nil {
|
||||||
return string(pkg.Body)
|
return "", err
|
||||||
|
}
|
||||||
|
pkg, err := c.readPkg()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(pkg.Body), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) auth() error {
|
func (c *Connection) auth() error {
|
||||||
c.sendCommand(3, []byte(c.pass))
|
c.sendCommand(3, []byte(c.pass))
|
||||||
pkg := c.readPkg()
|
pkg, err := c.readPkg()
|
||||||
if pkg.Type != 2 || pkg.Id != uniqueId {
|
if err != nil {
|
||||||
return errors.New("Incorrect password.")
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if pkg.Type != 2 || pkg.ID != uniqueID {
|
||||||
|
return errors.New("incorrect password")
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) sendCommand(typ int32, body []byte) {
|
func (c *Connection) sendCommand(typ int32, body []byte) error {
|
||||||
size := int32(4 + 4 + len(body) + 2)
|
size := int32(4 + 4 + len(body) + 2)
|
||||||
uniqueId += 1
|
uniqueID += 1
|
||||||
id := uniqueId
|
id := uniqueID
|
||||||
|
|
||||||
wtr := binaryReadWriter{ByteOrder: binary.LittleEndian}
|
wtr := binaryReadWriter{ByteOrder: binary.LittleEndian}
|
||||||
wtr.Write(size)
|
wtr.Write(size)
|
||||||
@ -56,41 +65,41 @@ func (c *Connection) sendCommand(typ int32, body []byte) {
|
|||||||
wtr.Write(body)
|
wtr.Write(body)
|
||||||
wtr.Write([]byte{0x0, 0x0})
|
wtr.Write([]byte{0x0, 0x0})
|
||||||
if wtr.err != nil {
|
if wtr.err != nil {
|
||||||
log.Println(wtr.err)
|
return wtr.err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.conn.Write(wtr.buf.Bytes())
|
c.conn.Write(wtr.buf.Bytes())
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) readPkg() Pkg {
|
func (c *Connection) readPkg() (pkg, error) {
|
||||||
const bufSize = 4096
|
const bufSize = 4096
|
||||||
b := make([]byte, bufSize)
|
b := make([]byte, bufSize)
|
||||||
|
|
||||||
// Doesn't handle split messages correctly.
|
// Doesn't handle split messages correctly.
|
||||||
read, err := c.conn.Read(b)
|
read, err := c.conn.Read(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
return pkg{}, err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p := Pkg{}
|
p := pkg{}
|
||||||
rdr := binaryReadWriter{ByteOrder: binary.LittleEndian,
|
rdr := binaryReadWriter{ByteOrder: binary.LittleEndian,
|
||||||
buf: bytes.NewBuffer(b)}
|
buf: bytes.NewBuffer(b)}
|
||||||
rdr.Read(&p.Size)
|
rdr.Read(&p.Size)
|
||||||
rdr.Read(&p.Id)
|
rdr.Read(&p.ID)
|
||||||
rdr.Read(&p.Type)
|
rdr.Read(&p.Type)
|
||||||
body := [bufSize - 12]byte{}
|
body := [bufSize - 12]byte{}
|
||||||
rdr.Read(&body)
|
rdr.Read(&body)
|
||||||
if rdr.err != nil {
|
if rdr.err != nil {
|
||||||
log.Println(rdr.err)
|
return p, rdr.err
|
||||||
}
|
}
|
||||||
p.Body = body[:read-12]
|
p.Body = body[:read-12]
|
||||||
return p
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Pkg struct {
|
type pkg struct {
|
||||||
Size int32
|
Size int32
|
||||||
Id int32
|
ID int32
|
||||||
Type int32
|
Type int32
|
||||||
Body []byte
|
Body []byte
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user