Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
fa0fedff
authored
Sep 03, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: pass through error out
parent
e7eb4474
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
56 additions
and
37 deletions
+56
-37
controller/relay-text.go
+0
-1
middleware/auth.go
+12
-1
model/cache.go
+16
-11
model/token.go
+24
-19
model/user.go
+4
-5
No files found.
controller/relay-text.go
View file @
fa0fedff
...
@@ -377,7 +377,6 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -377,7 +377,6 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
logContent
:=
fmt
.
Sprintf
(
"模型倍率 %.2f,分组倍率 %.2f"
,
modelRatio
,
groupRatio
)
logContent
:=
fmt
.
Sprintf
(
"模型倍率 %.2f,分组倍率 %.2f"
,
modelRatio
,
groupRatio
)
model
.
RecordConsumeLog
(
userId
,
promptTokens
,
completionTokens
,
textRequest
.
Model
,
tokenName
,
quota
,
logContent
)
model
.
RecordConsumeLog
(
userId
,
promptTokens
,
completionTokens
,
textRequest
.
Model
,
tokenName
,
quota
,
logContent
)
model
.
UpdateUserUsedQuotaAndRequestCount
(
userId
,
quota
)
model
.
UpdateUserUsedQuotaAndRequestCount
(
userId
,
quota
)
model
.
UpdateChannelUsedQuota
(
channelId
,
quota
)
model
.
UpdateChannelUsedQuota
(
channelId
,
quota
)
}
}
}
}
...
...
middleware/auth.go
View file @
fa0fedff
...
@@ -100,7 +100,18 @@ func TokenAuth() func(c *gin.Context) {
...
@@ -100,7 +100,18 @@ func TokenAuth() func(c *gin.Context) {
c
.
Abort
()
c
.
Abort
()
return
return
}
}
if
!
model
.
CacheIsUserEnabled
(
token
.
UserId
)
{
userEnabled
,
err
:=
model
.
IsUserEnabled
(
token
.
UserId
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusInternalServerError
,
gin
.
H
{
"error"
:
gin
.
H
{
"message"
:
err
.
Error
(),
"type"
:
"one_api_error"
,
},
})
c
.
Abort
()
return
}
if
!
userEnabled
{
c
.
JSON
(
http
.
StatusForbidden
,
gin
.
H
{
c
.
JSON
(
http
.
StatusForbidden
,
gin
.
H
{
"error"
:
gin
.
H
{
"error"
:
gin
.
H
{
"message"
:
"用户已被封禁"
,
"message"
:
"用户已被封禁"
,
...
...
model/cache.go
View file @
fa0fedff
...
@@ -103,23 +103,28 @@ func CacheDecreaseUserQuota(id int, quota int) error {
...
@@ -103,23 +103,28 @@ func CacheDecreaseUserQuota(id int, quota int) error {
return
err
return
err
}
}
func
CacheIsUserEnabled
(
userId
int
)
bool
{
func
CacheIsUserEnabled
(
userId
int
)
(
bool
,
error
)
{
if
!
common
.
RedisEnabled
{
if
!
common
.
RedisEnabled
{
return
IsUserEnabled
(
userId
)
return
IsUserEnabled
(
userId
)
}
}
enabled
,
err
:=
common
.
RedisGet
(
fmt
.
Sprintf
(
"user_enabled:%d"
,
userId
))
enabled
,
err
:=
common
.
RedisGet
(
fmt
.
Sprintf
(
"user_enabled:%d"
,
userId
))
if
err
==
nil
{
return
enabled
==
"1"
,
nil
}
userEnabled
,
err
:=
IsUserEnabled
(
userId
)
if
err
!=
nil
{
if
err
!=
nil
{
status
:=
common
.
UserStatusDisabled
return
false
,
err
if
IsUserEnabled
(
userId
)
{
}
status
=
common
.
UserStatusEnabled
enabled
=
"0"
}
if
userEnabled
{
enabled
=
fmt
.
Sprintf
(
"%d"
,
status
)
enabled
=
"1"
err
=
common
.
RedisSet
(
fmt
.
Sprintf
(
"user_enabled:%d"
,
userId
),
enabled
,
time
.
Duration
(
UserId2StatusCacheSeconds
)
*
time
.
Second
)
}
if
err
!=
nil
{
err
=
common
.
RedisSet
(
fmt
.
Sprintf
(
"user_enabled:%d"
,
userId
),
enabled
,
time
.
Duration
(
UserId2StatusCacheSeconds
)
*
time
.
Second
)
common
.
SysError
(
"Redis set user enabled error: "
+
err
.
Error
())
if
err
!=
nil
{
}
common
.
SysError
(
"Redis set user enabled error: "
+
err
.
Error
())
}
}
return
enabled
==
"1"
return
userEnabled
,
err
}
}
var
group2model2channels
map
[
string
]
map
[
string
][]
*
Channel
var
group2model2channels
map
[
string
]
map
[
string
][]
*
Channel
...
...
model/token.go
View file @
fa0fedff
...
@@ -39,32 +39,35 @@ func ValidateUserToken(key string) (token *Token, err error) {
...
@@ -39,32 +39,35 @@ func ValidateUserToken(key string) (token *Token, err error) {
}
}
token
,
err
=
CacheGetTokenByKey
(
key
)
token
,
err
=
CacheGetTokenByKey
(
key
)
if
err
==
nil
{
if
err
==
nil
{
if
token
.
Status
==
common
.
TokenStatusExhausted
{
return
nil
,
errors
.
New
(
"该令牌额度已用尽"
)
}
else
if
token
.
Status
==
common
.
TokenStatusExpired
{
return
nil
,
errors
.
New
(
"该令牌已过期"
)
}
if
token
.
Status
!=
common
.
TokenStatusEnabled
{
if
token
.
Status
!=
common
.
TokenStatusEnabled
{
return
nil
,
errors
.
New
(
"该令牌状态不可用"
)
return
nil
,
errors
.
New
(
"该令牌状态不可用"
)
}
}
if
token
.
ExpiredTime
!=
-
1
&&
token
.
ExpiredTime
<
common
.
GetTimestamp
()
{
if
token
.
ExpiredTime
!=
-
1
&&
token
.
ExpiredTime
<
common
.
GetTimestamp
()
{
token
.
Status
=
common
.
TokenStatusExpired
if
!
common
.
RedisEnabled
{
err
:=
token
.
SelectUpdate
()
token
.
Status
=
common
.
TokenStatusExpired
if
err
!=
nil
{
err
:=
token
.
SelectUpdate
()
common
.
SysError
(
"failed to update token status"
+
err
.
Error
())
if
err
!=
nil
{
common
.
SysError
(
"failed to update token status"
+
err
.
Error
())
}
}
}
return
nil
,
errors
.
New
(
"该令牌已过期"
)
return
nil
,
errors
.
New
(
"该令牌已过期"
)
}
}
if
!
token
.
UnlimitedQuota
&&
token
.
RemainQuota
<=
0
{
if
!
token
.
UnlimitedQuota
&&
token
.
RemainQuota
<=
0
{
token
.
Status
=
common
.
TokenStatusExhausted
if
!
common
.
RedisEnabled
{
err
:=
token
.
SelectUpdate
()
// in this case, we can make sure the token is exhausted
if
err
!=
nil
{
token
.
Status
=
common
.
TokenStatusExhausted
common
.
SysError
(
"failed to update token status"
+
err
.
Error
())
err
:=
token
.
SelectUpdate
()
if
err
!=
nil
{
common
.
SysError
(
"failed to update token status"
+
err
.
Error
())
}
}
}
return
nil
,
errors
.
New
(
"该令牌额度已用尽"
)
return
nil
,
errors
.
New
(
"该令牌额度已用尽"
)
}
}
go
func
()
{
token
.
AccessedTime
=
common
.
GetTimestamp
()
err
:=
token
.
SelectUpdate
()
if
err
!=
nil
{
common
.
SysError
(
"failed to update token"
+
err
.
Error
())
}
}()
return
token
,
nil
return
token
,
nil
}
}
return
nil
,
errors
.
New
(
"无效的令牌"
)
return
nil
,
errors
.
New
(
"无效的令牌"
)
...
@@ -141,8 +144,9 @@ func IncreaseTokenQuota(id int, quota int) (err error) {
...
@@ -141,8 +144,9 @@ func IncreaseTokenQuota(id int, quota int) (err error) {
func
increaseTokenQuota
(
id
int
,
quota
int
)
(
err
error
)
{
func
increaseTokenQuota
(
id
int
,
quota
int
)
(
err
error
)
{
err
=
DB
.
Model
(
&
Token
{})
.
Where
(
"id = ?"
,
id
)
.
Updates
(
err
=
DB
.
Model
(
&
Token
{})
.
Where
(
"id = ?"
,
id
)
.
Updates
(
map
[
string
]
interface
{}{
map
[
string
]
interface
{}{
"remain_quota"
:
gorm
.
Expr
(
"remain_quota + ?"
,
quota
),
"remain_quota"
:
gorm
.
Expr
(
"remain_quota + ?"
,
quota
),
"used_quota"
:
gorm
.
Expr
(
"used_quota - ?"
,
quota
),
"used_quota"
:
gorm
.
Expr
(
"used_quota - ?"
,
quota
),
"accessed_time"
:
common
.
GetTimestamp
(),
},
},
)
.
Error
)
.
Error
return
err
return
err
...
@@ -162,8 +166,9 @@ func DecreaseTokenQuota(id int, quota int) (err error) {
...
@@ -162,8 +166,9 @@ func DecreaseTokenQuota(id int, quota int) (err error) {
func
decreaseTokenQuota
(
id
int
,
quota
int
)
(
err
error
)
{
func
decreaseTokenQuota
(
id
int
,
quota
int
)
(
err
error
)
{
err
=
DB
.
Model
(
&
Token
{})
.
Where
(
"id = ?"
,
id
)
.
Updates
(
err
=
DB
.
Model
(
&
Token
{})
.
Where
(
"id = ?"
,
id
)
.
Updates
(
map
[
string
]
interface
{}{
map
[
string
]
interface
{}{
"remain_quota"
:
gorm
.
Expr
(
"remain_quota - ?"
,
quota
),
"remain_quota"
:
gorm
.
Expr
(
"remain_quota - ?"
,
quota
),
"used_quota"
:
gorm
.
Expr
(
"used_quota + ?"
,
quota
),
"used_quota"
:
gorm
.
Expr
(
"used_quota + ?"
,
quota
),
"accessed_time"
:
common
.
GetTimestamp
(),
},
},
)
.
Error
)
.
Error
return
err
return
err
...
...
model/user.go
View file @
fa0fedff
...
@@ -226,17 +226,16 @@ func IsAdmin(userId int) bool {
...
@@ -226,17 +226,16 @@ func IsAdmin(userId int) bool {
return
user
.
Role
>=
common
.
RoleAdminUser
return
user
.
Role
>=
common
.
RoleAdminUser
}
}
func
IsUserEnabled
(
userId
int
)
bool
{
func
IsUserEnabled
(
userId
int
)
(
bool
,
error
)
{
if
userId
==
0
{
if
userId
==
0
{
return
false
return
false
,
errors
.
New
(
"user id is empty"
)
}
}
var
user
User
var
user
User
err
:=
DB
.
Where
(
"id = ?"
,
userId
)
.
Select
(
"status"
)
.
Find
(
&
user
)
.
Error
err
:=
DB
.
Where
(
"id = ?"
,
userId
)
.
Select
(
"status"
)
.
Find
(
&
user
)
.
Error
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"no such user "
+
err
.
Error
())
return
false
,
err
return
false
}
}
return
user
.
Status
==
common
.
UserStatusEnabled
return
user
.
Status
==
common
.
UserStatusEnabled
,
nil
}
}
func
ValidateAccessToken
(
token
string
)
(
user
*
User
)
{
func
ValidateAccessToken
(
token
string
)
(
user
*
User
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment