Newer
Older
package net.i2p.android.i2ptunnel.loader;
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.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;
public static final int STARTING = 2;
public static final int NOT_RUNNING = 3;
public static final int STANDBY = 4;
private final Context mContext;
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;
mId = id;
}
public int getId() {
return mId;
}
public TunnelController getController() {
return mController;
}
/* General tunnel data for any type */
public String getName() {
if (mController.getName() != null)
return mController.getName();
else
return mContext.getResources()
.getString(R.string.i2ptunnel_new_tunnel);
}
return mController.getType();
}
return TunnelUtil.getTypeName(mController.getType(), mContext);
public String getDescription() {
String rv = mController.getDescription();
if (rv != null)
return rv;
return "";
}
public boolean startAutomatically() {
return mController.getStartOnLoad();
}
public int getStatus() {
if (mController.getIsRunning()) {
if (isClient() && mController.getIsStandby())
return STANDBY;
else
return RUNNING;
} else if (mController.getIsStarting()) return STARTING;
else return NOT_RUNNING;
}
public boolean isClient() {
return TunnelUtil.isClient(mController.getType());
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
}
/* Client tunnel data */
public boolean isSharedClient() {
return Boolean.parseBoolean(mController.getSharedClient());
}
public String getClientInterface() {
if ("streamrclient".equals(mController.getType()))
return mController.getTargetHost();
else
return mController.getListenOnInterface();
}
public String getClientPort() {
String rv = mController.getListenPort();
if (rv != null)
return rv;
return "";
}
public String getClientDestination() {
String rv;
if ("client".equals(getInternalType()) ||
"ircclient".equals(getInternalType()) ||
"streamrclient".equals(getInternalType()))
rv = mController.getTargetDestination();
else
rv = mController.getProxyList();
return rv != null ? rv : "";
}
/* Server tunnel data */
/**
* Call this to see if it is okay to linkify getServerTarget()
* @return true if getServerTarget() can be linkified, false otherwise.
*/
public boolean isServerTargetLinkValid() {
return ("httpserver".equals(mController.getType()) ||
"httpbidirserver".equals(mController.getType())) &&
mController.getTargetHost() != null &&
mController.getTargetPort() != null;
}
/**
* @return valid host:port only if isServerTargetLinkValid() is true
*/
public String getServerTarget() {
String host;
if ("streamrserver".equals(getInternalType()))
host = mController.getListenOnInterface();
else
host = mController.getTargetHost();
String port = mController.getTargetPort();
if (host == null) host = "";
if (port == null) port = "";
if (host.indexOf(':') >= 0)
host = '[' + host + ']';
return host + ":" + port;
}
public String getDestinationBase64() {
String rv = mController.getMyDestination();
if (rv != null)
return rv;
// if not running, do this the hard way
String keyFile = mController.getPrivKeyFile();
if (keyFile != null && keyFile.trim().length() > 0) {
PrivateKeyFile pkf = new PrivateKeyFile(keyFile);
try {
Destination d = pkf.getDestination();
if (d != null)
return d.toBase64();
} catch (Exception e) {}
}
return "";
}
public String getDestHashBase32() {
String rv = mController.getMyDestHashBase32();
if (rv != null)
return rv;
return "";
}
/* Data for some client and server tunnels */
/* Other output formats */
public String getIfacePort() {
String host;
if ("streamrclient".equals(getInternalType()))
host = mController.getTargetHost();
else
host = mController.getListenOnInterface();
String port = mController.getListenPort();
if (host == null) host = "";
if (port == null) port = "";
return host + ":" + port;
} else return getServerTarget();
}
public String getDetails() {
if (isClient())
details = getClientDestination();
else
details = "";
return details;
}
public Drawable getStatusIcon() {
switch (getStatus()) {
case STANDBY:
case STARTING:
return mContext.getResources()
.getDrawable(R.drawable.local_inprogress);
case RUNNING:
return mContext.getResources()
.getDrawable(R.drawable.local_up);
case NOT_RUNNING:
default:
return mContext.getResources()