禁止浏览器访问抖音网站

最近刷抖音上头,为了防止自己继续沉迷下去,决定写点代码,让电脑永远不能访问抖音网站,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define HOSTS_PATH "C:\\Windows\\System32\\drivers\\etc\\hosts"
#define BACKUP_PATH "C:\\Windows\\System32\\drivers\\etc\\hosts.bak"
#define BUFFER_SIZE 1024
// 函数声明
int BackupHosts(const char* backupPath);
int RestoreHosts(const char* originalPath, const char* backupPath);
// 辅助函数,用于复制临时文件到hosts文件
int fcopy(FILE* src, FILE* dest) {
    char buffer[BUFFER_SIZE];
    size_t bytesRead;
    while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, src)) > 0) {
        if (fwrite(buffer, 1, bytesRead, dest) != bytesRead) {
            return -1;
        }
    }
    return 0;
}
int AddHostsEntry(const char* domain) {
    FILE* hostsFile, * tempFile;
    char buffer[BUFFER_SIZE];
    const char* ip = "127.0.0.1";
    int added = 0;

    // 打开原hosts文件和临时文件
    hostsFile = fopen(HOSTS_PATH, "r+");
    if (hostsFile == NULL) {
        perror("Error opening hosts file");
        return -1;
    }

    // 创建临时文件
    tempFile = tmpfile();
    if (tempFile == NULL) {
        fclose(hostsFile);
        perror("Error creating temporary file");
        return -1;
    }

    // 读取原hosts文件内容,检查并添加新条目
    while (fgets(buffer, BUFFER_SIZE, hostsFile) != NULL) {
        if (strstr(buffer, domain) != NULL) {
            added = 1; // 已经存在该域名的条目
        }
        fputs(buffer, tempFile); // 复制到临时文件
    }

    // 如果没有找到该域名的条目,则添加新的条目
    if (!added) {
        fprintf(tempFile, "%s %s\n", ip, domain);
    }

    // 替换原hosts文件
    rewind(tempFile); // 重置临时文件的文件指针到开始位置
    if (fcopy(tempFile, hostsFile) == -1) {
        fclose(hostsFile);
        fclose(tempFile);
        return -1;
    }

    // 关闭文件
    fclose(hostsFile);
    fclose(tempFile);
    return 0;
}

int main() {
    // 首先备份原始的hosts文件
    if (BackupHosts(BACKUP_PATH) != 0) {
        fprintf(stderr, "Failed to backup the original hosts file.\n");
        system("pause");
        return 1;
    }

    // 这里可以添加修改hosts文件的代码
    // 例如:AddHostsEntry("example.com");
    char domain[] = "www.douyin.com"; // 需要禁止访问的域名
    if (AddHostsEntry(domain) == 0) {
        printf("Hosts file updated successfully.\n");
        system("pause");
    }
    else {
        perror("Failed to update hosts file");
        system("pause");
    }
    //return 0;
    // 
    system("pause");
    // 程序结束前,恢复原始的hosts文件
    /*
    if (RestoreHosts(HOSTS_PATH, BACKUP_PATH) != 0) {
        fprintf(stderr, "Failed to restore the original hosts file.\n");
        // 可以选择删除修改过的hosts文件,恢复备份文件
        // remove(HOSTS_PATH); // 删除修改过的hosts文件
        // rename(BACKUP_PATH, HOSTS_PATH); // 将备份文件重命名为hosts
    }
    */
    return 0;
}

// 备份原始的hosts文件
int BackupHosts(const char* backupPath) {
    if (remove(backupPath) == 0) {
        printf("Removed existing backup file.\n");
    }

    if (copyfile(HOSTS_PATH, backupPath) != 0) {
        return -1;
    }

    return 0;
}

// 恢复原始的hosts文件
int RestoreHosts(const char* originalPath, const char* backupPath) {
    return copyfile(backupPath, originalPath);
}

// 辅助函数,用于复制文件内容
int copyfile(const char* srcPath, const char* destPath) {
    FILE* srcFile, * destFile;
    char buffer[1024];
    size_t bytesRead, bytesWritten;

    srcFile = fopen(srcPath, "rb");
    if (srcFile == NULL) {
        perror("Error opening source file");
        return -1;
    }

    destFile = fopen(destPath, "wb");
    if (destFile == NULL) {
        perror("Error opening destination file");
        fclose(srcFile);
        return -1;
    }

    while ((bytesRead = fread(buffer, 1, sizeof(buffer), srcFile)) > 0) {
        bytesWritten = fwrite(buffer, 1, bytesRead, destFile);
        if (bytesRead != bytesWritten) {
            perror("Error writing to destination file");
            fclose(srcFile);
            fclose(destFile);
            return -1;
        }
    }

    fclose(srcFile);
    fclose(destFile);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
浏览器访问HTTP网站的过程通常涉及以下几个步骤。 首先,当输入一个网址(URL)时,浏览器会解析该网址,分析其中的协议部分(http://),确定需要使用的协议是HTTP。 接下来,浏览器会和网址中的主机(服务器)建立TCP连接。TCP是一种传输层协议,用于在网络上的两个主机之间建立可靠的连接。浏览器和服务器之间建立的TCP连接是双向的,可以用于双向通信。 一旦TCP连接建立成功,浏览器就会向服务器发送HTTP请求。HTTP请求由请求行、请求头和请求体组成。请求行包含请求的方法(GET、POST等)、路径和协议版本等信息,请求头包含一些附加的信息,如浏览器的信息、可接受的数据类型等,请求体包含一些用户提交的数据(如表单数据)。 服务器接收到浏览器发送的HTTP请求后,会解析请求,根据请求的路径和方法等信息执行相应的操作。例如,如果是GET请求,服务器会读取相应的资源(HTML页面、图片、视频等)并封装在HTTP响应中发送给浏览器浏览器接收到HTTP响应后,会先解析HTTP响应头。响应头中包含了一些元信息,如响应的状态码、内容类型等。根据状态码可以判断请求的处理结果,常见的状态码有200表示请求成功、404表示资源未找到等。 最后,浏览器会解析HTTP响应体,并根据响应体中的信息来渲染页面或执行相应的操作。如果响应体是HTML页面,浏览器会解析其中的标记语言,加载页面的样式、脚本和内容,并将其展示在浏览器窗口中。 总而言之,浏览器访问HTTP网站的过程涉及解析网址、建立TCP连接、发送HTTP请求、接收HTTP响应以及解析响应体等多个步骤。这些步骤共同完成了浏览器和服务器之间的通信和网页的加载。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值