Say you have a REST service running on your local machine which is tested using a HTTP client from the same machine.
Now, you have your cordova android client that consumes your REST service.
How do you make requests to the web service running on your local machine (not hosted yet!)?
We will setup your local network for the android client [, infact any client on the internet] to access the web server on your local machine!
We will setup the home router to route the traffic from a specific port (on the router) to a specific port on your local machine (the port to which the web service is listening to). So that any request sent to the router port is routed to the local machine - PORT FORWARDING.
1. Find your router's setup page. Usually 192.168.0.1 or 192.168.1.1 (know your login and password - usually admin, admin).
NOTE: The router setup screens could be different for your router. However, the steps should be the same.
2. Assign a static IP for your local machine on your router:
4. Restart the router for the settings to take effect.
5. Find the WAN IP to post requests: Go to Status -> WAN IP. post requests to WAN_IP:ROUTER_PORT.
Beware! now that you have opened up the access to your local server from the internet, take necessary security measures to keep it safe!
If your WAN IP is same as your public IP, there is no carrier grade NAT, and anyone can access your server (If they know the port on which you are working).
This setup is not specific to your android client. This can be used to configure ACCESS to a local web service from the local network.
There are certain configurations (Security) to be made in Cordova so that the client can make requests to the web service.
1. CSP (Content Security Policy) header overrides the same-origin policy and restricts the origin to a set of sources. Apart from the HTTP header, the <meta> tag in the HTML header also whitelists the origins. Add your web server in the meta tag.
2. Cordova config (config.xml) by default allows all the origins. Modify the config to restrict requests to known domains.
Cordova version: 6.2.0
Now, you have your cordova android client that consumes your REST service.
How do you make requests to the web service running on your local machine (not hosted yet!)?
We will setup your local network for the android client [, infact any client on the internet] to access the web server on your local machine!
We will setup the home router to route the traffic from a specific port (on the router) to a specific port on your local machine (the port to which the web service is listening to). So that any request sent to the router port is routed to the local machine - PORT FORWARDING.
1. Find your router's setup page. Usually 192.168.0.1 or 192.168.1.1 (know your login and password - usually admin, admin).
NOTE: The router setup screens could be different for your router. However, the steps should be the same.
2. Assign a static IP for your local machine on your router:
- Choose static IP address for the local machine from a valid range. Go to DHCP -> DHCP settings -> between start IP address and end IP address.
- Go to DHCP -> address reservation -> Add new -> enter a IP and the MAC address of your local machine.
- Go to Forwarding -> virtual servers -> add new -> enter the protocol (TCP), router port, web service port and the local machine IP address (static IP).
- Some routers support only same port numbers for the router port and service port.
4. Restart the router for the settings to take effect.
5. Find the WAN IP to post requests: Go to Status -> WAN IP. post requests to WAN_IP:ROUTER_PORT.
Beware! now that you have opened up the access to your local server from the internet, take necessary security measures to keep it safe!
If your WAN IP is same as your public IP, there is no carrier grade NAT, and anyone can access your server (If they know the port on which you are working).
This setup is not specific to your android client. This can be used to configure ACCESS to a local web service from the local network.
There are certain configurations (Security) to be made in Cordova so that the client can make requests to the web service.
1. CSP (Content Security Policy) header overrides the same-origin policy and restricts the origin to a set of sources. Apart from the HTTP header, the <meta> tag in the HTML header also whitelists the origins. Add your web server in the meta tag.
<meta http-equiv="Content-Security-Policy" content="default-src 'self' <protocol://WAN_IP:ROUTER_PORT/path> data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
2. Cordova config (config.xml) by default allows all the origins. Modify the config to restrict requests to known domains.
Cordova version: 6.2.0
<access origin="*://<WAN_IP:ROUTER_PORT>" subdomains="true"/> <allow-intent href="http://*/*" /> <allow-intent href="https://*/*" />
No comments:
Post a Comment