// ClientLive method for accept new connection from socket
func ClientLive(buffers Buffers, conf core.Config, c net.Conn) {
	defer core.ErrorNetworkHandler(c)

	buffer := bytes.NewBuffer([]byte{})
	player := entitie.NewPlayer(c)
	newPill := pill.NewPill()

	for getBytes := range buffers.GetReadChannel() {
		buffer.Reset()
		buffer.Write(getBytes)

		log.WithField("bytes", fmt.Sprintf("% x", buffer.Bytes())).Info("Print message from client")

		if buffer.Len() <= 2 {
			buffers.GetWriteChannel() <- []byte{0x00, 0x02}
			continue
		}

		opcodes, err := newPill.Decrypt(buffer.Bytes(), player)
		if err != nil {
			log.WithError(err).Panic("Error in pill decrypt")
		}

		if opcodes != nil {
			for _, opcode := range opcodes {
				pillEncrypt, err := newPill.Encrypt(newPill.GetPill(opcode), player)
				if err != nil {
					log.WithError(err).Error("Error in pill encrypt")
				}

				buffers.GetWriteChannel() <- pillEncrypt
			}
		}
	}
}
Exemple #2
0
// ConnectHandler method for accept new connection from socket
func ConnectHandler(buffers *core.Buffers, app Application, c net.Conn) {
	defer core.ErrorNetworkHandler(c)

	var (
		buffer      *bytes.Buffer      = bytes.NewBuffer([]byte{})
		player      *entitie.Player    = entitie.NewPlayer(buffers)
		database    *database.Database = database.NewDatabase(&app.DatabaseInfo)
		packetAlloc *packet.Packet     = packet.NewPacket(player, database)
	)

	// Once send first a packet
	packet, err := packetAlloc.Encode(support.OP_SERVER_CHAPSTR)
	if err != nil {
		log.WithError(err).Error("Error in packet encode")
	}

	buffers.GetWC() <- packet

	for getBytes := range buffers.GetRC() {
		buffer.Reset()
		buffer.Write(getBytes)

		log.WithField("bytes", fmt.Sprintf("% x", buffer.Bytes())).Info("Print message from client")

		// Ping <-> pong
		if buffer.Len() <= 2 {
			buffers.GetWC() <- []byte{0x00, 0x02}
			continue
		}

		opcodes, err := packetAlloc.Decode(buffer.Bytes())
		if err != nil {
			log.WithError(err).Error("Error in packet decode")
			return
		}

		if len(opcodes) == 0 {
			continue
		}

		for _, opcode := range opcodes {
			response, err := packetAlloc.Encode(opcode)
			if err != nil {
				log.WithError(err).Error("Error in packet encode")
				break
			}

			buffers.GetWC() <- response
		}

		if player.Error != nil {
			// Before disconnect
			time.Sleep(time.Second)

			log.WithError(player.Error).Error("Client was rejected by God!")
			return
		}
	}
}
// ClientLive method for accept new connection from socket
func ClientLive(buffers Buffers, conf core.Config, c net.Conn) {
	defer core.ErrorNetworkHandler(c)

	buffer := bytes.NewBuffer([]byte{})
	player := entitie.NewPlayer(c)
	pillInit := pill.NewPill()

	for getBytes := range buffers.GetReadChannel() {
		buffer.Write(getBytes)

		if conf.Base.Test {
			log.Panic("Client is break =_=")
		} else {
			log.WithField("bytes", fmt.Sprintf("% x", buffer.Bytes())).Info("Print message from client")

			if buffer.Len() >= 8 {
				opcodes, err := pillInit.Decrypt(buffer.Bytes(), *player)
				if err != nil {
					log.WithError(err).Panic("Error in pill decrypt")
				}

				if opcodes != nil {
					for _, v := range opcodes {
						pillEncrypt, err := pillInit.Encrypt(pillInit.SetOpcode(v).GetOutcomingCrumb())
						if err != nil {
							log.WithError(err).Error("Error in pill encrypt")
						}

						buffers.GetWriteChannel() <- pillEncrypt
					}
				}
			} else {
				buffers.GetWriteChannel() <- []byte{0x00, 0x02}
			}
		}

		buffer.Reset()
	}
}