mirror of
https://gitee.com/chuangxxt/share-copilot
synced 2026-12-02 06:47:54 +00:00
Compare commits
25
Commits
v1.0.4
..
89c6e2ffc1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
89c6e2ffc1 | ||
|
|
0b943666ad | ||
|
|
4796546eb0 | ||
|
|
e9f80439eb | ||
|
|
f12fdbc533 | ||
|
|
9f3dd9c2fd | ||
|
|
ee709ef643 | ||
|
|
a44264ee1c | ||
|
|
2133489752 | ||
|
|
04b1f4965c | ||
|
|
45e48642c8 | ||
|
|
bb9c824b05 | ||
|
|
76b9c3c0eb | ||
|
|
de84f79183 | ||
|
|
b63776ea8b | ||
|
|
24f285d8fe | ||
|
|
bd5143b8bb | ||
|
|
7801c29fdb | ||
|
|
015c27c65e | ||
|
|
ee7d0ae9ac | ||
|
|
0bd5c308b1 | ||
|
|
f76260ed6f | ||
|
|
b8c51714ba | ||
|
|
4260a9e0fd | ||
|
|
d905ce09c0 |
@@ -1,127 +0,0 @@
|
||||
木兰宽松许可证, 第2版
|
||||
|
||||
木兰宽松许可证, 第2版
|
||||
2020年1月 http://license.coscl.org.cn/MulanPSL2
|
||||
|
||||
|
||||
您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第2版(“本许可证”)的如下条款的约束:
|
||||
|
||||
0. 定义
|
||||
|
||||
“软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。
|
||||
|
||||
“贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。
|
||||
|
||||
“贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。
|
||||
|
||||
“法人实体”是指提交贡献的机构及其“关联实体”。
|
||||
|
||||
“关联实体”是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。
|
||||
|
||||
1. 授予版权许可
|
||||
|
||||
每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。
|
||||
|
||||
2. 授予专利许可
|
||||
|
||||
每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。
|
||||
|
||||
3. 无商标许可
|
||||
|
||||
“本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。
|
||||
|
||||
4. 分发限制
|
||||
|
||||
您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。
|
||||
|
||||
5. 免责声明与责任限制
|
||||
|
||||
“软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。
|
||||
|
||||
6. 语言
|
||||
“本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文版为准。
|
||||
|
||||
条款结束
|
||||
|
||||
如何将木兰宽松许可证,第2版,应用到您的软件
|
||||
|
||||
如果您希望将木兰宽松许可证,第2版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步:
|
||||
|
||||
1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字;
|
||||
|
||||
2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中;
|
||||
|
||||
3, 请将如下声明文本放入每个源文件的头部注释中。
|
||||
|
||||
Copyright (c) [Year] [name of copyright holder]
|
||||
[Software Name] is licensed under Mulan PSL v2.
|
||||
You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
You may obtain a copy of Mulan PSL v2 at:
|
||||
http://license.coscl.org.cn/MulanPSL2
|
||||
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
See the Mulan PSL v2 for more details.
|
||||
|
||||
|
||||
Mulan Permissive Software License,Version 2
|
||||
|
||||
Mulan Permissive Software License,Version 2 (Mulan PSL v2)
|
||||
January 2020 http://license.coscl.org.cn/MulanPSL2
|
||||
|
||||
Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions:
|
||||
|
||||
0. Definition
|
||||
|
||||
Software means the program and related documents which are licensed under this License and comprise all Contribution(s).
|
||||
|
||||
Contribution means the copyrightable work licensed by a particular Contributor under this License.
|
||||
|
||||
Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License.
|
||||
|
||||
Legal Entity means the entity making a Contribution and all its Affiliates.
|
||||
|
||||
Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity.
|
||||
|
||||
1. Grant of Copyright License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not.
|
||||
|
||||
2. Grant of Patent License
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken.
|
||||
|
||||
3. No Trademark License
|
||||
|
||||
No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4.
|
||||
|
||||
4. Distribution Restriction
|
||||
|
||||
You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software.
|
||||
|
||||
5. Disclaimer of Warranty and Limitation of Liability
|
||||
|
||||
THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW IT’S CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
6. Language
|
||||
|
||||
THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL.
|
||||
|
||||
END OF THE TERMS AND CONDITIONS
|
||||
|
||||
How to Apply the Mulan Permissive Software License,Version 2 (Mulan PSL v2) to Your Software
|
||||
|
||||
To apply the Mulan PSL v2 to your work, for easy identification by recipients, you are suggested to complete following three steps:
|
||||
|
||||
i Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner;
|
||||
|
||||
ii Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package;
|
||||
|
||||
iii Attach the statement to the appropriate annotated syntax at the beginning of each source file.
|
||||
|
||||
|
||||
Copyright (c) [Year] [name of copyright holder]
|
||||
[Software Name] is licensed under Mulan PSL v2.
|
||||
You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
You may obtain a copy of Mulan PSL v2 at:
|
||||
http://license.coscl.org.cn/MulanPSL2
|
||||
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
See the Mulan PSL v2 for more details.
|
||||
-183
@@ -1,183 +0,0 @@
|
||||
The beta version now supports full interface proxy,
|
||||
|
||||
So even code suggestions go through the proxy,
|
||||
|
||||
Still in testing.
|
||||
|
||||
|
||||
|
||||

|
||||
***
|
||||
# share-copilot
|
||||
|
||||
- Acts as a proxy server, forwarding requests to the Copilot plugin's API
|
||||
|
||||
- Supports both vscode and Jetbrains plugins
|
||||
|
||||
- Supports multiple users sharing a single token
|
||||
|
||||
- Optimize request logic to reduce suspended probability、
|
||||
|
||||
|
||||
|
||||

