Remote wake on computer (WOL) function based on Java

Source: Internet
Author: User
Tags base64

Wake on the network, WOL. Simply speaking, the computer is in the shutdown state, you can send special packets to the network card via the Internet, the network card after receiving the specified package, turn on the computer. WOL requires hardware to support this feature, the current market mainstream Ethernet card support WOL function, and wireless network card to find a number of not found to support the function of the wireless card.

I have successfully implemented the wake on LAN function at home, but if I need to operate my home computer at the company, and the wake-up network is based on the local area, I cannot. So I think of the network through the Access home router IP address implementation, but the home network IP address is changed, every time the router will be replaced, you can never restart the router every time you have to remember the network IP address it.

To solve this problem, I thought of the peanut shell. The home route is Tp-link, integrates the peanut Shell's DDNS function, after using discovers the cell broadband through the N heavy route, the peanut shell obtains the network IP address is not correct, tangled. After several twists and turns found can set a record for the peanut shell, specify the IP of the domain name, while the peanut shell also provides an interface to modify a record, so I came up with an idea: each time the router restarts, get the router network IP address, and then through the peanut shell interface to modify the domain a record, So the outside network can send the network wake-up package through the domain name, realize remote boot, the idea is how tall ah, but the process is very difficult!


Too much nonsense, let us first understand the peanut Shell Open Interface bar, I use is.

Address: http://open.oray.com/wiki/doku.php

Look at the documentation we learned that the main sub-DDNS protocol and HTTP protocol two. The DDNS protocol needs to keep the heartbeat packet, that is, sending the corresponding data to the peanut shells every time, obviously not what we want, so I choose the HTTP protocol.

The official HTTP protocol is as follows:

The client should update when the client discovers an IP address change or when the user modifies the settings.

All updates are basic to standard HTTP request delivery.

The server returns a return code that the client needs to parse.

HTTP request

Host Name: ddns.oray.com

HTTP port: 80

HTTPS Port: 443

Request support for HTTP and SSL-based HTTPS protocols (HTTPS requires a paying user to use)

All clients must send a complete user-agent file header to distinguish between different devices, null values, or illegal parameters that will cause the request to fail.

Example

1. Using URL validation

For browsers or applications (fetch, curl, lwp-request), you can include validation information in the URL.

Http://username:[email protected]/ph/update?hostname=yourhostname&myip=ipaddress

2. Raw HTTP GET request

The actual HTTP request, similar to the following code.

Where base-64-authorization use Base64 to encrypt the Username:password after the character substitution.

Get/ph/update?hostname=yourhostname&myip=ipaddress http/1.0

Host:ddns.oray.com

Authorization:basic base-64-authorization

User-agent:oray

Note that a GET request must be used and the post is not allowed.

Update parameters

Currently only allow the following parameters to be submitted

Parameter description

Hostname need to update the domain name, this domain name must be opened Peanut shell service. Multiple domain names are used, separated, and default is empty, then all active domain names under the passport are updated. Example: hostname=test.oray.com,customtest.oray.com

Myip need to update the IP address, can not fill. If not specified, the IP address obtained by the server will prevail.


At first I looked at this document I was foggy, after a few toss finally figured out.

1. Send HTTP requests to http://ddns.oray.com/ph/update?hostname=yourhostname&myip= in Get mode IPAddress

2.hostname value is the domain name to be modified

3. TheMyip value is the IP address to be modified, which is the public IP address of the router in the home

4. Request Header User-agent for browser model

5. Request Header Authorization for peanut Shell login name: Password Encrypt content with BASE64 method

For example, the account number is: 1234, the password is 12345, that is, 1234:12,345 BASE64 encryption result is: mtizndoxmjm0nq==

The question behind is how to get the public IP, found http://www.ip138.com/this site to get the public network IP address and router is consistent, and then see http://www.ip138.com/is actually through http://1111. Ip138.com/ic.as interface Get method, then only need to read the http://1111.ip138.com/ic.asp IP address on it.

