SAM: Use session for b32 lookups if available

so router doesn't use expl. tunnels and put LS in main db
This commit is contained in:
zzz
2026-03-27 16:39:02 -04:00
parent 2e0d9bd726
commit 8ce7e47624
5 changed files with 71 additions and 8 deletions

View File

@@ -60,4 +60,13 @@ interface SAMMessageSess extends Closeable {
public int getListenProtocol();
public int getListenPort();
/**
* Lookup a destination through the I2CP session.
* Blocking.
*
* @return the Destination or null
* @since 0.9.69
*/
public Destination lookupDest(String name) throws I2PSessionException ;
}

View File

@@ -210,6 +210,29 @@ abstract class SAMMessageSession implements SAMMessageSess {
return session.sendMessage(d, data, 0, data.length, proto, fromPort, toPort, opts);
}
/**
* Lookup a destination through the I2CP session.
* Blocking.
*
* @return the Destination or null
* @since 0.9.69
*/
public Destination lookupDest(String name) throws I2PSessionException {
return lookupDest(session, name);
}
/**
* Lookup a destination through the I2CP session.
* Blocking.
*
* @return the Destination or null
* @since 0.9.69
*/
static Destination lookupDest(I2PSession session, String name) throws I2PSessionException {
// session will convert b32 to hash, no need to do it here
return session.lookupDest(name, 10*1000);
}
/**
* Close a SAM message-based session.
*/

View File

@@ -347,6 +347,17 @@ class SAMStreamSession implements SAMMessageSess {
return true;
}
/**
* Lookup a destination through the I2CP session.
* Blocking.
*
* @return the Destination or null
* @since 0.9.69
*/
public Destination lookupDest(String name) throws I2PSessionException {
return SAMMessageSession.lookupDest(socketMgr.getSession(), name);
}
/**
* Close a SAM STREAM session.
*

View File

@@ -170,12 +170,12 @@ class SAMUtils {
if (d == null) {
String msg;
if (s.length() >= 516)
msg = "Bad Base64 dest: ";
msg = "Bad Base64 dest";
else if (s.length() >= 60 && s.endsWith(".b32.i2p"))
msg = "Lease set not found: ";
msg = "Lease set not found";
else
msg = "Host name not found: ";
throw new DataFormatException(msg + s);
msg = "Host name not found";
throw new DataFormatException(msg);
}
return d;
}

View File

@@ -19,6 +19,7 @@ import java.net.Socket;
import java.net.SocketTimeoutException;
import java.nio.channels.SocketChannel;
import java.nio.ByteBuffer;
import java.util.Locale;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicLong;
@@ -389,10 +390,29 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece
return writeString("NAMING REPLY RESULT=KEY_NOT_FOUND NAME=\"\" MESSAGE=\"Name=ME requires established session\"\n");
}
} else {
try {
dest = SAMUtils.getDest(name);
} catch (DataFormatException e) {
}
try {
if (name.length() >= 516 || !name.toLowerCase(Locale.US).endsWith(".b32.i2p")) {
// out of session
dest = SAMUtils.getDest(name);
} else if (streamSession != null) {
// lookup in-session so the router will use the client tunnels to get the LS
// and put the LS in the client's netdb
dest = streamSession.lookupDest(name);
} else if (datagramSession != null) {
dest = datagramSession.lookupDest(name);
} else if (rawSession != null) {
dest = rawSession.lookupDest(name);
} else {
// out of session
// leaseset will end up in the main netdb
// and will have to be looked up again by the router if a message is sent to it.
dest = SAMUtils.getDest(name);
}
} catch (I2PSessionException e) {
return writeString("NAMING REPLY RESULT=KEY_NOT_FOUND NAME=" + name, e.getMessage());
} catch (DataFormatException e) {
return writeString("NAMING REPLY RESULT=KEY_NOT_FOUND NAME=" + name, e.getMessage());
}
}
if (dest == null) {