-
Notifications
You must be signed in to change notification settings - Fork 0
/
Iminetsoft.Godaddy.cs
164 lines (145 loc) · 8.29 KB
/
Iminetsoft.Godaddy.cs
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Net;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Reflection;
namespace Iminetsoft
{
/// <summary>
/// Simple and fast GoDaddy (godaddy.com) API
/// You might need a developer key and secret code first! In this case, visit https://developer.godaddy.com/keys
/// <description>Developed and maintained by Iminetsoft | Version: 0.1 | Date: 17.01.2021</description>
/// </summary>
public class Godaddy
{
public enum RecordTypes { A, Nameserver, CNAME, MX, TXT, SRV, AAA, CAA }
public enum StatusCodes { OK=0, EmptyResponse = 1, RequestMalformed=400, AuthInvalid=401, Unauthorized = 403, NotFound=404, ProcessInvalid=422, ProcessTimeout=429, InternalError=500, GatewayTimeout=504 }
public string Domain { get; set; } = String.Empty;
public RecordTypes Type { get; set; } = RecordTypes.A;
public string Name { get; set; } = String.Empty;
public int Ttl { get; set; } = 3600;
public int Port { get; set; } = 1;
public int Weight { get; set; } = 0;
public string Key { private get; set; } = String.Empty;
public string Secret { private get; set; } = String.Empty;
public string Data { get; set; } = String.Empty;
public int Priority { get; set; } = 0;
public string UserAgent { get; set; } = "Iminetsoft.Godaddy.Net";
public Dictionary<string, string> ResponseHeaders { get; private set; } = new Dictionary<string, string>();
public string ExceptionMessage { private set; get; } = String.Empty;
public StatusCodes StatusCode = StatusCodes.EmptyResponse;
public Godaddy(){ }
public Godaddy(string domain, string name, RecordTypes type, int ttl, string key, string secret)
{
Domain = (domain.Length > 0 ? domain.Trim() : Domain);
Name = (name.Length > 0 ? name.Trim() : Name);
Type = type;
Ttl = (ttl >= 10 && ttl <=3600 ? ttl : Ttl);
Key = (key.Length > 0 ? key.Trim() : Key);
Secret = (secret.Length > 0 ? secret.Trim() : Secret);
}
/// <summary>
/// Gets the current data of the specified domain and DNS record
/// </summary>
/// <returns></returns>
public bool GetDnsRecord()
{
GetSetDnsRecord();
return (StatusCode == StatusCodes.OK ? true : false);
}
/// <summary>
/// Gets the current IP/hostname of the specified domain and DNS record, typed A
/// </summary>
/// <returns></returns>
public string GetDnsRecordString()
{
GetSetDnsRecord();
return (StatusCode == StatusCodes.OK ? Data : "");
}
/// <summary>
/// Creates or updates the specified DNS record
/// </summary>
/// <returns></returns>
public bool SetDnsRecord()
{
GetSetDnsRecord(true);
return (StatusCode == StatusCodes.OK ? true : false);
}
private void GetSetDnsRecord(bool UpdateData = false)
{
string HtmlContent = String.Empty;
string apiurl = $"https://api.godaddy.com/v1/domains/{Domain}/records/{Type.ToString()}/{Name}";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest webreq = (HttpWebRequest)System.Net.WebRequest.Create(apiurl);
webreq.Headers.Add("Authorization", $"sso-key {Key}:{Secret}");
webreq.ContentType = "application/json";
webreq.Accept = "application/json";
webreq.UserAgent = UserAgent;
if (UpdateData == true && RecordToJson().Length > 0)
{
webreq.Method = "PUT";
webreq.ContentLength = RecordToJson().Length;
using (var writer = new StreamWriter(webreq.GetRequestStream()))
{
writer.Write(RecordToJson());
}
}
else webreq.Method = "GET";
try
{
HttpWebResponse WebResponse = (HttpWebResponse)webreq.GetResponse();
Stream responseStream = responseStream = WebResponse.GetResponseStream();
StreamReader Reader = new StreamReader(responseStream, Encoding.Default);
HtmlContent = Reader.ReadToEnd();
ResponseHeaders.Clear();
foreach (string headkey in WebResponse.Headers.AllKeys)
{
ResponseHeaders.Add(headkey, WebResponse.Headers[headkey]);
}
if (WebResponse.StatusCode == HttpStatusCode.OK && HtmlContent.Length > 0)
{
dynamic jsondata = JsonConvert.DeserializeObject(HtmlContent);
if (jsondata != null && jsondata.Count != null && jsondata.Count > 0 && jsondata[0] != null)
{
if (jsondata[0].data != null) Data = jsondata[0].data.ToString().Trim();
if (jsondata[0].name != null) Name = jsondata[0].name.ToString().Trim();
if (jsondata[0].ttl != null) Ttl = jsondata[0].ttl;
if (jsondata[0].type != null) Type = (RecordTypes)System.Enum.Parse(typeof(RecordTypes), jsondata[0].type.ToString().Trim()); //jsondata[0].type.ToString().Trim();
StatusCode = StatusCodes.OK;
}
else StatusCode = StatusCodes.EmptyResponse;
}
else StatusCode = StatusCodes.EmptyResponse;
WebResponse.Close();
responseStream.Close();
}
catch (Exception e)
{
ExceptionMessage = e.Message;
StatusCode = StatusCodes.EmptyResponse;
}
}
/// <summary>
/// Current record export in JSON format - this needs primarly to create/update your DNS record
/// </summary>
/// <returns></returns>
public string RecordToJson()
{
Dictionary<string, object> pushrecord = new Dictionary<string, object>()
{
{ "data", Data },
{ "name", Name },
{ "ttl", Ttl },
{ "type", Type.ToString() },
{ "port", Port },
{ "priority", Priority },
{ "protocol", "string" },
{ "service", "string" },
{ "weight", Weight },
};
return JsonConvert.SerializeObject(new object[1] { pushrecord });
}
}
}