public class I2PSnarkUtil extends Object implements I2PSocketManager.DisconnectListener
This class provides I2P network integration and serves as a context object for I2PSnark operations. It handles:
This class acts as a singleton-like context that allows multiple individual Snark instances to run while sharing common I2P configuration and resources. Note that while multiple Snarks can share one I2PSnarkUtil instance, multiple SnarkManagers are not supported due to static resources.
| Modifier and Type | Field and Description |
|---|---|
static boolean | DEFAULT_COLLAPSE_PANELS |
static boolean | DEFAULT_ENABLE_ADDCREATE |
static boolean | DEFAULT_ENABLE_LIGHTBOX |
static boolean | DEFAULT_SHOW_STATUSFILTER |
static boolean | DEFAULT_USE_DHT |
static boolean | DEFAULT_USE_OPENTRACKERS |
static boolean | DEFAULT_VARY_INBOUND_HOPS |
static boolean | DEFAULT_VARY_OUTBOUND_HOPS |
static String | EEPGET_USER_AGENTUser agent for eepget requests. |
static int | MAX_CONNECTIONS |
static String | PROP_CLIENT_IDClient identity spoofing setting: empty (default, identify as I2PSnark),
"random" to pick a random known client profile per destination per run,
or the name of a known client to always impersonate it. |
static String | PROP_CLIENT_IDSOptional comma-separated subset of client names to choose from when
PROP_CLIENT_ID is "random"; unknown names are ignored. |
static String | PROP_MAX_BWBandwidth throttle property. |
| Constructor and Description |
|---|
I2PSnarkUtil(I2PAppContext ctx) |
I2PSnarkUtil(I2PAppContext ctx,
String baseName,
I2PSocketManager.DisconnectListener discon)Create a utility instance bound to the given context. |
| Modifier and Type | Method and Description |
|---|---|
void | banPeer(Hash h,
long period)Ban the given destination hash for the given period. |
boolean | collapsePanels() |
boolean | commentsEnabled() |
boolean | connect()Connect to the router, if we aren't already |
(package private) I2PSocket | connect(PeerID peer)Connect to the given destination |
(package private) I2PSocket | connect(PeerID peer,
byte[] ih)Connect to the given destination through the torrent's own destination, or the shared
destination if multi-dest is disabled or the torrent has no destination yet. |
boolean | connected() |
void | disconnect()Destroy the destination itself |
boolean | enableAddCreate() |
boolean | enableLightbox() |
boolean | enableVaryInboundHops() |
boolean | enableVaryOutboundHops() |
File | get(String url)Fetch the given URL, returning the file it is stored in, or null on error. |
File | get(String url,
boolean rewrite)Fetch the given URL to a file. |
File | get(String url,
boolean rewrite,
int retries)Fetch the given URL to a file. |
File | get(String url,
boolean rewrite,
int retries,
byte[] ih)Fetch the given URL through the torrent's own destination. |
byte[] | get(String url,
boolean rewrite,
int retries,
int initialSize,
int maxSize)Fetch to memory |
byte[] | get(String url,
boolean rewrite,
int retries,
int initialSize,
int maxSize,
byte[] ih)Fetch to memory through the torrent's own destination. |
File | get(String url,
int retries)Fetch the given URL to a file. |
String | getAPIKey()The API key, or null if not set. |
String | getAPITarget()The API target base path. |
List<String> | getBackupTrackers()List of open tracker announce URLs to use as backups even if disabled |
long | getBanDiscardPeriod()Duration of the ban applied to peers with an excessive discard ratio. |
ClientID.Profile | getClientID(byte[] ih)The spoofed client identity for the torrent's destination, or null when
identifying as I2PSnark. |
String | getCommentsName()The comments file name. |
I2PAppContext | getContext()The context this utility is bound to. |
(package private) Destination | getDestination(String ip)Base64 Hash or Hash.i2p or name.i2p using naming service |
(package private) static Destination | getDestinationFromBase64(String ip)Resolves a base64-encoded destination string. |
DHT | getDHT()The main DHT instance, or null if disabled or not started. |
DHT | getDHTForTorrent(byte[] ih)The DHT instance for the torrent, on the torrent's own destination, or the
shared DHT instance when multi-dest is disabled or the DHT is disabled. |
String | getEepProxyHost()The EepProxy host configured. |
int | getEepProxyPort()The EepProxy port configured. |
boolean | getEepProxySet()Whether an EepProxy is configured. |
boolean | getFilesPublic()Whether files in the data directory are world-readable. |
String | getI2CPHost()The I2CP host configured. |
Map<String,String> | getI2CPOptions()A copy of the I2CP options. |
int | getI2CPPort()The I2CP port configured. |
int | getMaxConnections()The maximum number of connections per torrent. |
int | getMaxDest()The maximum number of per-torrent destinations in multi-dest mode, 0 for one
destination per torrent. |
int | getMaxFilesPerTorrent()Maximum number of files a torrent may contain. |
int | getMaxUpBW()The maximum upload bandwidth per torrent. |
int | getMaxUploaders()The maximum number of uploaders per torrent. |
boolean | getMultiDest()Whether each torrent runs on its own destination. |
(package private) Destination | getMyDestination()The shared destination. |
(package private) Destination | getMyDestination(byte[] ih)The destination for the torrent, or the shared destination when multi-dest
is disabled or the torrent has no destination. |
(package private) static String | getNickname(String name)The tunnel nickname for a torrent's destination, "I2PSnark - <name>" with the
name reduced to just the content title, truncated to MAX_NAME_LENGTH characters
plus an ellipsis. |
List<String> | getOpenTrackers()List of open tracker announce URLs to use as backups |
TorrentDest | getOrCreateTorrentDest(String key,
String name)The destination for a torrent, creating the destination and session on first use,
or assigning the torrent to a shared pool when the configured maximum number of
destinations is exceeded. |
String | getOurIPString()Full Base64 of the Destination. |
String | getOurIPString(byte[] ih)Full Base64 of the destination for the torrent. |
int | getPoolIndex(byte[] ih)Pool index that would be assigned to the torrent, -1 when pooling is off. |
(package private) static String | getPoolNickname(int poolNum)Tunnel nickname for a shared pool, "I2PSnark - Pool <n>", with the pool's member
names carried in the i2psnark.poolMembers session property for the pool tooltip. |
boolean | getPreallocateFiles()Whether new torrents preallocate their storage files. |
I2PServerSocket | getServerSocket()The shared server socket. |
I2PServerSocket | getServerSocket(byte[] ih)The server socket for the torrent's destination, or the shared server socket
when multi-dest is disabled or the torrent has no destination. |
boolean | getShouldPadFiles()Whether BEP 47 padding files are added to new torrents created by
I2PSnark, so each file except the last ends on a piece boundary. |
I2PSocketManager | getSocketManager()For FetchAndAdd |
I2PSocketManager | getSocketManager(byte[] ih)The socket manager for the torrent's destination, or the shared manager when
multi-dest is disabled or the torrent has no destination. |
long | getStartedTime()When did we connect to the network? For RPC |
int | getStartupDelayMax()The maximum startup delay in minutes. |
int | getStartupDelayMin()The minimum startup delay in minutes. |
String | getString(int n,
String s,
String p)A translated plural string for the given count. |
String | getString(String key)A translated string for the given key. |
String | getString(String s,
Object o)Translate a string with a parameter This is a lot more expensive than getString(s, ctx), so
use sparingly. |
String | getString(String s,
Object o,
Object o2){0} and {1} |
File | getTempDir()The temporary directory used for downloads in progress. |
String | getTempDirProp()The staging directory configured via i2psnark.tempDir in
i2psnark.config, or null if the staging feature is disabled. |
Collection<TorrentDest> | getTorrentDests()All active transient destinations, for the tunnel quantity ramp in the IdleChecker. |
int[] | getTunnelCounts()The inbound and outbound tunnel counts allocated to our sessions, summed
over the primary session and all per-torrent destinations, so both grow
with the number of sessions in multi-dest mode. |
UDPTrackerClient | getUDPTrackerClient()The shared UDP tracker client, or null if disabled or not started. |
UDPTrackerClient | getUDPTrackerClient(byte[] ih)The UDP tracker client for the torrent's destination, or the shared client
when the torrent has no destination. |
String | getUserAgent(byte[] ih)The HTTP User-Agent to send for the torrent's requests: the spoofed
profile's UA when spoofing is on, else the default I2PSnark UA. |
boolean | getVaryInboundHops()Whether hops are randomly varied for inbound torrent tunnels. |
boolean | getVaryOutboundHops()Whether hops are randomly varied for outbound torrent tunnels. |
boolean | hasAPIKey()Whether both an API target and key are configured. |
boolean | isBanDiscardRatio()Whether to disconnect peers that cancel most of what they request. |
boolean | isBanned(Hash h)Is the given destination hash banned? |
boolean | isConnecting()Whether an I2CP connection attempt is in progress. |
boolean | isKnownOpenTracker(String url)Is this announce URL probably for an open tracker? |
(package private) static void | loadProps(Properties props,
File f)Same as DataHelper.loadProps() but allows '#' in values, so we can have filenames with '#' in
them in torrent config files. |
String | lookup(String name)Look up a destination by name. |
static int | parseInt(String s,
int dflt)Parse an int, returning the default on null or parse failure. |
static long | parseLong(String s,
long dflt)Parse a long, returning the default on null or parse failure. |
boolean | ratingsEnabled() |
void | removeAllTorrentDests()Destroy all transient destinations. |
void | removeTorrentDest(String key)Remove and destroy the destination for a torrent, destroying a pooled destination
only when its last torrent is removed. |
(package private) String | rewriteAnnounce(String origAnnounce)Given http://KEY.i2p/foo/announce turn it into http://i2p/KEY/foo/announce Given
http://tracker.blah.i2p/foo/announce leave it alone |
void | sessionDisconnected()Handle the I2CP session being disconnected, dropping the manager reference. |
void | setAPI(String target,
String key)Configure the API target and key. |
void | setBanDiscardPeriod(long period)Set the duration of the ban applied to peers with an excessive discard ratio. |
void | setBanDiscardRatio(boolean ban)Whether to disconnect peers that cancel most of what they request. |
void | setClientId(String setting)Client identity spoofing. |
void | setClientIdCandidates(List<ClientID.Profile> candidates)Restrict the candidate set for random client selection. |
void | setCollapsePanels(boolean yes) |
void | setCommentsEnabled(boolean yes) |
void | setCommentsName(String name) |
void | setEnableAddCreate(boolean yes) |
void | setEnableLightbox(boolean yes) |
void | setEnableVaryInboundHops(boolean yes) |
void | setEnableVaryOutboundHops(boolean yes) |
void | setFilesPublic(boolean yes)Whether files in the data directory are world-readable. |
void | setI2CPConfig(String i2cpHost,
int i2cpPort,
Map opts)Configure the I2CP connection parameters. |
void | setMaxConnections(int limit) |
void | setMaxDest(int maxDest)Configure the maximum number of per-torrent destinations, 0-1000; zero is unlimited. |
void | setMaxFilesPerTorrent(int max)Maximum number of files a torrent may contain. |
void | setMaxUpBW(int limit)This updates ALL the session options (not just the bw) and tells the router |
void | setMaxUploaders(int limit) |
void | setMultiDest(boolean multiDest) |
void | setOpenTrackers(List<String> ot)Configure the open tracker announce URLs to use as backups. |
void | setPreallocateFiles(boolean yes)Whether new torrents preallocate their storage files. |
void | setRatingsEnabled(boolean yes) |
void | setShouldPadFiles(boolean yes)Sets whether BEP 47 padding files are added to new torrents. |
void | setShowStatusFilter(boolean yes) |
void | setStartupDelayMax(int minutes) |
void | setStartupDelayMin(int minutes) |
void | setTempDirProp(String tempDir)Sets the staging directory (or null to disable the feature). |
void | setUDPEnabled(boolean yes) |
void | setUseDHT(boolean yes) |
void | setUseOpenTrackers(boolean yes) |
void | setVaryInboundHops(boolean yes)Whether hops are randomly varied for inbound torrent tunnels. |
void | setVaryOutboundHops(boolean yes) |
boolean | shouldUseDHT()Whether the DHT is used. |
boolean | shouldUseOpenTrackers()Whether open trackers are used as backups. |
boolean | showStatusFilter() |
(package private) static void | storeProps(Properties props,
File file)Same as DataHelper.loadProps() but allows '#' in values, so we can have filenames with '#' in
them in torrent config files. |
static String | toHex(byte[] b)Like DataHelper.toHexString but ensures no loss of leading zero bytes |
static String | trackerB32ToHostname(String url)Replace known base32 and base64 tracker hosts with their symbolic names, and strip the
leading scheme, for display purposes. |
boolean | udpEnabled() |
boolean | utCommentsEnabled() |
public static final boolean DEFAULT_COLLAPSE_PANELS
public static final boolean DEFAULT_ENABLE_ADDCREATE
public static final boolean DEFAULT_ENABLE_LIGHTBOX
public static final boolean DEFAULT_SHOW_STATUSFILTER
public static final boolean DEFAULT_USE_DHT
public static final boolean DEFAULT_USE_OPENTRACKERS
public static final boolean DEFAULT_VARY_INBOUND_HOPS
public static final boolean DEFAULT_VARY_OUTBOUND_HOPS
public static final String EEPGET_USER_AGENT
public static final int MAX_CONNECTIONS
public static final String PROP_CLIENT_ID
public static final String PROP_CLIENT_IDS
PROP_CLIENT_ID is "random"; unknown names are ignored.public static final String PROP_MAX_BW
public I2PSnarkUtil(I2PAppContext ctx)
public I2PSnarkUtil(I2PAppContext ctx, String baseName, I2PSocketManager.DisconnectListener discon)
baseName - generally "i2psnark"public void banPeer(Hash h, long period)
h - the hashperiod - the ban period in millisecondspublic boolean collapsePanels()
public boolean commentsEnabled()
public boolean connect()
I2PSocket connect(PeerID peer) throws IOException
IOExceptionI2PSocket connect(PeerID peer, byte[] ih) throws IOException
ih - the torrent's info hashIOExceptionpublic boolean connected()
public void disconnect()
public boolean enableAddCreate()
public boolean enableLightbox()
public boolean enableVaryInboundHops()
public boolean enableVaryOutboundHops()
public File get(String url)
public File get(String url, boolean rewrite)
rewrite - if true, convert http://KEY.i2p/foo/announce to http://i2p/KEY/foo/announcepublic File get(String url, boolean rewrite, int retries)
rewrite - if true, convert http://KEY.i2p/foo/announce to http://i2p/KEY/foo/announceretries - if > 0, set timeout to a few secondspublic File get(String url, boolean rewrite, int retries, byte[] ih)
ih - the torrent's info hashretries - if > 0, set timeout to a few secondspublic byte[] get(String url, boolean rewrite, int retries, int initialSize, int maxSize)
retries - if < 0, set timeout to a few secondsinitialSize - buffer sizemaxSize - fails if greaterpublic byte[] get(String url, boolean rewrite, int retries, int initialSize, int maxSize, byte[] ih)
retries - if < 0, set timeout to a few secondsinitialSize - buffer sizemaxSize - fails if greaterih - the torrent's info hashpublic File get(String url, int retries)
retries - if > 0, set timeout to a few secondspublic String getAPIKey()
public String getAPITarget()
public List<String> getBackupTrackers()
public long getBanDiscardPeriod()
public ClientID.Profile getClientID(byte[] ih)
ih - the torrent's info hash, or nullpublic String getCommentsName()
public I2PAppContext getContext()
Destination getDestination(String ip)
static Destination getDestinationFromBase64(String ip)
ip - destination string, with or without .i2p suffixpublic DHT getDHT()
public DHT getDHTForTorrent(byte[] ih)
ih - the torrent's info hashpublic String getEepProxyHost()
public int getEepProxyPort()
public boolean getEepProxySet()
public boolean getFilesPublic()
public String getI2CPHost()
public int getI2CPPort()
public int getMaxConnections()
public int getMaxDest()
public int getMaxFilesPerTorrent()
public int getMaxUpBW()
public int getMaxUploaders()
public boolean getMultiDest()
Destination getMyDestination()
Destination getMyDestination(byte[] ih)
ih - the torrent's info hashstatic String getNickname(String name)
MAX_NAME_LENGTH characters
plus an ellipsis. Parentheses and brackets with anything inside them (year, resolution,
release group), a metadata tail after the first dash, file extensions and common quality
tokens (resolution, codec, source) are removed, so the nickname is shorter and the title
stands out on the console's tunnels page. Local only; not shared with trackers or the DHT.name - the torrent namepublic List<String> getOpenTrackers()
public TorrentDest getOrCreateTorrentDest(String key, String name)
key - Base64 encoding of the torrent's info hashname - torrent name, used in the tunnel nicknamepublic String getOurIPString()
public String getOurIPString(byte[] ih)
public int getPoolIndex(byte[] ih)
ih - the torrent's info hash, or nullstatic String getPoolNickname(int poolNum)
poolNum - the sequential pool number, assigned when the pool destination was createdpublic boolean getPreallocateFiles()
public I2PServerSocket getServerSocket()
public I2PServerSocket getServerSocket(byte[] ih)
ih - the torrent's info hashpublic boolean getShouldPadFiles()
public I2PSocketManager getSocketManager()
public I2PSocketManager getSocketManager(byte[] ih)
ih - the torrent's info hashpublic long getStartedTime()
public int getStartupDelayMax()
public int getStartupDelayMin()
public String getString(int n, String s, String p)
n - the count, selects singular or plural forms - the singular formp - the plural formpublic String getString(String key)
public String getString(String s, Object o)
s - string to be translated containing {0} The {0} will be replaced by the parameter.
Single quotes must be doubled, i.e. ' -> '' in the string.o - parameter, not translated. To translate parameter also, use _t("foo {0} bar",
_t("baz")) Do not double the single quotes in the parameter. Use autoboxing to call with
ints, longs, floats, etc.public File getTempDir()
public String getTempDirProp()
i2psnark.tempDir in
i2psnark.config, or null if the staging feature is disabled. Set by
SnarkManager at startup and from the I2PSnark configuration page;
consumed by Storage.public Collection<TorrentDest> getTorrentDests()
public int[] getTunnelCounts()
public UDPTrackerClient getUDPTrackerClient()
public UDPTrackerClient getUDPTrackerClient(byte[] ih)
ih - the torrent's info hashpublic String getUserAgent(byte[] ih)
ih - the torrent's info hash, or nullpublic boolean getVaryInboundHops()
public boolean getVaryOutboundHops()
public boolean hasAPIKey()
public boolean isBanDiscardRatio()
public boolean isBanned(Hash h)
h - the hashpublic boolean isConnecting()
public boolean isKnownOpenTracker(String url)
static void loadProps(Properties props, File f) throws IOException
IOExceptionpublic String lookup(String name)
name - hostname or base64 destinationpublic static int parseInt(String s, int dflt)
s - may be nulldflt - returned on failurepublic static long parseLong(String s, long dflt)
s - may be nulldflt - returned on failurepublic boolean ratingsEnabled()
public void removeAllTorrentDests()
public void removeTorrentDest(String key)
String rewriteAnnounce(String origAnnounce)
public void sessionDisconnected()
sessionDisconnected in interface I2PSocketManager.DisconnectListenerpublic void setAPI(String target, String key)
target - the API target base pathkey - the API keypublic void setBanDiscardPeriod(long period)
period - the ban period in millisecondspublic void setBanDiscardRatio(boolean ban)
ban - true to enable the discard-ratio auto-banpublic void setClientId(String setting)
ClientID.getByName(java.lang.String).setting - the i2psnark.clientId valuepublic void setClientIdCandidates(List<ClientID.Profile> candidates)
candidates - the parsed candidate list, may be emptypublic void setCollapsePanels(boolean yes)
public void setCommentsEnabled(boolean yes)
public void setCommentsName(String name)
public void setEnableAddCreate(boolean yes)
public void setEnableLightbox(boolean yes)
public void setEnableVaryInboundHops(boolean yes)
public void setEnableVaryOutboundHops(boolean yes)
public void setFilesPublic(boolean yes)
public void setI2CPConfig(String i2cpHost, int i2cpPort, Map opts)
i2cpHost - may be null for no changei2cpPort - may be 0 for no changeopts - may be null for no changepublic void setMaxConnections(int limit)
public void setMaxDest(int maxDest)
maxDest - the maximum number of destinations, 0-1000; zero is unlimitedpublic void setMaxFilesPerTorrent(int max)
public void setMaxUpBW(int limit)
limit - KBpspublic void setMaxUploaders(int limit)
public void setMultiDest(boolean multiDest)
public void setOpenTrackers(List<String> ot)
ot - non-null list of announce URLspublic void setPreallocateFiles(boolean yes)
public void setRatingsEnabled(boolean yes)
public void setShouldPadFiles(boolean yes)
public void setShowStatusFilter(boolean yes)
public void setStartupDelayMax(int minutes)
public void setStartupDelayMin(int minutes)
public void setTempDirProp(String tempDir)
public void setUDPEnabled(boolean yes)
public void setUseDHT(boolean yes)
public void setUseOpenTrackers(boolean yes)
public void setVaryInboundHops(boolean yes)
public void setVaryOutboundHops(boolean yes)
public boolean shouldUseDHT()
public boolean shouldUseOpenTrackers()
public boolean showStatusFilter()
static void storeProps(Properties props, File file) throws IOException
IOExceptionpublic static String toHex(byte[] b)
public static String trackerB32ToHostname(String url)
url - may be nullpublic boolean udpEnabled()
public boolean utCommentsEnabled()