Skip to content

Commit

Permalink
Use sendraw/receiveraw and allow specifying Constellation IPC socket …
Browse files Browse the repository at this point in the history
…in PRIVATE_CONFIG (#278)
  • Loading branch information
patrickmn committed Feb 8, 2018
1 parent f651c1b commit 2a5a1d6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 87 deletions.
10 changes: 3 additions & 7 deletions private/constellation/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
)

type Config struct {
Socket string `toml:"socket"`
PublicKeys []string `toml:"publickeys"`
Socket string `toml:"socket"`
WorkDir string `toml:"workdir"`

// Deprecated
SocketPath string `toml:"socketPath"`
PublicKeyPath string `toml:"publicKeyPath"`
SocketPath string `toml:"socketPath"`
}

func LoadConfig(configPath string) (*Config, error) {
Expand All @@ -22,8 +21,5 @@ func LoadConfig(configPath string) (*Config, error) {
if cfg.Socket == "" {
cfg.Socket = cfg.SocketPath
}
if len(cfg.PublicKeys) == 0 {
cfg.PublicKeys = append(cfg.PublicKeys, cfg.PublicKeyPath)
}
return cfg, nil
}
32 changes: 22 additions & 10 deletions private/constellation/constellation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package constellation
import (
"fmt"
"github.com/patrickmn/go-cache"
"os"
"path/filepath"
"time"
)

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

func New(configPath string) (*Constellation, error) {
cfg, err := LoadConfig(configPath)
func New(path string) (*Constellation, error) {
info, err := os.Lstat(path)
if err != nil {
return nil, err
}
err = RunNode(configPath, cfg.Socket)
// We accept either the socket or a configuration file that points to
// a socket.
isSocket := info.Mode() & os.ModeSocket != 0
if !isSocket {
cfg, err := LoadConfig(path)
if err != nil {
return nil, err
}
path = filepath.Join(cfg.WorkDir, cfg.Socket)
}
err = RunNode(path)
if err != nil {
return nil, err
}
n, err := NewClient(cfg.PublicKeys[0], cfg.Socket)
n, err := NewClient(path)
if err != nil {
return nil, err
}
Expand All @@ -57,17 +69,17 @@ func New(configPath string) (*Constellation, error) {
}, nil
}

func MustNew(configPath string) *Constellation {
g, err := New(configPath)
func MustNew(path string) *Constellation {
g, err := New(path)
if err != nil {
panic(fmt.Sprintf("MustNew error: %v", err))
panic(fmt.Sprintf("MustNew: Failed to connect to Constellation (%s): %v", path, err))
}
return g
}

func MaybeNew(configPath string) *Constellation {
if configPath == "" {
func MaybeNew(path string) *Constellation {
if path == "" {
return nil
}
return MustNew(configPath)
return MustNew(path)
}
95 changes: 25 additions & 70 deletions private/constellation/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"os"
"os/exec"
"strings"
"time"
)

Expand Down Expand Up @@ -45,9 +46,8 @@ func unixClient(socketPath string) *http.Client {
}
}

func RunNode(cfgPath, nodeSocketPath string) error {
// launchNode(cfgPath)
c := unixClient(nodeSocketPath)
func RunNode(socketPath string) error {
c := unixClient(socketPath)
res, err := c.Get("http+unix://c/upcheck")
if err != nil {
return err
Expand All @@ -58,32 +58,11 @@ func RunNode(cfgPath, nodeSocketPath string) error {
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
httpClient *http.Client
}

func (c *Client) do(path string, apiReq interface{}) (*http.Response, error) {
func (c *Client) doJson(path string, apiReq interface{}) (*http.Response, error) {
buf := new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(apiReq)
if err != nil {
Expand All @@ -102,64 +81,40 @@ func (c *Client) do(path string, apiReq interface{}) (*http.Response, error) {
}

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)
buf := bytes.NewBuffer(pl)
req, err := http.NewRequest("POST", "http+unix://c/sendraw", buf)
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
if b64From != "" {
req.Header.Set("c11n-from", b64From)
}
key, err := base64.StdEncoding.DecodeString(sres.Key)
if err != nil {
return nil, err
req.Header.Set("c11n-to", strings.Join(b64To, ","))
req.Header.Set("Content-Type", "application/octet-stream")
res, err := c.httpClient.Do(req)
if err == nil && res.StatusCode != 200 {
return nil, fmt.Errorf("Non-200 status code: %+v", res)
}
return key, nil
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}

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)
req, err := http.NewRequest("GET", "http+unix://c/receiveraw", nil)
if err != nil {
return nil, err
}
pl, err := base64.StdEncoding.DecodeString(rres.Payload)
if err != nil {
return nil, err
req.Header.Set("c11n-key", base64.StdEncoding.EncodeToString(key))
res, err := c.httpClient.Do(req)
if err == nil && res.StatusCode != 200 {
return nil, fmt.Errorf("Non-200 status code: %+v", res)
}
return pl, nil
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}

func NewClient(publicKeyPath string, nodeSocketPath string) (*Client, error) {
b64PublicKey, err := ioutil.ReadFile(publicKeyPath)
if err != nil {
return nil, err
}
func NewClient(socketPath string) (*Client, error) {
return &Client{
httpClient: unixClient(nodeSocketPath),
b64PublicKey: string(b64PublicKey),
httpClient: unixClient(socketPath),
}, nil
}

0 comments on commit 2a5a1d6

Please sign in to comment.