示例#1
0
文件: get.go 项目: grunya404/dns
func parseRow(row connection.Row) (Watcher, error) {
	w := Watcher{}
	var intervalID int32
	var uuid string
	var usersBuf, logs []byte
	err := row.Scan(&w.ID, &uuid, &w.Added, &w.Updated, &intervalID, &logs, &usersBuf)
	err = json.Unmarshal(logs, &w.Logs)
	if err != nil {
		return w, err
	}
	var usersArr []int
	err = json.Unmarshal(usersBuf, &usersArr)
	if err != nil {
		return w, err
	}
	for _, u := range usersArr {
		user, err := users.GetByID(u).One()
		if err != nil {
			continue
		}
		w.Users = append(w.Users, user)
	}
	w.Domain, err = domains.GetByUUID(uuid).One()
	if err != nil {
		return w, err
	}
	w.Interval, err = intervals.GetByID(intervalID).One()
	return w, err
}
示例#2
0
文件: get.go 项目: grunya404/dns
func parseRow(row connection.Row) (Record, error) {
	r := Record{}
	var rUUID, dUUID string
	var rtID int32
	var pID int32
	var jsonBuf []byte
	err := row.Scan(&rUUID, &dUUID, &r.Name, &jsonBuf, &rtID, &r.Date, &pID, &r.Added)
	if err != nil {
		return r, err
	}
	err = json.Unmarshal(jsonBuf, &r.Args)
	if err != nil {
		return r, err
	}
	r.UUID = uuid.Parse(rUUID)
	r.Domain, err = domains.GetByUUID(dUUID).One()
	if err != nil {
		return r, err
	}
	r.Type, err = types.GetByID(rtID).One()
	if err != nil {
		return r, err
	}
	// ignore error as we dont need a Parser
	p, _ := parser.Get(parser.GetByID(), pID)
	r.Parser = p
	return r, nil
}
示例#3
0
文件: watchers.go 项目: grunya404/dns
func Create(w http.ResponseWriter, r *http.Request) {
	var post struct {
		Domain   string `json:"domain"`
		Name     string `json:"name"`
		Interval string `json:"interval"`
	}
	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&post)
	if err != nil {
		util.Error(err, w)
		return
	}
	var domain domains.Domain
	if len(post.Name) > 0 {
		name, tld, err := tlds.DetectDomainAndTLD(post.Name)
		if err != nil {
			util.Error(err, w)
			return
		}
		domain, err = domains.GetByNameAndTLD(name, tld).One()
		if err != nil {
			domain = domains.New(name, tld)
			err = domain.Insert()
			if err != nil {
				util.Error(err, w)
				return
			}
		}
	} else {
		domain, err = domains.GetByUUID(post.Domain).One()
		if err != nil {
			util.Error(err, w)
			return
		}
	}
	watcher, err := db.GetByDomain(domain).One()
	if err != nil {
		watcher, err = db.New(domain, post.Interval)
		if err != nil {
			util.Error(err, w)
			return
		}
	}
	watcher.SetLowerInterval(post.Interval)
	user, err := auth.GetUser(r)
	if err != nil {
		util.Error(err, w)
		return
	}
	watcher.AddUser(*user)
	err = watcher.Save()
	log.Info("%+v", watcher)
	dispatcher.AddDomain(domain)
	util.ToJSON(watcher, err, w)
}
示例#4
0
文件: records.go 项目: grunya404/dns
/*
	Search is used for retrieving records for a domain. It accepts the following
	GET params:

	+ duuid 	- uuid of domain
	+ domain	- string of domain
	+ name		- string of name

*/
func Search(w http.ResponseWriter, r *http.Request, params url.Values, limit, offset int) {
	query := db.SELECT
	var where []string
	var args []interface{}
	i := 1
	for k, _ := range params {
		switch k {
		case "name":
			where = append(where, fmt.Sprintf(k+" = $%d", i))
			args = append(args, params.Get(k))
			i++
		case "duuid", "domain":
			where = append(where, fmt.Sprintf("domain = $%d", i))
			args = append(args, params.Get(k))
			v := params.Get(k)
			params.Del("duuid")
			params.Del("domain")
			params.Set("domain", v)
			i++
		}
	}
	if len(where) > 0 {
		query += "WHERE " + strings.Join(where, " AND ") + " "
	}
	query += fmt.Sprintf("ORDER BY added DESC LIMIT $%d OFFSET $%d", len(args)+1, len(args)+2)
	args = append(args, limit, offset)
	log.Info("Query: " + query)
	log.Info("Args: %+v", args)
	recordList, err := db.GetList(query, args...)
	if err != nil {
		util.Error(err, w)
		return
	}
	// check for none sql errors
	// if we have no results dispatch a worker to get one
	if len(recordList) == 0 {
		// no domain lets grab one using what we assume is a duuid
		log.Info("no records")
		if duuid := params.Get("domain"); duuid != "" {
			log.Info("duuid: " + duuid)
			domain, err := domains.GetByUUID(duuid).One()
			if err != nil {
				log.Info("herererere")
				util.Error(err, w)
				return
			}
			results := <-dispatcher.AddDomain(domain)
			log.Info("%v", results)
			for _, result := range results {
				recordList = append(recordList, result)
			}
		}
	}
	util.ToJSON(cleanParserFromRecords(recordList), err, w)
}
示例#5
0
文件: domains.go 项目: grunya404/dns
func ByUUID(fn func(http.ResponseWriter, *http.Request, db.Domain)) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		uuid := vars["uuid"]
		domain, err := db.GetByUUID(uuid).One()
		if err != nil {
			util.Error(err, w)
			return
		}
		fn(w, r, domain)
	}
}
示例#6
0
文件: get.go 项目: grunya404/dns
func parseRow(row connection.Row) (Record, error) {
	w := Record{}
	var duuid, wuuid string
	var emails, organizations, phones, postcodes, names []byte
	err := row.Scan(
		&w.ID,
		&duuid,
		&w.Data,
		&w.Raw,
		&w.Contacts,
		&emails,
		&w.Added,
		&wuuid,
		&organizations,
		&phones,
		&postcodes,
		&names,
	)
	if err != nil {
		return w, err
	}
	err = json.Unmarshal(emails, &w.Emails)
	if err != nil {
		return w, err
	}
	err = json.Unmarshal(organizations, &w.Organizations)
	if err != nil {
		return w, err
	}
	err = json.Unmarshal(phones, &w.Phones)
	if err != nil {
		return w, err
	}
	err = json.Unmarshal(postcodes, &w.Postcodes)
	if err != nil {
		return w, err
	}
	err = json.Unmarshal(names, &w.Emails)
	if err != nil {
		return w, err
	}
	w.Domain, err = domains.GetByUUID(duuid).One()
	if err != nil {
		return w, err
	}
	w.UUID = uuid.Parse(wuuid)
	return w, nil
}
示例#7
0
文件: whois.go 项目: grunya404/dns
/*
	Search is used for retrieving whois records for a domain. It accepts the following
	GET params:

	+ uuid		- uuid of record
	+ duuid 	- uuid of domain
	+ domain	- string of domain

*/
func Search(w http.ResponseWriter, r *http.Request, params url.Values, limit, offset int) {
	query := db.SELECT
	var domain domains.Domain
	var where []string
	var args []interface{}
	var orderBy string
	i := 1
	for k, _ := range params {
		switch k {
		case "orderBy":
			orderBy = fmt.Sprintf("ORDER BY %s ", params.Get(k))
			if v, ok := params["order"]; ok {
				order := strings.ToLower(v[0])
				if order == "desc" {
					orderBy += "DESC "
				} else if order == "asc" {
					orderBy += "ASC "
				}
			}
		case "uuid":
			where = append(where, fmt.Sprintf(k+" = $%d", i))
			args = append(args, params.Get(k))
			i++
		case "duuid", "domain":
			where = append(where, fmt.Sprintf("domain = $%d", i))
			args = append(args, params.Get(k))
			v := params.Get(k)
			params.Del("duuid")
			params.Del("domain")
			params.Set("domain", v)
			i++
		case "name":
			name, tld, err := tlds.DetectDomainAndTLD(params.Get(k))
			if err != nil {
				util.Error(err, w)
				return
			}
			domain, err = domains.GetByNameAndTLD(name, tld).One()
			if err != nil {
				domain = domains.New(name, tld)
				err = domain.Insert()
				if err != nil {
					util.Error(err, w)
					return
				}
			}
			where = append(where, fmt.Sprintf("domain = $%d", i))
			args = append(args, domain.UUID.String())
			i++
		case "email":
			where = append(where, fmt.Sprintf("emails ? $%d", i))
			args = append(args, params.Get(k))
			i++
		case "raw":
			where = append(where, fmt.Sprintf("raw_whois ->>0 ILIKE $%d", i))
			args = append(args, "%"+params.Get(k)+"%")
			i++
		}
	}
	if len(where) > 0 {
		query += "WHERE " + strings.Join(where, " AND ") + " "
	}
	if len(orderBy) > 0 {
		query += orderBy + " "
	}
	query += fmt.Sprintf("ORDER BY added DESC LIMIT $%d OFFSET $%d", len(args)+1, len(args)+2)
	args = append(args, limit, offset)
	log.Info("Query: " + query)
	log.Info("Args: %+v", args)
	recordList, err := db.GetList(query, args...)
	if err != nil {
		panic(err)
	}
	// check for non sql errors
	// if we have no results dispatch a worker to get one
	if len(recordList) == 0 {
		if len(domain.Name) == 0 {
			// no domain lets grab one using what we assume is a duuid
			if duuid := params.Get("domain"); duuid != "" {
				log.Info("duuid: " + duuid)
				domain, err = domains.GetByUUID(duuid).One()
				if err != nil {
					util.Error(err, w)
					return
				}
			}
		}
		if len(domain.Name) == 0 {
			log.Error("Unable to detect domain for whois lookup, params: %+v", params)
			util.Error(errors.New("Unable to find domain for whois lookup."), w)
			return
		}
		result := <-dispatcher.AddDomain(domain)
		recordList = append(recordList, result)
	}
	util.ToJSON(recordList, err, w)
}