mirror of
https://gitee.com/chuangxxt/share-copilot
synced 2026-12-02 05:17:54 +00:00
Compare commits
81
Commits
1.0.2
...
81d2d822ba
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
81d2d822ba | ||
|
|
38e5acda74 | ||
|
|
3e494a0699 | ||
|
|
b34c43db39 | ||
|
|
6a244d0a0d | ||
|
|
5bf787a4ed | ||
|
|
870cba8e7d | ||
|
|
c60e42395a | ||
|
|
98f631c6af | ||
|
|
684e35ce4d | ||
|
|
94e890c976 | ||
|
|
fa6e9323cb | ||
|
|
15e82057af | ||
|
|
2f3c01549c | ||
|
|
04b2ae49a4 | ||
|
|
044c537a3b | ||
|
|
7ad57e2ffb | ||
|
|
8dde8c9689 | ||
|
|
71c4bcf440 | ||
|
|
4d6c9cc958 | ||
|
|
b8a4de014c | ||
|
|
f6df58a5bf | ||
|
|
dcef8e11c3 | ||
|
|
85303e6af4 | ||
|
|
3d5cf042d0 | ||
|
|
b4fef2b182 | ||
|
|
70d1477eac | ||
|
|
0f20914b45 | ||
|
|
e64ced07ce | ||
|
|
bcec57aa63 | ||
|
|
b899b471d8 | ||
|
|
2ad30c6089 | ||
|
|
ff0b745eb0 | ||
|
|
1fa05962ca | ||
|
|
8eaccae889 | ||
|
|
390aa9429b | ||
|
|
df73f496f9 | ||
|
|
fd1dea3ab3 | ||
|
|
465426e4e1 | ||
|
|
f166316c6f | ||
|
|
e897820767 | ||
|
|
78206866d4 | ||
|
|
934820d691 | ||
|
|
150a88d2b5 | ||
|
|
4fe39fff57 | ||
|
|
04fb051b23 | ||
|
|
f134f2541b | ||
|
|
285137ffce | ||
|
|
952a31f63b | ||
|
|
89069492c7 | ||
|
|
d9fef498b9 | ||
|
|
67a8e7631a | ||
|
|
69c962c57e | ||
|
|
c774fda80f | ||
|
|
848ca4da0e | ||
|
|
20b104543e | ||
|
|
3fd12318a7 | ||
|
|
84ecafbf94 | ||
|
|
4fd2515d43 | ||
|
|
dbdd0adc73 | ||
|
|
27b23c0831 | ||
|
|
f4cbf0364f | ||
|
|
89c05e98df | ||
|
|
7f81713d48 | ||
|
|
89e3e6a264 | ||
|
|
9290499ebb | ||
|
|
6f59bab64a | ||
|
|
d7a5a02952 | ||
|
|
dafdf2f72e | ||
|
|
5ac6b5863a | ||
|
|
e1a95fbc62 | ||
|
|
67f6bd3f2f | ||
|
|
9cc98d6114 | ||
|
|
47655810a5 | ||
|
|
dd6ea3c9dc | ||
|
|
d4f84153aa | ||
|
|
a002cab328 | ||
|
|
aaa1592761 | ||
|
|
fde826355f | ||
|
|
7abff5fe83 | ||
|
|
8cb096239b |
+183
@@ -0,0 +1,183 @@
|
|||||||
|
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,16 +1,67 @@
|
|||||||
|
**[中文](README.md) | [English](README-EN.md)**
|
||||||
|
|
||||||
|
|
||||||
|
更新代码补全接口代理:[代码补全代理说明](readme/codeTipsProxy.md)
|
||||||
|
|
||||||
# share-copilot
|
# share-copilot
|
||||||
|
|
||||||
- 作为代理服务器,中转copilot插件的API的请求
|
- 作为代理服务器,中转copilot插件的API的请求(也可以代理无良商贩的,自行研究)
|
||||||
- 支持vscode插件和jetbrains插件
|
- 支持vscode插件和jetbrains插件
|
||||||
- 支持多用户共享一个token
|
- 支持多用户共享一个token
|
||||||
- 优化请求逻辑,降低token被ban概率(别万人骑基本不可能封)
|
- 优化请求逻辑,降低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
|
测试环境:Linux4.18.0-305.3.1.el8.x86_64 GNU/Linux
|
||||||
需要 go环境 出问题自行chatgpt
|
需要 go环境 出问题自行chatgpt
|
||||||
```
|
```
|
||||||
```sh
|
```sh
|
||||||
|
git clone https://gitee.com/chuangxxt/share-copilot.git
|
||||||
wget https://dl.google.com/go/go1.21.1.linux-amd64.tar.gz
|
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
|
tar -C /usr/local -xzf go1.21.1.linux-amd64.tar.gz
|
||||||
|
|
||||||
@@ -19,22 +70,25 @@ vim ~/.bashrc
|
|||||||
export PATH=$PATH:/usr/local/go/bin
|
export PATH=$PATH:/usr/local/go/bin
|
||||||
export GOPATH=$HOME/go
|
export GOPATH=$HOME/go
|
||||||
export PATH=$PATH:$GOPATH/bin
|
export PATH=$PATH:$GOPATH/bin
|
||||||
|
|
||||||
#使生效环境变量
|
#使生效环境变量
|
||||||
source ~/.bashrc
|
source ~/.bashrc
|
||||||
#验证安装
|
#验证go配置
|
||||||
go version
|
go version
|
||||||
|
|
||||||
#cd到main.go源码目录
|
#cd到main.go源码目录
|
||||||
cd /share-copilot/source
|
cd /share-copilot/src
|
||||||
go build
|
go build
|
||||||
```
|
```
|
||||||
|
***
|
||||||
# 二、使用现成:
|
# 二、使用现成:
|
||||||
|
|
||||||
### 1.安装
|
### 1.安装
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone https://gitlab.com/luyoyu/share-copilot.git
|
wget https://gitee.com/chuangxxt/share-copilot/releases/download/v2.0.4/share-copilot-linux-amd64.zip
|
||||||
|
```
|
||||||
|
```sh
|
||||||
|
unzip share-copilot-linux-amd64.zip
|
||||||
```
|
```
|
||||||
```sh
|
```sh
|
||||||
cd share-copilot
|
cd share-copilot
|
||||||
@@ -45,6 +99,10 @@ bash install.sh
|
|||||||
|
|
||||||
### 2.配置
|
### 2.配置
|
||||||
|
|
||||||
|
```sh
|
||||||
|
vim config.json #修改配置信息
|
||||||
|
```
|
||||||
|
|
||||||
##### 2.1 config.json文件说明
|
##### 2.1 config.json文件说明
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@@ -61,7 +119,7 @@ keyPath//私钥路径-443必须
|
|||||||
github_api_url//默认不改
|
github_api_url//默认不改
|
||||||
|
|
||||||
token//自己的副驾驶token,可放多个,请求时随机使用
|
token//自己的副驾驶token,可放多个,请求时随机使用
|
||||||
//登录插件后在%userprofile%\AppData\Local\github-copilot\host.json ghu_开头
|
//登录插件后在%userprofile%\AppData\Local\github-copilot\hosts.json ghu_开头
|
||||||
|
|
||||||
verification//自定义验证
|
verification//自定义验证
|
||||||
```
|
```
|
||||||
@@ -84,7 +142,9 @@ verification//自定义验证
|
|||||||
"yours_token_2",
|
"yours_token_2",
|
||||||
"yours_token_3"
|
"yours_token_3"
|
||||||
]},
|
]},
|
||||||
"verification":""
|
"verification":"",
|
||||||
|
"diyMsg":"",
|
||||||
|
"isModMsg":false
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -92,11 +152,11 @@ verification//自定义验证
|
|||||||
|
|
||||||
### 3.启动
|
### 3.启动
|
||||||
|
|
||||||
```
|
```sh
|
||||||
scop r --运行 [Esc退出]
|
scop r #运行 [Ctrl+c退出] 如果运行失败添加可执行权限 chmod +x share-copilot
|
||||||
scop rb --后台运行
|
scop rb #后台运行
|
||||||
scop st --停止
|
scop st #停止
|
||||||
scop v --查看状态
|
scop v #查看是否运行
|
||||||
```
|
```
|
||||||
|
|
||||||
### 4.完整例子:
|
### 4.完整例子:
|
||||||
@@ -119,27 +179,31 @@ scop v --查看状态
|
|||||||
"ghu_GZgKFwraHorAxXXUvsUclOhxiYERPsSJeNuF",
|
"ghu_GZgKFwraHorAxXXUvsUclOhxiYERPsSJeNuF",
|
||||||
"ghu_SPUTCLvkMKoeMstPJmhSlYsYvCojhkFjGubl"
|
"ghu_SPUTCLvkMKoeMstPJmhSlYsYvCojhkFjGubl"
|
||||||
]},
|
]},
|
||||||
"verification":"i_am_free"
|
"verification":"i_am_free",
|
||||||
|
"diyMsg":"",
|
||||||
|
"isModMsg":false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
##### 4.2 运行成功截图:
|
##### 4.2 运行成功截图:
|
||||||
|
|
||||||

