@@ -12,6 +12,7 @@ import (
12
12
"net/http"
13
13
"os"
14
14
"os/exec"
15
+ "strings"
15
16
"time"
16
17
)
17
18
@@ -45,9 +46,8 @@ func unixClient(socketPath string) *http.Client {
45
46
}
46
47
}
47
48
48
- func RunNode (cfgPath , nodeSocketPath string ) error {
49
- // launchNode(cfgPath)
50
- c := unixClient (nodeSocketPath )
49
+ func RunNode (socketPath string ) error {
50
+ c := unixClient (socketPath )
51
51
res , err := c .Get ("http+unix://c/upcheck" )
52
52
if err != nil {
53
53
return err
@@ -58,32 +58,11 @@ func RunNode(cfgPath, nodeSocketPath string) error {
58
58
return errors .New ("Constellation Node API did not respond to upcheck request" )
59
59
}
60
60
61
- type SendRequest struct {
62
- Payload string `json:"payload"`
63
- From string `json:"from"`
64
- To []string `json:"to"`
65
- }
66
-
67
- type SendResponse struct {
68
- Key string `json:"key"`
69
- }
70
-
71
- type ReceiveRequest struct {
72
- Key string `json:"key"`
73
- To string `json:"to"`
74
- }
75
-
76
- type ReceiveResponse struct {
77
- Payload string `json:"payload"`
78
- }
79
-
80
61
type Client struct {
81
- httpClient * http.Client
82
- publicKey [32 ]byte
83
- b64PublicKey string
62
+ httpClient * http.Client
84
63
}
85
64
86
- func (c * Client ) do (path string , apiReq interface {}) (* http.Response , error ) {
65
+ func (c * Client ) doJson (path string , apiReq interface {}) (* http.Response , error ) {
87
66
buf := new (bytes.Buffer )
88
67
err := json .NewEncoder (buf ).Encode (apiReq )
89
68
if err != nil {
@@ -102,64 +81,40 @@ func (c *Client) do(path string, apiReq interface{}) (*http.Response, error) {
102
81
}
103
82
104
83
func (c * Client ) SendPayload (pl []byte , b64From string , b64To []string ) ([]byte , error ) {
105
- var from string
106
- if b64From == "" {
107
- from = c .b64PublicKey
108
- } else {
109
- from = b64From
110
- }
111
- req := & SendRequest {
112
- Payload : base64 .StdEncoding .EncodeToString (pl ),
113
- From : from ,
114
- To : b64To ,
115
- }
116
- res , err := c .do ("send" , req )
84
+ buf := bytes .NewBuffer (pl )
85
+ req , err := http .NewRequest ("POST" , "http+unix://c/sendraw" , buf )
117
86
if err != nil {
118
87
return nil , err
119
88
}
120
- defer res .Body .Close ()
121
- sres := new (SendResponse )
122
- err = json .NewDecoder (res .Body ).Decode (sres )
123
- if err != nil {
124
- return nil , err
89
+ if b64From != "" {
90
+ req .Header .Set ("c11n-from" , b64From )
125
91
}
126
- key , err := base64 .StdEncoding .DecodeString (sres .Key )
127
- if err != nil {
128
- return nil , err
92
+ req .Header .Set ("c11n-to" , strings .Join (b64To , "," ))
93
+ req .Header .Set ("Content-Type" , "application/octet-stream" )
94
+ res , err := c .httpClient .Do (req )
95
+ if err == nil && res .StatusCode != 200 {
96
+ return nil , fmt .Errorf ("Non-200 status code: %+v" , res )
129
97
}
130
- return key , nil
98
+ defer res .Body .Close ()
99
+ return ioutil .ReadAll (res .Body )
131
100
}
132
101
133
102
func (c * Client ) ReceivePayload (key []byte ) ([]byte , error ) {
134
- b64Key := base64 .StdEncoding .EncodeToString (key )
135
- req := & ReceiveRequest {
136
- Key : b64Key ,
137
- To : c .b64PublicKey ,
138
- }
139
- res , err := c .do ("receive" , req )
140
- if err != nil {
141
- return nil , err
142
- }
143
- defer res .Body .Close ()
144
- rres := new (ReceiveResponse )
145
- err = json .NewDecoder (res .Body ).Decode (rres )
103
+ req , err := http .NewRequest ("GET" , "http+unix://c/receiveraw" , nil )
146
104
if err != nil {
147
105
return nil , err
148
106
}
149
- pl , err := base64 .StdEncoding .DecodeString (rres .Payload )
150
- if err != nil {
151
- return nil , err
107
+ req .Header .Set ("c11n-key" , base64 .StdEncoding .EncodeToString (key ))
108
+ res , err := c .httpClient .Do (req )
109
+ if err == nil && res .StatusCode != 200 {
110
+ return nil , fmt .Errorf ("Non-200 status code: %+v" , res )
152
111
}
153
- return pl , nil
112
+ defer res .Body .Close ()
113
+ return ioutil .ReadAll (res .Body )
154
114
}
155
115
156
- func NewClient (publicKeyPath string , nodeSocketPath string ) (* Client , error ) {
157
- b64PublicKey , err := ioutil .ReadFile (publicKeyPath )
158
- if err != nil {
159
- return nil , err
160
- }
116
+ func NewClient (socketPath string ) (* Client , error ) {
161
117
return & Client {
162
- httpClient : unixClient (nodeSocketPath ),
163
- b64PublicKey : string (b64PublicKey ),
118
+ httpClient : unixClient (socketPath ),
164
119
}, nil
165
120
}
0 commit comments