8 |
|
using System.IO; |
9 |
|
using System.Net; |
10 |
|
using System.Text; |
11 |
+ |
#if !_FreeBSD_ |
12 |
+ |
using System.Windows.Forms; |
13 |
+ |
#endif |
14 |
|
|
15 |
|
public class HostUpdateSharp |
16 |
|
{ |
17 |
|
public static void Main(string[] args) |
18 |
|
{ |
19 |
< |
#if _uname_ = "FreeBSD 4.9-STABLE i386" |
20 |
< |
String os = "FreeBSD 4.9-STABLE i386"; |
21 |
< |
#else |
22 |
< |
OperatingSystem os = Environment.OSVersion; |
23 |
< |
#endif |
24 |
< |
StringBuilder host = new StringBuilder("host=" + Dns.GetHostName()); |
19 |
> |
foreach (string arg in args) |
20 |
> |
{ |
21 |
> |
switch (arg) |
22 |
> |
{ |
23 |
> |
case "-D": |
24 |
> |
debug = true; |
25 |
> |
break; |
26 |
> |
default: |
27 |
> |
uname = arg; |
28 |
> |
break; |
29 |
> |
} |
30 |
> |
} |
31 |
> |
|
32 |
> |
try |
33 |
> |
{ |
34 |
> |
new HostUpdateSharp(); |
35 |
> |
} |
36 |
> |
catch (Exception exception) |
37 |
> |
{ |
38 |
> |
Console.Error.WriteLine(exception); |
39 |
> |
Environment.Exit(1); |
40 |
> |
} |
41 |
> |
} |
42 |
> |
public HostUpdateSharp() |
43 |
> |
{ |
44 |
> |
StringBuilder host = new StringBuilder("host=" + |
45 |
> |
Dns.GetHostName().ToLower()); |
46 |
|
string url = "http://topsecret.douglasthrift.net/auth/hostupdate.cgi"; |
47 |
|
|
48 |
< |
if (host.ToString().IndexOf('.') < 0) |
48 |
> |
if ((host + "").IndexOf('.') < 0) |
49 |
|
{ |
50 |
< |
host.Append(".local.douglasthrift.net"); |
50 |
> |
host.Append(".douglasthrift.net"); |
51 |
|
} |
52 |
|
|
53 |
< |
Console.WriteLine(host); |
54 |
< |
Console.WriteLine(os); |
53 |
> |
WebHeaderCollection headers = new WebHeaderCollection(); |
54 |
> |
|
55 |
> |
headers.Add("Authorization", "Basic " + Convert.ToBase64String((new |
56 |
> |
ASCIIEncoding()).GetBytes("HostUpdate:frell2003"))); |
57 |
|
|
58 |
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); |
59 |
|
|
60 |
|
request.Accept = "text/plain"; |
61 |
|
request.ContentLength = host.Length; |
62 |
|
request.ContentType = "application/x-www-form-urlencoded"; |
63 |
< |
request.Credentials = new NetworkCredential("HostUpdate", "frell2003"); |
63 |
> |
request.Headers = headers; |
64 |
|
request.KeepAlive = false; |
65 |
|
request.Method = "POST"; |
66 |
< |
request.PreAuthenticate = true; |
67 |
< |
request.UserAgent = "Host Update Sharp/1.0 (" + os + ")"; |
66 |
> |
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 |
> |
#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 |
> |
content.Close(); |
87 |
|
} |
88 |
+ |
private static bool debug = false; |
89 |
+ |
private static string uname = ""; |
90 |
+ |
#if _FreeBSD_ |
91 |
+ |
private static string platform() |
92 |
+ |
{ |
93 |
+ |
if (uname == "") |
94 |
+ |
{ |
95 |
+ |
uname = "Unknown"; |
96 |
+ |
} |
97 |
+ |
|
98 |
+ |
return uname; |
99 |
+ |
} |
100 |
+ |
#else |
101 |
+ |
private static string platform() |
102 |
+ |
{ |
103 |
+ |
if (uname == "") |
104 |
+ |
{ |
105 |
+ |
OperatingSystem os = Environment.OSVersion; |
106 |
+ |
string system = "Unknown"; |
107 |
+ |
|
108 |
+ |
switch (os.Platform) |
109 |
+ |
{ |
110 |
+ |
case PlatformID.Win32NT: |
111 |
+ |
system = "Windows NT"; |
112 |
+ |
break; |
113 |
+ |
case PlatformID.Win32Windows: |
114 |
+ |
system = "Windows"; |
115 |
+ |
break; |
116 |
+ |
} |
117 |
+ |
|
118 |
+ |
uname = system + ' ' + os.Version.Major + '.' + os.Version.Minor + |
119 |
+ |
" x86"; |
120 |
+ |
} |
121 |
+ |
|
122 |
+ |
return uname; |
123 |
+ |
} |
124 |
+ |
#endif |
125 |
|
} |