session.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. use connection::Connection;
  2. use cryptoutil::Crypto;
  3. use protocol;
  4. use util;
  5. use std::iter::{FromIterator,repeat};
  6. use protobuf::*;
  7. use rand::thread_rng;
  8. pub struct Session {
  9. connection: Connection,
  10. crypto: Crypto,
  11. }
  12. impl Session {
  13. pub fn new() -> Session {
  14. Session {
  15. connection: Connection::connect(),
  16. crypto: Crypto::new(),
  17. }
  18. }
  19. pub fn login(&mut self) {
  20. let request = protobuf_init!(protocol::keyexchange::Request::new(), {
  21. data0 => {
  22. data0: 0x05,
  23. data1: 0x01,
  24. data2: 0x10800000000,
  25. },
  26. data1: 0,
  27. data2.data0 => {
  28. data0: self.crypto.public_key(),
  29. data1: 1,
  30. },
  31. random: util::rand_vec(&mut thread_rng(), 0x10),
  32. data4: vec![0x1e],
  33. data5: vec![0x08, 0x01]
  34. });
  35. let init_client_packet =
  36. self.connection.send_packet_prefix(&[0,4], &request.write_to_bytes().unwrap());
  37. let init_server_packet =
  38. self.connection.recv_packet();
  39. let response : protocol::keyexchange::Response =
  40. parse_from_bytes(&init_server_packet).unwrap();
  41. protobuf_bind!(response, { data.data0.data0.data0: remote_key });
  42. self.crypto.setup(&remote_key, &init_client_packet, &init_server_packet);
  43. return;
  44. let appkey = vec![];
  45. let request = protobuf_init!(protocol::authentication::AuthRequest::new(), {
  46. credentials => {
  47. username: "USERNAME".to_string(),
  48. method: protocol::authentication::AuthRequest_LoginMethod::PASSWORD,
  49. password: b"PASSWORD".to_vec(),
  50. },
  51. data1 => {
  52. data0: 0,
  53. data1: 0,
  54. partner: "Partner blabla".to_string(),
  55. deviceid: "abc".to_string()
  56. },
  57. version: "master-v1.8.0-gad9e5b46".to_string(),
  58. data3 => {
  59. data0: 1,
  60. appkey1: appkey[0x1..0x81].to_vec(),
  61. appkey2: appkey[0x81..0x141].to_vec(),
  62. data3: "".to_string(),
  63. data4: Vec::from_iter(repeat(0).take(20))
  64. }
  65. });
  66. //println!("{:?}", response);
  67. }
  68. }