Example #1
0
func (self *Client) sockCall(method string, ret interface{}, params ...interface{}) (err error) {

	if params != nil {
		self.request.Params = params
	} else {
		self.request.Params = []string{}
	}
	self.request.Id = rand.Uint32()
	self.request.Method = method
	self.request.Protocol.Id = self.request.Id
	self.request.Protocol.MagicNumber = uint32(self.opt[CLIENT_MAGIC_NUM].(uint32))

	var pack []byte

	if len(self.opt[CLIENT_PACKAGER].(string)) < 8 {
		for i := 0; i < len(self.opt[CLIENT_PACKAGER].(string)); i++ {
			self.request.Protocol.Packager[i] = self.opt[CLIENT_PACKAGER].(string)[i]
		}
	}

	pack, err = packager.Pack([]byte(self.opt[CLIENT_PACKAGER].(string)), self.request)

	if err != nil {
		return err
	}

	self.request.Protocol.BodyLength = uint32(len(pack) + PACKAGER_LENGTH)
	conn, conn_err := self.transport.Connection()

	if conn_err != nil {
		return conn_err
	}

	conn.Write(self.request.Protocol.Bytes().Bytes())
	conn.Write(pack)
	protocol_buffer := make([]byte, PROTOCOL_LENGTH+PACKAGER_LENGTH)
	conn.Read(protocol_buffer)
	self.request.Protocol.Init(bytes.NewBuffer(protocol_buffer))
	body_buffer := make([]byte, self.request.Protocol.BodyLength-PACKAGER_LENGTH)
	conn.Read(body_buffer)
	response := new(Response)
	err = packager.Unpack([]byte(self.opt[CLIENT_PACKAGER].(string)), body_buffer, &response)

	if response.Status != ERR_OKEY {
		return errors.New(response.Error)
	}
	//这里需要优化,需要干掉这次pack/unpack
	pack_data, err := packager.Pack(self.request.Protocol.Packager[:], response.Retval)
	err = packager.Unpack(self.request.Protocol.Packager[:], pack_data, ret)

	return err
}
Example #2
0
func (self *Server) sendResponse(response *Response) error {

	sendPackData, err := packager.Pack(response.Protocol.Packager[:], response)
	if err != nil {
		return err
	}

	encrypt := false
	encryptKey := ""

	e, ok := self.opt[SERVER_ENCRYPT]

	if ok == true {
		encrypt = e.(bool)
	}

	if encrypt {

		e, ok := self.opt[SERVER_ENCRYPT_PRIVATE_KEY]
		if ok == true {
			encryptKey = e.(string)
		}

		if len(encryptKey) < 1 {
			return errors.New("encrypt_private_key is empty.")
		}

		encryptBody := &EncryptBody{
			Key: []byte(encryptKey),
		}

		encryptBody.Encrypt(sendPackData)

		temp := bytes.NewBufferString("")

		binary.Write(temp, binary.BigEndian, encryptBody.BodyLen)
		binary.Write(temp, binary.BigEndian, encryptBody.RealLen)
		temp.Write(encryptBody.Body[:encryptBody.BodyLen])
		sendPackData = temp.Bytes()
	}

	response.Protocol.BodyLength = uint32(len(sendPackData) + 8)
	self.ctx.ResponseWriter.Write(response.Protocol.Bytes().Bytes())
	self.ctx.ResponseWriter.Write(sendPackData)
	return nil

}
Example #3
0
func (self *Client) httpCall(method string, ret interface{}, params ...interface{}) (err error) {

	if params != nil {
		self.request.Params = params
	} else {
		self.request.Params = []string{}
	}

	self.request.Id = rand.Uint32()
	self.request.Method = method
	self.request.Protocol.Id = self.request.Id
	self.request.Protocol.MagicNumber = uint32(self.opt[CLIENT_MAGIC_NUM].(uint32))

	var pack []byte

	if len(self.opt[CLIENT_PACKAGER].(string)) < 8 {

		for i := 0; i < len(self.opt[CLIENT_PACKAGER].(string)); i++ {
			self.request.Protocol.Packager[i] = self.opt[CLIENT_PACKAGER].(string)[i]
		}
	}

	pack, err = packager.Pack([]byte(self.opt[CLIENT_PACKAGER].(string)), self.request)

	if err != nil {
		return errors.New("[YarClient httpCall] Pack Params Error: " + err.Error())
	}

	e, ok := self.opt[CLIENT_ENCRYPT]

	encrypt := false
	encrypt_key := ""

	if ok == true {
		encrypt = e.(bool)
	}

	if encrypt {

		e, ok := self.opt[CLIENT_ENCRYPT_PRIVATE_KEY]

		if ok == false {
			return errors.New("encrypt_private_key empty.")
		}
		encrypt_key = e.(string)
	}

	if encrypt {

		self.request.Protocol.Encrypt = 1
		encryptBody := &EncryptBody{
			Key: []byte(encrypt_key),
		}

		err := encryptBody.Encrypt(pack)

		if err != nil {
			return errors.New("[Encrypt] error:" + err.Error())
		}

		encryptPack := bytes.NewBufferString("")

		binary.Write(encryptPack, binary.BigEndian, encryptBody.BodyLen)
		binary.Write(encryptPack, binary.BigEndian, encryptBody.RealLen)
		encryptPack.Write(encryptBody.Body)
		pack = encryptPack.Bytes()
	}

	self.request.Protocol.BodyLength = uint32(len(pack) + PACKAGER_LENGTH)

	post_buffer := bytes.NewBuffer(self.request.Protocol.Bytes().Bytes())
	post_buffer.Write(pack)

	//todo 停止验证HTTPS请求
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}

	httpClient := &http.Client{
		Transport: tr,
		Timeout:   time.Duration(self.opt[CLIENT_TIMEOUT].(int)) * time.Millisecond,
	}

	resp, err := httpClient.Post(self.hostname, "application/json", post_buffer)

	if err != nil {
		return errors.New("[YarClient httpCall] Http Post Error: " + err.Error())
	}

	allBody, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		return errors.New("[YarClient httpCall] Http Response Error: " + err.Error())
	}

	protocol_buffer := allBody[0 : PROTOCOL_LENGTH+PACKAGER_LENGTH]

	self.request.Protocol.Init(bytes.NewBuffer(protocol_buffer))

	bodyTotal := self.request.Protocol.BodyLength - PACKAGER_LENGTH

	if uint32(len(allBody)-PROTOCOL_LENGTH+PACKAGER_LENGTH) < bodyTotal {
		return errors.New("[YarClient httpCall] Http Response Content Error:" + string(allBody))
	}

	body_buffer := allBody[PROTOCOL_LENGTH+PACKAGER_LENGTH:]

	if self.request.Protocol.Encrypt == 1 {

		encryptBody := &EncryptBody{
			Key:  []byte(encrypt_key),
			Body: body_buffer[8:],
		}

		decryptBuffer := bytes.NewReader(body_buffer[:8])

		binary.Read(decryptBuffer, binary.BigEndian, &encryptBody.BodyLen)
		binary.Read(decryptBuffer, binary.BigEndian, &encryptBody.RealLen)

		data, err := encryptBody.Decrypt()

		if err != nil {
			return errors.New("[Decrypt] error:" + err.Error())
		}

		body_buffer = data[:encryptBody.RealLen]
	}

	response := new(Response)
	err = packager.Unpack([]byte(self.opt[CLIENT_PACKAGER].(string)), body_buffer, &response)

	if response.Status != ERR_OKEY {
		return errors.New(fmt.Sprintf("[YarClient httpCall] Yar Response Error: %s %d", response.Error, response.Status))
	}

	//这里需要优化,需要干掉这次pack/unpack
	pack_data, err := packager.Pack(self.request.Protocol.Packager[:], response.Retval)
	if err != nil {
		return errors.New("[YarClient httpCall] Pack Data Error: " + err.Error())
	}

	err = packager.Unpack(self.request.Protocol.Packager[:], pack_data, ret)

	if err != nil {
		return errors.New("[YarClient httpCall] Unpack Data Error: " + err.Error())
	}

	return nil
}