Compare commits

...

7 Commits

Author SHA1 Message Date
JustSong
46dfc6dcdd feat: relay all kinds of requests 2023-04-25 10:57:37 +08:00
JustSong
0198df5962 fix: only keep header Authorization & Content-Type 2023-04-25 10:47:25 +08:00
JustSong
4c0dc50af0 fix: fix http2: invalid Connection request header: ["upgrade"] 2023-04-25 10:19:00 +08:00
JustSong
423978baf4 fix: copy token to search input if clipboard.writeText is not available (close #6) 2023-04-25 09:46:58 +08:00
JustSong
5ed4a3d405 feat: improve the token edit page 2023-04-25 09:24:02 +08:00
Shiqi Mei
336c03a125 docs: add missing manual deployment documentation (#7)
* docs: add missing manual deployment documentation

* docs: fix manual deployment steps

* docs: update readme

---------

Co-authored-by: JustSong <songquanpeng@foxmail.com>
2023-04-24 20:55:22 +08:00
JustSong
918ba60802 feat: able to set the token's expiration time and number of uses 2023-04-24 20:52:40 +08:00
12 changed files with 206 additions and 101 deletions

View File

@@ -52,12 +52,13 @@ _✨ All in one 的 OpenAI 接口,整合各种 API 访问方式,开箱即用
+ [x] 自定义渠道
2. 支持通过负载均衡的方式访问多个渠道。
3. 支持单个访问渠道设置多个 API Key利用起来你的多个 API Key。
4. 支持 HTTP SSE
5. 多种用户登录注册方式:
4. 支持设置令牌的过期时间和使用次数
5. 支持 HTTP SSE。
6. 多种用户登录注册方式:
+ 邮箱登录注册以及通过邮箱进行密码重置。
+ [GitHub 开放授权](https://github.com/settings/applications/new)。
+ 微信公众号授权(需要额外部署 [WeChat Server](https://github.com/songquanpeng/wechat-server))。
6. 支持用户管理。
7. 支持用户管理。
## 部署
### 基于 Docker 进行部署
@@ -69,6 +70,14 @@ _✨ All in one 的 OpenAI 接口,整合各种 API 访问方式,开箱即用
1. 从 [GitHub Releases](https://github.com/songquanpeng/one-api/releases/latest) 下载可执行文件或者从源码编译:
```shell
git clone https://github.com/songquanpeng/one-api.git
# 构建前端
cd one-api/web
npm install
npm run build
# 构建后端
cd ..
go mod download
go build -ldflags "-s -w" -o one-api
````

View File

@@ -87,8 +87,10 @@ const (
)
const (
TokenStatusEnabled = 1 // don't use 0, 0 is the default value!
TokenStatusDisabled = 2 // also don't use 0
TokenStatusEnabled = 1 // don't use 0, 0 is the default value!
TokenStatusDisabled = 2 // also don't use 0
TokenStatusExpired = 3
TokenStatusExhausted = 4
)
const (

View File

@@ -24,10 +24,12 @@ func Relay(c *gin.Context) {
})
return
}
req.Header = c.Request.Header.Clone()
//req.Header = c.Request.Header.Clone()
// Fix HTTP Decompression failed
// https://github.com/stoplightio/prism/issues/1064#issuecomment-824682360
req.Header.Del("Accept-Encoding")
//req.Header.Del("Accept-Encoding")
req.Header.Set("Authorization", c.Request.Header.Get("Authorization"))
req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type"))
client := &http.Client{}
resp, err := client.Do(req)

View File

@@ -98,6 +98,8 @@ func AddToken(c *gin.Context) {
Key: common.GetUUID(),
CreatedTime: common.GetTimestamp(),
AccessedTime: common.GetTimestamp(),
ExpiredTime: token.ExpiredTime,
RemainTimes: token.RemainTimes,
}
err = cleanToken.Insert()
if err != nil {
@@ -151,8 +153,27 @@ func UpdateToken(c *gin.Context) {
})
return
}
if token.Status == common.TokenStatusEnabled {
if cleanToken.Status == common.TokenStatusExpired && cleanToken.ExpiredTime <= common.GetTimestamp() {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "令牌已过期,无法启用,请先修改令牌过期时间",
})
return
}
if cleanToken.Status == common.TokenStatusExhausted && cleanToken.RemainTimes == 0 {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "令牌可用次数已用尽,无法启用,请先修改令牌剩余次数",
})
return
}
}
cleanToken.Name = token.Name
cleanToken.Status = token.Status
cleanToken.ExpiredTime = token.ExpiredTime
cleanToken.RemainTimes = token.RemainTimes
err = cleanToken.Update()
if err != nil {
c.JSON(http.StatusOK, gin.H{

View File

@@ -15,6 +15,8 @@ type Token struct {
Name string `json:"name" gorm:"index" `
CreatedTime int64 `json:"created_time" gorm:"bigint"`
AccessedTime int64 `json:"accessed_time" gorm:"bigint"`
ExpiredTime int64 `json:"expired_time" gorm:"bigint;default:-1"` // -1 means never expired
RemainTimes int `json:"remain_times" gorm:"default:-1"` // -1 means infinite times
}
func GetAllUserTokens(userId int, startIdx int, num int) ([]*Token, error) {
@@ -38,13 +40,27 @@ func ValidateUserToken(key string) (token *Token, err error) {
err = DB.Where("key = ?", key).First(token).Error
if err == nil {
if token.Status != common.TokenStatusEnabled {
return nil, errors.New("该 token 已被禁用")
return nil, errors.New("该 token 状态不可用")
}
if token.ExpiredTime != -1 && token.ExpiredTime < common.GetTimestamp() {
token.Status = common.TokenStatusExpired
err := token.SelectUpdate()
if err != nil {
common.SysError("更新 token 状态失败:" + err.Error())
}
return nil, errors.New("该 token 已过期")
}
go func() {
token.AccessedTime = common.GetTimestamp()
err := token.Update()
if token.RemainTimes > 0 {
token.RemainTimes--
if token.RemainTimes == 0 {
token.Status = common.TokenStatusExhausted
}
}
err := token.SelectUpdate()
if err != nil {
common.SysError("更新 token 访问时间失败:" + err.Error())
common.SysError("更新 token 失败:" + err.Error())
}
}()
return token, nil
@@ -74,6 +90,11 @@ func (token *Token) Update() error {
return err
}
func (token *Token) SelectUpdate() error {
// This can update zero values
return DB.Model(token).Select("accessed_time", "remain_times", "status").Updates(token).Error
}
func (token *Token) Delete() error {
var err error
err = DB.Delete(token).Error

View File

@@ -10,6 +10,6 @@ func SetRelayRouter(router *gin.Engine) {
relayRouter := router.Group("/v1")
relayRouter.Use(middleware.GlobalAPIRateLimit(), middleware.TokenAuth(), middleware.Distribute())
{
relayRouter.POST("/chat/completions", controller.Relay)
relayRouter.Any("/*path", controller.Relay)
}
}

View File

@@ -18,7 +18,6 @@ import { StatusContext } from './context/Status';
import Channel from './pages/Channel';
import Token from './pages/Token';
import EditToken from './pages/Token/EditToken';
import AddToken from './pages/Token/AddToken';
import EditChannel from './pages/Channel/EditChannel';
import AddChannel from './pages/Channel/AddChannel';
@@ -116,7 +115,7 @@ function App() {
path='/token/add'
element={
<Suspense fallback={<Loading></Loading>}>
<AddToken />
<EditToken />
</Suspense>
}
/>

View File

@@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react';
import { Button, Form, Label, Pagination, Table } from 'semantic-ui-react';
import { Button, Form, Label, Message, Pagination, Table } from 'semantic-ui-react';
import { Link } from 'react-router-dom';
import { API, copy, showError, showSuccess, timestamp2string } from '../helpers';
import { API, copy, showError, showInfo, showSuccess, showWarning, timestamp2string } from '../helpers';
import { ITEMS_PER_PAGE } from '../constants';
@@ -13,6 +13,21 @@ function renderTimestamp(timestamp) {
);
}
function renderStatus(status) {
switch (status) {
case 1:
return <Label basic color='green'>已启用</Label>;
case 2:
return <Label basic color='red'> 已禁用 </Label>;
case 3:
return <Label basic color='yellow'> 已过期 </Label>;
case 4:
return <Label basic color='grey'> 已耗尽 </Label>;
default:
return <Label basic color='black'> 未知状态 </Label>;
}
}
const TokensTable = () => {
const [tokens, setTokens] = useState([]);
const [loading, setLoading] = useState(true);
@@ -88,25 +103,6 @@ const TokensTable = () => {
}
};
const renderStatus = (status) => {
switch (status) {
case 1:
return <Label basic color='green'>已启用</Label>;
case 2:
return (
<Label basic color='red'>
已禁用
</Label>
);
default:
return (
<Label basic color='grey'>
未知状态
</Label>
);
}
};
const searchTokens = async () => {
if (searchKeyword === '') {
// if keyword is blank, load files instead.
@@ -185,6 +181,14 @@ const TokensTable = () => {
>
状态
</Table.HeaderCell>
<Table.HeaderCell
style={{ cursor: 'pointer' }}
onClick={() => {
sortToken('remain_times');
}}
>
剩余次数
</Table.HeaderCell>
<Table.HeaderCell
style={{ cursor: 'pointer' }}
onClick={() => {
@@ -201,6 +205,14 @@ const TokensTable = () => {
>
访问时间
</Table.HeaderCell>
<Table.HeaderCell
style={{ cursor: 'pointer' }}
onClick={() => {
sortToken('expired_time');
}}
>
过期时间
</Table.HeaderCell>
<Table.HeaderCell>操作</Table.HeaderCell>
</Table.Row>
</Table.Header>
@@ -218,8 +230,10 @@ const TokensTable = () => {
<Table.Cell>{token.id}</Table.Cell>
<Table.Cell>{token.name ? token.name : '无'}</Table.Cell>
<Table.Cell>{renderStatus(token.status)}</Table.Cell>
<Table.Cell>{token.remain_times === -1 ? "无限制" : token.remain_times}</Table.Cell>
<Table.Cell>{renderTimestamp(token.created_time)}</Table.Cell>
<Table.Cell>{renderTimestamp(token.accessed_time)}</Table.Cell>
<Table.Cell>{token.expired_time === -1 ? "永不过期" : renderTimestamp(token.expired_time)}</Table.Cell>
<Table.Cell>
<div>
<Button
@@ -229,7 +243,8 @@ const TokensTable = () => {
if (await copy(token.key)) {
showSuccess('已复制到剪贴板!');
} else {
showError('复制失败!');
showWarning('无法复制到剪贴板,请手动复制,已将令牌填入搜索框。')
setSearchKeyword(token.key);
}
}}
>
@@ -272,7 +287,7 @@ const TokensTable = () => {
<Table.Footer>
<Table.Row>
<Table.HeaderCell colSpan='6'>
<Table.HeaderCell colSpan='8'>
<Button size='small' as={Link} to='/token/add' loading={loading}>
添加新的令牌
</Button>

View File

@@ -2,5 +2,6 @@ export const toastConstants = {
SUCCESS_TIMEOUT: 500,
INFO_TIMEOUT: 3000,
ERROR_TIMEOUT: 5000,
WARNING_TIMEOUT: 10000,
NOTICE_TIMEOUT: 20000
};

View File

@@ -31,6 +31,7 @@ export function isMobile() {
}
let showErrorOptions = { autoClose: toastConstants.ERROR_TIMEOUT };
let showWarningOptions = { autoClose: toastConstants.WARNING_TIMEOUT };
let showSuccessOptions = { autoClose: toastConstants.SUCCESS_TIMEOUT };
let showInfoOptions = { autoClose: toastConstants.INFO_TIMEOUT };
let showNoticeOptions = { autoClose: false };
@@ -74,6 +75,10 @@ export function showError(error) {
}
}
export function showWarning(message) {
toast.warn(message, showWarningOptions);
}
export function showSuccess(message) {
toast.success(message, showSuccessOptions);
}

View File

@@ -1,53 +0,0 @@
import React, { useState } from 'react';
import { Button, Form, Header, Segment } from 'semantic-ui-react';
import { API, showError, showSuccess } from '../../helpers';
const AddToken = () => {
const originInputs = {
name: '',
};
const [inputs, setInputs] = useState(originInputs);
const { name, display_name, password } = inputs;
const handleInputChange = (e, { name, value }) => {
setInputs((inputs) => ({ ...inputs, [name]: value }));
};
const submit = async () => {
if (inputs.name === '') return;
const res = await API.post(`/api/token/`, inputs);
const { success, message } = res.data;
if (success) {
showSuccess('令牌创建成功!');
setInputs(originInputs);
} else {
showError(message);
}
};
return (
<>
<Segment>
<Header as="h3">创建新的令牌</Header>
<Form autoComplete="off">
<Form.Field>
<Form.Input
label="名称"
name="name"
placeholder={'请输入名称'}
onChange={handleInputChange}
value={name}
autoComplete="off"
required
/>
</Form.Field>
<Button type={'submit'} onClick={submit}>
提交
</Button>
</Form>
</Segment>
</>
);
};
export default AddToken;

View File

@@ -1,25 +1,47 @@
import React, { useEffect, useState } from 'react';
import { Button, Form, Header, Segment } from 'semantic-ui-react';
import { useParams } from 'react-router-dom';
import { API, showError, showSuccess } from '../../helpers';
import { API, showError, showSuccess, timestamp2string } from '../../helpers';
const EditToken = () => {
const params = useParams();
const tokenId = params.id;
const [loading, setLoading] = useState(true);
const [inputs, setInputs] = useState({
name: ''
});
const { name } = inputs;
const isEdit = tokenId !== undefined;
const [loading, setLoading] = useState(isEdit);
const originInputs = {
name: '',
remain_times: -1,
expired_time: -1
};
const [inputs, setInputs] = useState(originInputs);
const { name, remain_times, expired_time } = inputs;
const handleInputChange = (e, { name, value }) => {
setInputs((inputs) => ({ ...inputs, [name]: value }));
};
const setExpiredTime = (month, day, hour, minute) => {
let now = new Date();
let timestamp = now.getTime() / 1000;
let seconds = month * 30 * 24 * 60 * 60;
seconds += day * 24 * 60 * 60;
seconds += hour * 60 * 60;
seconds += minute * 60;
if (seconds !== 0) {
timestamp += seconds;
setInputs({ ...inputs, expired_time: timestamp2string(timestamp) });
} else {
setInputs({ ...inputs, expired_time: -1 });
}
};
const loadToken = async () => {
let res = await API.get(`/api/token/${tokenId}`);
const { success, message, data } = res.data;
if (success) {
data.password = '';
if (data.expired_time !== -1) {
data.expired_time = timestamp2string(data.expired_time);
}
setInputs(data);
} else {
showError(message);
@@ -27,14 +49,37 @@ const EditToken = () => {
setLoading(false);
};
useEffect(() => {
loadToken().then();
if (isEdit) {
loadToken().then();
}
}, []);
const submit = async () => {
let res = await API.put(`/api/token/`, { ...inputs, id: parseInt(tokenId) });
if (!isEdit && inputs.name === '') return;
let localInputs = inputs;
localInputs.remain_times = parseInt(localInputs.remain_times);
if (localInputs.expired_time !== -1) {
let time = Date.parse(localInputs.expired_time);
if (isNaN(time)) {
showError('过期时间格式错误!');
return;
}
localInputs.expired_time = Math.ceil(time / 1000);
}
let res;
if (isEdit) {
res = await API.put(`/api/token/`, { ...localInputs, id: parseInt(tokenId) });
} else {
res = await API.post(`/api/token/`, localInputs);
}
const { success, message } = res.data;
if (success) {
showSuccess('令牌更新成功!');
if (isEdit) {
showSuccess('令牌更新成功!');
} else {
showSuccess('令牌创建成功!');
setInputs(originInputs);
}
} else {
showError(message);
}
@@ -43,18 +88,56 @@ const EditToken = () => {
return (
<>
<Segment loading={loading}>
<Header as='h3'>更新令牌信息</Header>
<Header as='h3'>{isEdit ? "更新令牌信息" : "创建新的令牌"}</Header>
<Form autoComplete='off'>
<Form.Field>
<Form.Input
label='名称'
name='name'
placeholder={'请输入新的名称'}
placeholder={'请输入名称'}
onChange={handleInputChange}
value={name}
autoComplete='off'
required={!isEdit}
/>
</Form.Field>
<Form.Field>
<Form.Input
label='剩余次数'
name='remain_times'
placeholder={'请输入剩余次数,-1 表示无限制'}
onChange={handleInputChange}
value={remain_times}
autoComplete='off'
type='number'
/>
</Form.Field>
<Form.Field>
<Form.Input
label='过期时间'
name='expired_time'
placeholder={'请输入过期时间,格式为 yyyy-MM-dd HH:mm:ss-1 表示无限制'}
onChange={handleInputChange}
value={expired_time}
autoComplete='off'
type='datetime-local'
/>
</Form.Field>
<Button type={'button'} onClick={() => {
setExpiredTime(0, 0, 0, 0);
}}>永不过期</Button>
<Button type={'button'} onClick={() => {
setExpiredTime(1, 0, 0, 0);
}}>一个月后过期</Button>
<Button type={'button'} onClick={() => {
setExpiredTime(0, 1, 0, 0);
}}>一天后过期</Button>
<Button type={'button'} onClick={() => {
setExpiredTime(0, 0, 1, 0);
}}>一小时后过期</Button>
<Button type={'button'} onClick={() => {
setExpiredTime(0, 0, 0, 1);
}}>一分钟后过期</Button>
<Button onClick={submit}>提交</Button>
</Form>
</Segment>