ncdumpzone: Refactor into library and executable.

pull/82/head
JeremyRand 6 years ago
parent c0b4e4d3e6
commit 1a6a3db340
No known key found for this signature in database
GPG Key ID: B3F2D165786D6570

@ -1,61 +1,54 @@
package main package ncdumpzone
import "github.com/namecoin/ncdns/ncdomain" import (
import "github.com/namecoin/ncdns/namecoin" "fmt"
import "github.com/namecoin/ncdns/tlsoverridefirefox" "io"
import "github.com/namecoin/ncdns/util" "strings"
import "github.com/hlandau/xlog"
import "strings"
import "fmt"
import "github.com/miekg/dns" "github.com/hlandau/xlog"
import "gopkg.in/hlandau/easyconfig.v1" "github.com/miekg/dns"
import "gopkg.in/hlandau/easyconfig.v1/cflag"
var log, Log = xlog.New("ncdumpzone") extratypes "github.com/hlandau/ncbtcjsontypes"
"github.com/namecoin/ncdns/namecoin"
var ( "github.com/namecoin/ncdns/ncdomain"
flagGroup = cflag.NewGroup(nil, "ncdumpzone") "github.com/namecoin/ncdns/tlsoverridefirefox"
rpchostFlag = cflag.String(flagGroup, "namecoinrpcaddress", "127.0.0.1:8336", "Namecoin RPC host:port") "github.com/namecoin/ncdns/util"
rpcuserFlag = cflag.String(flagGroup, "namecoinrpcusername", "", "Namecoin RPC username")
rpcpassFlag = cflag.String(flagGroup, "namecoinrpcpassword", "", "Namecoin RPC password")
formatFlag = cflag.String(flagGroup, "format", "zonefile", "Output format. \"zonefile\" = "+
"DNS zone file. \"firefox-override\" = Firefox "+
"cert_override.txt format.")
) )
var conn namecoin.Conn var log, Log = xlog.New("ncdumpzone")
var config = easyconfig.Configurator{
ProgramName: "ncdumpzone",
}
const perCall = 1000 const perCall = 1000
func printRR(rr dns.RR) { func dumpRR(rr dns.RR, dest io.Writer, format string) error {
if formatFlag.Value() == "zonefile" { switch format {
fmt.Print(rr.String(), "\n") case "zonefile":
} else if formatFlag.Value() == "firefox-override" { fmt.Fprint(dest, rr.String(), "\n")
case "firefox-override":
result, err := tlsoverridefirefox.OverrideFromRR(rr) result, err := tlsoverridefirefox.OverrideFromRR(rr)
if err != nil { if err != nil {
panic(err) return err
} }
fmt.Print(result) fmt.Fprint(dest, result)
} }
return nil
} }
func main() { func dumpName(item *extratypes.NameFilterItem, conn namecoin.Conn,
err := config.Parse(nil) dest io.Writer, format string) error {
if err != nil { // The order in which name_scan returns results is seemingly rather
log.Fatalf("Couldn't parse configuration: %s", err) // random, so we can't stop when we see a non-d/ name, so just skip it.
if !strings.HasPrefix(item.Name, "d/") {
return nil
} }
conn.Server = rpchostFlag.Value() suffix, err := util.NamecoinKeyToBasename(item.Name)
conn.Username = rpcuserFlag.Value() if err != nil {
conn.Password = rpcpassFlag.Value() return nil
}
if formatFlag.Value() != "zonefile" && formatFlag.Value() != "firefox-override" { getNameFunc := func(k string) (string, error) {
log.Fatalf("Invalid \"format\" argument: %s", formatFlag.Value()) return conn.Query(k)
} }
var errors []error var errors []error
@ -63,8 +56,29 @@ func main() {
errors = append(errors, err) errors = append(errors, err)
} }
getNameFunc := func(k string) (string, error) { value := ncdomain.ParseValue(item.Name, item.Value, getNameFunc, errFunc)
return conn.Query(k) if len(errors) > 0 {
return nil
}
rrs, err := value.RRsRecursive(nil, suffix+".bit.", "bit.")
log.Warne(err, "error generating RRs")
for _, rr := range rrs {
err = dumpRR(rr, dest, format)
if err != nil {
return err
}
}
return nil
}
// Dump extracts all domain names from conn, formats them according to the
// specified format, and writes the result to dest.
func Dump(conn namecoin.Conn, dest io.Writer, format string) error {
if format != "zonefile" && format != "firefox-override" {
return fmt.Errorf("Invalid \"format\" argument: %s", format)
} }
currentName := "d/" currentName := "d/"
@ -72,7 +86,9 @@ func main() {
for { for {
results, err := conn.Scan(currentName, perCall) results, err := conn.Scan(currentName, perCall)
log.Fatale(err, "scan") if err != nil {
return fmt.Errorf("scan: %s", err)
}
if len(results) <= continuing { if len(results) <= continuing {
log.Info("out of results, stopping") log.Info("out of results, stopping")
@ -89,31 +105,14 @@ func main() {
for i := range results { for i := range results {
r := &results[i] r := &results[i]
// The order in which name_scan returns results is seemingly rather err = dumpName(r, conn, dest, format)
// random, so we can't stop when we see a non-d/ name, so just skip it.
if !strings.HasPrefix(r.Name, "d/") {
continue
}
suffix, err := util.NamecoinKeyToBasename(r.Name)
if err != nil { if err != nil {
continue return err
}
errors = errors[0:0]
value := ncdomain.ParseValue(r.Name, r.Value, getNameFunc, errFunc)
if len(errors) > 0 {
continue
}
rrs, err := value.RRsRecursive(nil, suffix+".bit.", "bit.")
log.Warne(err, "error generating RRs")
for _, rr := range rrs {
printRR(rr)
} }
} }
currentName = results[len(results)-1].Name currentName = results[len(results)-1].Name
} }
return nil
} }

@ -0,0 +1,49 @@
package main
import (
"os"
"github.com/hlandau/xlog"
"gopkg.in/hlandau/easyconfig.v1"
"gopkg.in/hlandau/easyconfig.v1/cflag"
"github.com/namecoin/ncdns/namecoin"
"github.com/namecoin/ncdns/ncdumpzone"
)
var log, _ = xlog.New("ncdumpzone-main")
var (
flagGroup = cflag.NewGroup(nil, "ncdumpzone")
rpchostFlag = cflag.String(flagGroup, "namecoinrpcaddress",
"127.0.0.1:8336", "Namecoin RPC host:port")
rpcuserFlag = cflag.String(flagGroup, "namecoinrpcusername", "",
"Namecoin RPC username")
rpcpassFlag = cflag.String(flagGroup, "namecoinrpcpassword", "",
"Namecoin RPC password")
formatFlag = cflag.String(flagGroup, "format", "zonefile", "Output "+
"format. \"zonefile\" = DNS zone file. "+
"\"firefox-override\" = Firefox cert_override.txt format.")
)
var conn namecoin.Conn
var config = easyconfig.Configurator{
ProgramName: "ncdumpzone",
}
func main() {
err := config.Parse(nil)
if err != nil {
log.Fatalf("Couldn't parse configuration: %s", err)
}
conn.Server = rpchostFlag.Value()
conn.Username = rpcuserFlag.Value()
conn.Password = rpcpassFlag.Value()
err = ncdumpzone.Dump(conn, os.Stdout, formatFlag.Value())
if err != nil {
log.Fatalf("Couldn't dump zone: %s", err)
}
}
Loading…
Cancel
Save