derohe-proxy/proxy/client.go

97 lines
1.7 KiB
Go

package proxy
import (
"bytes"
"crypto/tls"
"encoding/hex"
"encoding/json"
"fmt"
"math/rand"
"net/url"
"time"
"github.com/deroproject/derohe/block"
"github.com/deroproject/derohe/rpc"
"github.com/gorilla/websocket"
)
var connection *websocket.Conn
// proxy-client
func Start_client(v string, w string) {
var err error
rand.Seed(time.Now().UnixMilli())
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)
if err != nil {
time.Sleep(5 * time.Second)
fmt.Println(err)
continue
}
for {
msg_type, recv_data, err := connection.ReadMessage()
if err != nil {
break
}
if msg_type != websocket.TextMessage {
continue
}
go SendTemplatesToNode(recv_data)
}
}
}
func SendToDaemon(buffer []byte) {
connection.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
connection.WriteMessage(websocket.TextMessage, buffer)
}
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
}