mirror of
https://github.com/bestnite/sub2sing-box.git
synced 2025-10-25 16:51:01 +00:00
feat: api server
This commit is contained in:
49
api/handler/convert.go
Normal file
49
api/handler/convert.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sub2sing-box/api/model"
|
||||
"sub2sing-box/internal"
|
||||
"sub2sing-box/pkg/util"
|
||||
|
||||
"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
|
||||
}
|
||||
j, err := internal.DecodeBase64(c.Query("data"))
|
||||
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
|
||||
}
|
||||
result, err := util.Convert(data.Subscriptions, data.Proxies, data.Template, data.Delete, data.Rename)
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.String(200, result)
|
||||
}
|
||||
9
api/model/convert.go
Normal file
9
api/model/convert.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package model
|
||||
|
||||
type ConvertRequest struct {
|
||||
Subscriptions []string `form:"subscription" json:"subscription"`
|
||||
Proxies []string `form:"proxy" json:"proxy"`
|
||||
Template string `form:"template" json:"template"`
|
||||
Delete string `form:"delete" json:"delete"`
|
||||
Rename map[string]string `form:"rename" json:"rename"`
|
||||
}
|
||||
22
api/server.go
Normal file
22
api/server.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sub2sing-box/api/handler"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RunServer(port uint16) {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.Default()
|
||||
|
||||
r.GET("/convert", handler.Convert)
|
||||
|
||||
fmt.Println("Server is running on port ", port)
|
||||
err := r.Run(":" + strconv.Itoa(int(port)))
|
||||
if err != nil {
|
||||
fmt.Println("Run server failed: ", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user