Ejemplo n.º 1
0
/*
Store writes the current state of the structure to disk so that it can be loaded
again later.
*/
func (enc *Encrypted) Store() error {
	// make org directory
	err := shared.MakeEncryptedDir(enc.RootPath)
	if err != nil {
		return err
	}
	// store self peer
	toxData, err := enc.channel.ToxData()
	if err != nil {
		return err
	}
	selfPeer := &shared.ToxPeerDump{
		SelfPeer: enc.Peer,
		ToxData:  toxData}
	return selfPeer.StoreTo(enc.RootPath + "/" + shared.LOCALDIR)
}
Ejemplo n.º 2
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
}
Ejemplo n.º 3
0
/*
Store writes a bootstrapped .TINZENITEDIR to disk. Call this if you want
persistant bootstrapping (and why wouldn't you?).
*/
func (b *Bootstrap) Store() error {
	trusted := b.IsTrusted()
	var err error
	if trusted {
		err = shared.MakeTinzeniteDir(b.path)
	} else {
		err = shared.MakeEncryptedDir(b.path)
	}
	if err != nil {
		return err
	}
	// write self peer if TRUSTED peer. Encrypted don't write their own peer.
	if trusted {
		err = b.peer.StoreTo(b.path + "/" + shared.STOREPEERDIR)
	}
	if err != nil {
		return err
	}
	// store local peer info with toxdata
	toxData, err := b.channel.ToxData()
	if err != nil {
		return err
	}
	toxPeerDump := &shared.ToxPeerDump{
		SelfPeer: b.peer,
		ToxData:  toxData}
	// write toxpeerdump
	if trusted {
		err = toxPeerDump.StoreTo(b.path + "/" + shared.STORETOXDUMPDIR)
	} else {
		err = toxPeerDump.StoreTo(b.path + "/" + shared.LOCALDIR)
	}
	if err != nil {
		return err
	}
	return nil
}
Ejemplo 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
}