1 |
Douglas Thrift |
77 |
// Host Update Sharp |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
using System; |
8 |
|
|
using System.IO; |
9 |
|
|
using System.Net; |
10 |
Douglas Thrift |
82 |
using System.Text; |
11 |
Douglas Thrift |
98 |
#if !_FreeBSD_ |
12 |
|
|
using System.Windows.Forms; |
13 |
|
|
#endif |
14 |
Douglas Thrift |
77 |
|
15 |
Douglas Thrift |
78 |
public class HostUpdateSharp |
16 |
Douglas Thrift |
77 |
{ |
17 |
Douglas Thrift |
78 |
public static void Main(string[] args) |
18 |
Douglas Thrift |
77 |
{ |
19 |
Douglas Thrift |
98 |
foreach (string arg in args) |
20 |
|
|
{ |
21 |
|
|
switch (arg) |
22 |
|
|
{ |
23 |
|
|
case "-D": |
24 |
|
|
debug = true; |
25 |
|
|
break; |
26 |
Douglas Thrift |
100 |
default: |
27 |
|
|
uname = arg; |
28 |
|
|
break; |
29 |
Douglas Thrift |
98 |
} |
30 |
|
|
} |
31 |
|
|
|
32 |
Douglas Thrift |
90 |
try |
33 |
|
|
{ |
34 |
|
|
new HostUpdateSharp(); |
35 |
|
|
} |
36 |
Douglas Thrift |
108 |
catch (Exception exception) |
37 |
|
|
{ |
38 |
|
|
Console.Error.WriteLine(exception); |
39 |
|
|
Environment.Exit(1); |
40 |
|
|
} |
41 |
Douglas Thrift |
90 |
} |
42 |
|
|
public HostUpdateSharp() |
43 |
|
|
{ |
44 |
Douglas Thrift |
104 |
StringBuilder host = new StringBuilder("host=" + |
45 |
|
|
Dns.GetHostName().ToLower()); |
46 |
Douglas Thrift |
78 |
string url = "http://topsecret.douglasthrift.net/auth/hostupdate.cgi"; |
47 |
|
|
|
48 |
Douglas Thrift |
88 |
if ((host + "").IndexOf('.') < 0) |
49 |
Douglas Thrift |
82 |
{ |
50 |
|
|
host.Append(".local.douglasthrift.net"); |
51 |
|
|
} |
52 |
|
|
|
53 |
Douglas Thrift |
96 |
WebHeaderCollection headers = new WebHeaderCollection(); |
54 |
Douglas Thrift |
93 |
|
55 |
Douglas Thrift |
96 |
headers.Add("Authorization", "Basic " + Convert.ToBase64String((new |
56 |
|
|
ASCIIEncoding()).GetBytes("HostUpdate:frell2003"))); |
57 |
Douglas Thrift |
93 |
|
58 |
Douglas Thrift |
82 |
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); |
59 |
|
|
|
60 |
|
|
request.Accept = "text/plain"; |
61 |
Douglas Thrift |
83 |
request.ContentLength = host.Length; |
62 |
Douglas Thrift |
82 |
request.ContentType = "application/x-www-form-urlencoded"; |
63 |
Douglas Thrift |
96 |
request.Headers = headers; |
64 |
Douglas Thrift |
83 |
request.KeepAlive = false; |
65 |
Douglas Thrift |
82 |
request.Method = "POST"; |
66 |
Douglas Thrift |
88 |
request.UserAgent = "Host Update Sharp/1.0 (" + platform() + ')'; |
67 |
|
|
|
68 |
|
|
StreamWriter post = new StreamWriter(request.GetRequestStream(), |
69 |
|
|
Encoding.ASCII); |
70 |
|
|
|
71 |
|
|
post.Write(host); |
72 |
|
|
post.Close(); |
73 |
|
|
|
74 |
|
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); |
75 |
|
|
|
76 |
|
|
StreamReader content = new StreamReader(response.GetResponseStream(), |
77 |
|
|
Encoding.ASCII); |
78 |
|
|
|
79 |
Douglas Thrift |
98 |
#if _FreeBSD_ |
80 |
|
|
if (debug) Console.Write(content.ReadToEnd()); |
81 |
|
|
#else |
82 |
|
|
if (debug) MessageBox.Show(content.ReadToEnd(), "Host Update Sharp", |
83 |
|
|
MessageBoxButtons.OK, MessageBoxIcon.Information); |
84 |
|
|
#endif |
85 |
|
|
|
86 |
Douglas Thrift |
90 |
content.Close(); |
87 |
Douglas Thrift |
77 |
} |
88 |
Douglas Thrift |
98 |
private static bool debug = false; |
89 |
Douglas Thrift |
100 |
private static string uname = ""; |
90 |
Douglas Thrift |
88 |
#if _FreeBSD_ |
91 |
Douglas Thrift |
100 |
private static string platform() |
92 |
|
|
{ |
93 |
|
|
if (uname == "") |
94 |
|
|
{ |
95 |
Douglas Thrift |
106 |
uname = "Unknown"; |
96 |
Douglas Thrift |
100 |
} |
97 |
Douglas Thrift |
101 |
|
98 |
|
|
return uname; |
99 |
Douglas Thrift |
100 |
} |
100 |
Douglas Thrift |
88 |
#else |
101 |
Douglas Thrift |
100 |
private static string platform() |
102 |
Douglas Thrift |
88 |
{ |
103 |
Douglas Thrift |
100 |
if (uname == "") |
104 |
Douglas Thrift |
88 |
{ |
105 |
Douglas Thrift |
100 |
OperatingSystem os = Environment.OSVersion; |
106 |
|
|
string system = "Unknown"; |
107 |
Douglas Thrift |
88 |
|
108 |
Douglas Thrift |
100 |
switch (os.Platform) |
109 |
Douglas Thrift |
88 |
{ |
110 |
Douglas Thrift |
100 |
case PlatformID.Win32NT: |
111 |
|
|
system = "Windows NT"; |
112 |
Douglas Thrift |
88 |
break; |
113 |
Douglas Thrift |
100 |
case PlatformID.Win32Windows: |
114 |
|
|
system = "Windows"; |
115 |
|
|
break; |
116 |
Douglas Thrift |
88 |
} |
117 |
Douglas Thrift |
100 |
|
118 |
|
|
uname = system + ' ' + os.Version.Major + '.' + os.Version.Minor + |
119 |
|
|
" x86"; |
120 |
Douglas Thrift |
88 |
} |
121 |
|
|
|
122 |
Douglas Thrift |
100 |
return uname; |
123 |
Douglas Thrift |
88 |
} |
124 |
|
|
#endif |
125 |
Douglas Thrift |
77 |
} |