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
// Copyright 2018 Urs Schulz
//
// This file is part of inwx-rs.
//
// inwx-rs 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.
//
// inwx-rs 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 inwx-rs.  If not, see <http://www.gnu.org/licenses/>.


extern crate xmlrpc;
extern crate reqwest;

use xmlrpc::Request as XMLRequest;
use xmlrpc::Transport;
pub use xmlrpc::Value;

use std::collections::BTreeMap;
use reqwest::header::Cookie;

use std::error::Error;


pub struct Request {
    method: String,
    params: BTreeMap<String, Value>,
}


impl Request {
    pub fn new(method: &str) -> Self {
        Self {
            method: method.to_owned(),
            params: BTreeMap::new(),
        }
    }

    pub fn param(&mut self, name: &str, value: Value) -> Option<Value> {
        self.params.insert(name.to_owned(), value)
    }

    fn build(&self) -> XMLRequest {
        XMLRequest::new(&self.method).arg(Value::Struct(self.params.clone()))
    }
}


#[derive(Debug)]
pub struct LoginInfo {
    pub customer_id: i32,
    pub account_id: i32,
    pub tfa: String,
    pub builddate: String,
    pub version: String,
}


#[derive(Debug)]
pub struct DomainInfo {
    pub ro_id: i32,
    pub domain: String,
    pub typ: String,
    pub slave_dns: Vec<(String, String)>,
    pub records: Vec<DomainRecord>,
}


#[derive(Debug)]
pub struct DomainRecord {
    pub id: i32,
    pub name: String,
    pub typ: String,
    pub content: String,
    pub ttl: i32,
    pub prio: i32,
}


pub struct Domrobot {
    testing: bool,
    cookies: Cookie,
}


#[derive(Debug, PartialEq)]
pub enum RequestError {
    NotLoggedIn,
    LoginFailed,
    SendFailed,
    InvalidResponse,
    CallError(i32, String),
}


impl Domrobot {
    pub fn new(testing: bool) -> Self {
        Self {
            testing: testing,
            cookies: Cookie::new(),
        }
    }
    pub fn url(&self) -> &'static str {
        const TESTING_URL: &'static str = "https://api.ote.domrobot.com/xmlrpc/";
        const URL: &'static str = "https://api.domrobot.com/xmlrpc/";

