-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
227 lines (212 loc) · 5.77 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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/c-bata/go-prompt"
"github.com/fatih/color"
"github.com/pkg/term/termios"
"github.com/smallkirby/aip/cmd"
"github.com/smallkirby/aip/pkg/conf"
"golang.org/x/sys/unix"
)
type FlagContext struct {
help *bool
quiet *bool
noninteractive *bool
version *bool
mail *bool
conffile *string
}
const VERSION = "0.1.0"
var original_tty_attr *unix.Termios
var target_urls []string
var context FlagContext
func main() {
parseArgs()
if *context.help {
usage()
os.Exit(0)
}
if *context.version {
fmt.Println(VERSION)
os.Exit(0)
}
if *context.noninteractive {
doNonInteractive()
} else {
if *context.quiet {
fmt.Fprintln(os.Stderr, "--quiet options is available only with -n option.")
os.Exit(1)
} else {
// go-prompt changes current tty attributes, and doesn't restore it...
if tty_attr, err := termios.Tcgetattr(0); err != nil {
log.Fatal(err)
} else {
original_tty_attr = tty_attr
}
defer restoreTty()
// run it
p := prompt.New(
executer,
completer,
prompt.OptionPrefix("(command)> "),
)
p.Run()
}
}
}
func doNonInteractive() {
safe_num := 0
danger_num := 0
error_num := 0
danger_urls := []string{}
quiet := *context.quiet
target_urls, err := cmd.ReadConf()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
}
ch := make(chan cmd.BoolResult, len(target_urls))
go cmd.CheckAll(target_urls, ch)
for result := range ch {
if result.Error != nil {
fmt.Fprintln(os.Stderr, result.Error.Error()) // not return
error_num += 1
} else {
if result.Result {
danger := color.New(color.FgRed, color.Bold).SprintFunc()
if !quiet {
fmt.Printf("%v: %v\n", danger("PUBLIC "), result.URL)
}
danger_urls = append(danger_urls, result.URL)
danger_num += 1
} else {
if !quiet {
fmt.Printf("%v: %v\n", color.GreenString("private"), result.URL)
}
safe_num += 1
}
}
}
if quiet {
for _, url := range danger_urls {
fmt.Println(url)
}
} else {
fmt.Printf("Result: %v public, %v private, %v errors\n", color.RedString(fmt.Sprintf("%v", danger_num)), color.GreenString(fmt.Sprintf("%v", safe_num)), color.BlueString(fmt.Sprintf("%v", error_num)))
}
if *context.mail {
if err := cmd.Mail(target_urls, danger_urls); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
func restoreTty() {
termios.Tcsetattr(0, termios.TCSANOW, original_tty_attr)
}
func completer(d prompt.Document) []prompt.Suggest {
s := []prompt.Suggest{
{Text: "check", Description: "Check all targets in config file."},
{Text: "check <URL>", Description: "Check if the page is public"},
{Text: "conf read", Description: "Read configuration file."},
{Text: "conf add <target>", Description: "Add target URL in configuration."},
{Text: "exit", Description: "I miss you..."},
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
func executer(com string) {
if strings.HasPrefix(com, "check") {
s := strings.Split(com, " ")
if len(s) == 1 {
if len(target_urls) >= 1 {
safe_num := 0
danger_num := 0
error_num := 0
ch := make(chan cmd.BoolResult, len(target_urls))
go cmd.CheckAll(target_urls, ch)
for result := range ch {
if result.Error != nil {
fmt.Fprintln(os.Stderr, result.Error.Error()) // not return
error_num += 1
} else {
if result.Result {
danger := color.New(color.FgRed, color.Bold).SprintFunc()
fmt.Printf("%v: %v\n", danger("PUBLIC "), result.URL)
danger_num += 1
} else {
fmt.Printf("%v: %v\n", color.GreenString("private"), result.URL)
safe_num += 1
}
}
}
fmt.Printf("Result: %v public, %v private, %v errors\n", color.RedString(fmt.Sprintf("%v", danger_num)), color.GreenString(fmt.Sprintf("%v", safe_num)), color.BlueString(fmt.Sprintf("%v", error_num)))
} else {
fmt.Fprintln(os.Stderr, "Read config before check.")
return
}
return
} else if len(s) != 2 {
fmt.Fprintln(os.Stderr, "should specify one URL")
return
}
target := s[1]
if result, err := cmd.Check(target); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
restoreTty()
os.Exit(0)
} else {
if result {
danger := color.New(color.FgGreen, color.Bold).SprintFunc()
fmt.Printf("%v: %v\n", danger("PUBLIC "), target)
} else {
fmt.Printf("%v: %v\n", color.GreenString("private"), target)
}
}
} else if strings.HasPrefix(com, "conf") {
s := strings.Split(com, " ")
if len(s) == 1 {
fmt.Printf("Invalid command: %v\n", com)
return
}
if s[1] == "read" {
if targets, err := conf.ReadConf(); err != nil {
log.Fatalln(err.Error())
} else {
target_urls = targets
fmt.Printf("Set %v URL as targets.\n", len(target_urls))
}
} else if s[1] == "add" {
if len(s) != 3 {
fmt.Printf("Invalid command: %v\n", com)
return
}
if err := cmd.AddConf(s[2]); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return
}
}
} else if com == "exit" {
fmt.Fprintln(os.Stderr, "I miss you...")
restoreTty()
os.Exit(0)
} else {
fmt.Fprintln(os.Stderr, "not imp")
}
}
func parseArgs() {
context.help = flag.Bool("help", false, "show help.")
context.quiet = flag.Bool("quiet", false, "silent mode. available only in non-interactive mode.")
context.version = flag.Bool("version", false, "show version information.")
context.mail = flag.Bool("mail", false, "send the result to specified e-mail.")
context.noninteractive = flag.Bool("n", false, "non-interactive mode.")
context.conffile = flag.String("f", "", "specify config file.")
flag.Parse()
}
func usage() {
title := color.New(color.FgBlue, color.Bold).SprintFunc()
fmt.Println(title("AIP: Am I Public..."))
fmt.Printf("version: %v\tnirugiri. 2021.\n", VERSION)
flag.Usage()
}