2024-12-01 06:32:40 +00:00
|
|
|
package golcg
|
2024-12-01 05:55:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LCG struct {
|
2024-12-01 06:32:40 +00:00
|
|
|
M uint32
|
|
|
|
A uint32
|
|
|
|
C uint32
|
|
|
|
Current uint32
|
2024-12-01 05:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLCG(seed int, m uint32) *LCG {
|
|
|
|
return &LCG{
|
2024-12-06 18:41:15 +00:00
|
|
|
M: 1<<32 - 1,
|
2024-12-01 06:32:40 +00:00
|
|
|
A: 1664525,
|
|
|
|
C: 1013904223,
|
|
|
|
Current: uint32(seed),
|
2024-12-01 05:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LCG) Next() uint32 {
|
2024-12-01 06:32:40 +00:00
|
|
|
l.Current = (l.A*l.Current + l.C) % l.M
|
|
|
|
return l.Current
|
2024-12-01 05:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type IPRange struct {
|
2024-12-01 06:32:40 +00:00
|
|
|
Start uint32
|
|
|
|
Total uint32
|
2024-12-01 05:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIPRange(cidr string) (*IPRange, error) {
|
|
|
|
_, network, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
start := ipToUint32(network.IP)
|
|
|
|
ones, bits := network.Mask.Size()
|
|
|
|
hostBits := uint(bits - ones)
|
2024-12-06 18:41:15 +00:00
|
|
|
|
|
|
|
var total uint32
|
|
|
|
if hostBits == 32 {
|
|
|
|
total = 0
|
|
|
|
} else {
|
|
|
|
total = 1 << hostBits
|
|
|
|
}
|
2024-12-01 05:55:06 +00:00
|
|
|
|
|
|
|
return &IPRange{
|
2024-12-01 06:32:40 +00:00
|
|
|
Start: start,
|
2024-12-06 18:41:15 +00:00
|
|
|
Total: total,
|
2024-12-01 05:55:06 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IPRange) GetIPAtIndex(index uint32) (string, error) {
|
2024-12-06 18:41:15 +00:00
|
|
|
if r.Total > 0 && index >= r.Total {
|
2024-12-01 05:55:06 +00:00
|
|
|
return "", errors.New("IP index out of range")
|
|
|
|
}
|
|
|
|
|
2024-12-01 06:32:40 +00:00
|
|
|
ip := uint32ToIP(r.Start + index)
|
2024-12-01 05:55:06 +00:00
|
|
|
return ip.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ipToUint32(ip net.IP) uint32 {
|
|
|
|
ip = ip.To4()
|
|
|
|
return uint32(ip[0])<<24 | uint32(ip[1])<<16 | uint32(ip[2])<<8 | uint32(ip[3])
|
|
|
|
}
|
|
|
|
|
|
|
|
func uint32ToIP(n uint32) net.IP {
|
|
|
|
ip := make(net.IP, 4)
|
|
|
|
ip[0] = byte(n >> 24)
|
|
|
|
ip[1] = byte(n >> 16)
|
|
|
|
ip[2] = byte(n >> 8)
|
|
|
|
ip[3] = byte(n)
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveState(seed int, cidr string, shard int, total int, lcgCurrent uint32) error {
|
2024-12-06 18:41:15 +00:00
|
|
|
fileName := fmt.Sprintf("golcg_%d_%s_%d_%d.state", seed, strings.Replace(cidr, "/", "_", -1), shard, total)
|
2024-12-01 05:55:06 +00:00
|
|
|
stateFile := filepath.Join(os.TempDir(), fileName)
|
|
|
|
|
|
|
|
return os.WriteFile(stateFile, []byte(fmt.Sprintf("%d", lcgCurrent)), 0644)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IPStream(cidr string, shardNum, totalShards, seed int, state *uint32) (<-chan string, error) {
|
|
|
|
ipRange, err := NewIPRange(cidr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
shardIndex := shardNum - 1
|
|
|
|
|
|
|
|
if seed == 0 {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
seed = rand.Intn(1<<32 - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
lcg := NewLCG(seed+shardIndex, 1<<32-1)
|
|
|
|
if state != nil {
|
2024-12-01 06:32:40 +00:00
|
|
|
lcg.Current = *state
|
2024-12-01 05:55:06 +00:00
|
|
|
}
|
|
|
|
|
2024-12-06 18:41:15 +00:00
|
|
|
var shardSize uint32
|
|
|
|
if ipRange.Total == 0 {
|
|
|
|
shardSize = (1<<32 - 1) / uint32(totalShards)
|
|
|
|
if uint32(shardIndex) < uint32(totalShards-1) {
|
|
|
|
shardSize++
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
shardSize = ipRange.Total / uint32(totalShards)
|
|
|
|
if uint32(shardIndex) < ipRange.Total%uint32(totalShards) {
|
|
|
|
shardSize++
|
|
|
|
}
|
2024-12-01 05:55:06 +00:00
|
|
|
}
|
|
|
|
|
2024-12-06 18:41:15 +00:00
|
|
|
out := make(chan string, 1000)
|
2024-12-01 05:55:06 +00:00
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
remaining := shardSize
|
|
|
|
|
|
|
|
for remaining > 0 {
|
2024-12-06 18:41:15 +00:00
|
|
|
next := lcg.Next()
|
|
|
|
var index uint32
|
|
|
|
if ipRange.Total > 0 {
|
|
|
|
index = next % ipRange.Total
|
|
|
|
} else {
|
|
|
|
index = next
|
|
|
|
}
|
|
|
|
|
2024-12-01 05:55:06 +00:00
|
|
|
if totalShards == 1 || index%uint32(totalShards) == uint32(shardIndex) {
|
|
|
|
ip, err := ipRange.GetIPAtIndex(index)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out <- ip
|
|
|
|
remaining--
|
|
|
|
|
|
|
|
if remaining%1000 == 0 {
|
2024-12-01 06:32:40 +00:00
|
|
|
SaveState(seed, cidr, shardNum, totalShards, lcg.Current)
|
2024-12-01 05:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|