Skip to content

Commit 9a2c1aa

Browse files
author
Serhiy Barhamon
authored
Merge pull request #4 from Lurk/evil-mode
Evil mode
2 parents 94fb83d + adc887e commit 9a2c1aa

File tree

6 files changed

+135
-24
lines changed

6 files changed

+135
-24
lines changed

Cargo.lock

Lines changed: 65 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "clink"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
authors = ["Sergey Bargamon <sergey@bargamon.ru>"]
55
edition = "2018"
66

@@ -10,4 +10,5 @@ edition = "2018"
1010
clipboard = "0.5.0"
1111
linkify = "0.4.0"
1212
url = "2.2.0"
13-
rustop = "1.1.1"
13+
rustop = "1.1.1"
14+
rand = "0.8.3"

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ sets values of fbclid, utm_source, utm_campaign and utm_medium GET params to "yo
2020

2121
inspired by this [tweet](https://twitter.com/ftrain/status/1359138516681314311?s=21)
2222

23+
### Evil mode
24+
```
25+
clink -m evil
26+
```
27+
swap two random chars in values of fbclid, utm_source, utm_campaign and utm_medium GET params in links that are in clipboard (Diabolical Laughter)
2328

2429
## Build
2530

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ fn main() {
3131
if previous_clipboard != current_clipboard {
3232
let cleaned = find_and_replace(&current_clipboard, &args.mode);
3333
if cleaned != current_clipboard {
34-
ctx.set_contents(cleaned).unwrap();
34+
ctx.set_contents(cleaned.clone()).unwrap();
3535
}
36-
previous_clipboard = current_clipboard;
36+
previous_clipboard = cleaned;
3737
}
3838
}
3939
Err(_e) => {}
4040
}
41-
thread::sleep(Duration::from_millis(100))
41+
thread::sleep(Duration::from_millis(150))
4242
}
4343
}

src/mode.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::str::FromStr;
66
pub enum Mode {
77
Remove,
88
YourMom,
9+
Evil,
910
}
1011

1112
#[derive(Debug)]
@@ -39,7 +40,10 @@ impl FromStr for Mode {
3940
match day {
4041
"remove" => Ok(Mode::Remove),
4142
"your_mom" => Ok(Mode::YourMom),
42-
_ => Err(ModeError::new("Mode can be \"remove\" or \"your_mom\"")),
43+
"evil" => Ok(Mode::Evil),
44+
_ => Err(ModeError::new(
45+
"Mode can be \"remove\", \"your_mom\" or \"evil\"",
46+
)),
4347
}
4448
}
4549
}
@@ -49,6 +53,7 @@ impl fmt::Display for Mode {
4953
match *self {
5054
Mode::Remove => write!(f, "Remove"),
5155
Mode::YourMom => write!(f, "YourMom"),
56+
Mode::Evil => write!(f, "Evil"),
5257
}
5358
}
5459
}

src/utils.rs

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::mode::Mode;
22
use linkify::{LinkFinder, LinkKind};
3+
use rand::Rng;
34
use url::Url;
45

