开源基于Rust编写的Web服务器

151 阅读8分钟

基于 RUST 的 WEB 资源服务器

Github 地址

LTPP-GIT 地址

官方文档

该项目于 2024 年 5 月 1 日开始开发

预期功能

功能支持情况当前情况
多线程支持
服务支持配置化
防盗链支持
gzip 支持
反向代理支持
自定义状态码对应资源文件
日志支持
负载均衡支持
域名绑定解析支持
资源解析
history 模式支持
自定义响应头

目前进度

  • 自定义响应头
  • 多线程支持
  • 配置化
  • 日志输出
  • 资源解析
  • history 模式支持
  • 域名绑定支持
  • 一个端口对应多域名支持
  • 反向代理支持
  • 负载均衡支持
  • 防盗链支持
  • gzip 支持

错误页

  • root_path 下名称为 对应状态码.html,例如 404 对应页面 404.html

JSON 示例

首次运行会自动生成 config.json 配置文件,填写好后重新运行即可

以上配置将当前目录作为访问地址的根目录, 监听了 8081 端口,绑定 IP域名127.0.0.1localhost 用来处理请求, 当访问为空则重写路径到当前目录的 index.html(适用于 Vue 等打包后的资源), 日志保存在当前目录 logs

配置中的 listen_ip 为服务端 IPbind_server_name 下的 key 为域名或者 IP, 一般 listen_ip127.0.0.1, 如果 bind_server_name 配置了 localhost 域名,那么可以使用 localhost 访问, 但是不支持 127.0.0.1,如需支持 127.0.0.1,在 bind_server_name 中添加即可 如果本地做了映射,需要同时添加映射的域名和 127.0.0.1

反向代理

  • 系统会检测 proxy 数组里每个元素是否是合法 URL,如果是合法 URL 那么系统会加入到反向代理的列表,如果最终反向代理列表为空则会加载静态资源
  • 反向代理会映射请求路径和请求参数,如果配置文件 proxy 中的元素加了路径,系统会忽略
  • 反向代理时,会随机从反向代理列表中获取一个代理地址 PROXY_URL ,系统会截取 PROXY_URL 字段除了 path 部分的 url,实际 path 来自请求时的 path,参数会进行拼接,如果 PROXY_URL 参数和请求参数冲突,请求参数会追加在 PROXY_URL 参数后面

负载均衡

  • 反向代理时,会随机从反向代理列表中获取一个代理地址 PROXY_URL

GZIP

  • 请求头 Accept-Encoding 需要包含 gzip 并且响应头 Content-Encoding 需要包含 gzip
  • 配置 gzip_level 用于设置压缩率,范围从 1 - 9,越大压缩的越小,耗时也会更久,建议 4/5 即可

异常

  • 如果出现页面/请求加载异常的情况,需要增加 buffer_size 数值

防盗链

  • hotlink_protection 使用正则进行匹配(系统内部会额外处理../防盗链情况)

运行效果

配置文件示例

