英文:
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("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()
</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 (
"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())
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论