-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (100 loc) · 2.01 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
package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
)
const ENDPOINT_URL = "https://login.microsoftonline.com/common/GetCredentialType"
const CONTENT_TYPE = "application/json"
type Request struct {
Username string
}
type Result struct {
Username string
Display string
IfExistsResult int
}
func valid_users(usernames_conn chan string, valid_user_conn chan string) {
for username := range usernames_conn {
if valid_user(username) != "" {
valid_user_conn <- username
} else {
valid_user_conn <- ""
}
}
}
func valid_user(username string) string {
request := Request{Username: username}
jm, err := json.Marshal(request)
if err != nil {
panic(err)
}
response, err := http.Post(
ENDPOINT_URL,
CONTENT_TYPE,
bytes.NewBuffer(jm),
)
defer response.Body.Close()
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
var r Result
if json.Unmarshal(body, &r); err != nil {
panic(err)
}
if r.IfExistsResult == 0 {
return username
} else {
return ""
}
}
func main() {
file := flag.String("f", "", "Specify a file")
username := flag.String("u", "", "Specify one username")
flag.Parse()
if *file != "" {
f, err := os.Open(*file)
if err != nil {
panic(err)
}
defer f.Close()
var line_count int
var usernames []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
usernames = append(usernames, scanner.Text())
line_count += 1
}
usernames_conn := make(chan string, line_count)
valid_user := make(chan string)
go func() {
for _, username := range usernames {
usernames_conn <- username
}
}()
for i := 0; i < line_count; i++ {
go valid_users(usernames_conn, valid_user)
}
for i := 0; i < line_count; i++ {
username := <-valid_user
if username != "" {
fmt.Println(username)
}
}
close(usernames_conn)
close(valid_user)
} else if *username != "" {
if valid_user(*username) != "" {
fmt.Println(*username)
}
}
}