登录
首页 >  Golang >  Go问答

计时器跑者

来源:stackoverflow

时间:2024-03-11 14:54:17 124浏览 收藏

小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《计时器跑者》,以下内容将会涉及到,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!

问题内容

我有一个防火墙,每当检测到攻击以及不再检测到攻击时,它都会发送不一致的 webhook。我想添加一些内容,每当它发送检测到攻击的网络钩子时,它就会启动秒表。然后,每当不再检测到秒表时,它就会停止秒表,以便它发送不再检测到的 webhook 的攻击持续时间(以秒为单位)。

这是检测到时的情况。

fmt.Println("Rlimit Final", rLimit)
        cmd := exec.Command("php", "start.php", strconv.Itoa(reqs), strconv.Itoa(rps), strconv.Itoa(requested), strconv.Itoa(passedcaptcha), "ONLINE", "200", "FIREWALL")
        cmd.Run()```

/*and this is when it's no longer detected:*/
        if rps <= 20 && mitigation != 0 {
            cmd := exec.Command("php", "end.php", strconv.Itoa(totalreqs), strconv.Itoa(largerNumber), strconv.Itoa(totalver), strconv.Itoa(passedcaptcha), "ONLINE", "200", "FIREWALL")
            cmd.Run()

正确答案


正如 burak 所建议的,可能是这样的。请注意,这意味着您只有一台防火墙,只能进行一次攻击,并且 webhooks 位于同一实例上,因此 attackend 可以访问attackstarttime.tmp 文件。

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "time"
)

func main() {
    attackStart()
    time.Sleep(time.Second*3)
    attackEnd()
}

func attackStart()  {

    //... my cmd PHP code

    fileName := "/tmp/attackStartTime.tmp"
    timeAttackStarted := []byte(time.Now().Local().Format(time.RFC3339))

    if err := ioutil.WriteFile(fileName, timeAttackStarted, 0666); err != nil {
        log.Fatal(err)
    }

}

func attackEnd() {
    
    //... my cmd PHP code
    fileName := "/tmp/attackStartTime.tmp"
    filecontent, err := ioutil.ReadFile(fileName)
    timeAttackEnded := time.Now().Local()
    if err != nil {
        log.Fatal(err)
    }
    timeAttackStarted, err := time.Parse(time.RFC3339, string(filecontent))
    if err != nil {
        log.Fatal(err)
    }
    duration := timeAttackEnded.Sub(timeAttackStarted)
    fmt.Printf("attack started at %v:\nattack ended: %v\nduration(seconds): %v\n",timeAttackStarted, timeAttackEnded, duration.Seconds())
}

终于介绍完啦!小伙伴们,这篇关于《计时器跑者》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>