{
  "server": [
    {
      "listen_ip": "0.0.0.0",
      "listen_port": 80,
      "bind_server_name": {
        "127.0.0.1": {
          "root_path": "./",
          "buffer_size": 10240,
          "gzip_level": 1,
          "log_dir_path": "./logs",
          "empty_path_try_files_path": "./index.html",
          "response_header_list": [
            "Server: RUST-WEB-SERVER",
            "Access-Control-Allow-Credentials: true",
            "Access-Control-Allow-Headers: *",
            "Access-Control-Allow-Methods: *",
            "Access-Control-Allow-Origin: *",
            "Git: https://git.ltpp.vip/root/RUST-WEB-SERVE.git",
            "Content-Type: text/html",
            "Content-Encoding: gzip"
          ],
          "proxy": [],
          "proxy_timeout_seconds": 10,
          "hotlink_protection": []
        },
        "localhost": {
          "root_path": "./",
          "buffer_size": 10240,
          "gzip_level": 1,
          "log_dir_path": "./logs",
          "empty_path_try_files_path": "./index.html",
          "response_header_list": [
            "Server: RUST-WEB-SERVER",
            "Access-Control-Allow-Credentials: true",
            "Access-Control-Allow-Headers: *",
            "Access-Control-Allow-Methods: *",
            "Access-Control-Allow-Origin: *",
            "Git: https://git.ltpp.vip/root/RUST-WEB-SERVE.git",
            "Content-Type: application/json",
            "Content-Encoding: gzip"
          ],
          "proxy": [
            "https://api.ltpp.vip",
            "https://api.ltpp.vip/Version/getVersion",
            "https://api.ltpp.vip/Version/getVersion?origin=proxy1"
          ],
          "proxy_timeout_seconds": 10,
          "hotlink_protection": []
        }
      }
    },
    {
      "listen_ip": "0.0.0.0",
      "listen_port": 81,
      "bind_server_name": {
        "127.0.0.1": {
          "root_path": "./",
          "buffer_size": 10240,
          "gzip_level": 1,
          "log_dir_path": "./logs",
          "empty_path_try_files_path": "./index.html",
          "response_header_list": [
            "Access-Control-Allow-Origin: *",
            "Access-Control-Allow-Methods: *",
            "Access-Control-Allow-Headers: *",
            "Access-Control-Allow-Credentials: true",
            "Server: RUST-WEB-SERVER",
            "Git: https://git.ltpp.vip/root/RUST-WEB-SERVE.git",
            "Content-Type: application/json",
            "Content-Encoding: gzip"
          ],
          "proxy": ["https://api.ltpp.vip/Version/getVersion?origin=proxy1"],
          "proxy_timeout_seconds": 10,
          "hotlink_protection": ["origin=1$"]
        }
      }
    }
  ]
}

运行效果

在这里插入图片描述 在这里插入图片描述

日志

支持配置,日期和完整请求记录

[2024-05-28 23:40:07]
Binding => 127.0.0.1:80
Server {
    root_path: "./",
    buffer_size: 10240,
    gzip_level: 1,
    log_dir_path: "./logs",
    empty_path_try_files_path: "./index.html",
    response_header_list: [
        "Server: RUST-WEB-SERVER",
        "Access-Control-Allow-Credentials: true",
        "Access-Control-Allow-Headers: *",
        "Access-Control-Allow-Methods: *",
        "Access-Control-Allow-Origin: *",
        "Git: https://git.ltpp.vip/root/RUST-WEB-SERVE.git",
        "Content-Type: text/html",
        "Content-Encoding: gzip",
    ],
    proxy: [],
    proxy_timeout_seconds: 10,
    hotlink_protection: [],
}
Binding => localhost:80
Server {
    root_path: "./",
    buffer_size: 10240,
    gzip_level: 1,
    log_dir_path: "./logs",
    empty_path_try_files_path: "./index.html",
    response_header_list: [
        "Server: RUST-WEB-SERVER",
        "Access-Control-Allow-Credentials: true",
        "Access-Control-Allow-Headers: *",
        "Access-Control-Allow-Methods: *",
        "Access-Control-Allow-Origin: *",
        "Git: https://git.ltpp.vip/root/RUST-WEB-SERVE.git",
        "Content-Type: application/json",
        "Content-Encoding: gzip",
    ],
    proxy: [
        "https://api.ltpp.vip",
        "https://api.ltpp.vip/Version/getVersion",
        "https://api.ltpp.vip/Version/getVersion?origin=proxy1",
    ],
    proxy_timeout_seconds: 10,
    hotlink_protection: [],
}



