mirror of
https://github.com/trafficlunar/computer-statistics.git
synced 2026-06-27 22:24:09 +00:00
fix: add authorization to websocket
This commit is contained in:
parent
b9c7be71ae
commit
8d84c99979
4 changed files with 334 additions and 3 deletions
|
|
@ -1,11 +1,32 @@
|
|||
use std::{env, net::TcpStream};
|
||||
|
||||
use tungstenite::{stream::MaybeTlsStream, Message, WebSocket};
|
||||
use tungstenite::{handshake::client::generate_key, http::Request, stream::MaybeTlsStream, Message, WebSocket};
|
||||
use url::Url;
|
||||
|
||||
use crate::notifications;
|
||||
|
||||
pub fn connect() -> Result<WebSocket<MaybeTlsStream<TcpStream>>, tungstenite::Error> {
|
||||
let (socket, _) = match tungstenite::connect(env::var("WEBSOCKET_URL").unwrap()) {
|
||||
let websocket_url = Url::parse(&env::var("WEBSOCKET_URL").unwrap()).expect("Invalid WebSocket URL");
|
||||
let host = websocket_url.host_str().expect("Host not found in URL");
|
||||
|
||||
let host_header = match websocket_url.port() {
|
||||
Some(port) => format!("{}:{}", host, port),
|
||||
None => host.to_string()
|
||||
};
|
||||
|
||||
let request = Request::builder()
|
||||
.method("GET")
|
||||
.uri(env::var("WEBSOCKET_URL").unwrap())
|
||||
.header("Authorization", env::var("WEBSOCKET_PASSWORD").unwrap())
|
||||
.header("Host", host_header)
|
||||
.header("Connection", "Upgrade")
|
||||
.header("Upgrade", "websocket")
|
||||
.header("Sec-WebSocket-Version", "13")
|
||||
.header("Sec-WebSocket-Key", generate_key())
|
||||
.body(())
|
||||
.unwrap();
|
||||
|
||||
let (socket, _) = match tungstenite::connect(request) {
|
||||
Ok(ws) => ws,
|
||||
Err(err) => {
|
||||
eprintln!("Unable to connect to WebSocket: {}", err);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue