-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud.go
191 lines (156 loc) · 4.18 KB
/
cloud.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
package scratchgonnect
import (
"encoding/json"
"net/http"
"strings"
"time"
websocket "github.com/gorilla/websocket"
)
// Structs
type handshake_cloud struct {
Method string `json:"method"`
Username string `json:"user"`
ProjectId string `json:"project_id"`
}
type set_cloud struct {
Method string `json:"method"`
Username string `json:"user"`
ProjectId string `json:"project_id"`
Name string `json:"name"`
Value int `json:"value"`
}
type CloudSocket struct {
Connection *websocket.Conn
ProjectId string
Variables []CloudVariable
Username string
}
type CloudVariable struct {
Name string
Value int
}
type fn func(*CloudSocket, string, int)
var default_timeout time.Duration = 5000000000
// Struct functions
func (c CloudSocket) DisconnectCloud() {
// Closes websocket connection
c.Connection.Close()
}
func (c CloudSocket) SetVariable(varname string, value int) {
// Sets a cloud variable
if get_variable_key(c.Variables, varname) != value {
c.Connection.SetWriteDeadline(time.Now().Add(default_timeout))
err := c.Connection.WriteJSON(set_cloud{
Method: "set",
Username: c.Username,
ProjectId: c.ProjectId,
Name: "☁ " + varname,
Value: value,
})
if err != nil {
panic(err)
}
for i, variable := range c.Variables {
if variable.Name == "☁ "+varname {
c.Variables[i].Value = value
break
}
}
}
}
func (c CloudSocket) GetVariable(varname string) int {
// Get variable value from CloudSocket
for _, variable := range c.Variables {
if variable.Name == "☁ "+varname {
return variable.Value
}
}
panic("Couldn't find variable!")
}
func (c CloudSocket) Listen(f fn) {
// Perform infinite loop for handling cloud events
for {
c.Connection.SetReadDeadline(time.Time{})
// Wait for a message to get recieved
_, p, err := c.Connection.ReadMessage()
if err != nil {
panic(err)
}
// Update cloud variable values and fire listener function
sets := strings.Split(string(p), "\n")
for _, set_action := range sets {
action_decoded := set_cloud{}
err := json.Unmarshal([]byte(set_action), &action_decoded)
if err != nil {
panic(err)
}
if action_decoded.Method == "set" {
for i, cloud_var := range c.Variables {
if cloud_var.Name == action_decoded.Name {
c.Variables[i].Value = action_decoded.Value
f(&c, cloud_var.Name, action_decoded.Value)
}
}
}
}
}
}
// Functions
func get_variable_key(variables []CloudVariable, name string) int {
// Search for variable name in CloudVariable array and return its value
for _, variable := range variables {
if variable.Name == "☁ "+name {
return variable.Value
}
}
return 0
}
func ConnectTurbowarpCloud(username string, project_id string) *CloudSocket {
header := http.Header{}
//header.Add("Cookie", "scratchsessionsid="+s.SessionId+";")
header.Set("Origin", "https://turbowarp.org")
header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36")
header.Set("Host", "clouddata.turbowarp.org")
// Connect to Turbowarp cloud websocket
c, _, err := websocket.DefaultDialer.Dial("wss://clouddata.turbowarp.org/", header)
if err != nil {
panic(err)
}
// If succesfull make a new CloudSocket object
new_socket := CloudSocket{
Connection: c,
ProjectId: project_id,
Username: username,
}
c.SetWriteDeadline(time.Now().Add(default_timeout))
// Perform handshake with Turbowarp cloud websocket
err = c.WriteJSON(handshake_cloud{
Method: "handshake",
Username: username,
ProjectId: project_id,
})
if err != nil {
panic(err)
}
c.SetReadDeadline(time.Now().Add(default_timeout))
// Receive information about current cloud data values
_, p, err := c.ReadMessage()
if err != nil {
panic(err)
}
sets := strings.Split(string(p), "\n")
for _, set_action := range sets {
action_decoded := set_cloud{}
err := json.Unmarshal([]byte(set_action), &action_decoded)
if err != nil {
panic(err)
}
if action_decoded.Method == "set" {
new_socket.Variables = append(new_socket.Variables, CloudVariable{
Name: action_decoded.Name,
Value: action_decoded.Value,
})
}
}
return &new_socket
}