mirror of
https://github.com/projectdiscovery/subfinder
synced 2026-06-21 14:05:24 +00:00
6867e076f1
Keeps module path as github.com/projectdiscovery/subfinder/v2 to preserve import compatibility. Signed-off-by: Mikel Olasagasti Uranga <mikel@olasagasti.info>
43 lines
886 B
Go
43 lines
886 B
Go
package subscraping
|
|
|
|
import (
|
|
"math/rand"
|
|
"strings"
|
|
|
|
"github.com/projectdiscovery/gologger"
|
|
)
|
|
|
|
const MultipleKeyPartsLength = 2
|
|
|
|
func PickRandom[T any](v []T, sourceName string) T {
|
|
var result T
|
|
length := len(v)
|
|
if length == 0 {
|
|
gologger.Debug().Msgf("Cannot use the %s source because there was no API key/secret defined for it.", sourceName)
|
|
return result
|
|
}
|
|
return v[rand.Intn(length)]
|
|
}
|
|
|
|
func CreateApiKeys[T any](keys []string, provider func(k, v string) T) []T {
|
|
var result []T
|
|
for _, key := range keys {
|
|
if keyPartA, keyPartB, ok := createMultiPartKey(key); ok {
|
|
result = append(result, provider(keyPartA, keyPartB))
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func createMultiPartKey(key string) (keyPartA, keyPartB string, ok bool) {
|
|
parts := strings.Split(key, ":")
|
|
ok = len(parts) == MultipleKeyPartsLength
|
|
|
|
if ok {
|
|
keyPartA = parts[0]
|
|
keyPartB = parts[1]
|
|
}
|
|
|
|
return
|
|
}
|