From 50d65e579fb1fa1b1199ca9a26233cfc9fde27be Mon Sep 17 00:00:00 2001 From: Sunbread Date: Sat, 4 Jan 2020 23:21:43 +0800 Subject: [PATCH 1/6] Add yggdrasil.Client() --- yggdrasil/yggdrasil.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/yggdrasil/yggdrasil.go b/yggdrasil/yggdrasil.go index 9bf4f87..a0cad1d 100644 --- a/yggdrasil/yggdrasil.go +++ b/yggdrasil/yggdrasil.go @@ -64,3 +64,9 @@ func rawPost(endpoint string, payload interface{}) (*http.Response, error) { // Do return client.Do(PostRequest) } + +// Client return the HTTP client for Yggdrasil. +// Only used when you want to modify settings of the HTTP client. +func Client() *http.Client { + return &client +} From b2a5f7b72061110d9e6de83b7f501548ff10cdd9 Mon Sep 17 00:00:00 2001 From: Sunbread Date: Sat, 4 Jan 2020 23:44:58 +0800 Subject: [PATCH 2/6] Add bot.JoinConn() --- bot/mcbot.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/bot/mcbot.go b/bot/mcbot.go index e45c4c1..1d4f70a 100644 --- a/bot/mcbot.go +++ b/bot/mcbot.go @@ -16,12 +16,27 @@ const ProtocolVersion = 575 // JoinServer connect a Minecraft server for playing the game. func (c *Client) JoinServer(addr string, port int) (err error) { //Connect - c.conn, err = net.DialMC(fmt.Sprintf("%s:%d", addr, port)) + conn, err := net.DialMC(fmt.Sprintf("%s:%d", addr, port)) if err != nil { err = fmt.Errorf("bot: connect server fail: %v", err) return } + //JoinConn + return c.JoinConn(conn) +} + +// JoinConn join a Minecraft server through a connection for playing the game. +func (c *Client) JoinConn(conn *net.Conn) (err error) { + //Set Conn + c.conn = conn + + //Get Addr + strform := c.conn.Socket.RemoteAddr().String() + var addr string + var port int + fmt.Sscanf(strform, "%s:%d", &addr, &port) + //Handshake err = c.conn.WritePacket( //Handshake Packet From 888c98a6d4e71483d1e25db6d60ff1d0c355c872 Mon Sep 17 00:00:00 2001 From: Sunbread Date: Sun, 5 Jan 2020 00:15:50 +0800 Subject: [PATCH 3/6] Change yggdrasil.Client() to yggdrasil.SetClient() --- yggdrasil/yggdrasil.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/yggdrasil/yggdrasil.go b/yggdrasil/yggdrasil.go index a0cad1d..ac54f8a 100644 --- a/yggdrasil/yggdrasil.go +++ b/yggdrasil/yggdrasil.go @@ -65,8 +65,8 @@ func rawPost(endpoint string, payload interface{}) (*http.Response, error) { return client.Do(PostRequest) } -// Client return the HTTP client for Yggdrasil. -// Only used when you want to modify settings of the HTTP client. -func Client() *http.Client { - return &client +// SetClient set the HTTP client for Yggdrasil. +// Only used when you want to use custom HTTP client settings. +func SetClient(newclient http.Client) { + client = newclient } From 933222faf59589c59fc30270786b0f2383733d89 Mon Sep 17 00:00:00 2001 From: Sunbread Date: Sun, 5 Jan 2020 00:33:35 +0800 Subject: [PATCH 4/6] Change bot.JoinConn(*go-mc/net.Conn) to bot.JoinConn(net.Conn) --- bot/mcbot.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/bot/mcbot.go b/bot/mcbot.go index 1d4f70a..b6c9c9f 100644 --- a/bot/mcbot.go +++ b/bot/mcbot.go @@ -6,8 +6,9 @@ package bot import ( "fmt" - "github.com/Tnze/go-mc/net" + mcnet "github.com/Tnze/go-mc/net" pk "github.com/Tnze/go-mc/net/packet" + "net" ) // ProtocolVersion , the protocol version number of minecraft net protocol @@ -16,20 +17,20 @@ const ProtocolVersion = 575 // JoinServer connect a Minecraft server for playing the game. func (c *Client) JoinServer(addr string, port int) (err error) { //Connect - conn, err := net.DialMC(fmt.Sprintf("%s:%d", addr, port)) + conn, err := mcnet.DialMC(fmt.Sprintf("%s:%d", addr, port)) if err != nil { err = fmt.Errorf("bot: connect server fail: %v", err) return } //JoinConn - return c.JoinConn(conn) + return c.JoinConn(conn.Socket) } // JoinConn join a Minecraft server through a connection for playing the game. -func (c *Client) JoinConn(conn *net.Conn) (err error) { +func (c *Client) JoinConn(conn net.Conn) (err error) { //Set Conn - c.conn = conn + c.conn = mcnet.WrapConn(conn) //Get Addr strform := c.conn.Socket.RemoteAddr().String() @@ -105,6 +106,6 @@ func (c *Client) JoinConn(conn *net.Conn) (err error) { // Conn return the MCConn of the Client. // Only used when you want to handle the packets by yourself -func (c *Client) Conn() *net.Conn { +func (c *Client) Conn() *mcnet.Conn { return c.conn } From 72b1820826c187737dc5ed2526881a42c0104e6b Mon Sep 17 00:00:00 2001 From: Tnze Date: Sun, 5 Jan 2020 01:14:38 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E7=94=A8=E6=9B=B4=E7=AE=80=E5=8D=95?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E5=BC=8F=E4=B8=BAbot=E5=8C=85=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/mcbot.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/bot/mcbot.go b/bot/mcbot.go index b6c9c9f..c1e5910 100644 --- a/bot/mcbot.go +++ b/bot/mcbot.go @@ -16,19 +16,26 @@ const ProtocolVersion = 575 // JoinServer connect a Minecraft server for playing the game. func (c *Client) JoinServer(addr string, port int) (err error) { - //Connect - conn, err := mcnet.DialMC(fmt.Sprintf("%s:%d", addr, port)) + conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", addr, port)) if err != nil { err = fmt.Errorf("bot: connect server fail: %v", err) return } + return c.join(conn) +} - //JoinConn - return c.JoinConn(conn.Socket) +// JoinServerWithDialer is similar to JoinServer but using a Dialer. +func (c *Client) JoinServerWithDialer(d Dialer, addr string) (err error) { + conn, err := d.Dial("tcp", addr) + if err != nil { + err = fmt.Errorf("bot: connect server fail: %v", err) + return + } + return c.join(conn) } // JoinConn join a Minecraft server through a connection for playing the game. -func (c *Client) JoinConn(conn net.Conn) (err error) { +func (c *Client) join(conn net.Conn) (err error) { //Set Conn c.conn = mcnet.WrapConn(conn) @@ -104,6 +111,12 @@ func (c *Client) JoinConn(conn net.Conn) (err error) { } } +// A Dialer is a means to establish a connection. +type Dialer interface { + // Dial connects to the given address via the proxy. + Dial(network, addr string) (c net.Conn, err error) +} + // Conn return the MCConn of the Client. // Only used when you want to handle the packets by yourself func (c *Client) Conn() *mcnet.Conn { From 9245a6b9ffebe674fae63c62a1fc3412fd8d6d41 Mon Sep 17 00:00:00 2001 From: Tnze Date: Sun, 5 Jan 2020 01:22:33 +0800 Subject: [PATCH 6/6] =?UTF-8?q?yggdrasil=E6=94=AF=E6=8C=81=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E7=9A=84=E6=96=B9=E5=BC=8F=E4=BB=8D=E9=9C=80=E8=80=83?= =?UTF-8?q?=E8=99=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yggdrasil/yggdrasil.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/yggdrasil/yggdrasil.go b/yggdrasil/yggdrasil.go index ac54f8a..9bf4f87 100644 --- a/yggdrasil/yggdrasil.go +++ b/yggdrasil/yggdrasil.go @@ -64,9 +64,3 @@ func rawPost(endpoint string, payload interface{}) (*http.Response, error) { // Do return client.Do(PostRequest) } - -// SetClient set the HTTP client for Yggdrasil. -// Only used when you want to use custom HTTP client settings. -func SetClient(newclient http.Client) { - client = newclient -}