Created
March 11, 2018 06:22
-
-
Save Silvenga/1a1cd84e64399889cf48f64c1c90e017 to your computer and use it in GitHub Desktop.
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
<Query Kind="Program"> | |
<NuGetReference>cPanelSharp</NuGetReference> | |
<NuGetReference>Newtonsoft.Json</NuGetReference> | |
<Namespace>cPanelSharp</Namespace> | |
<Namespace>Newtonsoft.Json</Namespace> | |
<Namespace>Newtonsoft.Json.Linq</Namespace> | |
</Query> | |
cPanelClient Client = new cPanelClient("silvenga", "ghost.mxroute.com", password: Util.GetPassword("mxroute-silvenga"), cpanel: true); | |
void Main() | |
{ | |
CreateDomain("enga.me", "silvenga.net"); | |
} | |
public void CreateDomain(string domain, string forwardTo) | |
{ | |
var domainExists = ListAliasDomains().Any(x => x == domain); | |
if (!domainExists) | |
{ | |
Console.WriteLine("Creating alais."); | |
CreateAliasDomains(domain); | |
} | |
var forwardExists = ListDomainForwards().Any(x => x.Dest == domain); | |
if (!forwardExists) | |
{ | |
Console.WriteLine("Creating forward."); | |
CreateDomainForwards(domain, forwardTo); | |
} | |
} | |
public IList<string> ListAliasDomains() | |
{ | |
var json = Client.Api2("Park", "listparkeddomains"); | |
var result = JObject.Parse(json); | |
return result["cpanelresult"]["data"].ToArray() | |
.Select(x => x["domain"]) | |
.Select(x => x.ToObject<string>()) | |
.ToList(); | |
} | |
public void CreateAliasDomains(string domain) | |
{ | |
Client.Api2("Park", "park", param: new | |
{ | |
domain = domain | |
}); | |
} | |
public IList<(string Forward, string Dest)> ListDomainForwards() | |
{ | |
var json = Client.Api2("Email", "listdomainforwards"); | |
var result = JObject.Parse(json); | |
return result["cpanelresult"]["data"].ToArray() | |
.Select(x => (x["forward"].ToObject<string>(), x["dest"].ToObject<string>())) | |
.ToList(); | |
} | |
public void CreateDomainForwards(string from, string to) | |
{ | |
Client.Api2("Email", "adddomainforward", param: new | |
{ | |
domain = from, | |
destdomain = to | |
}).Dump(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment