Friday, July 3, 2020

How to access the local files externally through ngrok and python HTTP Server?

This tutorial explains how to share or access the local files externally through ngrok and Python http server.
I have the below two scenarios
  • a set of files that needs to be shared outside
  • share a simple website externally
The above scenarios can be achieved through Apache Server and external DNS configurations but require more configurations efforts.
  • ngrok free or paid version
  • Python latest version
Let us see how to enable the above scenarios through ngrok and python HTTP server with minimal configuration effort.
As a first step download ngrok(ngrok.com) for the required OS and extract the files
ngrok
ngrok
The ngrok allows you to expose a webserver running on your local machine to the internet. Just tell ngrok what port your web server is listening on.
On the free plan, ngrok’s URLs are randomly generated and temporary. If you want to use the same URL every time, you need to upgrade to a paid plan so that you can use the subdomain option for a stable URL. There are different paid plans they will provide some advance features like custom/reserved domains and multiple tunnels etc
ngrok

How ngrok works

You download and run a program on your machine and provide it the port of a network service, usually a web server.
It connects to the ngrok cloud service which accepts traffic on a public address and relays that traffic through to the ngrok process running on your machine and then on to the local address you specified.
ngrok

Python HTTP Server


Python standard library comes with an in-built webserver which can be invoked for simple web client server communication
The required port number can be assigned and the web server is accessed through this port
My system has python version 3.7.0 installed— “py -vi”
In the first scenario, i want to share some regular files externally
share files externally through ngrok and python http server
To start the HTTP server, cd to to the folder that should be shared(C:\Albin\blogData\demo\Share) through command prompt and execute the command “py -m http.server 80” — change the port number as required
share files externally through ngrok and python http server
Now the files are accessible through localhost
share files externally through ngrok and python http server

Let's now start ngrok to share this folder externally, cd to the folder where ngrok was extracted(C:\Albin\SW\ngrok-stable-windows-amd64)
Execute “ngrok.exe http 80”(80 is where python HTTP server running)
share files externally through ngrok and python http server
Now the external requests(http/https) are forwarded to localhost webserver through ngrok proxy domain
share files externally through ngrok and python http server

The local folders can be shared directly without a HTTP server through inbuilt ngrok file server. To share the local folder directly through ngrok , as a first step configure the authtoken to the ngrok

The authtoken can be retrieved through ngrok dashboard — the user should signup for a account , copy the command to set the authtoken by navigating to the dashboard.

Execute the command

ngrok-auth-token

Start the ngrok process — e.g ngrok http “file:///C:\Albin\blogData\demo\blogproject.blogproject

ngrok-auth-token

Now the files under the specific folder is accessible externally

ngrok-auth-token
Let us now see how to enable the second scenario, to access simple website externally, created a index.html file along with some test files into a folder(C:\Albin\blogData\demo\site), cd to the folder where the index.html and other files are located
share files externally through ngrok and python http server
Re-start the HTTP server, the pages are now accessible outside
share files externally through ngrok and python http server
The traffic can be monitored through the following URL — http://127.0.0.1:4040
share files externally through ngrok and python http server
The ngrok and python HTTP server can be used to share the local files externally without much effort and configurations.



Thursday, July 2, 2020

Geo Location Based Redirects with CloudFront and Apache | Redirect Web traffic Based on Country of Origin with CloudFront

This tutorial explain the approach to enable Geo Location based redirects with CloudFront and Apache.

Geo IP based redirection

Geo IP based redirection is the process of automatically redirecting a website visitor by their geolocation.

There are multiple options to enable the location based redirects in Apache, one of the option is using Geo IP database like MaxMind Geo IP database to map users’s IP to their location. Maxmind Geo IP database can be enabled through Apache module.

If you are using any of the CDN e.g CloudFront provides specific headers with request location, CloudFront will detect the user’s country of origin and pass along the county code to origin server in the CloudFront-Viewer-Country header. You can use this information to customize your responses e.g redirecting the users to specific URL based on origin country.

Prerequisites

Website enabled with CloudFront CDN and Apache

CloudFront Configurations

As a first step white list the CloudFront-Viewer-Country header in Cloudfront distribution

Access <<CloudFront Distribution>> →Behaviors → <<Specific Behavior>>

Edit the behavior

Whitelist CloudFront-Viewer-Country header — the header with user’s country of origin will be sent to origin server(Apache) on every request.

CloudFront-Viewer-Country header will have the two letter country code based on the request origin.

Enable Apache Redirect

Let us now enable the required redirect configuration to virtualhost, add the below redirect rules to enable the the country specific redirects

<VirtualHost *:80>

