core: Load identity from config
Also, slightly refactor Ricochet to be an interface to get the various top-level components, and let the implementation define it (such as backend's RicochetCore RPC type)
This commit is contained in:
parent
ca3c672975
commit
b74073fd09
|
@ -23,12 +23,16 @@ func main() {
|
|||
log.Fatalf("config error: %v", err)
|
||||
}
|
||||
|
||||
ricochet := &RicochetCore{
|
||||
Network: ricochet.CreateNetwork(),
|
||||
Config: config,
|
||||
core := &RicochetCore{
|
||||
config: config,
|
||||
}
|
||||
core.network = ricochet.CreateNetwork()
|
||||
core.identity, err = ricochet.CreateIdentity(core)
|
||||
if err != nil {
|
||||
log.Fatalf("identity error: %v", err)
|
||||
}
|
||||
|
||||
rpcServer := grpc.NewServer()
|
||||
rpc.RegisterRicochetCoreServer(rpcServer, ricochet)
|
||||
rpc.RegisterRicochetCoreServer(rpcServer, core)
|
||||
rpcServer.Serve(listener)
|
||||
}
|
||||
|
|
|
@ -8,12 +8,25 @@ import (
|
|||
"log"
|
||||
)
|
||||
|
||||
var NotImplementedError error = errors.New("Not implemented")
|
||||
|
||||
type RicochetCore struct {
|
||||
Network *ricochet.Network
|
||||
Config *ricochet.Config
|
||||
config *ricochet.Config
|
||||
network *ricochet.Network
|
||||
identity *ricochet.Identity
|
||||
}
|
||||
|
||||
var NotImplementedError error = errors.New("Not implemented")
|
||||
func (core *RicochetCore) Config() *ricochet.Config {
|
||||
return core.config
|
||||
}
|
||||
|
||||
func (core *RicochetCore) Network() *ricochet.Network {
|
||||
return core.network
|
||||
}
|
||||
|
||||
func (core *RicochetCore) Identity() *ricochet.Identity {
|
||||
return core.identity
|
||||
}
|
||||
|
||||
func (core *RicochetCore) GetServerStatus(ctx context.Context, req *rpc.ServerStatusRequest) (*rpc.ServerStatusReply, error) {
|
||||
if req.RpcVersion != 1 {
|
||||
|
@ -27,12 +40,12 @@ func (core *RicochetCore) GetServerStatus(ctx context.Context, req *rpc.ServerSt
|
|||
}
|
||||
|
||||
func (core *RicochetCore) MonitorNetwork(req *rpc.MonitorNetworkRequest, stream rpc.RicochetCore_MonitorNetworkServer) error {
|
||||
events := core.Network.EventMonitor().Subscribe(20)
|
||||
defer core.Network.EventMonitor().Unsubscribe(events)
|
||||
events := core.Network().EventMonitor().Subscribe(20)
|
||||
defer core.Network().EventMonitor().Unsubscribe(events)
|
||||
|
||||
// Send initial status event
|
||||
{
|
||||
event := core.Network.GetStatus()
|
||||
event := core.Network().GetStatus()
|
||||
if err := stream.Send(&event); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -56,23 +69,26 @@ func (core *RicochetCore) MonitorNetwork(req *rpc.MonitorNetworkRequest, stream
|
|||
func (core *RicochetCore) StartNetwork(ctx context.Context, req *rpc.StartNetworkRequest) (*rpc.NetworkStatus, error) {
|
||||
// err represents the result of the first connection attempt, but as long
|
||||
// as 'ok' is true, the network has started and this call was successful.
|
||||
ok, err := core.Network.Start("tcp://127.0.0.1:9051", "")
|
||||
ok, err := core.Network().Start("tcp://127.0.0.1:9051", "")
|
||||
if !ok {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
status := core.Network.GetStatus()
|
||||
status := core.Network().GetStatus()
|
||||
return &status, nil
|
||||
}
|
||||
|
||||
func (core *RicochetCore) StopNetwork(ctx context.Context, req *rpc.StopNetworkRequest) (*rpc.NetworkStatus, error) {
|
||||
core.Network.Stop()
|
||||
status := core.Network.GetStatus()
|
||||
core.Network().Stop()
|
||||
status := core.Network().GetStatus()
|
||||
return &status, nil
|
||||
}
|
||||
|
||||
func (core *RicochetCore) GetIdentity(ctx context.Context, req *rpc.IdentityRequest) (*rpc.Identity, error) {
|
||||
return nil, NotImplementedError
|
||||
reply := rpc.Identity{
|
||||
Address: core.Identity().Address(),
|
||||
}
|
||||
return &reply, nil
|
||||
}
|
||||
|
||||
func (core *RicochetCore) MonitorContacts(req *rpc.MonitorContactsRequest, stream rpc.RicochetCore_MonitorContactsServer) error {
|
||||
|
|
|
@ -88,6 +88,13 @@ func main() {
|
|||
log.Fatalf("could not get status: %v", err)
|
||||
}
|
||||
log.Printf("get status result: %v", r)
|
||||
|
||||
identity, err := c.Backend.GetIdentity(context.Background(), &rpc.IdentityRequest{})
|
||||
if err != nil {
|
||||
log.Fatalf("could not get identity: %v", err)
|
||||
}
|
||||
log.Printf("identity: %v", identity)
|
||||
|
||||
case "connect":
|
||||
status, err := c.Backend.StartNetwork(context.Background(), &rpc.StartNetworkRequest{})
|
||||
if err != nil {
|
||||
|
|
|
@ -1,29 +1,50 @@
|
|||
package core
|
||||
|
||||
type Identity struct {
|
||||
internalId int
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"github.com/yawning/bulb/utils/pkcs1"
|
||||
"log"
|
||||
)
|
||||
|
||||
name string
|
||||
type Identity struct {
|
||||
address string
|
||||
privateKey *rsa.PrivateKey
|
||||
|
||||
contactList *ContactList
|
||||
}
|
||||
|
||||
func CreateIdentity(id int, name string) *Identity {
|
||||
me := &Identity{
|
||||
internalId: id,
|
||||
name: name,
|
||||
contactList: &ContactList{},
|
||||
}
|
||||
return me
|
||||
func CreateIdentity(core Ricochet) (*Identity, error) {
|
||||
me := &Identity{}
|
||||
|
||||
config := core.Config().OpenRead()
|
||||
defer config.Close()
|
||||
if config.Identity.ServiceKey != "" {
|
||||
keyData, err := base64.StdEncoding.DecodeString(config.Identity.ServiceKey)
|
||||
if err != nil {
|
||||
log.Printf("Decoding identity key failed: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (me *Identity) InternalId() int {
|
||||
return me.internalId
|
||||
me.privateKey, _, err = pkcs1.DecodePrivateKeyDER(keyData)
|
||||
if err != nil {
|
||||
log.Printf("Decoding identity key failed: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (me *Identity) Name() string {
|
||||
return me.name
|
||||
me.address, err = pkcs1.OnionAddr(&me.privateKey.PublicKey)
|
||||
if err != nil && me.address == "" {
|
||||
err = errors.New("Cannot calculate onion address")
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("Decoding identify key failed: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
me.address = "ricochet:" + me.address
|
||||
}
|
||||
|
||||
return me, nil
|
||||
}
|
||||
|
||||
func (me *Identity) Address() string {
|
||||
|
|
|
@ -1,20 +1,7 @@
|
|||
package core
|
||||
|
||||
type Ricochet struct {
|
||||
Network *Network
|
||||
Identity *Identity
|
||||
Config *Config
|
||||
}
|
||||
|
||||
func Initialize(configPath string) (*Ricochet, error) {
|
||||
cfg, err := LoadConfig(configPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ricochet := &Ricochet{
|
||||
Config: cfg,
|
||||
}
|
||||
|
||||
return ricochet, nil
|
||||
type Ricochet interface {
|
||||
Config() *Config
|
||||
Network() *Network
|
||||
Identity() *Identity
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue