summaryrefslogtreecommitdiffstats
path: root/rust
diff options
context:
space:
mode:
Diffstat (limited to 'rust')
-rw-r--r--rust/Cargo.toml2
-rw-r--r--rust/src/main.rs95
2 files changed, 88 insertions, 9 deletions
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 94d10fe..eea8f89 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-bendy = "0.3.3"
+bencode = "0.1.16"
chrono = "0.4.24"
hex = "0.4.3"
sha1 = "0.10.5"
diff --git a/rust/src/main.rs b/rust/src/main.rs
index 64fa16c..adb656c 100644
--- a/rust/src/main.rs
+++ b/rust/src/main.rs
@@ -1,11 +1,17 @@
extern crate hex;
extern crate urlencoding;
+extern crate bencode;
use std::collections::HashMap;
use std::net::IpAddr;
-use chrono::{DateTime, Utc};
+use chrono::{DateTime, Utc, TimeZone};
use sha2::{Sha256, Digest};
use sha1::{Sha1};
+use bencode::{Bencode, FromBencode};
+use bencode::util::ByteString;
+use std::str;
+use std::net::SocketAddr;
+
enum Version {
V1,
V2,
@@ -17,10 +23,25 @@ struct Torrent {
sha256: [u8; 32],
version: Version,
files: HashMap<String, u64>,
- source: IpAddr,
- date: DateTime<Utc>,
- hostname: String,
- software: String
+ source: Option<SocketAddr>,
+ date: Option<DateTime<Utc>>,
+ hostname: Option<String>,
+ software: Option<String>
+}
+impl Default for Torrent {
+ fn default() -> Torrent {
+ Torrent {
+ name: String::from("travnik_default_name_unset"),
+ sha1: [0; 20],
+ sha256: [0; 32],
+ version: Version::V1,
+ files: HashMap::new(),
+ source: None,
+ date: None,
+ hostname: None,
+ software: None
+ }
+ }
}
impl Torrent {
fn magnet(&self) -> String {
@@ -37,8 +58,66 @@ impl Torrent {
string
}
}
+fn sockaddr_from_source(string) -> Result<SockAddra> // TODO nadaljuj
+#[derive(Debug)]
+enum TorrentDecodeError {
+ NotADict,
+ NoName,
+ NoLength,
+ CreatedByNotUtf8,
+ SoftwareNotUtf8
+}
+impl FromBencode for Torrent {
+ type Err = TorrentDecodeError;
+ fn from_bencode(bencode: &Bencode) -> Result<Torrent, TorrentDecodeError> {
+ let metainfo = match bencode {
+ &Bencode::Dict(ref metainfo) => metainfo,
+ _ => return Err(TorrentDecodeError::NotADict)
+ };
+ let torrent = Torrent {
+ date: match metainfo.get(&ByteString::from_str("creation date")) {
+ Some(&Bencode::Number(c)) => Some(Utc.timestamp(c, 0)),
+ _ => None
+ },
+ hostname: match metainfo.get(&ByteString::from_str("created by")) {
+ Some(&Bencode::ByteString(ref s)) => match str::from_utf8(&s) {
+ Ok(v) => Some(match v.split(" ").nth(3) {
+ Some(c) => String::from(c),
+ None => String::from("b")
+ }),
+ Err(_) => return Err(TorrentDecodeError::CreatedByNotUtf8)
+ },
+ _ => None
+ },
+ software: match metainfo.get(&ByteString::from_str("source")) {
+ Some(&Bencode::Dict(ref s)) => match s.get(&ByteString::from_str("v")) {
+ Some(&Bencode::ByteString(ref s)) => match String::from_utf8(s.to_vec()) {
+ Ok(v) => Some(v),
+ Err(_) => return Err(TorrentDecodeError::SoftwareNotUtf8)
+ },
+ _ => None
+ },
+ _ => None
+ },
+ source: match metainfo.get(&ByteString::from_str("source")) {
+ Some(&Bencode::Dict(ref s)) => match s.get(&ByteString::from_str("ip")) {
+ Some(&Bencode::ByteString(ref s)) => match str::from_utf8(&s) {
+ Ok(v) => Some(sockaddr_from_source(v)?), // TODO nadaljuj
+ Err(_) => return Err(Torrent)
+ },
+ _ => None
+ },
+ _ => None
+ },
+ ..Default::default()
+ };
+ Ok(torrent)
+ }
+}
fn main() {
- let hash2 = Sha256::new().chain_update(b"test").finalize();
- let hash1 = Sha1::new().chain_update(b"test").finalize();
- println!("Hello, world! {:#?} {:#?}", hash2, hash1);
+ // let hash2 = Sha256::new().chain_update(b"test").finalize();
+ // let hash1 = Sha1::new().chain_update(b"test").finalize();
+ // println!("Hello, world! {:#?} {:#?}", hash2, hash1);
+ let bencode: bencode::Bencode = bencode::from_vec(String::from("d1:wlee").as_bytes().to_vec()).unwrap();
+ println!("{:#?}", bencode);
}