fscan/common/log.go

101 lines
1.9 KiB
Go
Raw Normal View History

2020-12-29 17:17:10 +08:00
package common
import (
"fmt"
"os"
2021-03-30 18:12:54 +08:00
"strings"
2021-04-22 12:06:03 +08:00
"sync"
"time"
"github.com/fatih/color"
2020-12-29 17:17:10 +08:00
)
2021-03-30 18:12:54 +08:00
var Num int64
var End int64
var Results = make(chan *string)
2020-12-29 17:17:10 +08:00
var Start = true
var LogSucTime int64
var LogErrTime int64
2021-03-30 22:30:16 +08:00
var WaitTime int64
2021-05-14 10:43:26 +08:00
var Silent bool
2021-05-06 11:37:29 +08:00
var LogWG sync.WaitGroup
2023-06-09 08:13:56 -04:00
var Outputfile string
2020-12-29 17:17:10 +08:00
func init() {
LogSucTime = time.Now().Unix()
go SaveLog()
}
2020-12-29 17:17:10 +08:00
func LogSuccess(result string) {
2021-05-06 11:37:29 +08:00
LogWG.Add(1)
LogSucTime = time.Now().Unix()
Results <- &result
2020-12-29 17:17:10 +08:00
}
func SaveLog() {
for result := range Results {
2023-07-18 13:43:11 +03:00
if !Silent {
if strings.Contains(*result, "[+]") {
color.Green(*result)
} else if strings.Contains(*result, "[*]") {
color.Cyan(*result)
}
2021-05-14 10:43:26 +08:00
}
2023-07-18 13:43:11 +03:00
2020-12-29 17:17:10 +08:00
if IsSave {
WriteFile(*result, Outputfile)
2020-12-29 17:17:10 +08:00
}
2023-07-18 13:43:11 +03:00
2021-05-06 11:37:29 +08:00
LogWG.Done()
2020-12-29 17:17:10 +08:00
}
}
func WriteFile(result string, filename string) {
var text = []byte(result + "\n")
2022-02-28 10:35:50 +08:00
fl, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
2020-12-29 17:17:10 +08:00
if err != nil {
fmt.Printf("Open %s error, %v\n", filename, err)
2020-12-29 17:17:10 +08:00
return
}
2023-07-18 13:43:11 +03:00
defer func() {
_ = fl.Close()
}()
if _, err := fl.Write(text); err != nil {
2021-03-30 22:33:16 +08:00
fmt.Printf("Write %s error, %v\n", filename, err)
2020-12-29 17:17:10 +08:00
}
}
func LogError(errinfo interface{}) {
2023-07-18 13:43:11 +03:00
if WaitTime == 0 || (time.Now().Unix()-LogSucTime) > WaitTime && (time.Now().Unix()-LogErrTime) > WaitTime {
color.Red(fmt.Sprintf("Completed %v/%v %v \n", End, Num, errinfo))
if WaitTime != 0 {
LogErrTime = time.Now().Unix()
}
2021-03-30 18:12:54 +08:00
}
}
func CheckErrs(err error) bool {
if err == nil {
return false
}
2021-04-21 00:13:04 +08:00
errs := []string{
"closed by the remote host", "too many connections",
"i/o timeout", "EOF", "A connection attempt failed",
"established connection failed", "connection attempt failed",
"Unable to read", "is not allowed to connect to this",
"no pg_hba.conf entry",
"No connection could be made",
2021-05-13 18:06:14 +08:00
"invalid packet size",
2021-05-27 14:19:21 +08:00
"bad connection",
2021-04-21 00:13:04 +08:00
}
2021-03-30 18:12:54 +08:00
for _, key := range errs {
if strings.Contains(strings.ToLower(err.Error()), strings.ToLower(key)) {
return true
}
}
2021-03-30 18:12:54 +08:00
return false
}