Esempio n. 1
0
	def loop(self):
		while True:
			if self.sm.is_unknown():
				continue;
			elif self.sm.is_connected():
				print("Sending authentication data...");
				p = packet.AuthenticationPacket();
				p.set_username(bytearray(config["USERNAME"], encoding="ASCII"));
				p.set_password(bytearray(config["PASSWORD"], encoding="ASCII"));
				self.secure_socket.send(p.get_buffer());
				self.sm.waiting_for_authentication();
			elif self.sm.is_waiting_for_authentication():
				buf = bytearray(self.secure_socket.recv(self.buffer_size));
				if len(buf) > 0:
					p = packet.Packet(buf);
					if p.get_type() == packet.PACKET_TYPE_ACK:
						print("Authentication succeeded...");
						self.sm.authenticated();
					elif p.get_type() == packet.PACKET_TYPE_NACK:
						print("Authentication failed...");
						return;
			elif self.sm.is_authenticated():
				buf = bytearray(self.secure_socket.recv(self.buffer_size));
				if len(buf) > 0:
					p = packet.ConfigurationPacket(buf);
					if p.get_type() != packet.PACKET_TYPE_CONFIGURATION:
						continue;
					print("Got configuration packet...")
					if (utils.Utils.check_buffer_is_empty(p.get_ipv4_address()) or 
						utils.Utils.check_buffer_is_empty(p.get_netmask()) or 
						utils.Utils.check_buffer_is_empty(p.get_mtu())):
						print("Invalid configuration");
						break;
					self.tun = tun.Tun(config["TUN_NAME"],
						bytearray(p.get_ipv4_address()).decode(encoding="ASCII"), 
						bytearray(p.get_netmask()).decode(encoding="ASCII"), 
						struct.unpack("I", bytearray(p.get_mtu()))[0]);
					self.tun_mtu = struct.unpack("I", bytearray(p.get_mtu()))[0];
					self.routing_.configure_default_route(bytearray(p.get_ipv4_address()).decode(encoding="ASCII"));
					self.routing_.configure_tunnel_route(self.server_ip, self.default_gw);
					self.dns_.configure_dns(self.dns_server);
					self.sm.configured();
			elif self.sm.is_configured():
				self.tun_thread = threading.Thread(target = self.tun_loop);
				self.tls_thread = threading.Thread(target = self.tls_loop);
				self.tun_thread.daemon = True;
				self.tls_thread.daemon = True;
				self.tun_thread.start();
				self.tls_thread.start();
				self.sm.running();
			elif self.sm.is_running():
				sleep(10);
Esempio n. 2
0
    def loop(self):
        while True:
            if self.sm.is_unknown():
                try:
                    (sock, addr) = self.secure_sock.accept()
                    self.client_socket = sock
                    self.client_address = addr
                    print("Got connection from %s" % (self.client_address[0]))
                    self.sm.connected()
                except:
                    print("Could not open the socket...")
                    sleep(1)
                    continue
            elif self.sm.is_connected():
                buf = None
                try:
                    buf = bytearray(self.client_socket.recv(self.buffer_size))
                    if len(buf) == 0:
                        raise Exception("Socket was closed")
                except:
                    print("Failed to read from socket...")
                    self.client_socket.close()
                    self.sm.unknown()
                    continue

                print("Received authentication packet...")
                p = packet.AuthenticationPacket(buf)
                try:
                    if p.get_type() != packet.PACKET_TYPE_AUTHENTICATION:
                        self.client_socket.close()
                        self.sm.unknown()
                        continue
                    if utils.Utils.check_buffer_is_empty(p.get_password()):
                        print("Invalid credentials")
                        try:
                            nack = packet.NegativeAcknowledgementPacket()
                            self.client_socket.send(nack.get_buffer())
                            #self.client_socket.close();
                        except:
                            print("Failed to write into socket...")
                        self.client_socket.close()
                        self.sm.unknown()
                        continue
                    if utils.Utils.check_buffer_is_empty(p.get_username()):
                        print("Invalid credentials")
                        try:
                            nack = packet.NegativeAcknowledgementPacket()
                            self.client_socket.send(nack.get_buffer())
                            #self.client_socket.close();
                        except:
                            print("Failed to write into socket...")
                        self.client_socket.close()
                        self.sm.unknown()
                        continue
                    if self.database.is_authentic(p.get_username(),
                                                  p.get_password(), self.salt):
                        self.sm.authenticated()
                        try:
                            ack = packet.AcknowledgementPacket()
                            self.client_socket.send(ack.get_buffer())
                        except:
                            print("Failed to write data into socket...")
                            self.client_socket.close()
                            self.sm.unknown()
                    else:
                        try:
                            nack = packet.NegativeAcknowledgementPacket()
                            self.client_socket.send(nack.get_buffer())
                            #self.client_socket.close();
                            #self.sm.unknown();
                        except:
                            print("Failed to write into socket...")
                        self.client_socket.close()
                        self.sm.unknown()
                except:
                    self.client_socket.close()
                    self.sm.unknown()
                    print("Could not parse data")
            elif self.sm.is_authenticated():
                self.client_ip = self.ip_pool.lease_ip()
                configuration = packet.ConfigurationPacket()
                configuration.set_netmask(
                    list(bytearray(self.tun_netmask, encoding="ASCII")))
                configuration.set_default_gw(
                    list(bytearray(self.tun_address, encoding="ASCII")))
                configuration.set_ipv4_address(
                    list(bytearray(self.client_ip, encoding="ASCII")))
                configuration.set_mtu(list(struct.pack("I", self.tun_mtu)))
                try:
                    self.client_socket.send(configuration.get_buffer())
                    self.sm.configured()
                except:
                    self.sm.unknown()
                    self.client_socket.close()
                    print("Failed to write into socket...")
            elif self.sm.is_configured():
                self.tun_thread = threading.Thread(target=self.tun_loop)
                self.tls_thread = threading.Thread(target=self.tls_loop)
                self.tun_thread.daemon = True
                self.tls_thread.daemon = True
                self.tun_thread.start()
                self.tls_thread.start()
                self.sm.running()
            elif self.sm.is_running():
                sleep(10)