Search in sources :

Example 1 with KeyData

use of net.i2p.router.startup.LoadRouterInfoJob.KeyData in project i2p.i2p by i2p.

the class RebuildRouterInfoJob method rebuildRouterInfo.

/**
 *  @param alreadyRunning unused
 */
void rebuildRouterInfo(boolean alreadyRunning) {
    _log.debug("Rebuilding the new router info");
    RouterInfo info = null;
    File infoFile = new File(getContext().getRouterDir(), CreateRouterInfoJob.INFO_FILENAME);
    File keyFile = new File(getContext().getRouterDir(), CreateRouterInfoJob.KEYS_FILENAME);
    File keyFile2 = new File(getContext().getRouterDir(), CreateRouterInfoJob.KEYS2_FILENAME);
    if (keyFile2.exists() || keyFile.exists()) {
        // ok, no need to rebuild a brand new identity, just update what we can
        RouterInfo oldinfo = getContext().router().getRouterInfo();
        if (oldinfo == null) {
            try {
                KeyData kd = LoadRouterInfoJob.readKeyData(keyFile, keyFile2);
                info = new RouterInfo();
                info.setIdentity(kd.routerIdentity);
            } catch (DataFormatException e) {
                _log.log(Log.CRIT, "Error reading in the key data from " + keyFile.getAbsolutePath(), e);
                keyFile.delete();
                keyFile2.delete();
                rebuildRouterInfo(alreadyRunning);
                return;
            } catch (IOException e) {
                _log.log(Log.CRIT, "Error reading in the key data from " + keyFile.getAbsolutePath(), e);
                keyFile.delete();
                keyFile2.delete();
                rebuildRouterInfo(alreadyRunning);
                return;
            }
        } else {
            // Make a new RI from the old identity, or else info.setAddresses() will throw an ISE
            info = new RouterInfo(oldinfo);
        }
        try {
            info.setAddresses(getContext().commSystem().createAddresses());
            Properties stats = getContext().statPublisher().publishStatistics(info.getHash());
            info.setOptions(stats);
            // info.setPeers(new HashSet()); // this would have the trusted peers
            info.setPublished(CreateRouterInfoJob.getCurrentPublishDate(getContext()));
            info.sign(getContext().keyManager().getSigningPrivateKey());
        } catch (DataFormatException dfe) {
            _log.log(Log.CRIT, "Error rebuilding the new router info", dfe);
            return;
        }
        if (!info.isValid()) {
            _log.log(Log.CRIT, "RouterInfo we just built is invalid: " + info, new Exception());
            return;
        }
        FileOutputStream fos = null;
        synchronized (getContext().router().routerInfoFileLock) {
            try {
                fos = new SecureFileOutputStream(infoFile);
                info.writeBytes(fos);
            } catch (DataFormatException dfe) {
                _log.log(Log.CRIT, "Error rebuilding the router information", dfe);
            } catch (IOException ioe) {
                _log.log(Log.CRIT, "Error writing out the rebuilt router information", ioe);
            } finally {
                if (fos != null)
                    try {
                        fos.close();
                    } catch (IOException ioe) {
                    }
            }
        }
    } else {
        _log.warn("Private key file " + keyFile.getAbsolutePath() + " deleted!  Rebuilding a brand new router identity!");
        // this proc writes the keys and info to the file as well as builds the latest and greatest info
        CreateRouterInfoJob j = new CreateRouterInfoJob(getContext(), null);
        synchronized (getContext().router().routerInfoFileLock) {
            info = j.createRouterInfo();
        }
    }
    // MessageHistory.initialize();
    getContext().router().setRouterInfo(info);
    _log.info("Router info rebuilt and stored at " + infoFile + " [" + info + "]");
}
Also used : DataFormatException(net.i2p.data.DataFormatException) RouterInfo(net.i2p.data.router.RouterInfo) FileOutputStream(java.io.FileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) DataFormatException(net.i2p.data.DataFormatException) IOException(java.io.IOException) KeyData(net.i2p.router.startup.LoadRouterInfoJob.KeyData)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 DataFormatException (net.i2p.data.DataFormatException)1 RouterInfo (net.i2p.data.router.RouterInfo)1 KeyData (net.i2p.router.startup.LoadRouterInfoJob.KeyData)1 SecureFileOutputStream (net.i2p.util.SecureFileOutputStream)1