Esempio n. 1
0
/*
Load tries to load the given directory as a bootstrap object, allowing it to
connect to an existing network. To actually start bootstrapping call
Bootstrap.Start(address). NOTE: will fail if already connected to other peers!
*/
func Load(path string, f Success) (*Bootstrap, error) {
	// we need to do some logic to detect where we can load stuff from
	trusted, err := isLoadable(path)
	if err != nil {
		return nil, err
	}
	// create object
	boot := &Bootstrap{
		path:   path,
		onDone: f}
	boot.cInterface = createChanInterface(boot)
	// load self peer from correct location
	var toxPeerDump *shared.ToxPeerDump
	if trusted {
		toxPeerDump, err = shared.LoadToxDumpFrom(path + "/" + shared.STORETOXDUMPDIR)
	} else {
		toxPeerDump, err = shared.LoadToxDumpFrom(path + "/" + shared.LOCALDIR)
	}
	if err != nil {
		return nil, err
	}
	boot.peer = toxPeerDump.SelfPeer
	channel, err := channel.Create(boot.peer.Name, toxPeerDump.ToxData, boot.cInterface)
	if err != nil {
		return nil, err
	}
	boot.channel = channel
	// bg stuff
	boot.wg.Add(1)
	boot.stop = make(chan bool, 1)
	go boot.run()
	return boot, nil
}
Esempio n. 2
0
/*
Load returns the Encrypted structure for an existing instance.
*/
func Load(path string, storage Storage) (*Encrypted, error) {
	// TODO missing check whether this is a valid path... FIXME
	// make missing dirs if path ok? createEncryptedDirectories(path)
	// ensure valid parameters
	if path == "" || storage == nil {
		return nil, shared.ErrIllegalParameters
	}
	// build structure
	encrypted := &Encrypted{
		RootPath: path,
		storage:  storage}
	// prepare interface
	encrypted.cInterface = createChanInterface(encrypted)
	// load data
	selfPeer, err := shared.LoadToxDumpFrom(path + "/" + shared.LOCALDIR)
	if err != nil {
		return nil, err
	}
	// set self peer
	encrypted.Peer = selfPeer.SelfPeer
	// build channel
	encrypted.channel, err = channel.Create(encrypted.Peer.Name, selfPeer.ToxData, encrypted.cInterface)
	if err != nil {
		return nil, err
	}
	// run background
	initialize(encrypted)
	// return instance
	return encrypted, nil
}
Esempio n. 3
0
/*
Create returns a struct that will allow to bootstrap to an existing Tinzenite
network. To actually start bootstrapping call Bootstrap.Start(address).

Path: the absolute path to the directory. localPeerName: the user defined name
of this peer. trusted: whether this should be a trusted peer or an encrypted
one. f: the callback to call once the bootstrap has successfully run.
*/
func Create(path, localPeerName string, trusted bool, f Success) (*Bootstrap, error) {
	if shared.IsTinzenite(path) {
		return nil, shared.ErrIsTinzenite
	}
	// build structure
	var err error
	if trusted {
		err = shared.MakeTinzeniteDir(path)
	} else {
		err = shared.MakeEncryptedDir(path)
	}
	// creation of structure error
	if err != nil {
		return nil, err
	}
	// create object
	boot := &Bootstrap{
		path:   path,
		onDone: f}
	boot.cInterface = createChanInterface(boot)
	channel, err := channel.Create(localPeerName, nil, boot.cInterface)
	if err != nil {
		return nil, err
	}
	boot.channel = channel
	// get address for peer
	address, err := boot.channel.Address()
	if err != nil {
		return nil, err
	}
	// make peer (at correct location!)
	peer, err := shared.CreatePeer(localPeerName, address, trusted)
	if err != nil {
		return nil, err
	}
	boot.peer = peer
	// bg stuff
	boot.wg.Add(1)
	boot.stop = make(chan bool, 1)
	go boot.run()
	return boot, nil
}
Esempio n. 4
0
/*
Create returns a new Encrypted instance, ready to be connected to an existing
network.
*/
func Create(path, peerName string, storage Storage) (*Encrypted, error) {
	// must start on empty directory
	if empty, err := shared.IsDirectoryEmpty(path); !empty {
		if err != nil {
			return nil, err
		}
		return nil, ErrNonEmpty
	}
	// ensure valid parameters
	if path == "" || peerName == "" || storage == nil {
		return nil, shared.ErrIllegalParameters
	}
	// flag whether we need to clen up after us
	var failed bool
	// make directories
	err := shared.MakeEncryptedDir(path)
	if err != nil {
		return nil, err
	}
	// if failed was set --> clean up by removing everything
	defer func() {
		if failed {
			shared.RemoveDirContents(path)
		}
	}()
	// build
	encrypted := &Encrypted{
		RootPath: path, // rootPath for storing root
		storage:  storage}
	// prepare chaninterface
	encrypted.cInterface = createChanInterface(encrypted)
	// build channel
	channel, err := channel.Create(peerName, nil, encrypted.cInterface)
	if err != nil {
		failed = true
		return nil, err
	}
	encrypted.channel = channel
	// get address for peer
	address, err := encrypted.channel.Address()
	if err != nil {
		failed = true
		return nil, err
	}
	// make peer with name, address, and set trusted to false
	peer, err := shared.CreatePeer(peerName, address, false)
	if err != nil {
		failed = true
		return nil, err
	}
	encrypted.Peer = peer
	// run background
	initialize(encrypted)
	// store initial copy
	err = encrypted.Store()
	if err != nil {
		failed = true
		return nil, err
	}
	// return instance
	return encrypted, nil
}