1use std::collections::BTreeMap;
17
18use serde_json::{json, Map, Value};
19
20use crate::error::{Error, Result};
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
24pub enum ProxyType {
25 #[default]
26 Http,
27 Socks4,
28 Socks5,
29}
30
31impl ProxyType {
32 fn as_str(self) -> &'static str {
33 match self {
34 ProxyType::Http => "http",
35 ProxyType::Socks4 => "socks4",
36 ProxyType::Socks5 => "socks5",
37 }
38 }
39}
40
41#[derive(Debug, Clone, Default)]
47pub struct Proxy {
48 pub proxy_type: ProxyType,
49 pub address: String,
50 pub port: u16,
51 pub login: String,
53 pub password: String,
55}
56
57impl Proxy {
58 pub fn new(proxy_type: ProxyType, address: impl Into<String>, port: u16) -> Self {
60 Self {
61 proxy_type,
62 address: address.into(),
63 port,
64 ..Default::default()
65 }
66 }
67
68 pub fn with_auth(mut self, login: impl Into<String>, password: impl Into<String>) -> Self {
70 self.login = login.into();
71 self.password = password.into();
72 self
73 }
74}
75
76#[derive(Debug, Clone, Default)]
78pub struct ImageSettings {
79 pub phrase: bool,
81 pub case_sensitive: bool,
83 pub numeric: u8,
85 pub math_operation: bool,
87 pub min_length: u32,
89 pub max_length: u32,
91 pub language_pool: String,
93 pub comment: String,
95 pub website_url: String,
97}
98
99#[derive(Debug, Clone, Default)]
101pub struct ImageToCoordinates {
102 pub mode: String,
104 pub comment: String,
106 pub website_url: String,
108}
109
110#[derive(Debug, Clone, Default)]
112pub struct RecaptchaV2 {
113 pub website_url: String,
114 pub website_key: String,
116 pub website_s_token: String,
117 pub is_invisible: bool,
119 pub data_s_value: String,
121 pub user_agent: String,
123 pub is_enterprise: bool,
125 pub enterprise_payload: BTreeMap<String, String>,
127 pub api_domain: String,
129 pub proxy: Proxy,
130}
131
132#[derive(Debug, Clone)]
136pub struct RecaptchaV3 {
137 pub website_url: String,
138 pub website_key: String,
139 pub min_score: f64,
141 pub page_action: String,
142 pub is_enterprise: bool,
143 pub api_domain: String,
144}
145
146impl Default for RecaptchaV3 {
147 fn default() -> Self {
148 Self {
149 website_url: String::new(),
150 website_key: String::new(),
151 min_score: 0.3,
152 page_action: String::new(),
153 is_enterprise: false,
154 api_domain: String::new(),
155 }
156 }
157}
158
159#[derive(Debug, Clone, Default)]
161pub struct HCaptcha {
162 pub website_url: String,
163 pub website_key: String,
164 pub is_invisible: bool,
165 pub is_enterprise: bool,
166 pub enterprise_payload: BTreeMap<String, String>,
168 pub user_agent: String,
170 pub cookies: String,
172 pub proxy: Proxy,
173}
174
175#[derive(Debug, Clone, Default)]
177pub struct FunCaptcha {
178 pub website_url: String,
179 pub website_public_key: String,
181 pub api_subdomain: String,
183 pub data_blob: String,
185 pub user_agent: String,
186 pub proxy: Proxy,
187}
188
189#[derive(Debug, Clone)]
191pub struct GeeTest {
192 pub website_url: String,
193 pub gt: String,
195 pub challenge: String,
197 pub api_subdomain: String,
198 pub version: u8,
200 pub init_parameters: Map<String, Value>,
202 pub user_agent: String,
203 pub proxy: Proxy,
204}
205
206impl Default for GeeTest {
207 fn default() -> Self {
208 Self {
209 website_url: String::new(),
210 gt: String::new(),
211 challenge: String::new(),
212 api_subdomain: String::new(),
213 version: 3,
214 init_parameters: Map::new(),
215 user_agent: String::new(),
216 proxy: Proxy::default(),
217 }
218 }
219}
220
221#[derive(Debug, Clone, Default)]
223pub struct Turnstile {
224 pub website_url: String,
225 pub website_key: String,
226 pub action: String,
227 pub c_data: String,
228 pub chl_page_data: String,
229 pub user_agent: String,
230 pub proxy: Proxy,
231}
232
233#[derive(Debug, Clone, Default)]
235pub struct Prosopo {
236 pub website_url: String,
237 pub website_key: String,
238 pub user_agent: String,
239 pub proxy: Proxy,
240}
241
242#[derive(Debug, Clone, Default)]
244pub struct FriendlyCaptcha {
245 pub website_url: String,
246 pub website_key: String,
247 pub user_agent: String,
248 pub proxy: Proxy,
249}
250
251#[derive(Debug, Clone, Default)]
253pub struct Altcha {
254 pub website_url: String,
255 pub challenge_url: String,
257 pub challenge_json: String,
259 pub user_agent: String,
260 pub proxy: Proxy,
261}
262
263#[derive(Debug, Clone, Default)]
265pub struct Amazon {
266 pub website_url: String,
267 pub website_key: String,
269 pub iv: String,
270 pub context: String,
271 pub captcha_script: String,
272 pub challenge_script: String,
273 pub jsapi_script: String,
275 pub waf_type: String,
277 pub user_agent: String,
278 pub proxy: Proxy,
279}
280
281#[derive(Debug, Clone, Default)]
283pub struct AntiGate {
284 pub website_url: String,
285 pub template_name: String,
287 pub variables: Map<String, Value>,
288 pub domains_of_interest: Vec<String>,
289 pub use_proxy: bool,
291 pub proxy: Proxy,
292}
293
294#[derive(Debug, Clone, Default)]
296pub struct AntiBotCookie {
297 pub website_url: String,
298 pub proxy: Proxy,
300}
301
302fn set_if_not_empty(task: &mut Map<String, Value>, name: &str, value: &str) {
305 if !value.is_empty() {
306 task.insert(name.to_owned(), json!(value));
307 }
308}
309
310fn set_payload(task: &mut Map<String, Value>, name: &str, payload: &BTreeMap<String, String>) {
311 if !payload.is_empty() {
312 task.insert(name.to_owned(), json!(payload));
313 }
314}
315
316fn object(pairs: Vec<(&str, Value)>) -> Map<String, Value> {
317 pairs
318 .into_iter()
319 .map(|(name, value)| (name.to_owned(), value))
320 .collect()
321}
322
323fn add_proxy(task: &mut Map<String, Value>, proxy: &Proxy) -> Result<()> {
325 if proxy.address.is_empty() {
326 return Err(Error::InvalidTask(
327 "proxy address is empty, it is required for a proxy-on task".into(),
328 ));
329 }
330
331 if proxy.port == 0 {
332 return Err(Error::InvalidTask("proxy port is not set".into()));
333 }
334
335 task.insert("proxyType".into(), json!(proxy.proxy_type.as_str()));
336 task.insert("proxyAddress".into(), json!(proxy.address));
337 task.insert("proxyPort".into(), json!(proxy.port));
338
339 if !proxy.login.is_empty() {
340 task.insert("proxyLogin".into(), json!(proxy.login));
341 task.insert("proxyPassword".into(), json!(proxy.password));
342 }
343
344 Ok(())
345}
346
347pub(crate) fn build_image(body: &str, settings: &ImageSettings) -> Result<Value> {
348 if body.is_empty() {
349 return Err(Error::InvalidTask("captcha image is empty".into()));
350 }
351
352 let language_pool = if settings.language_pool.is_empty() {
353 "en"
354 } else {
355 &settings.language_pool
356 };
357
358 let mut task = object(vec![
359 ("type", json!("ImageToTextTask")),
360 ("body", json!(body)),
361 ("phrase", json!(settings.phrase)),
362 ("case", json!(settings.case_sensitive)),
363 ("numeric", json!(settings.numeric)),
364 ("math", json!(settings.math_operation)),
365 ("minLength", json!(settings.min_length)),
366 ("maxLength", json!(settings.max_length)),
367 ("languagePool", json!(language_pool)),
368 ]);
369
370 set_if_not_empty(&mut task, "comment", &settings.comment);
371 set_if_not_empty(&mut task, "websiteURL", &settings.website_url);
372
373 Ok(Value::Object(task))
374}
375
376pub(crate) fn build_image_to_coordinates(
377 body: &str,
378 settings: &ImageToCoordinates,
379) -> Result<Value> {
380 if body.is_empty() {
381 return Err(Error::InvalidTask("captcha image is empty".into()));
382 }
383
384 let mode = if settings.mode.is_empty() {
385 "points"
386 } else {
387 &settings.mode
388 };
389
390 let mut task = object(vec![
391 ("type", json!("ImageToCoordinatesTask")),
392 ("body", json!(body)),
393 ("mode", json!(mode)),
394 ]);
395
396 set_if_not_empty(&mut task, "comment", &settings.comment);
397 set_if_not_empty(&mut task, "websiteURL", &settings.website_url);
398
399 Ok(Value::Object(task))
400}
401
402pub(crate) fn build_recaptcha_v2(settings: &RecaptchaV2, proxy_on: bool) -> Result<Value> {
403 let task_type = match (settings.is_enterprise, proxy_on) {
404 (true, true) => "RecaptchaV2EnterpriseTask",
405 (true, false) => "RecaptchaV2EnterpriseTaskProxyless",
406 (false, true) => "RecaptchaV2Task",
407 (false, false) => "RecaptchaV2TaskProxyless",
408 };
409
410 let mut task = object(vec![
411 ("type", json!(task_type)),
412 ("websiteURL", json!(settings.website_url)),
413 ("websiteKey", json!(settings.website_key)),
414 ]);
415
416 if !settings.is_enterprise || settings.is_invisible {
418 task.insert("isInvisible".into(), json!(settings.is_invisible));
419 }
420
421 set_if_not_empty(&mut task, "websiteSToken", &settings.website_s_token);
422 set_if_not_empty(&mut task, "recaptchaDataSValue", &settings.data_s_value);
423 set_if_not_empty(&mut task, "apiDomain", &settings.api_domain);
424 set_payload(&mut task, "enterprisePayload", &settings.enterprise_payload);
425
426 if proxy_on {
427 add_proxy(&mut task, &settings.proxy)?;
428 set_if_not_empty(&mut task, "userAgent", &settings.user_agent);
429 }
430
431 Ok(Value::Object(task))
432}
433
434pub(crate) fn build_recaptcha_v3(settings: &RecaptchaV3) -> Result<Value> {
435 if ![0.3, 0.7, 0.9].contains(&settings.min_score) {
436 return Err(Error::InvalidTask(format!(
437 "min_score must be one of 0.3, 0.7, 0.9; got {}",
438 settings.min_score
439 )));
440 }
441
442 let mut task = object(vec![
443 ("type", json!("RecaptchaV3TaskProxyless")),
444 ("websiteURL", json!(settings.website_url)),
445 ("websiteKey", json!(settings.website_key)),
446 ("minScore", json!(settings.min_score)),
447 ("isEnterprise", json!(settings.is_enterprise)),
448 ]);
449
450 set_if_not_empty(&mut task, "pageAction", &settings.page_action);
451 set_if_not_empty(&mut task, "apiDomain", &settings.api_domain);
452
453 Ok(Value::Object(task))
454}
455
456pub(crate) fn build_hcaptcha(settings: &HCaptcha, proxy_on: bool) -> Result<Value> {
457 let mut task = object(vec![
458 (
459 "type",
460 json!(if proxy_on {
461 "HCaptchaTask"
462 } else {
463 "HCaptchaTaskProxyless"
464 }),
465 ),
466 ("websiteURL", json!(settings.website_url)),
467 ("websiteKey", json!(settings.website_key)),
468 ("isInvisible", json!(settings.is_invisible)),
469 ("isEnterprise", json!(settings.is_enterprise)),
470 ]);
471
472 set_payload(&mut task, "enterprisePayload", &settings.enterprise_payload);
473
474 if proxy_on {
475 add_proxy(&mut task, &settings.proxy)?;
476 set_if_not_empty(&mut task, "userAgent", &settings.user_agent);
477 set_if_not_empty(&mut task, "cookies", &settings.cookies);
478 }
479
480 Ok(Value::Object(task))
481}
482
483pub(crate) fn build_funcaptcha(settings: &FunCaptcha, proxy_on: bool) -> Result<Value> {
484 let mut task = object(vec![
485 (
486 "type",
487 json!(if proxy_on {
488 "FunCaptchaTask"
489 } else {
490 "FunCaptchaTaskProxyless"
491 }),
492 ),
493 ("websiteURL", json!(settings.website_url)),
494 ("websitePublicKey", json!(settings.website_public_key)),
495 ]);
496
497 set_if_not_empty(
498 &mut task,
499 "funcaptchaApiJSSubdomain",
500 &settings.api_subdomain,
501 );
502 set_if_not_empty(&mut task, "data", &settings.data_blob);
503
504 if proxy_on {
505 add_proxy(&mut task, &settings.proxy)?;
506 set_if_not_empty(&mut task, "userAgent", &settings.user_agent);
507 }
508
509 Ok(Value::Object(task))
510}
511
512pub(crate) fn build_geetest(settings: &GeeTest, proxy_on: bool) -> Result<Value> {
513 if settings.version != 3 && settings.version != 4 {
514 return Err(Error::InvalidTask(format!(
515 "GeeTest version must be 3 or 4, got {}",
516 settings.version
517 )));
518 }
519
520 if settings.version == 3 && settings.challenge.is_empty() {
521 return Err(Error::InvalidTask(
522 "GeeTest v3 requires a challenge value".into(),
523 ));
524 }
525
526 let mut task = object(vec![
527 (
528 "type",
529 json!(if proxy_on {
530 "GeeTestTask"
531 } else {
532 "GeeTestTaskProxyless"
533 }),
534 ),
535 ("websiteURL", json!(settings.website_url)),
536 ("gt", json!(settings.gt)),
537 ("version", json!(settings.version)),
538 ]);
539
540 set_if_not_empty(&mut task, "challenge", &settings.challenge);
541 set_if_not_empty(
542 &mut task,
543 "geetestApiServerSubdomain",
544 &settings.api_subdomain,
545 );
546
547 if !settings.init_parameters.is_empty() {
548 task.insert(
549 "initParameters".into(),
550 Value::Object(settings.init_parameters.clone()),
551 );
552 }
553
554 if proxy_on {
555 add_proxy(&mut task, &settings.proxy)?;
556 set_if_not_empty(&mut task, "userAgent", &settings.user_agent);
557 }
558
559 Ok(Value::Object(task))
560}
561
562pub(crate) fn build_turnstile(settings: &Turnstile, proxy_on: bool) -> Result<Value> {
563 let mut task = object(vec![
564 (
565 "type",
566 json!(if proxy_on {
567 "TurnstileTask"
568 } else {
569 "TurnstileTaskProxyless"
570 }),
571 ),
572 ("websiteURL", json!(settings.website_url)),
573 ("websiteKey", json!(settings.website_key)),
574 ]);
575
576 set_if_not_empty(&mut task, "action", &settings.action);
577 set_if_not_empty(&mut task, "cData", &settings.c_data);
578 set_if_not_empty(&mut task, "chlPageData", &settings.chl_page_data);
579
580 if proxy_on {
581 add_proxy(&mut task, &settings.proxy)?;
582 set_if_not_empty(&mut task, "userAgent", &settings.user_agent);
583 }
584
585 Ok(Value::Object(task))
586}
587
588fn build_sitekey_task(
590 task_type: &str,
591 website_url: &str,
592 website_key: &str,
593 user_agent: &str,
594 proxy: Option<&Proxy>,
595) -> Result<Value> {
596 let mut task = object(vec![
597 ("type", json!(task_type)),
598 ("websiteURL", json!(website_url)),
599 ("websiteKey", json!(website_key)),
600 ]);
601
602 if let Some(proxy) = proxy {
603 add_proxy(&mut task, proxy)?;
604 set_if_not_empty(&mut task, "userAgent", user_agent);
605 }
606
607 Ok(Value::Object(task))
608}
609
610pub(crate) fn build_prosopo(settings: &Prosopo, proxy_on: bool) -> Result<Value> {
611 build_sitekey_task(
612 if proxy_on {
613 "ProsopoTask"
614 } else {
615 "ProsopoTaskProxyless"
616 },
617 &settings.website_url,
618 &settings.website_key,
619 &settings.user_agent,
620 proxy_on.then_some(&settings.proxy),
621 )
622}
623
624pub(crate) fn build_friendly_captcha(settings: &FriendlyCaptcha, proxy_on: bool) -> Result<Value> {
625 build_sitekey_task(
626 if proxy_on {
627 "FriendlyCaptchaTask"
628 } else {
629 "FriendlyCaptchaTaskProxyless"
630 },
631 &settings.website_url,
632 &settings.website_key,
633 &settings.user_agent,
634 proxy_on.then_some(&settings.proxy),
635 )
636}
637
638pub(crate) fn build_altcha(settings: &Altcha, proxy_on: bool) -> Result<Value> {
639 if settings.challenge_url.is_empty() && settings.challenge_json.is_empty() {
640 return Err(Error::InvalidTask(
641 "set either challenge_url or challenge_json".into(),
642 ));
643 }
644
645 let mut task = object(vec![
646 (
647 "type",
648 json!(if proxy_on {
649 "AltchaTask"
650 } else {
651 "AltchaTaskProxyless"
652 }),
653 ),
654 ("websiteURL", json!(settings.website_url)),
655 ]);
656
657 set_if_not_empty(&mut task, "challengeURL", &settings.challenge_url);
658 set_if_not_empty(&mut task, "challengeJSON", &settings.challenge_json);
659
660 if proxy_on {
661 add_proxy(&mut task, &settings.proxy)?;
662 set_if_not_empty(&mut task, "userAgent", &settings.user_agent);
663 }
664
665 Ok(Value::Object(task))
666}
667
668pub(crate) fn build_amazon(settings: &Amazon, proxy_on: bool) -> Result<Value> {
669 let mut task = object(vec![
670 (
671 "type",
672 json!(if proxy_on {
673 "AmazonTask"
674 } else {
675 "AmazonTaskProxyless"
676 }),
677 ),
678 ("websiteURL", json!(settings.website_url)),
679 ("websiteKey", json!(settings.website_key)),
680 ]);
681
682 set_if_not_empty(&mut task, "wafType", &settings.waf_type);
683 set_if_not_empty(&mut task, "iv", &settings.iv);
684 set_if_not_empty(&mut task, "context", &settings.context);
685 set_if_not_empty(&mut task, "captchaScript", &settings.captcha_script);
686 set_if_not_empty(&mut task, "challengeScript", &settings.challenge_script);
687 set_if_not_empty(&mut task, "jsapiScript", &settings.jsapi_script);
688
689 if proxy_on {
690 add_proxy(&mut task, &settings.proxy)?;
691 set_if_not_empty(&mut task, "userAgent", &settings.user_agent);
692 }
693
694 Ok(Value::Object(task))
695}
696
697pub(crate) fn build_antigate(settings: &AntiGate) -> Result<Value> {
698 let mut task = object(vec![
699 ("type", json!("AntiGateTask")),
700 ("websiteURL", json!(settings.website_url)),
701 ("templateName", json!(settings.template_name)),
702 ]);
703
704 if !settings.variables.is_empty() {
705 task.insert(
706 "variables".into(),
707 Value::Object(settings.variables.clone()),
708 );
709 }
710
711 if !settings.domains_of_interest.is_empty() {
712 task.insert(
713 "domainsOfInterest".into(),
714 json!(settings.domains_of_interest),
715 );
716 }
717
718 if settings.use_proxy {
719 add_proxy(&mut task, &settings.proxy)?;
720 }
721
722 Ok(Value::Object(task))
723}
724
725pub(crate) fn build_antibot_cookie(settings: &AntiBotCookie) -> Result<Value> {
726 let mut task = object(vec![
727 ("type", json!("AntiBotCookieTask")),
728 ("websiteURL", json!(settings.website_url)),
729 ]);
730
731 add_proxy(&mut task, &settings.proxy)?;
732
733 task.remove("proxyType");
735
736 Ok(Value::Object(task))
737}
738
739#[cfg(test)]
740mod tests {
741 use super::*;
742
743 fn proxy() -> Proxy {
744 Proxy::new(ProxyType::Socks5, "1.2.3.4", 1234).with_auth("login", "password")
745 }
746
747 #[test]
748 fn image_task() {
749 let settings = ImageSettings {
750 comment: "type in green".into(),
751 numeric: 1,
752 ..Default::default()
753 };
754
755 assert_eq!(
756 build_image("QUFB", &settings).unwrap(),
757 json!({
758 "type": "ImageToTextTask",
759 "body": "QUFB",
760 "phrase": false,
761 "case": false,
762 "numeric": 1,
763 "math": false,
764 "minLength": 0,
765 "maxLength": 0,
766 "languagePool": "en",
767 "comment": "type in green"
768 })
769 );
770
771 assert!(build_image("", &ImageSettings::default()).is_err());
772 }
773
774 #[test]
775 fn image_to_coordinates_defaults_to_points() {
776 let task = build_image_to_coordinates("QUFB", &ImageToCoordinates::default()).unwrap();
777
778 assert_eq!(task["mode"], "points");
779 assert_eq!(task["type"], "ImageToCoordinatesTask");
780 }
781
782 #[test]
783 fn recaptcha_v2_proxyless() {
784 let settings = RecaptchaV2 {
785 website_url: "https://website.com/".into(),
786 website_key: "KEY".into(),
787 is_invisible: true,
788 ..Default::default()
789 };
790
791 assert_eq!(
792 build_recaptcha_v2(&settings, false).unwrap(),
793 json!({
794 "type": "RecaptchaV2TaskProxyless",
795 "websiteURL": "https://website.com/",
796 "websiteKey": "KEY",
797 "isInvisible": true
798 })
799 );
800 }
801
802 #[test]
803 fn recaptcha_v2_proxy_on() {
804 let settings = RecaptchaV2 {
805 website_url: "https://website.com/".into(),
806 website_key: "KEY".into(),
807 user_agent: "UA".into(),
808 proxy: proxy(),
809 ..Default::default()
810 };
811
812 assert_eq!(
813 build_recaptcha_v2(&settings, true).unwrap(),
814 json!({
815 "type": "RecaptchaV2Task",
816 "websiteURL": "https://website.com/",
817 "websiteKey": "KEY",
818 "isInvisible": false,
819 "proxyType": "socks5",
820 "proxyAddress": "1.2.3.4",
821 "proxyPort": 1234,
822 "proxyLogin": "login",
823 "proxyPassword": "password",
824 "userAgent": "UA"
825 })
826 );
827 }
828
829 #[test]
830 fn recaptcha_v2_enterprise_switches_the_type() {
831 let mut payload = BTreeMap::new();
832 payload.insert("s".to_owned(), "TOKEN".to_owned());
833
834 let settings = RecaptchaV2 {
835 website_url: "https://website.com/".into(),
836 website_key: "KEY".into(),
837 is_enterprise: true,
838 enterprise_payload: payload,
839 api_domain: "recaptcha.net".into(),
840 ..Default::default()
841 };
842
843 let task = build_recaptcha_v2(&settings, false).unwrap();
844
845 assert_eq!(task["type"], "RecaptchaV2EnterpriseTaskProxyless");
846 assert_eq!(task["enterprisePayload"]["s"], "TOKEN");
847 assert_eq!(task["apiDomain"], "recaptcha.net");
848 assert!(task.get("isInvisible").is_none());
849
850 let invisible = RecaptchaV2 {
851 is_invisible: true,
852 proxy: proxy(),
853 ..settings
854 };
855 let task = build_recaptcha_v2(&invisible, true).unwrap();
856
857 assert_eq!(task["type"], "RecaptchaV2EnterpriseTask");
858 assert_eq!(task["isInvisible"], true);
859 }
860
861 #[test]
862 fn recaptcha_v3_checks_the_score() {
863 let settings = RecaptchaV3 {
864 website_url: "https://website.com/".into(),
865 website_key: "KEY".into(),
866 page_action: "login".into(),
867 min_score: 0.7,
868 ..Default::default()
869 };
870
871 let task = build_recaptcha_v3(&settings).unwrap();
872 assert_eq!(task["minScore"], 0.7);
873 assert_eq!(task["pageAction"], "login");
874
875 let broken = RecaptchaV3 {
876 min_score: 0.55,
877 ..settings
878 };
879 assert!(build_recaptcha_v3(&broken).is_err());
880 }
881
882 #[test]
883 fn hcaptcha_sends_lowercase_is_enterprise() {
884 let settings = HCaptcha {
885 website_url: "https://website.com/".into(),
886 website_key: "KEY".into(),
887 is_enterprise: true,
888 ..Default::default()
889 };
890
891 let task = build_hcaptcha(&settings, false).unwrap();
892
893 assert_eq!(task["isEnterprise"], true);
894 assert!(task.get("IsEnterprise").is_none());
895 }
896
897 #[test]
898 fn funcaptcha_keeps_the_blob_a_string() {
899 let settings = FunCaptcha {
900 website_url: "https://website.com/".into(),
901 website_public_key: "KEY".into(),
902 api_subdomain: "x.arkoselabs.com".into(),
903 data_blob: r#"{"blob":"B"}"#.into(),
904 proxy: Proxy::new(ProxyType::Http, "1.2.3.4", 8080),
905 ..Default::default()
906 };
907
908 let task = build_funcaptcha(&settings, true).unwrap();
909
910 assert_eq!(task["type"], "FunCaptchaTask");
911 assert_eq!(task["data"], r#"{"blob":"B"}"#);
912 assert_eq!(task["funcaptchaApiJSSubdomain"], "x.arkoselabs.com");
913 assert!(task.get("proxyLogin").is_none());
915 }
916
917 #[test]
918 fn geetest_versions() {
919 let v3 = GeeTest {
920 website_url: "https://website.com/".into(),
921 gt: "GT".into(),
922 challenge: "CHALLENGE".into(),
923 ..Default::default()
924 };
925 let task = build_geetest(&v3, false).unwrap();
926 assert_eq!(task["version"], 3);
927 assert_eq!(task["challenge"], "CHALLENGE");
928
929 let mut init = Map::new();
930 init.insert("riskType".into(), json!("slide"));
931
932 let v4 = GeeTest {
933 website_url: "https://website.com/".into(),
934 gt: "GT".into(),
935 version: 4,
936 init_parameters: init,
937 proxy: Proxy::new(ProxyType::Http, "1.2.3.4", 8080),
938 ..Default::default()
939 };
940 let task = build_geetest(&v4, true).unwrap();
941 assert_eq!(task["type"], "GeeTestTask");
942 assert_eq!(task["version"], 4);
943 assert_eq!(task["initParameters"]["riskType"], "slide");
944 assert!(task.get("challenge").is_none());
945
946 let no_challenge = GeeTest {
947 gt: "GT".into(),
948 ..Default::default()
949 };
950 assert!(build_geetest(&no_challenge, false).is_err());
951
952 let bad_version = GeeTest {
953 gt: "GT".into(),
954 version: 5,
955 ..Default::default()
956 };
957 assert!(build_geetest(&bad_version, false).is_err());
958 }
959
960 #[test]
961 fn turnstile_and_sitekey_tasks() {
962 let turnstile = Turnstile {
963 website_url: "https://website.com/".into(),
964 website_key: "0x4".into(),
965 action: "login".into(),
966 ..Default::default()
967 };
968
969 assert_eq!(
970 build_turnstile(&turnstile, false).unwrap(),
971 json!({
972 "type": "TurnstileTaskProxyless",
973 "websiteURL": "https://website.com/",
974 "websiteKey": "0x4",
975 "action": "login"
976 })
977 );
978
979 let prosopo = Prosopo {
980 website_url: "https://website.com/".into(),
981 website_key: "KEY".into(),
982 proxy: proxy(),
983 ..Default::default()
984 };
985 assert_eq!(
986 build_prosopo(&prosopo, false).unwrap()["type"],
987 "ProsopoTaskProxyless"
988 );
989 assert_eq!(
990 build_prosopo(&prosopo, true).unwrap()["type"],
991 "ProsopoTask"
992 );
993
994 let friendly = FriendlyCaptcha {
995 website_url: "https://website.com/".into(),
996 website_key: "KEY".into(),
997 ..Default::default()
998 };
999 assert_eq!(
1000 build_friendly_captcha(&friendly, false).unwrap()["type"],
1001 "FriendlyCaptchaTaskProxyless"
1002 );
1003 }
1004
1005 #[test]
1006 fn altcha_needs_a_challenge() {
1007 let settings = Altcha {
1008 website_url: "https://website.com/".into(),
1009 challenge_url: "/challenge".into(),
1010 ..Default::default()
1011 };
1012
1013 assert_eq!(
1014 build_altcha(&settings, false).unwrap(),
1015 json!({
1016 "type": "AltchaTaskProxyless",
1017 "websiteURL": "https://website.com/",
1018 "challengeURL": "/challenge"
1019 })
1020 );
1021
1022 let empty = Altcha {
1023 website_url: "https://website.com/".into(),
1024 ..Default::default()
1025 };
1026 assert!(build_altcha(&empty, false).is_err());
1027 }
1028
1029 #[test]
1030 fn amazon_widget() {
1031 let settings = Amazon {
1032 website_url: "https://website.com/".into(),
1033 website_key: "KEY".into(),
1034 waf_type: "widget".into(),
1035 jsapi_script: "https://x/jsapi.js".into(),
1036 ..Default::default()
1037 };
1038
1039 assert_eq!(
1040 build_amazon(&settings, false).unwrap(),
1041 json!({
1042 "type": "AmazonTaskProxyless",
1043 "websiteURL": "https://website.com/",
1044 "websiteKey": "KEY",
1045 "wafType": "widget",
1046 "jsapiScript": "https://x/jsapi.js"
1047 })
1048 );
1049 }
1050
1051 #[test]
1052 fn antigate_proxy_is_optional() {
1053 let mut variables = Map::new();
1054 variables.insert("login".into(), json!("value"));
1055
1056 let settings = AntiGate {
1057 website_url: "http://website.com/".into(),
1058 template_name: "Template".into(),
1059 variables,
1060 domains_of_interest: vec!["example.com".into()],
1061 use_proxy: true,
1062 proxy: Proxy::new(ProxyType::Http, "1.2.3.4", 1234),
1063 };
1064
1065 let task = build_antigate(&settings).unwrap();
1066 assert_eq!(task["variables"]["login"], "value");
1067 assert_eq!(task["domainsOfInterest"], json!(["example.com"]));
1068 assert_eq!(task["proxyType"], "http");
1069
1070 let without = AntiGate {
1071 use_proxy: false,
1072 ..settings
1073 };
1074 assert!(build_antigate(&without)
1075 .unwrap()
1076 .get("proxyAddress")
1077 .is_none());
1078 }
1079
1080 #[test]
1081 fn antibot_cookie_sends_no_proxy_type() {
1082 let settings = AntiBotCookie {
1083 website_url: "https://website.com/".into(),
1084 proxy: Proxy::new(ProxyType::Http, "1.2.3.4", 3128).with_auth("login", "password"),
1085 };
1086
1087 assert_eq!(
1088 build_antibot_cookie(&settings).unwrap(),
1089 json!({
1090 "type": "AntiBotCookieTask",
1091 "websiteURL": "https://website.com/",
1092 "proxyAddress": "1.2.3.4",
1093 "proxyPort": 3128,
1094 "proxyLogin": "login",
1095 "proxyPassword": "password"
1096 })
1097 );
1098 }
1099
1100 #[test]
1101 fn proxy_is_validated() {
1102 let no_address = RecaptchaV2 {
1103 website_url: "https://website.com/".into(),
1104 website_key: "KEY".into(),
1105 ..Default::default()
1106 };
1107 assert!(build_recaptcha_v2(&no_address, true).is_err());
1108
1109 let no_port = RecaptchaV2 {
1110 proxy: Proxy::new(ProxyType::Http, "1.2.3.4", 0),
1111 ..no_address.clone()
1112 };
1113 assert!(build_recaptcha_v2(&no_port, true).is_err());
1114
1115 let good = RecaptchaV2 {
1116 proxy: Proxy::new(ProxyType::Http, "1.2.3.4", 8080),
1117 ..no_address
1118 };
1119 assert!(build_recaptcha_v2(&good, true).is_ok());
1120 }
1121}