|
|
|
@ -25,19 +25,27 @@ import ( |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
|
// DefaultEnvPath is the default PATH for pre and post hooks that are shelled out to
|
|
|
|
|
DefaultEnvPath = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" |
|
|
|
|
ServiceUser = "salty" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
ErrNoMessages = errors.New("error: no messages found") |
|
|
|
|
ErrNoSender = errors.New("error: no sender configured") |
|
|
|
|
ErrNotConnected = errors.New("error: client not connected") |
|
|
|
|
// ErrNoMessages is an error returned when there are no further messages found for an inbox from the broker
|
|
|
|
|
ErrNoMessages = errors.New("error: no messages found") |
|
|
|
|
|
|
|
|
|
// ErrNoSender is an error returned when the client is not properly configured with a valid sender
|
|
|
|
|
ErrNoSender = errors.New("error: no sender configured") |
|
|
|
|
|
|
|
|
|
// ErrNotConnected is an error returned when the client is not properly configured or connected to a broker
|
|
|
|
|
ErrNotConnected = errors.New("error: client not connected") |
|
|
|
|
|
|
|
|
|
// ErrMissingIdentity is an error returned when the client is not properly configured with a valid identity
|
|
|
|
|
ErrMissingIdentity = errors.New("error: missing identity") |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type addrCache map[string]*Addr |
|
|
|
|
|
|
|
|
|
// Message contains the plaintext (decrypted) message and the sender's public key
|
|
|
|
|
type Message struct { |
|
|
|
|
Text string |
|
|
|
|
Key *keys.EdX25519PublicKey |
|
|
|
@ -205,10 +213,19 @@ func (cli *Client) messageHandler(extraenvs, prehook, posthook string, msgs chan |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (cli *Client) Me() *Addr { return cli.me } |
|
|
|
|
// Me returns our (self) address
|
|
|
|
|
func (cli *Client) Me() *Addr { return cli.me } |
|
|
|
|
|
|
|
|
|
// Key returns out (self) public key
|
|
|
|
|
func (cli *Client) Key() *keys.EdX25519PublicKey { return cli.key.PublicKey() } |
|
|
|
|
func (cli *Client) State() *State { return cli.state } |
|
|
|
|
|
|
|
|
|
// State returns the current state of the client
|
|
|
|
|
func (cli *Client) State() *State { return cli.state } |
|
|
|
|
|
|
|
|
|
// Env sets up a sensible (and hopefully secure) environment for running pre and post hooks
|
|
|
|
|
// Extra environment variables are parsed from extraenvs and some default variables injected
|
|
|
|
|
// into the new environment such as PATH, PWD and HOME as well as the current user's Salty address
|
|
|
|
|
// (SALTY_USER) and their public key (SALTY_IDENTITY).
|
|
|
|
|
func (cli *Client) Env(extraenvs string) []string { |
|
|
|
|
Path := DefaultEnvPath |
|
|
|
|
GoPath := os.Getenv("GOPATH") |
|
|
|
@ -242,6 +259,8 @@ func (cli *Client) Env(extraenvs string) []string { |
|
|
|
|
return env |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Outbox returns the URL of our (self) outbox for sending copies of our outgoing messages to
|
|
|
|
|
// which is later used by the client as a way to track messages sent.
|
|
|
|
|
func (cli *Client) Outbox() *url.URL { |
|
|
|
|
// use url struct copy to avoid modifying cli.me.Endpoint().Path
|
|
|
|
|
// https://github.com/golang/go/issues/38351
|
|
|
|
@ -253,6 +272,7 @@ func (cli *Client) Outbox() *url.URL { |
|
|
|
|
return &ep |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// OutboxAddr returns the address of our (self) outbox
|
|
|
|
|
func (cli *Client) OutboxAddr(to *Addr) *Addr { |
|
|
|
|
return &Addr{ |
|
|
|
|
User: to.User, |
|
|
|
@ -264,6 +284,7 @@ func (cli *Client) OutboxAddr(to *Addr) *Addr { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// OutboxClient returns a modified client for our (self) outbox
|
|
|
|
|
func (cli *Client) OutboxClient(to *Addr) *Client { |
|
|
|
|
if to == nil { |
|
|
|
|
to = cli.me |
|
|
|
@ -301,6 +322,8 @@ func (cli *Client) OutboxClient(to *Addr) *Client { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// String implements the fmt.Stringer interface and outputs who we (self) are,
|
|
|
|
|
// what our endpoint is we're connected to (broker), our outbox and our public key.
|
|
|
|
|
func (cli *Client) String() string { |
|
|
|
|
b := &bytes.Buffer{} |
|
|
|
|
fmt.Fprintln(b, "Me: ", cli.me) |
|
|
|
@ -393,6 +416,7 @@ func (cli *Client) Send(user, msg string) error { |
|
|
|
|
return cli.OutboxClient(addr).SendToAddr(cli.OutboxAddr(addr), msg) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// SendToAddr encrypts and sends the message to a specified address
|
|
|
|
|
func (cli *Client) SendToAddr(addr *Addr, msg string) error { |
|
|
|
|
if cli.me == nil || cli.me.IsZero() { |
|
|
|
|
return ErrNoSender |
|
|
|
|