-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodec_coinhive.go
140 lines (122 loc) · 3.33 KB
/
codec_coinhive.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
package stratum
import (
"context"
"encoding/json"
"io"
"net/rpc"
"strings"
"github.com/powerman/rpc-codec/jsonrpc2"
)
type CoinhiveServerCodec struct {
*serverCodec
req chServerRequest
}
// NewCoinhiveServerCodec returns a new rpc.ServerCodec for handling requests from the Coinhive Miner
func NewCoinhiveServerCodec(conn io.ReadWriteCloser) rpc.ServerCodec {
return &CoinhiveServerCodec{
serverCodec: &serverCodec{
dec: json.NewDecoder(conn),
enc: json.NewEncoder(conn),
c: conn,
ctx: context.Background(),
},
}
}
// NewCoinhiveServerCodecContext is NewCoinhiveServerCodec with given context provided
// within parameters for compatible RPC methods.
func NewCoinhiveServerCodecContext(ctx context.Context, conn io.ReadWriteCloser) rpc.ServerCodec {
codec := NewCoinhiveServerCodec(conn)
codec.(*CoinhiveServerCodec).ctx = ctx
return codec
}
type chServerRequest struct {
Version string `json:"jsonrpc"`
Method string `json:"type"`
Params *json.RawMessage `json:"params"`
ID *json.RawMessage `json:"id"`
}
type chServerResponse struct {
Method string `json:"type"`
Result interface{} `json:"params,omitempty"`
Error interface{} `json:"error,omitempty"`
}
type chNotification struct {
Method string `json:"type"`
Params interface{} `json:"params"`
}
// ReadRequestHeader implements rpc.ServerCodec
func (c *CoinhiveServerCodec) ReadRequestHeader(r *rpc.Request) (err error) {
// var raw json.RawMessage
if err := c.dec.Decode(&c.req); err != nil {
c.encmutex.Lock()
c.enc.Encode(chServerResponse{Error: errParse})
c.encmutex.Unlock()
return err
}
// if err := json.Unmarshal(raw, &c.req); err != nil {
// if err.Error() == "bad request" {
// c.encmutex.Lock()
// c.enc.Encode(chServerResponse{Error: errRequest})
// c.encmutex.Unlock()
// }
// return err
// }
r.ServiceMethod = strings.Title(c.req.Method)
if !strings.Contains(r.ServiceMethod, "mining") {
r.ServiceMethod = "mining." + r.ServiceMethod
}
return nil
}
// ReadRequestBody implements rpc.ServerCodec
func (c *CoinhiveServerCodec) ReadRequestBody(x interface{}) error {
if x == nil {
return nil
}
if x, ok := x.(jsonrpc2.WithContext); ok {
x.SetContext(c.ctx)
}
if c.req.Params == nil {
return nil
}
if err := json.Unmarshal(*c.req.Params, x); err != nil {
return jsonrpc2.NewError(errParams.Code, err.Error())
}
return nil
}
// WriteResponse implements rpc.ServerCodec
func (c *CoinhiveServerCodec) WriteResponse(r *rpc.Response, x interface{}) error {
// such silly business this
if c.req.Method == "submit" {
c.req.Method = "hash_accepted"
} else if c.req.Method == "auth" {
c.req.Method = "authed"
}
resp := chServerResponse{Method: c.req.Method}
if r.Error == "" {
if x == nil {
resp.Result = &null
} else {
resp.Result = x
}
} else {
raw := json.RawMessage(r.Error)
resp.Error = &raw
}
// mutex should not be necessary - io.Writer is meant to be threadsafe
// c.encmutex.Lock()
// defer c.encmutex.Unlock()
return c.enc.Encode(resp)
}
// Close implements rpc.ServerCodec
func (c *CoinhiveServerCodec) Close() error {
return c.c.Close()
}
func (c *CoinhiveServerCodec) Notify(method string, args interface{}) error {
payload := chNotification{
Method: method,
Params: args,
}
// c.encmutex.Lock()
// defer c.encmutex.Unlock()
return c.enc.Encode(payload)
}