package com.futurepress.staticserver; import android.util.Log; import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import fi.iki.elonen.SimpleWebServer; import java.io.File; import java.io.IOException; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.ServerSocket; import java.net.SocketException; import java.util.Enumeration; public class FPStaticServerModule extends ReactContextBaseJavaModule implements LifecycleEventListener { private static final String LOGTAG = "FPStaticServerModule"; private boolean keep_alive = false; private String localPath = ""; private boolean localhost_only = false; private int port = 9999; private final ReactApplicationContext reactContext; private SimpleWebServer server = null; private String url = ""; private File www_root = null; @Override // com.facebook.react.bridge.NativeModule public String getName() { return "FPStaticServer"; } @Override // com.facebook.react.bridge.LifecycleEventListener public void onHostPause() { } @Override // com.facebook.react.bridge.LifecycleEventListener public void onHostResume() { } public FPStaticServerModule(ReactApplicationContext reactApplicationContext) { super(reactApplicationContext); this.reactContext = reactApplicationContext; } private String __getLocalIpAddress() { try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { Enumeration<InetAddress> inetAddresses = networkInterfaces.nextElement().getInetAddresses(); while (true) { if (inetAddresses.hasMoreElements()) { InetAddress nextElement = inetAddresses.nextElement(); if (!nextElement.isLoopbackAddress()) { String hostAddress = nextElement.getHostAddress(); if (InetAddressUtils.a(hostAddress)) { Log.w(LOGTAG, "local IP: " + hostAddress); return hostAddress; } } } } } return "127.0.0.1"; } catch (SocketException e) { Log.e(LOGTAG, e.toString()); return "127.0.0.1"; } } /* JADX WARNING: Code restructure failed: missing block: B:14:?, code lost: r1.port = findRandomOpenPort().intValue(); */ /* JADX WARNING: Code restructure failed: missing block: B:15:0x0031, code lost: r1.port = 9999; */ /* JADX WARNING: Failed to process nested try/catch */ /* JADX WARNING: Missing exception handler attribute for start block: B:13:0x0026 */ @ReactMethod public void start(String str, String str2, Boolean bool, Boolean bool2, Promise promise) { if (this.server != null) { promise.a((Object) this.url); return; } if (str != null) { this.port = Integer.parseInt(str); if (this.port == 0) { try { this.port = findRandomOpenPort().intValue(); } catch (IOException unused) { this.port = 9999; } } } if (str2 == null || (!str2.startsWith("/") && !str2.startsWith("file:///"))) { this.www_root = new File(this.reactContext.getFilesDir(), str2); this.localPath = this.www_root.getAbsolutePath(); } else { this.www_root = new File(str2); this.localPath = this.www_root.getAbsolutePath(); } if (bool != null) { this.localhost_only = bool.booleanValue(); } if (bool2 != null) { this.keep_alive = bool2.booleanValue(); } try { if (this.localhost_only) { this.server = new WebServer("localhost", this.port, this.www_root); } else { this.server = new WebServer(__getLocalIpAddress(), this.port, this.www_root); } if (this.localhost_only) { this.url = "http://localhost:" + this.port; } else { this.url = "http://" + __getLocalIpAddress() + ":" + this.port; } this.server.d(); promise.a((Object) this.url); } catch (IOException e) { String message = e.getMessage(); if (this.server == null || !message.equals("bind failed: EADDRINUSE (Address already in use)")) { promise.a((String) null, message); } else { promise.a((Object) this.url); } } } private Integer findRandomOpenPort() throws IOException { try { ServerSocket serverSocket = new ServerSocket(0); int localPort = serverSocket.getLocalPort(); Log.w(LOGTAG, "port:" + localPort); serverSocket.close(); return Integer.valueOf(localPort); } catch (IOException unused) { return 0; } } @ReactMethod public void stop() { if (this.server != null) { Log.w(LOGTAG, "Stopped Server"); this.server.e(); this.server = null; } } @ReactMethod public void origin(Promise promise) { if (this.server != null) { promise.a((Object) this.url); } else { promise.a(""); } } @ReactMethod public void isRunning(Promise promise) { promise.a(Boolean.valueOf(this.server != null && this.server.b())); } @Override // com.facebook.react.bridge.LifecycleEventListener public void onHostDestroy() { stop(); } }