2016-07-01 03:18:55 +00:00
|
|
|
package core
|
|
|
|
|
2016-08-30 02:46:41 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2016-09-16 00:32:58 +00:00
|
|
|
protocol "github.com/s-rah/go-ricochet"
|
2016-09-20 01:53:21 +00:00
|
|
|
"github.com/special/notricochet/core/utils"
|
2016-09-16 00:32:58 +00:00
|
|
|
"github.com/special/notricochet/rpc"
|
|
|
|
"log"
|
2016-09-20 01:53:21 +00:00
|
|
|
"strconv"
|
2016-08-30 02:46:41 +00:00
|
|
|
"strings"
|
2016-09-16 00:32:58 +00:00
|
|
|
"sync"
|
2016-08-30 02:46:41 +00:00
|
|
|
"time"
|
2016-07-01 03:18:55 +00:00
|
|
|
)
|
|
|
|
|
2016-08-30 02:46:41 +00:00
|
|
|
// XXX There is generally a lot of duplication and boilerplate between
|
|
|
|
// Contact, ConfigContact, and rpc.Contact. This should be reduced somehow.
|
|
|
|
|
2016-07-01 03:18:55 +00:00
|
|
|
type Contact struct {
|
2016-09-20 01:53:21 +00:00
|
|
|
core *Ricochet
|
2016-09-16 00:32:58 +00:00
|
|
|
|
2016-09-20 01:53:21 +00:00
|
|
|
id int
|
|
|
|
data ConfigContact
|
|
|
|
status ricochet.Contact_Status
|
|
|
|
|
|
|
|
mutex sync.Mutex
|
|
|
|
events *utils.Publisher
|
2016-09-16 00:32:58 +00:00
|
|
|
|
|
|
|
connection *protocol.OpenConnection
|
2016-08-30 02:46:41 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 01:53:21 +00:00
|
|
|
func ContactFromConfig(core *Ricochet, id int, data ConfigContact, events *utils.Publisher) (*Contact, error) {
|
2016-08-30 02:46:41 +00:00
|
|
|
contact := &Contact{
|
2016-09-20 01:53:21 +00:00
|
|
|
core: core,
|
|
|
|
id: id,
|
|
|
|
data: data,
|
|
|
|
events: events,
|
2016-08-30 02:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if id < 0 {
|
|
|
|
return nil, fmt.Errorf("Invalid contact ID '%d'", id)
|
|
|
|
} else if len(data.Hostname) != 22 || !strings.HasSuffix(data.Hostname, ".onion") {
|
|
|
|
return nil, fmt.Errorf("Invalid contact hostname '%s", data.Hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
return contact, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) Id() int {
|
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) Nickname() string {
|
2016-09-16 00:32:58 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2016-08-30 02:46:41 +00:00
|
|
|
return c.data.Nickname
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) Address() string {
|
2016-09-16 00:32:58 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2016-08-30 02:46:41 +00:00
|
|
|
return "ricochet:" + c.data.Hostname[0:16]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) Hostname() string {
|
2016-09-16 00:32:58 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2016-08-30 02:46:41 +00:00
|
|
|
return c.data.Hostname
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) LastConnected() time.Time {
|
2016-09-16 00:32:58 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2016-08-30 02:46:41 +00:00
|
|
|
time, _ := time.Parse(time.RFC3339, c.data.LastConnected)
|
|
|
|
return time
|
|
|
|
}
|
2016-07-01 03:18:55 +00:00
|
|
|
|
2016-08-30 02:46:41 +00:00
|
|
|
func (c *Contact) WhenCreated() time.Time {
|
2016-09-16 00:32:58 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2016-08-30 02:46:41 +00:00
|
|
|
time, _ := time.Parse(time.RFC3339, c.data.WhenCreated)
|
|
|
|
return time
|
2016-07-01 03:18:55 +00:00
|
|
|
}
|
2016-09-16 00:32:58 +00:00
|
|
|
|
|
|
|
func (c *Contact) Status() ricochet.Contact_Status {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2016-09-20 01:53:21 +00:00
|
|
|
return c.status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) Data() *ricochet.Contact {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
data := &ricochet.Contact{
|
|
|
|
Id: int32(c.id),
|
|
|
|
Address: "ricochet:" + c.data.Hostname[0:16],
|
|
|
|
Nickname: c.data.Nickname,
|
|
|
|
WhenCreated: c.data.WhenCreated,
|
|
|
|
LastConnected: c.data.LastConnected,
|
|
|
|
Status: c.status,
|
2016-09-16 00:32:58 +00:00
|
|
|
}
|
2016-09-20 01:53:21 +00:00
|
|
|
return data
|
2016-09-16 00:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) SetConnection(conn *protocol.OpenConnection) error {
|
|
|
|
c.mutex.Lock()
|
|
|
|
|
|
|
|
if conn == c.connection {
|
2016-09-20 01:53:21 +00:00
|
|
|
c.mutex.Unlock()
|
2016-09-16 00:32:58 +00:00
|
|
|
return fmt.Errorf("Duplicate assignment of connection %v to contact %v", conn, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !conn.IsAuthed || conn.Closed {
|
2016-09-20 01:53:21 +00:00
|
|
|
c.mutex.Unlock()
|
|
|
|
conn.Close()
|
2016-09-16 00:32:58 +00:00
|
|
|
return fmt.Errorf("Connection %v is not in a valid state to assign to contact %v", conn, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.data.Hostname[0:16] != conn.OtherHostname {
|
2016-09-20 01:53:21 +00:00
|
|
|
c.mutex.Unlock()
|
|
|
|
conn.Close()
|
2016-09-16 00:32:58 +00:00
|
|
|
return fmt.Errorf("Connection hostname %s doesn't match contact hostname %s when assigning connection", conn.OtherHostname, c.data.Hostname[0:16])
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.connection != nil {
|
2016-09-20 01:53:21 +00:00
|
|
|
if c.shouldReplaceConnection(conn) {
|
|
|
|
// XXX Signal state change for connection loss?
|
2016-09-16 00:32:58 +00:00
|
|
|
c.connection.Close()
|
|
|
|
c.connection = nil
|
|
|
|
} else {
|
2016-09-20 01:53:21 +00:00
|
|
|
c.mutex.Unlock()
|
2016-09-16 00:32:58 +00:00
|
|
|
conn.Close()
|
|
|
|
return fmt.Errorf("Using existing connection")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this connection is inbound and there's an outbound attempt, keep this
|
|
|
|
// connection and cancel outbound if we haven't sent authentication yet, or
|
|
|
|
// if the outbound connection will lose the fallback comparison above.
|
|
|
|
// XXX implement this
|
|
|
|
|
2016-09-20 01:53:21 +00:00
|
|
|
// XXX react to connection state changes
|
2016-09-16 00:32:58 +00:00
|
|
|
c.connection = conn
|
2016-09-20 01:53:21 +00:00
|
|
|
c.status = ricochet.Contact_ONLINE
|
2016-09-16 00:32:58 +00:00
|
|
|
log.Printf("Assigned connection %v to contact %v", c.connection, c)
|
|
|
|
|
|
|
|
// XXX implicit accept contact requests
|
2016-09-20 01:53:21 +00:00
|
|
|
|
|
|
|
// Update LastConnected time
|
|
|
|
config := c.core.Config.OpenWrite()
|
|
|
|
c.data.LastConnected = time.Now().Format(time.RFC3339)
|
|
|
|
config.Contacts[strconv.Itoa(c.id)] = c.data
|
|
|
|
config.Save()
|
|
|
|
|
|
|
|
c.mutex.Unlock()
|
|
|
|
|
|
|
|
event := ricochet.ContactEvent{
|
|
|
|
Type: ricochet.ContactEvent_UPDATE,
|
|
|
|
Subject: &ricochet.ContactEvent_Contact{
|
|
|
|
Contact: c.Data(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c.events.Publish(event)
|
2016-09-16 00:32:58 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-20 01:53:21 +00:00
|
|
|
|
|
|
|
// Decide whether to replace the existing connection with conn.
|
|
|
|
// Assumes mutex is held.
|
|
|
|
func (c *Contact) shouldReplaceConnection(conn *protocol.OpenConnection) bool {
|
|
|
|
if c.connection == nil {
|
|
|
|
return true
|
|
|
|
} else if c.connection.Closed {
|
|
|
|
log.Printf("Replacing dead connection %v for contact %v", c.connection, c)
|
|
|
|
return true
|
|
|
|
} else if c.connection.Client == conn.Client {
|
|
|
|
// If the existing connection is in the same direction, always use the new one
|
|
|
|
log.Printf("Replacing existing same-direction connection %v with new connection %v for contact %v", c.connection, conn, c)
|
|
|
|
return true
|
|
|
|
} else if false {
|
|
|
|
// If the existing connection is more than 30 seconds old, use the new one
|
|
|
|
// XXX implement this
|
|
|
|
} else if preferOutbound := conn.MyHostname < conn.OtherHostname; preferOutbound == conn.Client {
|
|
|
|
// Fall back to string comparison of hostnames for a stable resolution
|
|
|
|
// New connection wins
|
|
|
|
log.Printf("Replacing existing connection %v with new connection %v for contact %v according to fallback order", c.connection, conn, c)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
// Old connection wins fallback
|
|
|
|
log.Printf("Keeping existing connection %v instead of new connection %v for contact %v according to fallback order", c.connection, conn, c)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2016-09-20 03:32:16 +00:00
|
|
|
|
|
|
|
func (c *Contact) OnConnectionClosed(conn *protocol.OpenConnection) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
|
|
|
|
if c.connection != conn {
|
|
|
|
c.mutex.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.connection = nil
|
|
|
|
c.status = ricochet.Contact_OFFLINE
|
|
|
|
|
|
|
|
config := c.core.Config.OpenWrite()
|
|
|
|
c.data.LastConnected = time.Now().Format(time.RFC3339)
|
|
|
|
config.Contacts[strconv.Itoa(c.id)] = c.data
|
|
|
|
config.Save()
|
|
|
|
|
|
|
|
c.mutex.Unlock()
|
|
|
|
|
|
|
|
event := ricochet.ContactEvent{
|
|
|
|
Type: ricochet.ContactEvent_UPDATE,
|
|
|
|
Subject: &ricochet.ContactEvent_Contact{
|
|
|
|
Contact: c.Data(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c.events.Publish(event)
|
|
|
|
}
|