-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
186 lines (151 loc) · 4.37 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
/* SuchMiner
* Copyright 2022 duggavo
*
* 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 <http://www.gnu.org/licenses/>.
*/
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
)
var debug = false
var Log *log.Logger
const VERSION = "1.0.0"
const Reset = "\u001b[0m"
const Red = "\u001b[31m"
const Green = "\u001b[32m"
const Yellow = "\u001b[33m"
const Magenta = "\u001b[35m"
const Cyan = "\u001b[36m"
const Bright = "\u001b[1m"
var hashrate float64 = 0
var hashrateRegex = regexp.MustCompile(`10s\/60s\/15m [\d\.n/a]+ [\d\.n/a]+`)
var addressRegex = regexp.MustCompile("^W[Wo][0-9AB][123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{94}$")
var spendKeyRegex = regexp.MustCompile("^[0-9a-fA-F]{64}$")
var address string
var spendKey string
var url string
func main() {
flag.BoolVar(&debug, "debug", false, "")
flag.Parse()
if debug {
Log = log.New(os.Stdout, Reset, log.Ldate|log.Ltime|log.Lshortfile)
} else {
Log = log.New(os.Stdout, Reset, log.Ldate|log.Ltime)
}
Log.Println(Reset + Cyan)
Log.Println(Cyan+"Starting", Magenta+Bright+"Such"+Yellow+"Miner "+Reset+Cyan+"v"+VERSION)
Log.Println(Reset)
if _, err := os.Stat("./deps/config.json"); errors.Is(err, os.ErrNotExist) {
getConfig()
setConfig()
}
var wowrig *exec.Cmd
if runtime.GOOS == "windows" {
wowrig = exec.Command("./deps/wowrig.exe", "--no-color")
} else {
wowrig = exec.Command("./deps/wowrig", "--no-color")
}
var outb saveOutput
wowrig.Stdout = &outb
Log.Println(Cyan + "Miner started")
wowrig.Run()
}
func getConfig() {
address = Prompt(Cyan + "Wownero address: " + Reset)
spendKey = Prompt(Cyan + "Spend key: " + Reset)
isRunningDaemon := Prompt(Cyan + "Are you running a Wownero daemon? (y/n) " + Reset)
if strings.HasPrefix(isRunningDaemon, "y") {
url = "127.0.0.1:34568"
} else {
url = "wowrig.mooo.com:34568"
}
if !addressRegex.Match([]byte(address)) {
fmt.Println(Red + "Error: address '" + address + "' is not valid")
return
} else if !spendKeyRegex.Match([]byte(spendKey)) {
fmt.Println(Red + "Error: spend key is not valid")
return
}
}
func setConfig() {
config := default_config
config = strings.Replace(config, "$ADDRESS", address, 1)
config = strings.Replace(config, "$SPEND", spendKey, 1)
config = strings.Replace(config, "$URL", url, 1)
os.WriteFile("./deps/config.json", []byte(config), 0600)
}
type saveOutput struct {
}
func (so *saveOutput) Write(p []byte) (n int, err error) {
parseHashrate(string(p))
parseMsr(string(p))
if debug {
os.Stdout.Write(p)
}
return len(p), nil
}
func Prompt(l string) string {
var s string
r := bufio.NewReader(os.Stdin)
for {
fmt.Fprint(os.Stdout, l)
s, _ = r.ReadString('\n')
if s != "" {
break
}
}
return strings.TrimSpace(s)
}
func parseHashrate(txt string) {
txt = strings.Split(txt, "max")[0]
indexes := hashrateRegex.FindIndex([]byte(txt))
if indexes == nil {
return
}
hashrateStr := strings.Split(string(txt[indexes[0]+12:indexes[0]+20]), " ")[0]
if hashrateStr == "n/a" {
return
}
hashrateNum, err := strconv.ParseFloat(hashrateStr, 64)
if err != nil {
panic(err)
}
if strings.Contains(txt, "KH/s") {
hashrateNum *= 1000
} else if strings.Contains(txt, "MH/s") {
hashrateNum *= 1000 * 1000
} else if strings.Contains(txt, "GH/s") {
hashrateNum *= 1000 * 1000 * 1000
}
hashrate = hashrateNum
Log.Println(Reset+Cyan+"Hashrate:"+Yellow, uint64(hashrate), Cyan+"H/s")
}
func parseMsr(txt string) {
if strings.Contains(txt, "FAILED TO APPLY MSR") {
Log.Println(Reset + Red + "Failed to apply MSR mod: hashrate will be low.")
Log.Println(Red + "Try running this miner as root." + Reset)
} else if strings.Contains(txt, "preset have been set") {
Log.Println(Reset + Green + "MSR mod has been applied! You are mining at best efficiency.")
}
}