        match self.testing {
            true => TESTING_URL,
            false => URL,
        }
    }

    pub fn send(&mut self, req: &Request) -> Result<BTreeMap<String, Value>, RequestError> {
        let tp = INWXTransport { robot: self };
        let res = req.build().call(tp);
        let res = res.map_err(|_| RequestError::SendFailed)?;

        const E: RequestError = RequestError::InvalidResponse;

        let res = res.as_struct().ok_or(E)?;
        let code = res.get("code").ok_or(E)?.as_i32().ok_or(E)?;
        let msg = res.get("msg").ok_or(E)?.as_str().ok_or(E)?;

        match code {
            1000 => {
                let data = res.get("resData").ok_or(E)?.as_struct().ok_or(E)?;
                Ok(data.clone())
            }
            1500 => Ok(BTreeMap::new()),
            _ => Err(RequestError::CallError(code, msg.to_string())),
        }
    }

    pub fn account_login(&mut self, user: &str, pass: &str) -> Result<LoginInfo, RequestError> {
        let mut req = Request::new("account.login");
        req.param("user", Value::from(user));
        req.param("pass", Value::from(pass));
        let res = self.send(&req)?;

        // first check, that we now have a domrobot cookie
        if self.cookies.get("domrobot").is_none() {
            return Err(RequestError::LoginFailed);
        }

        // parse info
        const E: RequestError = RequestError::InvalidResponse;
        Ok(LoginInfo {
            customer_id: res.get("customerId").ok_or(E)?.as_i32().ok_or(E)?,
            account_id: res.get("accountId").ok_or(E)?.as_i32().ok_or(E)?,
            tfa: res.get("tfa").ok_or(E)?.as_str().ok_or(E)?.to_owned(),
            builddate: res.get("builddate").ok_or(E)?.as_str().ok_or(E)?.to_owned(),
            version: res.get("version").ok_or(E)?.as_str().ok_or(E)?.to_owned(),
        })
    }

    pub fn account_logout(&mut self) -> Result<(), RequestError> {
        self.send(&Request::new("account.logout"))?;
        Ok(())
    }

    pub fn nameserver_info(&mut self, domain: &str) -> Result<DomainInfo, RequestError> {
        const E: RequestError = RequestError::InvalidResponse;

        let mut req = Request::new("nameserver.info");
        req.param("domain", Value::from(domain));
        let res = self.send(&req)?;

        let domain = res.get("domain").ok_or(E)?.as_str().ok_or(E)?.to_owned();
        let ro_id = res.get("roId").ok_or(E)?.as_i32().ok_or(E)?;
        let typ = res.get("type").ok_or(E)?.as_str().ok_or(E)?.to_owned();

        let mut slave_dns = Vec::new();
        if let Some(arr) = res.get("slaveDns") {
            for entry in arr.as_array().ok_or(E)? {
                let entry = entry.as_struct().ok_or(E)?;
                let name = entry.get("name").ok_or(E)?.as_str().ok_or(E)?.to_owned();
                let ip = entry.get("ip").ok_or(E)?.as_str().ok_or(E)?.to_owned();
                slave_dns.push((name, ip));
            }
        }

        let mut records = Vec::new();
        if let Some(arr) = res.get("record") {
            for record in arr.as_array().ok_or(E)? {
                let record = record.as_struct().ok_or(E)?;
                let id = record.get("id").ok_or(E)?.as_i32().ok_or(E)?;
                let name = record.get("name").ok_or(E)?.as_str().ok_or(E)?.to_owned();
                let typ = record.get("type").ok_or(E)?.as_str().ok_or(E)?.to_owned();
                let content = record
                    .get("content")
                    .ok_or(E)?
                    .as_str()
                    .ok_or(E)?
                    .to_owned();
                let ttl = record.get("ttl").ok_or(E)?.as_i32().ok_or(E)?;
                let prio = record.get("prio").ok_or(E)?.as_i32().ok_or(E)?;
                records.push(DomainRecord {
                    id,
                    name,
                    typ,
                    content,
                    ttl,
                    prio,
                });
            }
        }

        Ok(DomainInfo {
            domain,
            ro_id,
            typ,
            slave_dns,
            records,
        })
    }

    pub fn nameserver_update_record(&mut self, record: &DomainRecord) -> Result<(), RequestError> {
        let mut req = Request::new("nameserver.updateRecord");

        req.param("id", Value::from(record.id));
        req.param("name", Value::from(&*record.name));
        req.param("type", Value::from(&*record.typ));
        req.param("content", Value::from(&*record.content));
        req.param("prio", Value::from(record.prio));
        req.param("ttl", Value::from(record.ttl));

        self.send(&req)?;
        Ok(())
    }
}


struct INWXTransport<'a> {
    robot: &'a mut Domrobot,
}


impl<'a> Transport for INWXTransport<'a> {
    type Stream = reqwest::Response;

    fn transmit(self, request: &XMLRequest) -> Result<Self::Stream, Box<Error + Send + Sync>> {
        use xmlrpc::http::{build_headers, check_response};
        use reqwest::Client;
        use reqwest::header::SetCookie;

        let mut body = Vec::new();
        request.write_as_xml(&mut body).unwrap();

        let mut req = Client::new().post(self.robot.url());
        build_headers(&mut req, body.len() as u64);

        req.header(self.robot.cookies.clone());

        req.body(body);
        let res = req.send()?;
        check_response(&res)?;

        // eval the domrobot cookie
        if let Some(&SetCookie(ref content)) = res.headers().get() {
            let mut cookies = Cookie::new();
            for cookie in content {
                let mut parts = cookie.split(";").nth(0).unwrap().split("=");
                let key = parts.nth(0);
                let value = parts.nth(0);
                match (key, value) {
                    (Some(ref key), Some(ref value)) => {
                        cookies.set(key.to_string(), value.to_string());
                    }
                    _ => {
                        // ignore invalid cookies
                    }
                }
            }

            std::mem::replace(&mut self.robot.cookies, cookies);
        }

        Ok(res)
    }
}