-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
171 lines (156 loc) · 3.46 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
_ "embed"
"flag"
"fmt"
"github.com/captain686/go-warp2wireguard/services"
"github.com/captain686/go-warp2wireguard/util"
"github.com/charmbracelet/log"
"os"
"sync"
"time"
)
//go:embed template/clash_template
var clashTemplate string
var (
mu sync.Mutex
serviceType string
)
func init() {
flag.StringVar(&serviceType, "t", "wireguard", "operating mode [ wireguard | clash ]")
flag.Usage = func() {
_, err := fmt.Fprintf(os.Stderr, "Usage:\n")
if err != nil {
return
}
flag.PrintDefaults()
}
if _, err := os.Stat(util.ConfigPath); os.IsNotExist(err) {
// If the folder does not exist, create the folder
err := os.MkdirAll(util.ConfigPath, 0755)
if err != nil {
return
}
}
if _, err := os.Stat(util.OutPath); os.IsNotExist(err) {
// If the folder does not exist, create the folder
err := os.MkdirAll(util.OutPath, 0755)
if err != nil {
return
}
}
}
// Account registration using public key
func task(id, accountToken string) {
mu.Lock()
defer mu.Unlock()
token, err := services.FloWorker(id, accountToken)
if err != nil {
util.SleepBar("Waiting for retry", 10)
task(id, accountToken)
}
log.Info("Start Account Info Query")
util.SleepBar("Sleep Countdown", 60)
err = services.Query(id, *token, util.AccountFilePath)
if err != nil {
util.SleepBar("Waiting for retry", 10)
task(id, accountToken)
}
}
// Earn traffic using account ID
func regAndFlow(serviceType string) error {
if !util.FileCheck(util.KeyFilePath) || !util.FileCheck(util.AccountFilePath) {
log.Info("User information not detected")
err := services.AccountReg()
if err != nil {
return err
}
//util.SleepBar(60)
util.SleepBar("Sleep Countdown", 10)
}
if !util.FileCheck(util.GeoLite2CityPath) {
err := util.DownloadGeoLite()
if err != nil {
return err
}
}
accountInfo, err := util.ReadAccountInfo(util.AccountFilePath)
if err != nil || accountInfo == nil {
return err
}
account := *accountInfo
id := account.ID
accountToken := account.Token
task(id, accountToken)
//go func() {
cronFunc := services.CronFunction{
Function: task,
Id: id,
AccountToken: accountToken,
}
cronFunc.CronServe()
//}()
return nil
}
func speedTest(speedSort []util.Speed) []util.Speed {
coonMap, err := services.WarpSpeedTest()
if err != nil {
log.Error(err)
return nil
}
speedSort = services.SpeedSort(coonMap)
err = util.WriteToCsv(speedSort)
if err != nil {
log.Error(err)
return nil
}
return speedSort
}
func main() {
flag.Parse()
if serviceType != "wireguard" && serviceType != "clash" {
flag.Usage()
return
}
err := regAndFlow(serviceType)
if err != nil {
return
}
var speedSort []util.Speed
mu.Lock()
account, err := util.ReadAccountInfo(util.AccountFilePath)
if err != nil {
log.Error(err)
return
}
mu.Unlock()
publicKey := account.Config.Peers[0].PublicKey
if serviceType == "clash" {
go func() {
newSpeedSort := speedTest(speedSort)
err = util.WriteToCsv(newSpeedSort)
if err != nil {
log.Error(err)
return
}
}()
services.Subscribe(publicKey, clashTemplate)
}
if serviceType == "wireguard" {
speedSort = speedTest(speedSort)
err = util.WriteToCsv(speedSort)
if err != nil {
log.Error(err)
return
}
log.Info(fmt.Sprintf("[+] Speed test results are saved in %s", util.OutFilePath))
err = services.GenerateConf(speedSort[0].Server, publicKey)
if err != nil {
log.Error(err)
return
}
for {
time.Sleep(10 * time.Minute)
}
}
}