Skip to content

Commit

Permalink
Merge pull request #13 from piaolin/dev
Browse files Browse the repository at this point in the history
feat: add output file
  • Loading branch information
piaolin authored May 11, 2023
2 parents 2688491 + 8e5ca7f commit 2a4a307
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
45 changes: 33 additions & 12 deletions cmd/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ type detectArgsType struct {
google bool
email []string
phone []string
output string
//unique bool
}

var (
detectArgs detectArgsType
wg sync.WaitGroup
nonSiteData = "[-] There is no site data of %s\n"
existInfo = "[+] %-15s %-15s: %s\n"
nonExistInfo = "[-] %-15s %-15s: non exists\n"
reqErrorInfo = "[!] %-15s %-15s: %s requests error, retry %d/%d\n"
sleepMap = make(map[string]int64)
sleepChannel = make(map[string]chan bool)
searchInfo = "[+] %-15s %-15s: Please check in %s\n"
disableSiteInfo = "[!] %-15s %-15s: data of %s is temporarily unavailable\n"
nsfwInfo = "[!] %-15s %-15s: %s is nsfw\n"
detectArgs detectArgsType
wg sync.WaitGroup
nonSiteData = "[-] There is no site data of %s\n"
existInfo = "[+] %-15s %-15s: %s\n"
nonExistInfo = "[-] %-15s %-15s: non exists\n"
reqErrorInfo = "[!] %-15s %-15s: %s requests error, retry %d/%d\n"
sleepMap = make(map[string]int64)
sleepChannel = make(map[string]chan bool)
searchInfo = "[+] %-15s %-15s: Please check in %s\n"
disableSiteInfo = "[!] %-15s %-15s: data of %s is temporarily unavailable\n"
detectCompletedInfo = "[+] Detect completed, save to %s\n"
nsfwInfo = "[!] %-15s %-15s: %s is nsfw\n"
writeContent = make(chan string)
writeDone = make(chan bool)
)

func init() {
Expand All @@ -58,6 +62,7 @@ func init() {
detectCmd.Flags().BoolVar(&detectArgs.precisely, "precisely", false, "Check precisely")
detectCmd.Flags().StringVarP(&detectArgs.file, "file", "f", "data.json", "Site data file")
detectCmd.Flags().BoolVarP(&detectArgs.google, "google", "g", false, "Show google search result")
detectCmd.Flags().StringVarP(&detectArgs.output, "output", "o", "result.txt", "Result file")

//detectCmd.Flags().BoolVar(&detectArgs.unique, "unique", false, "Make new requests client for each site")
rootCmd.AddCommand(detectCmd)
Expand All @@ -71,12 +76,23 @@ var detectCmd = &cobra.Command{
}

func detect(_ *cobra.Command, _ []string) {
log.Infoln("Detect for", detectArgs.name)
if len(detectArgs.name) != 0 {
log.Infoln("Detect for", detectArgs.name)
}
if len(detectArgs.email) != 0 {
log.Infoln("Detect for", detectArgs.email)
}
if len(detectArgs.phone) != 0 {
log.Infoln("Detect for", detectArgs.phone)
}

if Verbose {
log.Infoln("Debug Mode")
log.SetLevel(log.DebugLevel)
}

go utils.WriteToFile(detectArgs.output, writeContent, writeDone)

log.Debugln(detectArgs)
siteData, err := ioutil.ReadFile(detectArgs.file)
if err != nil {
Expand Down Expand Up @@ -134,7 +150,10 @@ func detect(_ *cobra.Command, _ []string) {
}
}
}

wg.Wait()
writeDone <- true
log.Infof(detectCompletedInfo, detectArgs.output)
}

func detectSite(name, site, nameType string, siteBody gjson.Result) {
Expand Down Expand Up @@ -302,10 +321,12 @@ func detectUser(name, site string, requestTimes, retryTimes, detectCount int, fl
} else if !detectArgs.precisely {
// flag=true && precisely=false
log.Infof(existInfo, name, site, userPage)
writeContent <- fmt.Sprintf(existInfo, name, site, userPage)
return false
} else if requestTimes == detectCount {
// flag=true && precisely=true && last request
log.Infof(existInfo, name, site, userPage)
writeContent <- fmt.Sprintf(existInfo, name, site, userPage)
return true
} else {
return true
Expand Down
3 changes: 2 additions & 1 deletion data.json
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,8 @@
"header": {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"
},
"userPage": "http://www.xsssql.com/article/author/%s"
"userPage": "http://www.xsssql.com/article/author/%s",
"status": false
}
],
"login": {
Expand Down
20 changes: 20 additions & 0 deletions utils/writeToFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package utils

import (
"fmt"
"os"
)

func WriteToFile(filename string, content chan string, done chan bool) {
file, _ := os.Create(filename)
defer file.Close()

for {
select {
case line := <-content:
_, _ = fmt.Fprint(file, line)
case <-done:
return
}
}
}

0 comments on commit 2a4a307

Please sign in to comment.