package smile.heyi.html;import java.io.bufferedreader;import java.io.ioexception;import  java.io.inputstreamreader;import java.net.*;/** *  get public IP address  * */public class  GetInternetIP {private final static String url =  "http://1111. Ip138.com/ic.asp ";p ublic static string getip () {string ip = " "; String html = gethtml (); Int start = html.indexof ("[") +1;int end =  html.indexof ("]");//intercept IP address string ip = html.substring (start, end); return ip;} Private static string gethtml () {string s =  "";try {httpurlconnection  conn =  (httpurlconnection)   (New url (URL). OpenConnection ()); Conn.setrequestproperty (" User-agent ", " mozilla/5.0  (windows nt 5.1; rv:35.0)  gecko/20100101 firefox/ 35.0 "); Conn.setrequestmethod (" GET "); bufferedreader bf =&Nbsp;new bufferedreader (New inputstreamreader (Conn.getinputstream (), "GBK")); string tmp =  ""; while ((Tmp = bf.readline ())  != null) {s += tmp+ "\ r \ n ";}}  catch  (ioexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();} Return s;}}

under the interface that calls the peanut shell

package smile.heyi.html;import java.io.bufferedreader;import java.io.ioexception;import  java.io.inputstreamreader;import java.net.httpurlconnection;import java.net.url;import  smile.heyi.util.base64;/** *  through the peanut shell interface, modify the domain A record  * */public class changeddns { private string url =  "Http://ddns.oray.com/ph/update";p rivate string loginname  =  ";p rivate string password = " ";p Ublic changeddns (String name,  STRING PW) {THIS.LOGINNAME = NAME;THIS.PASSWORD = PW;} Public string change (STRING DOMAINNAME, STRING IP) {string s =  ""; URL  +=  "? hostname=" +domainname+ "&myip=" +ip;try {httpurlconnection conn =  ( HttpURLConnection)   (New url (URL). OpenConnection ()); Conn.setrequestproperty ("Host",  " Ddns.oray.com ");//simulate access to Conn.setrequestproperty as a Firefox browser (" User-agent ",  "mozilla/5.0  (windows nt 5.1; rv:35.0)  gecko/20100101 firefox/35.0");//To   Account: Password   BASE64 encryption result identity login Conn.setrequestproperty ("Authorization",  "basic " +base64.getbase64 ( Loginname+ ":" +password)); Conn.setrequestproperty ("Referrer",  url); Bufferedreader bf = new bufferedreader (New inputstreamreader (Conn.getInputStream ())); string tmp =  ""; while ((Tmp = bf.readline ())  != null) {s += tmp;}}  catch  (ioexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();} Return s;}}

All that's left is BASE64 encrypted login information

Note: The online look at the algorithm, too complex did not understand, from the online copy. This use of the Sun.misc.BASE64Decoder and Sun.misc.BASE64Encoder two packages, but eclipse will not be found, need to remove the JRE System library in the project and then re-add before you can, The reason seems to be because the two packages are sun employees themselves with the internal and not released (see the name will feel strange not to start with Java), does not guarantee that no problem, so still hope that the master himself to write it.

Package Smile.heyi.util;import Java.io.ioexception;import Sun.misc.BASE64Decoder; Import sun.misc.base64encoder;/** * Base64 + decrypt * */public class Base64 {public static String getBASE64 (string s) {return (NE W Base64encoder ()). Encode (S.getbytes ());} public static string deCodeBASE64 (String key) throws ioexception{byte[] B = (new Base64decoder ()). Decodebuffer (key); return new String (b);}}


The final question is how to call it when the router restarts, to perform these operations.

We cannot monitor whether the router is restarted, but can monitor the status of the computer card. Each time the router restarts (powered on), the network card loses Internet connectivity and then replies to an Internet connection. Then we can do a scheduled task, each time the network card connection event, the delay of 10 minutes or so to execute the above Java code, the delay is to prevent the network connection does not reply to get the network IP address.

This article is from the "Listen to the Rain Xuan" blog, please be sure to keep this source http://heyijx2.blog.51cto.com/925730/1613583

Remote wake on computer (WOL) function based on Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.