aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 352315be4124d27a87eaa07481a884ae0208e711 (plain) (blame)
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
use std::collections::HashSet;
use std::future::Future;
use std::net::SocketAddr;
use std::ops::RangeInclusive;
use std::pin::Pin;
use std::process::ExitCode;
use std::sync::Arc;

use clap::Parser;
use http_body_util::Full;
use hyper::body::Bytes;
use hyper::server::conn::http1;
use hyper::service::Service;
use hyper::{Request, Response, StatusCode};
use hyper_util::rt::TokioIo;
use num_bigfloat::BigFloat;
use num_traits::FromPrimitive;
use num_traits::Pow;
use rand::prelude::*;
use rand_pcg::Pcg64;
use rand_seeder::Seeder;
use tokio::net::TcpListener;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::EnvFilter;

/// Endless honeypot for webcrawlers
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
  /// seed for deterministic page generation
  seed: String,
  /// port for serving http traffic
  #[arg(short, long, default_value_t = 3200)]
  port: u16,
  /// minimum delay for responses (in milliseconds)
  #[arg(long, default_value_t = 0)]
  delay_min: u64,
  /// minimum delay for responses (in milliseconds)
  #[arg(long, default_value_t = 0)]
  delay_max: u64,
  /// maximum number of segments in a url path
  #[arg(long, default_value_t = 6)]
  url_segments: u8,
  /// minimum number of words in a paragraph
  #[arg(long, default_value_t = 10)]
  paragraph_min: u16,
  /// maximum number of words in a paragraph
  #[arg(long, default_value_t = 200)]
  paragraph_max: u16,
  /// minimum number of links on a page
  #[arg(long, default_value_t = 3)]
  links_min: u16,
  /// maximum number of links on a page
  #[arg(long, default_value_t = 10)]
  links_max: u16,
  #[arg(long)]
  href_prefix: Option<String>,
}

#[tokio::main]
async fn main() -> Result<ExitCode, Box<dyn std::error::Error + Send + Sync>> {
  let args = Args::parse();

  tracing_subscriber::fmt::fmt()
    .with_env_filter(
      EnvFilter::builder()
        .with_default_directive(LevelFilter::INFO.into())
        .with_env_var("RUST_LOG")
        .from_env()
        .expect("invalid env"),
    )
    .init();

  let generator = PageGenerator::new(&args);

  generator.stats();

  tracing::info!(port = args.port, "starting pitch lake");

  let addr = SocketAddr::from(([127, 0, 0, 1], args.port));

  let listener = TcpListener::bind(addr).await?;

  let svc = RandomPageService {
    ctx: Arc::new(generator),
  };

  loop {
    let (stream, _) = listener.accept().await?;

    let io = TokioIo::new(stream);

    let svc = svc.clone();

    tokio::task::spawn(async move {
      if let Err(err) = http1::Builder::new().serve_connection(io, svc).await {
        eprintln!("Error serving connection: {:?}", err);
      }
    });
  }
}

#[derive(Debug, Clone)]
struct RandomPageService {
  pub ctx: Arc<PageGenerator>,
}

impl Service<Request<hyper::body::Incoming>> for RandomPageService {
  type Response = Response<Full<Bytes>>;
  type Error = hyper::Error;

  type Future =
    Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

  fn call(&self, req: Request<hyper::body::Incoming>) -> Self::Future {
    fn mk_response(
      s: String,
      status_code: StatusCode,
    ) -> Result<Response<Full<Bytes>>, hyper::Error> {
      Ok(
        Response::builder()
          .status(status_code)
          .body(Full::new(Bytes::from(s)))
          .unwrap(),
      )
    }

    let ctx = self.ctx.clone();

    Box::pin(async move {
      match ctx.build_page(req).await {
        Ok(body) => mk_response(body, StatusCode::OK),
        Err((err_message, code)) => mk_response(err_message.to_string(), code),
      }
    })
  }
}

#[derive(Debug)]
struct PageGenerator {
  seed: String,
  segments: u8,
  href_prefix: Option<String>,
  paragraph_size: RangeInclusive<u16>,
  n_links: RangeInclusive<u16>,
  delay: RangeInclusive<u64>,
  dict: Vec<&'static str>,
  dict_set: HashSet<&'static str>,
}

impl PageGenerator {
  fn new(args: &Args) -> Self {
    let dictionary_data = include_bytes!(env!("DICTIONARY_FILE_PATH"));
    let dictionary_string: &'static str =
      std::str::from_utf8(dictionary_data).unwrap();
    PageGenerator {
      seed: args.seed.clone(),
      href_prefix: args.href_prefix.clone(),
      segments: args.url_segments,
      paragraph_size: args.paragraph_min
        ..=(args.paragraph_max.max(args.paragraph_min)),
      n_links: (args.links_min)..=(args.links_max.max(args.links_min)),
      delay: args.delay_min..=(args.delay_max.max(args.delay_min)),
      dict: dictionary_string.split_whitespace().collect(),
      dict_set: dictionary_string.split_whitespace().collect(),
    }
  }

