Search This Blog

01 May 2018

Native vs Progressive Web Application (PWA)

I recently watched a video comparing Native vs Progressive Web applications. It was about how a PWA can look and behave as much as possible as a Native app. There was a part something like - look here the browser address bar isn’t even visible and look we can add a shortcut to this page on the Home screen. Great so we are deceiving the user and it is not clear he is in the browser by hiding the address bar. So we are fragmenting the places we can have bookmarks to pages in the browser and on the Home screen - yes it is convenient to have an option in the browser to be able to place a bookmark to a page on the Home screen, but it is an option and the user should make a concise decision if it is something that he uses that much that he needs it on the Home screen, not some norm for all web applications. My point is if it is no obscure that we are using a web application that is running in the browser, the next things is users searching for an update at the App Store for this application, because they have forgotten that it is a site they are visiting with the web browser. And yes a big one is - if it is a web application it is write once run everywhere. Well yes, if you have written it with support for every browser that is. Users can run any number of browsers on their devices and if it is going to be “run everywhere”, then you need to build support for all the browsers. At least if it is native application with web content
it has to support only the Native WebView components for the systems. There is nothing bad about having a web application instead of Native application, there are pros and cons, but be proud of the good things about having a web application and be proud it is running in the web browser.

23 April 2018

Android Socket connections

Here we have an Android application that can act as a socket client, server or both for Peer-to-Peer connectivity. It is a kind of continuation to a previous blog post: https://techzealous.blogspot.bg/2018/04/peer-to-peer-connection-with-java-p2p_18.html
It can provide you with a convenient way to test connectivity between peers in a network without the need to create your own project if you only need a quick working example.
At the bottom part of the application we get a message log of what the application is doing or any exceptions that we get if the desired address and port combination is not available for use (it is already in use. After we close an address it is not made available by the OS for some time, so this has to be kept in mind as well).

GitHub repository of the application:
https://github.com/ektodorov/P2P_Sockets_Android

  (P2P Sockets Android application)

18 April 2018

Peer-to-Peer connection with Java (p2p)

Let us start with a bit of information on sockets.
In the case that we want to use the same port to connect and to accept incoming connections, we have to first start connecting to the remote host and then to start accepting incoming connections, because we can't bind to a Socket if it is already listening for connections, which is the case when we start accepting, and after that we won't be able to bind to the same port when we try to connect. Although on some systems it would be possible, it all depends on the sockets implementation in the OS.
As we can read in the Socket man page - http://www.skrenta.com/rt/man/socket.4.html :
"
SO_REUSEADDR
              Indicates   that   the  rules  used  in  validating
              addresses supplied in a bind(2) call  should  allow
              reuse  of local addresses. For PF_INET sockets this
              means that a socket may bind, except when there  is
              an  active  listening  socket bound to the address.
              When the listening socket is  bound  to  INADDR_ANY
              with  a  specific  port  then it is not possible to
              bind to this port for any local address.
"
More on this topic:

https://stackoverflow.com/questions/14388706/socket-options-so-
reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t/14388707#14388707

Now for the connections.

Connect:
We need to bind only using a port. When connecting the local address is usually "address any", that is why we only supply a port number. We are connecting to the public IP of the other machine (IP that is visible from the Internet) and we supply the IP address and the port number.

Socket socket = new Socket();
socket.setReuseAddress(true);
InetSocketAddress inetSocketAddress = new InetSocketAddress(44445);
socket.bind(inetSocketAddress);
InetSocketAddress socketAddress = new InetSocketAddress("11.222.333.555", 44446);
socket.connect(socketAddress, 30000);

Accept:
When accepting a connection we need to bind to the local IP address. If for some reason we don't get the actual local IP addres from getLocalHost (the OS may not allow us to have access to it and instead we either get the "address any" 0.0.0.0 or the loopback 127.0.0.1) we have to supply it ourselves:
InetAddress localAddress = InetAddress.getByName("192.168.100.10");

