Beispiel #1
0
func (c *SyncTimeCommand) Handle(packet *core.Packet, service *core.Service) error {
	utils.Debug("SYNC_TIME packet: ", packet)
	if service == nil {
		return errors.New("SYNC_TIME error: There is no service for this serviceId connected.")
	}
	timeBytes, err := time.Now().MarshalText()
	if err != nil {
		return err
	}
	answerPacket := core.ConstructPacket(packet.Sender, packet.ServiceId, packet.PacketType, packet.PacketId, []byte{})
	answerPacket.AppendContent(timeBytes)
	service.Write(answerPacket)
	return nil
}
func (c *ListServicesCommand) Handle(packet *core.Packet, service *core.Service) error {
	utils.Debug("LIST_SERVICES packet: ", packet)
	if service == nil {
		return errors.New("LIST_SERVICES error: There is no service for this serviceId connected.")
	}
	if len(packet.Content) > 0 {
		return errors.New("LIST_SERVICES error: The packet data is too long.")
	}

	answerPacket := core.ConstructPacket(packet.Sender, packet.ServiceId, packet.PacketType, packet.PacketId, []byte{})
	services := c.GetMaster().Services
	for _, service := range services {
		answerPacket.AppendContent(utils.UintToByte(uint(len(service.Name)+1), 2))
		answerPacket.AppendContent(utils.UintToByte(uint(service.ServiceId), 1))
		answerPacket.AppendContent([]byte(service.Name))
	}
	service.Write(answerPacket)
	return nil
}
Beispiel #3
0
func (c *AskTunnelCommand) Handle(packet *core.Packet, service *core.Service) error {
	utils.Debug("ASK_TUNNEL packet: ", packet)
	if service == nil {
		return errors.New("ASK_TUNNEL error: There is no service for this serviceId connected.")
	}
	if len(packet.Content) <= 0 {
		return errors.New("ASK_TUNNEL error: The packet data is too small.")
	}
	answerPacket := core.ConstructPacket(packet.Sender, packet.ServiceId, packet.PacketType, packet.PacketId, []byte{})
	services := c.GetMaster().Services
	for i := 0; i < len(packet.Content); i++ {
		tunnelRequest := utils.ByteToUint8(packet.Content[i])
		for _, service := range services {
			if port, ok := service.TunnelPorts[packet.ServiceId]; ok && service.ServiceId == tunnelRequest {
				answerPacket.AppendContent(utils.UintToByte(uint(len(service.TunnelHost)+3), 2))
				answerPacket.AppendContent(utils.UintToByte(uint(service.ServiceId), 1))
				answerPacket.AppendContent(utils.UintToByte(port, 2))
				answerPacket.AppendContent([]byte(service.TunnelHost))
			}
		}
	}
	service.Write(answerPacket)
	return nil
}