[2024-05-28 23:40:07]
Binding => 127.0.0.1:80
Server {
    root_path: "./",
    buffer_size: 10240,
    gzip_level: 1,
    log_dir_path: "./logs",
    empty_path_try_files_path: "./index.html",
    response_header_list: [
        "Server: RUST-WEB-SERVER",
        "Access-Control-Allow-Credentials: true",
        "Access-Control-Allow-Headers: *",
        "Access-Control-Allow-Methods: *",
        "Access-Control-Allow-Origin: *",
        "Git: https://git.ltpp.vip/root/RUST-WEB-SERVE.git",
        "Content-Type: text/html",
        "Content-Encoding: gzip",
    ],
    proxy: [],
    proxy_timeout_seconds: 10,
    hotlink_protection: [],
}
Binding => localhost:80
Server {
    root_path: "./",
    buffer_size: 10240,
    gzip_level: 1,
    log_dir_path: "./logs",
    empty_path_try_files_path: "./index.html",
    response_header_list: [
        "Server: RUST-WEB-SERVER",
        "Access-Control-Allow-Credentials: true",
        "Access-Control-Allow-Headers: *",
        "Access-Control-Allow-Methods: *",
        "Access-Control-Allow-Origin: *",
        "Git: https://git.ltpp.vip/root/RUST-WEB-SERVE.git",
        "Content-Type: application/json",
        "Content-Encoding: gzip",
    ],
    proxy: [
        "https://api.ltpp.vip",
        "https://api.ltpp.vip/Version/getVersion",
        "https://api.ltpp.vip/Version/getVersion?origin=proxy1",
    ],
    proxy_timeout_seconds: 10,
    hotlink_protection: [],
}



[2024-05-28 23:40:07]
Binding => 127.0.0.1:81
Server {
    root_path: "./",
    buffer_size: 10240,
    gzip_level: 1,
    log_dir_path: "./logs",
    empty_path_try_files_path: "./index.html",
    response_header_list: [
        "Access-Control-Allow-Origin: *",
        "Access-Control-Allow-Methods: *",
        "Access-Control-Allow-Headers: *",
        "Access-Control-Allow-Credentials: true",
        "Server: RUST-WEB-SERVER",
        "Git: https://git.ltpp.vip/root/RUST-WEB-SERVE.git",
        "Content-Type: application/json",
        "Content-Encoding: gzip",
    ],
    proxy: [
        "https://api.ltpp.vip/Version/getVersion?origin=proxy1",
    ],
    proxy_timeout_seconds: 10,
    hotlink_protection: [
        "origin=1$",
    ],
}



[2024-05-28 23:40:07]
Listening => 0.0.0.0:80


[2024-05-28 23:40:07]
Binding => 127.0.0.1:80


[2024-05-28 23:40:07]
Binding => localhost:80


[2024-05-28 23:40:07]
Binding => 127.0.0.1:80


[2024-05-28 23:40:07]
Listening => 0.0.0.0:81


[2024-05-28 23:40:07]
Binding => 127.0.0.1:81


[2024-05-28 23:40:07]
Binding => localhost:81


[2024-05-28 23:40:07]
Binding => 127.0.0.1:81


