예제 #1
0
파일: peer.go 프로젝트: cmars/conflux
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()
	}
}
예제 #2
0
파일: peer.go 프로젝트: cmars/conflux
func (r *Recover) HkpAddr() (string, error) {
	// Use remote HKP host:port as peer-unique identifier
	host, _, err := net.SplitHostPort(r.RemoteAddr.String())
	if err != nil {
		log.Errorf("cannot parse HKP remote address from %q: %v", r.RemoteAddr, err)
		return "", errgo.Mask(err)
	}
	if strings.Contains(host, ":") {
		host = fmt.Sprintf("[%s]", host)
	}
	return fmt.Sprintf("%s:%d", host, r.RemoteConfig.HTTPPort), nil
}
예제 #3
0
파일: main.go 프로젝트: squeed/openpgp
func main() {
	var matches, misses int
	var n int
	for opkr := range openpgp.ReadOpaqueKeyrings(os.Stdin) {
		match, miss, err := testKeyring(opkr)
		if err != nil {
			log.Errorf("key#%d: %v", n, errgo.Details(err))
		}
		matches += match
		misses += miss
	}
	log.Infof("matches=%d misses=%d\n", matches, misses)
}