-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
329 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package rcon | ||
|
||
import ( | ||
"bufio" | ||
"strings" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/refractorgscm/rcon2/packet" | ||
) | ||
|
||
func (c *Client) sendPacket(p *packet.Packet) error { | ||
out, err := p.Encode() | ||
if err != nil { | ||
return errors.Wrap(err, "could not encode packet") | ||
} | ||
|
||
if err := c.writeToConn(out); err != nil { | ||
return errors.Wrap(err, "could not send packet") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Client) readPacket() (*packet.Packet, error) { | ||
if c.conn == nil { | ||
return nil, ErrNotConnected | ||
} | ||
|
||
if err := c.conn.SetDeadline(time.Time{}); err != nil { | ||
if strings.HasSuffix(err.Error(), "use of closed network connection") { | ||
return nil, ErrNotConnected | ||
} | ||
|
||
return nil, errors.Wrap(err, "could not set connection deadline") | ||
} | ||
|
||
reader := bufio.NewReader(c.conn) | ||
|
||
res, err := packet.Decode(c.config.EndianMode, reader) | ||
if err != nil { | ||
if strings.HasSuffix(err.Error(), "use of closed network connection") { | ||
return nil, ErrNotConnected | ||
} | ||
|
||
return nil, errors.Wrap(err, "could not read packet") | ||
} | ||
|
||
c.log.Debug("Read packet ID: ", res.ID, ", Body: ", string(res.Body)) | ||
|
||
return res, nil | ||
} | ||
|
||
func (c *Client) readPacketTimeout() (*packet.Packet, error) { | ||
if c.conn == nil { | ||
return nil, ErrNotConnected | ||
} | ||
|
||
if err := c.conn.SetDeadline(time.Now().Add(c.config.ReadDeadline)); err != nil { | ||
if strings.HasSuffix(err.Error(), "use of closed network connection") { | ||
return nil, ErrNotConnected | ||
} | ||
|
||
return nil, errors.Wrap(err, "could not set connection deadline") | ||
} | ||
|
||
reader := bufio.NewReader(c.conn) | ||
|
||
res, err := packet.Decode(c.config.EndianMode, reader) | ||
if err != nil { | ||
if strings.HasSuffix(err.Error(), "use of closed network connection") { | ||
return nil, ErrNotConnected | ||
} | ||
|
||
return nil, errors.Wrap(err, "could not read packet") | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func (c *Client) writeToConn(data []byte) error { | ||
_, err := c.conn.Write(data) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package rcon | ||
|
||
import "errors" | ||
|
||
var ErrNotConnected = errors.New("not connected") | ||
var ErrAuthentication = errors.New("authentication failed") | ||
var ErrQueueTimeout = errors.New("queue timeout") | ||
var ErrReadTimeout = errors.New("read timeout") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package fakeserver | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"encoding/binary" | ||
"fmt" | ||
"net" | ||
|
||
"github.com/refractorgscm/rcon2/packet" | ||
) | ||
|
||
var AuthShouldFail = false | ||
|
||
type Server struct { | ||
port string | ||
mode binary.ByteOrder | ||
ctx context.Context | ||
terminate bool | ||
} | ||
|
||
func New(port uint16, mode binary.ByteOrder) *Server { | ||
portStr := fmt.Sprintf(":%d", port) | ||
|
||
return &Server{ | ||
port: portStr, | ||
mode: mode, | ||
} | ||
} | ||
|
||
func (s *Server) Listen() { | ||
l, err := net.Listen("tcp4", s.port) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer l.Close() | ||
|
||
fmt.Printf("Fake server listening on port %s\n", s.port) | ||
|
||
for { | ||
if s.terminate { | ||
break | ||
} | ||
|
||
c, err := l.Accept() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
go s.handleConnection(c) | ||
} | ||
} | ||
|
||
func (s *Server) Stop() { | ||
s.terminate = true | ||
} | ||
|
||
func (s *Server) handleConnection(conn net.Conn) { | ||
for { | ||
reader := bufio.NewReader(conn) | ||
|
||
p, err := packet.Decode(s.mode, reader) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(p) | ||
|
||
var res *packet.Packet | ||
|
||
switch p.Type { | ||
case packet.TypeAuth: | ||
res = s.handleAuthReq(p) | ||
case packet.TypeCommand: | ||
switch string(p.Body) { | ||
case "help": | ||
res = packet.New(p.Mode, packet.TypeCommandRes, []byte("firstplayer\notherplayer\nlastplayer"), nil) | ||
default: | ||
res = packet.New(p.Mode, packet.TypeCommandRes, []byte("Unknown command"), nil) | ||
} | ||
} | ||
|
||
out, _ := res.Encode() | ||
|
||
printBytes(out) | ||
|
||
conn.Write(out) | ||
} | ||
} | ||
|
||
func (s *Server) handleAuthReq(p *packet.Packet) *packet.Packet { | ||
res := packet.New(s.mode, packet.TypeAuthRes, []byte{}, []int32{}) | ||
res.ID = p.ID | ||
|
||
if AuthShouldFail { | ||
res.ID = packet.AuthFailedID | ||
return res | ||
} | ||
|
||
return res | ||
} | ||
|
||
func printBytes(arr []byte) { | ||
fmt.Printf("Bytes (%d): ", len(arr)) | ||
for _, b := range arr { | ||
fmt.Printf("%x ", b) | ||
} | ||
fmt.Print("\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module github.com/refractorgscm/rcon | ||
module github.com/refractorgscm/rcon2 | ||
|
||
go 1.17 | ||
|
||
|
Oops, something went wrong.