Embedded Networking Smart Bell -- Backstage jiva program design

Technical design 2019/08/30
← Return to technical article

Subject content and requirements: Content: Design and prepare server requirements for a smart web clock: 1, clocks are distributed around the world and connected to servers, from servers...

[[heading]]

[[/heading]]

  • Topic content and requirements:

Content: Design and design of a smart web clock server

Requires: 1 clock to be distributed around the world and connected to the server, UTC time from the server, which automatically converts to local time zone time according to the IP address of the clock.

2 Clocks may be pirated and the server can judge according to database ID, if the ID is not in the database, judged as a replica, given instructions, and notified of the clock automatic destruction program.

3 The server needs to be able to get a standard time through the NTP protocol, but the NTP should be a list that does not fail because a NTP server cannot access it. Of course, NTP time fixes can be done once a day.

4 Server interfaces can use TCP socket, HTTP or json interfaces. However, load issues need to be considered. Given the greater number of servers connected, the connection that has been successfully sent out should be automatically disconnected to ensure that it does not result in the simultaneous overloading of resources.

Servers should consider hacking, which should be judged if a large number of requests are automatically made by the hacker program, and filter the corresponding IP address. Of course, for a distributed attack, the factory product database needs to be judged and the IP address filtered if the ID number is not in this location.

Main design ideas:

  • Conversion time: you can import an IP database and get information from the network on the geographic location of the current IP.

My approach is to retrieve the longitude of the current IP address from the web site API-connected with ip addresses at latitude, and then calculate the time zone by code and convert local time according to the current time zone.

  • Detection of piracy: My practice is to compare the current ID with the database before obtaining geographic information from the API, and to automatically delete the program if, in the database, the API is connected and returned to the local IP and recorded with the new IP and Time Zone, and if the current ID does not activate the lower command 19bitid+5A in the database, when the next machine receives it.
  • Socket tcp connection: My approach is to set up a socket tcpserver that listens to port number: 9999; automatically compares the ID number in the database to local time if the current ID exists, depending on the IP address taken from the read-out package. Then send it down. Once you hear the client's package, the offline server automatically closes the link.

If not available, do not send time and close the link.

  • Whether or not it's a malicious connection: my approach is to connect the database for ID matching, if the ID does not exist, record the number of IP requests, if more than 10, filter the IP address, if the ID exists, if more than 10, filter the IP address temporarily.

Design process:

1. Checking for piracy and obtaining time zones:

  • The clock can get local time from everywhere, set a timer, and automatically pick up the corresponding IP time once a day
  • Detection of pirated copies and, if identified as pirated, issuance of a directive to the lower plane destruction procedure.

Huoqushiqu class method

Function

Main()

Performs the function of the main function, comparing the database with the time zone obtained through longitude.

getAddresses()

Obtain geographical location information from external API

getResult()

Connects to API, sets the parameters to connect to API.

shujuku()

Created on a database, connect to ID if ID exists to return to local time, and if not, issue command 19-bitid+5A

Data flow chart:

The geographic information code from API is as follows:

public String getAddresses(String content, String encodingString)

throws UnsupportedEncodingException {

String urlStr = String .format("https://ip.seeip.org/geoip/%s",content);

// Obtain the city information where the IP is located

String returnStr = this.getResult(urlStr, content, encodingString);

if (returnStr != null) {

// Process the returned city information

return returnStr;

The connection API part code is as follows:

private String getResult(String urlStr, String content, String encoding) {

URL url = null;

HttpURLConnection connection = null;

try {

url = new URL(urlStr);

Connection = (HtpURLConnect) url.openConnect();/ / New Connection Example

contact. setConnectTimeout(5000);// Set the connection timeout, in milliseconds

Connection.setReadTimeout (5000);// Set timeout for reading data, in milliseconds

condition. setDooutput(true);/ / Whether to open output stream True|false

condition. setDoInput(tru);// Whether to open inputstreamtruefalse

Section.setRequestMethod (“POST”);// Submission Method POST|GET

condition. setUseCaches(false);// Whether or not to cache

connect.connect();//open connect port

DataOutputStream out = new DataOutputStream(connection

.getoutputStream());//open output flow to the opposite server for writing data

Out.writeBytes;// Write data

Out. flush();// refresh

Out.close();// close output stream

BufferedReader reader = new BufferedReader(new InputStreamReader(

Connection.getInputStream(), encoding);//Return data to the data-to-end server after writing to the other side

/ , read in Buffered Reader Stream

StringBuffer buffer = new StringBuffer();

String line = "";

while ((line = reader.readLine()) != null) {

buffer.append(line);

}

reader.close();

return buffer.toString();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (connection != null) {

Contact.disconnect();// close the connection

}

}

return null;

}

2. Prevent server overload:

  • Server interfaces can be connected using the TCP socket interface and, in the event of excessive access, can automatically disconnect connections that have been successfully distributed to ensure that they are not overloaded.

Data flow chart:

3. Hacker attack prevention part:

5 If the current IP has multiple accesses to the server to determine if the current IP is in the database, if the current IP is temporarily shielded in the database, and if the current IP is not in the database, the server filters the current IP without a session.

Data flow chart:

Code test and results:

I. Acquisition of time portion code according to IP:

huoqushiqu huoqu = new huoqushiqu();

String ip = "169.235.24.133";

String address = "";

String ID ;

ID = "0123456789abcdf0102";//15 character serial numbers, 2 character request codes, 2 character working modes

SQL sjk =new SQL();

String a="";

int f = sjk.shujuku(ID,ip,a);

if(f==1) {

address = huoqu.getAddresses(ip, "utf-8");

String Jingdu=StringUtils.substilingBetween (adcess, "longitude":), ",";//get current IP longitude

System.out.println(jingdu);

int c;

c=Double.valueof(jingdu).intValue();//converting time zone through longitude

int d;

d=c%15;

c=c/15;

if(d>=7.5)

{c++;

}

if(c>0) {

a="GMT+"+c;

}

else

{

a="GMT"+c;

}

sjk.shujuku(ID,ip,a);

TimeZone tz=TimeZone.getTimeZone(a);

DateFormat df=DateFormat.getDateTimeInstance();

df.setTimeZone(tz);

System.out.println ( "Current IP Location:"+df.format(new Date());