56
#[cfg(test)]
@@ -10,60 +11,60 @@ mod find_and_replace {
1011
fn naive() {
1112
assert_eq!(
1213
find_and_replace(
13-
&"https://test.test/?fbclid=dsadsa&utm_source=fafa&utm_campaign=fafas&utm_medium=adsa".to_string(),
14+
"https://test.test/?fbclid=dsadsa&utm_source=fafa&utm_campaign=fafas&utm_medium=adsa",
1415
&Mode::Remove
1516
),
1617
"https://test.test/"
1718
);
1819
assert_eq!(
1920
find_and_replace(
20-
&"https://test.test/?fbclid=dsadsa&utm_source=fafa&utm_campaign=fafas&utm_medium=adsa".to_string(),
21+
"https://test.test/?fbclid=dsadsa&utm_source=fafa&utm_campaign=fafas&utm_medium=adsa",
2122
&Mode::YourMom
2223
),
2324
"https://test.test/?fbclid=your_mom&utm_source=your_mom&utm_campaign=your_mom&utm_medium=your_mom"
2425
);
26+
assert_ne!(
27+
find_and_replace(
28+
"https://test.test/?fbclid=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_source=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_campaign=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_medium=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs",
29+
&Mode::Evil
30+
),
31+
"https://test.test/?fbclid=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_source=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_campaign=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_medium=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs"
32+
);
2533
}
2634
#[test]
2735
fn should_preserve_query() {
2836
assert_eq!(
29-
find_and_replace(&"https://test.test/?abc=abc".to_string(), &Mode::Remove),
37+
find_and_replace("https://test.test/?abc=abc", &Mode::Remove),
3038
"https://test.test/?abc=abc"
3139
);
3240
assert_eq!(
33-
find_and_replace(&"https://test.test/?abc=abc".to_string(), &Mode::YourMom),
41+
find_and_replace("https://test.test/?abc=abc", &Mode::YourMom),
3442
"https://test.test/?abc=abc"
3543
);
3644
}
3745
#[test]
3846
fn multiple_params() {
3947
assert_eq!(
40-
find_and_replace(
41-
&"https://test.test/?abc=abc&fbclid=flksj".to_string(),
42-
&Mode::Remove
43-
),
48+
find_and_replace("https://test.test/?abc=abc&fbclid=flksj", &Mode::Remove),
4449
"https://test.test/?abc=abc"
4550
);
4651
assert_eq!(
47-
find_and_replace(
48-
&"https://test.test/?abc=abc&fbclid=flksj".to_string(),
49-
&Mode::YourMom
50-
),
52+
find_and_replace("https://test.test/?abc=abc&fbclid=flksj", &Mode::YourMom),
5153
"https://test.test/?abc=abc&fbclid=your_mom"
5254
);
5355
}
5456
#[test]
5557
fn multiple_links() {
5658
assert_eq!(
5759
find_and_replace(
58-
&"https://test.test/?abc=abc&fbclid=flksj\nhttps://test.test/?abc=abc&fbclid=flksj"
59-
.to_string(),
60+
"https://test.test/?abc=abc&fbclid=flksj\nhttps://test.test/?abc=abc&fbclid=flksj",
6061
&Mode::Remove
6162
),
6263
"https://test.test/?abc=abc\nhttps://test.test/?abc=abc"
6364
);
6465
assert_eq!(
6566
find_and_replace(
66-
&"https://test.test/?abc=abc&fbclid=flksj\nhttps://test.test/?abc=abc&fbclid=flksj".to_string(),
67+
"https://test.test/?abc=abc&fbclid=flksj\nhttps://test.test/?abc=abc&fbclid=flksj",
6768
&Mode::YourMom
6869
),
6970
"https://test.test/?abc=abc&fbclid=your_mom\nhttps://test.test/?abc=abc&fbclid=your_mom"
@@ -73,14 +74,14 @@ mod find_and_replace {
7374
fn multiple_links_and_text() {
7475
assert_eq!(
7576
find_and_replace(
76-
&"some text here https://test.test/?abc=abc&fbclid=flksj here \nand herehttps://test.test/?abc=abc&fbclid=flksj".to_string(),
77+
"some text here https://test.test/?abc=abc&fbclid=flksj here \nand herehttps://test.test/?abc=abc&fbclid=flksj",
7778
&Mode::Remove
7879
),
7980
"some text here https://test.test/?abc=abc here \nand herehttps://test.test/?abc=abc"
8081
);
8182
assert_eq!(
8283
find_and_replace(
83-
&"some text here https://test.test/?abc=abc&fbclid=flksj here \nand herehttps://test.test/?abc=abc&fbclid=flksj".to_string(),
84+
"some text here https://test.test/?abc=abc&fbclid=flksj here \nand herehttps://test.test/?abc=abc&fbclid=flksj",
8485
&Mode::YourMom
8586
),
8687
"some text here https://test.test/?abc=abc&fbclid=your_mom here \nand herehttps://test.test/?abc=abc&fbclid=your_mom"
@@ -126,6 +127,25 @@ fn process_query(query: url::form_urlencoded::Parse<'_>, mode: &Mode) -> Vec<(St
126127
}
127128
})
128129
.collect(),
130+
Mode::Evil => {
131+
let mut rng = rand::thread_rng();
132+
query
133+
.map(|p| {
134+
if is_hit(&p.0) {
135+
(
136+
p.0.to_string(),
137+
swap_two_chars(
138+
&p.1,
139+
rng.gen_range(0..p.1.to_string().len()),
140+
rng.gen_range(0..p.1.to_string().len()),
141+
),
142+
)
143+
} else {
144+
(p.0.to_string(), p.1.to_string())
145+
}
146+
})
147+
.collect()
148+
}
129149
}
130150
}
131151

@@ -144,3 +164,19 @@ mod is_hit {
144164
fn is_hit(p: &str) -> bool {
145165
p == "fbclid" || p == "utm_source" || p == "utm_campaign" || p == "utm_medium"
146166
}
167+
168+
#[cfg(test)]
169+
mod swap {
170+
use super::*;
171+
172+
#[test]
173+
fn test_all() {
174+
assert_eq!(swap_two_chars("0123456789", 2, 7), "0173456289");
175+
}
176+
}
177+
178+
fn swap_two_chars(s: &str, a: usize, b: usize) -> String {
179+
let mut char_vector: Vec<char> = s.chars().collect();
180+
char_vector.swap(a, b);
181+
char_vector.iter().collect()
182+
}

0 commit comments

Comments
 (0)