-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
413 lines (357 loc) · 9.88 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
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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"math"
"math/rand"
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"syscall"
"time"
"code-generation/utils"
"github.com/bits-and-blooms/bloom/v3"
"github.com/dustin/go-humanize"
"github.com/eiannone/keyboard"
"github.com/fatih/color"
"golang.org/x/crypto/bcrypt"
)
const (
minPrefixLength = 2
maxPrefixLength = 6
minCodeLength = 4
maxCodeLength = 16
maxNumCodes = 1000000000
charset = "0123456789"
numWorkers = 200
falsePositiveRate = 0.0000000001
)
type AppConfig struct {
Prefix string
Length int
NumCodes int
LineNumbers bool
Version bool
CodeTx string
FLineF string
HelpF string
RemainingTime time.Duration
AppCommand string
OSSpecDir string
AppVer string
}
type CodeResult struct {
Code string
}
func main() {
var config AppConfig
runtime.GOMAXPROCS(runtime.NumCPU())
flag.StringVar(&config.Prefix, "p", "", "Add prefix to the codes[2-6 characters]")
flag.IntVar(&config.Length, "l", 6, "The length of the generated numbers[4-16 digits]")
flag.IntVar(&config.NumCodes, "n", 1, "The number of generated codes[1-1 billion]")
flag.BoolVar(&config.LineNumbers, "a", false, "Add line numbers to the file")
flag.BoolVar(&config.Version, "v", false, "About")
flag.Parse()
config.CodeTx = "CODES"
config.FLineF = "codes were generated |"
config.HelpF = "Each time you use a code, delete it. You can use [CTRL + X]"
config.AppCommand = "codegen"
config.OSSpecDir = "Documents"
if runtime.GOOS == "linux" {
config.OSSpecDir = ""
}
if flag.NFlag() == 0 {
utils.Banner()
fmt.Println()
color.Green(
"%s %s %s %s %s",
"Usage:\t",
config.AppCommand,
"[-p prefix] [-l length_number] [-n total_codes]\t\nexample:",
config.AppCommand,
"-p=FT -l=6 -n=100\t\n",
)
color.Cyan(`Options:
-p Add prefix to the codes (2-6 characters)
-l The length of the generated code number (4-16 digits)
-n The number of generated codes (1-1 billion)
-a Add line numbers to the file
-v About
`)
color.Yellow("Tip: To stop code generation midway, simply press [CTRL + C]\n")
return
}
if config.Version {
utils.Banner()
}
if config.NumCodes == 1 {
config.CodeTx = "CODE"
config.FLineF = "code were generated at"
}
if !config.Version {
if !validatePrefix(config.Prefix) {
c := color.New(color.BgRed, color.FgBlack).Sprint("Error")
fmt.Printf("%s Prefix length should be between %d and %d characters\n", c, minPrefixLength, maxPrefixLength)
return
}
if !validateLength(config.Length) {
c := color.New(color.BgRed, color.FgBlack).Sprint("Error")
fmt.Printf("%s Code length should be between %d and %d digits\n", c, minCodeLength, maxCodeLength)
return
}
if config.NumCodes <= 0 || config.NumCodes > maxNumCodes {
c := color.New(color.BgRed, color.FgBlack).Sprint("Error")
fmt.Printf("%s Number of codes should be between 1 and %s\n", c, humanize.Comma(maxNumCodes))
return
}
psb := calculatePossibleOutcomes(config.Length)
if psb == config.NumCodes {
c, att := color.New(color.BgYellow, color.FgBlack).Sprint("Attention:"),
color.New(color.FgHiYellow).Add(color.Italic).Sprint("\nCode generation and saving may take a bit longer than usual based on the provided parameters!\n")
fmt.Printf("%s %s\n", c, att)
}
if config.NumCodes > psb {
c := color.New(color.BgYellow, color.FgBlack).Sprint("Warning")
fmt.Printf("%s Maximum [%s] numbers can be created with a length of [%d]\n",
c, humanize.Comma(int64(psb)), config.Length)
return
}
hash, err := GenerateHash(generateCodeWithPool("X", 12))
if err != nil {
log.Fatalf("error %s", err)
}
homePath, err := os.UserHomeDir()
if err != nil {
color.Red("Error getting user home directory")
return
}
path := filepath.Join(homePath, config.OSSpecDir, "Code Generator", "Files")
errDir := os.MkdirAll(path, 0755)
if errDir != nil {
color.Red("Error creating folder: %v", errDir)
return
}
filePath := filepath.Join(path, fmt.Sprintf(
"[%s]-%s_%v.txt",
config.Prefix,
config.CodeTx,
sanitizeKey(hash),
))
file, err := os.Create(filePath)
if err != nil {
color.Red("Error creating file: %v", err)
return
}
loadingTicker := time.NewTicker(100 * time.Millisecond)
loadingCounter := 0
ready := false
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
percentage := 0.0
done := make(chan struct{}) // Signal channel to notify workers to exit
var wg sync.WaitGroup
resultChan := make(chan CodeResult, numWorkers) // Buffered channel for worker results
// Create worker pool
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go worker(config.Prefix, config.Length, resultChan, done, &wg)
}
// Write generated codes to file
go func() {
codesBuffer := bufio.NewWriter(file)
// Generate the initial header and add it to the buffer
_, _ = fmt.Fprintf(
codesBuffer,
"%s %s %v\n%s\n%s\n",
humanize.Comma(int64(config.NumCodes)),
config.FLineF,
time.Now().Format("2006-01-02 15:04:05"),
config.HelpF,
strings.Repeat("-", 96),
)
startTime := time.Now()
for i := 1; i <= config.NumCodes; i++ {
elapsedTime := time.Since(startTime)
remainingIterations := config.NumCodes - i
config.RemainingTime = utils.CalculateRemainingTime(elapsedTime, i, remainingIterations)
codeResult := <-resultChan
code := codeResult.Code
if config.LineNumbers {
_, _ = fmt.Fprintf(codesBuffer, "%d: %s\n", i, code)
} else {
_, _ = fmt.Fprintf(codesBuffer, "%s\n", code)
}
percentage = float64(i) / float64(config.NumCodes) * 100
}
close(done) // Signal workers to exit
ready = true
err := codesBuffer.Flush()
if err != nil {
log.Fatalf("error during buffer flashing: %v", err)
}
}()
for {
select {
case <-loadingTicker.C:
fmt.Print("\033[?25l")
info := color.New(
color.FgBlack,
color.BgGreen,
).Sprintf(
"[ST: %.2f%% | RM: %s]",
percentage,
utils.FormatDuration(config.RemainingTime),
)
fmt.Printf(
"\r%s Generating %s codes with prefix '%s' %s%10s",
loadingAnimation(loadingCounter),
humanize.Comma(int64(config.NumCodes)),
config.Prefix,
info,
"",
)
loadingCounter = (loadingCounter + 1) % 2
if ready {
clearLine()
fmt.Print("\033[?25h")
color.Yellow("Generated codes saved to %s\r\n", filePath)
_ = file.Close()
if runtime.GOOS == "windows" {
c := color.New(color.BgGreen, color.FgBlack).Sprint("Press 'O' to open directory")
fmt.Printf("%s or any key to exit\r\n", c)
err := keyboard.Open()
if err != nil {
log.Fatal(err)
}
char, _, err := keyboard.GetKey()
if err != nil {
log.Fatal(err)
}
if char == 'o' || char == 'O' {
openDirectoryInExplorer(filepath.Join(homePath, config.OSSpecDir, "Code Generator", "Files"))
os.Exit(0)
} else {
os.Exit(0)
}
}
return
}
case <-sigChan:
fmt.Print("\033[?25h")
if file != nil {
_ = file.Close()
}
files, _ := os.ReadDir(path)
if len(files) == 1 {
_ = os.RemoveAll(filepath.Join(homePath, config.OSSpecDir, "Code Generator"))
} else {
err := os.Remove(filePath)
if err != nil {
log.Println(err)
}
}
clearLine()
c := color.New(color.BgRed, color.FgBlack).Sprint("Interrupted!")
fmt.Printf("%s Code generation interrupted. Cleaned up generated file.\r\n", c)
os.Exit(0)
}
}
}
}
func init() {
rand.NewSource(time.Now().UnixNano())
}
var stringBuilderPool = sync.Pool{
New: func() interface{} {
return &strings.Builder{}
},
}
func generateCodeWithPool(prefix string, length int) string {
builder := stringBuilderPool.Get().(*strings.Builder)
defer stringBuilderPool.Put(builder)
builder.Reset()
builder.WriteString(prefix)
builder.WriteString("-")
for i := 0; i < length; i++ {
builder.WriteByte(charset[rand.Intn(len(charset))])
}
return builder.String()
}
var filter = bloom.NewWithEstimates(maxNumCodes, falsePositiveRate)
func worker(
prefix string,
length int,
resultChan chan<- CodeResult,
done <-chan struct{},
wg *sync.WaitGroup,
) {
defer wg.Done()
duplicatesFound := 0
uniqueCodesGenerated := 0
for {
select {
case <-done:
return // Exit the worker when done signal is received
default:
for uniqueCodesGenerated < maxNumCodes {
newCode := generateCodeWithPool(prefix, length)
if filter.Test([]byte(newCode)) {
// Duplicate found
duplicatesFound++
} else {
// Not a duplicate, add to the filter
filter.Add([]byte(newCode))
resultChan <- CodeResult{Code: newCode}
uniqueCodesGenerated++
}
}
}
}
}
func validatePrefix(prefix string) bool {
return len(prefix) >= minPrefixLength && len(prefix) <= maxPrefixLength
}
func validateLength(length int) bool {
return length >= minCodeLength && length <= maxCodeLength
}
func GenerateHash(data string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(data), 14)
return string(bytes), err
}
func sanitizeKey(input string) string {
pattern := "[^a-zA-Z]+"
re := regexp.MustCompile(pattern)
sanitizedKey := re.ReplaceAllString(input, "")
output := strings.ToLower(sanitizedKey)
return output[4:30]
}
func calculatePossibleOutcomes(digits int) int {
return int(math.Pow10(digits))
}
func clearLine() {
fmt.Print("\033[2K\r")
}
func openDirectoryInExplorer(directory string) {
var cmd *exec.Cmd
switch runtimeOS := runtime.GOOS; runtimeOS {
case "windows":
cmd = exec.Command("explorer", directory)
default:
fmt.Printf("Unsupported operating system: %s\r\n", runtimeOS)
return
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
_ = cmd.Run()
}
func loadingAnimation(counter int) string {
animation := []string{"> ", " >"}
return animation[counter]
}