ServerSocket serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
InetAddress localAddress = InetAddress.getLocalHost();
InetSocketAddress socketAddress = new InetSocketAddress(localAddress, 44446);
serverSocket.bind(socketAddress);
Socket socket = serverSocket.accept();

If the clients are in the same LAN network or neither of them are behind a Firewall or a NAT, we can have one of them only accept incoming connection and the other one only connect and they will establish a connection. But if one or both of them are behind a Firewall / NAT we would have both of them connect and accept so that they punch holes in their Firewall / NAT so that the incoming connection can go through.
We have to remember that Peer-to-Peer connection is not guaranteed to succeed, because the hole punching may not be possible depending on the network setup that our hosts are in, either by accident or the network may have been setup by intention to prevent such connections on purpose. In that case we can have a relayed Peer-to-Peer communication between the two hosts - both connect to a server on the Internet and it relays the data between them. More on Peer-to-Peer communications over the Internet - http://www.brynosaurus.com/pub/net/p2pnat/

Connect and Accept part as Java methods and how to call them:

accepting("111.222.333.444", 44446);
connecting(44445, "111.222.333.555", 44446);
Of course each of these methods has to be called on a separate thread.


1:  public void accepting(String aHostLocal, int aPortLocal) {  
2:          System.out.println("accepting");  
3:          ServerSocket serverSocket = null;  
4:          OutputStream outputStream = null;  
5:          BufferedReader bufferedReader = null;  
6:          try {  
7:              serverSocket = new ServerSocket();  
8:              serverSocket.setReuseAddress(true);  
9:          
10:        System.out.println("accepting, bind to=" + aHostLocal);  
11:        InetSocketAddress socketAddress = new InetSocketAddress(aHostLocal, aPortLocal);  
12:        serverSocket.bind(socketAddress);  
13:          
14:        System.out.println("accepting, accept");  
15:        Socket socket = serverSocket.accept();  
16:          
17:        bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
18:        int character = bufferedReader.read();  
19:        System.out.println("accepting, read character=" + character);  
20:        outputStream = socket.getOutputStream();  
21:        outputStream.write(character);  
22:        outputStream.flush();  
23:      } catch (IOException e) {  
24:        e.printStackTrace();  
25:      } finally {  
26:          if(bufferedReader != null) {  
27:              try {  
28:                      bufferedReader.close();  
29:                  } catch (IOException e) {  
30:                      e.printStackTrace();  
31:                  }  
32:          }  
33:          if(outputStream != null) {  
34:              try {  
35:                      outputStream.close();  
36:                  } catch (IOException e) {  
37:                      e.printStackTrace();  
38:                  }  
39:          }  
40:          if(serverSocket != null) {  
41:              try {  
42:                      serverSocket.close();  
43:                  } catch (IOException e) {  
44:                      e.printStackTrace();  
45:                  }  
46:          }  
47:      }  
48:          System.out.println("accepting, end");  
49:      }  
50:        
51:      public void connecting(int aPortLocal, String aHostRemote, int aPortRemote) {  
52:          System.out.println("connecting");  
53:          Socket socket = null;  
54:          OutputStream outputStream = null;  
55:          BufferedReader bufferedReader = null;  
56:          try {              
57:              socket = new Socket();  
58:              socket.setReuseAddress(true);  
59:          
60:              if(aPortLocal > 0) {  
61:                  InetSocketAddress inetSocketAddress = new InetSocketAddress(aPortLocal);  
62:                  System.out.println("connecting, bind to=" + inetSocketAddress);  
63:                  socket.bind(inetSocketAddress);  
64:              }  
65:          
66:              System.out.println("connecting, mHostRemote=" + aHostRemote);  
67:              InetSocketAddress socketAddress = new InetSocketAddress(aHostRemote, aPortRemote);   
68:        System.out.println("connecting, connect");  
69:        socket.connect(socketAddress, 30000);  
70:          
71:        System.out.println("connecting, sending 10");  
72:        outputStream = socket.getOutputStream();  
73:        outputStream.write(10);  
74:        bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
75:        int character = bufferedReader.read();  
76:        System.out.println("connecting received character=" + character);  
77:        outputStream.flush();  
78:      } catch (IOException e) {  
79:        e.printStackTrace();  
80:      } finally {  
81:          if(bufferedReader != null) {  
82:              try {  
83:                      bufferedReader.close();  
84:                  } catch (IOException e) {  
85:                      e.printStackTrace();  
86:                  }  
87:          }  
88:          if(outputStream != null) {  
89:              try {  
90:                      outputStream.close();  
91:                  } catch (IOException e) {  
92:                      e.printStackTrace();  
93:                  }  
94:          }  
95:          if(socket != null) {  
96:              try {  
97:                      socket.close();  
98:                  } catch (IOException e) {  
99:                      e.printStackTrace();  
100:                  }  
101:          }  
102:      }  
103:          System.out.println("connecting, end");  
104:      }  

