-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.go
130 lines (109 loc) · 3.38 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
package main
import (
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
)
var (
protocol string
host string
cookieCoreMail string
cookieCoreMailSid string
)
type Response struct {
Code string `json:"code"`
Total int `json:"total"`
Items []Item `json:"var"`
}
type Item struct {
Name string `json:"true_name"`
Email string `json:"email"`
}
func init() {
flag.StringVar(&protocol, "protocol", "http", "Coremail login url protocol")
flag.StringVar(&host, "host", "mail.dlpu.edu.cn", "Coremail host")
flag.StringVar(&cookieCoreMail, "coremail_cookie", "", "Coremail value in Request Cookie")
flag.StringVar(&cookieCoreMailSid, "coremail_sid", "", "Coremail.sid value in Request Cookie")
}
func getAllGroup() [][]string {
url := buildURL("oab:getDirectories")
postData := fmt.Sprintf(`{dn: "a", attrIds: ["email"]}`)
resp := getResp(url, fmt.Sprintf("Coremail=%s", cookieCoreMail), postData)
//使用正则匹配出所有的组群
reg := regexp.MustCompile(`(?s)id":"(.{32})".*?"name":"(.*?)"`)
result1 := reg.FindAllStringSubmatch(resp, -1)
// fmt.Println("==================")
// fmt.Println(result1)
// fmt.Println("==================")
// for i := 0; i < len(result1); i++ {
// fmt.Println(result1[i][1], result1[i][2])
// }
// os.Exit(0)
return result1
}
func main() {
flag.Parse()
// 先获取组群
allGroupID := getAllGroup()
f, err := os.Create(host + ".email_list.csv")
if err != nil {
panic(err)
}
defer f.Close()
f.WriteString("\xEF\xBB\xBF")
w := csv.NewWriter(f)
w.Write([]string{"name", "email"})
// 根据组群ID获取组群下的所有通讯录
for i := 0; i < len(allGroupID); i++ {
url := buildURL("oab:listEx")
// a表示all,"/"后面就是组群ID
postData := fmt.Sprintf(`{"dn":"a/%s","returnAttrs":["true_name","email"],"start":%d,"limit":%d,"defaultReturnMeetingRoom":false}`, allGroupID[i][1], 0, 1)
fmt.Println("======", postData)
respCount := getResp(url, fmt.Sprintf("Coremail=%s", cookieCoreMail), postData)
var responseCount Response
json.Unmarshal([]byte(respCount), &responseCount)
for j := 0; j < responseCount.Total; j += 100 {
postData := fmt.Sprintf(`{"dn":"a/%s","returnAttrs":["true_name","email"],"start":%d,"limit":%d,"defaultReturnMeetingRoom":false}`, allGroupID[i][1], j, 100)
respList := getResp(url, fmt.Sprintf("Coremail=%s", cookieCoreMail), postData)
var responseList Response
json.Unmarshal([]byte(respList), &responseList)
for _, item := range responseList.Items {
// 联系人前面添加组群名,针对同名的人更便于区分
w.Write([]string{allGroupID[i][2] + "-" + item.Name, item.Email})
}
}
}
w.Flush()
}
func buildURL(fc string) string {
return protocol + "://" + host + "/coremail/s/json?func=" + fc + "&sid=" + cookieCoreMailSid
}
func getResp(url string, cookie, postData string) string {
client := &http.Client{}
fmt.Printf("[%s]\n", url)
req, err := http.NewRequest("POST", url, strings.NewReader(postData))
if err != nil {
fmt.Println(err)
return ""
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Cookie", cookie)
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return ""
}
defer resp.Body.Close()
fmt.Printf("[%s]\n", string(body))
return string(body)
}