2024-03-13 01:30:45 -04:00
|
|
|
package handler
|
2023-09-12 06:40:24 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-09-17 01:10:13 -04:00
|
|
|
|
|
|
|
"github.com/nitezs/sub2clash/config"
|
|
|
|
"github.com/nitezs/sub2clash/model"
|
|
|
|
"github.com/nitezs/sub2clash/validator"
|
2023-11-02 14:35:30 -04:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"gopkg.in/yaml.v3"
|
2023-09-12 06:40:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func SubmodHandler(c *gin.Context) {
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2023-09-13 01:47:22 -04:00
|
|
|
query, err := validator.ParseQuery(c)
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusBadRequest, err.Error())
|
2023-09-12 06:40:24 -04:00
|
|
|
return
|
|
|
|
}
|
2023-09-16 09:46:59 -04:00
|
|
|
sub, err := BuildSub(model.Clash, query, config.Default.ClashTemplate)
|
2023-09-12 06:40:24 -04:00
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2023-09-25 03:43:21 -04:00
|
|
|
if query.NodeListMode {
|
|
|
|
nodelist := model.NodeList{}
|
|
|
|
nodelist.Proxies = sub.Proxies
|
|
|
|
marshal, err := yaml.Marshal(nodelist)
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, "YAML序列化失败: "+err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.String(http.StatusOK, string(marshal))
|
|
|
|
return
|
|
|
|
}
|
2023-09-13 01:47:22 -04:00
|
|
|
marshal, err := yaml.Marshal(sub)
|
2023-09-12 06:40:24 -04:00
|
|
|
if err != nil {
|
2023-09-13 01:47:22 -04:00
|
|
|
c.String(http.StatusInternalServerError, "YAML序列化失败: "+err.Error())
|
2023-09-12 06:40:24 -04:00
|
|
|
return
|
|
|
|
}
|
2023-09-13 01:47:22 -04:00
|
|
|
c.String(http.StatusOK, string(marshal))
|
2023-09-12 06:40:24 -04:00
|
|
|
}
|