Exemplo n.º 1
0
func (b *Backend) jsonToDomain(name, jsonValue string) (*domain, error) {
	d := &domain{}

	v := ncdomain.ParseValue(name, jsonValue, b.resolveExtraName, nil)
	if v == nil {
		return nil, fmt.Errorf("couldn't parse value")
	}

	d.ncv = v

	return d, nil
}
Exemplo n.º 2
0
func TestSuite(t *testing.T) {
	items := testutil.SuiteReader(t)
	for ti := range items {
		resolve := func(name string) (string, error) {
			v, ok := ti.Names[name]
			if !ok {
				return "", fmt.Errorf("not found")
			}

			return v, nil
		}

		for k, jsonValue := range ti.Names {
			dnsName, err := convertName(k)
			if err != nil {
				continue
			}

			errCount := 0
			errFunc := func(err error, isWarning bool) {
				if !isWarning {
					errCount++
				}
				//fmt.Printf("Error:  %v\n", err)
			}

			v := ncdomain.ParseValue(k, jsonValue, resolve, errFunc)
			if v == nil {
				// TODO
				continue
			}

			rrstrs := []string{}
			rrs, _ := v.RRsRecursive(nil, dnsName+".bit.", dnsName+".bit.")
			for _, rr := range rrs {
				s := rr.String()
				s = strings.Replace(s, "\t600\t", "\t", -1) // XXX
				rrstrs = append(rrstrs, strings.Replace(s, "\t", " ", -1))
			}
			sort.Strings(rrstrs)
			rrstr := strings.Join(rrstrs, "\n")

			// CHECK MATCH
			if rrstr != ti.Records {
				t.Errorf("Didn't match: %s\n%+v\n    !=\n%+v\n\n%#v\n\n%#v", ti.ID, rrstr, ti.Records, v, rrs)
			}

			if errCount != ti.NumErrors {
				t.Errorf("Error count didn't match: %d != %d (%s)\n", errCount, ti.NumErrors, ti.ID)
			}
		}
	}
}
Exemplo n.º 3
0
func main() {
	flag.CommandLine.Usage = usage
	flag.Parse()
	args := flag.Args()
	names := map[string]string{}
	var primaryK, primaryV string

	if len(args)%2 != 0 || len(args) < 2 {
		usage()
	}

	conn.Username = *rpcuser
	conn.Password = *rpcpass
	conn.Server = *rpchost

	for i := 0; i+1 < len(args); i += 2 {
		k := args[i]
		v := args[i+1]
		if _, ok := names[k]; ok {
			fmt.Fprintf(os.Stderr, "duplicate name specified: %s\n", k)
			os.Exit(1)
		}

		v, err := translateValue(k, v)
		if err != nil {
			fmt.Fprintf(os.Stderr, "failed to translate value: %v\n", err)
			os.Exit(1)
		}

		if i == 0 {
			primaryK, primaryV = k, v
		}

		names[k] = v
	}

	value := ncdomain.ParseValue(primaryK, primaryV, func(k string) (string, error) {
		v, ok := names[k]
		if !ok {
			return "", fmt.Errorf("reference to unknown name")
		}

		return v, nil
	}, func(err error, isWarning bool) {
		if isWarning {
			fmt.Fprintf(os.Stderr, "Warning: %v\n", err)
		} else {
			fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		}
	})

	suffix, err := util.NamecoinKeyToBasename(primaryK)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Invalid key: %s\n", primaryK)
		os.Exit(1)
	}

	suffix += ".bit."

	rrs, err := value.RRsRecursive(nil, suffix, suffix)
	if err != nil {
		fmt.Printf("Error generating RRs: %v\n", err)
		os.Exit(1)
	}

	for _, rr := range rrs {
		fmt.Println(rr.String())
	}
}
Exemplo n.º 4
0
func (ws *webServer) handleLookup(rw http.ResponseWriter, req *http.Request) {
	info := struct {
		layoutInfo
		JSONMode       bool
		JSONValue      string
		Query          string
		Advanced       bool
		NamecoinName   string
		DomainName     string
		BareName       string
		NameParseError error
		ExistenceError error
		Expired        bool
		Value          string
		NCValue        *ncdomain.Value
		NCValueFmt     fmt.Formatter
		ParseErrors    []error
		ParseWarnings  []error
		RRs            []dns.RR
		RRError        error
		Valid          bool
	}{layoutInfo: *ws.layoutInfo()}

	defer func() {
		err := lookupPageTpl.Execute(rw, &info)
		log.Infoe(err, "lookup page tpl")
	}()

	q := req.FormValue("q")
	info.Query = q
	info.BareName, info.NamecoinName, info.NameParseError = util.ParseFuzzyDomainNameNC(q)
	if info.NameParseError != nil {
		return
	}

	info.Advanced = (req.FormValue("adv") != "")
	info.DomainName = info.BareName + ".bit."

	info.JSONValue = req.FormValue("value")
	info.Value = strings.Trim(info.JSONValue, " \t\r\n")
	if info.Value == "" {
		info.Value, info.ExistenceError = ws.s.namecoinConn.Query(info.NamecoinName)
		if info.ExistenceError != nil {
			return
		}
	} else {
		info.JSONMode = true
	}

	errorFunc := func(e error, isWarning bool) {
		if isWarning {
			info.ParseWarnings = append(info.ParseWarnings, e)
		} else {
			info.ParseErrors = append(info.ParseErrors, e)
		}
	}

	info.NCValue = ncdomain.ParseValue(info.NamecoinName, info.Value, ws.resolveFunc, errorFunc)
	if info.NCValue == nil {
		return
	}

	info.NCValueFmt = pretty.Formatter(info.NCValue)

	info.RRs, info.RRError = info.NCValue.RRsRecursive(nil, info.DomainName, "bit.")
	if len(info.ParseErrors) == 0 && info.RRError == nil {
		info.Valid = true
	}
}