-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmain.go
More file actions
969 lines (834 loc) · 26.1 KB
/
Copy pathmain.go
File metadata and controls
969 lines (834 loc) · 26.1 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
// v2go - High-Performance V2Ray Config Aggregator (Go Edition)
// Copyright (C) 2025 Danialsamadi
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"bufio"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/oschwald/geoip2-golang"
)
const (
timeout = 20 * time.Second
maxWorkers = 10
maxLinesPerFile = 500
)
var fixedText = `#profile-title: base64:8J+GkyBHaXRodWIgfCBEYW5pYWwgU2FtYWRpIPCfkI0=
#profile-update-interval: 1
#support-url: https://github.com/Danialsamadi/v2go
#profile-web-page-url: https://github.com/Danialsamadi/v2go
`
var protocols = []string{"vmess", "vless", "trojan", "ss", "ssr", "hy2", "tuic", "warp://"}
var links = []string{
"https://raw.githubusercontent.com/ALIILAPRO/v2rayNG-Config/main/sub.txt",
"https://raw.githubusercontent.com/mfuu/v2ray/master/v2ray",
"https://raw.githubusercontent.com/ts-sf/fly/main/v2",
"https://raw.githubusercontent.com/mahsanet/MahsaFreeConfig/refs/heads/main/mci/sub_1.txt",
"https://raw.githubusercontent.com/mahsanet/MahsaFreeConfig/refs/heads/main/mci/sub_2.txt",
"https://raw.githubusercontent.com/mahsanet/MahsaFreeConfig/refs/heads/main/mci/sub_3.txt",
"https://raw.githubusercontent.com/mahsanet/MahsaFreeConfig/refs/heads/main/app/sub.txt",
"https://raw.githubusercontent.com/mahsanet/MahsaFreeConfig/refs/heads/main/mtn/sub_1.txt",
"https://raw.githubusercontent.com/mahsanet/MahsaFreeConfig/refs/heads/main/mtn/sub_2.txt",
"https://raw.githubusercontent.com/mahsanet/MahsaFreeConfig/refs/heads/main/mtn/sub_3.txt",
"https://raw.githubusercontent.com/mahsanet/MahsaFreeConfig/refs/heads/main/mtn/sub_4.txt",
"https://raw.githubusercontent.com/yebekhe/vpn-fail/refs/heads/main/sub-link",
}
var dirLinks = []string{
"https://v2.alicivil.workers.dev",
"https://raw.githubusercontent.com/Surfboardv2ray/TGParse/main/splitted/mixed",
"https://raw.githubusercontent.com/itsyebekhe/PSG/main/lite/subscriptions/xray/normal/mix",
"https://raw.githubusercontent.com/HosseinKoofi/GO_V2rayCollector/main/mixed_iran.txt",
"https://raw.githubusercontent.com/arshiacomplus/v2rayExtractor/refs/heads/main/mix/sub.html",
"https://raw.githubusercontent.com/Rayan-Config/C-Sub/refs/heads/main/configs/proxy.txt",
"https://raw.githubusercontent.com/mahdibland/ShadowsocksAggregator/master/Eternity.txt",
"https://raw.githubusercontent.com/Everyday-VPN/Everyday-VPN/main/subscription/main.txt",
"https://raw.githubusercontent.com/MahsaNetConfigTopic/config/refs/heads/main/xray_final.txt",
"https://github.com/Epodonios/v2ray-configs/raw/main/All_Configs_Sub.txt",
"https://raw.githubusercontent.com/V2RayRoot/V2RayConfig/refs/heads/main/Config/vless.txt",
"https://raw.githubusercontent.com/V2RayRoot/V2RayConfig/refs/heads/main/Config/vmess.txt",
"https://raw.githubusercontent.com/ebrasha/free-v2ray-public-list/refs/heads/main/all_extracted_configs.txt",
"https://raw.githubusercontent.com/miladtahanian/V2RayScrapeByCountry/refs/heads/main/output_configs/Vless.txt",
"https://raw.githubusercontent.com/miladtahanian/V2RayScrapeByCountry/refs/heads/main/output_configs/Vmess.txt",
"https://raw.githubusercontent.com/miladtahanian/V2ray-Config/main/All_Configs_Sub.txt",
"https://raw.githubusercontent.com/SoliSpirit/v2ray-configs/refs/heads/main/all_configs.txt",
"https://raw.githubusercontent.com/Kolandone/v2raycollector/refs/heads/main/config.txt",
"https://raw.githubusercontent.com/mohamadfg-dev/telegram-v2ray-configs-collector/refs/heads/main/category/vless.txt",
"https://raw.githubusercontent.com/mohamadfg-dev/telegram-v2ray-configs-collector/refs/heads/main/category/vmess.txt",
"https://raw.githubusercontent.com/mohamadfg-dev/telegram-v2ray-configs-collector/refs/heads/main/category/trojan.txt",
"https://raw.githubusercontent.com/Surfboardv2ray/TGParse/refs/heads/main/configtg.txt",
"https://raw.githubusercontent.com/shabane/kamaji/refs/heads/master/hub/merged.txt",
"https://raw.githubusercontent.com/igareck/vpn-configs-for-russia/refs/heads/main/BLACK_VLESS_RUS_mobile.txt",
"https://raw.githubusercontent.com/igareck/vpn-configs-for-russia/refs/heads/main/BLACK_VLESS_RUS.txt",
"https://raw.githubusercontent.com/igareck/vpn-configs-for-russia/refs/heads/main/BLACK_SS+All_RUS.txt",
"https://raw.githubusercontent.com/frank-vpl/servers/refs/heads/main/irbox",
}
type Result struct {
URL string
Content string
IsBase64 bool
StatusCode int
Error error
}
var (
geoDB *geoip2.Reader
geoCache sync.Map // cache for host -> country code
)
func main() {
start := time.Now()
fmt.Println("Starting V2Ray config aggregator...")
// Ensure directories exist
base64Folder, err := ensureDirectoriesExist()
if err != nil {
fmt.Printf("Error creating directories: %v\n", err)
return
}
// Create HTTP client with connection pooling
client := &http.Client{
Timeout: timeout,
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 10,
IdleConnTimeout: 30 * time.Second,
},
}
// Download and open GeoIP database
if err := downloadGeoIPDB(); err != nil {
fmt.Printf("Warning: Could not download GeoIP database: %v\n", err)
} else {
db, err := geoip2.Open("GeoLite2-Country.mmdb")
if err == nil {
geoDB = db
defer geoDB.Close()
} else {
fmt.Printf("Warning: Could not open GeoIP database: %v\n", err)
}
}
// Fetch all URLs concurrently
fmt.Println("Fetching configurations from sources...")
allConfigs, failedLinks := fetchAllConfigs(client, links, dirLinks)
// Filter for protocols
fmt.Println("Filtering configurations and removing duplicates...")
originalCount := len(allConfigs)
filteredConfigs, configsByCountry := filterForProtocols(allConfigs, protocols)
fmt.Printf("Found %d unique valid configurations\n", len(filteredConfigs))
fmt.Printf("Removed %d duplicates\n", originalCount-len(filteredConfigs))
// Clean existing files
cleanExistingFiles(base64Folder)
// Write main config file (in current directory)
mainOutputFile := "AllConfigsSub.txt"
err = writeMainConfigFile(mainOutputFile, filteredConfigs)
if err != nil {
fmt.Printf("Error writing main config file: %v\n", err)
return
}
// Split into smaller files
fmt.Println("Splitting into smaller files...")
err = splitIntoFiles(base64Folder, filteredConfigs)
if err != nil {
fmt.Printf("Error splitting files: %v\n", err)
return
}
// Calculate protocol statistics
stats := calculateStats(filteredConfigs)
// Write country-specific files
fmt.Println("Writing country-specific files...")
writeCountryFiles(configsByCountry)
// Write summary to UPDATE_SUMMARY.md
processingTime := time.Since(start).Seconds()
writeUpdateSummary(len(filteredConfigs), stats, processingTime, originalCount, failedLinks)
// Now sort configurations by protocol
sortConfigs()
}
func ensureDirectoriesExist() (string, error) {
// Create Base64 directory
base64Folder := "Base64"
if err := os.MkdirAll(base64Folder, 0755); err != nil {
return "", err
}
// Create Splitted-By-Country directory
if err := os.MkdirAll("Splitted-By-Country", 0755); err != nil {
return "", err
}
return base64Folder, nil
}
func fetchAllConfigs(client *http.Client, base64Links, textLinks []string) ([]string, []string) {
var wg sync.WaitGroup
resultChan := make(chan Result, len(base64Links)+len(textLinks))
var failedLinks []string
// Worker pool for concurrent requests
semaphore := make(chan struct{}, maxWorkers)
// Fetch base64-encoded links
for _, link := range base64Links {
wg.Add(1)
go func(url string) {
defer wg.Done()
semaphore <- struct{}{}
defer func() { <-semaphore }()
res := fetchAndDecodeBase64(client, url)
resultChan <- res
}(link)
}
// Fetch text links
for _, link := range textLinks {
wg.Add(1)
go func(url string) {
defer wg.Done()
semaphore <- struct{}{}
defer func() { <-semaphore }()
res := fetchText(client, url)
resultChan <- res
}(link)
}
// Close channel when all goroutines are done
go func() {
wg.Wait()
close(resultChan)
}()
// Collect results
var allConfigs []string
for result := range resultChan {
if result.StatusCode != http.StatusOK || result.Error != nil {
status := "Error"
if result.StatusCode > 0 {
status = fmt.Sprintf("HTTP %d", result.StatusCode)
}
failedLinks = append(failedLinks, fmt.Sprintf("%s (%s)", result.URL, status))
continue
}
lines := strings.Split(strings.TrimSpace(result.Content), "\n")
allConfigs = append(allConfigs, lines...)
}
return allConfigs, failedLinks
}
func fetchAndDecodeBase64(client *http.Client, url string) Result {
res := Result{URL: url, IsBase64: true}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
res.Error = err
return res
}
resp, err := client.Do(req)
if err != nil {
res.Error = err
return res
}
defer resp.Body.Close()
res.StatusCode = resp.StatusCode
if resp.StatusCode != http.StatusOK {
return res
}
body, err := io.ReadAll(resp.Body)
if err != nil {
res.Error = err
return res
}
// Try to decode base64
decoded, err := decodeBase64(body)
if err != nil {
res.Error = err
return res
}
res.Content = decoded
return res
}
func fetchText(client *http.Client, url string) Result {
res := Result{URL: url, IsBase64: false}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
res.Error = err
return res
}
resp, err := client.Do(req)
if err != nil {
res.Error = err
return res
}
defer resp.Body.Close()
res.StatusCode = resp.StatusCode
if resp.StatusCode != http.StatusOK {
return res
}
body, err := io.ReadAll(resp.Body)
if err != nil {
res.Error = err
return res
}
res.Content = string(body)
return res
}
func decodeBase64(encoded []byte) (string, error) {
// Add padding if necessary
encodedStr := string(encoded)
if len(encodedStr)%4 != 0 {
encodedStr += strings.Repeat("=", 4-len(encodedStr)%4)
}
decoded, err := base64.StdEncoding.DecodeString(encodedStr)
if err != nil {
return "", err
}
return string(decoded), nil
}
// sanitizeConfig fixes common issues in config strings from upstream sources.
func sanitizeConfig(config string) string {
// Fix HTML entities: & → &
config = strings.ReplaceAll(config, "&", "&")
return config
}
// isValidConfig checks whether a config has parameters that would crash V2Ray clients.
// Returns false if the config should be skipped.
func isValidConfig(config string) bool {
// Extract query string (between ? and #)
qStart := strings.Index(config, "?")
if qStart < 0 {
return true // no query params, nothing to validate
}
qEnd := strings.Index(config[qStart:], "#")
var query string
if qEnd >= 0 {
query = config[qStart+1 : qStart+qEnd]
} else {
query = config[qStart+1:]
}
// Parse query params and validate sni and path
for _, param := range strings.Split(query, "&") {
kv := strings.SplitN(param, "=", 2)
if len(kv) != 2 {
continue
}
key := strings.TrimSpace(kv[0])
val := strings.TrimSpace(kv[1])
if key == "sni" || key == "path" {
// Reject if value contains non-ASCII chars (emojis, CJK, etc.) or raw brackets
for _, r := range val {
if r > 127 || r == '[' || r == ']' {
return false
}
}
}
}
return true
}
func filterForProtocols(data []string, protocols []string) ([]string, map[string][]string) {
var filtered []string
configsByCountry := make(map[string][]string)
seen := make(map[string]bool)
var mu sync.Mutex
type configRes struct {
line string
country string
proto string
}
// Use a worker pool for parallel country lookup and deduplication
jobs := make(chan string, 100)
results := make(chan configRes, 100)
const numWorkers = 300
var wg sync.WaitGroup
for w := 0; w < numWorkers; w++ {
wg.Add(1)
go func() {
defer wg.Done()
for line := range jobs {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Identify protocol
var currentProtocol string
for _, protocol := range protocols {
prefix := protocol
if !strings.HasSuffix(prefix, "://") && protocol != "warp://" {
prefix += "://"
}
if strings.HasPrefix(line, prefix) {
currentProtocol = protocol
break
}
}
if currentProtocol == "" {
continue
}
// Validate config: reject configs with invalid SNI/path that crash clients
if !isValidConfig(line) {
continue
}
// Smart Deduplication: Parse core identity (Address + Port)
identity := parseCoreIdentity(line, currentProtocol)
mu.Lock()
if seen[identity] {
mu.Unlock()
continue
}
seen[identity] = true
mu.Unlock()
// Life Guard: Port Checker (TCP Connectivity Test)
host, port := getHostPort(line, currentProtocol)
if !checkPort(host, port) {
continue
}
// Country Lookup (Parallelized as it involves DNS)
country := getCountryInfo(line, currentProtocol)
results <- configRes{line: line, country: country, proto: currentProtocol}
}
}()
}
go func() {
for _, line := range data {
// Sanitize before processing (fix & HTML entities, etc.)
jobs <- sanitizeConfig(line)
}
close(jobs)
}()
go func() {
wg.Wait()
close(results)
}()
for res := range results {
// Standardize the name sequentially to have correct indexing
cleanLine := standardizeName(res.line, res.proto, len(filtered)+1, res.country)
filtered = append(filtered, cleanLine)
countryKey := res.country
if countryKey == "" {
countryKey = "Unknown"
}
configsByCountry[countryKey] = append(configsByCountry[countryKey], cleanLine)
}
return filtered, configsByCountry
}
// standardizeName renames a configuration to a professional format: v2go | 🇩🇪 DE | Protocol | ID
func standardizeName(config string, protocol string, index int, country string) string {
flag := getFlag(country)
countryDisplay := ""
if country != "" {
if flag != "" {
countryDisplay = flag + " " + country + " | "
} else {
countryDisplay = country + " | "
}
}
newName := fmt.Sprintf("v2go | %s%s | %d", countryDisplay, strings.ToUpper(protocol), index)
switch protocol {
case "vmess":
trimmed := strings.TrimPrefix(config, "vmess://")
decoded, err := decodeBase64([]byte(trimmed))
if err != nil {
return config
}
var data map[string]interface{}
if err := json.Unmarshal([]byte(decoded), &data); err != nil {
return config
}
data["ps"] = newName
updated, _ := json.Marshal(data)
return "vmess://" + base64.StdEncoding.EncodeToString(updated)
case "ssr":
trimmed := strings.TrimPrefix(config, "ssr://")
decoded, err := decodeBase64([]byte(trimmed))
if err != nil {
return config
}
// SSR format: host:port:protocol:method:obfs:base64pass/?obfsparam=...&remarks=base64remarks&...
parts := strings.Split(decoded, "/?")
if len(parts) < 1 {
return config
}
mainInfo := parts[0]
params := ""
if len(parts) > 1 {
params = parts[1]
}
// Handle remarks in params
paramList := strings.Split(params, "&")
newParamList := []string{}
remarksFound := false
encodedName := strings.ReplaceAll(base64.StdEncoding.EncodeToString([]byte(newName)), "=", "")
for _, p := range paramList {
if strings.HasPrefix(p, "remarks=") {
newParamList = append(newParamList, "remarks="+encodedName)
remarksFound = true
} else if p != "" {
newParamList = append(newParamList, p)
}
}
if !remarksFound {
newParamList = append(newParamList, "remarks="+encodedName)
}
updatedDecoded := mainInfo + "/?" + strings.Join(newParamList, "&")
return "ssr://" + strings.ReplaceAll(base64.StdEncoding.EncodeToString([]byte(updatedDecoded)), "=", "")
default:
// Standard URL protocols: vless, trojan, ss, hy2, tuic
// Use simple string manipulation to avoid url.Parse re-encoding userinfo/query
var body string
if hi := strings.Index(config, "#"); hi >= 0 {
body = config[:hi]
} else {
body = config
}
// Trim trailing whitespace from body (some sources have trailing spaces before #)
body = strings.TrimRight(body, " \t")
result := body + "#" + url.PathEscape(newName)
return result
}
}
// parseCoreIdentity extracts the Protocol + Host + Port from a config line.
// This allows us to find duplicates that have different names or parameters but point to the same server.
func parseCoreIdentity(config string, protocol string) string {
config = strings.TrimSpace(config)
switch protocol {
case "vmess":
trimmed := strings.TrimPrefix(config, "vmess://")
decoded, err := decodeBase64([]byte(trimmed))
if err != nil {
return config // Fallback to full string if decoding fails
}
var data struct {
Add string `json:"add"`
Port interface{} `json:"port"` // Use interface because port can be string or int
}
if err := json.Unmarshal([]byte(decoded), &data); err != nil {
return config
}
return fmt.Sprintf("vmess://%s:%v", data.Add, data.Port)
case "ssr":
trimmed := strings.TrimPrefix(config, "ssr://")
decoded, err := decodeBase64([]byte(trimmed))
if err != nil {
// SSR padding is often weird, try simple trim if padding fails
return config
}
// SSR format: host:port:protocol:method:obfs:base64pass/?obfsparam=...
parts := strings.Split(decoded, ":")
if len(parts) >= 2 {
return fmt.Sprintf("ssr://%s:%s", parts[0], parts[1])
}
return config
default:
u, err := url.Parse(config)
if err != nil {
return config
}
host := u.Hostname()
port := u.Port()
if host == "" {
return config
}
return fmt.Sprintf("%s://%s:%s", protocol, host, port)
}
}
func getCountryInfo(config, protocol string) string {
if geoDB == nil {
return ""
}
host := ""
switch protocol {
case "vmess":
trimmed := strings.TrimPrefix(config, "vmess://")
decoded, err := decodeBase64([]byte(trimmed))
if err == nil {
var data struct {
Add string `json:"add"`
}
json.Unmarshal([]byte(decoded), &data)
host = data.Add
}
case "ssr":
trimmed := strings.TrimPrefix(config, "ssr://")
decoded, err := decodeBase64([]byte(trimmed))
if err == nil {
parts := strings.Split(decoded, ":")
if len(parts) > 0 {
host = parts[0]
}
}
default:
u, err := url.Parse(config)
if err == nil {
host = u.Hostname()
}
}
if host == "" {
return ""
}
// Check cache
if val, ok := geoCache.Load(host); ok {
return val.(string)
}
// Resolve IP if it's a domain
ip := net.ParseIP(host)
if ip == nil {
ips, err := net.LookupIP(host)
if err == nil && len(ips) > 0 {
ip = ips[0]
}
}
if ip == nil {
geoCache.Store(host, "")
return ""
}
record, err := geoDB.Country(ip)
if err != nil {
geoCache.Store(host, "")
return ""
}
code := record.Country.IsoCode
geoCache.Store(host, code)
return code
}
func getFlag(code string) string {
if len(code) != 2 {
return ""
}
code = strings.ToUpper(code)
return string(rune(code[0])+127397) + string(rune(code[1])+127397)
}
func downloadGeoIPDB() error {
dbPath := "GeoLite2-Country.mmdb"
if _, err := os.Stat(dbPath); err == nil {
return nil
}
fmt.Println("Downloading GeoIP database...")
// Using a reliable mirror
url := "https://raw.githubusercontent.com/6Kmfi6HP/maxmind/main/GeoLite2-Country.mmdb"
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(dbPath)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}
func cleanExistingFiles(base64Folder string) {
// Remove main files
os.Remove("AllConfigsSub.txt")
os.Remove("All_Configs_base64_Sub.txt")
// Remove split files
for i := 0; i < 20; i++ {
os.Remove(fmt.Sprintf("Sub%d.txt", i))
os.Remove(filepath.Join(base64Folder, fmt.Sprintf("Sub%d_base64.txt", i)))
}
// Clean Splitted-By-Country directory
files, err := os.ReadDir("Splitted-By-Country")
if err == nil {
for _, f := range files {
os.Remove(filepath.Join("Splitted-By-Country", f.Name()))
}
}
}
func writeCountryFiles(configsByCountry map[string][]string) {
countryDir := "Splitted-By-Country"
for country, configs := range configsByCountry {
filename := filepath.Join(countryDir, country+".txt")
file, err := os.Create(filename)
if err != nil {
continue
}
writer := bufio.NewWriter(file)
for _, config := range configs {
writer.WriteString(config + "\n")
}
writer.Flush()
file.Close()
}
}
func writeMainConfigFile(filename string, configs []string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
defer writer.Flush()
// Write fixed text
if _, err := writer.WriteString(fixedText); err != nil {
return err
}
// Write configs
for _, config := range configs {
if _, err := writer.WriteString(config + "\n"); err != nil {
return err
}
}
return nil
}
func splitIntoFiles(base64Folder string, configs []string) error {
numFiles := (len(configs) + maxLinesPerFile - 1) / maxLinesPerFile
// Reverse configs so newest go into Sub1, Sub2, etc.
reversedConfigs := make([]string, len(configs))
for i, config := range configs {
reversedConfigs[len(configs)-1-i] = config
}
for i := 0; i < numFiles; i++ {
// Create custom header for this file
profileTitle := fmt.Sprintf("🆓 Git:DanialSamadi | Sub%d 🔥", i+1)
encodedTitle := base64.StdEncoding.EncodeToString([]byte(profileTitle))
customFixedText := fmt.Sprintf(`#profile-title: base64:%s
#profile-update-interval: 1
#support-url: https://github.com/Danialsamadi/v2go
#profile-web-page-url: https://github.com/Danialsamadi/v2go
`, encodedTitle)
// Calculate slice bounds (using reversed configs)
start := i * maxLinesPerFile
end := start + maxLinesPerFile
if end > len(reversedConfigs) {
end = len(reversedConfigs)
}
// Write regular file (in current directory)
filename := fmt.Sprintf("Sub%d.txt", i+1)
if err := writeSubFile(filename, customFixedText, reversedConfigs[start:end]); err != nil {
return err
}
// Read the file and create base64 version
content, err := os.ReadFile(filename)
if err != nil {
return err
}
base64Filename := filepath.Join(base64Folder, fmt.Sprintf("Sub%d_base64.txt", i+1))
encodedContent := base64.StdEncoding.EncodeToString(content)
if err := os.WriteFile(base64Filename, []byte(encodedContent), 0644); err != nil {
return err
}
}
return nil
}
func writeSubFile(filename, header string, configs []string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
defer writer.Flush()
// Write header
if _, err := writer.WriteString(header); err != nil {
return err
}
// Write configs
for _, config := range configs {
if _, err := writer.WriteString(config + "\n"); err != nil {
return err
}
}
return nil
}
func calculateStats(configs []string) map[string]int {
stats := make(map[string]int)
for _, config := range configs {
for _, protocol := range protocols {
if strings.HasPrefix(config, protocol) {
stats[protocol]++
break
}
}
}
return stats
}
func writeUpdateSummary(total int, stats map[string]int, duration float64, originalTotal int, failedLinks []string) {
summaryPath := "UPDATE_SUMMARY.md"
file, err := os.Create(summaryPath)
if err != nil {
fmt.Printf("Error creating summary file: %v\n", err)
return
}
defer file.Close()
writer := bufio.NewWriter(file)
defer writer.Flush()
writer.WriteString("# V2Ray Config Update Summary\n")
writer.WriteString(fmt.Sprintf("Generated on: %s\n\n", time.Now().Format("2006-01-02 15:04:05 MST")))
writer.WriteString("## Configuration Statistics\n")
writer.WriteString(fmt.Sprintf("- Total unique configurations: %d\n", total))
writer.WriteString("- Protocol breakdown:\n")
// Sort protocols for consistent output
for _, p := range protocols {
count := stats[p]
writer.WriteString(fmt.Sprintf(" - %s: %d configs\n", p, count))
}
writer.WriteString("\n## Performance\n")
writer.WriteString(fmt.Sprintf("- Processing time: %.2f seconds\n", duration))
if originalTotal > 0 {
reduction := float64(originalTotal-total) / float64(originalTotal) * 100
writer.WriteString(fmt.Sprintf("- Duplicate removal: %.1f%% reduction (from %d to %d)\n", reduction, originalTotal, total))
}
if len(failedLinks) > 0 {
writer.WriteString("\n## ⚠️ Failed Links (404 or Errors)\n")
writer.WriteString("The following sources could not be reached or returned no data:\n")
for _, link := range failedLinks {
writer.WriteString(fmt.Sprintf("- %s\n", link))
}
} else {
writer.WriteString("\n## ✅ All Sources Successful\n")
writer.WriteString("All configured sources were reached successfully.\n")
}
}
func checkPort(host, port string) bool {
if host == "" || port == "" {
return false
}
address := net.JoinHostPort(host, port)
conn, err := net.DialTimeout("tcp", address, 2*time.Second)
if err != nil {
return false
}
conn.Close()
return true
}
func getHostPort(config, protocol string) (string, string) {
switch protocol {
case "vmess":
trimmed := strings.TrimPrefix(config, "vmess://")
decoded, err := decodeBase64([]byte(trimmed))
if err == nil {
var data struct {
Add string `json:"add"`
Port interface{} `json:"port"`
}
json.Unmarshal([]byte(decoded), &data)
return data.Add, fmt.Sprintf("%v", data.Port)
}
case "ssr":
trimmed := strings.TrimPrefix(config, "ssr://")
decoded, err := decodeBase64([]byte(trimmed))
if err == nil {
parts := strings.Split(decoded, ":")
if len(parts) >= 2 {
return parts[0], parts[1]
}
}
default:
u, err := url.Parse(config)
if err == nil {
return u.Hostname(), u.Port()
}
}
return "", ""
}