Esempio n. 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
}
Esempio n. 2
0
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
}
Esempio n. 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
}
Esempio n. 4
0
func main() {

	runtime.GOMAXPROCS(runtime.NumCPU())
	if config.LOG {
		logDirectory := config.LOGDIR
		if len(config.LOGDIR) > 0 && config.LOGDIR[len(config.LOGDIR)-1] != '/' {
			logDirectory = logDirectory + "/"
		}
		f, err := os.OpenFile(logDirectory+"log_"+time.Now().Format("02-01-2006_15-04")+".txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
		if err != nil {
			log.Fatalf("error opening file: %v", err)
		}
		defer f.Close()
		log.SetOutput(f)
	}

	var serviceId uint8 = 1
	serviceName := "ServiceTestName"
	tunnelHost := "core.craftolution.de"
	tunnelService := 1
	tunnelPort := 8123

	p := core.ConstructPacket(nil, serviceId, core.PACKETTYPE_CONNECT, 1, []byte{})
	p.AppendContent(utils.UintToByte(uint(len(serviceName)), 1))
	p.AppendContent([]byte(serviceName))
	p.AppendContent(utils.UintToByte(uint(len(tunnelHost)), 1))
	p.AppendContent([]byte(tunnelHost))
	p.AppendContent(utils.UintToByte(uint(tunnelService), 1))
	p.AppendContent(utils.UintToByte(uint(tunnelPort), 2))
	p.Prepare()

	master := core.NewMaster()
	if master == nil {
		return
	}

	var cmds []commands.Command = []commands.Command{}
	cmds = append(cmds, commands.NewConnectCommand(master))
	cmds = append(cmds, commands.NewDisconnectCommand(master))
	cmds = append(cmds, commands.NewSubscribeCommand(master))
	cmds = append(cmds, commands.NewUnsubscribeCommand(master))
	cmds = append(cmds, commands.NewListServicesCommand(master))
	cmds = append(cmds, commands.NewAskTunnelCommand(master))
	cmds = append(cmds, commands.NewSyncTimeCommand(master))
	cmds = append(cmds, commands.NewNothingCommand(master))

	clients := []*net.TCPConn{}

	go func() {
		if !config.RUN_TEST {
			return
		}

		utils.Print("Running test, program will quit automatically when the test is finished. (ETA: 1 min)")
		address, err := net.ResolveTCPAddr("tcp", config.CORE_HOST+":"+strconv.Itoa(config.CORE_PORT))
		if err != nil {
			utils.Print(err)
		}

		time.Sleep(time.Duration(3) * time.Second)
		for i := 0; i < 100; i++ {
			conn, err := net.DialTCP("tcp", nil, address)
			if err != nil {
				utils.Print(err)
				continue
			}
			serviceName := "Service " + strconv.Itoa(i)
			connectPacket := core.ConstructPacket(nil, uint8(i), core.PACKETTYPE_CONNECT, 1, []byte{})
			connectPacket.AppendContent(utils.UintToByte(uint(len(serviceName)), 1))
			connectPacket.AppendContent([]byte(serviceName))
			connectPacket.AppendContent(utils.UintToByte(uint(len(tunnelHost)), 1))
			connectPacket.AppendContent([]byte(tunnelHost))
			connectPacket.AppendContent(utils.UintToByte(uint(tunnelService), 1))
			connectPacket.AppendContent(utils.UintToByte(uint(tunnelPort), 2))
			conn.Write(connectPacket.Prepare())
			clients = append(clients, conn)
			time.Sleep(time.Duration(10) * time.Millisecond)

			listServicesPacket := core.ConstructPacket(nil, uint8(i), core.PACKETTYPE_LIST_SERVICES, 2, []byte{})
			conn.Write(listServicesPacket.Prepare())
			time.Sleep(time.Duration(10) * time.Millisecond)
		}

		time.Sleep(time.Duration(3) * time.Second)
		for i := 0; i < 100; i++ {
			conn := clients[i]
			disconnectPacket := core.ConstructPacket(nil, uint8(i), core.PACKETTYPE_DISCONNECT, 3, []byte{})
			conn.Write(disconnectPacket.Prepare())
			time.Sleep(time.Duration(10) * time.Millisecond)
		}

		time.Sleep(time.Duration(3) * time.Second)
		for i := 0; i < 100; i++ {
			clients[i].Close()
			time.Sleep(time.Duration(10) * time.Millisecond)
		}

		time.Sleep(time.Duration(3) * time.Second)
		clients := []*net.TCPConn{}
		for i := 1; i < 5; i++ {
			conn, err := net.DialTCP("tcp", nil, address)
			if err != nil {
				utils.Print(err)
				continue
			}
			serviceName := "Service " + strconv.Itoa(i)
			connectPacket := core.ConstructPacket(nil, uint8(i), core.PACKETTYPE_CONNECT, 1, []byte{})
			connectPacket.AppendContent(utils.UintToByte(uint(len(serviceName)), 1))
			connectPacket.AppendContent([]byte(serviceName))
			connectPacket.AppendContent(utils.UintToByte(uint(len(tunnelHost)), 1))
			connectPacket.AppendContent([]byte(tunnelHost))
			connectPacket.AppendContent(utils.UintToByte(uint(4), 1))
			connectPacket.AppendContent(utils.UintToByte(uint(1234), 2))
			connectPacket.AppendContent(utils.UintToByte(uint(2), 1))
			connectPacket.AppendContent(utils.UintToByte(uint(1235), 2))
			conn.Write(connectPacket.Prepare())
			clients = append(clients, conn)
			time.Sleep(time.Duration(10) * time.Millisecond)

			askTunnelPacket := core.ConstructPacket(nil, uint8(i), core.PACKETTYPE_ASK_TUNNEL, 2, []byte{})
			askTunnelPacket.AppendContent(utils.UintToByte(1, 1))
			askTunnelPacket.AppendContent(utils.UintToByte(2, 1))
			askTunnelPacket.AppendContent(utils.UintToByte(3, 1))
			askTunnelPacket.AppendContent(utils.UintToByte(4, 1))
			conn.Write(askTunnelPacket.Prepare())
			time.Sleep(time.Duration(10) * time.Millisecond)
		}

		time.Sleep(time.Duration(3) * time.Second)
		subscribePacket := core.ConstructPacket(nil, uint8(1), core.PACKETTYPE_SUBSCRIBE, 3, []byte{})
		subscribePacket.AppendContent(utils.UintToByte(14, 1))
		clients[0].Write(subscribePacket.Prepare())

		subscribePacket.ServiceId = uint8(4)
		subscribePacket.AppendContent(utils.UintToByte(1, 1))
		subscribePacket.AppendContent(utils.UintToByte(11, 1))
		clients[3].Write(subscribePacket.Prepare())

		time.Sleep(time.Duration(3) * time.Second)
		dataPacket := core.ConstructPacket(nil, uint8(2), uint8(13), 3, []byte{})
		dataPacket.AppendContent([]byte{8, 3, 7})
		for i := 10; i < 15; i++ {
			dataPacket.Next()
			dataPacket.PacketType = uint8(i)
			clients[1].Write(dataPacket.Prepare())
			time.Sleep(time.Duration(10) * time.Millisecond)
		}

		time.Sleep(time.Duration(3) * time.Second)
		disconnectPacket := core.ConstructPacket(nil, uint8(3), core.PACKETTYPE_DISCONNECT, 4, []byte{})
		clients[2].Write(disconnectPacket.Prepare())

		// no disconnect packets for other clients, test master functionality...
		time.Sleep(time.Duration(3) * time.Second)
		for i := 0; i < 4; i++ {
			clients[i].Close()
			time.Sleep(time.Duration(10) * time.Millisecond)
		}

		time.Sleep(time.Duration(3) * time.Second)
		master.Quit()

	}()

	master.Listen()

}