Skip to content

Commit

Permalink
Merge pull request #465 from MortezaBashsiz/golang-logger
Browse files Browse the repository at this point in the history
Golang logger
  • Loading branch information
SonyaCore authored Apr 16, 2023
2 parents 4acb2f2 + 88ba49e commit 425f52c
Show file tree
Hide file tree
Showing 11 changed files with 368 additions and 172 deletions.
4 changes: 1 addition & 3 deletions golang/configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ User ID : %v%v%v
WS Header Host: %v%v%v
WS Header Path : %v%v%v
Address Port : %v%v%v
SNI : %v%v%v
Upload Test : %v%v%v
Fronting Request Test : %v%v%v
Minimum Download Speed : %v%v%v
Expand All @@ -51,7 +50,6 @@ Total Threads : %v%v%v
utils.Colors.OKBLUE, C.Config.WsHeaderHost, utils.Colors.ENDC,
utils.Colors.OKBLUE, C.Config.WsHeaderPath, utils.Colors.ENDC,
utils.Colors.OKBLUE, C.Config.AddressPort, utils.Colors.ENDC,
utils.Colors.OKBLUE, C.Config.Sni, utils.Colors.ENDC,
utils.Colors.OKBLUE, C.Config.DoUploadTest, utils.Colors.ENDC,
utils.Colors.OKBLUE, C.Config.DoFrontingTest, utils.Colors.ENDC,
utils.Colors.OKBLUE, C.Worker.Download.MinDlSpeed, utils.Colors.ENDC,
Expand Down Expand Up @@ -99,7 +97,7 @@ func (C Configuration) CreateTestConfig(configPath string) Configuration {
C.Config.UserId = jsonFileContent["id"].(string)
C.Config.WsHeaderHost = jsonFileContent["host"].(string)
C.Config.AddressPort = jsonFileContent["port"].(string)
C.Config.Sni = jsonFileContent["serverName"].(string)
//C.Config.Sni = jsonFileContent["serverName"].(string)
C.Config.WsHeaderPath = "/" + strings.TrimLeft(jsonFileContent["path"].(string), "/")

C.PrintInformation()
Expand Down
2 changes: 1 addition & 1 deletion golang/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ require (
golang.org/x/exp v0.0.0-20230307190834-24139beb5833 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.7.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions golang/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
16 changes: 16 additions & 0 deletions golang/logger/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package logger

// errors const
const (
DownloadError = "Download Error"
UploadError = "Upload Error"
UploadLatency = "Upload Latency too high"
)

// status const
const (
OKStatus = LogStatus("[OK]")
FailStatus = LogStatus("[FAIL]")
ErrorStatus = LogStatus("[ERROR]")
InfoStatus = LogStatus("[INFO]")
)
67 changes: 67 additions & 0 deletions golang/logger/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package logger

import (
"CFScanner/utils"
"fmt"
"os"
"strings"
"time"
)

type LogStatus string

var (
red = utils.Colors.FAIL
green = utils.Colors.OKGREEN
yellow = utils.Colors.WARNING
reset = utils.Colors.ENDC
)

func (m *ScannerManage) String() string {
builder := &strings.Builder{}
builder.WriteString(time.Now().Format("2006/01/02 15:04:05"))
builder.WriteString(" ")

switch m.Status {
case FailStatus:
builder.WriteString(red)
case ErrorStatus:
builder.WriteString(red)
case InfoStatus:
builder.WriteString(yellow)
case OKStatus:
builder.WriteString(green)
}

builder.WriteString(string(m.Status))
builder.WriteString(reset)
builder.WriteByte(' ')
builder.WriteString(m.IP)
builder.WriteByte(' ')
builder.WriteString(reset)

if status := string(m.Status); len(status) > 0 {
switch m.Status {
case FailStatus:
builder.WriteString(yellow)
case OKStatus:
builder.WriteString(green)
}
builder.WriteString(" ")
builder.WriteString(fmt.Sprintf("%v", m.Message))
builder.WriteString(reset)
}

if err := m.Cause; err != "" {
builder.WriteString(red)
builder.WriteString(" ")
builder.WriteString(m.Cause)
builder.WriteString(reset)
}

return builder.String()
}

func (m *ScannerManage) Print() {
fmt.Fprintln(os.Stdout, m.String())
}
9 changes: 9 additions & 0 deletions golang/logger/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package logger

// ScannerManage is a logger for logging scanning process
type ScannerManage struct {
IP string
Status LogStatus
Message interface{}
Cause string
}
3 changes: 2 additions & 1 deletion golang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"os"
"runtime"
"strings"
"time"
)

// Program Info
var (
version = "1.6"
version = "v" + time.Now().Format("2006.01.02")
build = "Custom"
codename = "CFScanner , CloudFlare Scanner."
)
Expand Down
2 changes: 1 addition & 1 deletion golang/scanner/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func WriteCSV(file string, result []interface{}) {

func (c CSV) Output() {

log.Printf("%sOK %-15s %s avg_down_speed: %7.2fmbps avg_up_speed: %7.4fmbps avg_down_latency: %6.2fms avg_up_latency: %6.2fms avg_down_jitter: %6.2fms avg_up_jitter: %4.2fms%s\n",
log.Printf("%s[OK] %-15s %s avg_down_speed: %7.2fmbps avg_up_speed: %7.4fmbps avg_down_latency: %6.2fms avg_up_latency: %6.2fms avg_down_jitter: %6.2fms avg_up_jitter: %4.2fms%s\n",
utils.Colors.OKGREEN,
c.res.IP,
utils.Colors.OKBLUE,
Expand Down
Loading

0 comments on commit 425f52c

Please sign in to comment.