mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-06 00:33:43 +08:00
Compare commits
5 Commits
v0.1.4
...
v0.1.6-alp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
195e94a75d | ||
|
|
5bfc224669 | ||
|
|
fd149c242f | ||
|
|
b9cc5dfa3f | ||
|
|
8c305dc1bc |
@@ -46,6 +46,8 @@ var WeChatAccountQRCodeImageURL = ""
|
|||||||
var TurnstileSiteKey = ""
|
var TurnstileSiteKey = ""
|
||||||
var TurnstileSecretKey = ""
|
var TurnstileSecretKey = ""
|
||||||
|
|
||||||
|
var QuotaForNewUser = 100
|
||||||
|
|
||||||
const (
|
const (
|
||||||
RoleGuestUser = 0
|
RoleGuestUser = 0
|
||||||
RoleCommonUser = 1
|
RoleCommonUser = 1
|
||||||
@@ -63,7 +65,7 @@ var (
|
|||||||
// All duration's unit is seconds
|
// All duration's unit is seconds
|
||||||
// Shouldn't larger then RateLimitKeyExpirationDuration
|
// Shouldn't larger then RateLimitKeyExpirationDuration
|
||||||
var (
|
var (
|
||||||
GlobalApiRateLimitNum = 60000 // TODO: temporary set to 60000
|
GlobalApiRateLimitNum = 180
|
||||||
GlobalApiRateLimitDuration int64 = 3 * 60
|
GlobalApiRateLimitDuration int64 = 3 * 60
|
||||||
|
|
||||||
GlobalWebRateLimitNum = 60
|
GlobalWebRateLimitNum = 60
|
||||||
|
|||||||
@@ -104,6 +104,19 @@ func AddToken(c *gin.Context) {
|
|||||||
if isAdmin {
|
if isAdmin {
|
||||||
cleanToken.RemainTimes = token.RemainTimes
|
cleanToken.RemainTimes = token.RemainTimes
|
||||||
cleanToken.UnlimitedTimes = token.UnlimitedTimes
|
cleanToken.UnlimitedTimes = token.UnlimitedTimes
|
||||||
|
} else {
|
||||||
|
userId := c.GetInt("id")
|
||||||
|
quota, err := model.GetUserQuota(userId)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": false,
|
||||||
|
"message": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if quota > 0 {
|
||||||
|
cleanToken.RemainTimes = quota
|
||||||
|
}
|
||||||
}
|
}
|
||||||
err = cleanToken.Insert()
|
err = cleanToken.Insert()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -113,6 +126,10 @@ func AddToken(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if !isAdmin {
|
||||||
|
// update user quota
|
||||||
|
err = model.DecreaseUserQuota(c.GetInt("id"), cleanToken.RemainTimes)
|
||||||
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": true,
|
"success": true,
|
||||||
"message": "",
|
"message": "",
|
||||||
|
|||||||
@@ -243,6 +243,42 @@ func GetUser(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GenerateAccessToken(c *gin.Context) {
|
||||||
|
id := c.GetInt("id")
|
||||||
|
user, err := model.GetUserById(id, true)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": false,
|
||||||
|
"message": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user.AccessToken = common.GetUUID()
|
||||||
|
|
||||||
|
if model.DB.Where("token = ?", user.AccessToken).First(user).RowsAffected != 0 {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": false,
|
||||||
|
"message": "请重试,系统生成的 UUID 竟然重复了!",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := user.Update(false); err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": false,
|
||||||
|
"message": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": true,
|
||||||
|
"message": "",
|
||||||
|
"data": user.AccessToken,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func GetSelf(c *gin.Context) {
|
func GetSelf(c *gin.Context) {
|
||||||
id := c.GetInt("id")
|
id := c.GetInt("id")
|
||||||
user, err := model.GetUserById(id, false)
|
user, err := model.GetUserById(id, false)
|
||||||
|
|||||||
@@ -16,12 +16,31 @@ func authHelper(c *gin.Context, minRole int) {
|
|||||||
id := session.Get("id")
|
id := session.Get("id")
|
||||||
status := session.Get("status")
|
status := session.Get("status")
|
||||||
if username == nil {
|
if username == nil {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{
|
// Check access token
|
||||||
"success": false,
|
accessToken := c.Request.Header.Get("Authorization")
|
||||||
"message": "无权进行此操作,未登录",
|
if accessToken == "" {
|
||||||
})
|
c.JSON(http.StatusUnauthorized, gin.H{
|
||||||
c.Abort()
|
"success": false,
|
||||||
return
|
"message": "无权进行此操作,未登录且未提供 access token",
|
||||||
|
})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user := model.ValidateAccessToken(accessToken)
|
||||||
|
if user != nil && user.Username != "" {
|
||||||
|
// Token is valid
|
||||||
|
username = user.Username
|
||||||
|
role = user.Role
|
||||||
|
id = user.Id
|
||||||
|
status = user.Status
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": false,
|
||||||
|
"message": "无权进行此操作,access token 无效",
|
||||||
|
})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if status.(int) == common.UserStatusDisabled {
|
if status.(int) == common.UserStatusDisabled {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ func createRootAccountIfNeed() error {
|
|||||||
Role: common.RoleRootUser,
|
Role: common.RoleRootUser,
|
||||||
Status: common.UserStatusEnabled,
|
Status: common.UserStatusEnabled,
|
||||||
DisplayName: "Root User",
|
DisplayName: "Root User",
|
||||||
|
AccessToken: common.GetUUID(),
|
||||||
}
|
}
|
||||||
DB.Create(&rootUser)
|
DB.Create(&rootUser)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ func InitOptionMap() {
|
|||||||
common.OptionMap["WeChatAccountQRCodeImageURL"] = ""
|
common.OptionMap["WeChatAccountQRCodeImageURL"] = ""
|
||||||
common.OptionMap["TurnstileSiteKey"] = ""
|
common.OptionMap["TurnstileSiteKey"] = ""
|
||||||
common.OptionMap["TurnstileSecretKey"] = ""
|
common.OptionMap["TurnstileSecretKey"] = ""
|
||||||
|
common.OptionMap["QuotaForNewUser"] = strconv.Itoa(common.QuotaForNewUser)
|
||||||
common.OptionMapRWMutex.Unlock()
|
common.OptionMapRWMutex.Unlock()
|
||||||
options, _ := AllOption()
|
options, _ := AllOption()
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
@@ -131,5 +132,7 @@ func updateOptionMap(key string, value string) {
|
|||||||
common.TurnstileSiteKey = value
|
common.TurnstileSiteKey = value
|
||||||
case "TurnstileSecretKey":
|
case "TurnstileSecretKey":
|
||||||
common.TurnstileSecretKey = value
|
common.TurnstileSecretKey = value
|
||||||
|
case "QuotaForNewUser":
|
||||||
|
common.QuotaForNewUser, _ = strconv.Atoi(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
type Redemption struct {
|
type Redemption struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
UserId int `json:"user_id"`
|
UserId int `json:"user_id"`
|
||||||
Key string `json:"key" gorm:"uniqueIndex"`
|
Key string `json:"key" gorm:"type:char(32);uniqueIndex"`
|
||||||
Status int `json:"status" gorm:"default:1"`
|
Status int `json:"status" gorm:"default:1"`
|
||||||
Name string `json:"name" gorm:"index"`
|
Name string `json:"name" gorm:"index"`
|
||||||
Quota int `json:"quota" gorm:"default:100"`
|
Quota int `json:"quota" gorm:"default:100"`
|
||||||
@@ -48,7 +48,7 @@ func Redeem(key string, tokenId int) (quota int, err error) {
|
|||||||
return 0, errors.New("未提供 token id")
|
return 0, errors.New("未提供 token id")
|
||||||
}
|
}
|
||||||
redemption := &Redemption{}
|
redemption := &Redemption{}
|
||||||
err = DB.Where("key = ?", key).First(redemption).Error
|
err = DB.Where("`key` = ?", key).First(redemption).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.New("无效的兑换码")
|
return 0, errors.New("无效的兑换码")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
type Token struct {
|
type Token struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
UserId int `json:"user_id"`
|
UserId int `json:"user_id"`
|
||||||
Key string `json:"key" gorm:"uniqueIndex"`
|
Key string `json:"key" gorm:"type:char(32);uniqueIndex"`
|
||||||
Status int `json:"status" gorm:"default:1"`
|
Status int `json:"status" gorm:"default:1"`
|
||||||
Name string `json:"name" gorm:"index" `
|
Name string `json:"name" gorm:"index" `
|
||||||
CreatedTime int64 `json:"created_time" gorm:"bigint"`
|
CreatedTime int64 `json:"created_time" gorm:"bigint"`
|
||||||
@@ -39,7 +39,7 @@ func ValidateUserToken(key string) (token *Token, err error) {
|
|||||||
}
|
}
|
||||||
key = strings.Replace(key, "Bearer ", "", 1)
|
key = strings.Replace(key, "Bearer ", "", 1)
|
||||||
token = &Token{}
|
token = &Token{}
|
||||||
err = DB.Where("key = ?", key).First(token).Error
|
err = DB.Where("`key` = ?", key).First(token).Error
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if token.Status != common.TokenStatusEnabled {
|
if token.Status != common.TokenStatusEnabled {
|
||||||
return nil, errors.New("该 token 状态不可用")
|
return nil, errors.New("该 token 状态不可用")
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"gorm.io/gorm"
|
||||||
"one-api/common"
|
"one-api/common"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// User if you add sensitive fields, don't forget to clean them in setupLogin function.
|
// User if you add sensitive fields, don't forget to clean them in setupLogin function.
|
||||||
@@ -19,6 +21,8 @@ type User struct {
|
|||||||
WeChatId string `json:"wechat_id" gorm:"column:wechat_id;index"`
|
WeChatId string `json:"wechat_id" gorm:"column:wechat_id;index"`
|
||||||
VerificationCode string `json:"verification_code" gorm:"-:all"` // this field is only for Email verification, don't save it to database!
|
VerificationCode string `json:"verification_code" gorm:"-:all"` // this field is only for Email verification, don't save it to database!
|
||||||
Balance int `json:"balance" gorm:"type:int;default:0"`
|
Balance int `json:"balance" gorm:"type:int;default:0"`
|
||||||
|
AccessToken string `json:"access_token" gorm:"type:char(32);column:access_token;uniqueIndex"` // this token is for system management
|
||||||
|
Quota int `json:"quota" gorm:"type:int;default:0"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMaxUserId() int {
|
func GetMaxUserId() int {
|
||||||
@@ -67,6 +71,8 @@ func (user *User) Insert() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
user.Quota = common.QuotaForNewUser
|
||||||
|
user.AccessToken = common.GetUUID()
|
||||||
err = DB.Create(user).Error
|
err = DB.Create(user).Error
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -188,3 +194,25 @@ func IsAdmin(userId int) bool {
|
|||||||
}
|
}
|
||||||
return user.Role >= common.RoleAdminUser
|
return user.Role >= common.RoleAdminUser
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ValidateAccessToken(token string) (user *User) {
|
||||||
|
if token == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
token = strings.Replace(token, "Bearer ", "", 1)
|
||||||
|
user = &User{}
|
||||||
|
if DB.Where("access_token = ?", token).First(user).RowsAffected == 1 {
|
||||||
|
return user
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetUserQuota(id int) (quota int, err error) {
|
||||||
|
err = DB.Model(&User{}).Where("id = ?", id).Select("quota").Find("a).Error
|
||||||
|
return quota, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func DecreaseUserQuota(id int, quota int) (err error) {
|
||||||
|
err = DB.Model(&User{}).Where("id = ?", id).Update("quota", gorm.Expr("quota - ?", quota)).Error
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ func SetApiRouter(router *gin.Engine) {
|
|||||||
selfRoute.GET("/self", controller.GetSelf)
|
selfRoute.GET("/self", controller.GetSelf)
|
||||||
selfRoute.PUT("/self", controller.UpdateSelf)
|
selfRoute.PUT("/self", controller.UpdateSelf)
|
||||||
selfRoute.DELETE("/self", controller.DeleteSelf)
|
selfRoute.DELETE("/self", controller.DeleteSelf)
|
||||||
|
selfRoute.GET("/token", controller.GenerateAccessToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
adminRoute := userRoute.Group("/")
|
adminRoute := userRoute.Group("/")
|
||||||
|
|||||||
@@ -8,12 +8,12 @@ import (
|
|||||||
|
|
||||||
func SetRelayRouter(router *gin.Engine) {
|
func SetRelayRouter(router *gin.Engine) {
|
||||||
relayV1Router := router.Group("/v1")
|
relayV1Router := router.Group("/v1")
|
||||||
relayV1Router.Use(middleware.GlobalAPIRateLimit(), middleware.TokenAuth(), middleware.Distribute())
|
relayV1Router.Use(middleware.TokenAuth(), middleware.Distribute())
|
||||||
{
|
{
|
||||||
relayV1Router.Any("/*path", controller.Relay)
|
relayV1Router.Any("/*path", controller.Relay)
|
||||||
}
|
}
|
||||||
relayDashboardRouter := router.Group("/dashboard")
|
relayDashboardRouter := router.Group("/dashboard")
|
||||||
relayDashboardRouter.Use(middleware.GlobalAPIRateLimit(), middleware.TokenAuth(), middleware.Distribute())
|
relayDashboardRouter.Use(middleware.TokenAuth(), middleware.Distribute())
|
||||||
{
|
{
|
||||||
relayDashboardRouter.Any("/*path", controller.Relay)
|
relayDashboardRouter.Any("/*path", controller.Relay)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import { Button, Divider, Form, Header, Image, Modal } from 'semantic-ui-react';
|
import { Button, Divider, Form, Header, Image, Message, Modal } from 'semantic-ui-react';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
import { API, copy, showError, showInfo, showSuccess } from '../helpers';
|
import { API, copy, showError, showInfo, showSuccess } from '../helpers';
|
||||||
import Turnstile from 'react-turnstile';
|
import Turnstile from 'react-turnstile';
|
||||||
@@ -34,6 +34,17 @@ const PersonalSetting = () => {
|
|||||||
setInputs((inputs) => ({ ...inputs, [name]: value }));
|
setInputs((inputs) => ({ ...inputs, [name]: value }));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const generateAccessToken = async () => {
|
||||||
|
const res = await API.get('/api/user/token');
|
||||||
|
const { success, message, data } = res.data;
|
||||||
|
if (success) {
|
||||||
|
await copy(data);
|
||||||
|
showSuccess(`令牌已重置并已复制到剪贴板:${data}`);
|
||||||
|
} else {
|
||||||
|
showError(message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const bindWeChat = async () => {
|
const bindWeChat = async () => {
|
||||||
if (inputs.wechat_verification_code === '') return;
|
if (inputs.wechat_verification_code === '') return;
|
||||||
const res = await API.get(
|
const res = await API.get(
|
||||||
@@ -92,9 +103,13 @@ const PersonalSetting = () => {
|
|||||||
return (
|
return (
|
||||||
<div style={{ lineHeight: '40px' }}>
|
<div style={{ lineHeight: '40px' }}>
|
||||||
<Header as='h3'>通用设置</Header>
|
<Header as='h3'>通用设置</Header>
|
||||||
|
<Message>
|
||||||
|
注意,此处生成的令牌用于系统管理,而非用于请求 OpenAI 相关的服务,请知悉。
|
||||||
|
</Message>
|
||||||
<Button as={Link} to={`/user/edit/`}>
|
<Button as={Link} to={`/user/edit/`}>
|
||||||
更新个人信息
|
更新个人信息
|
||||||
</Button>
|
</Button>
|
||||||
|
<Button onClick={generateAccessToken}>生成系统访问令牌</Button>
|
||||||
<Divider />
|
<Divider />
|
||||||
<Header as='h3'>账号绑定</Header>
|
<Header as='h3'>账号绑定</Header>
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ const SystemSetting = () => {
|
|||||||
TurnstileSiteKey: '',
|
TurnstileSiteKey: '',
|
||||||
TurnstileSecretKey: '',
|
TurnstileSecretKey: '',
|
||||||
RegisterEnabled: '',
|
RegisterEnabled: '',
|
||||||
|
QuotaForNewUser: 0,
|
||||||
});
|
});
|
||||||
let originInputs = {};
|
let originInputs = {};
|
||||||
let [loading, setLoading] = useState(false);
|
let [loading, setLoading] = useState(false);
|
||||||
@@ -86,7 +87,8 @@ const SystemSetting = () => {
|
|||||||
name === 'WeChatServerToken' ||
|
name === 'WeChatServerToken' ||
|
||||||
name === 'WeChatAccountQRCodeImageURL' ||
|
name === 'WeChatAccountQRCodeImageURL' ||
|
||||||
name === 'TurnstileSiteKey' ||
|
name === 'TurnstileSiteKey' ||
|
||||||
name === 'TurnstileSecretKey'
|
name === 'TurnstileSecretKey' ||
|
||||||
|
name === 'QuotaForNewUser'
|
||||||
) {
|
) {
|
||||||
setInputs((inputs) => ({ ...inputs, [name]: value }));
|
setInputs((inputs) => ({ ...inputs, [name]: value }));
|
||||||
} else {
|
} else {
|
||||||
@@ -228,6 +230,25 @@ const SystemSetting = () => {
|
|||||||
/>
|
/>
|
||||||
</Form.Group>
|
</Form.Group>
|
||||||
<Divider />
|
<Divider />
|
||||||
|
<Header as='h3'>
|
||||||
|
运营设置
|
||||||
|
</Header>
|
||||||
|
<Form.Group widths={3}>
|
||||||
|
<Form.Input
|
||||||
|
label='新用户初始配额'
|
||||||
|
name='QuotaForNewUser'
|
||||||
|
onChange={handleInputChange}
|
||||||
|
autoComplete='off'
|
||||||
|
value={inputs.QuotaForNewUser}
|
||||||
|
type='number'
|
||||||
|
min='0'
|
||||||
|
placeholder='例如:100'
|
||||||
|
/>
|
||||||
|
</Form.Group>
|
||||||
|
<Form.Button onClick={()=>{
|
||||||
|
updateOption('QuotaForNewUser', inputs.QuotaForNewUser).then();
|
||||||
|
}}>保存运营设置</Form.Button>
|
||||||
|
<Divider />
|
||||||
<Header as='h3'>
|
<Header as='h3'>
|
||||||
配置 SMTP
|
配置 SMTP
|
||||||
<Header.Subheader>用以支持系统的邮件发送</Header.Subheader>
|
<Header.Subheader>用以支持系统的邮件发送</Header.Subheader>
|
||||||
|
|||||||
Reference in New Issue
Block a user