1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package public
- import (
- "bytes"
- cr "crypto/rand"
- "math/big"
- "math/rand"
- "qfw/util/redis"
- "service/config"
- "strings"
- "sync"
- "time"
- )
- var VarLSCPool = &LSCPool{
- JobQueue: make(chan string, config.Sysconfig.JobNum),
- WorkerNum: config.Sysconfig.WorkerNum,
- Lock: &sync.Mutex{},
- Randoms: "",
- }
- type LSCPool struct {
- JobQueue chan string //待取的口令
- WorkerNum int //当前工作的协程数
- Lock *sync.Mutex //口令锁
- Randoms string //口令集合
- }
- func (this *LSCPool) GetJob() string {
- return <-this.JobQueue
- }
- var r *rand.Rand
- func init() {
- r = rand.New(rand.NewSource(time.Now().Unix()))
- for i := 0; i < VarLSCPool.WorkerNum/2; i++ {
- go func() {
- for {
- VarLSCPool.JobQueue <- VarLSCPool.GetRandom()
- }
- }()
- }
- }
- //获取随机字符串
- func (this *LSCPool) GetRandom() string {
- this.Lock.Lock()
- defer this.Lock.Unlock()
- //如果使用过了 重新获取
- for {
- LSC := this.SpecialChar(0) + this.LetterRandom(8)
- if strings.Contains(this.Randoms, LSC) {
- continue
- }
- this.Randoms += LSC + "|"
- ok, _ := redis.Exists("other", "DIS_"+LSC)
- if ok {
- continue
- }
- // log.Println("获取口令:", LSC)
- return LSC
- }
- return ""
- }
- //字母随机串
- func (this *LSCPool) LetterRandom(LL int) (LR string) {
- bytes := make([]byte, LL)
- for i := 0; i < LL; i++ {
- b := r.Intn(26) + 65
- bytes[i] = byte(b)
- }
- LR = string(bytes)
- return
- }
- //特殊字符随机串
- func (this *LSCPool) SpecialChar(LL int) (SC string) {
- var str = "$#"
- b := bytes.NewBufferString(str)
- length := b.Len()
- bigInt := big.NewInt(int64(length))
- for i := 0; i < LL; i++ {
- randomInt, _ := cr.Int(cr.Reader, bigInt)
- SC += string(str[randomInt.Int64()])
- }
- return
- }
|