-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
179 lines (146 loc) · 3.96 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package goxbdm
import (
"errors"
"strconv"
"strings"
"fmt"
"regexp"
"github.com/0xdeafcafe/go-xbdm/clients"
)
// Client ..
type Client struct {
tcpClient *clients.TCPClient
consoleName string
ConsoleIP string
}
const (
defaultXboxTCPPort = 730
)
// DebugName returns the debug name of the Xbox.
func (client *Client) DebugName() (string, error) {
return client.SendCommand("dbgname")
}
// SendCommand sends a text command to the Xbox.
func (client *Client) SendCommand(command string) (string, error) {
_, err := client.tcpClient.WriteString(command)
if err != nil {
return "", err
}
resp, err := client.tcpClient.ReadString()
if err != nil {
return "", err
}
_, successful, message := validateResponse(resp)
if !successful {
return "", errors.New(message)
}
return message, nil
}
// ReadMultilineResponse reads the body of a multiline response and returns it.
func (client *Client) ReadMultilineResponse() ([]string, error) {
lines := make([]string, 0)
for {
str, err := client.tcpClient.ReadString()
if err != nil {
return lines, nil
}
if str == "." {
return lines, nil
}
lines = append(lines, str)
}
}
// ConsoleName returns the name of the console at the time the connection was established.
func (client *Client) ConsoleName() string {
return client.consoleName
}
// Close ends the connection with the Xbox.
func (client *Client) Close() {
client.tcpClient.Close()
}
// NewXBDMClient creates a new XBDM client.
func NewXBDMClient(xboxIP string) (*Client, error) {
return NewXBDMClientWithPort(xboxIP, defaultXboxTCPPort)
}
// NewXBDMClientWithPort creates a new XBDM client with a custom port.
func NewXBDMClientWithPort(xboxIP string, port int) (*Client, error) {
tcpClient, err := clients.NewTCPClientWithPort(xboxIP, port)
if err != nil {
return nil, err
}
// Create client
client := &Client{
tcpClient: tcpClient,
ConsoleIP: xboxIP,
}
// Store debug console name
dbgName, err := client.DebugName()
if err != nil {
return nil, err
}
client.consoleName = dbgName
return client, nil
}
// parseMultilineResponse reads the space separated values into a map.
func parseMultilineResponse(str string) (map[string]string, error) {
body := make(map[string]string)
// Remove header and eom
splitStr := strings.Split(str, "\r\n")
dataLines := splitStr[1 : len(splitStr)-2]
// Join array with spaces, then add an extra space at the end to save last entry
dataStr := strings.Join(dataLines, " ") + " "
currentKey := ""
currentValue := ""
isInsideQuote := false
onKey := true
for _, char := range dataStr {
// If we detect a space, and we aren't inside a double quote, switch to `onKey`
if char == ' ' && !isInsideQuote {
// Before we switch we need to save the read values to the `body` map and
// reset the `currentKey` and `currentValue` variables
body[currentKey] = currentValue
currentKey = ""
currentValue = ""
onKey = true
continue
}
// If we find an equals, and we aren't inside a double quote, switch to `!onKey`
if char == '=' && !isInsideQuote {
onKey = false
continue
}
// If we find a double quote, switch between inside and outside
if char == '"' {
isInsideQuote = !isInsideQuote
}
// Save the value to the relevant part
if onKey {
currentKey = fmt.Sprintf("%s%s", currentKey, string(char))
} else {
currentValue = fmt.Sprintf("%s%s", currentValue, string(char))
}
}
return body, nil
}
// validateResponse validates a string response from the Xbox.
func validateResponse(str string) (status int, successful bool, message string) {
r := regexp.MustCompile(`(?P<status>[\d]{3})-\s(?P<message>.*)`)
matches := r.FindStringSubmatch(str)
names := r.SubexpNames()
if len(matches) != 3 {
return -1, false, str
}
status = -1
message = ""
for i := range matches {
switch names[i] {
case "status":
status, _ = strconv.Atoi(matches[i])
break
case "message":
message = matches[i]
break
}
}
return status, status >= 200 && status <= 299, message
}