Skip to content
This repository has been archived by the owner on May 6, 2023. It is now read-only.

Commit

Permalink
🆕 客户端增加每30秒打印本地到矿池延迟; 修改debug参数关闭时, 日志等级为info
Browse files Browse the repository at this point in the history
  • Loading branch information
perrornet committed Jan 17, 2022
1 parent e9cbb91 commit 06e957c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 30 deletions.
44 changes: 18 additions & 26 deletions cmd/miner-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ var (

var (
// build 时加入
gitHash string
gitCommit string
buildTime string
goVersion string
)
var (
reqeustUrls = []string{
Expand All @@ -72,7 +69,7 @@ func (p *proxyService) checkWxPusher() error {
if err != nil {
pkg.Fatal("获取二维码url失败: %s", err.Error())
}
pkg.Info("请复制网址, 在浏览器打开, 并使用微信进行扫码登陆: %s", qrUrl)
fmt.Printf("请复制网址, 在浏览器打开, 并使用微信进行扫码登陆: %s\n", qrUrl)
pkg.Input("您是否扫描完成?(y/n):", func(s string) bool {
if strings.ToLower(s) == "y" {
return true
Expand All @@ -87,7 +84,7 @@ func (p *proxyService) checkWxPusher() error {
}
table, _ := gotable.Create("uid", "微信昵称")
for _, v := range users {
table.AddRow(map[string]string{
_ = table.AddRow(map[string]string{
"uid": v.UId,
"微信昵称": v.NickName,
})
Expand Down Expand Up @@ -118,11 +115,12 @@ func (p *proxyService) randomRequestHttp() {
Timeout: time.Second * 10,
}
index, _ := randutil.IntRange(0, len(reqeustUrls))
pkg.Debug("http请求: ", reqeustUrls[index])
resp, _ := client.Get(reqeustUrls[index])
if resp == nil {
return
}
resp.Body.Close()
_ = resp.Body.Close()
}

func (p *proxyService) run() {
Expand Down Expand Up @@ -170,6 +168,9 @@ func (p *proxyService) run() {
}
}()
}
if *isClient {
go status.ShowDelay(time.Second * 30)
}

if *isClient && *randomSendHttp {
go p.randomRequestHttp()
Expand Down Expand Up @@ -212,23 +213,14 @@ A:

func main() {
flag.Parse()
fmt.Println("版本信息: ")
fmt.Printf("git commit hash: %s\n", gitHash)
fmt.Printf("git commit message: %s\n", gitCommit)
if buildTime != "" {
t, err := time.Parse(time.ANSIC+" -0700", buildTime)
if err == nil {
fmt.Printf("build time: %s\n", t.Format("2006-01-02 15:04:05"))
}
}
fmt.Printf("golang version: %s\n", goVersion)

pkg.PrintHelp()
fmt.Printf("版本日志: %s\n", gitCommit)
if *debug {
pkg.InitLog(zapcore.DebugLevel, *logFile)
}

if !*debug {
pkg.InitLog(zapcore.WarnLevel, *logFile)
pkg.InitLog(zapcore.InfoLevel, *logFile)
}
if *newWxPusherUser || *wxPusherToken != "" {
if err := new(proxyService).checkWxPusher(); err != nil {
Expand Down Expand Up @@ -284,14 +276,14 @@ func main() {
log.Fatalln("停止代理服务失败", err)
}
log.Println("成功停止代理服务")
pkg.Input("是否需要移除代理服务?(y/n)", func(in string) bool {
if in == "y" {
if err := s.Uninstall(); err != nil {
log.Println("删除失败", err)
}
}
return true
})
//pkg.Input("是否需要移除代理服务?(y/n)", func(in string) bool {
// if in == "y" {
// if err := s.Uninstall(); err != nil {
// log.Println("删除失败", err)
// }
// }
// return true
//})
return
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ go 1.13

require (
github.com/BurntSushi/toml v1.0.0 // indirect
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2 // indirect
github.com/dustin/go-humanize v1.0.0
github.com/jmcvetta/randutil v0.0.0-20150817122601-2bb1b664bcff
github.com/kardianos/service v1.2.0
github.com/liushuochen/gotable v0.0.0-20220106123442-3486f065ca09
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/wxpusher/wxpusher-sdk-go v1.0.3
go.uber.org/zap v1.20.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
Expand Down
12 changes: 10 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2 h1:tjT4Jp4gxECvsJcYpAMtW2I3YqzBTPuB67OejxXs86s=
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2/go.mod h1:mk5IQ+Y0ZeO87b858TlA645sVcEcbiX6YqP98kt+7+w=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dimiro1/banner v1.1.0 h1:TSfy+FsPIIGLzaMPOt52KrEed/omwFO1P15VA8PMUh0=
github.com/dimiro1/banner v1.1.0/go.mod h1:tbL318TJiUaHxOUNN+jnlvFSgsh/RX7iJaQrGgOiTco=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/jmcvetta/randutil v0.0.0-20150817122601-2bb1b664bcff h1:6NvhExg4omUC9NfA+l4Oq3ibNNeJUdiAF3iBVB0PlDk=
Expand All @@ -18,12 +22,14 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/liushuochen/gotable v0.0.0-20220106123442-3486f065ca09 h1:iZ0bdoqkDu/UZozph5/rGMm6pzvEPts/Mwqa0MOg7CI=
github.com/liushuochen/gotable v0.0.0-20220106123442-3486f065ca09/go.mod h1:CxUy8nDvutaC1pOfaG9TRoYwdHHqoNstSPPKhomC9k8=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
Expand Down Expand Up @@ -52,7 +58,9 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
15 changes: 15 additions & 0 deletions pkg/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pkg

import (
"fmt"

"github.com/common-nighthawk/go-figure"
)

func PrintHelp() {
myFigure := figure.NewFigure("Miner Proxy", "", true)
myFigure.Print()
// 免责声明以及项目地址
fmt.Println("项目地址: https://github.com/PerrorOne/miner-proxy")
fmt.Println("免责声明: 本工具只适用于测试与学习使用, 请勿将其使用到挖矿活动上!!")
}
28 changes: 28 additions & 0 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var (
startTime = time.Now()
pushers sync.Map
m sync.Mutex
parseTime int64
totalParseCount int64
)

type Status struct {
Expand Down Expand Up @@ -50,6 +52,32 @@ func init() {
}()
}

func UpdateTimeParse(t int64) {
atomic.AddInt64(&parseTime, t)
atomic.AddInt64(&totalParseCount, 1)
}

func clearTimeParse() {
atomic.StoreInt64(&parseTime, 0)
atomic.StoreInt64(&totalParseCount, 0)
}

func ShowDelay(sleepTime time.Duration) {
defer func() {
time.AfterFunc(sleepTime, func() {
ShowDelay(sleepTime)
})
}()
p := atomic.LoadInt64(&parseTime)
t := atomic.LoadInt64(&totalParseCount)
if p == 0 || t == 0 {
return
}

pkg.Info("本地到服务器 %s 内延迟为 %s", sleepTime.String(), time.Duration(float64(p)/float64(t)).String())
clearTimeParse()
}

func Show(offlineTime time.Duration) {
table, _ := gotable.Create("Ip", "传输数据大小", "连接时长", "矿池")
var result []map[string]string
Expand Down
9 changes: 8 additions & 1 deletion proxy/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"io"
"io/ioutil"
"log"
"miner-proxy/pkg/status"
"strconv"
"strings"
"time"
)

type BufferedConn struct {
Expand Down Expand Up @@ -92,22 +94,27 @@ func NewPackage(data []byte) *Package {
}

func (p *Package) Pack(writer io.Writer) error {
startTime := time.Now()
if _, err := writer.Write([]byte{uint8(PackageStart)}); err != nil {
return err
}

status.UpdateTimeParse(time.Since(startTime).Nanoseconds())
length := strconv.Itoa(p.Length)
var lengthBuf = bytes.NewBufferString(length)
for lengthBuf.Len() < 10 {
lengthBuf.WriteString("-")
}
startTime = time.Now()
if _, err := writer.Write(lengthBuf.Bytes()); err != nil {
return err
}
status.UpdateTimeParse(time.Since(startTime).Nanoseconds())

startTime = time.Now()
if _, err := writer.Write(p.Data); err != nil {
return err
}
status.UpdateTimeParse(time.Since(startTime).Nanoseconds())
return nil
}

Expand Down

0 comments on commit 06e957c

Please sign in to comment.