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 |
+ |
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) { Console.Error.WriteLine(exception); } |
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=" + Dns.GetHostName()); |
44 |
> |
StringBuilder host = new StringBuilder("host=" + |
45 |
> |
Dns.GetHostName().ToLower()); |
46 |
|
string url = "http://topsecret.douglasthrift.net/auth/hostupdate.cgi"; |
47 |
|
|
48 |
|
if ((host + "").IndexOf('.') < 0) |
49 |
|
{ |
50 |
< |
host.Append(".local.douglasthrift.net"); |
50 |
> |
host.Append(".douglasthrift.net"); |
51 |
|
} |
52 |
|
|
53 |
< |
CredentialCache cache = new CredentialCache(); |
53 |
> |
WebHeaderCollection headers = new WebHeaderCollection(); |
54 |
|
|
55 |
< |
cache.Add(new Uri(url), "Basic", new NetworkCredential("HostUpdate", |
56 |
< |
"frell2003")); |
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 = cache; |
63 |
> |
request.Headers = headers; |
64 |
|
request.KeepAlive = false; |
65 |
|
request.Method = "POST"; |
45 |
– |
request.PreAuthenticate = true; |
66 |
|
request.UserAgent = "Host Update Sharp/1.0 (" + platform() + ')'; |
67 |
|
|
68 |
|
StreamWriter post = new StreamWriter(request.GetRequestStream(), |
76 |
|
StreamReader content = new StreamReader(response.GetResponseStream(), |
77 |
|
Encoding.ASCII); |
78 |
|
|
79 |
< |
Console.Write(content.ReadToEnd()); |
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 string platform() { return "FreeBSD 4.9-STABLE i386"; } |
64 |
< |
#else |
65 |
< |
private string platform() |
91 |
> |
private static string platform() |
92 |
|
{ |
93 |
< |
OperatingSystem os = Environment.OSVersion; |
68 |
< |
string system = "Unknown"; |
69 |
< |
|
70 |
< |
switch (os.Platform) |
93 |
> |
if (uname == "") |
94 |
|
{ |
95 |
< |
case PlatformID.Win32NT: |
73 |
< |
system = "Windows NT"; |
74 |
< |
break; |
75 |
< |
case PlatformID.Win32Windows: |
76 |
< |
system = "Windows"; |
77 |
< |
break; |
95 |
> |
uname = "Unknown"; |
96 |
|
} |
97 |
|
|
98 |
< |
int major = os.Version.Major, minor = os.Version.Minor; |
99 |
< |
string version = os + ""; |
100 |
< |
|
101 |
< |
switch (system) |
98 |
> |
return uname; |
99 |
> |
} |
100 |
> |
#else |
101 |
> |
private static string platform() |
102 |
> |
{ |
103 |
> |
if (uname == "") |
104 |
|
{ |
105 |
< |
case "Windows NT": |
106 |
< |
switch (major) |
105 |
> |
OperatingSystem os = Environment.OSVersion; |
106 |
> |
string system = "Unknown"; |
107 |
> |
|
108 |
> |
switch (os.Platform) |
109 |
|
{ |
110 |
< |
case 5: |
111 |
< |
switch (minor) |
112 |
< |
{ |
113 |
< |
case 0: |
114 |
< |
version = "Windows 2000"; |
93 |
< |
break; |
94 |
< |
case 1: |
95 |
< |
version = "Windows XP"; |
96 |
< |
break; |
97 |
< |
case 2: |
98 |
< |
version = "Windows .NET Server"; |
99 |
< |
break; |
100 |
< |
} |
110 |
> |
case PlatformID.Win32NT: |
111 |
> |
system = "Windows NT"; |
112 |
> |
break; |
113 |
> |
case PlatformID.Win32Windows: |
114 |
> |
system = "Windows"; |
115 |
|
break; |
116 |
|
} |
117 |
< |
break; |
118 |
< |
case "Windows": |
119 |
< |
if (major == 4) |
106 |
< |
{ |
107 |
< |
switch (minor) |
108 |
< |
{ |
109 |
< |
case 10: |
110 |
< |
version = " Windows 98"; |
111 |
< |
break; |
112 |
< |
case 90: |
113 |
< |
version = " Windows ME"; |
114 |
< |
break; |
115 |
< |
} |
116 |
< |
} |
117 |
< |
break; |
117 |
> |
|
118 |
> |
uname = system + ' ' + os.Version.Major + '.' + os.Version.Minor + |
119 |
> |
" x86"; |
120 |
|
} |
121 |
|
|
122 |
< |
return system + ' ' + major + '.' + minor + " [" + version + "] ix86"; |
122 |
> |
return uname; |
123 |
|
} |
124 |
|
#endif |
125 |
|
} |