@@ -25,9 +25,9 @@ pub async fn wss(certs: &(Vec<Certificate>, PrivateKey)) -> Result<()> {
25
25
let acceptor = tokio_rustls:: TlsAcceptor :: from ( Arc :: new ( config) ) ;
26
26
while let Some ( ( stream, _) ) = listener. accept ( ) . await . log ( ) {
27
27
let acceptor = acceptor. clone ( ) ;
28
- if let Some ( peer_address) = stream. peer_addr ( ) . log ( )
28
+ if let Some ( peer_address) = stream. local_addr ( ) . log ( )
29
29
&& let Some ( stream) = acceptor. accept ( stream) . await . log ( ) {
30
- accept_connection ( stream, peer_address) . await . eyre_log ( ) ;
30
+ accept_connection ( stream, peer_address. port ( ) ) . await . log ( ) ;
31
31
} ;
32
32
}
33
33
info ! ( "wss listening stopped" ) ;
@@ -36,29 +36,26 @@ pub async fn wss(certs: &(Vec<Certificate>, PrivateKey)) -> Result<()> {
36
36
pub async fn ws ( ) -> Result < ( ) > {
37
37
let listener = TcpListener :: bind ( & ws_server_addr ( ) ) . await ?;
38
38
while let Some ( ( stream, _) ) = listener. accept ( ) . await . log ( ) {
39
- if let Some ( peer_address ) = stream. peer_addr ( ) . log ( ) {
40
- accept_connection ( stream, peer_address ) . await . eyre_log ( ) ;
39
+ if let Some ( local_address ) = stream. local_addr ( ) . log ( ) {
40
+ accept_connection ( stream, local_address . port ( ) ) . await . log ( ) ;
41
41
} ;
42
42
}
43
43
info ! ( "ws listening stopped" ) ;
44
44
Ok ( ( ) )
45
45
}
46
46
47
- pub async fn accept_connection < S > ( stream : S , peer_address : SocketAddr ) -> Result < ( ) >
47
+ pub async fn accept_connection < S > ( stream : S , peer_id : u16 ) -> Result < ( ) >
48
48
where
49
49
S : AsyncRead + AsyncWrite + Unpin + Send + ' static ,
50
50
{
51
51
// handshake happens here
52
52
let ws_stream = tokio_tungstenite:: accept_async ( stream) . await ?;
53
53
54
- info ! ( "New WebSocket connection: {}" , peer_address) ;
55
-
56
- let conn_id = Arc :: new ( peer_address) ;
54
+ info ! ( "New WebSocket connection: {}" , peer_id) ;
57
55
58
56
let ( tx, mut rx) = mpsc:: channel ( 128 ) ;
59
57
let ( write, mut read) = ws_stream. split ( ) ;
60
58
61
- let conn_id_clone = conn_id. clone ( ) ;
62
59
tokio:: spawn ( async move {
63
60
let mut write = write;
64
61
while let Some ( ws_message) = rx. recv ( ) . await {
@@ -74,14 +71,14 @@ where
74
71
Ok ( _) => { }
75
72
} ;
76
73
}
77
- info ! ( "ws disconnected {}" , conn_id_clone ) ;
74
+ info ! ( "ws disconnected {}" , peer_id ) ;
78
75
rx. close ( ) ;
79
76
} ) ;
80
77
tokio:: spawn ( async move {
81
78
let tx = tx;
82
79
while let Some ( next) = read. next ( ) . await {
83
80
let tx = tx. clone ( ) ;
84
- let conn_id = conn_id . clone ( ) ;
81
+
85
82
match next {
86
83
Err ( ws:: Error :: ConnectionClosed ) | Err ( ws:: Error :: AlreadyClosed ) => {
87
84
break ;
93
90
Err ( e) => error ! ( "{:?}" , e. to_eyre( ) ) ,
94
91
Ok ( ws:: Message :: Binary ( data) ) => {
95
92
tokio:: spawn ( async move {
96
- receive_packets ( data, tx, conn_id ) . await . log ( ) ;
93
+ receive_packets ( data, tx, peer_id ) . await . log ( ) ;
97
94
} ) ;
98
95
}
99
96
Ok ( ws:: Message :: Ping ( data) ) => {
102
99
Ok ( msg) => warn ! ( "unexpected message {}" , msg) ,
103
100
}
104
101
}
105
- info ! ( "ws disconnected {}" , conn_id )
102
+ info ! ( "ws disconnected {}" , peer_id )
106
103
} ) ;
107
104
Ok ( ( ) )
108
105
}
0 commit comments