示例#1
0
文件: parser.go 项目: grunya404/dns
func (p *Parser) handleLine(line string) {
	rr, err := records.New(line, p.origin, p.TLD, p.ttl, p.Date, p.ID)
	if err != nil {
		log.Warn("handleLine:getRecord: %s", err)
		log.Warn("handleLine:line: %s", line)
		return
	}
	rr.Parser = p.Parser
	err = p.domainInsert.Add(&rr.Domain)
	if err != nil {
		log.Error("handleLine: Unable to bulk insert Domain: %s", err)
		return
	}
	err = p.recordInsert.Add(&rr)
	if err != nil {
		log.Error("handleLine:recordInsert.Add: %s", err)
		return
	}
	p.recordTypes[rr.Type.Name] = rr.Type.ID
}
示例#2
0
文件: records.go 项目: grunya404/dns
// New creates a new Record, it takes the entire resource record line, the origin of the zonefile, the tld associated, the ttl and a parse date. It then creates a formalized Record.
func New(line, origin string, tld tlds.TLD, ttl uint, date time.Time, parserID int32) (Record, error) {
	fields := strings.Fields(line)
	r := Record{}

	name := fields[0]
	// set origin if it's not already there
	if !strings.HasSuffix(name, ".") {
		name += "." + origin
	}
	// strip domain name
	name = domains.CleanDomain(name, tld)
	r.Domain = domains.New(name, tld)
	name = strings.TrimSuffix(name, ".")
	name = strings.TrimSuffix(name, r.Domain.TLD.Name)
	name = strings.TrimSuffix(name, r.Domain.Name)
	name = strings.TrimSuffix(name, ".")
	// check if we are referencing top level
	if len(name) == 0 {
		name = "@"
	}
	r.Name = name

	typeIdx := 1
	if len(fields) > 3 {
		fields = util.FilterIN(fields)
	}
	if len(fields) > 3 {
		ttlFromFields, err := strconv.ParseUint(fields[1], 10, 64)
		if err == nil {
			typeIdx = 2
			ttl = uint(ttlFromFields)
		} else {
			log.Warn("Unable to parse RR: len(fields) == %d, fields: %s", len(fields), fields)
		}
	}
	if len(fields) <= typeIdx {
		return r, errors.New("Unable to set typeIdx in getRecord.")
	}
	rt, err := types.Detect(fields[typeIdx])
	if err != nil {
		return r, errors.New("Unable to detect Record Type.")
	}
	r.Type, err = types.New(rt, tld)
	r.Args.Args = fields[typeIdx+1:]
	id := uuid.NewSHA1(uuid.NameSpace_OID, []byte(
		fmt.Sprintf("%v%d", r, parserID),
	))
	r.Date = date
	r.Args.TTL = ttl
	r.UUID = id
	r.Added = time.Now()
	return r, nil
}
示例#3
0
文件: parser.go 项目: grunya404/dns
func (p *Parser) handleVariable(line string) {
	fields := strings.Fields(line)
	if len(fields) == 2 {
		switch fields[0] {
		case "$origin":
			p.origin = fields[1]
			p.originCheck = true
		case "$ttl":
			ttl, err := strconv.ParseUint(fields[1], 10, 0)
			if err != nil {
				log.Warn("handleVariable:$ttl: %s", err)
				return
			}
			p.ttl = uint(ttl)
		}
	}
}