|

|
||||||
|
```
|
||||||
[Jetbrains]:用于Jetbrains插件
|
Jetbrains -- 用于Jetbrains插件
|
||||||
[Vscode]: 用于Vscode插件
|
Vscode -- 用于Vscode插件
|
||||||
[User Request]: 插件请求代理服务器成功次数--统计次数在00:00:00归零
|
Valid tokens: -- 配置里面有效的token数量
|
||||||
[GithubApi request]: 代理服务器请求githubApi次数
|
Succeed: -- 插件请求代理成功次数
|
||||||
`正常情况下GithubApi request小于User Request,因为请求的Token 几十分钟才过期`
|
Failed: -- 插件请求代理失败次数
|
||||||
|
GithubApi: --服务器请求gh_api次数
|
||||||
|
正常情况下 GithubApi < Succeed 因为请求的Token 几十分钟才过期
|
||||||
|
```
|
||||||
------
|
------
|
||||||
|
|
||||||
##### 4.3本地配置修改:
|
##### 4.3本地配置修改:可以使用zhile大佬的[cocopilot脚本](https://gitee.com/chuangxxt/share-copilot/releases/download/v1.0.3/cocopilot-master.zip) 改成自己的api一键配置
|
||||||
|
|
||||||
**以windows为例**
|
**以windows为例**
|
||||||
|
|
||||||
- **jetbrains插件修改:%userprofile%\AppData\Local\github-copilot\host.json**
|
- **jetbrains插件修改:%userprofile%\AppData\Local\github-copilot\hosts.json**
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
@@ -153,7 +217,7 @@ scop v --查看状态
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
- **vscode插件修改:%userprofile%\.vscode\extensions\github.copilot-xxxx\dist\extension.js**
|
- **vscode插件修改:%userprofile%\\.vscode\extensions\github.copilot-xxxx\dist\extension.js**
|
||||||
|
|
||||||
```js
|
```js
|
||||||
//添加下列代码,注意vscode插件更新需要重新添加,jetbrains则不用
|
//添加下列代码,注意vscode插件更新需要重新添加,jetbrains则不用
|
||||||
@@ -168,6 +232,3 @@ process.env.GITHUB_SERVER_URL="https://github.com";
|
|||||||
##### **4.4测试成功:**
|
##### **4.4测试成功:**
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
# 三、求投喂!谢谢您!
|
|
||||||

