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 } // 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. func (s *BaseOnionService) GetConnection(connectionID string) (*tapir.Connection, error) { conn, ok := s.connections.Load(connectionID) if !ok { return nil, errors.New("no connection found") } connection := conn.(*tapir.Connection) return connection, nil } // Connect initializes a new outbound connection to the given peer, using the defined Application func (s *BaseOnionService) Connect(hostname string, app tapir.Application) (string, error) { // 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() log.Debugf("Connected to %v [%v]", hostname, connectionID) s.connections.Store(connectionID, tapir.NewConnection(s.id, hostname, true, conn, app.NewInstance())) return connectionID, nil } log.Debugf("Error connecting to %v %v", hostname, err) return "", err } 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) log.Debugf("Starting a service on %v ", ls.AddressFull()) if err == nil { for { conn, err := ls.Accept() 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 { ls.Close() log.Debugf("Error accepting connection %v", err) return err } } } log.Debugf("Error listening to connection %v", err) return err }