#include "socket.hpp" #include "ByteBuffer.hpp" #include "main.h" #include class gwfsc : public client_socket { private: unsigned int file_hash; unsigned int file_uncompressed_length; unsigned int file_length; unsigned int file_crc32; unsigned int file_received; unsigned int data_length; std::string filename; ByteBuffer file; public: gwfsc(unsigned int hash, std::string filename) { this->file_hash = hash; this->filename = filename; this->file_received = 0; } void decompressFile() { unsigned char *Output = NULL; int OutSize = 0; if (this->file_uncompressed_length == this->file_length) { std::cout << "File Dump (Was not compressed):" << std:: endl; return; } // Add this stuff so the decompressor is nice file.WriteByte(0x08); file.WriteByte(0x00); file.WriteByte(0x01); file.WriteByte(0x80); file.WriteLong(this->file_uncompressed_length); UnpackGWDat(file.getBytes(), file.buff.size(), Output, OutSize); ByteBuffer decompressed(Output, OutSize); std::ofstream datafile (this->filename.c_str(), std::ios::out | std::ios::app | std::ios::binary); datafile.write((const char*)Output, OutSize); datafile.close(); delete[] Output; std::cout << "\nCompressed size: " << this->file_length << std::endl; std::cout << "Uncompressed size: " << this->file_uncompressed_length << std::endl; std::cout << "File Written: " << this->filename; } void dataReceived(unsigned char* data, int length) { ByteBuffer packet(data, length); while (true) { // Read command type from header int cmd = packet.ReadShort(); if (cmd < 0) { return; } // Read data length from header this->data_length = packet.ReadShort(); // How much more data is required? unsigned int required = this->data_length - packet.buff.size(); // Hell of a hack! while (required > 0) { unsigned char data[1]; int len; while ((len = recv(sock, (char*)data, sizeof(data), 0)) > 0) { packet.Write(data, len); required = required - len; if (required <= 0) break; } } switch (cmd) { default: std::cout << "Unknown Packet" << std::endl; packet.print_hex(); break; case 1266: // Unknown file std::cout << "File hash (" << this->file_hash << ") not found on server." << std::endl; close(); exit(1); break; case 1522: // File details this->file_hash = packet.ReadLong(); this->file_uncompressed_length = packet.ReadLong(); this->file_length = packet.ReadLong(); // File length this->file_crc32 = packet.ReadLong(); std::cout << "File Information: " << std::endl; std::cout << "\tRequested Hash: " << this->file_hash << std::endl; std::cout << "\tUncompressed size: " << this->file_uncompressed_length << std::endl; std::cout << "\tCompressed Size: " << this->file_length << std::endl; std::cout << "\tSomething: " << std::hex << packet.ReadLong() << std::dec << std::endl << std::endl; break; case 1779: case 1778: // We are getting a file this->file_received += packet.buff.size() - 4; unsigned char *file_packet; file_packet = new unsigned char[packet.buff.size() - 4]; packet.Read(file_packet, 0, packet.buff.size() - 4); file.Write(file_packet, packet.buff.size() - 4); delete[] file_packet; std::cout << "Downloading " << this->file_received << " / " << this->file_length << "..." << std::endl; // Send need more? { ByteBuffer request; //F3 07 08 00 00 40 00 00 request.WriteShort(2035); request.WriteShort(8); request.WriteLong(packet.buff.size() - 4); sendData(request.getBytes(), request.buff.size()); } if (this->file_received == this->file_length) { // Tell the server we're done ByteBuffer request; //F2 07 08 [File Uncompressed Length] request.WriteShort(2034); // F2 07 request.WriteShort(8); request.WriteLong(this->file_length); sendData(request.getBytes(), request.buff.size()); // Decompress the file decompressFile(); close(); } break; case 753: // This packet actually contains info about the current state of the DAT // Request a file ByteBuffer request; request.WriteShort(1010); // F2 03 request.WriteShort(12); // The length of this packet request.WriteLong(this->file_hash); // File hash #1? request.WriteLong(0); // File hash #2? sendData(request.getBytes(), request.buff.size()); std::cout << "Sent file request...\n" << std::endl; return; } } } }; int main(int argc, char* argv[]) { std::cout << "Guild Wars File Server Client (GWFSC)" << std::endl; std::cout << "Written by Arran Walker\n" << std::endl; unsigned char init[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0xF1, 0x00, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; if (argc > 2) { unsigned int hash; if (strlen(argv[1]) > 0 && argv[1][1] == 'x') { hash = strtoul(argv[1], NULL, 16); } else { hash = atoi(argv[1]); } gwfsc client(hash, argv[2]); bool success = false; if (argc == 4) { success = client.conn(argv[3], 6112); } else { success = client.conn("206.127.147.156", 6112); } if (!success) { std::cout << "Could not connect to server." << std::endl; exit(1); } client.sendData(init, sizeof(init)); client.receiveData(); } else { std::cout << "Usage: gwfsc [file server address]" << std::endl; } return 0; }