4

我想使用warp服务多个连接,以便我可以将每个 http 请求重定向到 https。这就是我现在要做的。

#[tokio::main]
async fn main() {

    let http_routes = warp::path("http").map(|| {
        println!("This is http server");
        warp::redirect(Uri::from_static("https://127.0.0.1:2443"))
    });

    let https_routes = warp::any().map(|| {
        println!("This is https server");
        "Hello, World! You are connected through tls connection."
    });

    let _http_warp = warp::serve(http_routes)
        .run(([127, 0, 0, 1], 2080)).await;

    let _https_warp = warp::serve(https_routes)
        .tls()
        .cert_path("sec/dmp.cert")
        .key_path("sec/dmp.key")
        .run(([127, 0, 0, 1], 2443)).await;
}

这不能按预期工作。它只侦听端口 2080 而不是 2443

我也试过future::join

#[tokio::main]
async fn main() {

    ----snip----

    let (_http_addr, http_warp) = warp::serve(http_routes)
        .bind_ephemeral(([127, 0, 0, 1], 2080));

    let (_https_addr, https_warp) = warp::serve(https_routes)
        .tls()
        .cert_path("sec/dmp.cert")
        .key_path("sec/dmp.key")
        .bind_ephemeral(([127, 0, 0, 1], 2443));

    future::join(http_warp, https_warp).await?
}

这给出了错误

error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
  --> src/main.rs:27:5
   |
27 |     future::join(http_wrap, https_wrap).await?
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `((), ())`
   |
   = help: the trait `std::ops::Try` is not implemented for `((), ())`
   = note: required by `std::ops::Try::into_result`
4

2 回答 2

3

让它运行完全是一件简单的事情,那就是删除?await 语句的后缀在末尾并放置一个分号。

future::join(http_wrap, https_wrap).await? // FROM THIS
future::join(http_wrap, https_wrap).await; // TO THIS

就是这样。现在两个期货同时运行,因此它监听两个端口。

于 2020-04-25T10:27:15.937 回答
0

这是kanudo 的解决方案,作为经纱中 tls https 重定向的完整工作示例

证书.pem

-----BEGIN CERTIFICATE-----
MIIEADCCAmigAwIBAgICAcgwDQYJKoZIhvcNAQELBQAwLDEqMCgGA1UEAwwhcG9u
eXRvd24gUlNBIGxldmV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
hRFNrXPvq5Y+2bI/DxtdzZLKAMXOMjDjj0XEgfOIn2aveOo3uE7zf1i+njxwQhPu
uSYA9AlBZiKGr2PCYSDPnViHOspVJjxRuAgyWM1Qf+CTC0D95aj0oz8CgYEAz5n4
Cb3/WfUHxMJLljJ7PlVmlQpF5Hk3AOR9+vtqTtdxRjuxW6DH2uAHBDdC3OgppUN4
CFj55kzc2HUuiHtmPtx8mK6G+otT7Lww+nLSFL4PvZ6CYxqcio5MPnoYd+pCxrXY
JFo2W7e4FkBOxb5PF5So5plg+d0z/QiA7aFP1osCgYEAtgi1rwC5qkm8prn4tFm6
hkcVCIXc+IWNS0Bu693bXKdGr7RsmIynff1zpf4ntYGpEMaeymClCY0ppDrMYlzU
RBYiFNdlBvDRj6s/H+FTzHRk2DT/99rAhY9nzVY0OQFoQIXK8jlURGrkmI/CYy66
XqBmo5t4zcHM7kaeEBOWEKkCgYAYnO6VaRtPNQfYwhhoFFAcUc+5t+AVeHGW/4AY
M5qlAlIBu64JaQSI5KqwS0T4H+ZgG6Gti68FKPO+DhaYQ9kZdtam23pRVhd7J8y+
xMI3h1kiaBqZWVxZ6QkNFzizbui/2mtn0/JB6YQ/zxwHwcpqx0tHG8Qtm5ZAV7PB
eLCYhQKBgQDALJxU/6hMTdytEU5CLOBSMby45YD/RrfQrl2gl/vA0etPrto4RkVq
UrkDO/9W4mZORClN3knxEFSTlYi8YOboxdlynpFfhcs82wFChs+Ydp1eEsVHAqtu
T+uzn0sroycBiBfVB949LExnzGDFUkhG0i2c2InarQYLTsIyHCIDEA==
-----END RSA PRIVATE KEY-----

货运.toml

[dependencies]
tokio = { version = "0.2", features = ["full"] }
warp = { version = "0.2", features = ["tls"] }
futures = "0.3.8"

main.rs

use warp::hyper::Uri;
use warp::Filter;
use futures::future;

#[tokio::main]
async fn main() {

    let http_routes = warp::path("http").map(|| {
        println!("This is http server");
        warp::redirect(Uri::from_static("https://127.0.0.1:2443"))
    });

    let https_routes = warp::any().map(|| {
        println!("This is https server");
        "Hello, World! You are connected through tls connection."
    });

    let (_http_addr, http_warp) = warp::serve(http_routes)
        .bind_ephemeral(([127, 0, 0, 1], 2080));

    let (_https_addr, https_warp) = warp::serve(https_routes)
        .tls()
        .cert_path("examples/tls/cert.pem")
        .key_path("examples/tls/key.rsa")
        .bind_ephemeral(([127, 0, 0, 1], 2443));

    future::join(http_warp, https_warp).await;
}

http://localhost:2080/http 重定向到:https ://127.0.0.1:2443/

Hello, World! You are connected through tls connection.

于 2020-12-08T05:20:16.327 回答