use of net.i2p.data.Hash in project i2p.i2p by i2p.
the class ConfigPeerHandler method processForm.
@Override
protected void processForm() {
if ("Save Configuration".equals(_action)) {
_context.router().saveConfig();
addFormNotice("Settings saved - not really!!!!!");
} else if (_action.equals(_t("Ban peer until restart"))) {
Hash h = getHash();
if (h != null) {
_context.banlist().banlistRouterForever(h, _t("Manually banned via {0}"), "<a href=\"configpeer\">configpeer</a>");
addFormNotice(_t("Peer") + " " + _peer + " " + _t("banned until restart"));
return;
}
addFormError(_t("Invalid peer"));
} else if (_action.equals(_t("Unban peer"))) {
Hash h = getHash();
if (h != null) {
if (_context.banlist().isBanlisted(h)) {
_context.banlist().unbanlistRouter(h);
addFormNotice(_t("Peer") + " " + _peer + " " + _t("unbanned"));
} else
addFormNotice(_t("Peer") + " " + _peer + " " + _t("is not currently banned"));
return;
}
addFormError(_t("Invalid peer"));
} else if (_action.equals(_t("Adjust peer bonuses"))) {
Hash h = getHash();
if (h != null) {
PeerProfile prof = _context.profileOrganizer().getProfile(h);
if (prof != null) {
try {
prof.setSpeedBonus(Integer.parseInt(_speed));
} catch (NumberFormatException nfe) {
addFormError(_t("Bad speed value"));
}
try {
prof.setCapacityBonus(Integer.parseInt(_capacity));
} catch (NumberFormatException nfe) {
addFormError(_t("Bad capacity value"));
}
addFormNotice("Bonuses adjusted for " + _peer);
} else
addFormError("No profile exists for " + _peer);
return;
}
addFormError(_t("Invalid peer"));
} else if (_action.startsWith("Check")) {
addFormError(_t("Unsupported"));
} else {
// addFormError(_t("Unsupported") + ' ' + _action + '.');
}
}
use of net.i2p.data.Hash in project i2p.i2p by i2p.
the class I2PSocketEepGet method sendRequest.
/**
* Look up the address, get a socket from the I2PSocketManager supplied in the constructor,
* and send the request.
*
* @param timeout ignored
*/
@Override
protected void sendRequest(SocketTimeout timeout) throws IOException {
if (_outputStream == null) {
File outFile = new File(_outputFile);
if (outFile.exists())
_alreadyTransferred = outFile.length();
}
if (_proxyIn != null)
try {
_proxyIn.close();
} catch (IOException ioe) {
}
if (_proxyOut != null)
try {
_proxyOut.close();
} catch (IOException ioe) {
}
if (_socket != null)
try {
_socket.close();
} catch (IOException ioe) {
}
try {
URI url = new URI(_actualURL);
if ("http".equals(url.getScheme())) {
String host = url.getHost();
if (host == null)
throw new MalformedURLException("no hostname: " + _actualURL);
int port = url.getPort();
if (port <= 0 || port > 65535)
port = 80;
// as the naming service accepts B64KEY (but not B64KEY.i2p atm)
if ("i2p".equals(host)) {
String file = url.getRawPath();
try {
int slash = 1 + file.substring(1).indexOf('/');
host = file.substring(1, slash);
_actualURL = "http://" + host + file.substring(slash);
String query = url.getRawQuery();
if (query != null)
_actualURL = _actualURL + '?' + query;
} catch (IndexOutOfBoundsException ioobe) {
throw new MalformedURLException("Bad /i2p/ format: " + _actualURL);
}
}
// Use existing I2PSession for lookups.
// This is much more efficient than using the naming service
Destination dest;
I2PSession sess = _socketManager.getSession();
if (sess != null && !sess.isClosed()) {
try {
if (host.length() == 60 && host.endsWith(".b32.i2p")) {
byte[] b = Base32.decode(host.substring(0, 52));
if (b != null) {
Hash h = Hash.create(b);
dest = sess.lookupDest(h, 20 * 1000);
} else {
dest = null;
}
} else {
dest = sess.lookupDest(host, 20 * 1000);
}
} catch (I2PSessionException ise) {
dest = null;
}
} else {
dest = _context.namingService().lookup(host);
}
if (dest == null)
throw new UnknownHostException("Unknown or non-i2p host: " + host);
// Set the timeouts, using the other existing options in the socket manager
// This currently duplicates what SocketTimeout is doing in EepGet,
// but when that's ripped out of EepGet to use setsotimeout, we'll need this.
Properties props = new Properties();
props.setProperty(I2PSocketOptions.PROP_CONNECT_TIMEOUT, "" + CONNECT_TIMEOUT);
props.setProperty(I2PSocketOptions.PROP_READ_TIMEOUT, "" + INACTIVITY_TIMEOUT);
// This is important - even if the underlying socket doesn't have a connect delay,
// we want to set it for this connection, so the request headers will go out
// in the SYN packet, saving one RTT.
props.setProperty(PROP_CONNECT_DELAY, CONNECT_DELAY);
I2PSocketOptions opts = _socketManager.buildOptions(props);
opts.setPort(port);
_socket = _socketManager.connect(dest, opts);
} else {
throw new MalformedURLException("Unsupported protocol: " + _actualURL);
}
} catch (URISyntaxException use) {
IOException ioe = new MalformedURLException("Bad URL");
ioe.initCause(use);
throw ioe;
} catch (I2PException ie) {
throw new IOException("I2P error", ie);
}
_proxyIn = _socket.getInputStream();
_proxyOut = _socket.getOutputStream();
// SocketTimeout doesn't take an I2PSocket, but no matter, because we
// always close our socket in fetch() above.
// timeout.setSocket(_socket);
String req = getRequest();
_proxyOut.write(DataHelper.getUTF8(req));
_proxyOut.flush();
}
use of net.i2p.data.Hash in project i2p.i2p by i2p.
the class I2PSessionImpl method destReceived.
/**
* Called by the message handler
* on reception of DestReplyMessage
* @param d non-null
*/
void destReceived(Destination d) {
Hash h = d.calculateHash();
synchronized (_lookupCache) {
_lookupCache.put(h, d);
}
for (LookupWaiter w : _pendingLookups) {
if (h.equals(w.hash)) {
synchronized (w) {
w.destination = d;
w.notifyAll();
}
}
}
}
use of net.i2p.data.Hash in project i2p.i2p by i2p.
the class I2PSessionImpl method destReceived.
/**
* Called by the message handler
* on reception of HostReplyMessage
* @param d non-null
* @since 0.9.11
*/
void destReceived(long nonce, Destination d) {
// notify by nonce and hash
Hash h = d.calculateHash();
for (LookupWaiter w : _pendingLookups) {
if (nonce == w.nonce || h.equals(w.hash)) {
synchronized (_lookupCache) {
if (w.name != null)
_lookupCache.put(w.name, d);
_lookupCache.put(h, d);
}
synchronized (w) {
w.destination = d;
w.notifyAll();
}
}
}
}
use of net.i2p.data.Hash in project i2p.i2p by i2p.
the class ConnectionAcceptor method run2.
private void run2() {
while (!stop) {
I2PServerSocket serverSocket = _util.getServerSocket();
while ((serverSocket == null) && (!stop)) {
if (!(_util.isConnecting() || _util.connected())) {
stop = true;
break;
}
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException ie) {
}
serverSocket = _util.getServerSocket();
}
if (stop)
break;
try {
I2PSocket socket = serverSocket.accept();
if (socket == null) {
continue;
} else {
if (socket.getPeerDestination().equals(_util.getMyDestination())) {
_log.error("Incoming connection from myself");
try {
socket.close();
} catch (IOException ioe) {
}
continue;
}
Hash h = socket.getPeerDestination().calculateHash();
if (socket.getLocalPort() == 80) {
_badCounter.increment(h);
if (_log.shouldLog(Log.WARN))
_log.error("Dropping incoming HTTP from " + h);
try {
socket.close();
} catch (IOException ioe) {
}
continue;
}
int bad = _badCounter.count(h);
if (bad >= MAX_BAD) {
if (_log.shouldLog(Log.WARN))
_log.warn("Rejecting connection from " + h + " after " + bad + " failures, max is " + MAX_BAD);
try {
socket.close();
} catch (IOException ioe) {
}
continue;
}
Thread t = new I2PAppThread(new Handler(socket), "I2PSnark incoming connection");
t.start();
}
} catch (I2PException ioe) {
int level = stop ? Log.WARN : Log.ERROR;
if (_log.shouldLog(level))
_log.log(level, "Error while accepting", ioe);
synchronized (this) {
if (!stop) {
locked_halt();
thread = null;
stop = true;
}
}
} catch (ConnectException ioe) {
// which does not currently call our halt(), although it should
if (_log.shouldWarn())
_log.warn("Error while accepting", ioe);
synchronized (this) {
if (!stop) {
locked_halt();
thread = null;
stop = true;
}
}
} catch (IOException ioe) {
int level = stop ? Log.WARN : Log.ERROR;
if (_log.shouldLog(level))
_log.log(level, "Error while accepting", ioe);
synchronized (this) {
if (!stop) {
locked_halt();
thread = null;
stop = true;
}
}
}
// catch oom?
}
if (_log.shouldLog(Log.WARN))
_log.warn("ConnectionAcceptor closed");
}
Aggregations