  fn stats(&self) -> () {
    let dict_len = BigFloat::from_usize(self.dict.len()).unwrap();
    let all_segment_lengths = 1..=self.segments;

    let n_pages = all_segment_lengths
      .map(|n_segments| dict_len.pow(BigFloat::from_u8(n_segments)))
      .sum::<BigFloat>();

    let avg_paragraph_words =
      BigFloat::from_u16(self.paragraph_size.clone().sum::<u16>())
        / BigFloat::from_usize(self.paragraph_size.clone().count()).unwrap();

    let avg_word_bytes = self
      .dict
      .iter()
      .map(|word| word.as_bytes().len())
      .map(|l| BigFloat::from_usize(l).unwrap())
      .sum::<BigFloat>()
      / dict_len;

    let avg_page_bytes = avg_paragraph_words * avg_word_bytes;

    let total_size_bytes = n_pages * avg_page_bytes;
    let bf1024 = BigFloat::from_u32(1024);
    let total_size_petabytes =
      total_size_bytes / bf1024 / bf1024 / bf1024 / bf1024 / bf1024;

    tracing::info!(
      n_pages = n_pages.to_string(),
      size_gb = format_size(total_size_petabytes)
    );
  }

  async fn build_page(
    &self,
    req: Request<hyper::body::Incoming>,
  ) -> Result<String, (&'static str, StatusCode)> {
    let delay = if *self.delay.start() > 0 {
      let delay_amount = rand::thread_rng().gen_range(self.delay.clone());
      tokio::time::sleep(tokio::time::Duration::from_millis(delay_amount))
        .await;
      Some(delay_amount)
    } else {
      None
    };

    let route = req.uri().path();

    for (i, segment) in route.split('/').filter(|v| v.len() > 0).enumerate() {
      if !self.dict_set.contains(segment) {
        return Err(("not found", StatusCode::NOT_FOUND));
      }
      tracing::debug!(i, segment);
      if i >= self.segments as usize {
        return Err(("not found", StatusCode::NOT_FOUND));
      }
    }

    let page_title = route.replace('/', " ").trim().to_string();

    let mut rng: Pcg64 =
      Seeder::from(format!("{}---{}", self.seed, route)).make_rng();
    let n_words = rng.gen_range(self.paragraph_size.clone());
    let n_links = rng.gen_range(self.n_links.clone());

    let random_paragraph = (0..n_words)
      .map(|_| self.random_word(&mut rng))
      .collect::<Vec<&str>>()
      .join(" ");

    let random_links = (0..n_links)
      .map(|_| self.random_route_link(&mut rng))
      .map(|link| format!("<p>{}</p>", link))
      .collect::<Vec<String>>()
      .join("");

    tracing::info!(route, delay);

    Ok(format!(
      "<!DOCTYPE html>\
      <html>\
        <head>\
          <title>{page_title}</title>\
          <meta property=\"og:title\" content=\"{page_title}\" />\
          <meta property=\"og:type\" content=\"article\" />\
          <meta property=\"og:locale\" content=\"en_US\" />\
        </head>\
        <body>\
          <p>{random_paragraph}</p>\
          {random_links}\
        </body>\
      </html>",
    ))
  }
  fn random_route_link(&self, rng: &mut Pcg64) -> String {
    let n_segments = rng.gen_range(1..=self.segments);
    let random_route = (0..n_segments)
      .map(|_| self.random_word(rng))
      .collect::<Vec<&str>>()
      .join("/");
    let label = self.random_word(rng);
    format!("<a href=\"/{}\">{}</a>", random_route, label)
  }

  pub fn random_word(&self, rng: &mut Pcg64) -> &'static str {
    let i = rng.gen_range(0..self.dict.len());
    self.dict[i]
  }
}

pub fn build_dict() -> Vec<&'static str> {
  let dictionary_data = include_bytes!(env!("DICTIONARY_FILE_PATH"));
  let dictionary_string: &'static str =
    std::str::from_utf8(dictionary_data).unwrap();
  dictionary_string.split_whitespace().collect()
}

pub fn format_size(mut bytes: BigFloat) -> String {
  let exponents: &[&str] = &[
    "kilobytes",
    "megabytes",
    "gigabytes",
    "terabytes",
    "petabytes",
    "exabytes",
    "zettabytes",
    "yottabytes",
    "ronnabytes",
    "quettabytes",
  ];

  let fv1024 = BigFloat::from_u32(1024);
  let mut current_label = "bytes".to_string();

  for label in exponents {
    let exp_val = bytes.log10().to_f32();
    if exp_val < 2.0 {
      break;
    }

    bytes = bytes / fv1024;
    current_label = label.to_string();
  }

  format!("{:.2} {}", bytes, current_label)
}