-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
231 lines (209 loc) · 6.79 KB
/
main.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
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/rekognition"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
"github.com/liuminhaw/sportcenter-booking/registry"
"github.com/liuminhaw/sportcenter-booking/secrets"
)
// create chrome instance in windowed mode, run, and close after 5 seconds
func main() {
headless := flag.Bool("headless", true, "Set chrome browser headless mode")
awsProfile := flag.String("profile", "default",
"Set aws credential to use in session, no credential will be set if using default value")
awsRegion := flag.String("region", "ap-northeast-1", "Set aws region to use in session")
flag.Parse()
var sess *session.Session
var err error
if *awsProfile == "default" {
log.Println("Default aws profile")
sess, err = session.NewSession()
} else {
log.Printf("Profile %s\n", *awsProfile)
sess, err = session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String(*awsRegion)},
Profile: *awsProfile,
})
}
if err != nil {
panic(err)
}
// Fetch secret
var keys secrets.Secret
encKey, err := secrets.GetSecret(sess, os.Getenv("_ENC_KEY"))
if err != nil {
log.Fatalf("error fetching encryption key: %v\n", err.Error())
}
if err := json.Unmarshal([]byte(encKey), &keys); err != nil {
log.Fatalf("error when unmarhsal from secret manager: %v\n", err.Error())
}
cookieObjs, err := getS3Objects(sess, os.Getenv("_S3_BUCKET"), "cookies/")
if err != nil {
panic(err)
}
registryObjs, err := getS3Objects(sess, os.Getenv("_S3_BUCKET"), "registry/")
if err != nil {
panic(err)
}
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag("headless", *headless),
)
var cookies = map[string]bool{}
for _, obj := range cookieObjs.Contents {
fmt.Printf("cookie key: %s\n", filepath.Base(*obj.Key))
cookies[filepath.Base(*obj.Key)] = true
}
log.Printf("Initial cookies: %+v", cookies)
var reserveInfo registry.Reservation
for _, obj := range registryObjs.Contents {
s3Registry := registry.Registry{
Bucket: os.Getenv("_S3_BUCKET"),
Dirname: filepath.Dir(*obj.Key),
Filename: filepath.Base(*obj.Key),
}
if err := s3Registry.FetchRegistryFile(sess, keys.S3Enc); err != nil {
log.Fatalf("fetch registry file content error: %v\n", err.Error())
}
if err := json.Unmarshal(s3Registry.Content, &reserveInfo); err != nil {
log.Fatalf("error when unmarhsal from registry: %v\n", err.Error())
}
fmt.Printf("Reserve information: %+v\n", reserveInfo)
if cookies[reserveInfo.Username] {
log.Printf("Cookie for %s exist", reserveInfo.Username)
} else {
genCookie(opts, sess, reserveInfo)
cookies[reserveInfo.Username] = true
}
}
time.Sleep(time.Second * 3)
// fmt.Printf("Title: %s\n", title)
}
// getS3Objects returns a slice of *s3.ListObjectsV2Output from s3://bucket/prefix/,
// and only returns objects without prefix/
func getS3Objects(sess *session.Session, bucket string, prefix string) (*s3.ListObjectsV2Output, error) {
svc := s3.New(sess)
objects, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(bucket),
Prefix: aws.String(prefix),
StartAfter: aws.String(prefix),
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case s3.ErrCodeNoSuchBucket:
fmt.Println(s3.ErrCodeNoSuchBucket, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return nil, err
}
return objects, nil
}
func genCookie(opts []func(*chromedp.ExecAllocator), sess *session.Session, reserveInfo registry.Reservation) {
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()
chromedp.ListenTarget(ctx, func(ev interface{}) {
if _, ok := ev.(*page.EventJavascriptDialogOpening); ok {
go func() {
if err := chromedp.Run(ctx,
page.HandleJavaScriptDialog(true),
); err != nil {
log.Fatal(err)
}
}()
}
})
var imgBuf []byte
if err := chromedp.Run(ctx,
chromedp.Navigate(os.Getenv("_DAAN_LOGIN")),
chromedp.WaitVisible("#ContentPlaceHolder1_loginid", chromedp.ByID),
chromedp.WaitVisible("#loginpw", chromedp.ByID),
chromedp.WaitVisible("#ContentPlaceHolder1_Captcha_text", chromedp.ByID),
chromedp.WaitVisible("#ContentPlaceHolder1_CaptchaImage", chromedp.ByID),
chromedp.WaitVisible("#login_but", chromedp.ByID),
chromedp.SendKeys("#ContentPlaceHolder1_loginid", reserveInfo.Username, chromedp.ByID),
chromedp.SendKeys("#loginpw", reserveInfo.Password, chromedp.ByID),
chromedp.Screenshot("#ContentPlaceHolder1_CaptchaImage", &imgBuf, chromedp.NodeVisible),
); err != nil {
log.Fatal(err)
}
reader := bytes.NewReader(imgBuf)
uploader := s3manager.NewUploader(sess)
uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(os.Getenv("_S3_BUCKET")),
Key: aws.String(fmt.Sprintf("captcha/%s.png", reserveInfo.Username)),
Body: reader,
})
rekognitionSvc := rekognition.New(sess)
resp, err := rekognitionSvc.DetectText(&rekognition.DetectTextInput{
Image: &rekognition.Image{
S3Object: &rekognition.S3Object{
Bucket: aws.String(os.Getenv("_S3_BUCKET")),
Name: aws.String(fmt.Sprintf("captcha/%s.png", reserveInfo.Username)),
},
},
})
var captchText string
if err == nil {
for _, info := range resp.TextDetections {
if *info.Type == "LINE" {
fmt.Printf("Confidence: %f\n", *info.Confidence)
fmt.Printf("Text: %s\n", *info.DetectedText)
captchText = *info.DetectedText
break
}
}
}
if err := chromedp.Run(ctx,
chromedp.SendKeys("ContentPlaceHolder1_Captcha_text", captchText, chromedp.ByID),
chromedp.Click("#login_but", chromedp.NodeVisible),
chromedp.ActionFunc(func(ctx context.Context) error {
cookies, err := network.GetAllCookies().Do(ctx)
if err != nil {
return err
}
for i, c := range cookies {
log.Printf("chrome cookie %d: %+v - %+v", i, c.Name, c.Value)
if c.Name == "ASP.NET_SessionId" {
cookie, err := c.MarshalJSON()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Marshal cookie: %s\n", cookie)
reader := bytes.NewReader(cookie)
uploader := s3manager.NewUploader(sess)
uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(os.Getenv("_S3_BUCKET")),
Key: aws.String(fmt.Sprintf("cookies/%s", reserveInfo.Username)),
Body: reader,
})
}
}
return nil
}),
); err != nil {
log.Fatal(err)
}
}