1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// SPDX-Copyright: Copyright (c) 2019 Daniel Edgecumbe (esotericnonsense)
// SPDX-License-Identifier: AGPL-3.0-only
//
// This file is part of botfair.  botfair is free software: you can
// redistribute it and/or modify it under the terms of the GNU Affero General
// Public License as published by the Free Software Foundation, either version
// 3 of the License, or (at your option) any later version.
//
// botfair is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License
// for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with botfair.  If not, see <http://www.gnu.org/licenses/>.

use crate::json_rpc::{RpcRequest, RpcResponse};
use crate::result::{Error, Result};
use reqwest::{Client, Identity};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::sync::{mpsc, Arc, RwLock};
use std::thread;
use std::time::Duration;

#[derive(Debug, Serialize)]
struct LoginRequestForm {
    username: String,
    password: String,
}

#[derive(Debug, Deserialize)]
#[allow(non_snake_case)]
struct LoginResponse {
    sessionToken: Option<String>,
    loginStatus: String, // TODO enum this
}

/// A container for the essential credentials required for the Betfair APING.
pub struct BFCredentials {
    username: String,
    password: String,
    pfx: Vec<u8>,
    app_key: String,
}

impl BFCredentials {
    pub fn new(
        username: String,
        password: String,
        pfx_path: String,
        app_key: String,
    ) -> Result<Self> {
        let pfx = std::fs::read(pfx_path)?;
        Ok(BFCredentials {
            username,
            password,
            pfx,
            app_key,
        })
    }
    fn as_login_request_form(&self) -> LoginRequestForm {
        LoginRequestForm {
            username: self.username.clone(),
            password: self.password.clone(),
        }
    }
    fn pfx(&self) -> &Vec<u8> {
        &self.pfx
    }
    fn app_key(&self) -> &String {
        &self.app_key
    }
}

/// A thread-safe client with automatic login implementing all methods of the
/// Betfair APING.
pub struct BFClient {
    client: reqwest::Client,
    destructor: mpsc::SyncSender<()>,
    session_token: Arc<RwLock<Option<String>>>,
    creds: BFCredentials,
    proxy_uri: Option<String>,
}

impl Drop for BFClient {
    fn drop(&mut self) {
        trace!("client: destructor triggered; signalling keepalive thread");
        self.destructor
            .send(())
            .expect("unable to signal keepalive thread");
    }
}

impl BFClient {
    pub fn new(
        creds: BFCredentials,
        proxy_uri: Option<String>,
    ) -> Result<Self> {
        let client: reqwest::Client = match &proxy_uri {
            Some(uri) => {
                let proxy = reqwest::Proxy::all(uri)?;
                Client::builder().proxy(proxy).build()?
            }
            None => reqwest::Client::new(),
        };

        let session_token = Arc::new(RwLock::new(None));

        let destructor = {
            let session_token = session_token.clone();
            let proxy_uri = proxy_uri.clone();
            let (tx, rx) = mpsc::sync_channel(0); // rendezvous channel
            thread::spawn(|| {
                Self::keepalive_thread(session_token, proxy_uri, rx)
            });
            tx
        };

        Ok(BFClient {
            client,
            destructor,
            session_token,
            creds,
            proxy_uri,
        })
    }

    /// This function is run once per BFClient as a thread. It ensures that the
    /// correct keepalive requests are made to the Betfair API such that the
    /// token does not expire.
    ///
    /// Note that it does not automatically re-login on expiry; for that to
    /// occur, a request must explicitly be made.
    ///
    /// In the future this could be implemented, which would reduce the latency
    /// of the first call after a (very) long spell of nothing, the so-called
    /// 'cold start problem'.
    fn keepalive_thread(
        session_token: Arc<RwLock<Option<String>>>,
        proxy_uri: Option<String>,
        rx: mpsc::Receiver<()>,
    ) {
        trace!("keepalive: thread spawned");
        let mut expired_token: Option<String> = None;
        loop {
            match rx.recv_timeout(Duration::from_millis(60000)) {
                Ok(_) => {
                    warn!("keepalive: destructor signal caught, exiting");
                    break;
                }
                Err(_) => {
                    let maybe_token: Option<String> = session_token
                        .read()
                        .expect("keepalive: could not lock session token")
                        .clone();

                    if maybe_token.is_some() && maybe_token == expired_token {
                        // TODO: login instead
                        warn!("keepalive: skipping, as token is expired");
                    }

                    match maybe_token {
                        None => {
                            debug!("keepalive: skipping, as no token");
                        }
                        Some(token) => {
                            debug!("keepalive: attempting");
                            match keepalive(&token, &proxy_uri) {
                                Ok(()) => {
                                    debug!("keepalive: successful");
                                }
                                Err(e) => {
                                    info!("keepalive failed: {:?}", e);
                                    // TODO: login instead
                                    expired_token = Some(token);
                                }
                            };
                        }
                    }
                }
            };
        }
    }

