1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-24 23:34:43 -05:00
sub2sing-box/common/convert.go

329 lines
7.4 KiB
Go
Raw Normal View History

2024-03-20 08:54:23 -04:00
package common
2024-03-11 11:39:58 -04:00
import (
"encoding/json"
"errors"
2024-03-11 13:34:08 -04:00
"fmt"
2024-03-11 11:39:58 -04:00
"os"
2024-03-18 10:32:33 -04:00
"path/filepath"
2024-03-11 11:39:58 -04:00
"regexp"
2024-03-19 09:02:53 -04:00
"sort"
2024-03-11 11:39:58 -04:00
"strings"
2024-03-20 08:54:23 -04:00
model2 "sub2sing-box/model"
"sub2sing-box/parser"
"sub2sing-box/util"
2024-03-11 11:39:58 -04:00
)
2024-03-19 09:02:53 -04:00
func Convert(
subscriptions []string,
proxies []string,
template string,
delete string,
rename map[string]string,
group bool,
groupType string,
sortKey string,
sortType string,
) (string, error) {
2024-03-11 13:34:08 -04:00
result := ""
var err error
proxyList, err := ConvertSubscriptionsToSProxy(subscriptions)
if err != nil {
return "", err
}
for _, proxy := range proxies {
p, err := ConvertCProxyToSProxy(proxy)
if err != nil {
return "", err
}
proxyList = append(proxyList, p)
}
if delete != "" {
proxyList, err = DeleteProxy(proxyList, delete)
if err != nil {
return "", err
}
}
for k, v := range rename {
proxyList, err = RenameProxy(proxyList, k, v)
if err != nil {
return "", err
}
}
keep := make(map[int]bool)
set := make(map[string]struct {
2024-03-20 08:54:23 -04:00
Proxy model2.Proxy
2024-03-11 13:34:08 -04:00
Count int
})
for i, p := range proxyList {
if _, exists := set[p.Tag]; !exists {
keep[i] = true
set[p.Tag] = struct {
2024-03-20 08:54:23 -04:00
Proxy model2.Proxy
2024-03-11 13:34:08 -04:00
Count int
}{p, 0}
} else {
p1, _ := json.Marshal(p)
p2, _ := json.Marshal(set[p.Tag])
if string(p1) != string(p2) {
set[p.Tag] = struct {
2024-03-20 08:54:23 -04:00
Proxy model2.Proxy
2024-03-11 13:34:08 -04:00
Count int
}{p, set[p.Tag].Count + 1}
keep[i] = true
proxyList[i].Tag = fmt.Sprintf("%s %d", p.Tag, set[p.Tag].Count)
} else {
keep[i] = false
}
}
}
2024-03-20 08:54:23 -04:00
var newProxyList []model2.Proxy
2024-03-11 13:34:08 -04:00
for i, p := range proxyList {
if keep[i] {
newProxyList = append(newProxyList, p)
}
}
proxyList = newProxyList
2024-03-20 08:54:23 -04:00
var outbounds []model2.Outbound
2024-03-19 09:02:53 -04:00
ps, err := json.Marshal(&proxyList)
if err != nil {
return "", err
}
err = json.Unmarshal(ps, &outbounds)
if err != nil {
return "", err
}
if group {
outbounds = AddCountryGroup(outbounds, groupType, sortKey, sortType)
}
2024-03-11 13:34:08 -04:00
if template != "" {
2024-03-19 09:02:53 -04:00
result, err = MergeTemplate(outbounds, template)
2024-03-11 13:34:08 -04:00
if err != nil {
return "", err
}
} else {
2024-03-19 09:02:53 -04:00
r, err := json.Marshal(outbounds)
2024-03-11 13:34:08 -04:00
result = string(r)
if err != nil {
return "", err
}
}
return string(result), nil
}
2024-03-20 08:54:23 -04:00
func AddCountryGroup(proxies []model2.Outbound, groupType string, sortKey string, sortType string) []model2.Outbound {
newGroup := make(map[string]model2.Outbound)
2024-03-19 09:02:53 -04:00
for _, p := range proxies {
if p.Type != "selector" && p.Type != "urltest" {
2024-03-20 08:54:23 -04:00
country := model2.GetContryName(p.Tag)
2024-03-19 09:02:53 -04:00
if group, ok := newGroup[country]; ok {
group.Outbounds = append(group.Outbounds, p.Tag)
newGroup[country] = group
} else {
2024-03-20 08:54:23 -04:00
newGroup[country] = model2.Outbound{
2024-03-19 09:02:53 -04:00
Tag: country,
Type: groupType,
Outbounds: []string{p.Tag},
InterruptExistConnections: true,
}
}
}
}
2024-03-20 08:54:23 -04:00
var groups []model2.Outbound
2024-03-19 09:02:53 -04:00
for _, p := range newGroup {
groups = append(groups, p)
}
if sortType == "asc" {
switch sortKey {
case "tag":
2024-03-20 08:54:23 -04:00
sort.Sort(model2.SortByTag(groups))
2024-03-19 09:02:53 -04:00
case "num":
2024-03-20 08:54:23 -04:00
sort.Sort(model2.SortByNumber(groups))
2024-03-19 09:02:53 -04:00
default:
2024-03-20 08:54:23 -04:00
sort.Sort(model2.SortByTag(groups))
2024-03-19 09:02:53 -04:00
}
} else {
switch sortKey {
case "tag":
2024-03-20 08:54:23 -04:00
sort.Sort(sort.Reverse(model2.SortByTag(groups)))
2024-03-19 09:02:53 -04:00
case "num":
2024-03-20 08:54:23 -04:00
sort.Sort(sort.Reverse(model2.SortByNumber(groups)))
2024-03-19 09:02:53 -04:00
default:
2024-03-20 08:54:23 -04:00
sort.Sort(sort.Reverse(model2.SortByTag(groups)))
2024-03-19 09:02:53 -04:00
}
}
return append(proxies, groups...)
}
2024-03-20 08:54:23 -04:00
func MergeTemplate(outbounds []model2.Outbound, template string) (string, error) {
var config model2.Config
var err error
if strings.HasPrefix(template, "http") {
data, err := util.Fetch(template, 3)
if err != nil {
return "", err
}
err = json.Unmarshal([]byte(data), &config)
if err != nil {
return "", err
}
} else {
if !strings.Contains(template, string(filepath.Separator)) {
if _, err := os.Stat(template); os.IsNotExist(err) {
template = filepath.Join("templates", template)
}
2024-03-18 10:32:33 -04:00
}
config, err = ReadTemplate(template)
2024-03-18 10:32:33 -04:00
}
2024-03-11 11:39:58 -04:00
proxyTags := make([]string, 0)
2024-03-19 09:02:53 -04:00
groupTags := make([]string, 0)
2024-03-20 08:54:23 -04:00
groups := make(map[string]model2.Outbound)
2024-03-11 11:39:58 -04:00
if err != nil {
return "", err
}
2024-03-19 09:02:53 -04:00
for _, p := range outbounds {
2024-03-20 08:54:23 -04:00
if model2.IsCountryGroup(p.Tag) {
2024-03-19 09:02:53 -04:00
groupTags = append(groupTags, p.Tag)
reg := regexp.MustCompile("[A-Za-z]{2}")
country := reg.FindString(p.Tag)
groups[country] = p
} else {
proxyTags = append(proxyTags, p.Tag)
}
2024-03-11 11:39:58 -04:00
}
2024-03-19 09:02:53 -04:00
reg := regexp.MustCompile("<[A-Za-z]{2}>")
2024-03-11 11:39:58 -04:00
for i, outbound := range config.Outbounds {
2024-03-18 10:32:33 -04:00
var parsedOutbound []string = make([]string, 0)
for _, o := range outbound.Outbounds {
if o == "<all-proxy-tags>" {
parsedOutbound = append(parsedOutbound, proxyTags...)
2024-03-19 09:02:53 -04:00
} else if o == "<all-country-tags>" {
parsedOutbound = append(parsedOutbound, groupTags...)
} else if reg.MatchString(o) {
country := strings.ToUpper(strings.Trim(reg.FindString(o), "<>"))
if group, ok := groups[country]; ok {
parsedOutbound = append(parsedOutbound, group.Outbounds...)
}
2024-03-18 10:32:33 -04:00
} else {
parsedOutbound = append(parsedOutbound, o)
2024-03-11 11:39:58 -04:00
}
}
2024-03-18 10:32:33 -04:00
config.Outbounds[i].Outbounds = parsedOutbound
2024-03-11 11:39:58 -04:00
}
2024-03-19 09:02:53 -04:00
config.Outbounds = append(config.Outbounds, outbounds...)
2024-03-11 11:39:58 -04:00
data, err := json.Marshal(config)
if err != nil {
return "", err
}
return string(data), nil
}
2024-03-20 08:54:23 -04:00
func ConvertCProxyToSProxy(proxy string) (model2.Proxy, error) {
2024-03-11 11:39:58 -04:00
for prefix, parseFunc := range parser.ParserMap {
if strings.HasPrefix(proxy, prefix) {
proxy, err := parseFunc(proxy)
if err != nil {
2024-03-20 08:54:23 -04:00
return model2.Proxy{}, err
2024-03-11 11:39:58 -04:00
}
return proxy, nil
}
}
2024-03-20 08:54:23 -04:00
return model2.Proxy{}, errors.New("Unknown proxy format")
2024-03-11 11:39:58 -04:00
}
func ConvertCProxyToJson(proxy string) (string, error) {
sProxy, err := ConvertCProxyToSProxy(proxy)
if err != nil {
return "", err
}
data, err := json.Marshal(&sProxy)
if err != nil {
return "", err
}
return string(data), nil
}
2024-03-20 08:54:23 -04:00
func ConvertSubscriptionsToSProxy(urls []string) ([]model2.Proxy, error) {
proxyList := make([]model2.Proxy, 0)
2024-03-11 11:39:58 -04:00
for _, url := range urls {
data, err := util.Fetch(url, 3)
2024-03-11 11:39:58 -04:00
if err != nil {
return nil, err
}
proxy, err := util.DecodeBase64(data)
2024-03-11 11:39:58 -04:00
if err != nil {
return nil, err
}
proxies := strings.Split(proxy, "\n")
for _, p := range proxies {
for prefix, parseFunc := range parser.ParserMap {
if strings.HasPrefix(p, prefix) {
proxy, err := parseFunc(p)
if err != nil {
return nil, err
}
proxyList = append(proxyList, proxy)
}
}
}
}
return proxyList, nil
}
func ConvertSubscriptionsToJson(urls []string) (string, error) {
proxyList, err := ConvertSubscriptionsToSProxy(urls)
if err != nil {
return "", err
}
result, err := json.Marshal(proxyList)
if err != nil {
return "", err
}
return string(result), nil
}
2024-03-20 08:54:23 -04:00
func ReadTemplate(path string) (model2.Config, error) {
2024-03-11 11:39:58 -04:00
data, err := os.ReadFile(path)
if err != nil {
2024-03-20 08:54:23 -04:00
return model2.Config{}, err
2024-03-11 11:39:58 -04:00
}
2024-03-20 08:54:23 -04:00
var res model2.Config
2024-03-11 11:39:58 -04:00
err = json.Unmarshal(data, &res)
if err != nil {
2024-03-20 08:54:23 -04:00
return model2.Config{}, err
2024-03-11 11:39:58 -04:00
}
return res, nil
}
2024-03-20 08:54:23 -04:00
func DeleteProxy(proxies []model2.Proxy, regex string) ([]model2.Proxy, error) {
2024-03-11 11:39:58 -04:00
reg, err := regexp.Compile(regex)
if err != nil {
return nil, err
}
2024-03-20 08:54:23 -04:00
var newProxies []model2.Proxy
2024-03-11 11:39:58 -04:00
for _, p := range proxies {
if !reg.MatchString(p.Tag) {
newProxies = append(newProxies, p)
}
}
return newProxies, nil
}
2024-03-20 08:54:23 -04:00
func RenameProxy(proxies []model2.Proxy, regex string, replaceText string) ([]model2.Proxy, error) {
2024-03-11 11:39:58 -04:00
reg, err := regexp.Compile(regex)
if err != nil {
return nil, err
}
for i, p := range proxies {
if reg.MatchString(p.Tag) {
proxies[i].Tag = reg.ReplaceAllString(p.Tag, replaceText)
}
}
return proxies, nil
}