org.klomp.snark

## Class I2PSnarkUtil

- java.lang.Object
- - org.klomp.snark.I2PSnarkUtil

- All Implemented Interfaces:
    I2PSocketManager.DisconnectListener

      public class I2PSnarkUtil
      extends Object
      implements I2PSocketManager.DisconnectListener

  I2P-specific utilities and context management for I2PSnark.

  This class provides I2P network integration and serves as a context object for I2PSnark operations. It handles:

  - I2P socket management and connection configuration
  - Proxy settings for external tracker communication
  - Peer banlist management
  - Bandwidth and connection limits
  - DHT integration and UDP tracker support
  - File and directory management
  - UI preferences and display options
  - Comments and ratings system configuration

  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.

- - ### Field Summary

    Fields
    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 int` | `DEFAULT_STARTUP_DELAY`
    `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_AGENT`
    `static int` | `MAX_CONNECTIONS`
    `static String` | `PROP_MAX_BW`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `I2PSnarkUtil(I2PAppContext ctx)`
    `I2PSnarkUtil(I2PAppContext ctx, String baseName, I2PSocketManager.DisconnectListener discon)`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `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
    `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)`
    `File` | `get(String url, boolean rewrite, int retries)`
    `byte[]` | `get(String url, boolean rewrite, int retries, int initialSize, int maxSize)` Fetch to memory
    `File` | `get(String url, int retries)`
    `String` | `getAPIKey()`
    `String` | `getAPITarget()`
    `List<String>` | `getBackupTrackers()` List of open tracker announce URLs to use as backups even if disabled
    `String` | `getCommentsName()`
    `I2PAppContext` | `getContext()`
    `(package private) Destination` | `getDestination(String ip)` Base64 Hash or Hash.i2p or name.i2p using naming service
    `(package private) static Destination` | `getDestinationFromBase64(String ip)` Base64 only - static (no naming service)
    `DHT` | `getDHT()`
    `String` | `getEepProxyHost()`
    `int` | `getEepProxyPort()`
    `boolean` | `getEepProxySet()`
    `boolean` | `getFilesPublic()`
    `String` | `getI2CPHost()`
    `Map<String,String>` | `getI2CPOptions()`
    `int` | `getI2CPPort()`
    `int` | `getMaxConnections()`
    `int` | `getMaxFilesPerTorrent()`
    `int` | `getMaxUpBW()`
    `int` | `getMaxUploaders()`
    `(package private) Destination` | `getMyDestination()`
    `List<String>` | `getOpenTrackers()` List of open tracker announce URLs to use as backups
    `String` | `getOurIPString()` Full Base64 of Destination
    `boolean` | `getPreallocateFiles()`
    `I2PServerSocket` | `getServerSocket()`
    `I2PSocketManager` | `getSocketManager()` For FetchAndAdd
    `long` | `getStartedTime()` When did we connect to the network? For RPC
    `int` | `getStartupDelay()`
    `String` | `getString(int n, String s, String p)` ngettext @since 0.7.14
    `String` | `getString(String key)` lang in routerconsole.lang property, else current locale
    `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()`
    `UDPTrackerClient` | `getUDPTrackerClient()`
    `boolean` | `getVaryInboundHops()`
    `boolean` | `getVaryOutboundHops()`
    `boolean` | `hasAPIKey()`
    `boolean` | `isConnecting()`
    `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)`
    `boolean` | `ratingsEnabled()`
    `(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()` DisconnectListener interface
    `void` | `setAPI(String target, String key)`
    `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)`
    `void` | `setI2CPConfig(String i2cpHost, int i2cpPort, Map opts)`
    `void` | `setMaxConnections(int limit)`
    `void` | `setMaxFilesPerTorrent(int max)`
    `void` | `setMaxUpBW(int limit)` This updates ALL the session options (not just the bw) and tells the router
    `void` | `setMaxUploaders(int limit)`
    `void` | `setOpenTrackers(List<String> ot)`
    `void` | `setPreallocateFiles(boolean yes)`
    `void` | `setRatingsEnabled(boolean yes)`
    `void` | `setShowStatusFilter(boolean yes)`
    `void` | `setStartupDelay(int minutes)`
    `void` | `setUDPEnabled(boolean yes)`
    `void` | `setUseDHT(boolean yes)`
    `void` | `setUseOpenTrackers(boolean yes)`
    `void` | `setVaryInboundHops(boolean yes)`
    `void` | `setVaryOutboundHops(boolean yes)`
    `boolean` | `shouldUseDHT()`
    `boolean` | `shouldUseOpenTrackers()`
    `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
    `boolean` | `udpEnabled()`
    `boolean` | `utCommentsEnabled()`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_COLLAPSE_PANELS

          public static final boolean DEFAULT_COLLAPSE_PANELS

      See Also:
        Constant Field Values

    - #### DEFAULT_ENABLE_ADDCREATE

          public static final boolean DEFAULT_ENABLE_ADDCREATE

      See Also:
        Constant Field Values

    - #### DEFAULT_ENABLE_LIGHTBOX

          public static final boolean DEFAULT_ENABLE_LIGHTBOX

      See Also:
        Constant Field Values

    - #### DEFAULT_SHOW_STATUSFILTER

          public static final boolean DEFAULT_SHOW_STATUSFILTER

      See Also:
        Constant Field Values

    - #### DEFAULT_STARTUP_DELAY

          public static final int DEFAULT_STARTUP_DELAY

      See Also:
        Constant Field Values

    - #### DEFAULT_USE_DHT

          public static final boolean DEFAULT_USE_DHT

      See Also:
        Constant Field Values

    - #### DEFAULT_USE_OPENTRACKERS

          public static final boolean DEFAULT_USE_OPENTRACKERS

      See Also:
        Constant Field Values

    - #### DEFAULT_VARY_INBOUND_HOPS

          public static final boolean DEFAULT_VARY_INBOUND_HOPS

      See Also:
        Constant Field Values

    - #### DEFAULT_VARY_OUTBOUND_HOPS

          public static final boolean DEFAULT_VARY_OUTBOUND_HOPS

      See Also:
        Constant Field Values

    - #### EEPGET_USER_AGENT

          public static final String EEPGET_USER_AGENT

      See Also:
        Constant Field Values

    - #### MAX_CONNECTIONS

          public static final int MAX_CONNECTIONS

      See Also:
        Constant Field Values

    - #### PROP_MAX_BW

          public static final String PROP_MAX_BW

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### I2PSnarkUtil

          public I2PSnarkUtil(I2PAppContext ctx)

    - #### I2PSnarkUtil

          public I2PSnarkUtil(I2PAppContext ctx,
                              String baseName,
                              I2PSocketManager.DisconnectListener discon)

      Parameters:
        `baseName` - generally "i2psnark"

  - ### Method Detail

    - #### collapsePanels

          public boolean collapsePanels()

    - #### commentsEnabled

          public boolean commentsEnabled()

    - #### connect

          public boolean connect()

      Connect to the router, if we aren't already

    - #### connect

          I2PSocket connect(PeerID peer)
                     throws IOException

      Connect to the given destination
      Throws:
        `IOException`

    - #### connected

          public boolean connected()

    - #### disconnect

          public void disconnect()

      Destroy the destination itself

    - #### enableAddCreate

          public boolean enableAddCreate()

    - #### enableLightbox

          public boolean enableLightbox()

    - #### enableVaryInboundHops

          public boolean enableVaryInboundHops()

    - #### enableVaryOutboundHops

          public boolean enableVaryOutboundHops()

    - #### get

          public File get(String url)

      Fetch the given URL, returning the file it is stored in, or null on error. No retries.

    - #### get

          public File get(String url,
                          boolean rewrite)

      Parameters:
        `rewrite` - if true, convert http://KEY.i2p/foo/announce to http://i2p/KEY/foo/announce

    - #### get

          public File get(String url,
                          boolean rewrite,
                          int retries)

      Parameters:
        `retries` - if > 0, set timeout to a few seconds

    - #### get

          public byte[] get(String url,
                            boolean rewrite,
                            int retries,
                            int initialSize,
                            int maxSize)

      Fetch to memory
      Parameters:
        `retries` - if < 0, set timeout to a few seconds
        `initialSize` - buffer size
        `maxSize` - fails if greater
      Returns:
        null on error

    - #### get

          public File get(String url,
                          int retries)

      Parameters:
        `retries` - if > 0, set timeout to a few seconds

    - #### getAPIKey

          public String getAPIKey()

    - #### getAPITarget

          public String getAPITarget()

    - #### getBackupTrackers

          public List<String> getBackupTrackers()

      List of open tracker announce URLs to use as backups even if disabled
      Returns:
        non-null

    - #### getCommentsName

          public String getCommentsName()

      Returns:
        non-null, "" if none

    - #### getContext

          public I2PAppContext getContext()

    - #### getDestination

          Destination getDestination(String ip)

      Base64 Hash or Hash.i2p or name.i2p using naming service

    - #### getDestinationFromBase64

          static Destination getDestinationFromBase64(String ip)

      Base64 only - static (no naming service)

    - #### getDHT

          public DHT getDHT()

      Returns:
        null if disabled or not started

    - #### getEepProxyHost

          public String getEepProxyHost()

    - #### getEepProxyPort

          public int getEepProxyPort()

    - #### getEepProxySet

          public boolean getEepProxySet()

    - #### getFilesPublic

          public boolean getFilesPublic()

    - #### getI2CPHost

          public String getI2CPHost()

    - #### getI2CPOptions

          public Map<String,String> getI2CPOptions()

      Returns:
        a copy

    - #### getI2CPPort

          public int getI2CPPort()

    - #### getMaxConnections

          public int getMaxConnections()

    - #### getMaxFilesPerTorrent

          public int getMaxFilesPerTorrent()

    - #### getMaxUpBW

          public int getMaxUpBW()

      Returns:
        KBps

    - #### getMaxUploaders

          public int getMaxUploaders()

    - #### getMyDestination

          Destination getMyDestination()

      Returns:
        dest or null

    - #### getOpenTrackers

          public List<String> getOpenTrackers()

      List of open tracker announce URLs to use as backups
      Returns:
        non-null, possibly unmodifiable, empty if disabled

    - #### getOurIPString

          public String getOurIPString()

      Full Base64 of Destination

    - #### getPreallocateFiles

          public boolean getPreallocateFiles()

    - #### getServerSocket

          public I2PServerSocket getServerSocket()

    - #### getSocketManager

          public I2PSocketManager getSocketManager()

      For FetchAndAdd
      Returns:
        null if not connected

    - #### getStartedTime

          public long getStartedTime()

      When did we connect to the network? For RPC
      Returns:
        0 if not connected

    - #### getStartupDelay

          public int getStartupDelay()

    - #### getString

          public String getString(int n,
                                  String s,
                                  String p)

      ngettext @since 0.7.14

    - #### getString

          public String getString(String key)

      lang in routerconsole.lang property, else current locale

    - #### getString

          public 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.
      Parameters:
        `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.

    - #### getString

          public String getString(String s,
                                  Object o,
                                  Object o2)

      {0} and {1}

    - #### getTempDir

          public File getTempDir()

    - #### getUDPTrackerClient

          public UDPTrackerClient getUDPTrackerClient()

      Returns:
        null if disabled or not started

    - #### getVaryInboundHops

          public boolean getVaryInboundHops()

    - #### getVaryOutboundHops

          public boolean getVaryOutboundHops()

    - #### hasAPIKey

          public boolean hasAPIKey()

    - #### isConnecting

          public boolean isConnecting()

    - #### isKnownOpenTracker

          public boolean isKnownOpenTracker(String url)

      Is this announce URL probably for an open tracker?

    - #### loadProps

          static void loadProps(Properties props,
                                File f)
                         throws IOException

      Same as DataHelper.loadProps() but allows '#' in values, so we can have filenames with '#' in them in torrent config files. '#' must be in column 1 for a comment.
      Throws:
        `IOException`

    - #### lookup

          public String lookup(String name)

    - #### ratingsEnabled

          public boolean ratingsEnabled()

    - #### rewriteAnnounce

          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

    - #### sessionDisconnected

          public void sessionDisconnected()

      DisconnectListener interface
      Specified by:
        `sessionDisconnected` in interface `I2PSocketManager.DisconnectListener`

    - #### setAPI

          public void setAPI(String target,
                             String key)

    - #### setCollapsePanels

          public void setCollapsePanels(boolean yes)

    - #### setCommentsEnabled

          public void setCommentsEnabled(boolean yes)

    - #### setCommentsName

          public void setCommentsName(String name)

    - #### setEnableAddCreate

          public void setEnableAddCreate(boolean yes)

    - #### setEnableLightbox

          public void setEnableLightbox(boolean yes)

    - #### setEnableVaryInboundHops

          public void setEnableVaryInboundHops(boolean yes)

    - #### setEnableVaryOutboundHops

          public void setEnableVaryOutboundHops(boolean yes)

    - #### setFilesPublic

          public void setFilesPublic(boolean yes)

    - #### setI2CPConfig

          public void setI2CPConfig(String i2cpHost,
                                    int i2cpPort,
                                    Map opts)

      Parameters:
        `i2cpHost` - may be null for no change
        `i2cpPort` - may be 0 for no change
        `opts` - may be null for no change

    - #### setMaxConnections

          public void setMaxConnections(int limit)

    - #### setMaxFilesPerTorrent

          public void setMaxFilesPerTorrent(int max)

    - #### setMaxUpBW

          public void setMaxUpBW(int limit)

      This updates ALL the session options (not just the bw) and tells the router
      Parameters:
        `limit` - KBps

    - #### setMaxUploaders

          public void setMaxUploaders(int limit)

    - #### setOpenTrackers

          public void setOpenTrackers(List<String> ot)

      Parameters:
        `ot` - non-null list of announce URLs

    - #### setPreallocateFiles

          public void setPreallocateFiles(boolean yes)

    - #### setRatingsEnabled

          public void setRatingsEnabled(boolean yes)

    - #### setShowStatusFilter

          public void setShowStatusFilter(boolean yes)

    - #### setStartupDelay

          public void setStartupDelay(int minutes)

    - #### setUDPEnabled

          public void setUDPEnabled(boolean yes)

    - #### setUseDHT

          public void setUseDHT(boolean yes)

    - #### setUseOpenTrackers

          public void setUseOpenTrackers(boolean yes)

    - #### setVaryInboundHops

          public void setVaryInboundHops(boolean yes)

    - #### setVaryOutboundHops

          public void setVaryOutboundHops(boolean yes)

    - #### shouldUseDHT

          public boolean shouldUseDHT()

    - #### shouldUseOpenTrackers

          public boolean shouldUseOpenTrackers()

    - #### showStatusFilter

          public boolean showStatusFilter()

    - #### storeProps

          static void storeProps(Properties props,
                                 File file)
                          throws IOException

      Same as DataHelper.loadProps() but allows '#' in values, so we can have filenames with '#' in them in torrent config files. '#' must be in column 1 for a comment.
      Throws:
        `IOException`

    - #### toHex

          public static String toHex(byte[] b)

      Like DataHelper.toHexString but ensures no loss of leading zero bytes

    - #### udpEnabled

          public boolean udpEnabled()

    - #### utCommentsEnabled

          public boolean utCommentsEnabled()