Skip to content

Commit a18736a

Browse files
author
v_frgfeng
committed
✨ copilot proxy
1 parent f352d0a commit a18736a

File tree

10 files changed

+73
-11
lines changed

10 files changed

+73
-11
lines changed

api/common/common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package common
2+
3+
type CommonApi struct {
4+
}

api/copilot/copilot.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,31 @@ func ClearAuthCount() {
478478
}
479479
}
480480
}
481+
482+
var pacContent string
483+
var pacTpl = `function FindProxyForURL(url, host) {
484+
if (host == 'githubusercontent.com' || host == 'githubcopilot.com' || host == 'cocopilot.net' || host == 'google.com') {
485+
return '%s'
486+
}
487+
return 'DIRECT'
488+
}
489+
`
490+
var directPac = `function FindProxyForURL(url, host) {
491+
return 'DIRECT'
492+
}
493+
`
494+
495+
func (co *CopilotApi) ProxyPacHandler(ctx *gin.Context) {
496+
proxy := global.Config.Copilot.Proxy.HTTP
497+
if pacContent == "" && proxy != "" {
498+
pacContent = fmt.Sprintf(pacTpl, proxy)
499+
}
500+
if pacContent == "" {
501+
pacContent = directPac
502+
}
503+
if proxy != "" && pacContent == directPac {
504+
pacContent = fmt.Sprintf(pacTpl, proxy)
505+
}
506+
ctx.Header("Content-Type", "application/x-ns-proxy-autoconfig")
507+
ctx.String(http.StatusOK, pacContent)
508+
}

api/enter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"github.com/dalefengs/chat-api-proxy/api/common"
45
"github.com/dalefengs/chat-api-proxy/api/copilot"
56
"github.com/dalefengs/chat-api-proxy/api/genai"
67
"github.com/dalefengs/chat-api-proxy/api/openai"
@@ -10,6 +11,7 @@ type ApiGroup struct {
1011
copilot.CopilotApi
1112
openai.OpenApi
1213
genai.GenApi
14+
common.CommonApi
1315
}
1416

1517
var ApiGroupApp = new(ApiGroup)

config.docker.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ copilot:
2121

2222
# cocopilot
2323
coTokenUrl: https://api.cocopilot.net/copilot_internal/v2/token
24+
proxy:
25+
HTTP: ""
26+
HTTPS: ""
2427

28+
2529
gemini:
2630
# https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${API_KEY}
2731
# https://api-gm.xfjy.in

config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ copilot:
2222
# cocopilot
2323
coTokenUrl: https://api.cocopilot.net/copilot_internal/v2/token
2424

25+
proxy:
26+
HTTP: ""
27+
HTTPS: ""
28+
2529
gemini:
2630
# https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${API_KEY}
2731
# https://api-gm.xfjy.in

core/environment.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ func InitEnvironmentVar() {
2222
if env := os.Getenv("CO_TOKEN_URL"); env != "" {
2323
global.Config.Copilot.CoTokenURL = env
2424
}
25+
if env := os.Getenv("HTTP_PROXY"); env != "" {
26+
global.Config.Copilot.Proxy.HTTP = env
27+
}
28+
if env := os.Getenv("HTTPS_PROXY"); env != "" {
29+
global.Config.Copilot.Proxy.HTTPS = env
30+
}
2531
}

global/config/copilot.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ type Copilot struct {
44
TokenURL string `yaml:"tokenUrl"`
55
CoTokenURL string `yaml:"coTokenUrl"`
66
CompletionsURL string `yaml:"completionsUrl"`
7+
Proxy Proxy `yaml:"proxy"`
78
}

global/config/system.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ type System struct {
55
Port int `mapstructure:"port" json:"port" yaml:"port"` // 端口值
66
RouterPrefix string `mapstructure:"router-prefix" json:"router-prefix" yaml:"router-prefix"`
77
}
8+
9+
type Proxy struct {
10+
HTTP string `mapstructure:"http" json:"http" yaml:"http"`
11+
HTTPS string `mapstructure:"https" json:"https" yaml:"https"`
12+
}

router/copilot.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,13 @@ import (
55
"github.com/gin-gonic/gin"
66
)
77

8-
func (r *Router) InitRouter(Router *gin.RouterGroup) (R gin.IRoutes) {
9-
baseRouter := Router.Group("")
10-
copilotApi := api.ApiGroupApp.CopilotApi
11-
{
12-
baseRouter.GET("/copilot_internal/v2/token", copilotApi.CoTokenHandler)
13-
baseRouter.POST("/v1/chat/completions", copilotApi.CompletionsHandler)
14-
baseRouter.POST("/chat/completions", copilotApi.CompletionsOfficialHandler)
15-
}
16-
return baseRouter
17-
}
18-
198
func (r *Router) InitCopilotRouter(Router *gin.RouterGroup) (R gin.IRoutes) {
209
baseRouter := Router.Group("copilot")
2110
copilotApi := api.ApiGroupApp.CopilotApi
2211
{
2312
baseRouter.GET("/copilot_internal/v2/token", copilotApi.TokenHandler) // 官方获取 token
2413
baseRouter.POST("/v1/chat/completions", copilotApi.CompletionsHandler)
14+
baseRouter.GET("/proxy.pac", copilotApi.ProxyPacHandler) // 自动配置代理
2515
}
2616
return baseRouter
2717
}

router/router.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package router
2+
3+
import (
4+
"github.com/dalefengs/chat-api-proxy/api"
5+
"github.com/gin-gonic/gin"
6+
)
7+
8+
func (r *Router) InitRouter(Router *gin.RouterGroup) (R gin.IRoutes) {
9+
baseRouter := Router.Group("")
10+
copilotApi := api.ApiGroupApp.CopilotApi
11+
_ = api.ApiGroupApp.CommonApi
12+
{
13+
baseRouter.GET("/copilot_internal/v2/token", copilotApi.CoTokenHandler)
14+
baseRouter.POST("/v1/chat/completions", copilotApi.CompletionsHandler)
15+
baseRouter.POST("/chat/completions", copilotApi.CompletionsOfficialHandler)
16+
}
17+
return baseRouter
18+
}

0 commit comments

Comments
 (0)