[2024-05-28 23:40:10]
HttpRequest {
    path: "/_static/out/browser/serviceWorker.js",
    method: "GET",
    query: [],
    headers: {
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "accept": "*/*",
        "service-worker": "script",
        "sec-fetch-dest": "serviceworker",
        "sec-fetch-mode": "same-origin",
        "referer": "http://127.0.0.1/_static/out/browser/serviceWorker.js",
        "accept-encoding": "gzip, deflate, br, zstd",
        "cache-control": "max-age=0",
        "host": "127.0.0.1",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0",
        "cookie": "SameSite=Lax; Hm_lvt_9793f42b498361373512340937deb2a0=1689155374; _ga=GA1.1.39230079.1707025003; _ga_69MPZE94D5=GS1.1.1707025002.1.1.1707026740.0.0.0; pma_lang=zh_CN; pma_theme=bootstrap; pmaUser-1=5RJrrwsVlvqY7uteEWcRoXI%2BvTKWWpA23SKZpX9BCDRw8Q17ueAcTXRd8Po%3D",
        "sec-fetch-site": "same-origin",
        "connection": "keep-alive",
    },
    body: {
        "sec-fetch-site": [
            "same-origin",
        ],
        "accept-encoding": [
            "gzip, deflate, br, zstd",
        ],
        "accept": [
            "*/*",
        ],
        "user-agent": [
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0",
        ],
        "connection": [
            "keep-alive",
        ],
        "cache-control": [
            "max-age=0",
        ],
        "cookie": [
            "SameSite=Lax; Hm_lvt_9793f42b498361373512340937deb2a0=1689155374; _ga=GA1.1.39230079.1707025003; _ga_69MPZE94D5=GS1.1.1707025002.1.1.1707026740.0.0.0; pma_lang=zh_CN; pma_theme=bootstrap; pmaUser-1=5RJrrwsVlvqY7uteEWcRoXI%2BvTKWWpA23SKZpX9BCDRw8Q17ueAcTXRd8Po%3D",
        ],
        "accept-language": [
            "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        ],
        "sec-fetch-mode": [
            "same-origin",
        ],
        "host": [
            "127.0.0.1",
        ],
        "referer": [
            "http://127.0.0.1/_static/out/browser/serviceWorker.js",
        ],
        "sec-fetch-dest": [
            "serviceworker",
        ],
        "service-worker": [
            "script",
        ],
    },
}

[2024-05-28 23:40:10]
Resource load fail => ./_static/out/browser/serviceWorker.js


[2024-05-28 23:40:21]
HttpRequest {
    path: "/Version/getVersion",
    method: "GET",
    query: [
        (
            "origin",
            "proxy1",
        ),
    ],
    headers: {
        "sec-ch-ua-mobile": "?0",
        "connection": "keep-alive",
        "sec-ch-ua": "\"Microsoft Edge\";v=\"125\", \"Chromium\";v=\"125\", \"Not.A/Brand\";v=\"24\"",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0",
        "sec-fetch-site": "none",
        "sec-fetch-user": "?1",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "sec-fetch-dest": "document",
        "host": "127.0.0.1:81",
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "accept-encoding": "gzip, deflate, br, zstd",
        "upgrade-insecure-requests": "1",
        "sec-fetch-mode": "navigate",
        "cookie": "SameSite=Lax; Hm_lvt_9793f42b498361373512340937deb2a0=1689155374; _ga=GA1.1.39230079.1707025003; _ga_69MPZE94D5=GS1.1.1707025002.1.1.1707026740.0.0.0; pma_lang=zh_CN; pma_theme=bootstrap; pmaUser-1=5RJrrwsVlvqY7uteEWcRoXI%2BvTKWWpA23SKZpX9BCDRw8Q17ueAcTXRd8Po%3D",
        "sec-ch-ua-platform": "\"Windows\"",
    },
    body: {
        "sec-fetch-user": [
            "?1",
        ],
        "accept-language": [
            "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        ],
        "sec-fetch-site": [
            "none",
        ],
        "accept": [
            "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        ],
        "sec-ch-ua-platform": [
            "\"Windows\"",
        ],
        "accept-encoding": [
            "gzip, deflate, br, zstd",
        ],
        "host": [
            "127.0.0.1:81",
        ],
        "sec-fetch-mode": [
            "navigate",
        ],
        "upgrade-insecure-requests": [
            "1",
        ],
        "sec-fetch-dest": [
            "document",
        ],
        "sec-ch-ua-mobile": [
            "?0",
        ],
        "cookie": [
            "SameSite=Lax; Hm_lvt_9793f42b498361373512340937deb2a0=1689155374; _ga=GA1.1.39230079.1707025003; _ga_69MPZE94D5=GS1.1.1707025002.1.1.1707026740.0.0.0; pma_lang=zh_CN; pma_theme=bootstrap; pmaUser-1=5RJrrwsVlvqY7uteEWcRoXI%2BvTKWWpA23SKZpX9BCDRw8Q17ueAcTXRd8Po%3D",
        ],
        "sec-ch-ua": [
            "\"Microsoft Edge\";v=\"125\", \"Chromium\";v=\"125\", \"Not.A/Brand\";v=\"24\"",
        ],
        "connection": [
            "keep-alive",
        ],
        "user-agent": [
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0",
        ],
    },
}

[2024-05-28 23:40:21]
Proxy url info => https://api.ltpp.vip:443/Version/getVersion?origin=proxy1&origin=proxy1


[2024-05-28 23:40:22]
Request response info:
HTTP/1.1 200 OK
content-length: 90
access-control-allow-methods: GET, POST, PATCH
access-control-allow-headers: Authorization,Requestid,Key,Content-Type,If-Match,If-Modified-Since,If-None-Match,If-Unmodified-Since,X-CSRF-TOKEN,X-Requested-With
git: https://git.ltpp.vip/root/RUST-WEB-SERVE.git
access-control-allow-origin: *
content-encoding: gzip
access-control-allow-credentials: true
content-type: application/json
server: nginx
connection: keep-alive
date: Tue, 28 May 2024 15:40:21 GMT
strict-transport-security: max-age=88888888
access-control-max-age: 88888888



{"code":1,"version":"2.8.0","ltpp_win_download_url":"","ltpp_mac_download_url":"","ltpp_apk_download_url":""}


[2024-05-28 23:40:24]
HttpRequest {
    path: "/Version/getVersion",
    method: "GET",
    query: [
        (
            "origin",
            "proxy1",
        ),
    ],
    headers: {
        "connection": "keep-alive",
        "accept-encoding": "gzip, deflate, br, zstd",
        "cache-control": "max-age=0",
        "sec-fetch-mode": "navigate",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-site": "none",
        "host": "localhost",
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "sec-ch-ua-mobile": "?0",
        "sec-fetch-user": "?1",
        "sec-fetch-dest": "document",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0",
        "upgrade-insecure-requests": "1",
        "cookie": "SameSite=Lax; NMTID=00O6oBSFUZ8KxSqKEY4tcbYD677Qa8AAAGKxntltw; pma_lang=zh_CN; pma_theme=bootstrap; pmaUser-1=c3t%2FwkS4xVdqQ2GLp7IkQdf9cc4d3ClA8ae5euc5DJ4EKvgSZv6qnjCaabI%3D",
        "sec-ch-ua": "\"Microsoft Edge\";v=\"125\", \"Chromium\";v=\"125\", \"Not.A/Brand\";v=\"24\"",
    },
    body: {
        "sec-ch-ua-platform": [
            "\"Windows\"",
        ],
        "sec-ch-ua": [
            "\"Microsoft Edge\";v=\"125\", \"Chromium\";v=\"125\", \"Not.A/Brand\";v=\"24\"",
        ],
        "upgrade-insecure-requests": [
            "1",
        ],
        "user-agent": [
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0",
        ],
        "cookie": [
            "SameSite=Lax; NMTID=00O6oBSFUZ8KxSqKEY4tcbYD677Qa8AAAGKxntltw; pma_lang=zh_CN; pma_theme=bootstrap; pmaUser-1=c3t%2FwkS4xVdqQ2GLp7IkQdf9cc4d3ClA8ae5euc5DJ4EKvgSZv6qnjCaabI%3D",
        ],
        "accept": [
            "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        ],
        "cache-control": [
            "max-age=0",
        ],
        "host": [
            "localhost",
        ],
        "sec-fetch-user": [
            "?1",
        ],
        "sec-fetch-dest": [
            "document",
        ],
        "accept-language": [
            "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        ],
        "connection": [
            "keep-alive",
        ],
        "sec-fetch-mode": [
            "navigate",
        ],
        "sec-ch-ua-mobile": [
            "?0",
        ],
        "accept-encoding": [
            "gzip, deflate, br, zstd",
        ],
        "sec-fetch-site": [
            "none",
        ],
    },
}

[2024-05-28 23:40:24]
Proxy url info => https://api.ltpp.vip:443/Version/getVersion?origin=proxy1


[2024-05-28 23:40:24]
Request response info:
HTTP/1.1 200 OK
content-length: 90
strict-transport-security: max-age=88888888
access-control-max-age: 88888888
access-control-allow-headers: Authorization,Requestid,Key,Content-Type,If-Match,If-Modified-Since,If-None-Match,If-Unmodified-Since,X-CSRF-TOKEN,X-Requested-With
content-type: application/json
access-control-allow-origin: *
access-control-allow-methods: GET, POST, PATCH
git: https://git.ltpp.vip/root/RUST-WEB-SERVE.git
date: Tue, 28 May 2024 15:40:23 GMT
server: nginx
connection: keep-alive
content-encoding: gzip
access-control-allow-credentials: true



{"code":1,"version":"2.8.0","ltpp_win_download_url":"","ltpp_mac_download_url":"","ltpp_apk_download_url":""}