Skip to content

Commit

Permalink
feat: hiding secret for some "lazy" models (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Feb 2, 2024
1 parent e277301 commit ed14c5a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
14 changes: 13 additions & 1 deletion channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (c *Channel) GetSecret() string {
return c.Secret
}

func (c *Channel) GetCurrentSecret() *string {
return c.CurrentSecret
}

// GetRandomSecret returns a random secret from the secret list
func (c *Channel) GetRandomSecret() string {
arr := strings.Split(c.GetSecret(), "\n")
Expand All @@ -61,7 +65,10 @@ func (c *Channel) GetRandomSecret() string {
}

idx := utils.Intn(len(arr))
return arr[idx]
secret := arr[idx]

c.CurrentSecret = &secret
return secret
}

func (c *Channel) SplitRandomSecret(num int) []string {
Expand Down Expand Up @@ -199,5 +206,10 @@ func (c *Channel) ProcessError(err error) error {
content = strings.Replace(content, item, "chatnio_upstream", -1)
}

secret := c.GetCurrentSecret()
if secret != nil {
content = strings.Replace(content, *secret, utils.ToSecret(*secret), -1)
}

return errors.New(content)
}
1 change: 1 addition & 0 deletions channel/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Channel struct {
Reflect *map[string]string `json:"-"`
HitModels *[]string `json:"-"`
ExcludeModels *[]string `json:"-"`
CurrentSecret *string `json:"-"`
}

type Sequence []*Channel
Expand Down
14 changes: 14 additions & 0 deletions utils/char.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,17 @@ func SafeSplit(data string, sep string, seglen int) (res []string) {
return arr[:seglen]
}
}

func ToSecret(raw string) string {
// like `axVbeixvN` => `axVb*****`

data := []rune(raw)
length := len(data)

if length < 4 {
return "****"
} else {
suffix := len(data) - 4
return string(data[:4]) + strings.Repeat("*", suffix)
}
}

0 comments on commit ed14c5a

Please sign in to comment.