Skip to content

Commit 2a5a1d6

Browse files
authored
Use sendraw/receiveraw and allow specifying Constellation IPC socket in PRIVATE_CONFIG (#278)
1 parent f651c1b commit 2a5a1d6

File tree

3 files changed

+50
-87
lines changed

3 files changed

+50
-87
lines changed

private/constellation/config.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import (
55
)
66

77
type Config struct {
8-
Socket string `toml:"socket"`
9-
PublicKeys []string `toml:"publickeys"`
8+
Socket string `toml:"socket"`
9+
WorkDir string `toml:"workdir"`
1010

1111
// Deprecated
12-
SocketPath string `toml:"socketPath"`
13-
PublicKeyPath string `toml:"publicKeyPath"`
12+
SocketPath string `toml:"socketPath"`
1413
}
1514

1615
func LoadConfig(configPath string) (*Config, error) {
@@ -22,8 +21,5 @@ func LoadConfig(configPath string) (*Config, error) {
2221
if cfg.Socket == "" {
2322
cfg.Socket = cfg.SocketPath
2423
}
25-
if len(cfg.PublicKeys) == 0 {
26-
cfg.PublicKeys = append(cfg.PublicKeys, cfg.PublicKeyPath)
27-
}
2824
return cfg, nil
2925
}

private/constellation/constellation.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package constellation
33
import (
44
"fmt"
55
"github.com/patrickmn/go-cache"
6+
"os"
7+
"path/filepath"
68
"time"
79
)
810

@@ -38,16 +40,26 @@ func (g *Constellation) Receive(data []byte) ([]byte, error) {
3840
return pl, nil
3941
}
4042

41-
func New(configPath string) (*Constellation, error) {
42-
cfg, err := LoadConfig(configPath)
43+
func New(path string) (*Constellation, error) {
44+
info, err := os.Lstat(path)
4345
if err != nil {
4446
return nil, err
4547
}
46-
err = RunNode(configPath, cfg.Socket)
48+
// We accept either the socket or a configuration file that points to
49+
// a socket.
50+
isSocket := info.Mode() & os.ModeSocket != 0
51+
if !isSocket {
52+
cfg, err := LoadConfig(path)
53+
if err != nil {
54+
return nil, err
55+
}
56+
path = filepath.Join(cfg.WorkDir, cfg.Socket)
57+
}
58+
err = RunNode(path)
4759
if err != nil {
4860
return nil, err
4961
}
50-
n, err := NewClient(cfg.PublicKeys[0], cfg.Socket)
62+
n, err := NewClient(path)
5163
if err != nil {
5264
return nil, err
5365
}
@@ -57,17 +69,17 @@ func New(configPath string) (*Constellation, error) {
5769
}, nil
5870
}
5971

60-
func MustNew(configPath string) *Constellation {
61-
g, err := New(configPath)
72+
func MustNew(path string) *Constellation {
73+
g, err := New(path)
6274
if err != nil {
63-
panic(fmt.Sprintf("MustNew error: %v", err))
75+
panic(fmt.Sprintf("MustNew: Failed to connect to Constellation (%s): %v", path, err))
6476
}
6577
return g
6678
}
6779

68-
func MaybeNew(configPath string) *Constellation {
69-
if configPath == "" {
80+
func MaybeNew(path string) *Constellation {
81+
if path == "" {
7082
return nil
7183
}
72-
return MustNew(configPath)
84+
return MustNew(path)
7385
}

private/constellation/node.go

Lines changed: 25 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
8061
type 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

10483
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)
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

133102
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)
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

Comments
 (0)