derohe-proxy/proxy/client.go

97 lines
1.7 KiB
Go
Raw Normal View History

2022-05-03 22:08:36 +02:00
package proxy
import (
2022-05-05 01:34:44 +02:00
"bytes"
2022-05-03 22:08:36 +02:00
"crypto/tls"
2022-05-05 01:34:44 +02:00
"encoding/hex"
"encoding/json"
2022-05-03 22:08:36 +02:00
"fmt"
2022-05-05 01:34:44 +02:00
"math/rand"
2022-05-03 22:08:36 +02:00
"net/url"
"time"
2022-05-05 01:34:44 +02:00
"github.com/deroproject/derohe/block"
"github.com/deroproject/derohe/rpc"
2022-05-03 22:08:36 +02:00
"github.com/gorilla/websocket"
)
var connection *websocket.Conn
2022-05-03 22:08:36 +02:00
// proxy-client
2022-05-03 22:08:36 +02:00
func Start_client(v string, w string) {
var err error
2022-05-05 01:34:44 +02:00
rand.Seed(time.Now().UnixMilli())
2022-05-03 22:08:36 +02:00
for {
u := url.URL{Scheme: "wss", Host: v, Path: "/ws/" + w}
dialer := websocket.DefaultDialer
dialer.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
fmt.Println("Connect to node", v, "using wallet address", w)
connection, _, err = websocket.DefaultDialer.Dial(u.String(), nil)
2022-05-03 22:08:36 +02:00
if err != nil {
time.Sleep(5 * time.Second)
fmt.Println(err)
continue
}
for {
msg_type, recv_data, err := connection.ReadMessage()
2022-05-05 01:34:44 +02:00
if err != nil {
2022-05-03 22:08:36 +02:00
break
2022-05-05 01:34:44 +02:00
}
if msg_type != websocket.TextMessage {
continue
2022-05-03 22:08:36 +02:00
}
go SendTemplatesToNode(recv_data)
}
2022-05-03 22:08:36 +02:00
}
}
func SendToDaemon(buffer []byte) {
connection.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
connection.WriteMessage(websocket.TextMessage, buffer)
2022-05-05 01:34:44 +02:00
}
func edit_blob(input []byte) (output []byte) {
var err error
var params rpc.GetBlockTemplate_Result
var mbl block.MiniBlock
var raw_hex []byte
var out bytes.Buffer
if err = json.Unmarshal(input, &params); err != nil {
return
}
if raw_hex, err = hex.DecodeString(params.Blockhashing_blob); err != nil {
return
}
if mbl.Deserialize(raw_hex); err != nil {
return
}
for i := range mbl.Nonce {
mbl.Nonce[i] = rand.Uint32()
}
params.Blockhashing_blob = fmt.Sprintf("%x", mbl.Serialize())
encoder := json.NewEncoder(&out)
if err = encoder.Encode(params); err != nil {
return
}
output = out.Bytes()
return
}