ServerAdmin [email protected]
DocumentRoot "C:\opt\communique\dispatcher\cache"
ServerName test.albinsblog.com
ServerAlias localhost

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/content/we-retail.html
RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^US$
RewriteRule ^.*$ https://test.albinsblog.com/content/we-retail/us/en.html [R=302,L]
RewriteCond %{REQUEST_URI} ^/content/we-retail.html
RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^IT$
RewriteRule ^.*$ https://test.albinsblog.com/content/we-retail/it/it.html [R=302,L]
RewriteCond %{REQUEST_URI} ^/content/we-retail.html
RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^CA
RewriteRule ^.*$ https://test.albinsblog.com/content/we-retail/ca/en.html [R=302,L]
RewriteCond %{REQUEST_URI} ^/content/we-retail.html
RewriteCond %{HTTP:CLOUDFRONT-VIEWER-COUNTRY} ^FR$
RewriteRule ^.*$ https://test.albinsblog.com/content/we-retail/fr/fr.html [R=302,L]
<Directory />
Options Indexes FollowSymLinks Includes
# Set includes to process .html files
AddOutputFilter INCLUDES .html
AddOutputFilterByType INCLUDES text/html
AllowOverride None
</Directory>

</VirtualHost>

I am using some VPN tool to initiate the connection from different origin country.

Connected the VPN to Canada

Now the user is redirected to Canada specific URL

The user is redirected to the country specific URL based on the users country of origin, CloudFront will detect the user’s country of origin and pass along the county code to origin server(Apache) in the CloudFront-Viewer-Country header. The Apache server redirect the user to the country specific URL’s based on the country code values in CloudFront-Viewer-Country header.





Friday, May 15, 2020

Error while creating the the new Maven projects - Caused by: java.lang.NoClassDefFoundError: org/apache/ivy/core/report/ResolveReport

Error while creating the new Maven projects - Caused by: java.lang.NoClassDefFoundError: org/apache/ivy/core/report/ResolveReport

I was getting the below exception while creating the AEM projects through Adobe Maven Arch Type 23, even the same issue was reported while generating the through Arch Type 22. 

mvn -e -B archetype:generate -D archetypeGroupId=com.adobe.granite.archetypes -D archetypeArtifactId=aem-project-archetype -D archetypeVersion=23 -D aemVersion=cloud -D appTitle="My Site" -D appId="mysite" -D groupId="com.mysite" -D frontendModule=general -D includeExamples=n -DappsFolderName=mysite

Enabled the flag -e to get more detailed errors - the below expection was thrown while executing the maven command

Number of foreign imports: 1
import: Entry[import  from realm ClassRealm[maven.api, parent: null]]

-----------------------------------------------------

    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:169)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: java.lang.NoClassDefFoundError: org/apache/ivy/core/report/ResolveReport
    at java.lang.Class.getDeclaredMethods0 (Native Method)
    at java.lang.Class.privateGetDeclaredMethods (Class.java:2701)
    at java.lang.Class.getDeclaredMethods (Class.java:1975)
    at org.codehaus.groovy.reflection.CachedClass$3$1.run (CachedClass.java:84)
    at java.security.AccessController.doPrivileged (Native Method)
    at org.codehaus.groovy.reflection.CachedClass$3.initValue (CachedClass.java:81)
    at org.codehaus.groovy.reflection.CachedClass$3.initValue (CachedClass.java:79)
    at org.codehaus.groovy.util.LazyReference.getLocked (LazyReference.java:46)
    at org.codehaus.groovy.util.LazyReference.get (LazyReference.java:33)
    at org.codehaus.groovy.reflection.CachedClass.getMethods (CachedClass.java:250)

After a long struggle identified the issue was not specific to the arch type and caused by the corrupted local maven repository, the issue got resolved after removing the org folder folder from the maven repository(renamed the folder to org_bak)

Adobe-maven-arch-type-error



Tuesday, January 21, 2020

Same site Cookie Attribute

Same site Cookie Attribute

This video explains the details on new Same site cookie Attribute recently introduced.
  • SameSite=Strict
  • SameSite=Lax
  • SameSite=None



Tuesday, December 3, 2019

How to share the Wireless Network Connection with Hyper-V in Windows 10?

I was trying to configure Hyper-V virtual machine with RedHat Linux in my windows 10 laptop and encountering some issues. This blog explain the steps followed to share the Wireless Network Connection with Hyper-V and RedHat Linux in Windows 10

Ensure the system is connected to Wireless Network

Turn Off the Virtual Machine

windows10-hyper-v-wireless-network
Click on "Virtual Switch Manager"
Click on "new virtual network switch" and Select External as type

windows10-hyper-v-wireless-network

Enter a name and select the wireless adapter from the list
Click on Apply and OK

windows10-hyper-v-wireless-network

Start and Connect to the virtual machine

Login to the user account

windows10-hyper-v-wireless-network

Click on the Network icon and further click on the Connect

windows10-hyper-v-wireless-network

Now you will be able to connect to the network

windows10-hyper-v-wireless-network

To connect automatically on login

Click on "Wired Settings" 

windows10-hyper-v-wireless-network

Click on Settings

windows10-hyper-v-wireless-network

Select "Connect Automatically" and Apply

windows10-hyper-v-wireless-network

Now the network will be connected automatically on login - the wireless network on the system should be connected