[clash] default proxy and proxy-group support #1066
This commit is contained in:
+27
-10
@@ -86,16 +86,15 @@ func (s *ClashService) GetClash(subId string) (*string, []string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
othersStr, err := s.getClashConfig()
|
basicConfig, err := s.getClashConfig()
|
||||||
if err != nil || len(othersStr) == 0 {
|
if err != nil || len(basicConfig) == 0 {
|
||||||
othersStr = basicClashConfig
|
basicConfig = basicClashConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := s.ConvertToClashMeta(outbounds)
|
resultStr, err := s.ConvertToClashMeta(outbounds, basicConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
resultStr := othersStr + "\n" + string(result)
|
|
||||||
|
|
||||||
updateInterval, _ := s.SettingService.GetSubUpdates()
|
updateInterval, _ := s.SettingService.GetSubUpdates()
|
||||||
headers := util.GetHeaders(client, updateInterval)
|
headers := util.GetHeaders(client, updateInterval)
|
||||||
@@ -112,7 +111,7 @@ func (s *ClashService) getClashConfig() (string, error) {
|
|||||||
return subClashExt, nil
|
return subClashExt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ClashService) ConvertToClashMeta(outbounds *[]map[string]interface{}) ([]byte, error) {
|
func (s *ClashService) ConvertToClashMeta(outbounds *[]map[string]interface{}, basicConfig string) (string, error) {
|
||||||
var proxies []interface{}
|
var proxies []interface{}
|
||||||
proxyTags := make([]string, 0)
|
proxyTags := make([]string, 0)
|
||||||
for _, obMap := range *outbounds {
|
for _, obMap := range *outbounds {
|
||||||
@@ -368,10 +367,28 @@ func (s *ClashService) ConvertToClashMeta(outbounds *[]map[string]interface{}) (
|
|||||||
proxyGroups[1]["proxies"] = proxyTags
|
proxyGroups[1]["proxies"] = proxyTags
|
||||||
proxyGroups[0]["proxies"] = append([]string{proxyGroups[1]["name"].(string)}, proxyTags...)
|
proxyGroups[0]["proxies"] = append([]string{proxyGroups[1]["name"].(string)}, proxyTags...)
|
||||||
|
|
||||||
output := map[string]interface{}{
|
// Merge proxies and proxy groups if exist
|
||||||
"proxies": proxies,
|
var output map[string]interface{}
|
||||||
"proxy-groups": proxyGroups,
|
err = yaml.Unmarshal([]byte(basicConfig), &output)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return yaml.Marshal(output)
|
if p, ok := output["proxies"].([]interface{}); ok {
|
||||||
|
output["proxies"] = append(p, proxies...)
|
||||||
|
} else {
|
||||||
|
output["proxies"] = proxies
|
||||||
|
}
|
||||||
|
|
||||||
|
if pg, ok := output["proxy-groups"].([]interface{}); ok {
|
||||||
|
output["proxy-groups"] = append(pg, proxyGroups[0], proxyGroups[1])
|
||||||
|
} else {
|
||||||
|
output["proxy-groups"] = proxyGroups
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := yaml.Marshal(output)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(result), nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user