use of net.i2p.util.Log in project i2p.i2p by i2p.
the class ThreadDump method dump.
/**
* Signal the wrapper to asynchronously dump threads to wrapper.log.
* It waits for the signal to complete (which should be fast)
* but does not wait for the dump itself.
*
* @param secondsToWait if <= 0, don't wait
* @return success, false if windows or no wrapper, true if secondsToWait <= 0,
* false if timed out, dump result otherwise
*/
public static boolean dump(I2PAppContext context, int secondsToWait) {
if (SystemVersion.isWindows() || !context.hasWrapper())
return false;
ShellCommand sc = new ShellCommand();
File i2pr = new File(context.getBaseDir(), "i2prouter");
String[] args = new String[2];
args[0] = i2pr.getAbsolutePath();
args[1] = "dump";
boolean success = sc.executeSilentAndWaitTimed(args, secondsToWait);
if (secondsToWait <= 0)
success = true;
if (success) {
Log log = context.logManager().getLog(ThreadDump.class);
File f = new File(context.getConfigDir(), "wrapper.log");
String loc = f.exists() ? f.getAbsolutePath() : "wrapper.log";
log.log(Log.CRIT, "Threads dumped to " + loc);
}
return success;
}
use of net.i2p.util.Log in project i2p.i2p-bote by i2p.
the class GeneralHelper method saveContact.
/**
* Updates a contact in the address book if the Destination <code>destinationString</code> exists,
* or adds a new contact to the address book.
* @param destinationString A base64-encoded Email Destination key
* @param name
* @param pictureBase64
* @param text
* @return null if sucessful, or an error message if an error occured
* @throws PasswordException
* @throws GeneralSecurityException
*/
public static String saveContact(String destinationString, String name, String pictureBase64, String text) throws PasswordException, GeneralSecurityException {
destinationString = Util.fixAddress(destinationString);
AddressBook addressBook = getInstance().getAddressBook();
Contact contact = addressBook.get(destinationString);
if (contact != null) {
contact.setName(name);
contact.setPictureBase64(pictureBase64);
contact.setText(text);
} else {
EmailDestination destination;
try {
destination = new EmailDestination(destinationString);
} catch (GeneralSecurityException e) {
Log log = new Log(GeneralHelper.class);
log.error("Can't save contact to address book.", e);
return e.getLocalizedMessage();
}
contact = new Contact(name, destination, pictureBase64, text);
addressBook.add(contact);
}
try {
addressBook.save();
return null;
} catch (IOException e) {
return e.getLocalizedMessage();
}
}
use of net.i2p.util.Log in project i2p.i2p-bote by i2p.
the class GeneralHelper method createOrModifyIdentity.
/**
* Updates an email identity if <code>createNew</code> is <code>false</code>,
* or adds a new identity if <code>createNew</code> is <code>true</code>.
* @param createNew
* @param cryptoImplId The id value of the cryptographic algorithm set to use for the new identity; ignored if <code>createNew</code> is <code>false</code>
* @param vanityPrefix An alphanumeric string the destination should start with; ignored if <code>createNew==false</code>.
* @param key A base64-encoded Email Destination key
* @param description
* @param publicName
* @param picture
* @param emailAddress
* @param setDefault If this is <code>true</code>, the identity becomes the new default identity. Otherwise, the default stays the same.
* @throws GeneralSecurityException
* @throws PasswordException
* @throws IOException
* @throws IllegalDestinationParametersException if <code>cryptoImplId</code> and <code>vanityPrefix</code> aren't compatible
*/
public static void createOrModifyIdentity(boolean createNew, int cryptoImplId, String vanityPrefix, String key, String publicName, String description, String pictureBase64, String emailAddress, Properties config, boolean setDefault, StatusListener<ChangeIdentityStatus> lsnr) throws GeneralSecurityException, PasswordException, IOException, IllegalDestinationParametersException {
Log log = new Log(GeneralHelper.class);
Identities identities = I2PBote.getInstance().getIdentities();
EmailIdentity identity;
if (createNew) {
CryptoImplementation cryptoImpl = CryptoFactory.getInstance(cryptoImplId);
if (cryptoImpl == null) {
log.error("Invalid ID number for CryptoImplementation: " + cryptoImplId);
throw new IllegalArgumentException("Invalid ID number for CryptoImplementation: " + cryptoImplId);
}
lsnr.updateStatus(ChangeIdentityStatus.GENERATING_KEYS);
try {
identity = new EmailIdentity(cryptoImpl, vanityPrefix);
} catch (GeneralSecurityException e) {
log.error("Can't generate email identity for CryptoImplementation: <" + cryptoImpl + "> with vanity prefix: <" + vanityPrefix + ">", e);
throw e;
}
} else
identity = identities.get(key);
identity.setPublicName(publicName);
identity.setDescription(description);
identity.setPictureBase64(pictureBase64);
identity.setEmailAddress(emailAddress);
// update the identity config
if (config != null)
identity.loadConfig(config, "", false);
if (createNew)
identities.add(identity);
else
identities.identityUpdated(key);
// update the default identity
if (setDefault)
identities.setDefault(identity);
}
use of net.i2p.util.Log in project i2p.i2p-bote by i2p.
the class RelayRequest method create.
/**
* Creates a <code>RelayRequest</code> containing <code>numHops</code> nested
* <code>RelayRequest</code>s<br/>
* Returns <code>null</code> if <code>numHops</code> is <code>0</code>.
* @param payload
* @param peerManager For obtaining relay peers
* @param numHops
* @param minDelay The minimum delay in milliseconds
* @param maxDelay The maximum delay in milliseconds
*/
public static RelayRequest create(CommunicationPacket payload, RelayPeerManager peerManager, int numHops, long minDelay, long maxDelay) {
List<Destination> relayPeers = peerManager.getRandomPeers(numHops);
Log log = new Log(RelayRequest.class);
if (log.shouldLog(Log.DEBUG)) {
StringBuilder debugMsg = new StringBuilder("Creating relay chain: [");
for (int i = relayPeers.size() - 1; i >= 0; i--) {
debugMsg.append(Util.toShortenedBase32(relayPeers.get(i)));
if (i > 0)
debugMsg.append(" --> ");
}
debugMsg.append("]");
log.debug(debugMsg.toString());
}
// calculate the number of pad bytes necessary to pad the payload to the maximum size possible
int maxSize = I2PBotePacket.MAX_DATAGRAM_SIZE - getMaxOverhead(numHops);
int padBytes = maxSize - payload.getSize();
if (padBytes < 0)
padBytes = 0;
CommunicationPacket request = payload;
for (Destination relayPeer : relayPeers) {
// generate a random time between minDelay and maxDelay in the future
long delay;
if (minDelay == maxDelay)
delay = minDelay;
else
delay = minDelay + Math.abs(random.nextLong()) % Math.abs(maxDelay - minDelay);
request = new RelayRequest(request, relayPeer, delay, padBytes);
// only pad the innermost packet (the payload)
padBytes = 0;
}
return (RelayRequest) request;
}
use of net.i2p.util.Log in project i2p.i2p-bote by i2p.
the class I2PBotePacket method createPacket.
/**
* Creates an <code>I2PBotePacket</code> from a file, using the same format as the
* {@link #createPacket(byte[])} method.
* @param file
* @throws MalformedPacketException
*/
public static I2PBotePacket createPacket(File file) throws MalformedPacketException {
if (file == null || !file.exists())
return null;
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
I2PBotePacket packet = createPacket(Util.readBytes(inputStream));
return packet;
} catch (IOException e) {
throw new MalformedPacketException("Can't read packet file: " + file.getAbsolutePath(), e);
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
Log log = new Log(I2PBotePacket.class);
log.error("Can't close stream.", e);
}
}
}
Aggregations