core: Adapt to protocol API changes

This commit is contained in:
John Brooks 2016-10-09 17:31:26 -07:00
parent e960c296fe
commit f9bc09c520
4 changed files with 144 additions and 129 deletions

View File

@ -221,7 +221,7 @@ func (c *Contact) connectOutbound(ctx context.Context, connChannel chan *protoco
} }
log.Printf("Successful outbound connection to contact %s", hostname) log.Printf("Successful outbound connection to contact %s", hostname)
oc, err := c.core.Protocol.ConnectOpen(conn, hostname[0:16]) oc, err := protocol.Open(conn, hostname[0:16])
if err != nil { if err != nil {
log.Printf("Contact connection protocol failure: %s", err) log.Printf("Contact connection protocol failure: %s", err)
oc.Close() oc.Close()
@ -242,6 +242,13 @@ func (c *Contact) connectOutbound(ctx context.Context, connChannel chan *protoco
// OnConnectionClosed. Alternatively, it will break because this // OnConnectionClosed. Alternatively, it will break because this
// is fragile and dumb. // is fragile and dumb.
// XXX BUG: This means no backoff for authentication failure // XXX BUG: This means no backoff for authentication failure
handler := &ProtocolConnection{
Conn: oc,
Contact: c,
MyHostname: c.core.Identity.Address()[9:],
PrivateKey: c.core.Identity.PrivateKey(),
}
go oc.Process(handler)
return return
} }
} }

View File

