diff options
author | Arne Schwabe <arne@rfc2549.org> | 2021-10-03 18:26:55 +0200 |
---|---|---|
committer | Arne Schwabe <arne@rfc2549.org> | 2021-10-03 18:26:55 +0200 |
commit | cb2323d7c5e9e54d8f5eb39e6e798e5c025e0640 (patch) | |
tree | 97fd4ca161e5123e4234bbfbfc13882b5297fd1b /remoteExample/src/main | |
parent | 164d6d6cca7f045db347b55c6412f279e060cbef (diff) |
Fix various issues in remoteExample
Diffstat (limited to 'remoteExample/src/main')
4 files changed, 73 insertions, 34 deletions
diff --git a/remoteExample/src/main/AndroidManifest.xml b/remoteExample/src/main/AndroidManifest.xml index 1278df95..2dce4ae9 100644 --- a/remoteExample/src/main/AndroidManifest.xml +++ b/remoteExample/src/main/AndroidManifest.xml @@ -25,5 +25,7 @@ </intent-filter> </activity> </application> - + <queries> + <package android:name="de.blinkt.openvpn"/> + </queries> </manifest> diff --git a/remoteExample/src/main/java/de/blinkt/openvpn/remote/MainFragment.java b/remoteExample/src/main/java/de/blinkt/openvpn/remote/MainFragment.java index c450bf22..46699434 100644 --- a/remoteExample/src/main/java/de/blinkt/openvpn/remote/MainFragment.java +++ b/remoteExample/src/main/java/de/blinkt/openvpn/remote/MainFragment.java @@ -21,14 +21,18 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; +import android.widget.Toast; +import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.InvocationTargetException; +import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.Socket; +import java.net.URL; import java.net.UnknownHostException; import java.util.List; @@ -50,6 +54,7 @@ public class MainFragment extends Fragment implements View.OnClickListener, Hand v.findViewById(R.id.getMyIP).setOnClickListener(this); v.findViewById(R.id.startembedded).setOnClickListener(this); v.findViewById(R.id.addNewProfile).setOnClickListener(this); + v.findViewById(R.id.addNewProfileEdit).setOnClickListener(this); mHelloWorld = (TextView) v.findViewById(R.id.helloworld); mStartVpn = (Button) v.findViewById(R.id.startVPN); mStatus = (TextView) v.findViewById(R.id.status); @@ -66,6 +71,7 @@ public class MainFragment extends Fragment implements View.OnClickListener, Hand private static final int START_PROFILE_BYUUID = 3; private static final int ICS_OPENVPN_PERMISSION = 7; private static final int PROFILE_ADD_NEW = 8; + private static final int PROFILE_ADD_NEW_EDIT = 9; protected IOpenVPNAPIService mService=null; @@ -74,12 +80,19 @@ public class MainFragment extends Fragment implements View.OnClickListener, Hand - private void startEmbeddedProfile(boolean addNew) + private void startEmbeddedProfile(boolean addNew, boolean editable) { try { - InputStream conf = getActivity().getAssets().open("test.conf"); + InputStream conf; + /* Try opening test.local.conf first */ + try { + conf = getActivity().getAssets().open("test.local.conf"); + } + catch (IOException e) { + conf = getActivity().getAssets().open("test.conf"); + } BufferedReader br = new BufferedReader(new InputStreamReader(conf)); - StringBuilder config= new StringBuilder(); + StringBuilder config = new StringBuilder(); String line; while(true) { line = br.readLine(); @@ -90,13 +103,15 @@ public class MainFragment extends Fragment implements View.OnClickListener, Hand br.close(); conf.close(); - if (addNew) - mService.addNewVPNProfile("nonEditable", false, config.toString()); - else + if (addNew) { + String name = editable ? "Profile from remote App" : "Non editable profile"; + mService.addNewVPNProfile(name, editable, config.toString()); + } else mService.startVPN(config.toString()); } catch (IOException | RemoteException e) { e.printStackTrace(); } + Toast.makeText(getActivity(), "Profile started/added", Toast.LENGTH_LONG).show(); } @Override @@ -267,6 +282,15 @@ public class MainFragment extends Fragment implements View.OnClickListener, Hand // TODO Auto-generated catch block e.printStackTrace(); } + + case R.id.addNewProfileEdit: + try { + prepareStartProfile(PROFILE_ADD_NEW_EDIT); + } catch (RemoteException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + default: break; } @@ -286,7 +310,7 @@ public class MainFragment extends Fragment implements View.OnClickListener, Hand public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { if(requestCode==START_PROFILE_EMBEDDED) - startEmbeddedProfile(false); + startEmbeddedProfile(false, false); if(requestCode==START_PROFILE_BYUUID) try { mService.startProfile(mStartUUID); @@ -303,7 +327,10 @@ public class MainFragment extends Fragment implements View.OnClickListener, Hand } if (requestCode == PROFILE_ADD_NEW) { - startEmbeddedProfile(true); + startEmbeddedProfile(true, false); + } + else if (requestCode == PROFILE_ADD_NEW_EDIT) { + startEmbeddedProfile(true, true); } } }; @@ -311,22 +338,21 @@ public class MainFragment extends Fragment implements View.OnClickListener, Hand String getMyOwnIP() throws UnknownHostException, IOException, RemoteException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { - String resp=""; - Socket client = new Socket(); - // Setting Keep Alive forces creation of the underlying socket, otherwise getFD returns -1 - client.setKeepAlive(true); - - - client.connect(new InetSocketAddress("v4address.com", 23),20000); - client.shutdownOutput(); - BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); - while (true) { - String line = in.readLine(); - if( line == null) - return resp; - resp+=line; - } + StringBuilder resp = new StringBuilder(); + URL url = new URL("https://icanhazip.com"); + HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); + try { + BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); + while (true) { + String line = in.readLine(); + if( line == null) + return resp.toString(); + resp.append(line); + } + } finally { + urlConnection.disconnect(); + } } diff --git a/remoteExample/src/main/res/layout/fragment_main.xml b/remoteExample/src/main/res/layout/fragment_main.xml index eda36740..0cc953f6 100644 --- a/remoteExample/src/main/res/layout/fragment_main.xml +++ b/remoteExample/src/main/res/layout/fragment_main.xml @@ -63,23 +63,33 @@ android:layout_toRightOf="@+id/startVPN" android:text="@string/disconnect" /> - <Button + <Button android:id="@+id/startembedded" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_alignParentLeft="true" android:layout_below="@+id/getMyIP" + android:layout_alignParentLeft="true" android:text="@string/start_embedded" /> <Button - android:id="@+id/addNewProfile" - style="?android:attr/buttonStyleSmall" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:text="@string/addNew" - android:layout_alignTop="@+id/startembedded" - android:layout_toRightOf="@+id/startembedded" - android:layout_toEndOf="@+id/startembedded" /> + android:id="@+id/addNewProfile" + style="?android:attr/buttonStyleSmall" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_alignTop="@+id/startembedded" + android:layout_toEndOf="@+id/startembedded" + android:layout_toRightOf="@+id/startembedded" + android:text="@string/addNew" /> + + <Button + android:id="@+id/addNewProfileEdit" + style="?android:attr/buttonStyleSmall" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_alignTop="@+id/addNewProfile" + android:layout_toEndOf="@+id/addNewProfile" + android:layout_toRightOf="@+id/addNewProfile" + android:text="@string/addNewEdit" /> </RelativeLayout> diff --git a/remoteExample/src/main/res/values/strings.xml b/remoteExample/src/main/res/values/strings.xml index 30e05648..5bb63fc3 100644 --- a/remoteExample/src/main/res/values/strings.xml +++ b/remoteExample/src/main/res/values/strings.xml @@ -16,6 +16,7 @@ <string name="disconnect">Disconnect</string> <string name="start_embedded">Start embedded profile</string> <string name="addNew">Add new Profile</string> + <string name="addNewEdit">Add editable profile</string> </resources> |