1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-25 00:14:42 -05:00
sub2sing-box/api/handler/convert.go

61 lines
1.1 KiB
Go
Raw Normal View History

2024-03-11 13:34:08 -04:00
package handler
import (
"encoding/json"
2024-09-19 06:12:24 -04:00
"github.com/nitezs/sub2sing-box/common"
"github.com/nitezs/sub2sing-box/model"
"github.com/nitezs/sub2sing-box/util"
2024-03-11 13:34:08 -04:00
"github.com/gin-gonic/gin"
)
func Convert(c *gin.Context) {
c.Header("Content-Type", "application/json")
if c.Query("data") == "" {
c.JSON(400, gin.H{
"error": "Missing data parameter",
})
return
}
2024-03-22 04:10:15 -04:00
j, err := util.DecodeBase64(c.Query("data"))
2024-03-11 13:34:08 -04:00
if err != nil {
c.JSON(400, gin.H{
"error": "Invalid data",
})
return
}
var data model.ConvertRequest
err = json.Unmarshal([]byte(j), &data)
if err != nil {
c.JSON(400, gin.H{
"error": err.Error(),
})
return
}
if data.Proxies == nil && data.Subscriptions == nil {
c.JSON(400, gin.H{
"error": "Must provide at least one subscription or proxy",
})
return
}
2024-03-20 08:54:23 -04:00
result, err := common.Convert(
2024-03-19 09:02:53 -04:00
data.Subscriptions,
data.Proxies,
data.Template,
data.Delete,
data.Rename,
data.Group,
2024-03-19 09:02:53 -04:00
data.GroupType,
data.SortKey,
data.SortType,
)
2024-03-11 13:34:08 -04:00
if err != nil {
c.JSON(400, gin.H{
"error": err.Error(),
})
return
}
c.String(200, result)
}