forked from sbreitf1/gohome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixfetch.go
424 lines (357 loc) · 12.8 KB
/
matrixfetch.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package main
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/sbreitf1/gohome/internal/pkg/stdio"
"github.com/beevik/etree"
)
const (
matrixSessionCookieName = "JSESSIONID"
matrixRendermapTokenCookieName = "oam.Flash.RENDERMAP.TOKEN"
urlMatrixLogin = "/login.jspx"
urlMatrixMainMenu = "/mainMenu.jsf"
matrixDebugPrint = false
matrixOutputFiles = false
)
var (
matrixVersionURL = "/matrix"
)
// FetchMatrixEntries returns today's entries available in "Aktuelle Buchungen" in Matrix and the current flexitime balance.
func FetchMatrixEntries(config MatrixConfig) ([]Entry, time.Duration, error) {
client, err := NewMatrixClient(config)
if err != nil {
return nil, 0, err
}
defer client.Close()
stdio.Debug("get entries")
entries, err := client.GetEntries()
if err != nil {
return nil, 0, fmt.Errorf("failed to retrieve entries: %s", err.Error())
}
stdio.Debug("get flexi time")
flexitime, err := client.GetFlexiTime()
if err != nil {
return nil, 0, fmt.Errorf("could not retrieve flexitime: %s", err.Error())
}
return entries, flexitime, nil
}
// MatrixConfig contains config parameters for Matrix connection and login.
type MatrixConfig struct {
Host string `json:"host"`
User string `json:"user"`
Pass string `json:"pass" jcrypt:"aes"`
}
// DormaClient represents an authorized connection to Matrix.
type MatrixClient struct {
config MatrixConfig
httpClient *http.Client
sessionID string
rendermapToken string
monthDataID string
bookingID string
lastVisitedPage string
nextUniqueToken string
nextViewState string
}
// NewDormaClient returns a logged in DormaClient.
func NewMatrixClient(config MatrixConfig) (*MatrixClient, error) {
client := &MatrixClient{
config: config,
httpClient: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse },
},
}
stdio.Debug("logging in")
if err := client.login(); err != nil {
return nil, fmt.Errorf("login failed: %s", err.Error())
}
stdio.Debug("visit self service page")
if err := client.visitSelfService(); err != nil {
return nil, fmt.Errorf("visit self-service failed: %s", err.Error())
}
return client, nil
}
// Close logs out from Matrix and closes the connection.
func (c *MatrixClient) Close() error {
return c.logout()
}
func (c *MatrixClient) login() error {
encodedUser := url.QueryEscape(c.config.User)
encodedPass := url.QueryEscape(c.config.Pass)
timeZoneName := "Europe/Berlin" //TODO dynamic
timeZoneOffset := "+01:00" //TODO dynamic
encodedTimeZoneName := url.QueryEscape(timeZoneName)
encodedTimeZoneOffset := url.QueryEscape(timeZoneOffset)
requestBody := fmt.Sprintf("userid=%s&password=%s&systemLevel=false&timezonename=%s&timezoneoffset=%s&timezonedst=true&loginButton=Anmeldung", encodedUser, encodedPass, encodedTimeZoneName, encodedTimeZoneOffset)
if err := c.detectRedirectURI(); err != nil {
return err
}
if _, err := c.postRedirect(matrixVersionURL+urlMatrixLogin, requestBody); err != nil {
return err
}
return nil
}
func (c *MatrixClient) detectRedirectURI() error {
resp, err := c.httpClient.Get(c.absoluteURL(matrixVersionURL + urlMatrixLogin))
if err != nil {
return err
}
if resp.StatusCode >= 300 && resp.StatusCode < 400 {
parts := strings.Split(resp.Header.Get("Location"), "/")
if len(parts) != 3 {
return fmt.Errorf("unexpected redirect url for login: %s", resp.Header.Get("Location"))
}
matrixVersionURL = "/" + parts[1]
stdio.Debug("detected matrix url %q", matrixVersionURL)
}
return nil
}
func (c *MatrixClient) visitSelfService() error {
requestBody := "uniqueToken=" + c.nextUniqueToken + "&autoScroll=&agmenuform_SUBMIT=1&javax.faces.ViewState=" + c.nextViewState + "&activateMenuItem=mss_root&menuIndex=4&agmenuform%3AassemblyGroupMenu=agmenuform%3AassemblyGroupMenu&data-matrix-treepath=mss_root&agmenuform%3AassemblyGroupMenu_menuid=_c3d3c76c-a976-4d74-a147-db02d56ddb08|4"
if _, err := c.postRedirect(matrixVersionURL+urlMatrixMainMenu, requestBody); err != nil {
return nil
}
return nil
}
func (c *MatrixClient) logout() error {
return nil
}
// GetEntries returns all entries for the current day.
func (c *MatrixClient) GetEntries() ([]Entry, error) {
requestBody := "uniqueToken=" + c.nextUniqueToken + "&menuform_SUBMIT=1&autoScroll=&javax.faces.ViewState=" + c.nextViewState + "&activateMenuItem=tim_searchWebBookingMss&menuform%3AmainMenu_mss_root_menuid=" + c.bookingID + "&data-matrix-treepath=mss_root.tim_searchWebBookingMss&menuform%3AmainMenu_mss_root=menuform%3AmainMenu_mss_root"
body, err := c.postRedirect(c.lastVisitedPage, requestBody)
if err != nil {
return nil, err
}
if matrixOutputFiles {
if err := os.WriteFile("entries.html", []byte(body), os.ModePerm); err != nil {
return nil, fmt.Errorf("output entries file: %s", err.Error())
}
}
doc := etree.NewDocument()
if err := doc.ReadFromString(body); err != nil {
return nil, fmt.Errorf("read xml string element: %s", err.Error())
}
tableData := doc.FindElement("//*[@id='mainbody:editWebBooking:logTable_data']")
tableRows := tableData.FindElements("//tr[@data-ri]")
today := time.Now()
timeStampRegex := regexp.MustCompile(`\s*(\d+):(\d+)\s*`)
entries := make([]Entry, 0)
for _, row := range tableRows {
timeSpan := row.ChildElements()[0].ChildElements()[0]
if timeSpan == nil {
return nil, fmt.Errorf("could not find time span")
}
timeStamp := timeSpan.Text()
m := timeStampRegex.FindStringSubmatch(timeStamp)
hour, _ := strconv.Atoi(m[1])
minute, _ := strconv.Atoi(m[2])
date := time.Date(today.Year(), today.Month(), today.Day(), hour, minute, 0, 0, time.Local)
if hour == 0 && minute == 0 {
stdio.Debug("ignore booking %q at 00:00", m[4])
continue
}
typeStr := row.ChildElements()[1].ChildElements()[0].Text()
typeStr = strings.ToLower(typeStr)
var entryType EntryType
if strings.Contains(typeStr, "kommen") ||
strings.Contains(typeStr, "arrive") ||
strings.Contains(typeStr, "business authorisation") {
entryType = EntryTypeCome
} else if strings.Contains(typeStr, "gehen") ||
strings.Contains(typeStr, "leave") ||
strings.Contains(typeStr, "hourly absence - end") ||
strings.Contains(typeStr, "hourly absence end") ||
strings.Contains(typeStr, "system - baend") {
if strings.Contains(typeStr, "sequence error") {
continue
}
entryType = EntryTypeLeave
} else if strings.Contains(typeStr, "???bookingtype.1034.name???") {
// "???BookingType.1034.name???" wird geschrieben, wenn man am Terminal den Kontostand abfragt
stdio.Debug("found strange booking type: %q", typeStr)
continue
} else if strings.Contains(typeStr, "valid until") {
stdio.Debug("found strange booking type: %q", typeStr)
continue
} else {
return nil, fmt.Errorf("cannot parse entry type from %q", typeStr)
}
entries = append(entries, Entry{Time: date, Type: entryType})
}
return entries, nil
}
// GetFlexiTime returns the current flexi time balance.
func (c *MatrixClient) GetFlexiTime() (time.Duration, error) {
requestBody := "uniqueToken=" + c.nextUniqueToken + "&menuform_SUBMIT=1&autoScroll=&javax.faces.ViewState=" + c.nextViewState + "&activateMenuItem=tim_persMonthlyReconciliation&menuform%3AmainMenu_mss_root_menuid=" + c.monthDataID + "&data-matrix-treepath=mss_root.tim_persMonthlyReconciliation&menuform%3AmainMenu_mss_root=menuform%3AmainMenu_mss_root"
body, err := c.postRedirect(c.lastVisitedPage, requestBody)
if err != nil {
return 0, err
}
if matrixOutputFiles {
if err := os.WriteFile("flexitime.html", []byte(body), os.ModePerm); err != nil {
return 0, fmt.Errorf("output flexitime file: %s", err.Error())
}
}
return c.parseFlexiTime(body)
}
func (c *MatrixClient) parseFlexiTime(body string) (time.Duration, error) {
doc := etree.NewDocument()
err := doc.ReadFromString(body)
if err != nil {
return 0, fmt.Errorf("unable to flexi-time balance html")
}
tableData := doc.FindElements("//*[@title='Balance previous day']")
if len(tableData) == 0 {
return 0, fmt.Errorf("unable to parse current flexi-time balance")
}
lastElement := tableData[len(tableData)-1]
if len(lastElement.ChildElements()) == 0 {
return 0, fmt.Errorf("unable to find flexi-time balance in element childs")
}
child := lastElement.ChildElements()[0]
text := child.Text()
isNegative := false
if strings.Contains(text, "-") {
isNegative = true
text = strings.ReplaceAll(text, "-", "")
}
splitString := strings.Split(text, ":")
if len(splitString) != 2 {
return 0, fmt.Errorf("unexpected time format found in flexi-time balance")
}
hours, _ := strconv.Atoi(splitString[0])
minutes, _ := strconv.Atoi(splitString[1])
if isNegative {
return -1 * (time.Duration(hours)*time.Hour + time.Duration(minutes)*time.Minute), nil
}
return (time.Duration(hours)*time.Hour + time.Duration(minutes)*time.Minute), nil
}
func (c *MatrixClient) absoluteURL(url string) string {
return strings.TrimRight(c.config.Host, "/") + "/" + strings.TrimLeft(url, "/")
}
func (c *MatrixClient) postRedirect(url, body string) (string, error) {
firstURL := c.absoluteURL(url)
request, err := http.NewRequest(http.MethodPost, firstURL, strings.NewReader(body))
if err != nil {
return "", err
}
c.setCookies(request)
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
response, err := c.httpClient.Do(request)
if err != nil {
return "", err
}
if (response.StatusCode < 300) || (399 < response.StatusCode) {
return "", fmt.Errorf("server returned code %d when 3xx was expected", response.StatusCode)
}
c.evalCookies(response)
if len(c.sessionID) == 0 {
return "", fmt.Errorf("missing Cookie " + matrixSessionCookieName)
}
if response.Header.Get("Location") == "favoritePage.jsf" {
// this happens when a custom start page is selected. force redirect to main menu instead
response.Header.Set("Location", matrixVersionURL+urlMatrixMainMenu)
}
if response.Header.Get("Location") == "afterLogin.jsf" {
// changed redirect in v4.4.2
response.Header.Set("Location", matrixVersionURL+urlMatrixMainMenu)
}
request, err = http.NewRequest(http.MethodGet, c.absoluteURL(response.Header.Get("Location")), nil)
if err != nil {
return "", err
}
c.setCookies(request)
c.lastVisitedPage = response.Header.Get("Location")
if matrixDebugPrint {
fmt.Println("lastVisitedPage:", c.lastVisitedPage)
}
response, err = c.httpClient.Do(request)
if err != nil {
return "", err
}
if response.StatusCode != 200 {
return "", fmt.Errorf("server returned code %d when 200 was expected", response.StatusCode)
}
buffer, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
body = string(buffer)
c.evalCookies(response)
pattern := regexp.MustCompile(`<input type="hidden" name="uniqueToken" value="([^"]*)" />`)
m := pattern.FindStringSubmatch(body)
if len(m) != 2 {
return "", fmt.Errorf("unable to parse unique token")
}
c.nextUniqueToken = m[1]
if matrixDebugPrint {
fmt.Println("UniqueToken:", c.nextUniqueToken)
}
pattern = regexp.MustCompile(`javax.faces.ViewState:\d+" value="([^"]*)"`)
m = pattern.FindStringSubmatch(body)
if len(m) != 2 {
return "", fmt.Errorf("unable to parse view state")
}
c.nextViewState = m[1]
if matrixDebugPrint {
fmt.Println("ViewState:", c.nextViewState)
}
pattern = regexp.MustCompile(`'tim_searchWebBookingMss','menuform:mainMenu_mss_root_menuid':'(\d+)'`)
m = pattern.FindStringSubmatch(body)
if len(m) == 2 {
c.bookingID = m[1]
if matrixDebugPrint {
fmt.Println("BookingID:", c.bookingID)
}
}
pattern = regexp.MustCompile(`'tim_persMonthlyReconciliation','menuform:mainMenu_mss_root_menuid':'(\d+)'`)
m = pattern.FindStringSubmatch(body)
if len(m) == 2 {
c.monthDataID = m[1]
if matrixDebugPrint {
fmt.Println("MonthDataID:", c.monthDataID)
}
}
return body, nil
}
func (c *MatrixClient) setCookies(request *http.Request) {
if len(c.sessionID) > 0 {
request.AddCookie(&http.Cookie{Name: matrixSessionCookieName, Value: c.sessionID})
}
if len(c.rendermapToken) > 0 {
request.AddCookie(&http.Cookie{Name: matrixRendermapTokenCookieName, Value: c.rendermapToken})
}
request.AddCookie(&http.Cookie{Name: "timezonedst", Value: "true"})
request.AddCookie(&http.Cookie{Name: "timezonename", Value: "Europe/Berlin"})
request.AddCookie(&http.Cookie{Name: "timezoneoffset", Value: "+01:00"})
}
func (c *MatrixClient) evalCookies(response *http.Response) {
for _, cookie := range response.Cookies() {
if cookie.Name == matrixSessionCookieName {
if matrixDebugPrint {
fmt.Println("SessionID:", cookie.Value)
}
c.sessionID = cookie.Value
}
if cookie.Name == matrixRendermapTokenCookieName {
if matrixDebugPrint {
fmt.Println("RendermapToken:", cookie.Value)
}
c.rendermapToken = cookie.Value
}
}
}