Skip to content

Commit

Permalink
Merge pull request #715 from guardianproject/proxy_port_fixes
Browse files Browse the repository at this point in the history
Proxy Port Fixes
  • Loading branch information
n8fr8 authored Jul 26, 2022
2 parents 6d258c4 + 99003b1 commit c3aab3c
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 31 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ android {
minSdkVersion 16
applicationId 'org.torproject.android'
targetSdkVersion 31
versionCode 1662300200
versionCode 1662300300

versionName getVersionName()
archivesBaseName = "Orbot-$versionName"
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/java/org/torproject/android/OrbotMainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ private void updateStatus(String torServiceMsg, String newTorStatus) {
case STATUS_OFF:
lblStatus.setText(String.format("Tor v%s", OrbotService.BINARY_TOR_VERSION));
imgStatus.setImageResource(R.drawable.toroff);
lblPorts.setText(R.string.ports_not_set);
lblPorts.setText("");
mBtnStart.setText(R.string.menu_start);
mPulsator.start();
resetBandwidthStatTextviews();
Expand Down Expand Up @@ -944,9 +944,15 @@ public void handleMessage(final Message msg) {
break;

case MESSAGE_PORTS:
int socksPort = data.getInt("socks");
int httpPort = data.getInt("http");
oma.lblPorts.setText(String.format(Locale.getDefault(), "SOCKS: %d | HTTP: %d", socksPort, httpPort));
var text = "";
var socksPort = data.getInt("socks");
if (socksPort > 0) text += "SOCKS: " + socksPort;
var httpPort = data.getInt("http");
if (httpPort > 0) {
if (socksPort > 0) text += " | ";
text += "HTTP: " + httpPort;
}
oma.lblPorts.setText(text);
break;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
TextView tvObfs4 = view.findViewById(R.id.tvObfs4);
tvObfs4.setText(getString(R.string.obfs4_url, IPtProxy.obfs4ProxyVersion()));

TextView tvSnowflake = view.findViewById(R.id.tvSnowflake);
tvSnowflake.setText(getString(R.string.snowflake_url, IPtProxy.snowflakeVersion()));

boolean buildAboutText = true;

if (savedInstanceState != null) {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/layout_about.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@


<TextView
android:id="@+id/tvSnowflake"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="@string/snowflake_url"
tools:text="@string/snowflake_url"
android:textColor="@android:color/white"
android:textColorLink="@android:color/white" />

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/layout_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
android:gravity="center"
android:lines="1"
android:maxLines="1"
android:text="@string/ports_not_set"
android:text=""
android:textSize="14sp" />

<LinearLayout
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<string name="tor_url" translatable="false">Tor: https://www.torproject.org</string>
<string name="libevent_url" translatable="false">LibEvent: http://www.monkey.org/~provos/libevent/</string>
<string name="obfs4_url" translatable="false">obfs4 v%s: https://gitlab.com/yawning/obfs4</string>
<string name="snowflake_url" translatable="false">Snowflake: https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake</string>
<string name="snowflake_url" translatable="false">Snowflake v%s: https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake</string>
<string name="openssl_url" translatable="false">OpenSSL: http://www.openssl.org</string>
<string name="third_party_software">3rd-Party-Software: </string>
<string name="version">Version:</string>
Expand Down Expand Up @@ -177,7 +177,6 @@
<string name="menu_new_identity">New Identity</string>
<string name="user_services">User services</string>
<string name="app_services">App services</string>
<string name="ports_not_set" translatable="false">SOCKS: -- | HTTP: --</string>
<string name="refresh_apps">Refresh Apps</string>
<!-- MoatActivity -->
<string name="request_bridges">Request Bridges</string>
Expand Down
1 change: 1 addition & 0 deletions orbotservice/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies {

//building from AAR built from https://github.com/guardianproject/tor-android/commit/95eca2a8c9eb44068f11332d7731580a13ef7b28
// until a new full release is done
//implementation files('../libs/i.aar')
implementation files('../libs/tor-android-binary-release.aar')
implementation files('../libs/geoip.jar')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package org.torproject.android.service;

import static org.torproject.jni.TorService.ACTION_ERROR;

import android.annotation.SuppressLint;
import android.app.Application;
import android.app.Notification;
Expand Down Expand Up @@ -432,6 +434,7 @@ public void onCreate() {
IntentFilter filter = new IntentFilter(TorControlCommands.SIGNAL_NEWNYM);
filter.addAction(CMD_ACTIVE);
filter.addAction(ACTION_STATUS);
filter.addAction(ACTION_ERROR);
filter.addAction(LOCAL_ACTION_NOTIFICATION_START);

mActionBroadcastReceiver = new ActionBroadcastReceiver();
Expand Down Expand Up @@ -511,16 +514,18 @@ private File updateTorrcCustomFile() throws IOException {
ipv6Pref += " IPv6Traffic NoIPv4Traffic ";
}

extraLines.append("SOCKSPort ").append(socksPortPref).append(isolate).append(ipv6Pref).append('\n');
if (!Prefs.openProxyOnAllInterfaces()) {
extraLines.append("SOCKSPort ").append(socksPortPref).append(isolate).append(ipv6Pref).append('\n');
} else {
extraLines.append("SOCKSPort 0.0.0.0:").append(socksPortPref).append(ipv6Pref).append(isolate).append("\n");
extraLines.append("SocksPolicy accept *:*").append('\n');
}

extraLines.append("SafeSocks 0").append('\n');
extraLines.append("TestSocks 0").append('\n');

if (Prefs.openProxyOnAllInterfaces())
extraLines.append("SocksListenAddress 0.0.0.0").append('\n');


extraLines.append("HTTPTunnelPort ").append(httpPortPref).append(isolate).append('\n');


if (prefs.getBoolean(PREF_CONNECTION_PADDING, false)) {
extraLines.append("ConnectionPadding 1").append('\n');
}
Expand All @@ -543,7 +548,7 @@ private File updateTorrcCustomFile() throws IOException {
var dnsPort = prefs.getString("pref_dnsport", TOR_DNS_PORT_DEFAULT + "");

extraLines.append("TransPort ").append(checkPortOrAuto(transPort)).append(isolate).append('\n');
extraLines.append("DNSPort ").append(checkPortOrAuto(dnsPort)).append('\n');
extraLines.append("DNSPort ").append(checkPortOrAuto(dnsPort)).append(isolate).append('\n');

extraLines.append("VirtualAddrNetwork 10.192.0.0/10").append('\n');
extraLines.append("AutomapHostsOnResolve 1").append('\n');
Expand Down Expand Up @@ -572,7 +577,7 @@ private File updateTorrcCustomFile() throws IOException {
debug("torrc.custom=" + extraLines.toString());

var fileTorRcCustom = TorService.getTorrc(this);
updateTorConfigCustom(fileTorRcCustom, extraLines.toString());
updateTorConfigCustom(fileTorRcCustom, extraLines.toString(), false);
return fileTorRcCustom;
}

Expand All @@ -593,8 +598,8 @@ private String checkPortOrAuto(String portString) {
return portString;
}

public void updateTorConfigCustom(File fileTorRcCustom, String extraLines) throws IOException {
var ps = new PrintWriter(new FileWriter(fileTorRcCustom, false));
public void updateTorConfigCustom(File fileTorRcCustom, String extraLines, boolean append) throws IOException {
var ps = new PrintWriter(new FileWriter(fileTorRcCustom, append));
ps.print(extraLines);
ps.flush();
ps.close();
Expand Down Expand Up @@ -632,6 +637,8 @@ private void replyWithStatus(Intent startRequest) {

}

private boolean showTorServiceErrorMsg = false;

/**
* The entire process for starting tor and related services is run from this method.
*/
Expand All @@ -647,6 +654,7 @@ private void startTor() {
showToolbarNotification("", NOTIFY_ID, R.drawable.ic_stat_tor);

startTorService();
showTorServiceErrorMsg = true;

if (Prefs.hostOnionServicesEnabled()) {
try {
Expand Down Expand Up @@ -704,7 +712,7 @@ private synchronized void startTorService() throws Exception {
updateTorConfigCustom(TorService.getDefaultsTorrc(this),
"DNSPort 0\n" +
"TransPort 0\n" +
"DisableNetwork 1\n");
"DisableNetwork 1\n", false);

var fileTorrcCustom = updateTorrcCustomFile();
if ((!fileTorrcCustom.exists()) || (!fileTorrcCustom.canRead()))
Expand Down Expand Up @@ -740,6 +748,7 @@ public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
if (conn != null) {
try {
initControlConnection();
if (conn == null) return; // maybe there was an error setting up the control connection

//override the TorService event listener
conn.addRawEventListener(mOrbotRawEventListener);
Expand Down Expand Up @@ -809,18 +818,22 @@ private void initControlConnection() {
try {
String confSocks = conn.getInfo("net/listeners/socks");
StringTokenizer st = new StringTokenizer(confSocks, " ");

confSocks = st.nextToken().split(":")[1];
confSocks = confSocks.substring(0, confSocks.length() - 1);
mPortSOCKS = Integer.parseInt(confSocks);

if (confSocks.trim().isEmpty()) {
mPortSOCKS = 0;
} else {
confSocks = st.nextToken().split(":")[1];
confSocks = confSocks.substring(0, confSocks.length() - 1);
mPortSOCKS = Integer.parseInt(confSocks);
}
String confHttp = conn.getInfo("net/listeners/httptunnel");
st = new StringTokenizer(confHttp, " ");

confHttp = st.nextToken().split(":")[1];
confHttp = confHttp.substring(0, confHttp.length() - 1);
mPortHTTP = Integer.parseInt(confHttp);

if (confHttp.trim().isEmpty()) {
mPortHTTP = 0;
} else {
st = new StringTokenizer(confHttp, " ");
confHttp = st.nextToken().split(":")[1];
confHttp = confHttp.substring(0, confHttp.length() - 1);
mPortHTTP = Integer.parseInt(confHttp);
}
String confDns = conn.getInfo("net/listeners/dns");
st = new StringTokenizer(confDns, " ");
if (st.hasMoreTokens()) {
Expand Down Expand Up @@ -1411,6 +1424,14 @@ public void onReceive(Context context, Intent intent) {
startTor();
break;
}
case ACTION_ERROR: {
if (showTorServiceErrorMsg) {
Toast.makeText(context, getString(R.string.orbot_config_invalid), Toast.LENGTH_LONG).show();
showTorServiceErrorMsg = false;
}
stopTor();
break;
}
case ACTION_STATUS: {
// hack for https://github.com/guardianproject/tor-android/issues/73 remove when fixed
var newStatus = intent.getStringExtra(EXTRA_STATUS);
Expand Down

0 comments on commit c3aab3c

Please sign in to comment.