Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Portforward sourceaddress #1353

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/src/main/java/org/connectbot/PortForwardListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ else if (HostDatabase.PORTFORWARD_REMOTE.equals(portForward.getType()))
nicknameEdit.setText(portForward.getNickname());

final EditText sourcePortEdit = editTunnelView.findViewById(R.id.portforward_source);
sourcePortEdit.setText(String.valueOf(portForward.getSourcePort()));
sourcePortEdit.setText(portForward.getSourceAddrAndPort());

final EditText destEdit = editTunnelView.findViewById(R.id.portforward_destination);
if (HostDatabase.PORTFORWARD_DYNAMIC5.equals(portForward.getType())) {
Expand Down Expand Up @@ -341,7 +341,7 @@ public void onClick(DialogInterface dialog, int which) {
break;
}

portForward.setSourcePort(Integer.parseInt(sourcePortEdit.getText().toString()));
portForward.setSource(sourcePortEdit.getText().toString());
portForward.setDest(destEdit.getText().toString());

// Use the new settings for the existing connection.
Expand Down
70 changes: 68 additions & 2 deletions app/src/main/java/org/connectbot/bean/PortForwardBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class PortForwardBean extends AbstractBean {
private long hostId = -1;
private String nickname = null;
private String type = null;
private String sourceAddr = null;
private int sourcePort = -1;
private String destAddr = null;
private int destPort = -1;
Expand All @@ -56,6 +57,26 @@ public PortForwardBean(long id, long hostId, String nickname, String type, int s
this.hostId = hostId;
this.nickname = nickname;
this.type = type;
this.sourceAddr = "127.0.0.1";
this.sourcePort = sourcePort;
this.destAddr = destAddr;
this.destPort = destPort;
}
/**
* @param id database ID of port forward
* @param nickname Nickname to use to identify port forward
* @param type One of the port forward types from {@link HostDatabase}
* @param sourceAddr Source hostname or IP address
* @param sourcePort Source port number
* @param destAddr Destination hostname or IP address
* @param destPort Destination port number
*/
public PortForwardBean(long id, long hostId, String nickname, String type, String sourceAddr, int sourcePort, String destAddr, int destPort) {
this.id = id;
this.hostId = hostId;
this.nickname = nickname;
this.type = type;
this.sourceAddr = sourceAddr;
this.sourcePort = sourcePort;
this.destAddr = destAddr;
this.destPort = destPort;
Expand All @@ -70,7 +91,8 @@ public PortForwardBean(long hostId, String nickname, String type, String source,
this.hostId = hostId;
this.nickname = nickname;
this.type = type;
this.sourcePort = Integer.parseInt(source);
//this.sourcePort = Integer.parseInt(source);
setSource(source);

setDest(dest);
}
Expand Down Expand Up @@ -136,6 +158,49 @@ public int getSourcePort() {
return sourcePort;
}

/**
* @return the sourceAddr
*/
public String getSourceAddr() {
if (sourceAddr != null) {
return sourceAddr;
}
else {
return "127.0.0.1";
}
}

/**
* @return source address and port or just port if now address specified
*/
@SuppressLint("DefaultLocale")
public String getSourceAddrAndPort() {
if (sourceAddr == null)
{
return String.format("%d", sourcePort);
}
else
{
return String.format("%s:%d", sourceAddr, sourcePort);
}
}

/**
* @param source The source in "host:port" format
*/
public final void setSource(String source) {
String[] sourceSplit = source.split(":", -1);
if (sourceSplit.length == 1)
{
this.sourceAddr = null;
this.sourcePort = Integer.parseInt(sourceSplit[0]);
}
else
{
this.sourceAddr = sourceSplit[0];
this.sourcePort = Integer.parseInt(sourceSplit[sourceSplit.length - 1]);
}
}
/**
* @param dest The destination in "host:port" format
*/
Expand Down Expand Up @@ -211,7 +276,7 @@ public CharSequence getDescription() {
String description = "Unknown type";

if (HostDatabase.PORTFORWARD_LOCAL.equals(type)) {
description = String.format("Local port %d to %s:%d", sourcePort, destAddr, destPort);
description = String.format("Local port %s to %s:%d", getSourceAddrAndPort(), destAddr, destPort);
} else if (HostDatabase.PORTFORWARD_REMOTE.equals(type)) {
description = String.format("Remote port %d to %s:%d", sourcePort, destAddr, destPort);
} else if (HostDatabase.PORTFORWARD_DYNAMIC5.equals(type)) {
Expand All @@ -231,6 +296,7 @@ public ContentValues getValues() {
values.put(HostDatabase.FIELD_PORTFORWARD_HOSTID, hostId);
values.put(HostDatabase.FIELD_PORTFORWARD_NICKNAME, nickname);
values.put(HostDatabase.FIELD_PORTFORWARD_TYPE, type);
values.put(HostDatabase.FIELD_PORTFORWARD_SOURCEADDR, sourceAddr);
values.put(HostDatabase.FIELD_PORTFORWARD_SOURCEPORT, sourcePort);
values.put(HostDatabase.FIELD_PORTFORWARD_DESTADDR, destAddr);
values.put(HostDatabase.FIELD_PORTFORWARD_DESTPORT, destPort);
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/org/connectbot/transport/SSH.java
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,9 @@ public boolean enablePortForward(PortForwardBean portForward) {
LocalPortForwarder lpf = null;
try {
lpf = connection.createLocalPortForwarder(
new InetSocketAddress(InetAddress.getLocalHost(), portForward.getSourcePort()),
//new InetSocketAddress(InetAddress.getLocalHost(), portForward.getSourcePort()),
//portForward.getDestAddr(), portForward.getDestPort());
new InetSocketAddress(portForward.getSourceAddr(), portForward.getSourcePort()),
portForward.getDestAddr(), portForward.getDestPort());
} catch (Exception e) {
Log.e(TAG, "Could not create local port forward", e);
Expand Down
18 changes: 13 additions & 5 deletions app/src/main/java/org/connectbot/util/HostDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class HostDatabase extends RobustSQLiteOpenHelper implements HostStorage,
public final static String TAG = "CB.HostDatabase";

public final static String DB_NAME = "hosts";
public final static int DB_VERSION = 25;
public final static int DB_VERSION = 26;

public final static String TABLE_HOSTS = "hosts";
public final static String FIELD_HOST_NICKNAME = "nickname";
Expand Down Expand Up @@ -83,6 +83,7 @@ public class HostDatabase extends RobustSQLiteOpenHelper implements HostStorage,
public final static String FIELD_PORTFORWARD_HOSTID = "hostid";
public final static String FIELD_PORTFORWARD_NICKNAME = "nickname";
public final static String FIELD_PORTFORWARD_TYPE = "type";
public final static String FIELD_PORTFORWARD_SOURCEADDR = "sourceaddr";
public final static String FIELD_PORTFORWARD_SOURCEPORT = "sourceport";
public final static String FIELD_PORTFORWARD_DESTADDR = "destaddr";
public final static String FIELD_PORTFORWARD_DESTPORT = "destport";
Expand Down Expand Up @@ -227,6 +228,7 @@ private void createTables(SQLiteDatabase db) {
+ FIELD_PORTFORWARD_HOSTID + " INTEGER, "
+ FIELD_PORTFORWARD_NICKNAME + " TEXT, "
+ FIELD_PORTFORWARD_TYPE + " TEXT NOT NULL DEFAULT '" + PORTFORWARD_LOCAL + "', "
+ FIELD_PORTFORWARD_SOURCEADDR + " TEXT, "
+ FIELD_PORTFORWARD_SOURCEPORT + " INTEGER NOT NULL DEFAULT 8080, "
+ FIELD_PORTFORWARD_DESTADDR + " TEXT, "
+ FIELD_PORTFORWARD_DESTPORT + " TEXT)");
Expand Down Expand Up @@ -396,6 +398,11 @@ public void onRobustUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) t
+ " FROM " + TABLE_HOSTS);
db.execSQL("DROP TABLE " + TABLE_HOSTS);
db.execSQL("ALTER TABLE " + TABLE_HOSTS + "_upgrade RENAME TO " + TABLE_HOSTS);
// fall through
case 25:
db.execSQL("ALTER TABLE " + TABLE_PORTFORWARDS
+ " ADD COLUMN " + FIELD_PORTFORWARD_SOURCEADDR + " TEXT ");
// fall through
}
}

Expand Down Expand Up @@ -762,7 +769,7 @@ public List<PortForwardBean> getPortForwardsForHost(HostBean host) {
}

Cursor c = mDb.query(TABLE_PORTFORWARDS, new String[] {
"_id", FIELD_PORTFORWARD_NICKNAME, FIELD_PORTFORWARD_TYPE, FIELD_PORTFORWARD_SOURCEPORT,
"_id", FIELD_PORTFORWARD_NICKNAME, FIELD_PORTFORWARD_TYPE, FIELD_PORTFORWARD_SOURCEADDR, FIELD_PORTFORWARD_SOURCEPORT,
FIELD_PORTFORWARD_DESTADDR, FIELD_PORTFORWARD_DESTPORT},
FIELD_PORTFORWARD_HOSTID + " = ?", new String[] {String.valueOf(host.getId())},
null, null, null);
Expand All @@ -773,9 +780,10 @@ public List<PortForwardBean> getPortForwardsForHost(HostBean host) {
host.getId(),
c.getString(1),
c.getString(2),
c.getInt(3),
c.getString(4),
c.getInt(5));
c.getString(3),
c.getInt(4),
c.getString(5),
c.getInt(6));
portForwards.add(pfb);
}

Expand Down
7 changes: 3 additions & 4 deletions app/src/main/res/layout/dia_portforward.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,15 @@
android:paddingEnd="10dip"
android:paddingRight="10dip"
android:text="@string/prompt_source_port"
android:textAppearance="?android:attr/textAppearanceMedium"/>
android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
android:id="@+id/portforward_source"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="8080"
android:inputType="number"
/>
android:hint="[127.0.0.1:]8080"
android:inputType="textEmailAddress" />
</TableRow>

<TableRow>
Expand Down