From cfa3805e0a05de38ab7ad53e751dfa12bad2785d Mon Sep 17 00:00:00 2001 From: Imi <27138272+iminet@users.noreply.github.com> Date: Sun, 17 Jan 2021 14:54:40 +0100 Subject: [PATCH 1/5] Initial release --- Iminetsoft.Godaddy.cs | 149 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 Iminetsoft.Godaddy.cs diff --git a/Iminetsoft.Godaddy.cs b/Iminetsoft.Godaddy.cs new file mode 100644 index 0000000..36d905f --- /dev/null +++ b/Iminetsoft.Godaddy.cs @@ -0,0 +1,149 @@ +using System; +using System.Net; +using System.IO; +using System.Text; +using Newtonsoft.Json; +using System.Collections.Generic; + +namespace Iminetsoft +{ + /// + /// 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 + /// Developed and maintained by Iminetsoft | Version: 0.1 | Date: 17.01.2021 + /// + 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 Dictionary ResponseHeaders { get; private set; } = new Dictionary(); + public string ExceptionMessage { private set; get; } = String.Empty; + public StatusCodes StatusCode = StatusCodes.EmptyResponse; + + /// + /// Gets the current data of the specified domain and DNS record + /// + /// + public bool GetDnsRecord() + { + GetSetDnsRecord(); + return (StatusCode == StatusCodes.OK ? true : false); + } + + /// + /// Gets the current IP/hostname of the specified domain and DNS record, typed A + /// + /// + public string GetDnsRecordString() + { + GetSetDnsRecord(); + return (StatusCode == StatusCodes.OK ? Data : ""); + } + + /// + /// Creates or updates the specified DNS record + /// + /// + 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.Headers.Add("Content-Type", "application/json"); + webreq.Headers.Add("accept", "application/json"); + + 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; + } + } + + /// + /// Current record export in JSON format - this needs primarly to create/update your DNS record + /// + /// + public string RecordToJson() + { + Dictionary pushrecord = new Dictionary() + { + { "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 }); + } + } +} \ No newline at end of file From df774e12cda6891e97308b6c7dd0296819e50855 Mon Sep 17 00:00:00 2001 From: Imi <27138272+iminet@users.noreply.github.com> Date: Sun, 17 Jan 2021 15:07:23 +0100 Subject: [PATCH 2/5] Create README.md --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..6736765 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +# GodaddyApi.Net +This is a simple API solution for handling your GoDaddy DNS records + +## Requirements +- .NET 4.5 or :NET Core 3.1 +- Newtonsoft Json.NET from [NuGet](https://www.nuget.org/packages/Newtonsoft.Json/) +- Developer key and secred from [GoDaddy](https://developer.godaddy.com/keys) + + +## Usage + +### This is a basic sample code for getting current DNS record + +``` +Iminetsoft.Godaddy gd = new Iminetsoft.Godaddy(); +gd.Domain = "yourdomain.com"; +gd.Type = Iminetsoft.Godaddy.RecordTypes.A; +gd.Name = "yourrecordname" // (for example @ for the domain itself or test for a subdomain)"; +gd.Ttl = 600; +gd.Secret = "YourSecret"; +gd.Key = "YourKey"; +gd.GetDnsRecord(); +Console.WriteLine(gd.Data); +``` + +### And now I going to show you, how can you update a DNS record (or create if it didn't exist before) +``` +Iminetsoft.Godaddy gd = new Iminetsoft.Godaddy(); +gd.Domain = "yourdomain.com"; +gd.Type = Iminetsoft.Godaddy.RecordTypes.A; +gd.Name = "yourrecordname" // (for example @ for the domain itself or test for a subdomain)"; +gd.Ttl = 600; +gd.Secret = "YourSecret"; +gd.Key = "YourKey"; +gd.Data = "8.8.8.8" // Here goes your new IP address +gd.SetDnsRecord(); +``` + +## Other info +Please keep in your mind, this code is still under development, so I am not responsible for it. +Let me know if you have any issue or idea how should I make it better. From 150510ee7a257aadafdac08fc3fb942516e51a41 Mon Sep 17 00:00:00 2001 From: Imi <27138272+iminet@users.noreply.github.com> Date: Mon, 18 Jan 2021 09:22:36 +0100 Subject: [PATCH 3/5] Some buf fixes --- Iminetsoft.Godaddy.cs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Iminetsoft.Godaddy.cs b/Iminetsoft.Godaddy.cs index 36d905f..d6bdf78 100644 --- a/Iminetsoft.Godaddy.cs +++ b/Iminetsoft.Godaddy.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Net; using System.IO; using System.Text; @@ -29,10 +29,23 @@ namespace Iminetsoft public string Data { get; set; } = String.Empty; public int Priority { get; set; } = 0; + public string UserAgent { get; set; } = "Iminetsoft.Godaddy.Net"; + public Dictionary ResponseHeaders { get; private set; } = new Dictionary(); 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); + } + /// /// Gets the current data of the specified domain and DNS record /// @@ -71,8 +84,9 @@ namespace Iminetsoft ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest webreq = (HttpWebRequest)System.Net.WebRequest.Create(apiurl); webreq.Headers.Add("Authorization", $"sso-key {Key}:{Secret}"); - webreq.Headers.Add("Content-Type", "application/json"); - webreq.Headers.Add("accept", "application/json"); + webreq.ContentType = "application/json"; + webreq.Accept = "application/json"; + webreq.UserAgent = UserAgent; if (UpdateData == true && RecordToJson().Length > 0) { @@ -146,4 +160,4 @@ namespace Iminetsoft return JsonConvert.SerializeObject(new object[1] { pushrecord }); } } -} \ No newline at end of file +} From f13d11742ebf4c13582f8f64834d1a00eaeeeac8 Mon Sep 17 00:00:00 2001 From: Imi <27138272+iminet@users.noreply.github.com> Date: Mon, 18 Jan 2021 09:22:55 +0100 Subject: [PATCH 4/5] Some bug fixes --- Iminetsoft.Godaddy.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Iminetsoft.Godaddy.cs b/Iminetsoft.Godaddy.cs index d6bdf78..c31d757 100644 --- a/Iminetsoft.Godaddy.cs +++ b/Iminetsoft.Godaddy.cs @@ -69,7 +69,7 @@ namespace Iminetsoft /// /// Creates or updates the specified DNS record /// - /// + /// public bool SetDnsRecord() { GetSetDnsRecord(true); From e6769d5bb2960e93a83739a7b88698538c85ee9c Mon Sep 17 00:00:00 2001 From: Imi <27138272+iminet@users.noreply.github.com> Date: Mon, 26 Sep 2022 11:27:53 +0200 Subject: [PATCH 5/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6736765..e7f2147 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This is a simple API solution for handling your GoDaddy DNS records ## Requirements - .NET 4.5 or :NET Core 3.1 - Newtonsoft Json.NET from [NuGet](https://www.nuget.org/packages/Newtonsoft.Json/) -- Developer key and secred from [GoDaddy](https://developer.godaddy.com/keys) +- Developer key and secret from [GoDaddy](https://developer.godaddy.com/keys) ## Usage