|
|
||||||
-15
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"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
@@ -1,46 +0,0 @@
|
|||||||
#!/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.
|
After Width: | Height: | Size: 562 KiB |
@@ -0,0 +1,68 @@
|
|||||||
|
# 代码补全 代理说明
|
||||||
|
|
||||||
|
**-以windows为例**
|
||||||
|
|
||||||
|
## **vscode:**
|
||||||
|
|
||||||
|
#### 1.修改文件:**
|
||||||
|
|
||||||
|
`%userprofile%\.vscode\extensions\github.copilot-xxxx\dist\extension.js`
|
||||||
|
|
||||||
|
|
||||||
|
**1.1.extension.js全文搜索替换下面两个接口,替换为自己的url**
|
||||||
|
|
||||||
|
|
||||||
|
```js
|
||||||
|
"https://copilot-proxy.githubusercontent.com"
|
||||||
|
替换为"https://127.0.0.1" (你的url)
|
||||||
|
这个是代码提示接口
|
||||||
|
---------------------------------------------------------------
|
||||||
|
"https://copilot-telemetry.githubusercontent.com/telemetry"
|
||||||
|
替换为"https://127.0.0.1/telemetry" (你的url/telemetry)
|
||||||
|
这个应该是copilot把你代码上传拿去深度学习的,直接改成null好像也不影响使用
|
||||||
|
```
|
||||||
|
|
||||||
|
**1.2.也可以使用[脚本](https://gitee.com/chuangxxt/share-copilot/releases/download/v2.0.4/vscode%E4%B8%80%E9%94%AE%E9%85%8D%E7%BD%AE%E8%84%9A%E6%9C%AC.bat)一键修改,需要将extension.js原来增加的内容删掉,再配置脚本一键修改,脚本会自动备份原来的文件**
|
||||||
|
|
||||||
|
|
||||||
|
```bash
|
||||||
|
@echo off
|
||||||
|
setlocal
|
||||||
|
|
||||||
|
:: 将127.0.0.1改为自己的Api url GITHUB_TOKEN改为自己代理服务器中间件验证内容
|
||||||
|
set "GITHUB_TOKEN=123456"
|
||||||
|
set "GITHUB_API_URL=http://127.0.0.1"
|
||||||
|
set "completionsUrl=http://127.0.0.1"
|
||||||
|
set "telemetryUrl=http://127.0.0.1/telemetry"
|
||||||
|
|
||||||
|
set "CODESPACES=true"
|
||||||
|
set "GITHUB_SERVER_URL=https://github.com"
|
||||||
|
|
||||||
|
...略...
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2.测试
|
||||||
|
|
||||||
|
全部接口代理后是走一个ip了,虽然不知道有没有意义
|
||||||
|
|
||||||
|
不过可以搞点有意思的东西,修改一下配置文件
|
||||||
|
|
||||||
|
```json
|
||||||
|
"diyMsg":"//授权已经过期,重新获取-> https://gitee.com/chuangxxt/share-copilot",
|
||||||
|
"isModMsg":true
|
||||||
|
```
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## **jetbrains:**
|
||||||
|
|
||||||
|
代理服务器是支持的,插件我不会改,等个有缘人
|
||||||
|
|
||||||
|
我修改了插件里面js文件里面的接口,改为自己的,然后重新构建成exe,
|
||||||
|
打包后有问题,可以运行,代理联网等都正常
|
||||||
|
|
||||||
|
但是代码提示无法在ide里面显示
|
||||||
|
目前找不到原因(不懂node.js)
|
||||||
|
|
||||||
|

|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 21 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 136 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
Binary file not shown.
@@ -1,7 +0,0 @@
|
|||||||
/*.pem
|
|
||||||
/*.idea
|
|
||||||
/*.gitignore
|
|
||||||
/*.json
|
|
||||||
/config.json
|
|
||||||
/*.exe
|
|
||||||
/share-copilot
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
@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
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
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 (
|
|
||||||
//初始化需要返回给客户端的响应体
|
|
||||||
responseData map[string]interface{}
|
|
||||||
requestCountMutex sync.Mutex
|
|
||||||
githubApiCount = 0
|
|
||||||
requestCount = 0
|
|
||||||
successCount = 0
|
|
||||||
configFile Config
|
|
||||||
)
|
|
||||||
-243
@@ -1,243 +0,0 @@
|
|||||||
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"
|
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
//初始化配置文件
|
|
||||||
configFile = initConfig()
|
|
||||||
// 创建Gin引擎
|
|
||||||
gin.SetMode(gin.ReleaseMode)
|
|
||||||
gin.DefaultWriter = io.Discard
|
|
||||||
engine := gin.New()
|
|
||||||
// 设置信任的代理
|
|
||||||
err := engine.SetTrustedProxies([]string{"127.0.0.1"})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
// 定义路由
|
|
||||||
domainDefault := engine.Group("/", DomainMiddleware(configFile.Server.Domain))
|
|
||||||
domainDefault.GET("/copilot_internal/v2/token", getToken())
|
|
||||||
initServer(engine)
|
|
||||||
// 显示信息
|
|
||||||
showMsg()
|
|
||||||
}
|
|
||||||
func initServer(engine *gin.Engine) {
|
|
||||||
// 配置支持的应用程序协议
|
|
||||||
server := &http.Server{
|
|
||||||
Addr: ":443",
|
|
||||||
TLSConfig: &tls.Config{
|
|
||||||
NextProtos: []string{"http/1.1", "http/1.2", "http/2"}, // 支持的应用程序协议列表
|
|
||||||
},
|
|
||||||
Handler: engine,
|
|
||||||
}
|
|
||||||
// 启动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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
func showMsg() {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
var jetStr = color.WhiteString("[Jetbrains]")
|
|
||||||
var vsStr = color.WhiteString("[Vscode]")
|
|
||||||
|
|
||||||
fmt.Println(jetStr + ": " + color.HiBlueString(url+"/copilot_internal/v2/token"))
|
|
||||||
fmt.Println(vsStr + ": " + color.HiBlueString(url))
|
|
||||||
fmt.Println(color.WhiteString("-----------------------------------------------------------------------"))
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
var s2 = color.WhiteString("[Succeed]")
|
|
||||||
var s3 = color.WhiteString("[Failed]")
|
|
||||||
var s4 = 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) //
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func getToken() gin.HandlerFunc {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
// 请求计数
|
|
||||||
incrementRequestCount()
|
|
||||||
// 如果配置了verification,则需要获取请求头中的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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//判断时间戳key是否存在
|
|
||||||
if _, exists := responseData["expires_at"]; exists {
|
|
||||||
// 获取当前时间的Unix时间戳
|
|
||||||
currentTime := time.Now().Unix()
|
|
||||||
if expiresAt, ok := responseData["expires_at"].(float64); ok {
|
|
||||||
// 判断expires_at是否已经过期
|
|
||||||
expiresAtInt64 := int64(expiresAt)
|
|
||||||
//提前一分钟请求
|
|
||||||
if expiresAtInt64 > currentTime+60 {
|
|
||||||
//fmt.Println("\n未过期无需请求")
|
|
||||||
respProxy(c)
|
|
||||||
} else {
|
|
||||||
//fmt.Println("\n已过期重新请求")
|
|
||||||
getGithubApi(c)
|
|
||||||
respProxy(c)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fmt.Println("Age is not an int")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
//向githubApi发送请求
|
|
||||||
//fmt.Println("\n第一次请求")
|
|
||||||
getGithubApi(c)
|
|
||||||
respProxy(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func respProxy(c *gin.Context) {
|
|
||||||
// 将map转换为JSON字符串
|
|
||||||
responseJSON, err := json.Marshal(responseData)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "JSON marshaling error"})
|
|
||||||
}
|
|
||||||
// 请求成功统计
|
|
||||||
incrementSuccessCount()
|
|
||||||
// 将JSON字符串作为响应体返回
|
|
||||||
c.Header("Content-Type", "application/json")
|
|
||||||
c.String(http.StatusOK, string(responseJSON))
|
|
||||||
}
|
|
||||||
func getGithubApi(c *gin.Context) {
|
|
||||||
githubApiCount++
|
|
||||||
// 设置请求头
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.Unmarshal(response.Body(), &responseData)
|
|
||||||
if err != nil {
|
|
||||||
// 处理JSON解析错误
|
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "JSON parsing error"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func initConfig() Config {
|
|
||||||
// 读取配置文件
|
|
||||||
exePath, err := os.Executable()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
// 获取执行文件所在目录
|
|
||||||
exeDir := filepath.Dir(exePath)
|
|
||||||
configFile, err := os.Open(exeDir + "/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 incrementRequestCount() {
|
|
||||||
requestCount++
|
|
||||||
}
|
|
||||||
func incrementSuccessCount() {
|
|
||||||
successCount++
|
|
||||||
}
|
|
||||||
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 || requestDomain == "127.0.0.1" {
|
|
||||||
c.Next()
|
|
||||||
} else {
|
|
||||||
c.String(403, "Forbidden")
|
|
||||||
c.Abort()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
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]
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
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"`
|
||||||
|
DiyMsg string `json:"diyMsg"`
|
||||||
|
IsModMsg bool `json:"isModMsg"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// 全局变量
|
||||||
|
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
|
||||||
|
|
||||||
|
completionUrl = "https://copilot-proxy.githubusercontent.com/v1/engines/copilot-codex/completions"
|
||||||
|
telemetryUrl = "https://copilot-telemetry.githubusercontent.com/telemetry"
|
||||||
|
)
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
loadConfig() // 1.加载服务器配置文件
|
||||||
|
initGinEngine() // 2.初始化Gin引擎
|
||||||
|
initValidGhuTokenMap() // 3.初始化有效Ghu_token
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
Routes() // 1.url路由
|
||||||
|
StartServer() // 2.启动服务器
|
||||||
|
showMsg() // 3.控制台信息显示
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
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.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
+114
@@ -0,0 +1,114 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"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, _ := url.Parse(targetURL)
|
||||||
|
isModMsgEnabled := configFile.IsModMsg && targetURL == completionUrl
|
||||||
|
director := func(req *http.Request) {
|
||||||
|
req.URL.Host = target.Host
|
||||||
|
req.Host = target.Host
|
||||||
|
req.URL.Scheme = "https"
|
||||||
|
}
|
||||||
|
proxy := &httputil.ReverseProxy{
|
||||||
|
Director: director,
|
||||||
|
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {},
|
||||||
|
}
|
||||||
|
r.Any(routePath, func(c *gin.Context) {
|
||||||
|
if isModMsgEnabled {
|
||||||
|
modProxyResp(c)
|
||||||
|
} else {
|
||||||
|
proxy.ServeHTTP(c.Writer, c.Request)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func modProxyResp(c *gin.Context) {
|
||||||
|
c.Header("Content-Type", "text/plain;charset=utf-8")
|
||||||
|
c.Header("X-Forwarded-Proto", "http")
|
||||||
|
runes := []rune(configFile.DiyMsg)
|
||||||
|
for _, r := range runes {
|
||||||
|
modStr := fmt.Sprintf("data: {\"id\":\"cmpl-7xy1GLgssjHEubVrPyt534VRYVF0t\",\"model\":\"cushman-ml\",\"created\":1694526422,\"choices\":[{\"text\":\"%c\",\"index\":0,\"finish_reason\":null,\"logprobs\":null}]}\n", r)
|
||||||
|
c.String(200, modStr)
|
||||||
|
}
|
||||||
|
c.String(200, "data: [DONE]\n")
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
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
@@ -0,0 +1,86 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
START_SCRIPT_PATH="/usr/local/bin/scop"
|
|
||||||
rm -f "$START_SCRIPT_PATH"
|
|
||||||
echo "卸载完成!"
|
|
||||||
Reference in New Issue
Block a user