/
node.go
165 lines (151 loc) · 3.43 KB
/
node.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
package constellation
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/tv42/httpunix"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"time"
)
func launchNode(cfgPath string) (*exec.Cmd, error) {
cmd := exec.Command("constellation-node", cfgPath)
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
go io.Copy(os.Stderr, stderr)
if err := cmd.Start(); err != nil {
return nil, err
}
time.Sleep(100 * time.Millisecond)
return cmd, nil
}
func unixTransport(socketPath string) *httpunix.Transport {
t := &httpunix.Transport{
DialTimeout: 1 * time.Second,
RequestTimeout: 5 * time.Second,
ResponseHeaderTimeout: 5 * time.Second,
}
t.RegisterLocation("c", socketPath)
return t
}
func unixClient(socketPath string) *http.Client {
return &http.Client{
Transport: unixTransport(socketPath),
}
}
func RunNode(cfgPath, nodeSocketPath string) error {
// launchNode(cfgPath)
c := unixClient(nodeSocketPath)
res, err := c.Get("http+unix://c/upcheck")
if err != nil {
return err
}
if res.StatusCode == 200 {
return nil
}
return errors.New("Constellation Node API did not respond to upcheck request")
}
type SendRequest struct {
Payload string `json:"payload"`
From string `json:"from"`
To []string `json:"to"`
}
type SendResponse struct {
Key string `json:"key"`
}
type ReceiveRequest struct {
Key string `json:"key"`
To string `json:"to"`
}
type ReceiveResponse struct {
Payload string `json:"payload"`
}
type Client struct {
httpClient *http.Client
publicKey [32]byte
b64PublicKey string
}
func (c *Client) do(path string, apiReq interface{}) (*http.Response, error) {
buf := new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(apiReq)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", "http+unix://c/"+path, buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
res, err := c.httpClient.Do(req)
if err == nil && res.StatusCode != 200 {
return nil, fmt.Errorf("Non-200 status code: %+v", res)
}
return res, err
}
func (c *Client) SendPayload(pl []byte, b64From string, b64To []string) ([]byte, error) {
var from string
if b64From == "" {
from = c.b64PublicKey
} else {
from = b64From
}
req := &SendRequest{
Payload: base64.StdEncoding.EncodeToString(pl),
From: from,
To: b64To,
}
res, err := c.do("send", req)
if err != nil {
return nil, err
}
defer res.Body.Close()
sres := new(SendResponse)
err = json.NewDecoder(res.Body).Decode(sres)
if err != nil {
return nil, err
}
key, err := base64.StdEncoding.DecodeString(sres.Key)
if err != nil {
return nil, err
}
return key, nil
}
func (c *Client) ReceivePayload(key []byte) ([]byte, error) {
b64Key := base64.StdEncoding.EncodeToString(key)
req := &ReceiveRequest{
Key: b64Key,
To: c.b64PublicKey,
}
res, err := c.do("receive", req)
if err != nil {
return nil, err
}
defer res.Body.Close()
rres := new(ReceiveResponse)
err = json.NewDecoder(res.Body).Decode(rres)
if err != nil {
return nil, err
}
pl, err := base64.StdEncoding.DecodeString(rres.Payload)
if err != nil {
return nil, err
}
return pl, nil
}
func NewClient(publicKeyPath string, nodeSocketPath string) (*Client, error) {
b64PublicKey, err := ioutil.ReadFile(publicKeyPath)
if err != nil {
return nil, err
}
return &Client{
httpClient: unixClient(nodeSocketPath),
b64PublicKey: string(b64PublicKey),
}, nil
}