[Add] Make machine services persistant.
This commit is contained in:
		
							parent
							
								
									761283ca63
								
							
						
					
					
						commit
						e0ef512969
					
				@ -33,6 +33,12 @@ void DatabaseManager::init(void) {
 | 
			
		||||
          "type INTEGER NOT NULL,"
 | 
			
		||||
          "content TEXT"
 | 
			
		||||
          ");";
 | 
			
		||||
  _db <<  "CREATE TABLE IF NOT EXISTS services ("
 | 
			
		||||
          "id INTEGER PRIMARY KEY AUTOINCREMENT,"
 | 
			
		||||
          "machine_id INTEGER NOT NULL,"
 | 
			
		||||
          "port INTEGER NOT NULL,"
 | 
			
		||||
          "name TEXT NOT NULL"
 | 
			
		||||
          ");";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool DatabaseManager::create_player(const std::string& username, const std::string& password,
 | 
			
		||||
@ -85,6 +91,10 @@ bool DatabaseManager::create_player(const std::string& username, const std::stri
 | 
			
		||||
          << machine_id << bin_id << name << FILE_NODE <<node->content;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Add default SSH service. */
 | 
			
		||||
    _db << "INSERT INTO services (machine_id, port, name) VALUES (?, ?, ?);"
 | 
			
		||||
        << machine_id << 22 << "SSH";
 | 
			
		||||
 | 
			
		||||
    _db << "COMMIT";
 | 
			
		||||
  } catch(const std::exception& e) {
 | 
			
		||||
    _db << "ROLLBACK;"; /* Ensure atomicity. */
 | 
			
		||||
 | 
			
		||||
@ -72,9 +72,6 @@ Machine* MachineManager::create_machine(uint32_t id, const std::string& hostname
 | 
			
		||||
  /* Assign the VFS to the new machine. */
 | 
			
		||||
  new_machine->vfs_root = root;
 | 
			
		||||
 | 
			
		||||
  /* Default services. All machines have SSH currently. */
 | 
			
		||||
  new_machine->services[22] = "SSH v1.2";
 | 
			
		||||
 | 
			
		||||
  if(system_type == "npc") {
 | 
			
		||||
    vfs_node* npc_file = new_node("npc_system.txt", FILE_NODE, root);
 | 
			
		||||
    npc_file->content = "This guy sucks nuts!";
 | 
			
		||||
@ -128,6 +125,12 @@ Machine* MachineManager::load_machine(long long machine_id, DatabaseManager* db_
 | 
			
		||||
      }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
  db_manager->_db << "SELECT port, name FROM services WHERE machine_id = ?;"
 | 
			
		||||
                  << machine_id
 | 
			
		||||
                  >> [&](int port, std::string name) {
 | 
			
		||||
      machine->services[port] = name;
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
  if(root) {
 | 
			
		||||
    build_tree(root, nodes);
 | 
			
		||||
    machine->vfs_root = root;
 | 
			
		||||
 | 
			
		||||
@ -28,16 +28,6 @@ NetworkManager::NetworkManager(const std::string& db_path) :
 | 
			
		||||
      _world_machines[ip_address] = _machine_manager.load_machine(id, _db_manager.get());
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
  /*
 | 
			
		||||
   * TODO: Services are not yet persistant. I'll add them in-mem for now.
 | 
			
		||||
   */
 | 
			
		||||
  if(_world_machines.count("8.8.8.8")) {
 | 
			
		||||
    _world_machines["8.8.8.8"]->services[80] = "HTTPD v2.4";
 | 
			
		||||
  }
 | 
			
		||||
  if(_world_machines.count("10.0.2.15")) {
 | 
			
		||||
    _world_machines["10.0.2.15"]->services[21] = "FTPd v3.0";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  fprintf(stderr, "Created world with %zu networks\n", _world_machines.size());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -248,11 +238,12 @@ void NetworkManager::_seed_npc_machines(void) {
 | 
			
		||||
  struct NpcDef {
 | 
			
		||||
    std::string hostname;
 | 
			
		||||
    std::string ip;
 | 
			
		||||
    std::map<int, std::string> services;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  std::vector<NpcDef> npc_defs = {
 | 
			
		||||
    {"dns.google", "8.8.8.8"},
 | 
			
		||||
    {"corp.internal", "10.0.2.15"}
 | 
			
		||||
    {"dns.google", "8.8.8.8", {{80, "HTTPD v2.4"}}},
 | 
			
		||||
    {"corp.internal", "10.0.2.15", {{21, "FTPd v3.0"}}}
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  try {
 | 
			
		||||
@ -284,6 +275,12 @@ void NetworkManager::_seed_npc_machines(void) {
 | 
			
		||||
                            "VALUES(?, ?, ?, ?, ?);"
 | 
			
		||||
                         << machine_id << bin_id << name << FILE_NODE << node->content;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      for(const auto& service : def.services) {
 | 
			
		||||
        _db_manager->_db << "INSERT INTO services (machine_id, port, name) "
 | 
			
		||||
                            "VALUES (?, ?, ?);"
 | 
			
		||||
                         << machine_id << service.first << service.second;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  } catch(const std::exception& e) {
 | 
			
		||||
    fprintf(stderr, "Error seeding NPCs: %s. Database may be in an inconsistent state.\n", e.what());
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user