|
||||
|
||||
***
|
||||
# 一、Build It Yourself:
|
||||
```sh
|
||||
Test Environment: Linux4.18.0-305.3.1.el8.x86_64 GNU/Linux
|
||||
Requires Go environment;
|
||||
resolve any issues chatgpt.
|
||||
```
|
||||
```sh
|
||||
git clone https://gitlab.com/luyoyu/share-copilot.git
|
||||
wget https://dl.google.com/go/go1.21.1.linux-amd64.tar.gz
|
||||
tar -C /usr/local -xzf go1.21.1.linux-amd64.tar.gz
|
||||
|
||||
vim ~/.bashrc
|
||||
# Add the following environment variables
|
||||
export PATH=$PATH:/usr/local/go/bin
|
||||
export GOPATH=$HOME/go
|
||||
export PATH=$PATH:$GOPATH/bin
|
||||
|
||||
# Make the environment variables effective
|
||||
source ~/.bashrc
|
||||
# Verify the installation
|
||||
go version
|
||||
# CD to the source directory of main.go
|
||||
cd /share-copilot/source
|
||||
go build
|
||||
```
|
||||
***
|
||||
# 二、Use the Ready-Made Version:
|
||||
|
||||
### 1.Installation
|
||||
|
||||
```sh
|
||||
wget https://gitlab.com/luyoyu/share-copilot/uploads/73bca0887b3ea39622e88cb88d7d0297/share-copilot-linux-amd64.zip
|
||||
```
|
||||
```sh
|
||||
unzip share-copilot-linux-amd64.zip
|
||||
```
|
||||
|
||||
```sh
|
||||
cd share-copilot
|
||||
```
|
||||
|
||||
```sh
|
||||
bash install.sh
|
||||
```
|
||||
|
||||
### 2.Configuration
|
||||
|
||||
```sh
|
||||
vim config.json
|
||||
```
|
||||
|
||||
##### 2.1 Explanation of config.json
|
||||
|
||||
```js
|
||||
domain // Listening domain or IP (can be reverse proxied with nginx)
|
||||
host // IP
|
||||
port // Port 80 or 443
|
||||
certPath // Public key path - required for 443
|
||||
keyPath // Private key path - required for 443
|
||||
github_api_url // Default, usually not changed
|
||||
token // Your Copilot token(s), you can put multiple, they will be used randomly in requests
|
||||
// After logging into the Copilot plugin, find them in %userprofile%\AppData\Local\github-copilot\host.json (starting with "ghu_")
|
||||
verification // Custom verification
|
||||
|
||||
```
|
||||
|
||||
##### 2.2 Example
|
||||
|
||||
```json
|
||||
{
|
||||
"server": {
|
||||
"domain": "example.com",
|
||||
"host": "0.0.0.0",
|
||||
"port": 443,
|
||||
"certPath":"./example.pem",
|
||||
"keyPath":"./example.pem"
|
||||
},
|
||||
"copilot_config":{
|
||||
"github_api_url": "https://api.github.com/copilot_internal/v2/token",
|
||||
"token":[
|
||||
"yours_token_1",
|
||||
"yours_token_2",
|
||||
"yours_token_3"
|
||||
]},
|
||||
"verification":""
|
||||
}
|
||||
|
||||
```
|
||||
The format must be correct;
|
||||
|
||||
### 3.Run
|
||||
|
||||
```sh
|
||||
scop r # Run [Ctrl+c to exit] chmod +x share-copilot if Permission denied
|
||||
scop rb # Run in the background
|
||||
scop st # Stop
|
||||
scop v # View status
|
||||
```
|
||||
|
||||
### 4.Complete Example:
|
||||
|
||||
##### 4.1 Server-side Configuration (Modify config.json)
|
||||
|
||||
```json
|
||||
{
|
||||
"server": {
|
||||
"domain": "api.example.com",
|
||||
"host": "0.0.0.0",
|
||||
"port": 443,
|
||||
"certPath":"./example.pem",
|
||||
"keyPath":"./example.pem"
|
||||
},
|
||||
"copilot_config":{
|
||||
"github_api_url": "https://api.github.com/copilot_internal/v2/token",
|
||||
"token":[
|
||||
"ghu_xMNAYLcJAPqAfiGoobrWffkJoNcGMVJtETKA",
|
||||
"ghu_GZgKFwraHorAxXXUvsUclOhxiYERPsSJeNuF",
|
||||
"ghu_SPUTCLvkMKoeMstPJmhSlYsYvCojhkFjGubl"
|
||||
]},
|
||||
"verification":"i_am_free"
|
||||
}
|
||||
```
|
||||
|
||||
------
|
||||
|
||||
##### 4.3Local Configuration Changes:
|
||||
|
||||
**Using Windows as an example**
|
||||
|
||||
- **For Jetbrains Plugin: Modify %userprofile%\AppData\Local\github-copilot\host.json**
|
||||
|
||||
```json
|
||||
{
|
||||
"github.com":{
|
||||
"user":"suibian",//Fill in as desired
|
||||
"oauth_token":"i_am_free",// Corresponds to the "verification" value above
|
||||
"dev_override":{
|
||||
"copilot_token_url":"https://api.example.com/copilot_internal/v2/token"
|
||||
// Your address
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
- **For Vscode Plugin: Modify %userprofile%\.vscode\extensions\github.copilot-xxxx\dist\extension.js**
|
||||
|
||||
```js
|
||||
//Add the following code, note that you'll need to re-add it if the vscode plugin gets updated; Jetbrains does not require this step
|
||||
process.env.GITHUB_TOKEN="i_am_free"; // Corresponds to the "verification" value above
|
||||
process.env.GITHUB_API_URL="https://api.example.com"; // Your address
|
||||
process.env.CODESPACES="true";
|
||||
process.env.GITHUB_SERVER_URL="https://github.com";
|
||||
```
|
||||
|
||||
------
|
||||
|
||||
##### **Successful Testing:**
|
||||
|
||||

|
||||
@@ -1,109 +1,26 @@
|
||||
[中文](README.md) | [English](README-EN.md)
|
||||
|
||||
|
||||
# share-copilot
|
||||
|
||||
- 作为代理服务器,中转copilot插件的API的请求(也可以代理无良商贩的,自行研究)
|
||||
- 支持vscode插件和jetbrains插件
|
||||
- 支持多用户共享一个token
|
||||
- 优化请求逻辑,降低suspended概率(别万人骑基本不会)
|
||||
|
||||
```sh
|
||||
简单说一下
|
||||
本地插件的请求逻辑:
|
||||
|
||||
1.本地插件携ghu_token 请求github的API
|
||||
|
||||
2.返回带有时间戳的copilot的token(简称co_token),
|
||||
代码提示都是用的co_token
|
||||
|
||||
3.co_token到期->ghu_token重新请求->获得新的co_token
|
||||
-------------------------------------------------------------
|
||||
代理服务器的逻辑:
|
||||
|
||||
代理的是"ghu_token请求"这一环节
|
||||
|
||||
1.使用ghu_token请求github的API,暂存co_token
|
||||
|
||||
2.多用户请求时,判断co_token有没有过期,没过期直接返回co_token,
|
||||
减少对ghu_token的请求次数(大约10-20分钟过期)
|
||||
|
||||
3.至于本地co_token,也就是代码提示没有走代理(可以代理,但是要修改插件,jetbrains的插件还要重新build)
|
||||
猜测风控目前也只是停留在请求ghu_token这一层
|
||||
|
||||
"cocopilot也是代理了ghu_token请求,并没有破解插件"
|
||||
|
||||
```
|
||||
|
||||
------
|
||||
|
||||
```xml
|
||||
| 文件名 | 说明
|
||||
| -------------- | -------------------
|
||||
| config.json | 配置文件
|
||||
| copilot_api | guh_token和co_token相关逻辑
|
||||
| global | 全局变量、常量或配置
|
||||
| main | 主要入口文件
|
||||
| middleware | 中间件,验证域名,验证自定义请求头
|
||||
| routers | 路由信息,定义不同请求路径的处理逻辑。
|
||||
| server | 服务端的代码或相关配置,用于启动和运行服务器。
|
||||
| show_msg | 用于显示消息或信息的函数,展示内容。
|
||||
|
||||
```
|
||||
|
||||
***
|
||||
# 一、自行编译:
|
||||
```
|
||||
测试环境:Linux4.18.0-305.3.1.el8.x86_64 GNU/Linux
|
||||
需要 go环境 出问题自行chatgpt
|
||||
```
|
||||
```sh
|
||||
git clone https://gitee.com/chuangxxt/share-copilot.git
|
||||
wget https://dl.google.com/go/go1.21.1.linux-amd64.tar.gz
|
||||
tar -C /usr/local -xzf go1.21.1.linux-amd64.tar.gz
|
||||
|
||||
vim ~/.bashrc
|
||||
#添加下列环境
|
||||
export PATH=$PATH:/usr/local/go/bin
|
||||
export GOPATH=$HOME/go
|
||||
export PATH=$PATH:$GOPATH/bin
|
||||
#使生效环境变量
|
||||
source ~/.bashrc
|
||||
#验证go配置
|
||||
go version
|
||||
|
||||
#cd到main.go源码目录
|
||||
cd /share-copilot/src
|
||||
go build
|
||||
```
|
||||
***
|
||||
# 二、使用现成:
|
||||
|
||||
### 1.安装
|
||||
|
||||
```sh
|
||||
wget https://gitee.com/chuangxxt/share-copilot/releases/download/1.0.2/share-copilot-linux-amd64.zip
|
||||
```
|
||||
```sh
|
||||
unzip share-copilot-linux-amd64.zip
|
||||
git clone https://gitee.com/chuangxxt/share-copilot.git
|
||||
```
|
||||
```
|
||||
```sh
|
||||
cd share-copilot
|
||||
```
|
||||
```sh
|
||||
```
|
||||
bash install.sh
|
||||
```
|
||||
|
||||
### 2.配置
|
||||
|
||||
```sh
|
||||
vim config.json #修改配置信息
|
||||
```
|
||||
|
||||
##### 2.1 config.json文件说明
|
||||
|
||||
```js
|
||||
domain//监听域名或ip 可用nginx反代
|
||||
domain//绑定域名
|
||||
|
||||
host //ip
|
||||
|
||||
@@ -116,7 +33,7 @@ keyPath//私钥路径-443必须
|
||||
github_api_url//默认不改
|
||||
|
||||
token//自己的副驾驶token,可放多个,请求时随机使用
|
||||
//登录插件后在%userprofile%\AppData\Local\github-copilot\hosts.json ghu_开头
|
||||
//登录插件后在%userprofile%\AppData\Local\github-copilot\host.json ghu_开头
|
||||
|
||||
verification//自定义验证
|
||||
```
|
||||
@@ -147,16 +64,16 @@ verification//自定义验证
|
||||
|
||||
### 3.启动
|
||||
|
||||
```sh
|
||||
scop r #运行 [Ctrl+c退出] 如果运行失败添加可执行权限 chmod +x share-copilot
|
||||
scop rb #后台运行
|
||||
scop st #停止
|
||||
scop v #查看是否运行
|
||||
```
|
||||
scop r --运行
|
||||
scop rb --后台运行
|
||||
scop st --停止
|
||||
scop v --查看状态
|
||||
```
|
||||
|
||||
### 4.完整例子:
|
||||
### 4.完整示例一:
|
||||
|
||||
##### 4.1 服务端配置修改config.json
|
||||
config.json
|
||||
|
||||
```json
|
||||
{
|
||||
@@ -178,50 +95,21 @@ scop v #查看是否运行
|
||||
}
|
||||
```
|
||||
|
||||
##### 4.2 运行成功截图:
|
||||
运行截图
|
||||
|
||||

