Merge branch 'main' of ssh://ssh.git.vh7.uk:222/jakew/echo-go
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
commit
859bcf7e44
3 changed files with 26 additions and 11 deletions
|
@ -7,6 +7,8 @@ services:
|
|||
- name: echo-server
|
||||
pull: always
|
||||
image: ghcr.io/will-scargill/echo:latest
|
||||
environment:
|
||||
ECHO_MYSQL_DB: "1"
|
||||
|
||||
steps:
|
||||
- name: test
|
||||
|
|
33
main.go
33
main.go
|
@ -14,8 +14,6 @@ import (
|
|||
"git.vh7.uk/jakew/echo-go/crypto"
|
||||
)
|
||||
|
||||
const EchoVersion = "3.17"
|
||||
|
||||
func randomHex(n int) (string, error) {
|
||||
b := make([]byte, n)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
|
@ -79,16 +77,31 @@ func (c *Client) Send(msgType MessageType, data *string, subType *string, metada
|
|||
return err
|
||||
}
|
||||
|
||||
func (c *Client) rawReceive(messageEnding string) (string, error) {
|
||||
received := ""
|
||||
|
||||
for !strings.HasSuffix(received, messageEnding) {
|
||||
data := make([]byte, 20480)
|
||||
_, err := c.Con.Read(data)
|
||||
if err != nil {
|
||||
return received, err
|
||||
}
|
||||
|
||||
received = received + string(bytes.Trim(data, "\x00"))
|
||||
}
|
||||
|
||||
return received, nil
|
||||
}
|
||||
|
||||
func (c *Client) ReceivePlain() ([]RawMessage, error) {
|
||||
data := make([]byte, 20480)
|
||||
_, err := c.Con.Read(data)
|
||||
received, err := c.rawReceive("}")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
messages := []RawMessage{}
|
||||
|
||||
for _, message := range strings.Split(string(bytes.Trim(data, "\x00")), "}") {
|
||||
for _, message := range strings.Split(received, "}") {
|
||||
if strings.TrimSpace(message) == "" {
|
||||
continue
|
||||
}
|
||||
|
@ -106,15 +119,14 @@ func (c *Client) ReceivePlain() ([]RawMessage, error) {
|
|||
}
|
||||
|
||||
func (c *Client) Receive() ([]RawMessage, error) {
|
||||
data := make([]byte, 20480)
|
||||
_, err := c.Con.Read(data)
|
||||
received, err := c.rawReceive("]")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
messages := []RawMessage{}
|
||||
|
||||
for _, message := range strings.Split(string(bytes.Trim(data, "\x00")), "]") {
|
||||
for _, message := range strings.Split(received, "]") {
|
||||
if strings.TrimSpace(message) == "" {
|
||||
continue
|
||||
}
|
||||
|
@ -141,7 +153,7 @@ func (c *Client) Receive() ([]RawMessage, error) {
|
|||
return messages, nil
|
||||
}
|
||||
|
||||
func (c *Client) handshakeLoop(password string) error {
|
||||
func (c *Client) HandshakeLoop(clientVersion string, password string) error {
|
||||
log.Println("sending server info request")
|
||||
err := c.SendPlain(ReqServerInfo, nil, nil, nil)
|
||||
if err != nil {
|
||||
|
@ -175,7 +187,8 @@ func (c *Client) handshakeLoop(password string) error {
|
|||
data, err := json.Marshal([]string{
|
||||
c.Username,
|
||||
password,
|
||||
EchoVersion,
|
||||
clientVersion,
|
||||
"",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -8,7 +8,7 @@ func TestCanConnect(t *testing.T) {
|
|||
t.Fatalf("failed to create client: %v", err)
|
||||
}
|
||||
|
||||
err = client.handshakeLoop("mypassword")
|
||||
err = client.HandshakeLoop("mypassword", "4.0.0")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to run handshake loop: %v", err)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue