-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtangalanga.go
129 lines (99 loc) · 2.3 KB
/
tangalanga.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
pb "github.com/elcuervo/tangalanga/proto"
"github.com/golang/protobuf/proto"
)
type Option func(*Tangalanga)
func WithTransport(transport *http.Transport) Option {
return func(t *Tangalanga) {
t.client = &http.Client{
Transport: transport,
Timeout: 5 * time.Second,
}
}
}
type Tangalanga struct {
Found int
Missing int
Suspicious int
ExpiredToken bool
client *http.Client
}
func (t *Tangalanga) Close() {
}
func NewTangalanga(opts ...Option) (*Tangalanga, error) {
c := &Tangalanga{
Found: 0,
Missing: 0,
Suspicious: 0,
ExpiredToken: false,
}
for _, opt := range opts {
opt(c)
}
return c, nil
}
func (t *Tangalanga) FindMeeting(id int) (*pb.Meeting, error) {
meetId := strconv.Itoa(id)
p := url.Values{"cv": {"5.0.25694.0524"}, "mn": {meetId}, "uname": {"tangalanga"}}
req, err := http.NewRequest("POST", zoomUrl, strings.NewReader(p.Encode()))
if err != nil {
if *debugFlag {
fmt.Printf("%s\nerror: %s\n", color.Red("bad request"), err.Error())
}
return nil, err
}
cookie := fmt.Sprintf("zpk=%s", *token)
req.Header.Add("Cookie", cookie)
req.Header.Add("ZM-CAP", "2535978022733895607,32676")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := t.client.Do(req)
if err != nil {
if *debugFlag {
fmt.Printf("%s\nerror: %s\n", color.Red("can't connect to Zoom!!"), err.Error())
}
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
if *debugFlag {
fmt.Printf("%s\nerror: %s\n", color.Red("bad body"), err.Error())
}
return nil, err
}
m := &pb.Meeting{}
err = proto.Unmarshal(body, m)
if err != nil {
if *debugFlag {
fmt.Printf("%s\nerror: %s\n", color.Red("can't unpack protobuf"), err.Error())
}
return nil, err
}
missing := m.GetError() != 0
if missing {
t.Missing++
info := m.GetInformation()
if m.GetError() == 124 {
t.ExpiredToken = true
}
// suspicious not found when there are too many
if info == "Meeting not existed." {
t.Suspicious++
} else {
t.Suspicious = 0
}
return nil, fmt.Errorf("%s: %s", color.Blue("zoom"), color.Red(info))
} else {
// Reset suspicious counter when found
t.Suspicious = 0
t.Found++
}
return m, nil
}