Skip to main content

shinden_pl_api/
headers.rs

1use anyhow::Result;
2use reqwest::header::{
3    HeaderMap, HeaderName, HeaderValue,
4    ACCEPT, ACCEPT_ENCODING, ACCEPT_LANGUAGE, CACHE_CONTROL,
5    CONNECTION, CONTENT_TYPE, REFERER,
6    UPGRADE_INSECURE_REQUESTS, USER_AGENT,
7};
8
9static SEC_FETCH_DEST: HeaderName = HeaderName::from_static("sec-fetch-dest");
10static SEC_FETCH_MODE: HeaderName = HeaderName::from_static("sec-fetch-mode");
11static SEC_FETCH_SITE: HeaderName = HeaderName::from_static("sec-fetch-site");
12static SEC_FETCH_USER: HeaderName = HeaderName::from_static("sec-fetch-user");
13static SEC_GPC: HeaderName = HeaderName::from_static("sec-gpc");
14
15#[derive(Debug, PartialEq, Eq, Hash)]
16pub enum RequestType {
17    Frontend,
18    Login,
19    Api,
20}
21
22pub fn get_headers_for_type(req_type: RequestType, url_context: Option<&str>) -> Result<HeaderMap> {
23    let mut headers = HeaderMap::new();
24
25    headers.insert(USER_AGENT, HeaderValue::from_static("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"));
26    headers.insert(ACCEPT_LANGUAGE, HeaderValue::from_static("pl-PL,pl;q=0.9,en-US;q=0.8,en;q=0.7"));
27    headers.insert(CONNECTION, HeaderValue::from_static("keep-alive"));
28    headers.insert(ACCEPT_ENCODING, HeaderValue::from_static("gzip, deflate, br"));
29
30    match req_type {
31        RequestType::Frontend => {
32            headers.insert(ACCEPT, HeaderValue::from_static("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"));
33            headers.insert(CACHE_CONTROL, HeaderValue::from_static("max-age=0"));
34            headers.insert(SEC_FETCH_DEST.clone(), HeaderValue::from_static("document"));
35            headers.insert(SEC_FETCH_MODE.clone(), HeaderValue::from_static("navigate"));
36            headers.insert(SEC_FETCH_SITE.clone(), HeaderValue::from_static("same-origin"));
37            headers.insert(SEC_FETCH_USER.clone(), HeaderValue::from_static("?1"));
38            headers.insert(SEC_GPC.clone(), HeaderValue::from_static("1"));
39            headers.insert(UPGRADE_INSECURE_REQUESTS, HeaderValue::from_static("1"));
40
41            if let Some(url) = url_context {
42                if url.starts_with("https://shinden.pl/") {
43                    headers.insert(REFERER, HeaderValue::from_str(url)?);
44                } else {
45                    headers.insert(REFERER, HeaderValue::from_static("https://shinden.pl/"));
46                }
47            } else {
48                headers.insert(REFERER, HeaderValue::from_static("https://shinden.pl/"));
49            }
50        }
51        RequestType::Login => {
52            headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/x-www-form-urlencoded"));
53            headers.insert(ACCEPT, HeaderValue::from_static("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"));
54            headers.insert(REFERER, HeaderValue::from_static("https://shinden.pl/main/login"));
55            headers.insert(SEC_FETCH_SITE.clone(), HeaderValue::from_static("same-origin"));
56            headers.insert(SEC_FETCH_MODE.clone(), HeaderValue::from_static("navigate"));
57            headers.insert(SEC_FETCH_DEST.clone(), HeaderValue::from_static("document"));
58            headers.insert(UPGRADE_INSECURE_REQUESTS, HeaderValue::from_static("1"));
59        }
60        RequestType::Api => {
61            headers.insert(ACCEPT, HeaderValue::from_static("application/json, text/plain, */*"));
62            headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
63            if let Some(url) = url_context {
64                if url.starts_with("https://shinden.pl/") {
65                    headers.insert(REFERER, HeaderValue::from_str(url)?);
66                }
67            }
68        }
69    }
70
71    Ok(headers)
72}