mirror of
https://github.com/bestnite/sub2sing-box.git
synced 2025-06-16 10:03:18 +08:00
feat: api server
This commit is contained in:
@ -1,10 +1,8 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"sub2sing-box/internal/model"
|
||||
. "sub2sing-box/pkg/util"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@ -34,87 +32,11 @@ var convertCmd = &cobra.Command{
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
result := ""
|
||||
var err error
|
||||
|
||||
proxyList, err := ConvertSubscriptionsToSProxy(subscriptions)
|
||||
result, err = Convert(subscriptions, proxies, template, delete, rename)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
for _, proxy := range proxies {
|
||||
p, err := ConvertCProxyToSProxy(proxy)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
proxyList = append(proxyList, p)
|
||||
}
|
||||
|
||||
if delete != "" {
|
||||
proxyList, err = DeleteProxy(proxyList, delete)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for k, v := range rename {
|
||||
proxyList, err = RenameProxy(proxyList, k, v)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
keep := make(map[int]bool)
|
||||
set := make(map[string]struct {
|
||||
Proxy model.Proxy
|
||||
Count int
|
||||
})
|
||||
for i, p := range proxyList {
|
||||
if _, exists := set[p.Tag]; !exists {
|
||||
keep[i] = true
|
||||
set[p.Tag] = struct {
|
||||
Proxy model.Proxy
|
||||
Count int
|
||||
}{p, 0}
|
||||
} else {
|
||||
p1, _ := json.Marshal(p)
|
||||
p2, _ := json.Marshal(set[p.Tag])
|
||||
if string(p1) != string(p2) {
|
||||
set[p.Tag] = struct {
|
||||
Proxy model.Proxy
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
var newProxyList []model.Proxy
|
||||
for i, p := range proxyList {
|
||||
if keep[i] {
|
||||
newProxyList = append(newProxyList, p)
|
||||
}
|
||||
}
|
||||
proxyList = newProxyList
|
||||
|
||||
if template != "" {
|
||||
result, err = MergeTemplate(proxyList, template)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
r, err := json.Marshal(proxyList)
|
||||
result = string(r)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if output != "" {
|
||||
err = os.WriteFile(output, []byte(result), 0666)
|
||||
if err != nil {
|
||||
|
23
cmd/server.go
Normal file
23
cmd/server.go
Normal file
@ -0,0 +1,23 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"sub2sing-box/api"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var port uint16
|
||||
|
||||
func init() {
|
||||
runCmd.Flags().Uint16VarP(&port, "port", "p", 8080, "server port")
|
||||
RootCmd.AddCommand(runCmd)
|
||||
}
|
||||
|
||||
var runCmd = &cobra.Command{
|
||||
Use: "server",
|
||||
Long: "Run the server",
|
||||
Short: "Run the server",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
api.RunServer(port)
|
||||
},
|
||||
}
|
Reference in New Issue
Block a user