feat(adapter): add support for additional parameters in Adapter and Client structs
This commit is contained in:
parent
8436c2d6ee
commit
77a5373d44
@ -8,16 +8,24 @@ import (
|
||||
)
|
||||
|
||||
type Adapter struct {
|
||||
Type string // 协议类型
|
||||
SiteName string // 站点名称
|
||||
Servers []*node.Node // 服务器列表
|
||||
UserInfo User // 用户信息
|
||||
ClientTemplate string // 客户端配置模板
|
||||
OutputFormat string // 输出格式,默认是 base64
|
||||
SubscribeName string // 订阅名称
|
||||
Params map[string]string // 其他参数
|
||||
}
|
||||
|
||||
type Option func(*Adapter)
|
||||
|
||||
func WithParams(params map[string]string) Option {
|
||||
return func(opts *Adapter) {
|
||||
opts.Params = params
|
||||
}
|
||||
}
|
||||
|
||||
// WithServers 设置服务器列表
|
||||
func WithServers(servers []*node.Node) Option {
|
||||
return func(opts *Adapter) {
|
||||
@ -76,6 +84,7 @@ func (adapter *Adapter) Client() (*Client, error) {
|
||||
OutputFormat: adapter.OutputFormat,
|
||||
Proxies: []Proxy{},
|
||||
UserInfo: adapter.UserInfo,
|
||||
Params: adapter.Params,
|
||||
}
|
||||
|
||||
proxies, err := adapter.Proxies(adapter.Servers)
|
||||
@ -101,7 +110,9 @@ func (adapter *Adapter) Proxies(servers []*node.Node) ([]Proxy, error) {
|
||||
}
|
||||
for _, protocol := range protocols {
|
||||
if protocol.Type == item.Protocol {
|
||||
proxies = append(proxies, Proxy{
|
||||
proxies = append(
|
||||
proxies,
|
||||
Proxy{
|
||||
Sort: item.Sort,
|
||||
Name: item.Name,
|
||||
Server: item.Address,
|
||||
@ -149,7 +160,8 @@ func (adapter *Adapter) Proxies(servers []*node.Node) ([]Proxy, error) {
|
||||
CertMode: protocol.CertMode,
|
||||
CertDNSProvider: protocol.CertDNSProvider,
|
||||
CertDNSEnv: protocol.CertDNSEnv,
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,6 +99,7 @@ type Client struct {
|
||||
OutputFormat string // json, yaml, etc.
|
||||
Proxies []Proxy // List of proxy configurations
|
||||
UserInfo User // User information
|
||||
Params map[string]string // Additional parameters
|
||||
}
|
||||
|
||||
func (c *Client) Build() ([]byte, error) {
|
||||
@ -119,6 +120,7 @@ func (c *Client) Build() ([]byte, error) {
|
||||
"OutputFormat": c.OutputFormat,
|
||||
"Proxies": proxies,
|
||||
"UserInfo": c.UserInfo,
|
||||
"Params": c.Params,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -23,6 +23,10 @@ func SubscribeHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
|
||||
ua := c.GetHeader("User-Agent")
|
||||
req.UA = c.Request.Header.Get("User-Agent")
|
||||
req.Flag = c.Query("flag")
|
||||
req.Type = c.Query("type")
|
||||
// 获取所有查询参数
|
||||
req.Params = getQueryMap(c.Request)
|
||||
|
||||
if svcCtx.Config.Subscribe.PanDomain {
|
||||
domain := c.Request.Host
|
||||
domainArr := strings.Split(domain, ".")
|
||||
@ -94,3 +98,14 @@ func RegisterSubscribeHandlers(router *gin.Engine, serverCtx *svc.ServiceContext
|
||||
}
|
||||
router.GET(path, SubscribeHandler(serverCtx))
|
||||
}
|
||||
|
||||
// GetQueryMap 将 http.Request 的查询参数转换为 map[string]string
|
||||
func getQueryMap(r *http.Request) map[string]string {
|
||||
result := make(map[string]string)
|
||||
for k, v := range r.URL.Query() {
|
||||
if len(v) > 0 {
|
||||
result[k] = v[0]
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@ -108,6 +108,7 @@ func (l *SubscribeLogic) Handler(req *types.SubscribeRequest) (resp *types.Subsc
|
||||
Traffic: userSubscribe.Traffic,
|
||||
SubscribeURL: l.getSubscribeV2URL(req.Token),
|
||||
}),
|
||||
adapter.WithParams(req.Params),
|
||||
)
|
||||
|
||||
// Get client config
|
||||
|
||||
@ -4,7 +4,9 @@ type (
|
||||
SubscribeRequest struct {
|
||||
Flag string
|
||||
Token string
|
||||
Type string
|
||||
UA string
|
||||
Params map[string]string
|
||||
}
|
||||
SubscribeResponse struct {
|
||||
Config []byte
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user