|
||||
```
|
||||
Jetbrains -- 用于Jetbrains插件
|
||||
Vscode -- 用于Vscode插件
|
||||
Valid tokens: -- 配置里面有效的token数量
|
||||
Succeed: -- 插件请求代理成功次数
|
||||
Failed: -- 插件请求代理失败次数
|
||||
GithubApi: --服务器请求gh_api次数
|
||||
正常情况下 GithubApi < Succeed 因为请求的Token 几十分钟才过期
|
||||
```
|
||||
------
|
||||

|
||||
|
||||
##### 4.3本地配置修改:可以使用zhile大佬的[cocopilot脚本](https://gitee.com/chuangxxt/share-copilot/releases/download/v1.0.3/cocopilot-master.zip) 改成自己的api一键配置
|
||||
时间-合计请求次数: 0 | 成功次数 0 | 失败次数 0 统计次数在00:00:00归零
|
||||
|
||||
**以windows为例**
|
||||
|
||||
- **jetbrains插件修改:%userprofile%\AppData\Local\github-copilot\hosts.json**
|
||||
|
||||
|
||||
|
||||
修改%userprofile%\AppData\Local\github-copilot\host.json
|
||||
|
||||
```json
|
||||
{
|
||||
"github.com":{
|
||||
"user":"suibian",//随意填写
|
||||
"oauth_token":"i_am_free",//与上面verification对应
|
||||
"dev_override":{
|
||||
"copilot_token_url":"https://api.example.com/copilot_internal/v2/token"
|
||||
//你的地址
|
||||
}
|
||||
}
|
||||
}
|
||||
{"github.com":{"user":"suibian","oauth_token":"i_am_free","dev_override":{"copilot_token_url":"https://api.example.com/copilot_internal/v2/token"}}}
|
||||
```
|
||||
- **vscode插件修改:%userprofile%\\.vscode\extensions\github.copilot-xxxx\dist\extension.js**
|
||||
|
||||
```js
|
||||
//添加下列代码,注意vscode插件更新需要重新添加,jetbrains则不用
|
||||
process.env.GITHUB_TOKEN="i_am_free"; //与上面verification对应
|
||||
process.env.GITHUB_API_URL="https://api.example.com"; //你的地址
|
||||
process.env.CODESPACES="true";
|
||||
process.env.GITHUB_SERVER_URL="https://github.com";
|
||||
```
|
||||
|
||||
------
|
||||
|
||||
##### **4.4测试成功:**
|
||||
测试:
|
||||
|
||||

