2022-05-24 17:17:12 +01:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"net/url"
|
2022-11-16 23:54:30 +00:00
|
|
|
"sync"
|
2022-05-24 17:17:12 +01:00
|
|
|
"time"
|
|
|
|
|
2022-10-03 15:35:05 +01:00
|
|
|
"derohe-proxy/config"
|
|
|
|
|
2022-05-24 17:17:12 +01:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
2022-07-19 20:44:13 +01:00
|
|
|
type (
|
|
|
|
GetBlockTemplate_Params struct {
|
|
|
|
Wallet_Address string `json:"wallet_address"`
|
|
|
|
Block bool `json:"block"`
|
|
|
|
Miner string `json:"miner"`
|
|
|
|
}
|
|
|
|
GetBlockTemplate_Result struct {
|
|
|
|
JobID string `json:"jobid"`
|
|
|
|
Blocktemplate_blob string `json:"blocktemplate_blob,omitempty"`
|
|
|
|
Blockhashing_blob string `json:"blockhashing_blob,omitempty"`
|
|
|
|
Difficulty string `json:"difficulty"`
|
|
|
|
Difficultyuint64 uint64 `json:"difficultyuint64"`
|
|
|
|
Height uint64 `json:"height"`
|
|
|
|
Prev_Hash string `json:"prev_hash"`
|
|
|
|
EpochMilli uint64 `json:"epochmilli"`
|
|
|
|
Blocks uint64 `json:"blocks"` // number of blocks found
|
|
|
|
MiniBlocks uint64 `json:"miniblocks"` // number of miniblocks found
|
|
|
|
Rejected uint64 `json:"rejected"` // reject count
|
|
|
|
LastError string `json:"lasterror"` // last error
|
|
|
|
Status string `json:"status"`
|
|
|
|
Orphans uint64 `json:"orphans"`
|
|
|
|
Hansen33Mod bool `json:"hansen33_mod"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-11-16 23:54:30 +00:00
|
|
|
type clientConn struct {
|
|
|
|
conn *websocket.Conn
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
var connection clientConn
|
2022-05-24 17:17:12 +01:00
|
|
|
var Blocks uint64
|
|
|
|
var Minis uint64
|
|
|
|
var Rejected uint64
|
2022-07-19 20:44:13 +01:00
|
|
|
var Orphans uint64
|
2023-03-27 22:13:50 +01:00
|
|
|
|
|
|
|
var Hashrate uint64
|
|
|
|
var Connected int64
|
|
|
|
var difficulty uint64
|
2022-05-24 17:17:12 +01:00
|
|
|
|
|
|
|
// proxy-client
|
2022-10-03 15:35:05 +01:00
|
|
|
func Start_client(w string) {
|
2022-05-24 17:17:12 +01:00
|
|
|
var err error
|
2022-05-28 11:52:52 +01:00
|
|
|
var last_diff uint64
|
|
|
|
var last_height uint64
|
2022-05-24 17:17:12 +01:00
|
|
|
|
|
|
|
rand.Seed(time.Now().UnixMilli())
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
2022-10-03 15:35:05 +01:00
|
|
|
u := url.URL{Scheme: "wss", Host: config.Daemon_address, Path: "/ws/" + w}
|
2022-05-24 17:17:12 +01:00
|
|
|
|
|
|
|
dialer := websocket.DefaultDialer
|
|
|
|
dialer.TLSClientConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
|
|
|
|
2022-10-03 15:35:05 +01:00
|
|
|
if !config.Pool_mode {
|
|
|
|
fmt.Printf("%v Connected to node %v\n", time.Now().Format(time.Stamp), config.Daemon_address)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%v Connected to node %v using wallet %v\n", time.Now().Format(time.Stamp), config.Daemon_address, w)
|
|
|
|
}
|
2022-11-16 23:54:30 +00:00
|
|
|
connection.conn, _, err = websocket.DefaultDialer.Dial(u.String(), nil)
|
2022-05-24 17:17:12 +01:00
|
|
|
if err != nil {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
fmt.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-19 20:44:13 +01:00
|
|
|
var params GetBlockTemplate_Result
|
2022-05-24 17:17:12 +01:00
|
|
|
|
|
|
|
for {
|
2022-11-16 23:54:30 +00:00
|
|
|
msg_type, recv_data, err := connection.conn.ReadMessage()
|
2022-05-24 17:17:12 +01:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg_type != websocket.TextMessage {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.Unmarshal(recv_data, ¶ms); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
Blocks = params.Blocks
|
|
|
|
Minis = params.MiniBlocks
|
|
|
|
Rejected = params.Rejected
|
2022-07-19 20:44:13 +01:00
|
|
|
Orphans = params.Orphans
|
2022-05-24 17:17:12 +01:00
|
|
|
|
2023-03-27 22:13:50 +01:00
|
|
|
if config.Pool_mode {
|
|
|
|
difficulty = params.Difficultyuint64
|
2022-07-19 20:44:13 +01:00
|
|
|
}
|
|
|
|
|
2022-10-03 15:35:05 +01:00
|
|
|
if config.Minimal {
|
2022-05-28 11:52:52 +01:00
|
|
|
if params.Height != last_height || params.Difficultyuint64 != last_diff {
|
|
|
|
last_height = params.Height
|
|
|
|
last_diff = params.Difficultyuint64
|
2022-10-03 15:35:05 +01:00
|
|
|
go SendTemplateToNodes(recv_data)
|
2022-05-28 11:52:52 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-10-03 15:35:05 +01:00
|
|
|
go SendTemplateToNodes(recv_data)
|
2022-05-28 11:52:52 +01:00
|
|
|
}
|
2022-05-24 17:17:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SendToDaemon(buffer []byte) {
|
2022-11-16 23:54:30 +00:00
|
|
|
connection.Lock()
|
|
|
|
defer connection.Unlock()
|
|
|
|
|
|
|
|
connection.conn.WriteMessage(websocket.TextMessage, buffer)
|
|
|
|
|
2022-05-24 17:17:12 +01:00
|
|
|
}
|