pkg/rands/net.go
2023-09-18 15:51:20 +08:00

45 lines
995 B
Go

package pkg
import (
"fmt"
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func RandMac() string {
return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", rand.Intn(256), rand.Intn(256), rand.Intn(256), rand.Intn(256), rand.Intn(256), rand.Intn(256))
}
func RandIp() string {
return fmt.Sprintf("%d.%d.%d.%d", rand.Intn(256), rand.Intn(256), rand.Intn(256), rand.Intn(256))
}
func RandPort() uint16 {
return uint16(rand.Intn(35536) + 30000)
}
func RandVni() uint32 {
return uint32(rand.Intn(1 << 24))
}
func RandDstPort() uint16 {
// 固定端口范围,流量好看点,也能发现是否有人拿我们流量去做其他测试
ports := []uint16{
80, 8080, 8081, 8082, 8083, 8084, 8085, 8086, 8087,
}
list := []int{
8000, 10000, 12000, 14000, 16000, 18000, 20000, 22000, 24000,
}
for _, item := range list {
for i := 0; i < 20; i++ {
ports = append(ports, uint16(rand.Intn(30)+item))
}
}
size := len(ports)
return ports[rand.Intn(size)]
}