Пример #1
0
func (handler *_TunnelClient) Active(context gorpc.Context) error {

	handler.agents = make(map[string]*_Agent)

	handler.system.addTunnel(handler.name, handler, context.Pipeline())

	// send TunnelWhoAmI

	whoAmI := gorpc.NewTunnelWhoAmI()

	whoAmI.Services = handler.system.system.AgentServices()

	var buff bytes.Buffer

	gorpc.WriteTunnelWhoAmI(&buff, whoAmI)

	message := gorpc.NewMessage()

	message.Code = gorpc.CodeTunnelWhoAmI

	message.Content = buff.Bytes()

	context.Send(message)

	return nil
}
Пример #2
0
Файл: dh.go Проект: gsrpc/gorpc
func (handler *_CryptoClient) Active(context gorpc.Context) error {
	// create whoAmI message

	message := gorpc.NewMessage()

	message.Code = gorpc.CodeWhoAmI

	whoAmI := gorpc.NewWhoAmI()

	whoAmI.ID = handler.device

	whoAmI.Context = []byte(handler.dhKey.Exchange().String())

	var buff bytes.Buffer

	err := gorpc.WriteWhoAmI(&buff, whoAmI)

	if err != nil {
		context.Close()
		return err
	}

	message.Content = buff.Bytes()

	handler.V("send whoAmI handshake")

	context.Send(message)

	return gorpc.ErrSkip

}
Пример #3
0
Файл: dh.go Проект: gsrpc/gorpc
func (handler *_CryptoClient) MessageReceived(context gorpc.Context, message *gorpc.Message) (*gorpc.Message, error) {

	if message.Code == gorpc.CodeHeartbeat {
		return message, nil
	}

	if handler.block == nil {

		handler.V("expect handshake accept")

		if message.Code != gorpc.CodeAccept {

			handler.E("unexpect message(%s)", message.Code)

			context.Close()

			return nil, gserrors.Newf(gorpc.ErrRPC, "expect handshake(Accept) but got(%s)", message.Code)
		}

		handler.V("parse handshake accept")

		val, ok := new(big.Int).SetString(string(message.Content), 0)

		if !ok {
			context.Close()
			return nil, gserrors.Newf(gorpc.ErrRPC, "parse Accept#Content as big.Int error")
		}

		key := make([]byte, des.BlockSize)

		keyval := handler.dhKey.Gen(val).Uint64()

		binary.BigEndian.PutUint64(key[:8], keyval)

		handler.V("shared key \n\t%d\n\t%v ", keyval, key)

		block, err := des.NewCipher(key)

		if err != nil {
			context.Close()
			return nil, gserrors.Newf(err, "create new des Cipher error")
		}

		handler.block = block

		handler.V("%s handshake -- success", context.Name())

		context.FireActive()

		for _, message := range handler.cached {
			message, _ = handler.MessageSending(context, message)
			context.Send(message)
		}

		handler.cached = nil

		return nil, nil
	}

	if message.Code == gorpc.CodeHeartbeat {
		return message, nil
	}

	blocksize := handler.block.BlockSize()

	if len(message.Content)%blocksize != 0 {
		context.Close()
		return nil, gserrors.Newf(gorpc.ErrRPC, "%s invalid encrypt data", context)
	}

	blocks := len(message.Content) / blocksize

	for i := 0; i < blocks; i++ {
		offset := i * blocksize
		v := message.Content[offset : offset+blocksize]
		handler.block.Decrypt(v, v)
	}

	message.Content = PKCS5UnPadding(message.Content)

	return message, nil
}
Пример #4
0
Файл: dh.go Проект: gsrpc/gorpc
func (handler *_CryptoServer) MessageReceived(context gorpc.Context, message *gorpc.Message) (*gorpc.Message, error) {

	if message.Code == gorpc.CodeHeartbeat {
		return message, nil
	}

	if handler.block == nil {

		handler.V("expect WhoAmI message")
		// expect whoAmI message
		if message.Code != gorpc.CodeWhoAmI {
			context.Close()
			return nil, gserrors.Newf(gorpc.ErrRPC, "expect WhoAmI message but got(%s)", message.Code)
		}

		handler.V("parse WhoAmI message")

		whoAmI, err := gorpc.ReadWhoAmI(bytes.NewBuffer(message.Content))

		if err != nil {
			context.Close()
			return nil, err
		}

		val, ok := new(big.Int).SetString(string(whoAmI.Context), 0)

		if !ok {
			context.Close()
			return nil, gserrors.Newf(gorpc.ErrRPC, "parse WhoAmI#Context as big.Int error")
		}

		dhKey, err := handler.resovler.Resolve(whoAmI.ID)

		if err != nil {
			context.Close()
			return nil, err
		}

		message.Code = gorpc.CodeAccept

		message.Content = []byte(dhKey.Exchange().String())

		context.Send(message)

		key := make([]byte, des.BlockSize)

		keyval := dhKey.Gen(val).Uint64()

		binary.BigEndian.PutUint64(key[:8], keyval)

		handler.V("shared key \n\t%d\n\t%v ", keyval, key)

		block, err := des.NewCipher(key)

		if err != nil {
			context.Close()
			return nil, gserrors.Newf(err, "create new des Cipher error")
		}

		handler.block = block

		handler.device = whoAmI.ID

		context.FireActive()

		for _, message := range handler.cached {
			message, _ = handler.MessageSending(context, message)
			context.Send(message)
		}

		handler.cached = nil

		handler.V("%s handshake -- success", context.Name())

		return nil, nil

	}

	blocksize := handler.block.BlockSize()

	if len(message.Content)%blocksize != 0 {
		context.Close()
		return nil, gserrors.Newf(gorpc.ErrRPC, "invalid encrypt data")
	}

	blocks := len(message.Content) / blocksize

	for i := 0; i < blocks; i++ {
		offset := i * blocksize
		v := message.Content[offset : offset+blocksize]
		handler.block.Decrypt(v, v)
	}

	message.Content = PKCS5UnPadding(message.Content)

	return message, nil
}