Exemplo n.º 1
0
func (p *Peer) flush() {
	p.muElements.Lock()

	for _, z := range p.insertElements {
		err := p.ptree.Insert(z)
		if err != nil {
			log.Warningf("cannot insert %q into prefix tree: %v", z, errgo.Details(err))
		}
	}
	if len(p.insertElements) > 0 {
		p.logFields("mutate", log.Fields{"elements": len(p.insertElements)}).Debugf("inserted")
	}

	for _, z := range p.removeElements {
		err := p.ptree.Remove(z)
		if err != nil {
			log.Warningf("cannot remove %q from prefix tree: %v", z, errgo.Details(err))
		}
	}
	if len(p.removeElements) > 0 {
		p.logFields("mutate", log.Fields{"elements": len(p.removeElements)}).Debugf("removed")
	}

	p.insertElements = nil
	p.removeElements = nil
	if p.mutatedFunc != nil {
		p.mutatedFunc()
	}
	p.muElements.Unlock()
}
Exemplo n.º 2
0
func (st *storage) createIndexes() {
	for _, crIndexSQL := range crIndexesSQL {
		_, err := st.Exec(crIndexSQL)
		if err != nil {
			log.Warningf("error executing %q: %v", crIndexSQL, err)
		}
	}
}
Exemplo n.º 3
0
func (p *Peer) Serve() error {
	addr, err := p.settings.ReconNet.Resolve(p.settings.ReconAddr)
	if err != nil {
		return errgo.Mask(err)
	}
	matcher, err := p.settings.Matcher()
	if err != nil {
		log.Errorf("cannot create matcher: %v", err)
		return errgo.Mask(err)
	}

	ln, err := net.Listen(addr.Network(), addr.String())
	if err != nil {
		return errgo.Mask(err)
	}
	p.t.Go(func() error {
		<-p.t.Dying()
		return ln.Close()
	})

	for {
		conn, err := ln.Accept()
		if err != nil {
			return errgo.Mask(err)
		}

		if tcConn, ok := conn.(*net.TCPConn); ok {
			tcConn.SetKeepAlive(true)
			tcConn.SetKeepAlivePeriod(3 * time.Minute)

			remoteAddr := tcConn.RemoteAddr().(*net.TCPAddr)
			if !matcher.Match(remoteAddr.IP) {
				log.Warningf("connection rejected from %q", remoteAddr)
				conn.Close()
				continue
			}
		}

		p.muDie.Lock()
		if p.isDying() {
			conn.Close()
			return nil
		}
		p.t.Go(func() error {
			err = p.Accept(conn)
			if errgo.Cause(err) == ErrPeerBusy {
				p.logErr(GOSSIP, err).Debug()
			} else if err != nil {
				p.logErr(SERVE, err).Errorf("recon with %v failed", conn.RemoteAddr())
			}
			return nil
		})
		p.muDie.Unlock()
	}
}
Exemplo n.º 4
0
func testKeyring(opkr *openpgp.OpaqueKeyring) (int, int, error) {
	var buf bytes.Buffer
	for _, op := range opkr.Packets {
		err := op.Serialize(&buf)
		if err != nil {
			return 0, 0, errgo.Mask(err)
		}
	}
	pk, err := opkr.Parse()
	if err != nil {
		return 0, 0, errgo.Mask(err)
	}
	dupDigest, err := openpgp.SksDigest(pk, md5.New())
	if err != nil {
		return 0, 0, errgo.Mask(err)
	}

	err = openpgp.DropDuplicates(pk)
	if err != nil {
		return 0, 0, errgo.Mask(err)
	}
	dedupDigest, err := openpgp.SksDigest(pk, md5.New())
	if err != nil {
		return 0, 0, errgo.Mask(err)
	}
	cmd := exec.Command("./sks_hash")
	var out bytes.Buffer
	cmd.Stdin = bytes.NewBuffer(buf.Bytes())
	cmd.Stdout = &out
	err = cmd.Run()
	if err != nil {
		return 0, 0, errgo.Mask(err)
	}
	sksDigest := strings.ToLower(strings.TrimSpace(string(out.Bytes())))
	if dedupDigest != sksDigest {
		log.Warningf("hkp=%q hkp_dedup=%q sks=%q", dupDigest, dedupDigest, sksDigest)
		var out bytes.Buffer
		armw, err := armor.Encode(&out, xopenpgp.PublicKeyType, nil)
		if err != nil {
			return 0, 1, errgo.Mask(err)
		}
		armw.Write(buf.Bytes())
		armw.Close()
		fmt.Println(out.String())
		return 0, 1, nil
	}
	return 1, 0, nil
}
Exemplo n.º 5
0
func (p *Peer) setReadDeadline(conn net.Conn, d time.Duration) {
	err := conn.SetReadDeadline(time.Now().Add(d))
	if err != nil {
		log.Warningf("failed to set read deadline: %v")
	}
}