Skip to content

Commit a2fc38f

Browse files
committedMay 29, 2023
Merge branch 'develop'
2 parents 8e7c55a + 77e3ae5 commit a2fc38f

File tree

134 files changed

+4591
-2175
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+4591
-2175
lines changed
 

‎client/packages/android/app/src/main/java/org/openmsupply/client/CertWebViewClient.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.io.FileInputStream;
2525
import java.io.FileNotFoundException;
2626
import java.security.MessageDigest;
27-
import java.security.PublicKey;
2827
import java.security.cert.Certificate;
2928
import java.security.cert.CertificateException;
3029
import java.security.cert.CertificateFactory;
@@ -119,7 +118,7 @@ private boolean validateLocalCertificate(SslCertificate targetCert) {
119118
* It needs to be checked that we know/trust the server before performing the
120119
* certificate validation.
121120
*/
122-
private boolean validateNonLocalCertificate(SslCertificate targetCert, NativeApi.omSupplyServer connectedServer) {
121+
private boolean validateNonLocalCertificate(SslCertificate targetCert, NativeApi.FrontEndHost connectedServer) {
123122
// Calculate SSL fingerprint
124123
MessageDigest md = null;
125124
try {
@@ -164,7 +163,7 @@ public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError e
164163

165164
String url = error.getUrl();
166165
Boolean isDiscovery = url.startsWith(nativeApi.getLocalUrl());
167-
NativeApi.omSupplyServer connectedServer = nativeApi.getConnectedServer();
166+
NativeApi.FrontEndHost connectedServer = nativeApi.getConnectedServer();
168167
Boolean isConnectedToServer = connectedServer != null && url.startsWith(connectedServer.getUrl());
169168

170169
// Default behaviour if not connected to a server or not discovery
@@ -200,7 +199,7 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request
200199
// will not only reload, but will open the URL in a browser tab
201200
// for local URLs we don't want this to happen!
202201
Uri url = request.getUrl();
203-
if(url.toString().startsWith(this.nativeApi.getServerUrl())) {
202+
if (url.toString().startsWith(nativeApi.getServerUrl())) {
204203
return false;
205204
}
206205

‎client/packages/android/app/src/main/java/org/openmsupply/client/NativeApi.java

+56-11
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,27 @@
2222
import java.io.IOException;
2323
import java.net.HttpURLConnection;
2424
import java.net.InetAddress;
25+
import java.net.MalformedURLException;
2526
import java.net.URL;
2627
import java.nio.charset.StandardCharsets;
2728
import java.util.ArrayDeque;
2829
import java.util.Deque;
2930

31+
import javax.net.ssl.HttpsURLConnection;
3032
import javax.net.ssl.SSLHandshakeException;
33+
import javax.net.ssl.SSLSocketFactory;
3134

3235
@CapacitorPlugin(name = "NativeApi")
3336
public class NativeApi extends Plugin implements NsdManager.DiscoveryListener {
3437
private static final String LOG_FILE_NAME = "remote_server.log";
3538
public static final String OM_SUPPLY = "omSupply";
36-
private static final String DEFAULT_URL = "https://localhost:8000/";
39+
private static final Integer DEFAULT_PORT = DiscoveryConstants.PORT;
40+
private static final String DEFAULT_URL = "https://localhost:" + DEFAULT_PORT + "/";
3741
private static final String CONFIGURATION_GROUP = "omSupply_preferences";
3842
DiscoveryConstants discoveryConstants;
3943
JSArray discoveredServers;
4044
Deque<NsdServiceInfo> serversToResolve;
41-
omSupplyServer connectedServer;
45+
FrontEndHost connectedServer;
4246
NsdManager discoveryManager;
4347
boolean isDebug;
4448
boolean isAdvertising;
@@ -73,7 +77,7 @@ public boolean getIsDebug() {
7377
return isDebug;
7478
}
7579

76-
public omSupplyServer getConnectedServer() {
80+
public FrontEndHost getConnectedServer() {
7781
return connectedServer;
7882
}
7983

@@ -243,10 +247,7 @@ public void connectedServer(PluginCall call) {
243247
call.resolve(connectedServer == null ? null : connectedServer.data);
244248
}
245249

246-
@PluginMethod()
247-
public void connectToServer(PluginCall call) {
248-
omSupplyServer server = new omSupplyServer(call.getData());
249-
250+
private void onConnectToServer(FrontEndHost server) {
250251
stopServerDiscovery();
251252
connectedServer = server;
252253

@@ -257,7 +258,38 @@ public void connectToServer(PluginCall call) {
257258
WebView webView = bridge.getWebView();
258259
this.serverUrl = url;
259260
// .post to run on UI thread
260-
webView.post(() -> webView.loadUrl(url));
261+
webView.post(() -> webView.loadUrl(server.getConnectionUrl()));
262+
}
263+
264+
@PluginMethod()
265+
public void connectToServer(PluginCall call) throws MalformedURLException {
266+
FrontEndHost server = new FrontEndHost(call.getData());
267+
JSObject response = new JSObject();
268+
269+
try {
270+
URL url = new URL(server.getConnectionUrl());
271+
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
272+
urlc.setRequestMethod("GET");
273+
urlc.connect();
274+
int status = urlc.getResponseCode();
275+
276+
if (status == 200) {
277+
onConnectToServer(server);
278+
response.put("success", true);
279+
} else {
280+
response.put("success", false);
281+
response.put("error", "Connecting to server: response code="+ status);
282+
}
283+
} catch (SSLHandshakeException e) {
284+
// server is running and responding with an SSL error
285+
// which we will ignore, so ok to proceed
286+
onConnectToServer(server);
287+
response.put("success", true);
288+
} catch (IOException e) {
289+
response.put("success", false);
290+
response.put("error", e.getMessage());
291+
}
292+
call.resolve(response);
261293
}
262294

263295
// NsdManager.DiscoveryListener
@@ -410,19 +442,32 @@ public void readLog(PluginCall call) {
410442
call.resolve(response);
411443
}
412444

413-
public class omSupplyServer {
445+
/** Helper class to get access to the JS FrontEndHost data */
446+
public class FrontEndHost {
414447
JSObject data;
415448

416-
public omSupplyServer(JSObject data) {
449+
public FrontEndHost(JSObject data) {
417450
this.data = data;
418451
}
419452

453+
/**
454+
* Constructs the server's base url string including protocol, ip and port,
455+
* e.g. https://127.0.0.1:8000
456+
*/
420457
public String getUrl() {
458+
return data.getString("protocol") + "://" + data.getString("ip") + ":" + data.getString("port");
459+
}
460+
461+
/**
462+
* Constructs the url to be used when connecting to a server,
463+
* e.g. https://127.0.0.1:8000/login
464+
*/
465+
public String getConnectionUrl() {
421466
String path = "";
422467
if (data.getString("path") != null) {
423468
path = "/" + data.getString("path");
424469
}
425-
return data.getString("protocol") + "://" + data.getString("ip") + ":" + data.getString("port") + path;
470+
return getUrl() + path;
426471
}
427472

428473
public boolean isLocal() {

0 commit comments

Comments
 (0)
Please sign in to comment.