编译websocket
This commit is contained in:
+7
-5
@@ -17,7 +17,7 @@ parking_lot = "0.12.1"
|
||||
rand = "0.8.5"
|
||||
sha2 = { version = "0.10.6", features = ["oid"] }
|
||||
thiserror = "1.0.37"
|
||||
protobuf = "3.2.0"
|
||||
protobuf = "=3.2.0"
|
||||
socket2 = { version = "0.5.2", features = ["all"] }
|
||||
aes-gcm = { version = "0.10.2", optional = true }
|
||||
ring = { version = "0.17.0", optional = true }
|
||||
@@ -32,7 +32,7 @@ spki = { version = "0.7.2", features = ["fingerprint", "alloc", "base64"], optio
|
||||
openssl-sys = { git = "https://github.com/lbl8603/rust-openssl", optional = true }
|
||||
libsm = { git = "https://github.com/lbl8603/libsm", optional = true }
|
||||
|
||||
mio = { version = "1.0.0", features = ["os-poll", "net", "os-ext"] }
|
||||
mio = { version = "=0.8.11", features = ["os-poll", "net", "os-ext"] }
|
||||
crossbeam-queue = "0.3.11"
|
||||
anyhow = "1.0.82"
|
||||
dns-parser = "0.8.0"
|
||||
@@ -45,18 +45,19 @@ zstd = { version = "0.13.1", optional = true }
|
||||
fnv = "1.0.7"
|
||||
igd = { version = "0.12.1", optional = true }
|
||||
tokio-tungstenite = { version = "0.23.1", optional = true }
|
||||
rustls = { version = "0.23.0", features = ["ring"], default-features = false, optional = true }
|
||||
futures-util = "0.3.30"
|
||||
[target.'cfg(target_os = "windows")'.dependencies]
|
||||
libloading = "0.8.0"
|
||||
|
||||
|
||||
[build-dependencies]
|
||||
protobuf-codegen = "3.2.0"
|
||||
protobuf-codegen = "=3.2.0"
|
||||
protoc-bin-vendored = "3.0.0"
|
||||
cfg_aliases = "0.2.1"
|
||||
|
||||
[features]
|
||||
default = ["websocket", "server_encrypt", "aes_gcm", "aes_cbc", "aes_ecb", "sm4_cbc", "chacha20_poly1305", "ip_proxy", "port_mapping", "lz4_compress", "zstd_compress", "integrated_tun"]
|
||||
default = ["server_encrypt", "aes_gcm", "aes_cbc", "aes_ecb", "sm4_cbc", "chacha20_poly1305", "ip_proxy", "port_mapping", "lz4_compress", "zstd_compress", "integrated_tun"]
|
||||
openssl = ["openssl-sys"]
|
||||
# 从源码编译
|
||||
openssl-vendored = ["openssl-sys/vendored"]
|
||||
@@ -73,4 +74,5 @@ lz4_compress = ["lz4_flex"]
|
||||
zstd_compress = ["zstd"]
|
||||
integrated_tun = ["tun"]
|
||||
upnp = ["igd"]
|
||||
websocket = ["tokio-tungstenite"]
|
||||
ws = ["tokio-tungstenite"]
|
||||
wss = ["ws", "tokio-tungstenite/rustls-tls-native-roots", "rustls"]
|
||||
@@ -8,7 +8,7 @@ use crate::channel::handler::RecvChannelHandler;
|
||||
use crate::channel::sender::{AcceptSocketSender, ConnectUtil};
|
||||
use crate::channel::tcp_channel::tcp_listen;
|
||||
use crate::channel::udp_channel::udp_listen;
|
||||
#[cfg(feature = "websocket")]
|
||||
#[cfg(feature = "ws")]
|
||||
use crate::channel::ws_channel::ws_connect_accept;
|
||||
use crate::util::StopManager;
|
||||
|
||||
@@ -20,7 +20,7 @@ pub mod punch;
|
||||
pub mod sender;
|
||||
pub mod tcp_channel;
|
||||
pub mod udp_channel;
|
||||
#[cfg(feature = "websocket")]
|
||||
#[cfg(feature = "ws")]
|
||||
pub mod ws_channel;
|
||||
|
||||
pub const BUFFER_SIZE: usize = 1024 * 64;
|
||||
@@ -308,7 +308,7 @@ where
|
||||
context.clone(),
|
||||
stop_manager.clone(),
|
||||
)?;
|
||||
#[cfg(feature = "websocket")]
|
||||
#[cfg(feature = "ws")]
|
||||
ws_connect_accept(_ws_connect_r, recv_handler, context.clone(), stop_manager)?;
|
||||
|
||||
Ok((udp_socket_sender, connect_util))
|
||||
|
||||
@@ -8,7 +8,8 @@ use std::thread;
|
||||
use std::time::Duration;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::sync::mpsc::{channel, Receiver};
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
use tokio_tungstenite::tungstenite::http::StatusCode;
|
||||
use tokio_tungstenite::tungstenite::{Error, Message};
|
||||
use tokio_tungstenite::{connect_async, MaybeTlsStream, WebSocketStream};
|
||||
|
||||
use crate::channel::context::ChannelContext;
|
||||
@@ -71,15 +72,44 @@ const WS_ADDR: SocketAddr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFI
|
||||
|
||||
async fn connect_ws<H>(
|
||||
data: Vec<u8>,
|
||||
url: String,
|
||||
mut url: String,
|
||||
recv_handler: H,
|
||||
context: ChannelContext,
|
||||
) -> anyhow::Result<()>
|
||||
where
|
||||
H: RecvChannelHandler,
|
||||
{
|
||||
let (mut ws, response) =
|
||||
tokio::time::timeout(Duration::from_secs(3), connect_async(url)).await??;
|
||||
let mut count = 0;
|
||||
log::info!("尝试建立连接 {:?}", url);
|
||||
let (mut ws, response) = loop {
|
||||
count += 1;
|
||||
if count > 3 {
|
||||
Err(anyhow::anyhow!("发生多次重定向,链接终止"))?
|
||||
}
|
||||
match tokio::time::timeout(Duration::from_secs(3), connect_async(url)).await? {
|
||||
Ok(rs) => break rs,
|
||||
Err(e) => {
|
||||
if let Error::Http(res) = &e {
|
||||
if res.status() == StatusCode::MOVED_PERMANENTLY
|
||||
|| res.status() == StatusCode::FOUND
|
||||
|| res.status() == StatusCode::SEE_OTHER
|
||||
|| res.status() == StatusCode::TEMPORARY_REDIRECT
|
||||
|| res.status() == StatusCode::PERMANENT_REDIRECT
|
||||
{
|
||||
if let Some(v) = res.headers().get("Location") {
|
||||
if let Ok(redirect) = v.to_str() {
|
||||
log::info!("url重定向响应头 {:?}", res.headers());
|
||||
log::info!("url重定向地址 {}", redirect);
|
||||
url = redirect.to_string();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Err(e)?;
|
||||
}
|
||||
}
|
||||
};
|
||||
log::info!("ws协议握手 {:?}", response);
|
||||
ws.send(Message::Binary(data)).await?;
|
||||
let (mut ws_write, ws_read) = ws.split();
|
||||
|
||||
+11
-10
@@ -111,16 +111,17 @@ impl Config {
|
||||
let mut server_address_str = server_address_str.to_lowercase();
|
||||
let mut _query_dns = true;
|
||||
let mut protocol = ConnectProtocol::UDP;
|
||||
#[cfg(feature = "websocket")]
|
||||
{
|
||||
if server_address_str.starts_with("ws://") {
|
||||
protocol = ConnectProtocol::WS;
|
||||
_query_dns = false;
|
||||
}
|
||||
if server_address_str.starts_with("wss://") {
|
||||
protocol = ConnectProtocol::WSS;
|
||||
_query_dns = false;
|
||||
}
|
||||
if server_address_str.starts_with("ws://") {
|
||||
#[cfg(not(feature = "ws"))]
|
||||
Err(anyhow!("Ws not supported"))?;
|
||||
protocol = ConnectProtocol::WS;
|
||||
_query_dns = false;
|
||||
}
|
||||
if server_address_str.starts_with("wss://") {
|
||||
#[cfg(not(feature = "wss"))]
|
||||
Err(anyhow!("Wss not supported"))?;
|
||||
protocol = ConnectProtocol::WSS;
|
||||
_query_dns = false;
|
||||
}
|
||||
|
||||
let mut server_address = "0.0.0.0:0".parse().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user