@@ -39,48 +39,64 @@ pub async fn run(port: u16, rpc_sender: RpcSender) {
39
39
40
40
use super :: rpc:: RpcP2pConnectionIncomingResponse ;
41
41
42
+ let handle = |sender : RpcSender , offer : Box < webrtc:: Offer > | async move {
43
+ let mut rx = sender
44
+ . multishot_request (
45
+ 2 ,
46
+ RpcRequest :: P2pConnectionIncoming ( P2pConnectionIncomingInitOpts {
47
+ peer_id : PeerId :: from_public_key ( offer. identity_pub_key . clone ( ) ) ,
48
+ signaling : IncomingSignalingMethod :: Http ,
49
+ offer,
50
+ } ) ,
51
+ )
52
+ . await ;
53
+
54
+ match rx. recv ( ) . await {
55
+ Some ( RpcP2pConnectionIncomingResponse :: Answer ( answer) ) => {
56
+ let status = match & answer {
57
+ P2pConnectionResponse :: Accepted ( _) => StatusCode :: OK ,
58
+ P2pConnectionResponse :: Rejected ( reason) => match reason. is_bad ( ) {
59
+ false => StatusCode :: OK ,
60
+ true => StatusCode :: BAD_REQUEST ,
61
+ } ,
62
+ P2pConnectionResponse :: SignalDecryptionFailed => StatusCode :: BAD_REQUEST ,
63
+ P2pConnectionResponse :: InternalError => StatusCode :: INTERNAL_SERVER_ERROR ,
64
+ } ;
65
+ with_json_reply ( & answer, status)
66
+ }
67
+ _ => {
68
+ let resp = P2pConnectionResponse :: internal_error_str ( ) ;
69
+ with_json_reply ( & resp, StatusCode :: INTERNAL_SERVER_ERROR )
70
+ }
71
+ }
72
+ } ;
73
+
42
74
let rpc_sender_clone = rpc_sender. clone ( ) ;
43
- warp:: path!( "mina" / "webrtc" / "signal" )
44
- . and ( warp:: post ( ) )
45
- . and ( warp:: filters:: body:: json ( ) )
46
- . then ( move |offer : Box < webrtc:: Offer > | {
75
+ let get = warp:: path!( "mina" / "webrtc" / "signal" / String )
76
+ . and ( warp:: get ( ) )
77
+ . then ( move |offer : String | {
47
78
let rpc_sender_clone = rpc_sender_clone. clone ( ) ;
48
79
async move {
49
- let mut rx = rpc_sender_clone
50
- . multishot_request (
51
- 2 ,
52
- RpcRequest :: P2pConnectionIncoming ( P2pConnectionIncomingInitOpts {
53
- peer_id : PeerId :: from_public_key ( offer. identity_pub_key . clone ( ) ) ,
54
- signaling : IncomingSignalingMethod :: Http ,
55
- offer,
56
- } ) ,
57
- )
58
- . await ;
59
-
60
- match rx. recv ( ) . await {
61
- Some ( RpcP2pConnectionIncomingResponse :: Answer ( answer) ) => {
62
- let status = match & answer {
63
- P2pConnectionResponse :: Accepted ( _) => StatusCode :: OK ,
64
- P2pConnectionResponse :: Rejected ( reason) => match reason. is_bad ( ) {
65
- false => StatusCode :: OK ,
66
- true => StatusCode :: BAD_REQUEST ,
67
- } ,
68
- P2pConnectionResponse :: SignalDecryptionFailed => {
69
- StatusCode :: BAD_REQUEST
70
- }
71
- P2pConnectionResponse :: InternalError => {
72
- StatusCode :: INTERNAL_SERVER_ERROR
73
- }
74
- } ;
75
- with_json_reply ( & answer, status)
76
- }
77
- _ => {
78
- let resp = P2pConnectionResponse :: internal_error_str ( ) ;
79
- with_json_reply ( & resp, StatusCode :: INTERNAL_SERVER_ERROR )
80
- }
80
+ let decode_res = Err ( ( ) ) . or_else ( move |_| {
81
+ let json = bs58:: decode ( & offer) . into_vec ( ) . or ( Err ( ( ) ) ) ?;
82
+ serde_json:: from_slice ( & json) . or ( Err ( ( ) ) )
83
+ } ) ;
84
+ match decode_res {
85
+ Err ( ( ) ) => with_json_reply (
86
+ & P2pConnectionResponse :: SignalDecryptionFailed ,
87
+ StatusCode :: BAD_REQUEST ,
88
+ ) ,
89
+ Ok ( offer) => handle ( rpc_sender_clone. clone ( ) , offer) . await ,
81
90
}
82
91
}
83
- } )
92
+ } ) ;
93
+
94
+ let rpc_sender_clone = rpc_sender. clone ( ) ;
95
+ let post = warp:: path!( "mina" / "webrtc" / "signal" )
96
+ . and ( warp:: post ( ) )
97
+ . and ( warp:: filters:: body:: json ( ) )
98
+ . then ( move |offer : Box < webrtc:: Offer > | handle ( rpc_sender_clone. clone ( ) , offer) ) ;
99
+ get. or ( post)
84
100
} ;
85
101
86
102
// TODO(binier): make endpoint only accessible locally.
0 commit comments