-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
648 lines (592 loc) · 16.5 KB
/
Copy pathmain.go
File metadata and controls
648 lines (592 loc) · 16.5 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
package main
import (
"crypto/sha1"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"os"
"reflect"
"runtime"
"strconv"
"strings"
"time"
"github.com/NDRAEY/Pradz"
)
type Balance struct {
Result struct {
Balance struct {
Balance float32 `json:"balance"`
Created string
Username string `json:"username"`
Verifed string `json:"verifed"`
} `json:"balance"`
Miners []byte `json:"miners"`
Transactions []byte `json:"transactions"`
}
Server string
Success bool
}
type PoolInfo struct {
Ip string
Name string
Port int32
Server string
Success bool
}
type Stats struct {
Connections int `json:"Active connections"`
TotalMined float64 `json:"All-timed mined DUCO"`
CurrentDifficulty int `json:"Current difficulty"`
DUCOHashrate string
JustSwap float32
NodeS float32
PancakeSwap float32
SushiSwap float32
DUCO float32 `json:"DUCO price"`
BCH float32
NANO float32
TRX float32
XMG float32
Api string
Kolka struct {
Banned int
Jailed int
}
LastHash string
LastSync float64
LastUpdated string
MinedHashes string
}
type Currency struct {
Date string
Usd struct {
Ada, Aed, Afm float32
Afn, All, Amd, Aoa, Ars, Aud, Awg, Azn float32
Bam, Bbd, Bch, Bdt, Bgn, Bhd, Bif float32
Bmd, Bnb, Bnd, Bob, Brl, Bsd, Btc, Btn, Bwp float32
Byn, Byr, Bzd, Cad, Cdf, Chf, Clf, Clp, Cny float32
Cop, Crc, Cuc, Cup, Cve, Czk, Djf, Dkk, Doge float32
Dop, Dzd, Egp, Ern, Etb, Etc, Eth, Eur, Fjd float32
Fkp, Gbp, Gel, Ggp, Ghs, Gip, Gmd, Gnf, Gtq float32
Gyd, Hkd, Hnl, Hrk, Htg, Huf, Idr, Ils, Imp float32
Inr, Iqd, Irr, Isk, Jep, bJmd, Jod, Jpy, Kes float32
Kgs, Khr, Kmf, Kpw, Krw, Kwd, Kyd, Kzt, Lak float32
Lbp, Link, Lkr, Lrd, Isl, Ltc, Itl, Lvl, Lyd float32
Mad, Mdl, Mga, Mkd, Mmk, Mnt, Mop, Mro, Mur float32
Mvr, Mwk, Mxn, Myr, Mzn, Nad, Ngn, Nio, Nok float32
Npr, Nzd, Omr, Pab, Pen, Pgk, Php, Pkr, Pln, Pyg, Qar, Ron, Rsd float32
Rub, Rwf, Sar, Sbd, Scr, Sdg, Sek float32
Sgd, Shp, Sll, Sos, Srd, Std, Svc, Syp, Szl, Thb float32
Theta, Tjs, Tmt, Tnd, Top, Trx, Try, Ttd, Twd, Tzs float32
Uah, Ugx, Usd, Usdt float32
Uyu, Uzs, Vef, Vnd, Vuv, Wst, Xaf, Xag float32
Xau, Xcd, Xdr, Xlm, Xmr, Xof, Xpf, Xrp float32
Yer, Zar, Zmk, Zmw, Zwl float32
}
}
type Configuration struct {
Username string `json:"username"`
Currency string `json:"currency"`
Difficulty string `json:"diff"`
Threads string `json:"threads"`
FeedEvery int32 `json:"feed-every"`
}
var acc int
var username string = "ndraey"
var data Balance
var pool PoolInfo
var threads int = 7
var priced Stats
var additconv string = "none"
var currency Currency
var config Configuration
var defconfigfile string = "config.json"
var feedevery time.Duration
var difficulty string = "low"
var balance float32
var oldbalance float32
var version string = "v1.2"
func main() {
feedevery = time.Duration(45)
threads = runtime.NumCPU()
/*
args:=os.Args[1:]
for i:=0; i<len(args); i++ {
if args[i]=="-t" { //threads
if len(args)>i+1 { //exists
threads, _ = strconv.Atoi(args[i+1])
args=RemoveIndex(args,i)
args=RemoveIndex(args,i)
}else{
print("Threads (-t) parameter needs argument\n")
os.Exit(1)
}
}
if len(args)==0 {break}
if args[i]=="-h" {
print(os.Args[0]+` [-t THREADS] nickname`+"\n")
os.Exit(1)
}
}
if len(args)>=1 {
username = args[len(args)-1]
}
*/
if _, err := os.Stat(defconfigfile); errors.Is(err, os.ErrNotExist) {
fmt.Printf("Welcome to PulseMiner configuration menu!\n\n")
fmt.Printf("\n")
var usr, crn, dif, thr string
var fev int
for usr == "" {
print("Your username: ")
fmt.Scanf("%s", &usr)
}
print("Choose your currency [RUB,UAH,CZK... or none]: ")
fmt.Scanf("%s", &crn)
for dif != "low" && dif != "medium" && dif != "high" {
print("Choose difficulty [low,medium,high]: ")
fmt.Scanf("%s", &dif)
}
print("Threads [or auto]: ")
fmt.Scanf("%s", &thr)
print("Print balance every (seconds) [default: 45]: ")
fmt.Scanf("%d", &fev)
if fev == 0 {
fev = 45
}
file, err2 := os.Create("config.json")
defer file.Close()
if err2 != nil {
log.Fatal(err)
}
err3 := ioutil.WriteFile("config.json",
[]byte("{\n\t"+
mkjsonentry("username", usr, 1)+"\n\t"+
mkjsonentry("currency", crn, 1)+"\n\t"+
mkjsonentry("diff", dif, 1)+"\n\t"+
mkjsonentry("threads", thr, 1)+"\n\t"+
mkjsonentry("feed-every", fev, 0)+"\n"+
"}\n"),
0644)
if err3 != nil {
log.Fatal(err)
}
}
rawconfig, err := ioutil.ReadFile(defconfigfile)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
json.Unmarshal(rawconfig, &config)
if config.Username != "" {
username = config.Username
}
if config.Currency != "" {
if isinarray(getcurrs(), strings.Title(strings.ToLower(config.Currency))) {
additconv = config.Currency
} else {
fmt.Printf("Warning: Currency %s not found in list. Setting to none...\n", config.Currency)
}
}
if config.Threads != "" {
if config.Threads != "auto" {
th, err := strconv.Atoi(config.Threads)
if err != nil {
log.Fatal(err)
}
threads = th
}
}
if config.FeedEvery != 0 {
feedevery = time.Duration(config.FeedEvery)
}
if config.Difficulty != "" {
difficulty = config.Difficulty
}
/*
bannerw:=47
unlen:=14+len(username)
currlen:=21+len(additconv)
threadslen:=12+len(strconv.Itoa(threads))
fevlen:=31+len(strconv.Itoa(int(feedevery)))
dlen:=15+len(difficulty)
verlen:=26+len(version)
print("-----------------------------------------------\n")
print("| Welcome to PulseMiner "+version+strings.Repeat(" ",bannerw-verlen)+" |\n")
print("| |\n")
print("| Mining to: "+username+strings.Repeat(" ",bannerw-unlen)+"|\n")
print("| Currency to show: "+additconv+strings.Repeat(" ",bannerw-currlen)+"|\n")
print("| Threads: "+strconv.Itoa(threads)+strings.Repeat(" ",bannerw-threadslen)+"|\n")
print("| Difficulty: "+strings.ToUpper(difficulty)+strings.Repeat(" ",bannerw-dlen)+"|\n")
print("| Show balance every: "+strconv.Itoa(int(feedevery))+" seconds"+strings.Repeat(" ",bannerw-fevlen)+"|\n")
print("-----------------------------------------------\n")
*/
table := Pradz.PradzTable{}
table.Init()
table.AddElement("Welcome to PulseMiner " + version + "!")
table.AddElement("")
table.AddElement("Mining to: " + username)
table.AddElement("Currency to show: " + additconv)
table.AddElement("Threads: " + strconv.Itoa(threads))
table.AddElement("Difficulty: " + strings.ToUpper(difficulty))
table.AddElement("Show balance every: " + strconv.Itoa(int(feedevery)) + " seconds")
fmt.Printf("%s\n", table.Render())
print("\nGetting best pool...\n")
tothashrate := make([]float64, threads)
for {
pooldata, err := getnet("https://server.duinocoin.com/getPool")
if err != nil {
print("Error getting pool! Press Ctrl-C to exit...\n")
for {
}
}
json.Unmarshal(pooldata, &pool)
fmt.Printf("Selected pool %s:%d\n", pool.Ip, pool.Port)
if pool.Ip == "" {
fmt.Printf("%sWarning:%s Received empty ip, retrying...\n",
colorize(93),
colorize(0))
} else {
break
}
}
print("Connecting...\n")
rand.Seed(time.Now().UnixNano())
minerid := rand.Int31() % 32768 * 2
// Main function
for i := 0; i < threads; i++ {
go mine(pool.Ip, pool.Port, minerid, i, tothashrate)
time.Sleep(10 * time.Millisecond)
}
print("[1] Preparing some data...\n")
for {
info, err := http.Get("https://server.duinocoin.com/users/" + username)
if err != nil {
log.Fatal(err)
time.Sleep(2 * time.Second)
continue
}
body, err := ioutil.ReadAll(info.Body)
if err != nil {
log.Fatal(err)
time.Sleep(2 * time.Second)
continue
}
json.Unmarshal(body, &data)
oldbalance = data.Result.Balance.Balance
balance = oldbalance // avoiding report at startup
break
}
print("[2] Preparing some data...\n")
for {
info, err := http.Get("https://server.duinocoin.com/users/" + username)
if err != nil {
log.Fatal(err)
time.Sleep(2 * time.Second)
continue
}
body, err := ioutil.ReadAll(info.Body)
if err != nil {
log.Fatal(err)
time.Sleep(2 * time.Second)
continue
}
json.Unmarshal(body, &data)
balance = data.Result.Balance.Balance
pricedata, err := getnet("https://server.duinocoin.com/statistics")
if err != nil {
log.Fatal(err)
time.Sleep(2 * time.Second)
}
json.Unmarshal(pricedata, &priced)
if additconv != "none" {
fmt.Printf("[feed] Balance: %s%.5f%s DUCO (≈%s%.5f%s USD)"+
currconv(balance*priced.DUCO)+"\n",
colorize(93),
balance,
colorize(0),
colorize(93),
balance*priced.DUCO,
colorize(0))
} else {
fmt.Printf("[feed] Balance: %s%.5f%s DUCO (≈%s%.5f%s USD)\n",
colorize(93),
balance,
colorize(0),
colorize(93),
balance*priced.DUCO,
colorize(0))
}
if balance-oldbalance > 0 {
baldiffs := ((balance - oldbalance) / float32(feedevery))
print(strings.Repeat("=", 65) + "\n")
fmt.Printf("[report] %s+%.8f%s DUCO\n",
colorize(92),
balance-oldbalance,
colorize(0))
if additconv != "none" {
fmt.Printf("[report] Hourly: %s%.5f%s/hr (≈%s%.5f%s USD) %s\n",
colorize(92),
baldiffs*3600,
colorize(0),
colorize(92),
baldiffs*3600*priced.DUCO,
colorize(0),
currconv(baldiffs*3600*priced.DUCO))
fmt.Printf("[report] Daily: %s%.5f%s/day (≈%s%.5f%s USD) %s\n",
colorize(92),
baldiffs*3600*24,
colorize(0),
colorize(92),
baldiffs*3600*24*priced.DUCO,
colorize(0),
currconv(baldiffs*3600*24*priced.DUCO))
fmt.Printf("[report] Weekly: %s%.5f%s/wk (≈%s%.5f%s USD) %s\n",
colorize(92),
baldiffs*3600*24*7,
colorize(0),
colorize(92),
baldiffs*3600*24*7*priced.DUCO,
colorize(0),
currconv(baldiffs*3600*24*7*priced.DUCO))
// Just average 30.
fmt.Printf("[report] Monthly: %s%.5f%s/mon (≈%s%.5f%s USD) %s\n",
colorize(92),
baldiffs*3600*24*30,
colorize(0),
colorize(92),
baldiffs*3600*24*30*priced.DUCO,
colorize(0),
currconv(baldiffs*3600*24*30*priced.DUCO))
} else {
fmt.Printf("[report] Hourly: %s%.5f%s/hr (≈%s%.5f%s USD)\n",
colorize(92),
baldiffs*3600,
colorize(0),
colorize(92),
baldiffs*3600*priced.DUCO,
colorize(0))
fmt.Printf("[report] Daily: %s%.5f%s/day (≈%s%.5f%s USD)\n",
colorize(92),
baldiffs*3600*24,
colorize(0),
colorize(92),
baldiffs*3600*24*priced.DUCO,
colorize(0))
fmt.Printf("[report] Weekly: %s%.5f%s/wk (≈%s%.5f%s USD)\n",
colorize(92),
baldiffs*3600*24*7,
colorize(0),
colorize(92),
baldiffs*3600*24*7*priced.DUCO,
colorize(0))
fmt.Printf("[report] Monthly: %s%.5f%s/mon (≈%s%.5f%s USD)\n",
colorize(92),
baldiffs*3600*24*30,
colorize(0),
colorize(92),
baldiffs*3600*24*30*priced.DUCO,
colorize(0))
}
print(strings.Repeat("=", 65) + "\n")
}
time.Sleep(feedevery * time.Second)
oldbalance = balance
}
}
func getnet(url string) ([]byte, error) {
info, err := http.Get(url)
if err != nil {
log.Fatal(err)
return []byte(""), err
} else {
body, err := ioutil.ReadAll(info.Body)
return body, err
}
}
func sum(array []float64) float64 {
result := float64(0)
for _, v := range array {
result += v
}
return result
}
func currconv(value float32) string {
r := fmt.Sprintf(" (%.5f %s)",
currconvf(value), strings.ToUpper(additconv))
return r
}
func currconvf(value float32) float32 {
st1, err := getnet("https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/usd.json")
if err != nil {
log.Fatal(err)
}
json.Unmarshal(st1, ¤cy)
r := reflect.ValueOf(currency.Usd)
res := reflect.Indirect(r).FieldByName(strings.Title(strings.ToLower(additconv)))
return float32(res.Float()) * value
}
func colorize(color int) string {
str := ""
if runtime.GOOS == "windows" {
str = "\x1b[" + strconv.Itoa(color) + "m"
} else {
str = "\033[" + strconv.Itoa(color) + "m"
}
return str
}
func mine(ip string, port, minerid int32, cpuid int, thr []float64) {
fmt.Printf("[cpu%d] Started...\n", cpuid)
conn, _ := net.Dial("tcp", ip+":"+strconv.Itoa(int(port)))
ver := make([]byte, 10)
conn.Read(ver)
for {
job_ := string(writejobreq(conn))
job := strings.Split(job_, ",")
if len(job) < 3 {
fmt.Printf("[cpu%d] %sError:%s Thread #%d got empty job! PulseMiner stopped this thread.\n",
cpuid,
colorize(91),
colorize(0),
cpuid)
thr[cpuid] = 0
fmt.Printf("[cpu%d] Trying to reload in 5s...\n",
cpuid)
time.Sleep(5 * time.Second)
mine(ip, port, minerid, cpuid, thr)
break
}
jh := job[0]
nh := job[1]
diff := strings.Replace(string(job[2]), "\n", "", -1)
diff = strings.Replace(string(diff), "\x00", "", -1)
diffi, err := strconv.ParseInt(diff, 10, 32)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
nonce, hashrate, time := DecodeHash(jh, nh, int(diffi))
conn.Write([]byte(
strconv.Itoa(int(nonce)) + "," +
strconv.Itoa(int(hashrate)) + ",Official PC Miner,pulsemon-go,," +
strconv.Itoa(int(minerid))))
result := make([]byte, 32)
conn.Read(result)
thr[cpuid] = hashrate
if strings.HasPrefix(string(result), "GOOD") {
acc++
fmt.Printf("[%d] [cpu%d] \033[32mGOOD\033[0m H: %sH/s T: %.2fs\n",
acc,
cpuid,
convr(sum(thr)),
time)
} else if strings.HasPrefix(string(result), "BAD") {
fmt.Printf("%s\n", string(result))
fmt.Printf("[%d] [cpu%d] \033[31;1mBAD\033[0m H: %sH/s T: %.2fs\n",
acc,
cpuid,
convr(sum(thr)),
time)
}
}
}
func gettime() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
func RemoveIndex(s []string, i int) []string {
return s[:i+copy(s[i:], s[i+1:])]
}
func convr(num float64) string {
rts := ""
if num > (1000 * 1000 * 1000) {
k := strconv.FormatFloat(num/(1000*1000*1000), 'f', 2, 64)
rts = k + "G"
} else if num > (1000 * 1000) {
k := strconv.FormatFloat(num/(1000*1000), 'f', 2, 64)
rts = k + "M"
} else if num > 1000 {
k := strconv.FormatFloat(num/1000, 'f', 2, 64)
rts = k + "k"
} else {
rts = strconv.Itoa(int(num))
}
return rts
}
func DecodeHash(prev string, result string, diff int) (int, float64, float64) {
i := int(0)
hr := float64(0)
tm := float64(gettime() / 1000)
tmm := float64(0)
for i = 0; i < (100*diff + 1); i++ {
hash := sha1.Sum([]byte(prev + strconv.Itoa(i)))
hashd := fmt.Sprintf("%02x", hash)
if hashd == result {
tmm = float64(gettime()/1000) - tm
if tmm <= 1 {
tmm = 1
}
hr = float64(i) / tmm
break
}
}
return i, hr, tmm
}
func writejobreq(con net.Conn) []byte {
con.Write([]byte("JOB," + username + "," + strings.ToUpper(difficulty)))
//con.Write([]byte("JOB," + username + ",AVR"))
req := make([]byte, 90)
con.Read(req)
time.Sleep(20 * time.Millisecond) // Not affect on mining speed
return req
}
func copystr(dst io.Writer, src io.Reader) {
if _, err := io.Copy(dst, src); err != nil {
log.Fatal(err)
}
}
func mkjsonentry(key string, value interface{}, cl int) string {
tstr := "\"" + key + "\":"
switch value.(type) {
case int32:
tstr += strconv.Itoa(value.(int))
case int64:
tstr += strconv.Itoa(value.(int))
case int:
tstr += strconv.Itoa(value.(int))
case string:
tstr += "\"" + value.(string) + "\""
}
if cl == 1 {
tstr += ","
}
return tstr
}
func getcurrs() []string {
// Idk how to get all struct keys and made this stuff...
// It's just a simple parser turns a map with one object to string
// and finds keys
exa := map[Currency]string{Currency{}: "Pulsemon-Digimon"}
raw := fmt.Sprintf("%+v", exa)
raw = raw[strings.Index(raw, "{")+1:]
raw = raw[strings.Index(raw, "{")+1:]
raw = raw[:strings.Index(raw, "}")]
spl := strings.Split(raw, " ")
for idx, elm := range spl {
spl[idx] = strings.Split(elm, ":")[0]
}
return spl
}
func isinarray(array []string, value string) bool {
for _, b := range array {
if b == value {
return true
}
}
return false
}