-
Notifications
You must be signed in to change notification settings - Fork 0
/
edistribucion.go
382 lines (319 loc) · 9.17 KB
/
edistribucion.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Porting https://github.com/trocotronic/edistribucion to Go
package edistribucion
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"regexp"
"strings"
"time"
"github.com/gocolly/colly"
"github.com/gocolly/colly/storage"
)
const dashboardURL = "https://zonaprivada.edistribucion.com/areaprivada/s/sfsites/aura?"
var auraConfig AuraConfig
type Context struct {
Mode string `json:"mode"`
App string `json:"app"`
Fwuid string `json:"fwuid"`
Loaded Loaded `json:"loaded"`
Apce uint `json:"apce"`
Apck string `json:"apck"`
Mlr uint `json:"mlr"`
PathPrefix string `json:"pathPrefix"`
Dns string `json:"dns"`
Ls uint `json:"ls"`
}
type Loaded struct {
Token string `json:"APPLICATION@markup://siteforce:loginApp2"`
}
type Action struct {
ID uint `json:"id"`
Descriptor string `json:"descriptor"`
CallingDescriptor string `json:"callingDescriptor"`
Params map[string]string `json:"params"`
}
type Data struct {
Message Message `json:"message"`
Context Context `json:"aura.context"`
PageURI string `json:"aura.pageURI"`
Token string `json:"aura.token"`
}
type Message struct {
Actions []Action `json:"actions"`
}
type LoginResponse struct {
Events []Event `json:"events"`
}
type Event struct {
Descriptor string `json:"descriptor"`
Attributes Attribute `json:"attributes"`
}
type Attribute struct {
Values Value `json:"values"`
}
type Value struct {
Url string `json:"url"`
}
type AuraConfig struct {
Token string `json:"token"`
}
type ActionsResponse struct {
Context Context `json:"context"`
}
type GetLoginActionResponse struct {
ActionsResponse
Actions []GetLoginResponse `json:"actions"`
}
type GetLoginResponse struct {
ID string `json:"id"`
State string `json:"state"`
ReturnValue GetLoginReturnValue `json:"returnValue"`
}
type GetLoginReturnValue struct {
ID string `json:"Id"`
Name string `json:"Name"`
FirstName string `json:"firstName"`
Visibility Visibility `json:"visibility"`
}
type Visibility struct {
ID string `json:"Id"`
}
type CupsActionResponse struct {
ActionsResponse
Actions []CupsResponse `json:"actions"`
}
type Cups struct {
Id string `json:"Id"`
Name string `json:"Name"`
ProvisioningAddress string `json:"Provisioning_address__c"`
ButonLink string `json:"ButtonLink"`
}
type CupsList struct {
Cups []Cups `json:"lstCups"`
}
type CupsResponse struct {
Id string `json:"Id"`
State string `json:"state"`
CupsReturnValue CupsReturnValue `json:"returnValue"`
}
type CupsReturnValue struct {
Data CupsList `json:"data"`
}
type MeterActionResponse struct {
ActionsResponse
Actions []MeterResponse `json:"actions"`
}
type MeterResponse struct {
Id string `json:"Id"`
State string `json:"state"`
MeterReturnValue MeterReturnValue `json:"returnValue"`
}
type MeterReturnValue struct {
Data MeterInfo `json:"data"`
HasWarning bool `json:"hasWarning"`
RejectPromise bool `json:"rejectPromise"`
Warning MeterWarning `json:"warning"`
}
type MeterInfo struct {
PotenciaActual float64 `json:"potenciaActual"`
Totalizador string `json:"totalizador"`
EstadoICP string `json:"estadoICP"`
PotenciaContratada float64 `json:"potenciaContratada"`
Percentage string `json:"percent"`
}
type MeterWarning struct {
Message string `json:"message"`
}
var actions = map[string]Action{
"getLoginInfo": Action{
ID: 215,
Descriptor: "WP_Monitor_CTRL/ACTION$getLoginInfo",
CallingDescriptor: "WP_Monitor",
Params: map[string]string{"serviceNumber": "S011"},
},
"getCups": Action{
ID: 270,
Descriptor: "WP_ContadorICP_F2_CTRL/ACTION$getCUPSReconectarICP",
CallingDescriptor: "WP_Reconnect_ICP",
Params: map[string]string{"visSelected": ""},
},
"getMeter": Action{
ID: 294,
Descriptor: "WP_ContadorICP_F2_CTRL/ACTION$consultarContador",
CallingDescriptor: "WP_Reconnect_Detail",
Params: map[string]string{"cupsId": ""},
},
}
type Client struct {
username string
password string
collector *colly.Collector
ctx *Context
accountID string
Debug bool
}
func (c *Client) MeterInfo(cupsID string) (*MeterInfo, error) {
var met MeterActionResponse
getMeter := actions["getMeter"]
getMeter.Params["cupsId"] = cupsID
err := c.sendAction(getMeter, "WP_ContadorICP_F2_CTRL.consultarContador", &met)
if err != nil {
return nil, err
}
rv := met.Actions[0].MeterReturnValue
if rv.HasWarning {
return nil, errors.New(rv.Warning.Message)
}
return &met.Actions[0].MeterReturnValue.Data, nil
}
func NewClient(username, password string) *Client {
c := colly.NewCollector()
c.SetRequestTimeout(90 * time.Second)
c.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
return &Client{username: username, password: password, collector: c}
}
func (cl *Client) Login() error {
c := cl.collector
c.SetStorage(&storage.InMemoryStorage{})
cl.ctx = &Context{}
c.OnHTML("script", func(e *colly.HTMLElement) {
path := e.Attr("src")
if regexp.MustCompile(`resources.js`).MatchString(path) {
j := strings.Split(path, "/")[5]
decoded, err := url.QueryUnescape(j)
if err != nil {
panic(err)
}
//var data map[string]string
err = json.Unmarshal([]byte(decoded), cl.ctx)
if err != nil {
panic(err)
}
}
})
c.Visit("https://zonaprivada.edistribucion.com/areaprivada/s/login?ec=302&startURL=%2Fareaprivada%2Fs%2F")
params := map[string]string{
"username": cl.username,
"password": cl.password,
"startUrl": "/areaprivada/s/",
}
action := Action{
ID: 91,
Descriptor: "LightningLoginFormController/ACTION$login",
CallingDescriptor: "WP_LoginForm",
Params: params,
}
msg := Message{[]Action{action}}
//d := Data{Message: msg, Context: ctx, Token: "undefined", PageURI: "/areaprivada/s/login/?language=es&startURL=%2Fareaprivada%2Fs%2F&ec=302"}
msgm, err := json.Marshal(msg)
if err != nil {
panic(err)
}
ctxm, err := json.Marshal(cl.ctx)
if err != nil {
panic(err)
}
d := map[string]string{
"message": string(msgm),
"aura.context": string(ctxm),
"aura.pageURI": "/areaprivada/s/login/?language=es&startURL=%2Fareaprivada%2Fs%2F&ec=302",
"aura.token": "undefined",
}
var response LoginResponse
c.OnResponse(func(r *colly.Response) {
json.Unmarshal(r.Body, &response)
})
err = c.Post("https://zonaprivada.edistribucion.com/areaprivada/s/sfsites/aura?other.LightningLoginForm.login=1", d)
if err != nil {
return err
}
if len(response.Events) == 0 {
return errors.New("invalid login response")
}
c = c.Clone()
c.OnResponse(func(r *colly.Response) {
// TODO: add debugging
//fmt.Println(string(r.Body))
})
// Follow the redirect after login
err = c.Visit(response.Events[0].Attributes.Values.Url)
if err != nil {
return err
}
c = c.Clone()
c.OnResponse(func(r *colly.Response) {
p := regexpGroups(`(?P<auraConfig>var auraConfig = )(?P<json>.*?);`, string(r.Body))
json.Unmarshal([]byte(p["json"]), &auraConfig)
})
// Landing page
err = c.Visit("https://zonaprivada.edistribucion.com/areaprivada/s/")
if err != nil {
return err
}
// Get login info
var ar GetLoginActionResponse
err = cl.sendAction(actions["getLoginInfo"], "WP_Monitor_CTRL.getLoginInfo", &ar)
if err != nil {
return err
}
cl.accountID = ar.Actions[0].ReturnValue.Visibility.ID
return nil
}
func (c *Client) ListCups() ([]Cups, error) {
var gc CupsActionResponse
getCups := actions["getCups"]
getCups.Params["visSelected"] = c.accountID
err := c.sendAction(getCups, "WP_ContadorICP_F2_CTRL.getCUPSReconectarICP", &gc)
if err != nil {
return nil, err
}
return gc.Actions[0].CupsReturnValue.Data.Cups, nil
}
func (client *Client) sendAction(action Action, command string, actionResponse interface{}) error {
ctx := client.ctx
c := client.collector.Clone()
msg := Message{[]Action{action}}
msgm, err := json.Marshal(msg)
if err != nil {
return err
}
ctxm, err := json.Marshal(ctx)
if err != nil {
return err
}
d := map[string]string{
"message": string(msgm),
"aura.context": string(ctxm),
"aura.pageURI": "/areaprivada/s/wp-online-access",
"aura.token": auraConfig.Token,
}
c.OnRequest(func(r *colly.Request) {
r.Headers.Set("Accept", "application/json")
})
c.OnResponse(func(r *colly.Response) {
if strings.HasPrefix(r.Headers.Get("Content-Type"), "application/json") {
if client.Debug {
fmt.Println(string(r.Body))
}
json.Unmarshal(r.Body, actionResponse)
}
})
err = c.Post(dashboardURL+command, d)
if err != nil {
return err
}
return nil
}
func regexpGroups(regEx, url string) (paramsMap map[string]string) {
var compRegEx = regexp.MustCompile(regEx)
match := compRegEx.FindStringSubmatch(url)
paramsMap = make(map[string]string)
for i, name := range compRegEx.SubexpNames() {
if i > 0 && i <= len(match) {
paramsMap[name] = match[i]
}
}
return paramsMap
}