tapir/networks/tor/BaseOnionService.go

147 lines
4.7 KiB
Go
Raw Permalink Normal View History

2019-06-06 16:41:03 +00:00
package tor
import (
"crypto/rand"
"cwtch.im/tapir"
"encoding/base64"
"errors"
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
"git.openprivacy.ca/openprivacy/libricochet-go/identity"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"golang.org/x/crypto/ed25519"
"sync"
"time"
)
// BaseOnionService is a concrete implementation of the service interface over Tor onion services.
type BaseOnionService struct {
connections sync.Map
acn connectivity.ACN
id identity.Identity
privateKey ed25519.PrivateKey
2019-07-17 18:44:08 +00:00
ls connectivity.ListenService
2019-06-06 16:41:03 +00:00
}
// Init initializes a BaseOnionService with a given private key and identity
// The private key is needed to initialize the Onion listen socket, ideally we could just pass an Identity in here.
func (s *BaseOnionService) Init(acn connectivity.ACN, sk ed25519.PrivateKey, id identity.Identity) {
// run add onion
// get listen context
s.acn = acn
s.id = id
s.privateKey = sk
}
// WaitForCapabilityOrClose blocks until the connection has the given capability or the underlying connection is closed
// (through error or user action)
func (s *BaseOnionService) WaitForCapabilityOrClose(cid string, name string) (*tapir.Connection, error) {
conn, err := s.GetConnection(cid)
if err == nil {
for {
if conn.HasCapability(name) {
return conn, nil
}
if conn.Closed {
return nil, errors.New("connection is closed")
}
time.Sleep(time.Millisecond * 200)
}
}
return nil, err
}
// GetConnection returns a connection for a given hostname.
2019-07-16 20:22:30 +00:00
func (s *BaseOnionService) GetConnection(hostname string) (*tapir.Connection, error) {
var conn *tapir.Connection
s.connections.Range(func(key, value interface{}) bool {
connection := value.(*tapir.Connection)
if connection.Hostname == hostname {
if !connection.Closed {
conn = connection
return false
}
}
return true
})
if conn == nil {
2019-06-06 16:41:03 +00:00
return nil, errors.New("no connection found")
}
2019-07-16 20:22:30 +00:00
return conn, nil
2019-06-06 16:41:03 +00:00
}
// Connect initializes a new outbound connection to the given peer, using the defined Application
2019-07-30 23:43:07 +00:00
func (s *BaseOnionService) Connect(hostname string, app tapir.Application) (bool, error) {
2019-07-16 20:22:30 +00:00
_, err := s.GetConnection(hostname)
if err == nil {
// Note: This check is not 100% reliable. And we may end up with two connections between peers
// This can happen when a client connects to a server as the server is connecting to the client
// Because at the start of the connection the server cannot derive the true hostname of the client until it
// has auth'd
// We mitigate this by performing multiple checks when Connect'ing
2019-07-30 23:43:07 +00:00
return true, errors.New("already connected to " + hostname)
2019-07-16 20:22:30 +00:00
}
2019-06-06 16:41:03 +00:00
// connects to a remote server
// spins off to a connection struct
log.Debugf("Connecting to %v", hostname)
conn, _, err := s.acn.Open(hostname)
if err == nil {
connectionID := s.getNewConnectionID()
2019-07-16 20:22:30 +00:00
// Second check. If we didn't catch a double connection attempt before the Open we *should* catch it now because
// the auth protocol is quick and Open over onion connections can take some time.
// Again this isn't 100% reliable.
_, err := s.GetConnection(hostname)
if err == nil {
conn.Close()
2019-07-30 23:43:07 +00:00
return true, errors.New("already connected to " + hostname)
2019-07-16 20:22:30 +00:00
}
2019-06-06 16:41:03 +00:00
log.Debugf("Connected to %v [%v]", hostname, connectionID)
s.connections.Store(connectionID, tapir.NewConnection(s.id, hostname, true, conn, app.NewInstance()))
2019-07-30 23:43:07 +00:00
return true, nil
2019-06-06 16:41:03 +00:00
}
log.Debugf("Error connecting to %v %v", hostname, err)
2019-07-30 23:43:07 +00:00
return false, err
2019-06-06 16:41:03 +00:00
}
func (s *BaseOnionService) getNewConnectionID() string {
id := make([]byte, 10)
rand.Read(id)
connectionID := "connection-" + base64.StdEncoding.EncodeToString(id)
return connectionID
}
// Listen starts a blocking routine that waits for incoming connections and initializes connections with them based
// on the given Application.
func (s *BaseOnionService) Listen(app tapir.Application) error {
// accepts a new connection
// spins off to a connection struct
ls, err := s.acn.Listen(s.privateKey, 9878)
2019-07-17 18:44:08 +00:00
s.ls = ls
2019-06-06 16:41:03 +00:00
log.Debugf("Starting a service on %v ", ls.AddressFull())
if err == nil {
for {
2019-07-17 18:44:08 +00:00
conn, err := s.ls.Accept()
2019-06-06 16:41:03 +00:00
if err == nil {
tempHostname := s.getNewConnectionID()
log.Debugf("Accepted connection from %v", tempHostname)
s.connections.Store(tempHostname, tapir.NewConnection(s.id, tempHostname, false, conn, app.NewInstance()))
} else {
log.Debugf("Error accepting connection %v", err)
return err
}
}
}
log.Debugf("Error listening to connection %v", err)
return err
}
2019-07-17 18:44:08 +00:00
func (s *BaseOnionService) Shutdown() {
s.ls.Close()
2019-07-17 19:00:27 +00:00
s.connections.Range(func(key, value interface{}) bool {
connection := value.(*tapir.Connection)
connection.Close()
2019-07-23 18:41:49 +00:00
return true
2019-07-17 19:00:27 +00:00
})
2019-07-17 18:44:08 +00:00
}