Esempio n. 1
0
void cb_downloadStart(FileServer_Private& client)
{
    START_REPLY(e_FileToCli_FileDownloadReply);

    // Trans ID
    client.m_buffer.write<uint32_t>(DS::RecvValue<uint32_t>(client.m_sock));

    // Download filename
    char16_t buffer[260];
    DS::RecvBuffer(client.m_sock, buffer, sizeof(buffer));
    buffer[259] = 0;
    DS::String filename = DS::String::FromUtf16(buffer);

    // Build ID
    uint32_t buildId = DS::RecvValue<uint32_t>(client.m_sock);
    if (buildId && buildId != DS::Settings::BuildId()) {
        fprintf(stderr, "[File] Wrong Build ID from %s: %d\n",
                DS::SockIpAddress(client.m_sock).c_str(), buildId);
        DS::CloseSock(client.m_sock);
        return;
    }

    // Ensure filename is jailed to our data path
    if (filename.find("..") != -1) {
        client.m_buffer.write<uint32_t>(DS::e_NetFileNotFound);
        client.m_buffer.write<uint32_t>(0);     // Reader ID
        client.m_buffer.write<uint32_t>(0);     // File size
        client.m_buffer.write<uint32_t>(0);     // Data packet size
        SEND_REPLY();
        return;
    }
    filename.replace("\\", "/");

    filename = DS::Settings::FileRoot() + filename;
    int fd = open(filename.c_str(), O_RDONLY);

    if (fd < 0) {
        fprintf(stderr, "[File] Could not open file %s\n[File] Requested by %s\n",
                filename.c_str(), DS::SockIpAddress(client.m_sock).c_str());
        client.m_buffer.write<uint32_t>(DS::e_NetFileNotFound);
        client.m_buffer.write<uint32_t>(0);     // Reader ID
        client.m_buffer.write<uint32_t>(0);     // File size
        client.m_buffer.write<uint32_t>(0);     // Data packet size
        SEND_REPLY();
        return;
    }

    std::unique_ptr<FileRequest> req(new FileRequest(fd));
    client.m_buffer.write<uint32_t>(DS::e_NetSuccess);
    client.m_buffer.write<uint32_t>(++client.m_readerId);
    client.m_buffer.write<uint32_t>(req->m_size);

    FileRequest::chunk_t c = req->nextChunk();
    client.m_buffer.write<uint32_t>(std::get<1>(c));
    SEND_FD_REPLY(req->m_fd, req->m_pos, std::get<1>(c));
    if (!std::get<0>(c)) {
        client.m_downloads[client.m_readerId] = std::move(req);
    }
}
Esempio n. 2
0
void cb_downloadStart(AuthServer_Private& client)
{
    START_REPLY(e_AuthToCli_FileDownloadChunk);

    // Trans ID
    uint32_t transId = DS::CryptRecvValue<uint32_t>(client.m_sock, client.m_crypt);
    client.m_buffer.write<uint32_t>(transId);

    // Download filename
    DS::String filename = DS::CryptRecvString(client.m_sock, client.m_crypt);

    // Ensure filename is jailed to our data path
    if (filename.find("..") != -1) {
        client.m_buffer.write<uint32_t>(DS::e_NetFileNotFound);
        client.m_buffer.write<uint32_t>(0);     // File size
        client.m_buffer.write<uint32_t>(0);     // Chunk offset
        client.m_buffer.write<uint32_t>(0);     // Data packet size
        SEND_REPLY();
        return;
    }
    filename.replace("\\", "/");

    filename = DS::Settings::AuthRoot() + filename;
    DS::FileStream* stream = new DS::FileStream();
    try {
        stream->open(filename.c_str(), "rb");
    } catch (const DS::FileIOException& ex) {
        fprintf(stderr, "[Auth] Could not open file %s: %s\n[Auth] Requested by %s\n",
                filename.c_str(), ex.what(), DS::SockIpAddress(client.m_sock).c_str());
        client.m_buffer.write<uint32_t>(DS::e_NetFileNotFound);
        client.m_buffer.write<uint32_t>(0);     // File size
        client.m_buffer.write<uint32_t>(0);     // Chunk offset
        client.m_buffer.write<uint32_t>(0);     // Data packet size
        SEND_REPLY();
        delete stream;
        return;
    }

    client.m_buffer.write<uint32_t>(DS::e_NetSuccess);
    client.m_buffer.write<uint32_t>(stream->size());
    client.m_buffer.write<uint32_t>(stream->tell());

    uint8_t data[CHUNK_SIZE];
    if (stream->size() > CHUNK_SIZE) {
        client.m_buffer.write<uint32_t>(CHUNK_SIZE);
        stream->readBytes(data, CHUNK_SIZE);
        client.m_buffer.writeBytes(data, CHUNK_SIZE);
        client.m_downloads[transId] = stream;
    } else {
        client.m_buffer.write<uint32_t>(stream->size());
        stream->readBytes(data, stream->size());
        client.m_buffer.writeBytes(data, stream->size());
        delete stream;
    }

    SEND_REPLY();
}
Esempio n. 3
0
std::list<AuthServer_AgeInfo> configure_static_ages()
{
    AuthServer_AgeInfo age;
    std::list<AuthServer_AgeInfo> configs;

    DS::String filename = DS::Settings::SettingsPath() + "/static_ages.ini";
    FILE* cfgfile = fopen(filename.c_str(), "r");
    if (!cfgfile) {
        fprintf(stderr, "Cannot open %s for reading\n", filename.c_str());
        return configs;
    }

    try {
        char buffer[4096];
        bool haveAge = false;
        while (fgets(buffer, 4096, cfgfile)) {
            DS::String line = DS::String(buffer).strip('#');
            if (line.isEmpty())
                continue;

            if (line.strip().c_str()[0] == '[') {
                if (haveAge)
                    configs.push_back(age);
                age.clear();

                DS::String header = line.strip();
                header.replace("[","");
                header.replace("]","");

                if (header == "auto")
                    age.m_ageId = gen_uuid();
                else
                    age.m_ageId = DS::Uuid(header.c_str());

                haveAge = true;
                continue;
            }

            std::vector<DS::String> params = line.split('=', 1);
            if (params.size() != 2) {
                fprintf(stderr, "Warning: Invalid config line: %s\n", line.c_str());
                continue;
            }

            // Clean any whitespace around the '='
            params[0] = params[0].strip();
            params[1] = params[1].strip();

            if (params[0] == "Filename") {
                age.m_filename = params[1];
            } else if (params[0] == "Instance") {
                age.m_instName = params[1];
            } else if (params[0] == "UserName") {
                age.m_userName = params[1];
            } else {
                fprintf(stderr, "Warning: Unknown setting '%s' ignored\n",
                        params[0].c_str());
            }
        }

        if (haveAge)
            configs.push_back(age);
    } catch (DS::AssertException ex) {
        fprintf(stderr, "[Auth] Assertion failed at %s:%ld:  %s\n",
                ex.m_file, ex.m_line, ex.m_cond);
        fclose(cfgfile);
        return configs;
    }

    fclose(cfgfile);
    return configs;
}
Esempio n. 4
0
void dm_htserv()
{
    printf("[Status] Running on %s\n", DS::SockIpAddress(s_listenSock).c_str());
    try {
        for ( ;; ) {
            DS::SocketHandle client;
            try {
                client = DS::AcceptSock(s_listenSock);
            } catch (DS::SockHup) {
                break;
            }

            if (!client)
                continue;

            std::list<DS::String> lines;
            for ( ;; ) {
                std::unique_ptr<char[]> buffer;
                DS::String scratch;
                try {
                    size_t bufSize = DS::PeekSize(client);
                    buffer.reset(new char[scratch.length() + bufSize + 1]);
                    memcpy(buffer.get(), scratch.c_str(), scratch.length());
                    DS::RecvBuffer(client, buffer.get() + scratch.length(), bufSize);
                    buffer[scratch.length() + bufSize] = 0;
                } catch (const DS::SockHup&) {
                    lines.clear();
                    break;
                }

                char* cp = buffer.get();
                char* sp = buffer.get();
                while (*cp) {
                    if (*cp == '\r' || *cp == '\n') {
                        if (*cp == '\r' && *(cp + 1) == '\n') {
                            // Delete both chars of the Windows newline
                            *cp++ = 0;
                        }
                        *cp++ = 0;
                        lines.push_back(DS::String(sp));
                        sp = cp;
                        scratch = "";
                    } else {
                        ++cp;
                    }
                }
                if (cp != sp)
                    scratch += sp;

                if (lines.size() && lines.back().isEmpty()) {
                    // Got the separator line
                    break;
                }
            }

            if (lines.empty()) {
                DS::FreeSock(client);
                continue;
            }

            // First line contains the action
            std::vector<DS::String> action = lines.front().split();
            if (action.size() < 2) {
                fprintf(stderr, "[Status] Incorrectly formatted HTTP request: %s\n",
                        lines.front().c_str());
                DS::FreeSock(client);
                continue;
            }
            if (action[0] != "GET") {
                fprintf(stderr, "[Status] Unsupported method: %s\n", action[0].c_str());
                DS::FreeSock(client);
                continue;
            }
            if (action.size() < 3 || action[2] != "HTTP/1.1") {
                fputs("[Status] Unsupported HTTP Version\n", stderr);
                DS::FreeSock(client);
                continue;
            }
            DS::String path = action[1];
            lines.pop_front();

            for (auto lniter = lines.begin(); lniter != lines.end(); ++lniter) {
                // TODO: See if any of these fields are useful to us...
            }

            if (path == "/status") {
                DS::String json = "{'online':true";
                DS::String welcome = DS::Settings::WelcomeMsg();
                welcome.replace("\"", "\\\"");
                json += DS::String::Format(",'welcome':\"%s\"", welcome.c_str());
                json += "}\r\n";
                // TODO: Add more status fields (players/ages, etc)

                SEND_RAW(client, "HTTP/1.1 200 OK\r\n");
                SEND_RAW(client, "Server: Dirtsand\r\n");
                SEND_RAW(client, "Connection: close\r\n");
                SEND_RAW(client, "Accept-Ranges: bytes\r\n");
                DS::String lengthParam = DS::String::Format("Content-Length: %u\r\n", json.length());
                DS::SendBuffer(client, lengthParam.c_str(), lengthParam.length());
                SEND_RAW(client, "Content-Type: application/json\r\n");
                SEND_RAW(client, "\r\n");
                DS::SendBuffer(client, json.c_str(), json.length());
                DS::FreeSock(client);
            } else if (path == "/welcome") {
                DS::String welcome = DS::Settings::WelcomeMsg();
                welcome.replace("\\n", "\r\n");

                SEND_RAW(client, "HTTP/1.1 200 OK\r\n");
                SEND_RAW(client, "Server: Dirtsand\r\n");
                SEND_RAW(client, "Connection: close\r\n");
                SEND_RAW(client, "Accept-Ranges: bytes\r\n");
                DS::String lengthParam = DS::String::Format("Content-Length: %u\r\n", welcome.length());
                DS::SendBuffer(client, lengthParam.c_str(), lengthParam.length());
                SEND_RAW(client, "Content-Type: text/plain\r\n");
                SEND_RAW(client, "\r\n");
                DS::SendBuffer(client, welcome.c_str(), welcome.length());
                DS::FreeSock(client);
            } else {
                DS::String content = DS::String::Format("No page found at %s\r\n", path.c_str());

                SEND_RAW(client, "HTTP/1.1 404 NOT FOUND\r\n");
                SEND_RAW(client, "Server: Dirtsand\r\n");
                SEND_RAW(client, "Connection: close\r\n");
                SEND_RAW(client, "Accept-Ranges: bytes\r\n");
                DS::String lengthParam = DS::String::Format("Content-Length: %u\r\n", content.length());
                DS::SendBuffer(client, lengthParam.c_str(), lengthParam.length());
                SEND_RAW(client, "Content-Type: text/plain\r\n");
                SEND_RAW(client, "\r\n");
                DS::SendBuffer(client, content.c_str(), content.length());
                DS::FreeSock(client);
            }
        }
    } catch (DS::AssertException ex) {
        fprintf(stderr, "[Status] Assertion failed at %s:%ld:  %s\n",
                ex.m_file, ex.m_line, ex.m_cond);
    }

    DS::FreeSock(s_listenSock);
}
Esempio n. 5
0
void cb_downloadStart(FileServer_Private& client)
{
    START_REPLY(e_FileToCli_FileDownloadReply);

    // Trans ID
    client.m_buffer.write<uint32_t>(DS::RecvValue<uint32_t>(client.m_sock));

    // Download filename
    chr16_t buffer[260];
    DS::RecvBuffer(client.m_sock, buffer, 260 * sizeof(chr16_t));
    buffer[259] = 0;
    DS::String filename = DS::String::FromUtf16(buffer);

    // Build ID
    uint32_t buildId = DS::RecvValue<uint32_t>(client.m_sock);
    if (buildId && buildId != CLIENT_BUILD_ID) {
        fprintf(stderr, "[File] Wrong Build ID from %s: %d\n",
                DS::SockIpAddress(client.m_sock).c_str(), buildId);
        DS::CloseSock(client.m_sock);
        return;
    }

    // Ensure filename is jailed to our data path
    if (filename.find("..") != -1) {
        client.m_buffer.write<uint32_t>(DS::e_NetFileNotFound);
        client.m_buffer.write<uint32_t>(0);     // Reader ID
        client.m_buffer.write<uint32_t>(0);     // File size
        client.m_buffer.write<uint32_t>(0);     // Data packet size
        SEND_REPLY();
        return;
    }
    filename.replace("\\", "/");

    filename = DS::Settings::FileRoot() + filename;
    DS::FileStream* stream = new DS::FileStream();
    try {
        stream->open(filename.c_str(), "rb");
    } catch (DS::FileIOException ex) {
        fprintf(stderr, "[File] Could not open file %s: %s\n[File] Requested by %s\n",
                filename.c_str(), ex.what(), DS::SockIpAddress(client.m_sock).c_str());
        client.m_buffer.write<uint32_t>(DS::e_NetFileNotFound);
        client.m_buffer.write<uint32_t>(0);     // Reader ID
        client.m_buffer.write<uint32_t>(0);     // File size
        client.m_buffer.write<uint32_t>(0);     // Data packet size
        SEND_REPLY();
        delete stream;
        return;
    }

    client.m_buffer.write<uint32_t>(DS::e_NetSuccess);
    client.m_buffer.write<uint32_t>(++client.m_readerId);
    client.m_buffer.write<uint32_t>(stream->size());

    uint8_t data[CHUNK_SIZE];
    if (stream->size() > CHUNK_SIZE) {
        client.m_buffer.write<uint32_t>(CHUNK_SIZE);
        stream->readBytes(data, CHUNK_SIZE);
        client.m_buffer.writeBytes(data, CHUNK_SIZE);
        client.m_downloads[client.m_readerId] = stream;
    } else {
        client.m_buffer.write<uint32_t>(stream->size());
        stream->readBytes(data, stream->size());
        client.m_buffer.writeBytes(data, stream->size());
        delete stream;
    }

    SEND_REPLY();
}