|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"server": {
|
||||
"domain": "example.con",
|
||||
"host": "0.0.0.0",
|
||||
"port": 443,
|
||||
"certPath":"./example.pem",
|
||||
"keyPath":"./example.pem"
|
||||
},
|
||||
"copilot_config":{
|
||||
"github_api_url": "https://api.github.com/copilot_internal/v2/token",
|
||||
"token":[
|
||||
"yours_token"
|
||||
]},
|
||||
"verification":""
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
#!/bin/bash
|
||||
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
START_SCRIPT_PATH="/usr/local/bin/scop"
|
||||
cat > "$START_SCRIPT_PATH" <<EOF
|
||||
#!/bin/bash
|
||||
EXECUTABLE_DIR="$SCRIPT_DIR"
|
||||
EXECUTABLE="\$EXECUTABLE_DIR/share-copilot"
|
||||
case "\$1" in
|
||||
r)
|
||||
echo "Running share-copilot..."
|
||||
"\$EXECUTABLE"
|
||||
;;
|
||||
rb)
|
||||
echo "Running share-copilot in the background..."
|
||||
"\$EXECUTABLE" >/dev/null 2>&1 &
|
||||
;;
|
||||
st)
|
||||
echo "Stopping share-copilot..."
|
||||
pkill -f "\$EXECUTABLE"
|
||||
;;
|
||||
v)
|
||||
if pgrep -f "\$EXECUTABLE" >/dev/null; then
|
||||
echo "share-copilot is running"
|
||||
else
|
||||
echo "share-copilot is not running"
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
echo "Usage: \$0 {r|rb|st|v}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
|
||||
|
||||
exit 0
|
||||
EOF
|
||||
chmod +x "$START_SCRIPT_PATH"
|
||||
echo "-----------------
|
||||
install success!
|
||||
-----------------
|
||||
scop r --运行
|
||||
scop rb --后台运行
|
||||
scop st --停止
|
||||
scop v --查看状态
|
||||
-----------------"
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 15 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 136 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 11 KiB |
Binary file not shown.
@@ -0,0 +1,10 @@
|
||||
@echo off
|
||||
SET CGO_ENABLED=0
|
||||
SET GOOS=linux
|
||||
SET GOARCH=amd64
|
||||
go build
|
||||
@echo off
|
||||
SET CGO_ENABLED=0
|
||||
SET GOOS=windows
|
||||
SET GOARCH=amd64
|
||||
go build
|
||||
+15
-19
@@ -3,37 +3,33 @@ module share-copilot
|
||||
go 1.21
|
||||
|
||||
require (
|
||||
github.com/fatih/color v1.15.0
|
||||
github.com/gin-gonic/gin v1.9.1
|
||||
github.com/go-resty/resty/v2 v2.7.0
|
||||
github.com/nsf/termbox-go v1.1.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytedance/sonic v1.9.1 // indirect
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
|
||||
github.com/bytedance/sonic v1.10.0 // indirect
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
|
||||
github.com/chenzhuoyu/iasm v0.9.0 // indirect
|
||||
github.com/fatih/color v1.15.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/gin-gonic/gin v1.9.1 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.14.0 // indirect
|
||||
github.com/go-playground/validator/v10 v10.15.3 // indirect
|
||||
github.com/go-resty/resty/v2 v2.7.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
|
||||
github.com/leodido/go-urn v1.2.4 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.9 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.11 // indirect
|
||||
golang.org/x/arch v0.3.0 // indirect
|
||||
golang.org/x/crypto v0.9.0 // indirect
|
||||
golang.org/x/net v0.10.0 // indirect
|
||||
golang.org/x/sys v0.8.0 // indirect
|
||||
golang.org/x/text v0.9.0 // indirect
|
||||
google.golang.org/protobuf v1.30.0 // indirect
|
||||
golang.org/x/arch v0.5.0 // indirect
|
||||
golang.org/x/crypto v0.13.0 // indirect
|
||||
golang.org/x/net v0.15.0 // indirect
|
||||
golang.org/x/sys v0.12.0 // indirect
|
||||
golang.org/x/text v0.13.0 // indirect
|
||||
google.golang.org/protobuf v1.31.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
+64
-35
@@ -1,41 +1,47 @@
|
||||
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
|
||||
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
|
||||
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
|
||||
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
|
||||
github.com/bytedance/sonic v1.10.0 h1:qtNZduETEIWJVIyDl01BeNxur2rW9OwTQ/yBqFRkKEk=
|
||||
github.com/bytedance/sonic v1.10.0/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA=
|
||||
github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo=
|
||||
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
|
||||
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
|
||||
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
|
||||
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
|
||||
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
|
||||
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
|
||||
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js=
|
||||
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
|
||||
github.com/go-playground/validator/v10 v10.15.3 h1:S+sSpunYjNPDuXkWbK+x+bA7iXiW296KG4dL3X7xUZo=
|
||||
github.com/go-playground/validator/v10 v10.15.3/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
|
||||
github.com/go-resty/resty/v2 v2.3.0/go.mod h1:UpN9CgLZNsv4e9XG50UU8xdI0F43UQ4HmxLBDwaroHU=
|
||||
github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
|
||||
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
|
||||
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
|
||||
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
|
||||
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
@@ -43,18 +49,13 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
|
||||
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
|
||||
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
|
||||
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
|
||||
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
@@ -65,40 +66,68 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
|
||||
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
|
||||
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
|
||||
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
|
||||
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
|
||||
golang.org/x/arch v0.5.0 h1:jpGode6huXQxcskEIpOCvrU+tzo81b6+oFLUYXWtH/Y=
|
||||
golang.org/x/arch v0.5.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
|
||||
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
|
||||
golang.org/x/image v0.12.0 h1:w13vZbU4o5rKOFFR8y7M+c4A5jXDC0uXTdHYRP8X2DQ=
|
||||
golang.org/x/image v0.12.0/go.mod h1:Lu90jvHG7GfemOIcldsh9A2hS01ocl6oNO7ype5mEnk=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
|
||||
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
||||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
|
||||
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
|
||||
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||
+244
@@ -0,0 +1,244 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/fatih/color"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
type Config struct {
|
||||
Server struct {
|
||||
Domain string `json:"domain"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
CertPath string `json:"certPath"`
|
||||
KeyPath string `json:"keyPath"`
|
||||
} `json:"server"`
|
||||
CopilotConfig struct {
|
||||
GithubApiUrl string `json:"github_api_url"`
|
||||
Token []string `json:"token"`
|
||||
} `json:"copilot_config"`
|
||||
Verification string `json:"verification"`
|
||||
}
|
||||
var (
|
||||
requestCountMutex sync.Mutex
|
||||
requestCount int
|
||||
successCount int
|
||||
configFile = Config{}
|
||||
)
|
||||
func main() {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
gin.DefaultWriter = io.Discard
|
||||
//初始化配置文件
|
||||
configFile = initConfig()
|
||||
// 创建Gin引擎
|
||||
engine := gin.New()
|
||||
// 自定义错误处理程序
|
||||
engine.Use(func(c *gin.Context) {
|
||||
c.Next()
|
||||
err := c.Errors.Last()
|
||||
if err != nil {
|
||||
log.Printf("Error: %v", err.Err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
|
||||
}
|
||||
})
|
||||
// 设置信任的代理
|
||||
err := engine.SetTrustedProxies([]string{"127.0.0.1"})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// 配置支持的应用程序协议
|
||||
server := &http.Server{
|
||||
Addr: ":443",
|
||||
TLSConfig: &tls.Config{
|
||||
NextProtos: []string{"http/1.1", "http/1.2", "http/2"}, // 支持的应用程序协议列表
|
||||
},
|
||||
Handler: engine,
|
||||
}
|
||||
// 定义路由
|
||||
domainDefault := engine.Group("/", DomainMiddleware(configFile.Server.Domain))
|
||||
domainDefault.GET("/copilot_internal/v2/token", getToken())
|
||||
// 启动Gin服务器并监听端口
|
||||
listenAddress := configFile.Server.Host + ":" + strconv.Itoa(configFile.Server.Port)
|
||||
go func() {
|
||||
if configFile.Server.Port != 443 {
|
||||
err := engine.Run(listenAddress)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
err := server.ListenAndServeTLS(configFile.Server.CertPath, configFile.Server.KeyPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
// 显示url
|
||||
displayMsg()
|
||||
ticker := time.NewTicker(1 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
showRequestCount()
|
||||
}
|
||||
}
|
||||
func getToken() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 请求计数
|
||||
incrementRequestCount()
|
||||
// 获取请求头中的Authorization令牌
|
||||
if configFile.Verification != "" {
|
||||
token := c.GetHeader("Authorization")
|
||||
tokenStr := strings.ReplaceAll(token, " ", "")
|
||||
configCert := strings.ReplaceAll(configFile.Verification, " ", "")
|
||||
if tokenStr != "token"+configCert {
|
||||
// 拒绝不符合Verification的请求
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "Bad credentials",
|
||||
"documentation_url": "https://docs.github.com/rest"})
|
||||
return
|
||||
}
|
||||
}
|
||||
// 设置请求头
|
||||
headers := map[string]string{
|
||||
"Authorization": "token " + getRandomToken(configFile.CopilotConfig.Token),
|
||||
"editor-version": c.GetHeader("editor-version"),
|
||||
"editor-plugin-version": c.GetHeader("editor-plugin-version"),
|
||||
"user-agent": c.GetHeader("user-agent"),
|
||||
"accept": c.GetHeader("accept"),
|
||||
"accept-encoding": c.GetHeader("accept-encoding"),
|
||||
}
|
||||
// 发起GET请求
|
||||
response, err := resty.New().R().
|
||||
SetHeaders(headers).
|
||||
Get(configFile.CopilotConfig.GithubApiUrl)
|
||||
if err != nil {
|
||||
// 处理请求错误
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
// 检查响应状态码
|
||||
if response.StatusCode() != http.StatusOK {
|
||||
// 处理非200响应
|
||||
c.JSON(response.StatusCode(), gin.H{"error": "Request failed"})
|
||||
return
|
||||
}
|
||||
// 请求成功统计
|
||||
incrementSuccessCount()
|
||||
// 从响应中获取内容并发送回客户端
|
||||
responseBody := response.String()
|
||||
// 设置响应头 重要
|
||||
c.Header("Content-Type", "application/json")
|
||||
c.String(http.StatusOK, responseBody)
|
||||
}
|
||||
}
|
||||
func initConfig() Config {
|
||||
// 打开或创建 error.log 文件
|
||||
logFile, err := os.OpenFile("error.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer logFile.Close()
|
||||
// 设置日志输出到文件
|
||||
log.SetOutput(logFile)
|
||||
// 读取配置文件
|
||||
configFile, err := os.Open("./config.json")
|
||||
if err != nil {
|
||||
panic("file \"./config.json\" not found")
|
||||
}
|
||||
defer func(configFile *os.File) {
|
||||
err := configFile.Close()
|
||||
if err != nil {
|
||||
panic("close file \"./config.json\" err")
|
||||
}
|
||||
}(configFile)
|
||||
decoder := json.NewDecoder(configFile)
|
||||
config := Config{}
|
||||
err = decoder.Decode(&config)
|
||||
if err != nil {
|
||||
panic("config format err")
|
||||
}
|
||||
return config
|
||||
}
|
||||
func displayMsg() {
|
||||
c := color.New(color.FgGreen)
|
||||
fmt.Printf("\n------------------------------------------------------------\n")
|
||||
if configFile.Server.Port == 80 {
|
||||
_, err := c.Println("[ApiUri] http://" + configFile.Server.Domain + "/copilot_internal/v2/token\r")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else if configFile.Server.Port == 443 {
|
||||
_, err := c.Println("[ApiUrl] https://" + configFile.Server.Domain + "/copilot_internal/v2/token\r")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
_, err := c.Println("[ApiUrl] http://" + configFile.Server.Domain + ":" + strconv.Itoa(configFile.Server.Port) + "/copilot_internal/v2/token\r")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
fmt.Printf("------------------------------------------------------------\n")
|
||||
}
|
||||
func incrementRequestCount() {
|
||||
requestCount++
|
||||
}
|
||||
func incrementSuccessCount() {
|
||||
successCount++
|
||||
}
|
||||
func showRequestCount() {
|
||||
requestCountMutex.Lock()
|
||||
count := requestCount
|
||||
sCount := successCount
|
||||
requestCountMutex.Unlock()
|
||||
currentTime := time.Now().Format("2006-01-02 15:04:05")
|
||||
if time.Now().Format("15:04:05") == "00:00:00" {
|
||||
resetRequestCount()
|
||||
}
|
||||
// 黄色文本
|
||||
timeStr := fmt.Sprintf("\x1b[33m%s\x1b[0m", currentTime)
|
||||
// 蓝色文本
|
||||
countStr := fmt.Sprintf("\x1b[34m%d\x1b[0m", count)
|
||||
// 绿色文本
|
||||
successCountStr := fmt.Sprintf("\x1b[32m%d\x1b[0m", sCount)
|
||||
// 红色文本
|
||||
failureCountStr := fmt.Sprintf("\x1b[31m%d\x1b[0m", count-sCount)
|
||||
fmt.Printf("\033[1G%s - Total Count: %s | Success Count %s | Fail Count %s ", timeStr, countStr, successCountStr, failureCountStr)
|
||||
}
|
||||
func resetRequestCount() {
|
||||
requestCountMutex.Lock()
|
||||
defer requestCountMutex.Unlock()
|
||||
requestCount = 0
|
||||
successCount = 0
|
||||
}
|
||||
func getRandomToken(tokens []string) string {
|
||||
if len(tokens) == 0 {
|
||||
return "" // 返回空字符串或处理其他错误情况
|
||||
}
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
randomIndex := r.Intn(len(tokens))
|
||||
return tokens[randomIndex]
|
||||
}
|
||||
func DomainMiddleware(domain string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 检查域名是否匹配
|
||||
requestDomain := strings.Split(c.Request.Host, ":")[0]
|
||||
if requestDomain == domain {
|
||||
c.Next()
|
||||
} else {
|
||||
c.String(403, "Forbidden")
|
||||
c.Abort()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 初始化有效的github token列表
|
||||
func initValidGhuTokenMap() {
|
||||
for _, token := range configFile.CopilotConfig.Token {
|
||||
if getGithubApi(token) {
|
||||
// 有效的token
|
||||
validGhuTokenMap[token] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 代理服务器返回copilot token
|
||||
func proxyResp(c *gin.Context, respDataMap map[string]interface{}) {
|
||||
// 将map转换为JSON字符串
|
||||
responseJSON, _ := json.Marshal(respDataMap)
|
||||
// 请求成功统计
|
||||
successCount++
|
||||
// 将响应体作为JSON字符串返回
|
||||
c.Header("Content-Type", "application/json")
|
||||
c.String(http.StatusOK, string(responseJSON))
|
||||
}
|
||||
|
||||
// 获取有效的 copilot token
|
||||
func getCopilotToken() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
//用户请求代理服务器的计数
|
||||
requestCount++
|
||||
//随机从有效的github token列表中获取一个token
|
||||
token := getRandomToken(validGhuTokenMap)
|
||||
//通过token取对应的copilot token的map数据
|
||||
if respDataMap, exists := getTokenData(token); exists {
|
||||
//没过期直接返回
|
||||
if !isTokenExpired(respDataMap) {
|
||||
proxyResp(c, respDataMap)
|
||||
return
|
||||
}
|
||||
}
|
||||
//过期了或者没取到,重新获取
|
||||
if getGithubApi(token) {
|
||||
proxyResp(c, copilotTokenMap[token])
|
||||
} else {
|
||||
// 重新获取失败,返回自定义消息 400
|
||||
diyBadRequest(c, 400, "can't get copilot token")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 请求github api
|
||||
func getGithubApi(token string) bool {
|
||||
// githubApi请求计数
|
||||
githubApiCount++
|
||||
// 设置请求头
|
||||
headers := map[string]string{
|
||||
"Authorization": "token " + token,
|
||||
}
|
||||
// 发起GET请求
|
||||
response, _ := resty.New().R().
|
||||
SetHeaders(headers).
|
||||
Get(configFile.CopilotConfig.GithubApiUrl)
|
||||
// 判断响应状态码
|
||||
if response.StatusCode() == http.StatusOK {
|
||||
// 响应状态码为200 OK
|
||||
respDataMap := map[string]interface{}{}
|
||||
_ = json.Unmarshal(response.Body(), &respDataMap)
|
||||
copilotTokenMap[token] = respDataMap
|
||||
return true
|
||||
} else {
|
||||
// 响应状态码不为200 map删除无效token
|
||||
delete(validGhuTokenMap, token)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// 从map中获取github token对应的copilot token
|
||||
func getTokenData(token string) (map[string]interface{}, bool) {
|
||||
respDataMap, exists := copilotTokenMap[token]
|
||||
return respDataMap, exists
|
||||
}
|
||||
|
||||
// 检测copilot token是否过期
|
||||
func isTokenExpired(respDataMap map[string]interface{}) bool {
|
||||
if expiresAt, ok := respDataMap["expires_at"].(float64); ok {
|
||||
currentTime := time.Now().Unix()
|
||||
expiresAtInt64 := int64(expiresAt)
|
||||
return expiresAtInt64 <= currentTime+60
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// 从map中随机获取一个github token
|
||||
func getRandomToken(m map[string]bool) string {
|
||||
ghuTokenArray := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
ghuTokenArray = append(ghuTokenArray, k)
|
||||
}
|
||||
if len(ghuTokenArray) == 0 {
|
||||
return "" // 没有有效的token,返回空字符串
|
||||
}
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
randomIndex := r.Intn(len(ghuTokenArray))
|
||||
return ghuTokenArray[randomIndex]
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Config 配置文件结构体
|
||||
type Config struct {
|
||||
Server struct {
|
||||
Domain string `json:"domain"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
CertPath string `json:"certPath"`
|
||||
KeyPath string `json:"keyPath"`
|
||||
} `json:"server"`
|
||||
CopilotConfig struct {
|
||||
GithubApiUrl string `json:"github_api_url"`
|
||||
Token []string `json:"token"`
|
||||
} `json:"copilot_config"`
|
||||
Verification string `json:"verification"`
|
||||
}
|
||||
|
||||
// 全局变量
|
||||
var (
|
||||
copilotGinEngine *gin.Engine
|
||||
//有效ghu_token的map
|
||||
validGhuTokenMap = make(map[string]bool)
|
||||
//与有效ghu_token对于的co_token的map
|
||||
copilotTokenMap = make(map[string]map[string]interface{})
|
||||
//服务器配置文件
|
||||
configFile Config
|
||||
//请求计数锁
|
||||
requestCountMutex sync.Mutex
|
||||
//githubApi请求计数
|
||||
githubApiCount = 0
|
||||
//总请求计数
|
||||
requestCount = 0
|
||||
//请求成功计数
|
||||
successCount = 0
|
||||
)
|
||||
|
||||
// 测试的玩意儿
|
||||
var (
|
||||
diyMsg = ";//授权已经过期,重新获取-> https://www.sharecopilot.top"
|
||||
completionUrl = "https://copilot-proxy.githubusercontent.com/v1/engines/copilot-codex/completions"
|
||||
telemetryUrl = "https://copilot-telemetry.githubusercontent.com/telemetry"
|
||||
isModMsg = true
|
||||
)
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
package main
|
||||
|
||||
func init() {
|
||||
loadConfig() // 1.加载服务器配置文件
|
||||
initGinEngine() // 2.初始化Gin引擎
|
||||
initValidGhuTokenMap() // 3.初始化有效Ghu_token
|
||||
}
|
||||
|
||||
func main() {
|
||||
Routes() // 1.url路由
|
||||
StartServer() // 2.启动服务器
|
||||
showMsg() // 3.控制台信息显示
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//1.DomainMiddleware 域名中间件
|
||||
//2.VerifyRequestMiddleware 代理服务器自定义的token验证中间件
|
||||
|
||||
// DomainMiddleware 域名中间件
|
||||
func DomainMiddleware(domain string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 获取请求的域名(不包括端口)
|
||||
requestDomain := strings.Split(c.Request.Host, ":")[0]
|
||||
// 使用子域名的情况下,检查域名是否匹配或为本地地址
|
||||
if requestDomain == domain || requestDomain == "127.0.0.1" || requestDomain == "localhost" {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
c.AbortWithStatus(403)
|
||||
}
|
||||
}
|
||||
|
||||
// VerifyRequestMiddleware 代理服务器自定义的token验证中间件
|
||||
func VerifyRequestMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if configFile.Verification != "" {
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if authHeader != "token "+configFile.Verification {
|
||||
c.JSON(401, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package main
|
||||
|
||||
// Routes 自定义代理服务器路由 附加中间件(域名验证和请求验证)
|
||||
func Routes() {
|
||||
// 定义根路由
|
||||
copilotApi := copilotGinEngine.Group("/copilot_internal", DomainMiddleware(configFile.Server.Domain), VerifyRequestMiddleware())
|
||||
|
||||
// 定义代理github token的路由
|
||||
copilotApi.GET("/v2/token", getCopilotToken())
|
||||
|
||||
// 定义 completions路由的反向代理
|
||||
registerProxyRoute(copilotGinEngine, "/v1/engines/copilot-codex/completions", completionUrl)
|
||||
|
||||
// 定义 telemetry路由的反向代理 ,
|
||||
registerProxyRoute(copilotGinEngine, "/telemetry", telemetryUrl)
|
||||
}
|
||||
-148
@@ -1,148 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// 加载服务器配置文件
|
||||
func loadConfig() {
|
||||
// 获取可执行文件所在目录 ./config.json
|
||||
exePath, _ := os.Executable()
|
||||
exeDir := filepath.Dir(exePath)
|
||||
configFileTemp, err := os.Open(exeDir + "/config.json")
|
||||
if err != nil {
|
||||
panic("file \"./config.json\" not found")
|
||||
}
|
||||
//函数退出时关闭文件流
|
||||
defer func(configFile *os.File) {
|
||||
_ = configFile.Close()
|
||||
}(configFileTemp)
|
||||
//解析json
|
||||
decoder := json.NewDecoder(configFileTemp)
|
||||
err = decoder.Decode(&configFile)
|
||||
if err != nil {
|
||||
panic("config format err")
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化Gin引擎
|
||||
func initGinEngine() {
|
||||
// 设置gin模式为发布模式
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
//关闭gin日志输出 自认为小项目没什么用
|
||||
gin.DefaultWriter = io.Discard
|
||||
// 创建Gin引擎
|
||||
copilotGinEngine = gin.New()
|
||||
// 设置信任的前置代理 用nginx反代需要 不写这个编译会有个警告看着难受
|
||||
if err := copilotGinEngine.SetTrustedProxies([]string{"127.0.0.1"}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// StartServer 启动服务器并监听ip和端口
|
||||
func StartServer() {
|
||||
//监听地址是host+port
|
||||
listenAddress := configFile.Server.Host + ":" + strconv.Itoa(configFile.Server.Port)
|
||||
server := &http.Server{
|
||||
Addr: listenAddress,
|
||||
TLSConfig: &tls.Config{
|
||||
NextProtos: []string{"http/1.1", "http/1.2", "http/2"},
|
||||
},
|
||||
Handler: copilotGinEngine,
|
||||
}
|
||||
go func() {
|
||||
if configFile.Server.Port != 443 {
|
||||
err := copilotGinEngine.Run(listenAddress)
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
err := server.ListenAndServeTLS(configFile.Server.CertPath, configFile.Server.KeyPath)
|
||||
log.Fatal(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// 自定义请求失败返回的状态码和错误信息
|
||||
func diyBadRequest(c *gin.Context, code int, errorMessage string) {
|
||||
c.JSON(code, gin.H{
|
||||
"message": errorMessage,
|
||||
"documentation_url": "https://docs.github.com/rest",
|
||||
})
|
||||
}
|
||||
|
||||
// registerProxyRoute 反向代理路由
|
||||
func registerProxyRoute(r *gin.Engine, routePath, targetURL string) {
|
||||
target, err := url.Parse(targetURL)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
proxy := &httputil.ReverseProxy{
|
||||
Director: func(req *http.Request) {
|
||||
req.URL.Host = target.Host
|
||||
req.URL.Scheme = "https"
|
||||
req.Host = target.Host
|
||||
},
|
||||
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
|
||||
},
|
||||
}
|
||||
r.Any(routePath, func(c *gin.Context) {
|
||||
proxy.Director = func(req *http.Request) {
|
||||
req.URL.Host = target.Host
|
||||
req.URL.Scheme = "https"
|
||||
req.Host = target.Host
|
||||
}
|
||||
if isModMsg {
|
||||
modProxyResp(c, proxy)
|
||||
}
|
||||
proxy.ServeHTTP(c.Writer, c.Request)
|
||||
})
|
||||
}
|
||||
|
||||
// 修改代码提示内容
|
||||
func modProxyResp(c *gin.Context, proxy *httputil.ReverseProxy) {
|
||||
// 在代理响应之前修改响应
|
||||
proxy.ModifyResponse = func(response *http.Response) error {
|
||||
responseBuffer := new(bytes.Buffer)
|
||||
_, readErr := responseBuffer.ReadFrom(response.Body)
|
||||
if readErr != nil {
|
||||
return readErr
|
||||
}
|
||||
responseData := responseBuffer.Bytes()
|
||||
// 定义正则表达式模式
|
||||
pattern := `(.*?)data:`
|
||||
// 编译正则表达式
|
||||
regex := regexp.MustCompile(pattern)
|
||||
// 查找匹配项
|
||||
match := regex.FindStringSubmatch(string(responseData))
|
||||
var replacedData = ""
|
||||
if len(match) >= 2 {
|
||||
replacedData = match[1]
|
||||
} else {
|
||||
fmt.Println("No match found.")
|
||||
}
|
||||
// 将字符串转换为[]rune类型的字符数组
|
||||
runes := []rune(diyMsg)
|
||||
newStr := ""
|
||||
// 遍历字符数组,对每个字符进行处理
|
||||
for _, r := range runes {
|
||||
// 在字符前后添加内容,生成新的字符串
|
||||
newStr += fmt.Sprintf("data: {\"id\":\"cmpl-7xy1GLgssjHEubVrPyt534VRYVF0t\",\"model\":\"cushman-ml\",\"created\":1694526422,\"choices\":[{\"text\":\"%c\",\"index\":0,\"finish_reason\":null,\"logprobs\":null}]}\n", r)
|
||||
}
|
||||
newStr = replacedData + newStr + "data: [DONE]\n"
|
||||
fmt.Println(newStr)
|
||||
c.Data(response.StatusCode, response.Header.Get("Content-Type"), []byte(newStr))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fatih/color"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 重置计数
|
||||
func resetRequestCount() {
|
||||
requestCountMutex.Lock()
|
||||
defer requestCountMutex.Unlock()
|
||||
requestCount = 0
|
||||
successCount = 0
|
||||
}
|
||||
|
||||
// 控制台显示信息 无关紧要的内容
|
||||
func showMsg() {
|
||||
fmt.Println(color.WhiteString("-----------------------------------------------------------------------"))
|
||||
fmt.Println(color.HiBlueString(" _ _ _ _ \n ___| |__ __ _ _ __ ___ ___ ___ _ __ (_) | ___ | |_ \n/ __| '_ \\ / _` | '__/ _ \\_____ / __/ _ \\| '_ \\| | |/ _ \\| __|\n\\__ \\ | | | (_| | | | __/_____| (_| (_) | |_) | | | (_) | |_ \n|___/_| |_|\\__,_|_| \\___| \\___\\___/| .__/|_|_|\\___/ \\__|\n |_| \n"))
|
||||
fmt.Println(color.WhiteString("[Source Code]") + "https://gitee.com/chuangxxt/share-copilot")
|
||||
fmt.Println(color.WhiteString("-----------------------------------------------------------------------"))
|
||||
var url = ""
|
||||
if configFile.Server.Port == 80 {
|
||||
url = "http://" + configFile.Server.Domain
|
||||
} else if configFile.Server.Port == 443 {
|
||||
url = "https://" + configFile.Server.Domain
|
||||
} else {
|
||||
url = "http://" + configFile.Server.Domain + ":" + strconv.Itoa(configFile.Server.Port)
|
||||
}
|
||||
jetStr, vsStr, valid := color.WhiteString("[Jetbrains]"), color.WhiteString("[Vscode]"), color.WhiteString("[Valid tokens]")
|
||||
fmt.Printf("%s: %s/copilot_internal/v2/token\n%s: %s\n%s: %d\n",
|
||||
jetStr, color.HiBlueString(url), vsStr, color.HiBlueString(url), valid, len(validGhuTokenMap))
|
||||
fmt.Println("-----------------------------------------------------------------------")
|
||||
for {
|
||||
requestCountMutex.Lock()
|
||||
sCount := successCount
|
||||
tCount := requestCount
|
||||
gCount := githubApiCount
|
||||
requestCountMutex.Unlock()
|
||||
currentTime := time.Now().Format("2006-01-02 15:04:05")
|
||||
if "00:00:00" == currentTime {
|
||||
resetRequestCount()
|
||||
}
|
||||
s2, s3, s4 := color.WhiteString("[Succeed]"), color.WhiteString("[Failed]"), color.WhiteString("[GithubApi]")
|
||||
// 打印文本
|
||||
fmt.Printf("\033[G%s - %s: %s %s: %s %s: %s ",
|
||||
color.HiYellowString(currentTime),
|
||||
s2, color.GreenString(strconv.Itoa(sCount)),
|
||||
s3, color.RedString(strconv.Itoa(tCount-sCount)),
|
||||
s4, color.CyanString(strconv.Itoa(gCount)))
|
||||
time.Sleep(1 * time.Second) //
|
||||
}
|
||||
}
|
||||
-86
@@ -1,86 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func getGithubTest(c *gin.Context, token string) {
|
||||
githubApiCount++
|
||||
data1 := `
|
||||
{
|
||||
"chat_enabled": false,
|
||||
"code_quote_enabled": false,
|
||||
"code_quote_v2_enabled": false,
|
||||
"copilotignore_enabled": false,
|
||||
"expires_at": 3194360727,
|
||||
"prompt_8k": true,
|
||||
"public_suggestions": "disabled",
|
||||
"refresh_in": 1500,
|
||||
"sku": "free_educational",
|
||||
"telemetry": "disabled",
|
||||
"token": "tid=;exp=1694360727;sku=free_educational;st=dotcom;8kp=1:",
|
||||
"tracking_id": ""
|
||||
}
|
||||
`
|
||||
data2 := `
|
||||
{
|
||||
"chat_enabled": false,
|
||||
"code_quote_enabled": false,
|
||||
"code_quote_v2_enabled": false,
|
||||
"copilotignore_enabled": false,
|
||||
"expires_at": 2294360727,
|
||||
"prompt_8k": true,
|
||||
"public_suggestions": "disabled",
|
||||
"refresh_in": 1500,
|
||||
"sku": "free_educational",
|
||||
"telemetry": "disabled",
|
||||
"token": "tid=;exp=1694360727;sku=free_educational;st=dotcom;8kp=1:",
|
||||
"tracking_id": ""
|
||||
}
|
||||
`
|
||||
data3 := `
|
||||
{
|
||||
"chat_enabled": false,
|
||||
"code_quote_enabled": false,
|
||||
"code_quote_v2_enabled": false,
|
||||
"copilotignore_enabled": false,
|
||||
"expires_at": 3394360727,
|
||||
"prompt_8k": true,
|
||||
"public_suggestions": "disabled",
|
||||
"refresh_in": 1500,
|
||||
"sku": "free_educational",
|
||||
"telemetry": "disabled",
|
||||
"token": "tid=;exp=1694360727;sku=free_educational;st=dotcom;8kp=1:",
|
||||
"tracking_id": ""
|
||||
}
|
||||
`
|
||||
//响应体map
|
||||
var respDataMap = make(map[string]interface{})
|
||||
if token == "1" {
|
||||
err := json.Unmarshal([]byte(data1), &respDataMap)
|
||||
if err != nil {
|
||||
// 处理JSON解析错误
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "JSON parsing error"})
|
||||
return
|
||||
}
|
||||
} else if token == "2" {
|
||||
err := json.Unmarshal([]byte(data2), &respDataMap)
|
||||
if err != nil {
|
||||
// 处理JSON解析错误
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "JSON parsing error"})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
err := json.Unmarshal([]byte(data3), &respDataMap)
|
||||
if err != nil {
|
||||
// 处理JSON解析错误
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "JSON parsing error"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
//token map
|
||||
copilotTokenMap[token] = respDataMap
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
START_SCRIPT_PATH="/usr/local/bin/scop"
|
||||
rm -f "$START_SCRIPT_PATH"
|
||||
echo "卸载完成!"
|
||||
Reference in New Issue
Block a user