    fn req_internal<T1: Serialize, T2: DeserializeOwned>(
        &self,
        maybe_token: &Option<String>,
        rpc_request: &RpcRequest<T1>,
    ) -> Result<RpcResponse<T2>> {
        match maybe_token {
            None => Err(Error::General(
                "req_internal: must login first".to_owned(),
            )),
            Some(token) => {
                const JSONRPC_URI: &str =
                    "https://api.betfair.com/exchange/betting/json-rpc/v1";

                trace!("Performing a query to the JSON-RPC api");

                Ok(self
                    .client
                    .post(JSONRPC_URI)
                    .header("X-Application", self.creds.app_key())
                    .header("X-Authentication", token)
                    .json(&rpc_request)
                    .send()?
                    .json()
                    .unwrap())
            }
        }
    }

    pub(super) fn req<T1: Serialize, T2: DeserializeOwned>(
        &self,
        req: RpcRequest<T1>,
    ) -> Result<RpcResponse<T2>> {
        // Initially acquire the token via a read lock

        trace!("req: taking token read lock");
        let token_lock = self.session_token.read().unwrap();
        let mut token = token_lock.clone();
        drop(token_lock);
        trace!("req: dropped token read lock");

        loop {
            debug!("req: attempting request");
            match self.req_internal(&token, &req) {
                Ok(resp) => {
                    debug!("req: request successful");
                    break Ok(resp);
                }
                Err(_) => {
                    // Assume the only error possible is an auth error
                    // TODO: check if it's an exception, auth error,
                    // etc; an exception should just be propagated to the
                    // caller

                    info!("req: login required");
                    trace!("req: taking token write lock");
                    let mut token_lock = self.session_token.write().unwrap();

                    if token != *token_lock {
                        // Another thread has already performed the login.
                        token = token_lock.clone();
                        continue;
                    }

                    token = loop {
                        debug!("login: sending request");
                        match self.login() {
                            Ok(token) => {
                                info!("login: success");
                                break Some(token);
                            }
                            Err(e) => {
                                warn!("login: failed {:?}", e);

                                // TODO: exponential backoff
                                debug!("login: sleeping for 5000ms");
                                thread::sleep(Duration::from_millis(5000));
                            }
                        }
                    };

                    *token_lock = token.clone();
                    drop(token_lock); // explicit drop for logging purposes
                    trace!("req: dropped token write lock");
                }
            }
        }
    }

    fn login(&self) -> Result<String> {
        const CERTLOGIN_URI: &str =
            "https://identitysso-cert.betfair.com/api/certlogin";

        let ident =
            Identity::from_pkcs12_der(self.creds.pfx().as_slice(), "")?;

        let client: reqwest::Client = match &(self.proxy_uri) {
            Some(uri) => {
                let proxy = reqwest::Proxy::all(uri)?;
                Client::builder().identity(ident).proxy(proxy).build()?
            }
            None => Client::builder().identity(ident).build()?,
        };

        let login_request_form = self.creds.as_login_request_form();

        let login_response: LoginResponse = client
            .post(CERTLOGIN_URI)
            .header(
                "X-Application",
                format!("schroedinger_{}", rand::random::<u128>()),
            )
            .form(&login_request_form)
            .send()?
            .json()?;
        match login_response.sessionToken {
            Some(token) => Ok(token),
            None => Err(Error::BFLoginFailure(format!(
                "loginStatus: {}",
                login_response.loginStatus
            ))),
        }
    }
}

fn keepalive(token: &String, proxy_uri: &Option<String>) -> Result<()> {
    const KEEPALIVE_URI: &str =
        "https://identitysso.betfair.com/api/keepAlive";

    let client: Client = match proxy_uri {
        Some(uri) => {
            let proxy = reqwest::Proxy::all(uri)?;
            Client::builder().proxy(proxy).build()?
        }
        None => Client::new(),
    };

    let keep_alive_response: KeepAliveResponse = client
        .get(KEEPALIVE_URI)
        .header("Accept", "application/json")
        .header(
            "X-Application",
            format!("schroedinger_{}", rand::random::<u128>()),
        )
        .header("X-Authentication", token)
        .send()?
        .json()?;

    match keep_alive_response.status {
        KeepAliveStatus::SUCCESS => Ok(()),
        KeepAliveStatus::FAIL => Err(Error::BFKeepAliveFailure(
            keep_alive_response.error.unwrap(),
        )),
    }
}

#[derive(Debug, Deserialize)]
#[allow(non_camel_case_types)]
pub enum KeepAliveError {
    // TODO should this really be public?
    #[serde(rename = "")]
    NONE,
    INPUT_VALIDATION_ERROR,
    INTERNAL_ERROR,
    NO_SESSION,
}

#[derive(Debug, Deserialize)]
#[allow(non_camel_case_types)]
enum KeepAliveStatus {
    SUCCESS,
    FAIL,
}

#[derive(Debug, Deserialize)]
#[allow(non_snake_case)]
struct KeepAliveResponse {
    token: String,
    product: String,
    status: KeepAliveStatus,
    error: Option<KeepAliveError>,
}