mirror of
https://gitee.com/chuangxxt/share-copilot
synced 2026-12-03 00:07:51 +00:00
增加代码提示修改功能
This commit is contained in:
@@ -39,3 +39,11 @@ var (
|
||||
//请求成功计数
|
||||
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 = false
|
||||
)
|
||||
|
||||
@@ -2,6 +2,15 @@ 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)
|
||||
}
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
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"
|
||||
)
|
||||
|
||||
@@ -76,3 +81,61 @@ func diyBadRequest(c *gin.Context, code int, errorMessage string) {
|
||||
"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.")
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user