Ejemplo n.º 1
0
func (r UserResource) updateOne(req *restful.Request, resp *restful.Response) {
	updatedUser := new(db.User)
	err := req.ReadEntity(updatedUser)
	if err != nil { // bad request
		resp.WriteErrorString(http.StatusBadRequest, err.Error())
		return
	}
	dbmap, err := db.OpenDbmap()
	if err != nil {
		resp.WriteErrorString(http.StatusInternalServerError, err.Error())
		return
	}
	defer db.CloseDbmap(dbmap)

	//dbmap.
}
Ejemplo n.º 2
0
func doForwardToResolver(server *net.UDPConn, forwardConns []*net.UDPConn, resolverAddrs []string, recvMsgChannel chan RecvMsg) {
	buf := make([]byte, MAX_BUF_LEN)
	log.Println("start doForwardToResolver")
	addrIndex := 0
	connIndex := 0
	rand.Seed(time.Now().UTC().UnixNano())
	for recvMsg := range recvMsgChannel {
		//unpack message
		var msg dns.Message
		_, err := msg.UnpackHeaderAndQuestion(recvMsg.data)
		if err != nil {
			log.Println(err)
			continue
		}
		//if only one question in message
		if len(msg.Question) == 1 {
			//check whether the domain name is in record manager
			question := &msg.Question[0]
			var records []db.Record
			dbmap, err := db.OpenDbmap()
			if err != nil {
				log.Println(err)
				continue
			}
			defer db.CloseDbmap(dbmap)
			_, err = dbmap.Select(&records, "select * from record where name=? and class=? and type=?", question.Name, question.Class, question.Type)
			if err != nil {
				log.Println(err)
				continue
			}
			//construct an answer record and send to client
			if len(records) > 0 {
				msg.Hdr.Rcode = dns.RCODE_SUCCESS
				msg.Hdr.QueryResponse = dns.QR_RESPONSE
				msg.Hdr.RecursionAvailable = true
				for _, record := range records {
					rr, err := RRConstruct(&record)
					if err != nil {
						log.Println(err)
						continue
					}
					msg.Answer = append(msg.Answer, rr)
				}
				log.Println("construct answer ")
				buflen, err := msg.Pack(buf, true)
				_, err = server.WriteToUDP(buf[:buflen], &recvMsg.addr)
				if err != nil {
					log.Println(err)
					continue
				}
				continue
			}
		}
		//forward to upstream resolver
		addr, err := net.ResolveUDPAddr("udp", resolverAddrs[addrIndex])
		if err != nil {
			log.Println(err)
			continue
		}
		log.Println("write request to resolver")
		conn := forwardConns[connIndex]
		//modify id
		newId := uint16(rand.Int())
		recvMsg.data[0] = uint8(newId >> 8)
		recvMsg.data[1] = uint8(newId)
		_, err = conn.WriteToUDP(recvMsg.data, addr)
		if err != nil {
			log.Println(err)
			continue
		}
		connIndex += 1
		if connIndex >= len(forwardConns) {
			connIndex = 0
		}
		addrIndex += 1
		if addrIndex >= len(resolverAddrs) {
			addrIndex = 0
		}

		var key SessionKey
		key.forwardConn = conn
		key.name = (&msg.Question[0]).Name
		key.id = newId

		var value SessionValue
		value.clientAddr = recvMsg.addr
		value.id = msg.Hdr.Id
		value.time = time.Now().Unix()

		gsession.Lock()
		gsession.buffer[key] = value
		gsession.Unlock()
	}
}