|
10 | 10 | import java.io.InputStreamReader; |
11 | 11 | import java.io.OutputStream; |
12 | 12 | import java.io.PrintStream; |
| 13 | +import java.io.PrintWriter; |
13 | 14 | import java.io.StringWriter; |
14 | 15 | import java.lang.Thread.UncaughtExceptionHandler; |
15 | 16 | import java.net.URL; |
|
19 | 20 | import java.util.TimerTask; |
20 | 21 |
|
21 | 22 | public class DnsMadeEasy { |
| 23 | + static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); |
| 24 | + |
22 | 25 | String user = "", pass = "", id = "", lastIP = ""; |
23 | 26 | int minutes = 30; |
24 | 27 | final File configFile = new File(System.getProperty("user.home"), ".dnsmadeeasy/config.txt"); |
25 | | - final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); |
26 | 28 |
|
27 | 29 | public DnsMadeEasy () throws IOException { |
28 | 30 | loadConfig(); |
29 | 31 |
|
| 32 | + log("Started.", null); |
30 | 33 | new Timer("Timer").schedule(new TimerTask() { |
31 | 34 | public void run () { |
32 | | - System.out.print(dateFormat.format(new Date()) + ", Started."); |
33 | 35 | try { |
34 | 36 | update(user, pass, id); |
35 | 37 | } catch (IOException ex) { |
36 | 38 | ex.printStackTrace(); |
37 | 39 | } |
38 | 40 | } |
39 | 41 | }, 0, minutes * 60 * 1000); |
40 | | - |
41 | | - // Don't return so service isn't terminated. |
42 | | - while (true) { |
43 | | - synchronized (this) { |
44 | | - try { |
45 | | - wait(); |
46 | | - } catch (InterruptedException ignored) { |
47 | | - } |
48 | | - } |
49 | | - } |
50 | 42 | } |
51 | 43 |
|
52 | 44 | void update (String user, String pass, String id) throws IOException { |
53 | | - String newIP = read("http://www.dnsmadeeasy.com/myip.jsp"); |
| 45 | + String newIP; |
| 46 | + try { |
| 47 | + newIP = http("http://www.dnsmadeeasy.com/myip.jsp"); |
| 48 | + } catch (IOException ex) { |
| 49 | + log("Error obtaining IP.", ex); |
| 50 | + return; |
| 51 | + } |
54 | 52 | if (newIP.equals(lastIP)) return; |
55 | 53 |
|
56 | | - System.out.print(dateFormat.format(new Date()) + ", " + newIP + ", "); |
57 | | - String result = read("http://www.dnsmadeeasy.com/servlet/updateip?username=" + user + "&password=" + pass + "&id=" + id |
| 54 | + String result = http("http://www.dnsmadeeasy.com/servlet/updateip?username=" + user + "&password=" + pass + "&id=" + id |
58 | 55 | + "&ip=" + newIP); |
59 | | - System.out.println(result); |
| 56 | + log(newIP + ", " + result, null); |
60 | 57 | if (result.equals("success")) { |
61 | 58 | lastIP = newIP; |
62 | 59 | saveConfig(); |
63 | 60 | } |
64 | 61 | } |
65 | 62 |
|
66 | | - String read (String url) throws IOException { |
| 63 | + String http (String url) throws IOException { |
67 | 64 | InputStreamReader reader = new InputStreamReader(new URL(url).openStream()); |
68 | 65 | StringWriter writer = new StringWriter(128); |
69 | 66 | char[] buffer = new char[1024]; |
@@ -134,22 +131,29 @@ public void write (byte[] b, int off, int len) throws IOException { |
134 | 131 | } |
135 | 132 | } |
136 | 133 |
|
137 | | - static public void main (final String[] args) throws Exception { |
| 134 | + static void log (String message, Throwable ex) { |
| 135 | + if (ex != null) { |
| 136 | + StringWriter buffer = new StringWriter(1024); |
| 137 | + ex.printStackTrace(new PrintWriter(buffer)); |
| 138 | + message += "\n" + buffer.toString(); |
| 139 | + } |
| 140 | + System.out.println(dateFormat.format(new Date()) + " " + message); |
| 141 | + } |
| 142 | + |
| 143 | + static public void main (String[] args) throws Exception { |
138 | 144 | try { |
139 | 145 | File dir = new File(System.getProperty("user.home"), ".dnsmadeeasy"); |
140 | 146 | dir.mkdirs(); |
141 | 147 | FileOutputStream logFile = new FileOutputStream(new File(dir, "dnsmadeeasy.log")); |
142 | 148 | System.setOut(new PrintStream(new MultiplexOutputStream(System.out, logFile), true)); |
143 | 149 | System.setErr(new PrintStream(new MultiplexOutputStream(System.err, logFile), true)); |
144 | 150 | } catch (Throwable ex) { |
145 | | - System.out.println("Unable to write log file."); |
146 | | - ex.printStackTrace(); |
| 151 | + log("Unable to write log file.", ex); |
147 | 152 | } |
148 | 153 |
|
149 | 154 | Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { |
150 | 155 | public void uncaughtException (Thread thread, Throwable ex) { |
151 | | - ex.printStackTrace(); |
152 | | - System.out.println("Uncaught exception, exiting."); |
| 156 | + log("Uncaught exception, exiting.", ex); |
153 | 157 | System.exit(0); |
154 | 158 | } |
155 | 159 | }); |
|
0 commit comments