-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
157 lines (131 loc) · 3.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"os"
"time"
)
type User struct {
UUID string `json:"uuid"`
AlterID int `json:"alterId"`
}
type Inbound struct {
Type string `json:"type"`
Listen string `json:"listen"`
ListenPort int `json:"listen_port"`
Users []User `json:"users"`
TCPFastOpen bool `json:"tcp_fast_open"`
UDPFragment bool `json:"udp_fragment"`
Sniff bool `json:"sniff"`
ProxyProtocol bool `json:"proxy_protocol"`
}
type Config struct {
Inbounds []Inbound `json:"inbounds"`
Outbounds []Outbound `json:"outbounds"`
}
type Outbound struct {
Type string `json:"type"`
}
func getLocalIP() (string, error) {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return "", err
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String(), nil
}
func generateVmessLink(inbound Inbound) string {
user := inbound.Users[0] // Assuming there's only one user in the inbound
// vmessLink := fmt.Sprintf("vmess://%s@%s:%d", user.UUID, inbound.Listen, inbound.ListenPort)
// Generate base64 encoded JSON with necessary settings
settingsJSON := fmt.Sprintf(`{
"v": "2",
"ps": "vmess",
"add": "%s",
"port": "%d",
"id": "%s",
"aid": %d,
"net": "tcp",
"type": "none",
"host": "",
"path": "",
"tls": ""
}`, inbound.Listen, inbound.ListenPort, user.UUID, user.AlterID)
// Base64 encode the settings
settingsBase64 := base64.StdEncoding.EncodeToString([]byte(settingsJSON))
// Append settings to the vmess link
vmessLink := fmt.Sprintf("vmess://%s", settingsBase64)
return vmessLink
}
func generateAndWriteVmessLinks() {
// 获取本机 IP 地址
ip, err := getLocalIP()
if err != nil {
fmt.Println("Error getting local IP:", err)
return
}
// 打开 JSON 文件
file, err := os.Open("/usr/local/etc/sing-box/config.json")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
// 读取文件内容
byteValue, err := io.ReadAll(file)
if err != nil {
fmt.Println("Error reading file:", err)
return
}
// 解析 JSON
var config Config
err = json.Unmarshal(byteValue, &config)
if err != nil {
fmt.Println("Error decoding JSON:", err)
return
}
// 创建文件用于写入 VMESS 链接
outputFile, err := os.Create("/file/vmess_links.txt")
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer outputFile.Close()
// 生成 VMESS 链接并写入文件
for _, inbound := range config.Inbounds {
if inbound.Type == "vmess" {
inbound.Listen = ip
vmessLink := generateVmessLink(inbound)
_, err := outputFile.WriteString(vmessLink + "\n")
if err != nil {
fmt.Println("Error writing to file:", err)
return
}
}
}
}
func runDaily(task func()) {
// 获取当前时间
now := time.Now()
// 计算下一个执行时间点
next := now.Add(24 * time.Hour) // 每天运行一次
// 计算距离下一个执行时间点的等待时间
waitDuration := next.Sub(now)
// 创建定时器
timer := time.NewTimer(waitDuration)
// 等待定时器触发
<-timer.C
// 执行任务
task()
}
func main() {
// 第一次运行时,运行一次函数
generateAndWriteVmessLinks()
runDaily(func() {
generateAndWriteVmessLinks()
})
}