Skip to content

Commit

Permalink
1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
gooaclok819 committed Apr 12, 2024
1 parent b05589a commit f469872
Show file tree
Hide file tree
Showing 96 changed files with 653 additions and 121 deletions.
12 changes: 12 additions & 0 deletions api/mentu.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ func GetMenus(c *gin.Context) {
KeepAlive: true,
},
},
{
Path: "template",
Component: "subcription/template",
Name: "Template",
Meta: Meta{
Title: "templatelist",
Icon: "document",
Hidden: false,
Roles: []string{"ADMIN"},
KeepAlive: true,
},
},
},
},
}
Expand Down
14 changes: 13 additions & 1 deletion api/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,28 @@ import (
func NodeUpdadte(c *gin.Context) {
var node models.Node
name := c.PostForm("name")
oldname := c.PostForm("oldname")
oldlink := c.PostForm("oldlink")
link := c.PostForm("link")
if name == "" || link == "" {
c.JSON(400, gin.H{
"msg": "节点名称 or 备注不能为空",
})
return
}
// 查找旧节点
node.Name = oldname
node.Link = oldlink
err := node.Find()
if err != nil {
c.JSON(400, gin.H{
"msg": err.Error(),
})
return
}
node.Name = name
node.Link = link
err := node.Update()
err = node.Update()
if err != nil {
c.JSON(400, gin.H{
"msg": "更新失败",
Expand Down
136 changes: 136 additions & 0 deletions api/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package api

import (
"log"
"os"

"github.com/gin-gonic/gin"
)

type Temp struct {
File string `json:"file"`
Text string `json:"text"`
CreateDate string `json:"create_date"`
}

func GetTempS(c *gin.Context) {
files, err := os.ReadDir("./template")
if err != nil {
c.JSON(400, gin.H{
"msg": err.Error(),
})
return
}
var temps []Temp
for _, file := range files {
info, _ := file.Info()
time := info.ModTime().Format("2006-01-02 15:04:05")
text, _ := os.ReadFile("./template/" + file.Name())
temp := Temp{
File: file.Name(),
Text: string(text),
CreateDate: time,
}
temps = append(temps, temp)
}
if len(temps) == 0 {
c.JSON(200, gin.H{
"code": "00000",
"data": []string{},
"msg": "ok",
})
return
}
c.JSON(200, gin.H{
"code": "00000",
"data": temps,
"msg": "ok",
})
}
func UpdateTemp(c *gin.Context) {
filename := c.PostForm("filename")
oldname := c.PostForm("oldname")
text := c.PostForm("text")
err := os.Rename("./template/"+oldname, "./template/"+filename)
if err != nil {
log.Println(err)
c.JSON(400, gin.H{
"msg": "改名失败",
})
return
}
err = os.WriteFile("./template/"+filename, []byte(text), 0666)
if err != nil {
log.Println(err)
c.JSON(400, gin.H{
"msg": "修改失败",
})
return
}
c.JSON(200, gin.H{
"code": "00000",
"msg": "修改成功",
})

}
func AddTemp(c *gin.Context) {
filename := c.PostForm("filename")
text := c.PostForm("text")
if filename == "" || text == "" {
c.JSON(400, gin.H{
"msg": "文件名或者类型或内容不能为空",
})
return
}
// 检查文件是否存在
_, err := os.ReadFile("./template/" + filename)
if err == nil {
log.Println(err)
c.JSON(400, gin.H{
"msg": "文件已存在",
})
return
}
// 检查目录是否创建
_, err = os.Stat("./template/")
if err != nil {
if os.IsNotExist(err) {
os.Mkdir("./template/", os.ModePerm)
}
}
err = os.WriteFile("./template/"+filename, []byte(text), 0666)
if err != nil {
log.Println(err)
c.JSON(400, gin.H{
"msg": "上传失败",
})
return
}
c.JSON(200, gin.H{
"code": "00000",
"msg": "上传成功",
})
}
func DelTemp(c *gin.Context) {
filename := c.PostForm("filename")
_, err := os.ReadFile("./template/" + filename)
if err != nil {
log.Println(err)
c.JSON(400, gin.H{
"msg": "文件不存在",
})
return
}
err = os.Remove("./template/" + filename)
if err != nil {
log.Println(err)
c.JSON(400, gin.H{
"msg": "删除失败",
})
return
}
c.JSON(200, gin.H{
"code": "00000",
"msg": "删除成功",
})
}
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ func Templateinit() {
// 设置template路径

// 检查目录是否创建
_, err := os.Stat("./template")
_, err := os.Stat("./template/clash.yaml")
if err != nil {
if os.IsNotExist(err) {
os.Mkdir("./template", os.ModePerm)
os.MkdirAll("./template", os.ModePerm)
}
}
_, err = os.Stat("./template/clash.yaml")
Expand Down Expand Up @@ -77,6 +77,7 @@ func main() {
routers.Nodes(r)
routers.Clients(r)
routers.Total(r)
routers.Templates(r)
// 启动服务
r.Run(":8000")
}
2 changes: 1 addition & 1 deletion models/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (node *Node) Add() error {

// 更新节点
func (node *Node) Update() error {
return DB.Where("link = ? or name = ?", node.Link, node.Name).Updates(node).Error
return DB.Model(node).Updates(node).Error
}

// 查找节点是否重复
Expand Down
7 changes: 5 additions & 2 deletions node/clash.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func EncodeClash(urls []string, sqlconfig SqlConfig) ([]byte, error) {
Sni: trojan.Query.Sni,
Network: trojan.Query.Type,
Flow: trojan.Query.Flow,
Alpn: trojan.Query.Alpn,
Ws_opts: ws_opts,
Udp: sqlconfig.Udp,
Skip_cert_verify: sqlconfig.Cert,
Expand Down Expand Up @@ -247,6 +248,7 @@ func EncodeClash(urls []string, sqlconfig SqlConfig) ([]byte, error) {
Client_fingerprint: vless.Query.Fp,
Network: vless.Query.Type,
Flow: vless.Query.Flow,
Alpn: vless.Query.Alpn,
Ws_opts: ws_opts,
Reality_opts: reality_opts,
Udp: sqlconfig.Udp,
Expand All @@ -272,7 +274,7 @@ func EncodeClash(urls []string, sqlconfig SqlConfig) ([]byte, error) {
Auth_str: hy.Auth,
Up: hy.UpMbps,
Down: hy.DownMbps,
Alpn: []string{hy.ALPN},
Alpn: hy.ALPN,
Peer: hy.Peer,
Udp: sqlconfig.Udp,
Skip_cert_verify: sqlconfig.Cert,
Expand All @@ -295,6 +297,7 @@ func EncodeClash(urls []string, sqlconfig SqlConfig) ([]byte, error) {
Port: hy2.Port,
Auth_str: hy2.Auth,
Sni: hy2.Sni,
Alpn: hy2.ALPN,
Obfs: hy2.Obfs,
Password: hy2.Password,
Obfs_password: hy2.ObfsPassword,
Expand Down Expand Up @@ -324,7 +327,7 @@ func EncodeClash(urls []string, sqlconfig SqlConfig) ([]byte, error) {
Password: tuic.Password,
Uuid: tuic.Uuid,
Congestion_control: tuic.Congestion_control,
Alpn: []string{tuic.Alpn},
Alpn: tuic.Alpn,
Udp_relay_mode: tuic.Udp_relay_mode,
Disable_sni: disable_sni,
Sni: tuic.Sni,
Expand Down
31 changes: 27 additions & 4 deletions node/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,33 @@ func Base64Decode(s string) string {
}
}

// url.ParseQuery解析后还原得到原字符,然后解析base64编码
func Base64NameDecode(s string) string {
s = strings.ReplaceAll(s, " ", "+")
return Base64Decode(s)
// base64解码不自动补齐
func Base64Decode2(s string) string {
// 去除空格
s = strings.ReplaceAll(s, " ", "")
// 判断是否有特殊字符来判断是标准base64还是url base64
match, err := regexp.MatchString(`[_-]`, s)
if err != nil {
fmt.Println(err)
}
if !match {
// 默认使用标准解码
decoded, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return s // 返回原字符串
}
decoded_str := string(decoded)
return decoded_str

} else {
// 如果有特殊字符则使用URL解码
decoded, err := base64.URLEncoding.DecodeString(s)
if err != nil {
return s // 返回原字符串
}
decoded_str := string(decoded)
return decoded_str
}
}

// 检查环境
Expand Down
13 changes: 9 additions & 4 deletions node/hy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"strconv"
"strings"
)

type HY struct {
Expand All @@ -14,7 +15,7 @@ type HY struct {
Auth string
UpMbps int
DownMbps int
ALPN string
ALPN []string
Name string
}

Expand All @@ -28,7 +29,7 @@ func CallHy() {
Auth: "",
UpMbps: 11,
DownMbps: 55,
ALPN: "h3",
// ALPN: "h3",
}
fmt.Println(EncodeHYURL(hy))
}
Expand All @@ -50,7 +51,7 @@ func EncodeHYURL(hy HY) string {
q.Set("auth", hy.Auth)
q.Set("upmbps", strconv.Itoa(hy.UpMbps))
q.Set("downmbps", strconv.Itoa(hy.DownMbps))
q.Set("alpn", hy.ALPN)
// q.Set("alpn", hy.ALPN)
// 检查query是否有空值,有的话删除
for k, v := range q {
if v[0] == "" {
Expand All @@ -77,7 +78,11 @@ func DecodeHYURL(s string) (HY, error) {
auth := u.Query().Get("auth")
upMbps, _ := strconv.Atoi(u.Query().Get("upmbps"))
downMbps, _ := strconv.Atoi(u.Query().Get("downmbps"))
alpn := u.Query().Get("alpn")
alpns := u.Query().Get("alpn")
alpn := strings.Split(alpns, ",")
if alpns == "" {
alpn = nil
}
// 如果没有设置 Name,则使用 Fragment 作为 Name
name := u.Fragment
if name == "" {
Expand Down
Loading

0 comments on commit f469872

Please sign in to comment.