Skip to content

Commit

Permalink
Merge pull request #1 from hosting-de-labs/master
Browse files Browse the repository at this point in the history
RFC8605 Compliance
  • Loading branch information
teward authored Sep 8, 2022
2 parents af93e7e + 3fa3563 commit 5319cb0
Show file tree
Hide file tree
Showing 12 changed files with 513 additions and 21 deletions.
46 changes: 42 additions & 4 deletions bootstrap/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"time"

"github.com/openrdap/rdap/bootstrap/cache"
Expand All @@ -103,6 +105,13 @@ import (
// A RegistryType represents a bootstrap registry type.
type RegistryType int

type jsonDocument struct {
Description string
Publication string
Version string
Services [][][]string
}

const (
DNS RegistryType = iota
IPv4
Expand Down Expand Up @@ -236,19 +245,48 @@ func (c *Client) download(ctx context.Context, registry RegistryType) ([]byte, R
return nil, nil, fmt.Errorf("Server returned non-200 status code: %s", resp.Status)
}

json, err := ioutil.ReadAll(resp.Body)
jsonString, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}

// existiert ein custom-RegistryType file?
dc := cache.NewDiskCache()
if _, err := os.Stat(dc.Dir + "/custom-" + c.filenameFor(registry)); err == nil {
// path/to/whatever exists
custom, err := ioutil.ReadFile(dc.Dir + "/custom-" + c.filenameFor(registry))
if err != nil {
return nil, nil, err
}

var customDecoded jsonDocument
err = json.Unmarshal(custom, &customDecoded)
if err != nil {
return nil, nil, err
}

var decoded jsonDocument
err = json.Unmarshal(jsonString, &decoded)
if err != nil {
return nil, nil, err
}

decoded.Services = append(decoded.Services, customDecoded.Services...)

jsonString, err = json.Marshal(decoded)
if err != nil {
return nil, nil, err
}
}

var s Registry
s, err = newRegistry(registry, json)
s, err = newRegistry(registry, jsonString)

if err != nil {
return json, nil, err
return jsonString, nil, err
}

return json, s, nil
return jsonString, s, nil
}

func (c *Client) freshenFromCache(registry RegistryType) {
Expand Down
45 changes: 45 additions & 0 deletions bootstrap/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,48 @@ func NewFile(jsonDocument []byte) (*File, error) {

return f, nil
}

func (f *File) AddEntries(jsonDocument []byte) error {
var doc struct {
Description string
Publication string
Version string

Services [][][]string
}

err := json.Unmarshal(jsonDocument, &doc)
if err != nil {
return err
}

for _, s := range doc.Services {
if len(s) != 2 {
return errors.New("Malformed bootstrap (bad services array)")
}

entries := s[0]
rawURLs := s[1]

var urls []*url.URL

for _, rawURL := range rawURLs {
url, err := url.Parse(rawURL)

// Ignore unparsable URLs.
if err != nil {
continue
}

urls = append(urls, url)
}

if len(urls) > 0 {
for _, entry := range entries {
f.Entries[entry] = urls
}
}
}

return nil
}
68 changes: 68 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,45 @@ func RunCLI(args []string, stdout io.Writer, stderr io.Writer, options CLIOption
return 1
}

// resp.Object
secondLookup := false
var registrarUrl *url.URL
if req.Type.String() == "domain" {
// try to follow

d, ok := resp.Object.(*Domain)
if ok {
for _, link := range d.Links {
if link.Href != resp.URL.String() {
// zweiter lookup
registrarUrl, err = url.Parse(link.Href)
secondLookup = true
if err != nil {
printError(stderr, fmt.Sprintf("Error: %s", err))
return 1
}
}
}
}
}

var secondReq *Request
var secondResp *Response
if secondLookup {
secondStart := time.Now()

secondReq = NewRawRequest(registrarUrl)
secondResp, err = client.Do(secondReq)

verbose("")
verbose(fmt.Sprintf("rdap: Finished second query in %s", time.Since(secondStart)))

if err != nil {
printError(stderr, fmt.Sprintf("Registrar Error: %s", err))
secondLookup = false
}
}

// Insert a blank line to seperate verbose messages/proper output.
if *verboseFlag {
fmt.Fprintln(stderr, "")
Expand All @@ -516,35 +555,64 @@ func RunCLI(args []string, stdout io.Writer, stderr io.Writer, options CLIOption

// Print the response out in text format?
if *outputFormatText {
fmt.Fprintln(stdout, "RDAP from Registry:")
printer := &Printer{
Writer: stdout,

BriefLinks: true,
}
printer.Print(resp.Object)
if secondLookup {
fmt.Fprintf(stdout, "\n\nRDAP from Registrar:\n")
printer.Print(secondResp.Object)
}
}

// Print the raw response out?
if *outputFormatRaw {
fmt.Fprintln(stdout, "RDAP from Registry:")
fmt.Printf("%s", resp.HTTP[0].Body)
if secondLookup {
fmt.Fprintf(stdout, "\n\nRDAP from Registrar:\n")
fmt.Printf("%s", secondResp.HTTP[0].Body)
}
}

// Print the response, JSON pretty-printed?
if *outputFormatJSON {
var out bytes.Buffer
json.Indent(&out, resp.HTTP[0].Body, "", " ")
fmt.Fprintln(stdout, "RDAP from Registry:")
out.WriteTo(os.Stdout)

if secondLookup {
json.Indent(&out, secondResp.HTTP[0].Body, "", " ")
fmt.Fprintf(stdout, "\n\nRDAP from Registrar:\n")
out.WriteTo(os.Stdout)
}
}

// Print WHOIS style response out?
if *outputFormatWhois {
fmt.Fprintln(stdout, "RDAP from Registry:")
w := resp.ToWhoisStyleResponse()

for _, key := range w.KeyDisplayOrder {
for _, value := range w.Data[key] {
fmt.Fprintf(stdout, "%s: %s\n", key, safePrint(value))
}
}

if secondLookup {
fmt.Fprintf(stdout, "\n\nRDAP from Registrar:\n")
w = secondResp.ToWhoisStyleResponse()

for _, key := range w.KeyDisplayOrder {
for _, value := range w.Data[key] {
fmt.Fprintf(stdout, "%s: %s\n", key, safePrint(value))
}
}
}
}

_ = fetchRolesFlag
Expand Down
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func (c *Client) Do(req *Request) (*Response, error) {
for _, r := range reqs {
c.Verbose(fmt.Sprintf("client: GET %s", r.URL()))

resp.URL = r.URL()
httpResponse := c.get(r)
resp.HTTP = append(resp.HTTP, httpResponse)

Expand Down
Loading

0 comments on commit 5319cb0

Please sign in to comment.