NanoHttpd / nanohttpd

Tiny, easily embeddable HTTP server in Java.
http://nanohttpd.org
BSD 3-Clause "New" or "Revised" License
6.92k stars 1.69k forks source link

nanohttpd with SSL on Android 6.0.1 #516

Open wluijk opened 5 years ago

wluijk commented 5 years ago

I use this code to create the HTTP server and enable the SSL in my service. Can anyone shine light on the place (path) where the keystore.jks must be placed in order to get found by the HTTP server on Android 6.0.1?

When there is a flaw in my code, please let me know.


public class MyService extends Service {
    private static MyHTTPD server;
    private static final String TAG = "CTRL_FOR_LED";
    Intent ctx;

    @Override
    public IBinder onBind(Intent intent) {
        ctx = intent;
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate service");
        try {
            server = new MyHTTPD();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");

        try {
            server.makeSecure(NanoHTTPD.makeSSLSocketFactory("/path/keystore.jks", "myPassword".toCharArray()), null );
            server.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        server.stop();
        Log.d(TAG, "onDestroy");
    }
}
LordFokas commented 5 years ago

The socket is built here:

https://github.com/NanoHttpd/nanohttpd/blob/master/core/src/main/java/org/nanohttpd/protocols/http/sockets/SecureServerSocketFactory.java

So that path is likely some sort of hardcoded or env variable moved around in java internals...

yasfdany commented 3 years ago

the Classpath path is on src/main/resources . just create the resources folder if it's not there yet

note : It is not /res folder