-
Notifications
You must be signed in to change notification settings - Fork 6
/
common.go
264 lines (235 loc) · 5.85 KB
/
common.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package ttyc
import (
"bytes"
"encoding/json"
"fmt"
"github.com/Depau/ttyc/utils"
"github.com/TwinProduction/go-color"
strftimeMod "github.com/lestrrat-go/strftime"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"runtime"
"strings"
"time"
)
const VERSION = "v0.4"
const COPYRIGHT = "Copyright (c) 2022 Davide Depau\n\n" +
"License: GNU GPL version 3.0 or later <https://www.gnu.org/licenses/gpl-3.0.html>.\n" +
"This is free software; see the source for copying conditions. There is NO\n" +
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
var Strftime, _ = strftimeMod.New("%H:%M:%S")
type TokenDTO struct {
Token string `json:"token"`
}
type Implementation uint8
const (
ImplementationTtyd = iota
ImplementationWiSe
)
type SttyDTO struct {
Baudrate *uint `json:"baudrate"`
Databits *uint8 `json:"databits"`
Stopbits *uint8 `json:"stopbits"`
Parity *string `json:"parity"`
}
type sttyInDTO struct {
Baudrate uint `json:"baudrate"`
Databits uint8 `json:"bits"`
Stopbits uint8 `json:"stop"`
Parity *int `json:"parity"`
}
const (
UrlForToken = iota
UrlForWebSocket
UrlForStty
UrlForStats
UrlForWhoami
)
func GetUrlFor(urlFor int, baseURL *url.URL) (outUrl *url.URL) {
outUrl, _ = url.Parse(baseURL.String())
switch urlFor {
case UrlForToken:
outUrl.Path = path.Join(baseURL.Path, "token")
case UrlForStty:
outUrl.Path = path.Join(baseURL.Path, "stty")
case UrlForStats:
outUrl.Path = path.Join(baseURL.Path, "stats")
case UrlForWhoami:
outUrl.Path = path.Join(baseURL.Path, "whoami")
case UrlForWebSocket:
if baseURL.Scheme == "https" {
outUrl.Scheme = "wss"
} else {
outUrl.Scheme = "ws"
}
outUrl.Path = path.Join(baseURL.Path, "ws")
default:
panic("invalid urlfor\n")
}
return
}
func Handshake(url *url.URL, credentials *url.Userinfo) (token string, impl Implementation, server string, err error) {
var resp *http.Response
var body []byte
if resp, err = http.Get(url.String()); err != nil {
Trace()
return
}
resp, err = utils.EnsureAuth(resp, credentials, nil)
if err != nil {
Trace()
return
}
defer resp.Body.Close()
impl = ImplementationTtyd
server = ""
if srv := resp.Header.Get("Server"); srv != "" {
server = srv
if strings.Contains(strings.ToLower(srv), "wi-se") {
impl = ImplementationWiSe
}
}
if body, err = ioutil.ReadAll(resp.Body); err != nil {
Trace()
return
}
dto := TokenDTO{}
if err = json.Unmarshal(body, &dto); err != nil {
Trace()
return
}
token = dto.Token
return
}
func parseStty(body []byte) (stty SttyDTO, err error) {
sttyIn := sttyInDTO{}
err = json.Unmarshal(body, &sttyIn)
if err != nil {
Trace()
return
}
stty = SttyDTO{
Baudrate: &sttyIn.Baudrate,
Databits: &sttyIn.Databits,
Stopbits: &sttyIn.Stopbits,
}
if sttyIn.Parity == nil {
stty.Parity = nil
} else {
var parity string
if *sttyIn.Parity == 0 {
parity = "even"
} else {
parity = "odd"
}
stty.Parity = &parity
}
return
}
func GetStty(url *url.URL, credentials *url.Userinfo) (stty SttyDTO, err error) {
httpClient := http.Client{
Timeout: 3 * time.Second,
}
resp, err := httpClient.Get(url.String())
if err != nil {
Trace()
return
}
resp, err = utils.EnsureAuth(resp, credentials, nil)
if err != nil {
Trace()
return
}
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
Trace()
return
}
stty, err = parseStty(buf)
return
}
func Stty(url *url.URL, credentials *url.Userinfo, dto *SttyDTO) (stty SttyDTO, err error) {
// Generate json manually since golang can't generate it properly
var jsonItems []string
if dto.Baudrate != nil {
jsonItems = append(jsonItems, fmt.Sprintf("\"baudrate\": %d", *dto.Baudrate))
}
if dto.Stopbits != nil {
jsonItems = append(jsonItems, fmt.Sprintf("\"stop\": %d", *dto.Stopbits))
}
if dto.Databits != nil {
jsonItems = append(jsonItems, fmt.Sprintf("\"bits\": %d", *dto.Databits))
}
if dto.Parity != nil {
if *dto.Parity == "none" {
jsonItems = append(jsonItems, "\"parity\": null")
} else if *dto.Parity == "even" {
jsonItems = append(jsonItems, "\"parity\": 0")
} else if *dto.Parity == "odd" {
jsonItems = append(jsonItems, "\"parity\": 1")
}
}
var sb strings.Builder
sb.WriteString("{")
sb.WriteString(strings.Join(jsonItems, ","))
sb.WriteString("}")
resp, err := http.Post(url.String(), "application/json", bytes.NewBuffer([]byte(sb.String())))
if err != nil {
Trace()
return
}
resp, err = utils.EnsureAuth(resp, credentials, bytes.NewBuffer([]byte(sb.String())))
if err != nil {
Trace()
return
}
if resp.StatusCode != 200 {
err = fmt.Errorf("HTTP status %d", resp.StatusCode)
return
}
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
Trace()
return
}
stty, err = parseStty(buf)
return
}
func PlatformGray() string {
if runtime.GOOS == "windows" {
return color.Gray
}
return "\u001B[1;30m"
}
func PlatformYellow() string {
if runtime.GOOS == "windows" {
return color.Yellow
}
return "\u001B[31m"
}
func TtycErrFprintf(w io.Writer, format string, a ...interface{}) {
// Ignore fprintf errors here since I wasn't planning to care anywhere else regardless
_, _ = fmt.Fprintf(w, color.Red+"[ttyc %s] ", Strftime.FormatString(time.Now()))
_, _ = fmt.Fprintf(w, format, a...)
_, _ = fmt.Fprint(w, color.Reset)
}
func TtycFprintf(w io.Writer, format string, a ...interface{}) {
// Ignore fprintf errors here since I wasn't planning to care anywhere else regardless
_, _ = fmt.Fprintf(w, PlatformYellow()+"[ttyc %s] ", Strftime.FormatString(time.Now()))
_, _ = fmt.Fprintf(w, format, a...)
_, _ = fmt.Fprint(w, color.Reset)
}
func TtycErrPrintf(format string, args ...interface{}) {
TtycErrFprintf(os.Stderr, format, args...)
}
// Cause why not
func TtycAngryPrintf(format string, args ...interface{}) {
TtycErrPrintf(format, args...)
}
func TtycPrintf(format string, args ...interface{}) {
TtycFprintf(os.Stdout, format, args...)
}