I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 99d720c6 authored by str4d's avatar str4d
Browse files

Implemented tunnel creation backend

The methods in TunnelConfig and TunnelUtil are taken almost verbatim from
IndexBean (modified to be static in TunnelUtil). The tunnel creation wizard
works now, and the new tunnel will start if auto start is enabled.
parent c46ba4f2
No related branches found
No related tags found
No related merge requests found
......@@ -89,6 +89,8 @@
<string name="i2ptunnel_not_initialized">Tunnels are not initialized yet, please reload in two minutes.</string>
<string name="i2ptunnel_new_tunnel">New Tunnel</string>
<string name="i2ptunnel_msg_config_saved">Configuration changes saved</string>
<string name="i2ptunnel_msg_config_save_failed">Failed to save configuration</string>
<string name="i2ptunnel_wizard_k_client_server">Client or Server</string>
<string name="i2ptunnel_wizard_v_client">Client tunnel</string>
......
......@@ -6,6 +6,7 @@ import net.i2p.android.i2ptunnel.activity.TunnelWizardActivity;
import net.i2p.android.i2ptunnel.adapter.TunnelEntryAdapter;
import net.i2p.android.i2ptunnel.loader.TunnelEntry;
import net.i2p.android.i2ptunnel.loader.TunnelEntryLoader;
import net.i2p.android.i2ptunnel.util.TunnelConfig;
import net.i2p.android.router.R;
import net.i2p.i2ptunnel.TunnelControllerGroup;
import android.app.Activity;
......@@ -157,6 +158,9 @@ public class TunnelListFragment extends ListFragment
if (requestCode == TUNNEL_WIZARD_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
Bundle tunnelData = data.getExtras().getBundle(TUNNEL_WIZARD_DATA);
TunnelConfig cfg = TunnelConfig.createFromWizard(getActivity(), mGroup, tunnelData);
TunnelEntry tunnel = TunnelEntry.createNewTunnel(getActivity(), mGroup, cfg);
mAdapter.add(tunnel);
}
}
}
......
package net.i2p.android.i2ptunnel.loader;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.Toast;
import net.i2p.android.i2ptunnel.util.TunnelConfig;
import net.i2p.android.i2ptunnel.util.TunnelUtil;
import net.i2p.android.router.R;
import net.i2p.data.Destination;
import net.i2p.data.PrivateKeyFile;
import net.i2p.i2ptunnel.TunnelController;
import net.i2p.i2ptunnel.TunnelControllerGroup;
public class TunnelEntry {
public static final int RUNNING = 1;
......@@ -17,6 +23,20 @@ public class TunnelEntry {
private final TunnelController mController;
private final int mId;
public static TunnelEntry createNewTunnel(
Context ctx,
TunnelControllerGroup tcg,
TunnelConfig cfg) {
int tunnelId = tcg.getControllers().size();
List<String> msgs = TunnelUtil.saveTunnel(
ctx, tcg, -1, cfg.getConfig());
// TODO: Do something else with the other messages.
Toast.makeText(ctx.getApplicationContext(),
msgs.get(0), Toast.LENGTH_LONG).show();
TunnelController cur = TunnelUtil.getController(tcg, tunnelId);
return new TunnelEntry(ctx, cur, tunnelId);
}
public TunnelEntry(Context context, TunnelController controller, int id) {
mContext = context;
mController = controller;
......@@ -46,44 +66,7 @@ public class TunnelEntry {
}
public String getType() {
if ("client".equals(mController.getType()))
return mContext.getResources()
.getString(R.string.i2ptunnel_type_client);
else if ("httpclient".equals(mController.getType()))
return mContext.getResources()
.getString(R.string.i2ptunnel_type_httpclient);
else if ("ircclient".equals(mController.getType()))
return mContext.getResources()
.getString(R.string.i2ptunnel_type_ircclient);
else if ("server".equals(mController.getType()))
return mContext.getResources()
.getString(R.string.i2ptunnel_type_server);
else if ("httpserver".equals(mController.getType()))
return mContext.getResources()
.getString(R.string.i2ptunnel_type_httpserver);
else if ("sockstunnel".equals(mController.getType()))
return mContext.getResources()
.getString(R.string.i2ptunnel_type_sockstunnel);
else if ("socksirctunnel".equals(mController.getType()))
return mContext.getResources()
.getString(R.string.i2ptunnel_type_socksirctunnel);
else if ("connectclient".equals(mController.getType()))
return mContext.getResources()
.getString(R.string.i2ptunnel_type_connectclient);
else if ("ircserver".equals(mController.getType()))
return mContext.getResources()
.getString(R.string.i2ptunnel_type_ircserver);
else if ("streamrclient".equals(mController.getType()))
return mContext.getResources()
.getString(R.string.i2ptunnel_type_streamrclient);
else if ("streamrserver".equals(mController.getType()))
return mContext.getResources()
.getString(R.string.i2ptunnel_type_streamrserver);
else if ("httpbidirserver".equals(mController.getType()))
return mContext.getResources()
.getString(R.string.i2ptunnel_type_httpbidirserver);
else
return mController.getType();
return TunnelUtil.getTypeName(mController.getType(), mContext);
}
public String getDescription() {
......@@ -108,17 +91,7 @@ public class TunnelEntry {
}
public boolean isClient() {
return isClient(mController.getType());
}
public static boolean isClient(String type) {
return ( ("client".equals(type)) ||
("httpclient".equals(type)) ||
("sockstunnel".equals(type)) ||
("socksirctunnel".equals(type)) ||
("connectclient".equals(type)) ||
("streamrclient".equals(type)) ||
("ircclient".equals(type)));
return TunnelUtil.isClient(mController.getType());
}
/* Client tunnel data */
......
This diff is collapsed.
package net.i2p.android.i2ptunnel.util;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import android.content.Context;
import android.content.res.Resources;
import net.i2p.I2PAppContext;
import net.i2p.android.router.R;
import net.i2p.android.router.util.Util;
import net.i2p.i2ptunnel.TunnelController;
import net.i2p.i2ptunnel.TunnelControllerGroup;
import net.i2p.util.FileUtil;
import net.i2p.util.SecureFile;
public abstract class TunnelUtil {
public static TunnelController getController(TunnelControllerGroup tcg, int tunnel) {
if (tunnel < 0) return null;
if (tcg == null) return null;
List<TunnelController> controllers = tcg.getControllers();
if (controllers.size() > tunnel)
return controllers.get(tunnel);
else
return null;
}
public static List<String> saveTunnel(Context ctx,
TunnelControllerGroup tcg,
int tunnelId,
Properties config) {
// Get current tunnel controller
TunnelController cur = getController(tcg, tunnelId);
if (config == null) {
List<String> ret = new ArrayList<String>();
ret.add("Invalid params");
return ret;
}
if (cur == null) {
// creating new
cur = new TunnelController(config, "", true);
tcg.addController(cur);
if (cur.getStartOnLoad())
cur.startTunnelBackground();
} else {
cur.setConfig(config, "");
}
// Only modify other shared tunnels
// if the current tunnel is shared, and of supported type
if (Boolean.parseBoolean(cur.getSharedClient()) && isClient(cur.getType())) {
// all clients use the same I2CP session, and as such, use the same I2CP options
List<TunnelController> controllers = tcg.getControllers();
for (int i = 0; i < controllers.size(); i++) {
TunnelController c = controllers.get(i);
// Current tunnel modified by user, skip
if (c == cur) continue;
// Only modify this non-current tunnel
// if it belongs to a shared destination, and is of supported type
if (Boolean.parseBoolean(c.getSharedClient()) && isClient(c.getType())) {
Properties cOpt = c.getConfig("");
if (config.getProperty("option.inbound.quantity") != null)
cOpt.setProperty("option.inbound.quantity", config.getProperty("option.inbound.quantity"));
if (config.getProperty("option.outbound.quantity") != null)
cOpt.setProperty("option.outbound.quantity", config.getProperty("option.outbound.quantity"));
if (config.getProperty("option.inbound.length") != null)
cOpt.setProperty("option.inbound.length", config.getProperty("option.inbound.length"));
if (config.getProperty("option.outbound.length") != null)
cOpt.setProperty("option.outbound.length", config.getProperty("option.outbound.length"));
if (config.getProperty("option.inbound.lengthVariance") != null)
cOpt.setProperty("option.inbound.lengthVariance", config.getProperty("option.inbound.lengthVariance"));
if (config.getProperty("option.outbound.lengthVariance") != null)
cOpt.setProperty("option.outbound.lengthVariance", config.getProperty("option.outbound.lengthVariance"));
if (config.getProperty("option.inbound.backupQuantity") != null)
cOpt.setProperty("option.inbound.backupQuantity", config.getProperty("option.inbound.backupQuantity"));
if (config.getProperty("option.outbound.backupQuantity") != null)
cOpt.setProperty("option.outbound.backupQuantity", config.getProperty("option.outbound.backupQuantity"));
cOpt.setProperty("option.inbound.nickname", TunnelConfig.CLIENT_NICKNAME);
cOpt.setProperty("option.outbound.nickname", TunnelConfig.CLIENT_NICKNAME);
c.setConfig(cOpt, "");
}
}
}
return doSave(ctx, tcg);
}
/**
* Stop the tunnel, delete from config,
* rename the private key file if in the default directory
*/
public static List<String> deleteTunnel(Context ctx, TunnelControllerGroup tcg, int tunnelId) {
List<String> msgs;
TunnelController cur = getController(tcg, tunnelId);
if (cur == null) {
msgs = new ArrayList<String>();
msgs.add("Invalid tunnel number");
return msgs;
}
msgs = tcg.removeController(cur);
msgs.addAll(doSave(ctx, tcg));
// Rename private key file if it was a default name in
// the default directory, so it doesn't get reused when a new
// tunnel is created.
// Use configured file name if available, not the one from the form.
String pk = cur.getPrivKeyFile();
//if (pk == null)
// pk = _privKeyFile;
if (pk != null && pk.startsWith("i2ptunnel") && pk.endsWith("-privKeys.dat") &&
((!isClient(cur.getType())) || cur.getPersistentClientKey())) {
I2PAppContext context = I2PAppContext.getGlobalContext();
File pkf = new File(context.getConfigDir(), pk);
if (pkf.exists()) {
String name = cur.getName();
if (name == null) {
name = cur.getDescription();
if (name == null) {
name = cur.getType();
if (name == null)
name = Long.toString(context.clock().now());
}
}
name = "i2ptunnel-deleted-" + name.replace(' ', '_') + '-' + context.clock().now() + "-privkeys.dat";
File backupDir = new SecureFile(context.getConfigDir(), TunnelController.KEY_BACKUP_DIR);
File to;
if (backupDir.isDirectory() || backupDir.mkdir())
to = new File(backupDir, name);
else
to = new File(context.getConfigDir(), name);
boolean success = FileUtil.rename(pkf, to);
if (success)
msgs.add("Private key file " + pkf.getAbsolutePath() +
" renamed to " + to.getAbsolutePath());
}
}
return msgs;
}
private static List<String> doSave(Context ctx, TunnelControllerGroup tcg) {
List<String> rv = tcg.clearAllMessages();
try {
tcg.saveConfig();
rv.add(0, ctx.getResources().getString(R.string.i2ptunnel_msg_config_saved));
} catch (IOException ioe) {
Util.e("Failed to save config file", ioe);
rv.add(0, ctx.getResources().getString(R.string.i2ptunnel_msg_config_save_failed) + ": " + ioe.toString());
}
return rv;
}
/* General tunnel data for any type */
public static String getTypeFromName(String typeName, Context ctx) {
Resources res = ctx.getResources();
if (res.getString(R.string.i2ptunnel_type_client).equals(typeName))
return "client";
else if (res.getString(R.string.i2ptunnel_type_httpclient).equals(typeName))
return "httpclient";
else if (res.getString(R.string.i2ptunnel_type_ircclient).equals(typeName))
return "ircclient";
else if (res.getString(R.string.i2ptunnel_type_server).equals(typeName))
return "server";
else if (res.getString(R.string.i2ptunnel_type_httpserver).equals(typeName))
return "httpserver";
else if (res.getString(R.string.i2ptunnel_type_sockstunnel).equals(typeName))
return "sockstunnel";
else if (res.getString(R.string.i2ptunnel_type_socksirctunnel).equals(typeName))
return "socksirctunnel";
else if (res.getString(R.string.i2ptunnel_type_connectclient).equals(typeName))
return "connectclient";
else if (res.getString(R.string.i2ptunnel_type_ircserver).equals(typeName))
return "ircserver";
else if (res.getString(R.string.i2ptunnel_type_streamrclient).equals(typeName))
return "streamrclient";
else if (res.getString(R.string.i2ptunnel_type_streamrserver).equals(typeName))
return "streamrserver";
else if (res.getString(R.string.i2ptunnel_type_httpbidirserver).equals(typeName))
return "httpbidirserver";
else
return typeName;
}
public static String getTypeName(String type, Context context) {
Resources res = context.getResources();
if ("client".equals(type))
return res.getString(R.string.i2ptunnel_type_client);
else if ("httpclient".equals(type))
return res.getString(R.string.i2ptunnel_type_httpclient);
else if ("ircclient".equals(type))
return res.getString(R.string.i2ptunnel_type_ircclient);
else if ("server".equals(type))
return res.getString(R.string.i2ptunnel_type_server);
else if ("httpserver".equals(type))
return res.getString(R.string.i2ptunnel_type_httpserver);
else if ("sockstunnel".equals(type))
return res.getString(R.string.i2ptunnel_type_sockstunnel);
else if ("socksirctunnel".equals(type))
return res.getString(R.string.i2ptunnel_type_socksirctunnel);
else if ("connectclient".equals(type))
return res.getString(R.string.i2ptunnel_type_connectclient);
else if ("ircserver".equals(type))
return res.getString(R.string.i2ptunnel_type_ircserver);
else if ("streamrclient".equals(type))
return res.getString(R.string.i2ptunnel_type_streamrclient);
else if ("streamrserver".equals(type))
return res.getString(R.string.i2ptunnel_type_streamrserver);
else if ("httpbidirserver".equals(type))
return res.getString(R.string.i2ptunnel_type_httpbidirserver);
else
return type;
}
public static boolean isClient(String type) {
return ( ("client".equals(type)) ||
("httpclient".equals(type)) ||
("sockstunnel".equals(type)) ||
("socksirctunnel".equals(type)) ||
("connectclient".equals(type)) ||
("streamrclient".equals(type)) ||
("ircclient".equals(type)));
}
public static String getPrivateKeyFile(TunnelControllerGroup tcg, int tunnel) {
TunnelController tun = getController(tcg, tunnel);
if (tun != null && tun.getPrivKeyFile() != null)
return tun.getPrivKeyFile();
if (tunnel < 0)
tunnel = tcg == null ? 999 : tcg.getControllers().size();
return "i2ptunnel" + tunnel + "-privKeys.dat";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment