New functions

This commit is contained in:
刘河
2019-02-24 13:17:43 +08:00
parent 750ecb824a
commit db43405237
20 changed files with 287 additions and 74 deletions

View File

@@ -372,7 +372,19 @@ func (s *Csv) DelClient(id int) error {
return errors.New("不存在")
}
func (s *Csv) NewClient(c *Client) {
func (s *Csv) NewClient(c *Client) error {
var isNotSet bool
reset:
if c.VerifyKey == "" || isNotSet {
isNotSet = true
c.VerifyKey = crypt.GetRandomString(16)
}
if !s.VerifyVkey(c.VerifyKey, c.id) {
if isNotSet {
goto reset
}
return errors.New("Vkey duplicate, please reset")
}
if c.Id == 0 {
c.Id = s.GetClientId()
}
@@ -383,6 +395,16 @@ func (s *Csv) NewClient(c *Client) {
defer s.Unlock()
s.Clients = append(s.Clients, c)
s.StoreClientsToCsv()
return nil
}
func (s *Csv) VerifyVkey(vkey string, id int) bool {
for _, v := range s.Clients {
if v.VerifyKey == vkey && v.Id != id {
return false
}
}
return true
}
func (s *Csv) GetClientId() int {

View File

@@ -2,6 +2,7 @@ package file
import (
"github.com/cnlh/nps/lib/rate"
"math"
"strings"
"sync"
)
@@ -60,6 +61,9 @@ func NewClient(vKey string, noStore bool, noDisplay bool) *Client {
func (s *Client) GetId() int {
s.Lock()
defer s.Unlock()
if s.id == math.MaxInt32 {
s.id = 0
}
s.id++
return s.id
}