@@ -12,6 +12,7 @@ import (
1212 "net/http"
1313 "os"
1414 "os/exec"
15+ "strings"
1516 "time"
1617)
1718
@@ -45,9 +46,8 @@ func unixClient(socketPath string) *http.Client {
4546 }
4647}
4748
48- func RunNode (cfgPath , nodeSocketPath string ) error {
49- // launchNode(cfgPath)
50- c := unixClient (nodeSocketPath )
49+ func RunNode (socketPath string ) error {
50+ c := unixClient (socketPath )
5151 res , err := c .Get ("http+unix://c/upcheck" )
5252 if err != nil {
5353 return err
@@ -58,32 +58,11 @@ func RunNode(cfgPath, nodeSocketPath string) error {
5858 return errors .New ("Constellation Node API did not respond to upcheck request" )
5959}
6060
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-
8061type Client struct {
81- httpClient * http.Client
82- publicKey [32 ]byte
83- b64PublicKey string
62+ httpClient * http.Client
8463}
8564
86- func (c * Client ) do (path string , apiReq interface {}) (* http.Response , error ) {
65+ func (c * Client ) doJson (path string , apiReq interface {}) (* http.Response , error ) {
8766 buf := new (bytes.Buffer )
8867 err := json .NewEncoder (buf ).Encode (apiReq )
8968 if err != nil {
@@ -102,64 +81,40 @@ func (c *Client) do(path string, apiReq interface{}) (*http.Response, error) {
10281}
10382
10483func (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 )
11786 if err != nil {
11887 return nil , err
11988 }
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 )
12591 }
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 )
12997 }
130- return key , nil
98+ defer res .Body .Close ()
99+ return ioutil .ReadAll (res .Body )
131100}
132101
133102func (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 )
146104 if err != nil {
147105 return nil , err
148106 }
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 )
152111 }
153- return pl , nil
112+ defer res .Body .Close ()
113+ return ioutil .ReadAll (res .Body )
154114}
155115
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 ) {
161117 return & Client {
162- httpClient : unixClient (nodeSocketPath ),
163- b64PublicKey : string (b64PublicKey ),
118+ httpClient : unixClient (socketPath ),
164119 }, nil
165120}
0 commit comments