net

package
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 30, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package net pack network connection for Minecraft.

Index

Examples

Constants

View Source
const DefaultPort = 25565
View Source
const MaxRCONPackageSize = 4096

Variables

View Source
var DefaultDialer = Dialer{}

Functions

This section is empty.

Types

type Conn

type Conn struct {
	Socket net.Conn
	io.Reader
	io.Writer
	// contains filtered or unexported fields
}

Conn is a minecraft Connection

func DialMC

func DialMC(addr string) (*Conn, error)

DialMC create a Minecraft connection Lookup SRV records only if port doesn't exist or equals to 0.

func DialMCTimeout

func DialMCTimeout(addr string, timeout time.Duration) (*Conn, error)

DialMCTimeout acts like DialMC but takes a timeout.

func WrapConn

func WrapConn(conn net.Conn) *Conn

WrapConn warp a net.Conn to MC-Conn Helps you modify the connection process (e.g. using DialContext).

func (*Conn) Close

func (c *Conn) Close() error

Close the connection

func (*Conn) ReadPacket

func (c *Conn) ReadPacket(p *pk.Packet) error

ReadPacket read a Packet from Conn.

func (*Conn) SetCipher

func (c *Conn) SetCipher(ecoStream, decoStream cipher.Stream)

SetCipher load the decode/encode stream to this Conn

func (*Conn) SetThreshold

func (c *Conn) SetThreshold(t int)

SetThreshold set threshold to Conn. The data packet with length equal or longer then threshold will be compressed when sending.

func (*Conn) WritePacket

func (c *Conn) WritePacket(p pk.Packet) error

WritePacket write a Packet to Conn.

type Dialer

type Dialer net.Dialer

Dialer implements MCDialer interface.

It can be easily convert from net.Dialer.

dialer := net.Dialer{}
mcDialer := (*Dialer)(&dialer)

func (*Dialer) DialMCContext

func (d *Dialer) DialMCContext(ctx context.Context, addr string) (*Conn, error)

type Listener

type Listener struct{ net.Listener }

A Listener is a minecraft Listener

func ListenMC

func ListenMC(addr string) (*Listener, error)

ListenMC listen as TCP but Accept a mc Conn

func (Listener) Accept

func (l Listener) Accept() (Conn, error)

Accept a minecraft Conn

type MCDialer

type MCDialer interface {
	// The DialMCContext dial TCP connection to a minecraft server, and warp the net.Conn by calling [WrapConn].
	DialMCContext(ctx context.Context, addr string) (*Conn, error)
}

MCDialer provide DialMCContext method, can be used to dial a minecraft server. Dialer is its default implementation, and support SRV lookup.

Typically, if you want to use built-in proxies or custom dialer, you can hook go-mc/bot package by implement this interface. When implementing a custom MCDialer, SRV lookup is optional.

type RCONClientConn

type RCONClientConn interface {
	Cmd(cmd string) error
	Resp() (resp string, err error)
	Close() error
}

func DialRCON

func DialRCON(addr string, password string) (client RCONClientConn, err error)

DialRCON connect to a RCON server and return the connection after login. We promise the returned RCONClientConn is an RCONConn, so you can convert them by type assertions if you need call the ReadPacket() or WritePacket() methods.

Example
conn, err := DialRCON("localhost:25575", "CORRECT_PASSWORD")
if err != nil {
	panic(err)
}
defer conn.Close()

err = conn.Cmd("TEST COMMAND")
if err != nil {
	panic(err)
}

for {
	// Server may send the result in more(or less) than one packet.
	// See: https://wiki.vg/RCON#Fragmentation
	resp, err := conn.Resp()
	if err != nil {
		fmt.Print(err)
	}
	fmt.Printf("Server response: %q", resp)
	break
}
Output:

type RCONConn

type RCONConn struct {
	net.Conn
	ReqID int32
}

func (*RCONConn) AcceptCmd

func (r *RCONConn) AcceptCmd() (string, error)

func (*RCONConn) AcceptLogin

func (r *RCONConn) AcceptLogin(password string) error

func (*RCONConn) Cmd

func (r *RCONConn) Cmd(cmd string) error

func (*RCONConn) ReadPacket

func (r *RCONConn) ReadPacket() (RequestID, Type int32, Payload string, err error)

func (*RCONConn) Resp

func (r *RCONConn) Resp() (resp string, err error)

func (*RCONConn) RespCmd

func (r *RCONConn) RespCmd(resp string) error

func (*RCONConn) WritePacket

func (r *RCONConn) WritePacket(RequestID, Type int32, Payload string) error

type RCONListener

type RCONListener struct{ net.Listener }

func ListenRCON

func ListenRCON(addr string) (*RCONListener, error)

ListenRCON announces on the local network address, accepting RCON clients.

Example
l, err := ListenRCON("localhost:25575")
if err != nil {
	panic(err)
}
defer l.Close()

for {
	conn, err := l.Accept()
	if err != nil {
		fmt.Printf("Accept connection error: %v", err)
	}

	go func(conn RCONServerConn) {
		err = conn.AcceptLogin("CORRECT_PASSWORD")
		if err != nil {
			fmt.Printf("Login fail: %v", err)
		}
		defer conn.Close()

		// The client is login, we are accepting its command
		for {
			cmd, err := conn.AcceptCmd()
			if err != nil {
				fmt.Printf("Read command fail: %v", err)
				break
			}

			resp := handleCommand(cmd)

			// Return the result of command.
			// It's allowed to call RespCmd multiple times for one command.
			err = conn.RespCmd(resp)
			if err != nil {
				fmt.Printf("Response command fail: %v", err)
				break
			}
		}
	}(conn)
}
Output:

func (*RCONListener) Accept

func (r *RCONListener) Accept() (RCONServerConn, error)

Accept RCON connection for client. We promise the returned RCONServerConn is an RCONConn, so you can convert them by type assertions if you need call the ReadPacket() or WritePacket() methods.

type RCONServerConn

type RCONServerConn interface {
	AcceptLogin(password string) error
	AcceptCmd() (cmd string, err error)
	RespCmd(resp string) error
	Close() error
}

RCONServerConn is the connection in the server side.

type ReadWriter

type ReadWriter interface {
	Reader
	Writer
}

type Reader

type Reader interface {
	ReadPacket() (pk.Packet, error)
}

type Writer

type Writer interface {
	WritePacket(p pk.Packet) error
}

Directories

Path Synopsis
Package CFB8 implements CFB8 block cipher mode of operation used by Minecraft protocol.
Package CFB8 implements CFB8 block cipher mode of operation used by Minecraft protocol.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL