package com.stoutner.privacybrowser.activities; import java.io.IOException; import java.net.DatagramSocket; import java.net.ServerSocket; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import androidx.appcompat.app.AppCompatActivity; public class I2PRouter extends AppCompatActivity { private static final int MIN_PORT_NUMBER = 0; private static final int MAX_PORT_NUMBER = 65535; private static final int DEFAULT_PROXY_PORT = 4444; private int proxy_port = DEFAULT_PROXY_PORT; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); } public void startService(View view) { Log.i("main","starting"); Intent serviceIntent = new Intent(this, I2PTunnelForegroundService.class); serviceIntent.putExtra("inputExtra", "Start Foreground Service in Android"); serviceIntent.setAction("START_FOREGROUND_ACTION"); startService(serviceIntent); } public void stopService(View view) { Log.i("main","stopping"); Intent serviceIntent = new Intent(this, I2PTunnelForegroundService.class); serviceIntent.putExtra("inputExtra", "Stop Foreground Service in Android"); serviceIntent.setAction("STOP_FOREGROUND_ACTION"); startService(serviceIntent); } public static boolean ProxyIsDown(){ return ProxyIsDown(DEFAULT_PROXY_PORT); } /** https://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/AvailablePortFinder.java?view=markup#l130 * Checks to see if a specific port is available. * * @param port the port to check for availability */ public static boolean ProxyIsDown(int port) { if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) { throw new IllegalArgumentException("Invalid start port: " + port); } ServerSocket ss = null; DatagramSocket ds = null; try { ss = new ServerSocket(port); ss.setReuseAddress(true); ds = new DatagramSocket(port); ds.setReuseAddress(true); return true; } catch (IOException e) { } finally { if (ds != null) { ds.close(); } if (ss != null) { try { ss.close(); } catch (IOException e) { /* should not be thrown */ } } } return false; } }