use of com.biglybt.pif.messaging.generic.GenericMessageStartpoint in project BiglyBT by BiglySoftware.
the class BuddyPluginNetwork method registerMessageHandler.
protected void registerMessageHandler() {
try {
addRequestListener(new BuddyPluginBuddyRequestListener() {
@Override
public Map requestReceived(BuddyPluginBuddy from_buddy, int subsystem, Map request) throws BuddyPluginException {
if (subsystem == SUBSYSTEM_INTERNAL) {
if (!from_buddy.isAuthorised()) {
throw (new BuddyPluginException("Unauthorised"));
}
return (processInternalRequest(from_buddy, request));
}
return (null);
}
@Override
public void pendingMessages(BuddyPluginBuddy[] from_buddies) {
}
});
msg_registration = plugin_interface.getMessageManager().registerGenericMessageType(isPublicNetwork() ? "AZBUDDY" : "BGBUDDYA", "Buddy message handler (" + target_network + ")", STREAM_CRYPTO, new GenericMessageHandler() {
@Override
public boolean accept(GenericMessageConnection connection) throws MessageException {
if (!plugin.isClassicEnabled()) {
return (false);
}
DDBDetails details = null;
InetSocketAddress address = connection.getEndpoint().getNotionalAddress();
final String originator = AddressUtils.getHostAddress(address);
String net = AENetworkClassifier.categoriseAddress(address);
for (DDBDetails d : ddb_details) {
if (d.getNetwork() == net) {
details = d;
break;
}
}
if (details == null) {
if (TRACE) {
System.out.println("accept - no details for " + net);
}
return (false);
}
if (details.getNetwork() != AENetworkClassifier.AT_PUBLIC) {
boolean ok = false;
GenericMessageStartpoint start = connection.getStartpoint();
InetSocketAddress start_address = null;
InetSocketAddress ddb_address1 = null;
InetSocketAddress ddb_address2 = null;
if (start != null) {
start_address = start.getNotionalAddress();
ddb_address1 = details.getDDB().getDHTPlugin().getConnectionOrientedEndpoint();
ok = AddressUtils.sameHost(start_address, ddb_address1);
if (!ok) {
ddb_address2 = details.getDDB().getLocalContact().getAddress();
if (ddb_address2 != null) {
ok = AddressUtils.sameHost(start_address, ddb_address2);
}
}
}
if (!ok) {
if (TRACE) {
System.out.println("accept - ddb address mismatch: " + start_address + "/" + ddb_address1 + "/" + ddb_address2);
}
return (false);
}
}
final DDBDetails f_details = details;
if (TRACE) {
System.out.println("accept " + originator);
}
try {
String reason = "Friend: Incoming connection establishment (" + originator + ")";
plugin.addRateLimiters(connection);
connection = getSTSConnection(connection, reason, new SEPublicKeyLocator() {
@Override
public boolean accept(Object context, SEPublicKey other_key) {
String other_key_str = Base32.encode(other_key.encodeRawPublicKey());
if (TRACE) {
System.out.println("Incoming: acceptKey - " + other_key_str);
}
try {
synchronized (BuddyPluginNetwork.this) {
int unauth_count = 0;
for (int i = 0; i < buddies.size(); i++) {
BuddyPluginBuddy buddy = (BuddyPluginBuddy) buddies.get(i);
if (buddy.getPublicKey().equals(other_key_str)) {
if (!buddy.isAuthorised()) {
log(buddy, "Incoming connection from " + originator + " failed as for unauthorised buddy");
return (false);
}
buddy.incomingConnection(f_details, (GenericMessageConnection) context);
return (true);
}
if (!buddy.isAuthorised()) {
unauth_count++;
}
}
if (unauth_count < MAX_UNAUTH_BUDDIES) {
if (tooManyUnauthConnections(originator)) {
log(null, "Too many recent unauthorised connections from " + originator);
return (false);
}
BuddyPluginBuddy buddy = addBuddy(other_key_str, SUBSYSTEM_AZ2, false, false);
if (buddy != null) {
buddy.incomingConnection(f_details, (GenericMessageConnection) context);
return (true);
} else {
log(null, "Incoming connection from " + originator + " failed due to pk mismatch");
return (false);
}
} else {
log(null, "Incoming connection from " + originator + " rejected, too many unauthorised buddies");
return (false);
}
}
} catch (Throwable e) {
log(null, "Incomming connection from " + originator + " failed", e);
return (false);
}
}
});
} catch (Throwable e) {
connection.close();
log(null, "Incoming connection from " + originator + " failed", e);
}
return (true);
}
});
} catch (Throwable e) {
log(null, "Failed to register message listener", e);
}
}
Aggregations