This repository was archived by the owner on Oct 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cs
More file actions
125 lines (113 loc) · 3.81 KB
/
Client.cs
File metadata and controls
125 lines (113 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Microsoft.Extensions.Options;
using System.Text;
namespace DDNSClient;
public sealed class Client
{
string User, Password, UserAgent, Hostname;
string? Ip;
readonly ILogger Logger;
readonly HttpClient HttpClient = new();
Result LastResult;
public Client(ILogger<Client> logger, IOptions<DDNSConfig> options)
{
Logger = logger;
// unpack the config options
User = options.Value.Username;
Password = options.Value.Password;
UserAgent = "DDNSClient/1.0 " + options.Value.Email;
Hostname = options.Value.Hostname;
// configure the HTTP client
HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Basic" + Convert.ToBase64String(Encoding.UTF8.GetBytes($"{User}:{Password}")));
HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", UserAgent);
}
public async Task<int> Initialize()
{
try
{
Ip = await GetCurrentIP();
PerformDNSUpdate(); // Force update on init to make sure everything works.
}
catch (HttpRequestException ex)
{
Logger.LogError("HTTP Error: {ExMessage}", ex.Message);
LastResult = Result.NetworkError;
}
return 0;
}
public async void Update()
{
string ip;
try
{
if (Ip == null && LastResult == Result.NetworkError)
{
Initialize(); // reforce init, since it didn't occur first time due to network errors
}
else
{
ip = await GetCurrentIP();
if (ip == Ip)
{
Logger.LogInformation("IP unchanged, waiting five minutes.");
LastResult = Result.NoChange;
}
else
{
Ip = ip;
Logger.LogInformation("IP change detected. Updating DNS entry.");
PerformDNSUpdate();
}
}
}
catch (HttpRequestException ex)
{
Logger.LogError("Error updating IP. {ExMessage}", ex.Message);
LastResult = Result.NetworkError;
}
}
/// <summary>
/// Send a Dynamic DNS update, using the Ip field's value, and update the LastResult field.
/// </summary>
async void PerformDNSUpdate()
{
// Google DNS update URI pattern
// https: //username:password@domains.google.com/nic/update?hostname=subdomain.yourdomain.com&myip=1.2.3.4
string reqURI = $"https://{User}:{Password}@domains.google.com/nic/update?hostname={Hostname}&myip={Ip}";
string retCode = await HttpClient.GetStringAsync(reqURI);
if (retCode.StartsWith("good"))
{
Logger.LogInformation("DNS update successful. Return code: ", retCode);
LastResult = Result.Success;
}
else if (retCode.StartsWith("nochg"))
{
Logger.LogInformation("No change in DNS record. Return code: ", retCode);
LastResult = Result.NoChange;
}
else
{
Logger.LogError("Error with DNS update request: {retCode} \nExiting...", retCode);
LastResult = Result.Failed;
Environment.Exit(2); // Some inherent problem with DNS updates, that must be fixed before retrying.
}
}
async Task<string> GetCurrentIP()
{
// TODO make ip detection service user configurable
return await HttpClient.GetStringAsync("https://domains.google.com/checkip");
}
}
public class DDNSConfig
{
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string Hostname { get; set; }
}
enum Result
{
Success,
NoChange,
Failed,
NetworkError
}