-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
176 lines (148 loc) · 4.34 KB
/
main.go
File metadata and controls
176 lines (148 loc) · 4.34 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"gopkg.in/yaml.v3"
)
var verbose bool
func main() {
urlFlag := flag.String("url", "", "subscription URL (required)")
outputFlag := flag.String("o", "", "output file path (default: stdout)")
templateFlag := flag.String("template", "", "custom template YAML to override dns/rules/proxy-groups")
verboseFlag := flag.Bool("v", false, "verbose logging")
flag.Parse()
verbose = *verboseFlag
var rawContent string
if *urlFlag != "" {
logVerbose("Fetching subscription from: %s", *urlFlag)
content, err := FetchSubscription(*urlFlag)
if err != nil {
log.Fatalf("Failed to fetch subscription: %v", err)
}
rawContent = content
} else {
// Try reading from stdin
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
data, err := io.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("Failed to read stdin: %v", err)
}
rawContent = string(data)
} else {
// No URL flag and no stdin pipe — enter interactive mode
runInteractive()
return
}
}
links, err := DecodeSubscription(rawContent)
if err != nil {
log.Fatalf("Failed to decode subscription: %v", err)
}
logVerbose("Decoded %d links", len(links))
proxies := ParseLinks(links)
if len(proxies) == 0 {
log.Fatal("No valid proxies found in subscription")
}
logVerbose("Parsed %d proxies", len(proxies))
for i, p := range proxies {
logVerbose(" [%d] %s (%s) %s:%d", i+1, p.Tag, p.Type, p.Server, p.Port)
}
generateAndWrite(proxies, *outputFlag, *templateFlag)
}
func generateAndWrite(proxies []Proxy, outputPath, templatePath string) {
cfg := GenerateConfig(proxies)
if templatePath != "" {
logVerbose("Applying template: %s", templatePath)
if err := MergeTemplate(&cfg, templatePath); err != nil {
log.Fatalf("Failed to apply template: %v", err)
}
}
output, err := yaml.Marshal(&cfg)
if err != nil {
log.Fatalf("Failed to marshal YAML: %v", err)
}
// Add a header comment
header := "# Generated by sub2mihomo\n# https://github.com/ByteTrue/sub2mihomo\n\n"
result := header + string(output)
if outputPath != "" {
if err := os.WriteFile(outputPath, []byte(result), 0644); err != nil {
log.Fatalf("Failed to write output file: %v", err)
}
logVerbose("Config written to: %s", outputPath)
fmt.Fprintf(os.Stderr, "Generated config with %d proxies -> %s\n", len(proxies), outputPath)
} else {
fmt.Print(result)
}
}
func runInteractive() {
fmt.Println()
fmt.Println("sub2mihomo - convert v2ray subscriptions to mihomo config")
fmt.Println("https://github.com/ByteTrue/sub2mihomo")
fmt.Println()
reader := bufio.NewReader(os.Stdin)
// Step 1: Get subscription URL or share link
input := prompt(reader, "Enter subscription URL or paste share link: ")
if input == "" {
log.Fatal("No input provided")
}
var rawContent string
if strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://") {
fmt.Println(" Fetching subscription...")
content, err := FetchSubscription(input)
if err != nil {
log.Fatalf("Failed to fetch subscription: %v", err)
}
rawContent = content
} else {
// Treat as raw share link(s) directly
rawContent = input
}
links, err := DecodeSubscription(rawContent)
if err != nil {
log.Fatalf("Failed to decode subscription: %v", err)
}
proxies := ParseLinks(links)
if len(proxies) == 0 {
log.Fatal("No valid proxies found")
}
fmt.Printf(" Found %d proxies:\n", len(proxies))
for i, p := range proxies {
fmt.Printf(" [%d] %s (%s) %s:%d\n", i+1, p.Tag, p.Type, p.Server, p.Port)
}
fmt.Println()
// Step 2: Get output path
outputPath := prompt(reader, "Output file path (config.yaml): ")
if outputPath == "" {
outputPath = "config.yaml"
}
// Step 3: Get template path
templatePath := prompt(reader, "Custom template path (leave empty to skip): ")
fmt.Println()
generateAndWrite(proxies, outputPath, templatePath)
}
func prompt(reader *bufio.Reader, msg string) string {
fmt.Printf("? %s\n> ", msg)
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
return strings.TrimSpace(line)
}
log.Fatalf("Failed to read input: %v", err)
}
return strings.TrimSpace(line)
}
func logVerbose(format string, args ...interface{}) {
if verbose {
msg := fmt.Sprintf(format, args...)
if !strings.HasSuffix(msg, "\n") {
msg += "\n"
}
fmt.Fprint(os.Stderr, "[verbose] "+msg)
}
}