@ -4,6 +4,7 @@ import (
"crypto/rsa" "crypto/rsa"
"encoding/base64" "encoding/base64"
"errors" "errors"
protocol "github.com/s-rah/go-ricochet"
"github.com/special/notricochet/core/utils" "github.com/special/notricochet/core/utils"
"github.com/yawning/bulb/utils/pkcs1" "github.com/yawning/bulb/utils/pkcs1"
"log" "log"
@ -98,6 +99,29 @@ func (me *Identity) setPrivateKey(key *rsa.PrivateKey) error {
return nil return nil
} }
type identityService struct {
Identity *Identity
MyHostname string
}
func (is *identityService) OnNewConnection(oc *protocol.OpenConnection) {
log.Printf("Inbound connection accepted")
oc.MyHostname = is.MyHostname
// XXX Should have pre-auth handling, timeouts
identity := is.Identity
handler := &ProtocolConnection{
Conn: oc,
GetContactByHostname: func(hostname string) *Contact {
return identity.ContactList().ContactByAddress("ricochet:" + hostname)
},
}
go oc.Process(handler)
}
func (is *identityService) OnFailedConnection(err error) {
log.Printf("Inbound connection failed: %v", err)
}
// BUG(special): No error handling for failures under publishService // BUG(special): No error handling for failures under publishService
func (me *Identity) publishService(key *rsa.PrivateKey) { func (me *Identity) publishService(key *rsa.PrivateKey) {
// This call will block until a control connection is available and the // This call will block until a control connection is available and the
@ -127,7 +151,17 @@ func (me *Identity) publishService(key *rsa.PrivateKey) {
} }
log.Printf("Identity service published, accepting connections") log.Printf("Identity service published, accepting connections")
go me.core.Protocol.ServeListener(listener) is := &identityService{
Identity: me,
MyHostname: me.Address()[9:],
}
err = protocol.Serve(listener, is)
if err != nil {
log.Printf("Identity listener failed: %v", err)
// XXX handle
return
}
} }
func (me *Identity) Address() string { func (me *Identity) Address() string {

View File

@ -1,212 +1,188 @@
package core package core
import ( import (
"crypto/rsa"
"encoding/asn1" "encoding/asn1"
protocol "github.com/s-rah/go-ricochet" protocol "github.com/s-rah/go-ricochet"
"log" "log"
"net"
"time" "time"
) )
type Protocol struct { type ProtocolConnection struct {
core *Ricochet Conn *protocol.OpenConnection
Contact *Contact
service *protocol.Ricochet // Client-side authentication
handler *protocolHandler MyHostname string
PrivateKey rsa.PrivateKey
// Service-side authentication
GetContactByHostname func(hostname string) *Contact
} }
// Implements protocol.RicochetService func (pc *ProtocolConnection) OnReady(oc *protocol.OpenConnection) {
type protocolHandler struct { if pc.Conn != nil && pc.Conn != oc {
p *Protocol log.Panicf("ProtocolConnection is already assigned connection %v, but OnReady called for connection %v", pc.Conn, oc)
}
func CreateProtocol(core *Ricochet) *Protocol {
p := &Protocol{
core: core,
service: new(protocol.Ricochet),
} }
p.handler = &protocolHandler{p: p}
p.service.Init()
return p
}
func (p *Protocol) ServeListener(listener net.Listener) { pc.Conn = oc
p.service.ServeListener(p.handler, listener)
}
// Strangely, ServeListener starts a background routine that watches a channel if pc.Conn.Client {
// on p.service for new connections and dispatches their events to the handler log.Printf("Connected to %s", pc.Conn.OtherHostname)
// for the listener. API needs a little work here. pc.Conn.MyHostname = pc.MyHostname
func (p *Protocol) ConnectOpen(conn net.Conn, host string) (*protocol.OpenConnection, error) { pc.Conn.IsAuthed = true // Outbound connections are authenticated
oc, err := p.service.ConnectOpen(conn, host) pc.Conn.Authenticate(1)
if err != nil {
return nil, err
}
oc.MyHostname = p.core.Identity.Address()[9:]
return oc, nil
}
func (handler *protocolHandler) OnReady() {
log.Printf("protocol: OnReady")
}
func (handler *protocolHandler) OnConnect(oc *protocol.OpenConnection) {
log.Printf("protocol: OnConnect: %v", oc)
if oc.Client {
log.Printf("Connected to %s", oc.OtherHostname)
oc.IsAuthed = true // Outbound connections are authenticated
oc.Authenticate(1)
} else {
// Strip ricochet:
oc.MyHostname = handler.p.core.Identity.Address()[9:]
} }
} }
func (handler *protocolHandler) OnDisconnect(oc *protocol.OpenConnection) { func (pc *ProtocolConnection) OnDisconnect() {
log.Printf("protocol: OnDisconnect: %v", oc) log.Printf("protocol: OnDisconnect: %v", pc)
if oc.OtherHostname != "" { if pc.Contact != nil {
contact := handler.p.core.Identity.ContactList().ContactByAddress("ricochet:" + oc.OtherHostname) pc.Contact.OnConnectionClosed(pc.Conn)
if contact != nil {
contact.OnConnectionClosed(oc)
}
} }
} }
// Authentication Management // Authentication Management
func (handler *protocolHandler) OnAuthenticationRequest(oc *protocol.OpenConnection, channelID int32, clientCookie [16]byte) { func (pc *ProtocolConnection) OnAuthenticationRequest(channelID int32, clientCookie [16]byte) {
log.Printf("protocol: OnAuthenticationRequest") log.Printf("protocol: OnAuthenticationRequest")
oc.ConfirmAuthChannel(channelID, clientCookie) pc.Conn.ConfirmAuthChannel(channelID, clientCookie)
} }
func (handler *protocolHandler) OnAuthenticationChallenge(oc *protocol.OpenConnection, channelID int32, serverCookie [16]byte) { func (pc *ProtocolConnection) OnAuthenticationChallenge(channelID int32, serverCookie [16]byte) {
log.Printf("protocol: OnAuthenticationChallenge") log.Printf("protocol: OnAuthenticationChallenge")
privateKey := handler.p.core.Identity.PrivateKey() publicKeyBytes, _ := asn1.Marshal(pc.PrivateKey.PublicKey)
publicKeyBytes, _ := asn1.Marshal(privateKey.PublicKey) pc.Conn.SendProof(1, serverCookie, publicKeyBytes, &pc.PrivateKey)
oc.SendProof(1, serverCookie, publicKeyBytes, &privateKey)
} }
func (handler *protocolHandler) OnAuthenticationProof(oc *protocol.OpenConnection, channelID int32, publicKey []byte, signature []byte, isKnownContact bool) { func (pc *ProtocolConnection) OnAuthenticationProof(channelID int32, publicKey []byte, signature []byte) {
result := oc.ValidateProof(channelID, publicKey, signature) result := pc.Conn.ValidateProof(channelID, publicKey, signature)
var contact *Contact
if result { if result {
if len(oc.OtherHostname) != 16 { if len(pc.Conn.OtherHostname) != 16 {
log.Printf("protocol: Invalid format for hostname '%s' in authentication proof", oc.OtherHostname) log.Printf("protocol: Invalid format for hostname '%s' in authentication proof", pc.Conn.OtherHostname)
result = false result = false
} else { } else {
contact = handler.p.core.Identity.ContactList().ContactByAddress("ricochet:" + oc.OtherHostname) pc.Contact = pc.GetContactByHostname(pc.Conn.OtherHostname)
} }
} }
isKnownContact = (contact != nil) isKnownContact := (pc.Contact != nil)
oc.SendAuthenticationResult(channelID, result, isKnownContact) pc.Conn.SendAuthenticationResult(channelID, result, isKnownContact)
oc.IsAuthed = result pc.Conn.IsAuthed = result
oc.CloseChannel(channelID) pc.Conn.CloseChannel(channelID)
log.Printf("protocol: OnAuthenticationProof, result: %v, contact: %v", result, contact) log.Printf("protocol: OnAuthenticationProof, result: %v, contact: %v", result, pc.Contact)
if result && contact != nil { if result && pc.Contact != nil {
contact.OnConnectionAuthenticated(oc) pc.Contact.OnConnectionAuthenticated(pc.Conn)
} }
} }
func (handler *protocolHandler) OnAuthenticationResult(oc *protocol.OpenConnection, channelID int32, result bool, isKnownContact bool) { func (pc *ProtocolConnection) OnAuthenticationResult(channelID int32, result bool, isKnownContact bool) {
oc.IsAuthed = result pc.Conn.IsAuthed = result
oc.CloseChannel(channelID) pc.Conn.CloseChannel(channelID)
if !result { if !result {
log.Printf("protocol: Outbound connection authentication to %s failed", oc.OtherHostname) log.Printf("protocol: Outbound connection authentication to %s failed", pc.Conn.OtherHostname)
oc.Close() pc.Conn.Close()
return return
} }
// XXX Contact request, removed cases // XXX Contact request, removed cases
if !isKnownContact { if !isKnownContact {
log.Printf("protocol: Outbound connection authentication to %s succeeded, but we are not a known contact", oc.OtherHostname) log.Printf("protocol: Outbound connection authentication to %s succeeded, but we are not a known contact", pc.Conn.OtherHostname)
oc.Close() pc.Conn.Close()
return return
} }
contact := handler.p.core.Identity.ContactList().ContactByAddress("ricochet:" + oc.OtherHostname) log.Printf("protocol: Outbound connection to %s authenticated", pc.Conn.OtherHostname)
if contact == nil { if pc.Contact != nil {
log.Printf("protocol: Outbound connection authenticated to %s succeeded, but no matching contact found", oc.OtherHostname) pc.Contact.OnConnectionAuthenticated(pc.Conn)
oc.Close()
return
} }
log.Printf("protocol: Outbound connection to %s authenticated", oc.OtherHostname)
contact.OnConnectionAuthenticated(oc)
} }
// Contact Management // Contact Management
func (handler *protocolHandler) IsKnownContact(hostname string) bool { func (pc *ProtocolConnection) OnContactRequest(channelID int32, nick string, message string) {
contact := handler.p.core.Identity.ContactList().ContactByAddress("ricochet:" + hostname)
return contact != nil
} }
func (handler *protocolHandler) OnContactRequest(oc *protocol.OpenConnection, channelID int32, nick string, message string) { func (pc *ProtocolConnection) OnContactRequestAck(channelID int32, status string) {
}
func (handler *protocolHandler) OnContactRequestAck(oc *protocol.OpenConnection, channelID int32, status string) {
} }
// Managing Channels // Managing Channels
func (handler *protocolHandler) OnOpenChannelRequest(oc *protocol.OpenConnection, channelID int32, channelType string) { func (pc *ProtocolConnection) IsChannelAllowed(channelType string) bool {
log.Printf("open channel request: %v %v", channelID, channelType) switch channelType {
oc.AckOpenChannel(channelID, channelType) case "im.ricochet.auth.hidden-service":
return !pc.Conn.IsAuthed && pc.Contact == nil
case "im.ricochet.chat":
return pc.Conn.IsAuthed && pc.Contact != nil
case "im.ricochet.contact.request":
return pc.Conn.IsAuthed && pc.Contact == nil
}
return false
} }
func (handler *protocolHandler) OnOpenChannelRequestSuccess(oc *protocol.OpenConnection, channelID int32) { func (pc *ProtocolConnection) OnOpenChannelRequest(channelID int32, channelType string) {
log.Printf("open channel request success: %v %v", channelID) log.Printf("open channel request: %v %v", channelID, channelType)
pc.Conn.AckOpenChannel(channelID, channelType)
} }
func (handler *protocolHandler) OnChannelClosed(oc *protocol.OpenConnection, channelID int32) {
func (pc *ProtocolConnection) OnOpenChannelRequestSuccess(channelID int32) {
log.Printf("open channel request success: %v", channelID)
}
func (pc *ProtocolConnection) OnChannelClosed(channelID int32) {
log.Printf("channel closed: %v", channelID) log.Printf("channel closed: %v", channelID)
} }
// Chat Messages // Chat Messages
// XXX messageID should be (at least) uint32 // XXX messageID should be (at least) uint32
func (handler *protocolHandler) OnChatMessage(oc *protocol.OpenConnection, channelID int32, messageID int32, message string) { func (pc *ProtocolConnection) OnChatMessage(channelID int32, messageID int32, message string) {
// XXX no time delta? // XXX no time delta?
// XXX sanity checks, message contents, etc // XXX sanity checks, message contents, etc
log.Printf("chat message: %d %d %s", channelID, messageID, message) log.Printf("chat message: %d %d %s", channelID, messageID, message)
// XXX ugllly // XXX error case
contact := handler.p.core.Identity.ContactList().ContactByAddress("ricochet:" + oc.OtherHostname) if pc.Contact == nil {
if contact != nil { pc.Conn.Close()
conversation := contact.Conversation()
conversation.Receive(uint64(messageID), time.Now().Unix(), message)
} }
oc.AckChatMessage(channelID, messageID) // XXX cache?
conversation := pc.Contact.Conversation()
conversation.Receive(uint64(messageID), time.Now().Unix(), message)
pc.Conn.AckChatMessage(channelID, messageID)
} }
func (handler *protocolHandler) OnChatMessageAck(oc *protocol.OpenConnection, channelID int32, messageID int32) {
func (pc *ProtocolConnection) OnChatMessageAck(channelID int32, messageID int32) {
// XXX no success // XXX no success
log.Printf("chat ack: %d %d", channelID, messageID) log.Printf("chat ack: %d %d", channelID, messageID)
// XXX Also ugly // XXX error case
contact := handler.p.core.Identity.ContactList().ContactByAddress("ricochet:" + oc.OtherHostname) if pc.Contact == nil {
if contact != nil { pc.Conn.Close()
conversation := contact.Conversation()
conversation.UpdateSentStatus(uint64(messageID), true)
} }
conversation := pc.Contact.Conversation()
conversation.UpdateSentStatus(uint64(messageID), true)
} }
// Handle Errors // Handle Errors
func (handler *protocolHandler) OnFailedChannelOpen(oc *protocol.OpenConnection, channelID int32, errorType string) { func (pc *ProtocolConnection) OnFailedChannelOpen(channelID int32, errorType string) {
log.Printf("failed channel open: %d %s", channelID, errorType) log.Printf("failed channel open: %d %s", channelID, errorType)
oc.UnsetChannel(channelID) pc.Conn.UnsetChannel(channelID)
} }
func (handler *protocolHandler) OnGenericError(oc *protocol.OpenConnection, channelID int32) { func (pc *ProtocolConnection) OnGenericError(channelID int32) {
oc.RejectOpenChannel(channelID, "GenericError") pc.Conn.RejectOpenChannel(channelID, "GenericError")
} }
func (handler *protocolHandler) OnUnknownTypeError(oc *protocol.OpenConnection, channelID int32) { func (pc *ProtocolConnection) OnUnknownTypeError(channelID int32) {
oc.RejectOpenChannel(channelID, "UnknownTypeError") pc.Conn.RejectOpenChannel(channelID, "UnknownTypeError")
} }
func (handler *protocolHandler) OnUnauthorizedError(oc *protocol.OpenConnection, channelID int32) { func (pc *ProtocolConnection) OnUnauthorizedError(channelID int32) {
oc.RejectOpenChannel(channelID, "UnauthorizedError") pc.Conn.RejectOpenChannel(channelID, "UnauthorizedError")
} }
func (handler *protocolHandler) OnBadUsageError(oc *protocol.OpenConnection, channelID int32) { func (pc *ProtocolConnection) OnBadUsageError(channelID int32) {
oc.RejectOpenChannel(channelID, "BadUsageError") pc.Conn.RejectOpenChannel(channelID, "BadUsageError")
} }
func (handler *protocolHandler) OnFailedError(oc *protocol.OpenConnection, channelID int32) { func (pc *ProtocolConnection) OnFailedError(channelID int32) {
oc.RejectOpenChannel(channelID, "FailedError") pc.Conn.RejectOpenChannel(channelID, "FailedError")
} }

View File

@ -3,7 +3,6 @@ package core
type Ricochet struct { type Ricochet struct {
Config *Config Config *Config
Network *Network Network *Network
Protocol *Protocol
Identity *Identity Identity *Identity
} }
@ -11,7 +10,6 @@ func (core *Ricochet) Init(conf *Config) error {
var err error var err error
core.Config = conf core.Config = conf
core.Network = CreateNetwork() core.Network = CreateNetwork()
core.Protocol = CreateProtocol(core)
core.Identity, err = CreateIdentity(core) core.Identity, err = CreateIdentity(core)
if err != nil { if err != nil {
return err return err