-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
245 lines (210 loc) · 5.08 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
// +build go1.9
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"syscall"
//"github.com/yvasiyarov/gorelic"
"github.com/PowPool/dashpool/api"
"github.com/PowPool/dashpool/payouts"
"github.com/PowPool/dashpool/proxy"
"github.com/PowPool/dashpool/storage"
. "github.com/PowPool/dashpool/util"
"golang.org/x/crypto/ssh/terminal"
)
var (
LatestTag = ""
LatestTagCommitSHA1 = ""
LatestCommitSHA1 = ""
BuildTime = ""
)
var cfg proxy.Config
var backend *storage.RedisClient
func startProxy() {
s := proxy.NewProxy(&cfg, backend)
s.Start()
}
func startApi() {
s := api.NewApiServer(&cfg.Api, backend)
s.Start()
}
func startBlockUnlocker() {
u := payouts.NewBlockUnlocker(&cfg.BlockUnlocker, backend)
u.Start()
}
//func startPayoutsProcessor() {
// u := payouts.NewPayoutsProcessor(&cfg.Payouts, backend)
// u.Start()
//}
// this function is for performance profile
//func startNewrelic() {
// if cfg.NewrelicEnabled {
// nr := gorelic.NewAgent()
// nr.Verbose = cfg.NewrelicVerbose
// nr.NewrelicLicense = cfg.NewrelicKey
// nr.NewrelicName = cfg.NewrelicName
// nr.Run()
// }
//}
func readConfig(cfg *proxy.Config) {
configFileName := "config.json"
if len(os.Args) > 1 {
configFileName = os.Args[1]
}
configFileName, _ = filepath.Abs(configFileName)
log.Printf("Loading config: %v", configFileName)
configFile, err := os.Open(configFileName)
if err != nil {
log.Fatal("File error: ", err.Error())
}
defer configFile.Close()
jsonParser := json.NewDecoder(configFile)
if err := jsonParser.Decode(&cfg); err != nil {
log.Fatal("Config error: ", err.Error())
}
}
func readSecurityPass() ([]byte, error) {
fmt.Printf("Enter Security Password: ")
var fd int
if terminal.IsTerminal(int(syscall.Stdin)) {
fd = int(syscall.Stdin)
} else {
tty, err := os.Open("/dev/tty")
if err != nil {
return nil, errors.New("error allocating terminal")
}
defer tty.Close()
fd = int(tty.Fd())
}
SecurityPass, err := terminal.ReadPassword(fd)
if err != nil {
return nil, err
}
return SecurityPass, nil
}
func decryptPoolConfigure(cfg *proxy.Config, passBytes []byte) error {
b, err := Ae64Decode(cfg.UpstreamCoinBaseEncrypted, passBytes)
if err != nil {
return err
}
cfg.UpstreamCoinBase = string(b)
// check address
if !IsValidDashAddress(cfg.UpstreamCoinBase) {
return errors.New("decryptPoolConfigure: IsValidDashAddress")
}
b, err = Ae64Decode(cfg.Redis.PasswordEncrypted, passBytes)
if err != nil {
return err
}
cfg.Redis.Password = string(b)
return nil
}
func getDeviceIPs() (map[string]struct{}, error) {
ipAddrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
DeviceIPs := make(map[string]struct{})
for _, addr := range ipAddrs {
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ip4 := ipNet.IP.To4(); ip4 != nil {
DeviceIPs[ip4.String()] = struct{}{}
}
}
}
return DeviceIPs, nil
}
func initPeerName(cfg *proxy.Config) error {
deviceIPs, err := getDeviceIPs()
if err != nil {
return err
}
for _, c := range cfg.Cluster {
_, ok := deviceIPs[c.NodeIp]
if ok {
cfg.Name = c.NodeName
cfg.Id = c.NodeId
cfg.NodeIp = c.NodeIp
return nil
}
}
return errors.New("local Node is not in the Pool cluster")
}
func OptionParse() {
var showVer bool
flag.BoolVar(&showVer, "v", false, "show build version")
flag.Parse()
if showVer {
fmt.Printf("Latest Tag: %s\n", LatestTag)
fmt.Printf("Latest Tag Commit SHA1: %s\n", LatestTagCommitSHA1)
fmt.Printf("Latest Commit SHA1: %s\n", LatestCommitSHA1)
fmt.Printf("Build Time: %s\n", BuildTime)
os.Exit(0)
}
}
func main() {
OptionParse()
readConfig(&cfg)
//rand.Seed(time.Now().UnixNano())
// init log file
_ = os.Mkdir("logs", os.ModePerm)
iLogFile := "logs/info.log"
eLogFile := "logs/error.log"
sLogFile := "logs/share.log"
bLogFile := "logs/block.log"
InitLog(iLogFile, eLogFile, sLogFile, bLogFile, cfg.Log.LogSetLevel)
// set rlimit nofile value
SetRLimit(100000)
err := initPeerName(&cfg)
if err != nil {
Error.Fatal("initPeerName error: ", err.Error())
}
Info.Println("Init Peer Name as:", cfg.Name)
if cfg.Threads > 0 {
runtime.GOMAXPROCS(cfg.Threads)
Info.Printf("Running with %v threads", cfg.Threads)
}
//startNewrelic()
secPassBytes, err := readSecurityPass()
if err != nil {
Error.Fatal("Read Security Password error: ", err.Error())
}
err = decryptPoolConfigure(&cfg, secPassBytes)
if err != nil {
Error.Fatal("Decrypt Pool Configure error: ", err.Error())
}
backend = storage.NewRedisClient(&cfg.Redis, cfg.Coin)
pong, err := backend.Check()
if err != nil {
Error.Printf("Can't establish connection to backend: %v", err)
} else {
Error.Printf("Backend check reply: %v", pong)
}
defer func() {
if r := recover(); r != nil {
Error.Println(string(debug.Stack()))
}
}()
if cfg.Proxy.Enabled {
go startProxy()
}
if cfg.Api.Enabled {
go startApi()
}
if cfg.BlockUnlocker.Enabled {
go startBlockUnlocker()
}
//if cfg.Payouts.Enabled {
// go startPayoutsProcessor()
//}
quit := make(chan bool)
<-quit
}