Franta – Občasník malého ajťáka

Domény, Hosting, Cestování

Shell aplikace pro registraci domen v GO (Subreg-REST API)

Jendoducha shell aplikace pro jednoduche i hromadne registrace domen v GO na jednoho konkretniho drzitele v Shellu.

package main

import (
    "bytes"
    "bufio"
    "encoding/json"
    "fmt"
    "flag"
    "os"
    "io/ioutil"
    "log"
    "net/http"
)

// Config holds the API configuration
type Config struct {
    RegistrantID string            `json:"registrant_id"`
    APIKey       string            `json:"api_key"`
    Params       map[string]interface{} `json:"params"`
}

type Host struct {
    Hostname string `json:"hostname"`
}

// DomainRequest defines the structure of the domain registration request
type DomainRequest struct {
    Period int `json:"period"`
    Registrant struct {
        ID string `json:"id"`
    } `json:"registrant"`
    Contacts struct {
        Admin struct {
            ID string `json:"id"`
        } `json:"admin"`
        Tech struct {
            ID string `json:"id"`
        } `json:"tech"`
        Billing struct {
            ID string `json:"id"`
        } `json:"billing"`
    } `json:"contacts"`
    NS struct {
	Hosts []Host `json:"hosts"`
    } `json:"ns"`
    Params       map[string]interface{} `json:"params"`
    PaymentMethod string            `json:"payment_method"`
    ValidateOnly int               `json:"validate_only"`
    Premium       *int                   `json:"premium,omitempty"` // optional field, will not be included if nil
}

func main() {

    premiumFlag := flag.Bool("p", false, "Include premium service")
    filename := flag.String("f", "", "File containing domain names, one per line")
    flag.Parse()

    // Load configuration
    configFile, err := ioutil.ReadFile("/etc/regdomains.json")
    if err != nil {
        log.Fatalf("Error reading config file /etc/regdomains.json: %v", err)
    }

    var config Config
    if err := json.Unmarshal(configFile, &config); err != nil {
        log.Fatalf("Error parsing config file: %v", err)
    }

    var domains []string
    if *filename != "" {
        file, err := os.Open(*filename)
        if err != nil {
            log.Fatalf("Error opening file: %v", err)
        }
        defer file.Close()

        scanner := bufio.NewScanner(file)
        for scanner.Scan() {
            domains = append(domains, scanner.Text())
        }
        if err := scanner.Err(); err != nil {
            log.Fatalf("Error reading domains from file: %v", err)
        }
    } else if flag.NArg() > 0 {
        domains = append(domains, flag.Args()...)
    } else {
        log.Fatalf("No domains provided. Use -f to specify a file or provide domains as arguments.")
    }

    for _, domain := range domains {
        sendDomainRegistration(domain, config, premiumFlag)
    }

}

func sendDomainRegistration(domain string, config Config, premiumFlag *bool) {

    // Prepare the domain registration request
    domainRequest := DomainRequest{
        Period: 1,
        PaymentMethod: "credit",
        ValidateOnly: 0,
        Params: config.Params,
    }
    domainRequest.Registrant.ID = config.RegistrantID
    domainRequest.Contacts.Admin.ID = config.RegistrantID
    domainRequest.Contacts.Tech.ID = config.RegistrantID
    domainRequest.Contacts.Billing.ID = config.RegistrantID
    domainRequest.NS.Hosts = []Host{
        {Hostname: "ns.parktons.com"},
        {Hostname: "ns2.parktons.com"},
    }

    if *premiumFlag {
        premium := 1
        domainRequest.Premium = &premium
    }

    // Marshal the request into JSON
    requestBody, err := json.Marshal(domainRequest)
    if err != nil {
        log.Fatalf("Error marshaling request: %v", err)
    }

    // Set up the HTTP request
    url := fmt.Sprintf("https://api.subreg.cz/domains/%s", domain)
    req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
    if err != nil {
        log.Fatalf("Error creating request: %v", err)
    }
    req.Header.Set("Authorization", "Bearer "+config.APIKey)
    req.Header.Set("Content-Type", "application/json")

    // Send the request
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        log.Fatalf("Error sending request to API: %v", err)
    }
    defer resp.Body.Close()

    // Read the response
    responseBody, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatalf("Error reading response body: %v", err)
    }
    fmt.Printf("%s: %s\n", domain, string(responseBody))
}

Jako parametry k registraci pouzivam /etc/regdomains.json konfigurak:

{
  "registrant_id": "G-xxxxxx",
  "api_key": "<API Klic>",
  "params": {
    "SSL": 1,
    "premium": 1,
    "entityType" : 7,
    "nationalityCode" : "CZ",
    "regCode" : "xx",
    "idnum" : "xx",
    "uktype" : "FCORP",
    "co-no" : "xx",
    "identity" : "xx",
    "type" : 1,
    "tech_identity" : "xx",
    "tech_type" : 1,
    "trustee" : 1
  }
}

V Configu si muzeme definovat vsechny parametry z API pole “params” aby nam prochazeli i ruzne exoticke koncovky.

Pouziti:

# Jednoducha registrace

./regdomains domena.tld
domena.tld: {"orderid":16658492}

# Registrace ze souboru (co radek, to domena):

./regdomains -f soubor.txt
domena.tld: {"orderid":16658492}
premiumdomena.tld: {"error":"Premium domain","code_major":501,"code_minor":1008}

# Registrace premiove domeny

./regdomains -p premiumdomena.tld
premiumdomena.tld: {"orderid":16658492}

# Registrace domen ze souboru s povolenymi premiovkami

./regdomains -p -f soubor.txt
domena.tld: {"orderid":16658492}
premiumdomena.tld: {"orderid":16658493}

Napsat komentář

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *

Tato stránka používá Akismet k omezení spamu. Podívejte se, jak vaše data z komentářů zpracováváme..