Last active
June 6, 2023 09:20
-
-
Save rkttu/8fae3d68b422de7fa866 to your computer and use it in GitHub Desktop.
IPtime router reboot C# application code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can compile this code with csc.exe or any C# supported shell. | |
/* | |
* Copyright (c) 2016 ClouDeveloper | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining | |
* a copy of this software and associated documentation files (the "Software"), | |
* to deal in the Software without restriction, including without limitation | |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
* and/or sell copies of the Software, and to permit persons to whom the Software | |
* is furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE | |
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
* */ | |
using System; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace iptimeReboot | |
{ | |
internal static class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
try | |
{ | |
Console.WriteLine("IPtime reboot utility, v1.0"); | |
Console.WriteLine("(c) ClouDeveloper, All rights reserved."); | |
string targetHost = null; | |
string userId = null; | |
string userPwd = null; | |
if (args.Length < 3) | |
{ | |
Console.WriteLine("This command line tool also accepts three parameters."); | |
Console.WriteLine("args: iptimeReboot.exe <host address> <admin id> <admin password>"); | |
Console.WriteLine(); | |
Console.Write("Target IPtime device IP: "); | |
targetHost = Console.ReadLine(); | |
Console.Write("Administrator ID: "); | |
userId = Console.ReadLine(); | |
Console.Write("Password: "); | |
userPwd = Console.ReadLine(); | |
} | |
else | |
{ | |
targetHost = args.ElementAtOrDefault(0); | |
userId = args.ElementAtOrDefault(1); | |
userPwd = args.ElementAtOrDefault(2); | |
} | |
if (String.IsNullOrWhiteSpace(targetHost)) | |
{ | |
Console.Error.WriteLine("Target IPtime device IP address is required."); | |
return; | |
} | |
IPAddress addr; | |
if (!IPAddress.TryParse(targetHost, out addr)) | |
{ | |
Console.Error.WriteLine("This program does not accept FQDN or host alias. Please specify IP adress only."); | |
return; | |
} | |
byte[] addrBytes = addr.GetAddressBytes(); | |
if (addrBytes.Length != 4) | |
{ | |
Console.Error.WriteLine("Sorry for inconvenience; This program supports IPv4 notation only."); | |
return; | |
} | |
if (addrBytes[0] == 10 || (addrBytes[0] == 172 && (addrBytes[1] >= 16 || addrBytes[1] <= 31)) || (addrBytes[0] == 192 && addrBytes[1] == 168)) | |
{ | |
Console.Error.WriteLine("This program does not intended to control remote IPtime device. Please use this program in your local area network only."); | |
return; | |
} | |
if (String.IsNullOrWhiteSpace(userId)) | |
{ | |
Console.Error.WriteLine("This program does not intended to control unsecured IPtime device. Please setup your IPtime device's administration credential and try again."); | |
return; | |
} | |
if (String.IsNullOrWhiteSpace(userPwd)) | |
{ | |
Console.Error.WriteLine("This program does not intended to control unsecured IPtime device. Please setup your IPtime device's administration credential and try again."); | |
return; | |
} | |
UriBuilder targetUriBuilder = new UriBuilder() | |
{ | |
Scheme = Uri.UriSchemeHttp, | |
Host = targetHost, | |
Port = 80, | |
Path = "/do_cmd.htm" | |
}; | |
UriBuilder pingUriBuilder = new UriBuilder() | |
{ | |
Scheme = targetUriBuilder.Scheme, | |
Host = targetUriBuilder.Host, | |
Port = targetUriBuilder.Port, | |
Path = "/" | |
}; | |
UriBuilder refererUriBuilder = new UriBuilder() | |
{ | |
Scheme = targetUriBuilder.Scheme, | |
Host = targetUriBuilder.Host, | |
Port = targetUriBuilder.Port, | |
Path = "/sysconf_misc.html" | |
}; | |
HttpClient client = new HttpClient() { Timeout = TimeSpan.FromMinutes(1d) }; | |
HttpRequestMessage targetRequest = new HttpRequestMessage(HttpMethod.Post, targetUriBuilder.Uri); | |
targetRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html")); | |
targetRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xhtml+xml")); | |
targetRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("image/jxr")); | |
targetRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); | |
targetRequest.Headers.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"); | |
targetRequest.Headers.Referrer = refererUriBuilder.Uri; | |
targetRequest.Headers.AcceptEncoding.TryParseAdd("gzip"); | |
targetRequest.Headers.AcceptEncoding.TryParseAdd("deflate"); | |
targetRequest.Headers.Connection.Add("Keep-Alive"); | |
targetRequest.Headers.Pragma.Add(new NameValueHeaderValue("no-cache")); | |
targetRequest.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Concat(userId, ":", userPwd)))); | |
Console.WriteLine("Sending restart request to {0}. Please wait a minute.", targetHost); | |
client.SendAsync(targetRequest).ContinueWith(x => | |
{ | |
switch (x.Status) | |
{ | |
case TaskStatus.Canceled: | |
case TaskStatus.RanToCompletion: | |
Console.WriteLine("Check that your IPTime device is restarted successfully."); | |
break; | |
case TaskStatus.Faulted: | |
Exception actualException = x.Exception; | |
if (actualException is AggregateException) | |
actualException = ((AggregateException)x.Exception).InnerException; | |
Console.Error.WriteLine("Request has been failed. Reason: {0}", x.Exception.Message); | |
break; | |
default: | |
Console.WriteLine(x.Status.ToString()); | |
break; | |
} | |
}).Wait(); | |
Console.ReadLine(); | |
} | |
catch (Exception ex) | |
{ | |
Console.Error.WriteLine("Unexpected error occurred: {0}", ex.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment