|
|
@@ -1,94 +1,189 @@
|
|
|
-use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
|
|
-use crate::proxy;
|
|
|
-use tungstenite::client::{ connect_with_proxy, ProxyAutoStream};
|
|
|
-use tungstenite::{ WebSocket};
|
|
|
-use tungstenite::protocol::WebSocketConfig;
|
|
|
-use url::Url;
|
|
|
-use crate::proxy::ParsingDetail;
|
|
|
-
|
|
|
-
|
|
|
-pub enum WsType {
|
|
|
- WsP(WsProxy),
|
|
|
- Ws(WsNoProxy),
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-#[derive(Debug)]
|
|
|
-pub struct WsProxy {
|
|
|
- socket: Option<WebSocket<ProxyAutoStream>>,
|
|
|
- proxy: ParsingDetail,
|
|
|
-}
|
|
|
-
|
|
|
-impl WsProxy {
|
|
|
- pub fn new(proxy: ParsingDetail) -> WsProxy {
|
|
|
- WsProxy { socket: None, proxy }
|
|
|
- }
|
|
|
- pub fn connect(&mut self, base_url: String) {
|
|
|
- let request_url = Url::parse(base_url.as_str()).unwrap();
|
|
|
-
|
|
|
-
|
|
|
- let ip_array: Vec<&str> = self.proxy.ip_address.split(".").collect();
|
|
|
- let proxy_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(
|
|
|
- ip_array[0].parse().unwrap(),
|
|
|
- ip_array[1].parse().unwrap(),
|
|
|
- ip_array[2].parse().unwrap(),
|
|
|
- ip_array[3].parse().unwrap())
|
|
|
- ), self.proxy.port.parse().unwrap());
|
|
|
- let websocket_config = Some(WebSocketConfig {
|
|
|
- max_send_queue: Some(16),
|
|
|
- max_message_size: Some(16 * 1024 * 1024),
|
|
|
- max_frame_size: Some(16 * 1024 * 1024),
|
|
|
- accept_unmasked_frames: false,
|
|
|
- });
|
|
|
- let max_redirects = 5;
|
|
|
- match connect_with_proxy(request_url.clone(), proxy_address, websocket_config, max_redirects) {
|
|
|
- Ok((socket, _)) => {
|
|
|
- // 连接成功,可以继续处理逻辑
|
|
|
- self.socket = Option::from(socket);
|
|
|
- println!("----ws-链接成功");
|
|
|
- }
|
|
|
- Err(err) => {
|
|
|
- panic!("连接代理失败:{:?}", err);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-#[derive(Debug)]
|
|
|
-pub struct WsNoProxy {}
|
|
|
-
|
|
|
-impl WsNoProxy {
|
|
|
- pub fn new() -> WsNoProxy {
|
|
|
- WsNoProxy {}
|
|
|
- }
|
|
|
- pub fn connect(&mut self, base_url: String, proxy_ip: String, proxy_port: u16) -> Result<bool, bool> {
|
|
|
- Ok(true)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-pub struct WsTool {
|
|
|
- base_url: String,
|
|
|
- socket: WsType,
|
|
|
-}
|
|
|
-
|
|
|
-impl WsTool {
|
|
|
- pub fn new(base_url: String) -> WsTool {
|
|
|
- /*******走代理:根据环境变量配置来决定,如果配置了走代理,没有配置不走*******/
|
|
|
- let parsing_detail = proxy::ParsingDetail::parsing_environment_variables();
|
|
|
- if parsing_detail.ip_address.len() > 0 && parsing_detail.port.len() > 0 {
|
|
|
- WsTool { base_url, socket: WsType::WsP(WsProxy::new(parsing_detail)) }
|
|
|
- } else {
|
|
|
- WsTool { base_url, socket: WsType::Ws(WsNoProxy::new()) }
|
|
|
- }
|
|
|
- }
|
|
|
- pub fn connect(&self) {
|
|
|
- let url = self.base_url.clone();
|
|
|
- match &self.socket {
|
|
|
- WsType::WsP(ws_no_proxy) => {
|
|
|
- // let mut mutable_ws = ws_no_proxy.clone();
|
|
|
- // mutable_ws.connect(url);
|
|
|
- }
|
|
|
- WsType::Ws(ws_no_proxy) => {}
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
+// use std::io;
|
|
|
+// use std::io::{Write};
|
|
|
+// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
|
|
+// use crate::proxy;
|
|
|
+// use tungstenite::client::{AutoStream, connect_with_proxy, ProxyAutoStream};
|
|
|
+// use tungstenite::{Message, WebSocket};
|
|
|
+// use tungstenite::protocol::WebSocketConfig;
|
|
|
+// use url::Url;
|
|
|
+// use crate::proxy::ParsingDetail;
|
|
|
+// use crate::socket_tool::WsType::{WsNone, WsP};
|
|
|
+//
|
|
|
+//
|
|
|
+// pub enum WsType {
|
|
|
+// WsP(WebSocket<ProxyAutoStream>),
|
|
|
+// Ws(WebSocket<AutoStream>),
|
|
|
+// WsNone(String),
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// #[derive(Debug)]
|
|
|
+// pub struct WsProxy {
|
|
|
+// socket: Option<WebSocket<ProxyAutoStream>>,
|
|
|
+// proxy: ParsingDetail,
|
|
|
+// }
|
|
|
+//
|
|
|
+// impl WsProxy {
|
|
|
+// pub fn new(proxy: ParsingDetail) -> WsProxy {
|
|
|
+// WsProxy { socket: None, proxy }
|
|
|
+// }
|
|
|
+// pub fn connect(&mut self, base_url: String) {
|
|
|
+// loop {
|
|
|
+// println!("--开始链接:");
|
|
|
+// let request_url = Url::parse(base_url.as_str()).unwrap();
|
|
|
+//
|
|
|
+// let ip_array: Vec<&str> = self.proxy.ip_address.split(".").collect();
|
|
|
+// let proxy_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(
|
|
|
+// ip_array[0].parse().unwrap(),
|
|
|
+// ip_array[1].parse().unwrap(),
|
|
|
+// ip_array[2].parse().unwrap(),
|
|
|
+// ip_array[3].parse().unwrap())
|
|
|
+// ), self.proxy.port.parse().unwrap());
|
|
|
+// let websocket_config = Some(WebSocketConfig {
|
|
|
+// max_send_queue: Some(16),
|
|
|
+// max_message_size: Some(16 * 1024 * 1024),
|
|
|
+// max_frame_size: Some(16 * 1024 * 1024),
|
|
|
+// accept_unmasked_frames: false,
|
|
|
+// });
|
|
|
+// let max_redirects = 5;
|
|
|
+// let ws = match connect_with_proxy(request_url.clone(), proxy_address, websocket_config, max_redirects) {
|
|
|
+// Ok((socket, res)) => {
|
|
|
+// socket
|
|
|
+// }
|
|
|
+// Err(err) => {
|
|
|
+// println!("连接代理失败:{:?},重连启动", err);
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+// };
|
|
|
+// self.socket = Option::from(ws);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// pub fn subscription(ws_proxy: &WsProxy) {
|
|
|
+// loop {}
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// #[derive(Debug)]
|
|
|
+// pub struct WsNoProxy {}
|
|
|
+//
|
|
|
+// impl WsNoProxy {
|
|
|
+// pub fn new() -> WsNoProxy {
|
|
|
+// WsNoProxy {}
|
|
|
+// }
|
|
|
+// pub fn connect(&mut self, base_url: String, proxy_ip: String, proxy_port: u16) -> Result<bool, bool> {
|
|
|
+// Ok(true)
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// pub struct WsTool {
|
|
|
+// base_url: String,
|
|
|
+// ws_type: WsType,
|
|
|
+// proxy: ParsingDetail,
|
|
|
+// }
|
|
|
+//
|
|
|
+// impl WsTool {
|
|
|
+// pub fn new(base_url: String) -> WsTool {
|
|
|
+// /*******走代理:根据环境变量配置来决定,如果配置了走代理,没有配置不走*******/
|
|
|
+// let parsing_detail = proxy::ParsingDetail::parsing_environment_variables();
|
|
|
+// if parsing_detail.ip_address.len() > 0 && parsing_detail.port.len() > 0 {
|
|
|
+// WsTool { base_url, ws_type: WsP(""), proxy: parsing_detail }
|
|
|
+// } else {
|
|
|
+// WsTool { base_url, ws_type: WsType::Ws(WebSocket::<AutoStream>::new()), proxy: parsing_detail }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// pub fn connect(&mut self) {
|
|
|
+// let mut res = false;
|
|
|
+// match &self.ws_type {
|
|
|
+// WsType::WsP(ws_p) => {
|
|
|
+// self.proxy_connect();
|
|
|
+// }
|
|
|
+// WsType::Ws(ws_p) => {
|
|
|
+// self.proxy_connect();
|
|
|
+// }
|
|
|
+// WsNone(ws_none) => {}
|
|
|
+// }
|
|
|
+// }
|
|
|
+// pub fn read(&mut self) {
|
|
|
+// let mut res = false;
|
|
|
+// match &self.ws_type {
|
|
|
+// WsType::WsP(ws_p) => {
|
|
|
+// println!("????????");
|
|
|
+// loop {
|
|
|
+// println!("1231321");
|
|
|
+// }
|
|
|
+// }
|
|
|
+// WsType::Ws(ws_p) => {}
|
|
|
+// WsNone(ws_none) => {}
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// /**读取*/
|
|
|
+// // pub fn proxy_read(mut ws_p: &WebSocket<ProxyAutoStream>) {
|
|
|
+// // /*****消息溜***/
|
|
|
+// // let mut stdout = io::stdout();
|
|
|
+// // let mut stderr = io::stderr();
|
|
|
+// // /******订阅信息********/
|
|
|
+// // loop {
|
|
|
+// // let msg =ws_p.read_message();
|
|
|
+// // match msg {
|
|
|
+// // Ok(Message::Text(text)) => {
|
|
|
+// // writeln!(stdout, "Text-响应--{:?}", text.clone()).unwrap();
|
|
|
+// // }
|
|
|
+// // Ok(Message::Ping(s)) => {
|
|
|
+// // writeln!(stdout, "Ping-响应--{:?}", String::from_utf8(s.clone())).unwrap();
|
|
|
+// // let _ = ws_p.write_message(Message::Pong(Vec::from("pong")));
|
|
|
+// // writeln!(stdout, "回应-pong---{:?}", String::from_utf8(s.clone())).unwrap();
|
|
|
+// // }
|
|
|
+// // Ok(Message::Pong(s)) => {
|
|
|
+// // // println!("Pong-响应--{:?}", String::from_utf8(s));
|
|
|
+// // writeln!(stdout, "Pong-响应--{:?}", String::from_utf8(s.clone())).unwrap();
|
|
|
+// // }
|
|
|
+// // Ok(Message::Close(_)) => {
|
|
|
+// // // println!("socket 关闭: ");
|
|
|
+// // writeln!(stdout, "Close-响应").unwrap();
|
|
|
+// // }
|
|
|
+// // Err(error) => {
|
|
|
+// // // println!("Error receiving message: {}", error);
|
|
|
+// // writeln!(stdout, "Err-响应{}", error).unwrap();
|
|
|
+// // break;
|
|
|
+// // }
|
|
|
+// // _ => {}
|
|
|
+// // }
|
|
|
+// // }
|
|
|
+// // ws_p.close(None).unwrap();
|
|
|
+// // }
|
|
|
+//
|
|
|
+// /**链接*/
|
|
|
+// //代理
|
|
|
+// pub fn proxy_connect(&mut self) {
|
|
|
+// println!("--开始链接:");
|
|
|
+// let request_url = Url::parse(self.base_url.as_str()).unwrap();
|
|
|
+//
|
|
|
+// let ip_array: Vec<&str> = self.proxy.ip_address.split(".").collect();
|
|
|
+// let proxy_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(
|
|
|
+// ip_array[0].parse().unwrap(),
|
|
|
+// ip_array[1].parse().unwrap(),
|
|
|
+// ip_array[2].parse().unwrap(),
|
|
|
+// ip_array[3].parse().unwrap())
|
|
|
+// ), self.proxy.port.parse().unwrap());
|
|
|
+// let websocket_config = Some(WebSocketConfig {
|
|
|
+// max_send_queue: Some(16),
|
|
|
+// max_message_size: Some(16 * 1024 * 1024),
|
|
|
+// max_frame_size: Some(16 * 1024 * 1024),
|
|
|
+// accept_unmasked_frames: false,
|
|
|
+// });
|
|
|
+// let max_redirects = 5;
|
|
|
+// let ws = match connect_with_proxy(request_url.clone(), proxy_address, websocket_config, max_redirects) {
|
|
|
+// Ok((socket, res)) => {
|
|
|
+// socket
|
|
|
+// }
|
|
|
+// Err(err) => {
|
|
|
+// println!("连接代理失败:{:?},重连启动", err);
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// };
|
|
|
+// self.ws_type = WsP(ws);
|
|
|
+// }
|
|
|
+// //非代理
|
|
|
+// }
|