-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch-import.go
176 lines (164 loc) · 4.43 KB
/
batch-import.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
// FormatA struct for the first JSON format
type ServerInfo struct {
ID string `json:"id,omitempty"`
Remarks string `json:"remarks,omitempty"`
Server string `json:"server"`
ServerPort int `json:"server_port"`
Password string `json:"password"`
Method string `json:"method"`
Prefix string `json:"prefix"`
Plugin string `json:"plugin,omitempty"`
PluginOpts string `json:"plugin_opts,omitempty"`
}
// FormatC struct for the SIP008 JSON format
type SIP008Config struct {
Version int `json:"version"`
Servers []ServerInfo `json:"servers"`
BytesUsed uint64 `json:"bytes_used,omitempty"`
BytesRemaining uint64 `json:"bytes_remaining,omitempty"`
AdditionalProps map[string]interface{} // For custom fields
}
func parseDynamicConfig(data []byte) ([]string, error) {
//Parse if simple JSON format
server, err := parseSingleJSON(data)
if err == nil {
return []string{server}, nil
}
// Parse if SIP008 JSON format
servers, err := parseSIP008(data)
if err == nil {
return servers, nil
} else {
fmt.Println("parseSIP008 error:", err)
}
// Parse if CSV format
servers, err = parseBase64URLLine(data)
if err == nil {
return servers, nil
} else {
fmt.Println("parseBase64URLLine error:", err)
}
servers, err = parseCSVformat(data)
if err == nil {
return servers, nil
} else {
fmt.Println("parseCSVformat error:", err)
}
return []string{}, fmt.Errorf("unknown format")
// parse
}
func getDynamicConfig(url string) ([]string, error) {
response, err := http.Get(url)
if err != nil {
fmt.Println("Error fetching URL:", err)
return []string{}, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
fmt.Println("Error reading response:", err)
return []string{}, err
}
conf, err := parseDynamicConfig(body)
if err != nil {
fmt.Println("Error detecting format:", err)
return []string{}, err
}
return conf, nil
}
func parseSingleJSON(data []byte) (string, error) {
//Parse if simple JSON format
var config ServerInfo
err := json.Unmarshal(data, &config)
if err != nil {
return "", err
}
return makeShadowsocksURLfromJSON(&config)
}
func parseSIP008(data []byte) ([]string, error) {
//Parse if SIP008 JSON format
var config SIP008Config
err := json.Unmarshal(data, &config)
if err != nil {
return []string{}, err
}
if config.Version == 1 {
var result []string
for _, server := range config.Servers {
configURL, err := makeShadowsocksURLfromJSON(&server)
if err != nil {
return []string{}, err
}
result = append(result, configURL)
}
return result, nil
}
return []string{}, fmt.Errorf("unknown SIP008 version: %d", config.Version)
}
func parseCSVformat(data []byte) ([]string, error) {
// fmt.Println("Printing response string:")
str := string(data)
configs := strings.Split(str, "\n")
fmt.Println("Printing response string:")
fmt.Println(configs)
// check of each line contains a valid URL
var validConfigs []string
for _, config := range configs {
// Ignore blank lines
if config == "" {
continue
}
u, err := url.Parse(config)
// ignore invalid URLs
if err != nil {
continue
}
fmt.Println("scheme:", u.Scheme)
// ignore non-URLs
if u.Scheme == "" {
continue
}
validConfigs = append(validConfigs, config)
}
return validConfigs, nil
}
// https://www.v2fly.org/en_US/v5/config/service/subscription.html#subscription-container
func parseBase64URLLine(data []byte) ([]string, error) {
decoded, err := base64.URLEncoding.WithPadding(base64.NoPadding).DecodeString(string(data))
if err != nil {
return []string{}, err
}
return parseCSVformat(decoded)
}
func makeShadowsocksURLfromJSON(config *ServerInfo) (string, error) {
if config.ServerPort == 0 {
return "", fmt.Errorf("missing server port")
}
if config.Method == "" {
return "", fmt.Errorf("missing method")
}
if config.Password == "" {
return "", fmt.Errorf("missing password")
}
if config.Server == "" {
return "", fmt.Errorf("missing server")
}
configURL := "ss://" + config.Method + ":" + config.Password + "@" + config.Server + ":" + fmt.Sprint(config.ServerPort)
if config.Prefix != "" {
configURL += "/?prefix=" + url.QueryEscape(config.Prefix)
}
if config.Plugin != "" {
configURL += "&plugin=" + url.QueryEscape(config.Plugin)
}
return configURL, nil
}