31 March 2018

Sentinel image capture software

I have updated the Sentinel project. It now has a Desktop application as well. You can checkout the project at GitHub:


Now you can directly download an application without the need to build the project yourself. There is a Downloads directory that has an Android application and a Desktop application for Windows, Mac OS and Linux.
As you can read in the readme of the project repository, for the Eclipse project we are not using Maven, because one of the libraries has issues resolving from Maven and instead of having some of them with Maven and that one not, we decided to go for manual dependency resolution for all of them.

(Sentinel for desktop)

The images or videos will be recorded in the current working directory of the application.

27 February 2018

Page security


Some time ago I noticed a problem with the security on some of the pages of a bank.
If we visit this address https://www.<blurred :)>.bg/bg/debitni-karti/page/10 , we see the result on image1 - green padlock and green text in the address bar of the browser, which informs us that the communication with this page is secured with an Extended Validation SSL certificate.

(image1)

If we continue browsing to the site and go to the pages for card products,  for example: https://www.<blurred :)>.bg/bg/page/3301#nasheto-predlojenie , we no longer see the green padlock and green text, but a gray padlock with a yellow triangle and exclamation mark. If we click on it we click on it it says that the connection to the page is not secure and the reason is that some of the content is not served over https - image2, image3, image4
(image2)

(image3)

 
 (image4)

If we look at the source code of the page we see that three of the images on that page are with URLs over http protocol. If we try to access these images we see that they are not available and that the server redirects us to their new location which is over https protocol. So the browser is making an unnecessary request over http just to get a response code 302 and a "Location" header with the new address of the image.
So the images have been removed from http and they are only accessible by https, but they have just forgotten to update some of the pages "src" attributes, and the browser is making an unnecessary http request.
(image5)


Needles to say I have informed the bank of their insecure pages and they have immediately solved the issue, of-course not missing to send a "Thanks" my way, which is always nice to hear.

I remember pointing out a problem with the e-commerce pages of a PC parts store, which prevented them from being loaded - the user had to manually reload the page if it was opened as a new tab. They fixed it after a week or so, but didn't even bother to say "Thank you". 

But here we see www.<blurred :)>.bg having a lightning fast reaction and this is what is expected when it comes to core business :)

17 February 2018

A * (A Star pathfinding)

    The repository contains a Java Eclipse project and Xcode iOS application project.
With the iOS application we can set the size of the maze and create it by touching on the squares to turn them from an empty walk able space into a wall square. We can set the start and end location and we can swipe on the map to scroll it in all directions if it can't fit on the screen, and we can pinch to zoom in and out in order to make the whole board visible or to make it easier to click on the board. Initially the size of the board is the number of squares that fit in the view size. We can set the number of squares we want the board to be in the "Width" and "Height" input fields and press on the "Create" button and we will get a new board with the desired number of squares.
    The Java implementation doesn't have a UI. We give it the maze in the ConstantsA class and we get the result in the console.


The start and end location are the squares with green color, the red colored squares are the walls and the yellow colored squares is the path.


Repository of the project at GitHub: