秒表 Go 语言

huangapple go评论81阅读模式
英文:

Stopwatch go lang

问题

我有一个防火墙,每当检测到攻击时,它会发送一个 Discord Webhook,当攻击不再被检测到时也会发送。我想添加一个功能,当它发送攻击检测到的 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()

以下是攻击不再被检测到时的代码:

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()
英文:

i have a firewall that sends a discord webhook whenever an attack is detected and whenever the attack is no longer detected. I want to add something where it starts a stopwatch whenever it sends the webhook for attack detected. then stops the stopwatch whenever its no longer detected so that it sends how long the attack lasted in seconds with the no longer detected webhook.

This is for when its detected.

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

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

</details>


# 答案1
**得分**: 2

可能是这样的,就像Burak建议的那样。请注意,这意味着你只有一个防火墙,只能有一个攻击,并且Webhooks位于同一实例上,因此attackStartTime.tmp文件对attackEnd是可访问的。

```go
package main

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

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

func attackStart()  {

    //... 我的 cmd PHP 代码

	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() {
	
    //... 我的 cmd PHP 代码
	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("攻击开始时间:%v\n攻击结束时间:%v\n持续时间(秒):%v\n",timeAttackStarted, timeAttackEnded, duration.Seconds())
}
英文:

Could be something like this, as Burak suggested. Note, that it implies that you have only one firewall which can have only one attack, and the webhooks are located on the same instance, so the attackStartTime.tmp file is reachable for attackEnd.

package main

import (
	&quot;fmt&quot;
	&quot;io/ioutil&quot;
	&quot;log&quot;
	&quot;time&quot;
)

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

func attackStart()  {

    //... my cmd PHP code

	fileName := &quot;/tmp/attackStartTime.tmp&quot;
	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 := &quot;/tmp/attackStartTime.tmp&quot;
	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(&quot;attack started at %v:\nattack ended: %v\nduration(seconds): %v\n&quot;,timeAttackStarted, timeAttackEnded, duration.Seconds())
}

huangapple
  • 本文由 发表于 2022年5月19日 22:42:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/72306610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定