Пример #1
0
func (l *Listener) handshake(conn *blend.Conn) (sourceAddr *Addr, err error) {
	if conn == nil {
		return nil, errors.NewError("listener off")
	}
	defer conn.Close()
	//
	var msg interface{}
	msg, err = conn.Read()
	if err != nil {
		return nil, err
	}
	defer func() {
		if err != nil {
			conn.Write(&RejectMsg{err})
		} else {
			err = conn.Write(&WelcomeMsg{})
		}
	}()
	hello, ok := msg.(*HelloMsg)
	if !ok {
		log.Println("rejecting", conn.RemoteAddr().String(), "unknown hello message type")
		return nil, errors.NewError("rejecting unknown hello type")
	}
	// Accept user connections
	da, ok := hello.SourceAddr.(*Addr)
	if !ok {
		log.Println("rejecting", conn.RemoteAddr().String(), "unknown source address type")
		return nil, errors.NewError("rejecting unknown source address type")
	}
	reverseAddr(da, conn.RemoteAddr())
	la, ok := hello.TargetAddr.(*Addr)
	if !ok {
		log.Println("rejecting ", conn.RemoteAddr().String(), "unknown target address type")
		return nil, errors.NewError("rejecting unknown target address type")
	}
	if la.WorkerID() != l.addr.WorkerID() {
		log.Println("rejecting", conn.RemoteAddr().String(), "due to worker identity mismatch")
		return nil, errors.NewError("rejecting worker identity mismatch, looks for %s, got %s", la.WorkerID(), l.addr.WorkerID())
	}
	if la.PID != os.Getpid() {
		log.Println("rejecting", conn.RemoteAddr().String(), "due to worker PID mismatch")
		return nil, errors.NewError("rejecting worker PID mismatch, looks for %d, got %d", la.PID, os.Getpid())
	}
	return da, nil
}
Пример #2
0
func (d *Dialer) auth(addr n.Addr, conn *blend.Conn) error {
	defer conn.Close()
	if err := conn.Write(&HelloMsg{
		SourceAddr: d.dialback,
		TargetAddr: addr,
	}); err != nil {
		return err
	}
	msg, err := conn.Read()
	if err != nil {
		return err
	}
	switch q := msg.(type) {
	case *WelcomeMsg:
		return nil
	case *RejectMsg:
		return errors.NewError("dial rejected by remote (%s)", errors.Unpack(q.Err))
	}
	return errors.NewError("unknown welcome response")
}