File size: 6,756 Bytes
b36163c
 
 
 
cea95e3
b36163c
 
 
 
 
 
 
 
 
 
 
 
 
 
cea95e3
 
b36163c
 
 
 
b37cd5f
 
 
318784c
b37cd5f
b36163c
b37cd5f
 
 
 
 
 
 
 
 
 
 
 
 
 
b36163c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cea95e3
 
b36163c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cea95e3
 
 
 
 
 
 
 
 
 
 
 
b36163c
 
 
 
 
 
 
 
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
from flask import Flask, request, Response
import requests
import logging
from urllib.parse import urljoin
from urllib.parse import quote
from bs4 import BeautifulSoup
import re

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = Flask(__name__)

# Target URL base
TARGET_BASE_URL = "https://superetka.com"

@app.route('/proxy_image')
def proxy_image():
    from urllib.parse import unquote
    image_url = unquote(request.args.get('url'))
    if not image_url:
        return 'No URL provided', 400
    
    try:
        # Get headers from the incoming request
        headers = {key: value for key, value in request.headers if key.lower() != 'host'}
        headers['Referer'] = TARGET_BASE_URL
        headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36'
        
        # Forward the request to get the image
        resp = requests.get(
            image_url,
            headers=headers,
            cookies=request.cookies,
            timeout=30,
            allow_redirects=True
        )
        
        # Check response status
        if resp.status_code != 200:
            logger.error(f"Error response from target: {resp.status_code}")
            return f"Error: {resp.status_code}", resp.status_code
            
        return Response(resp.content, mimetype=resp.headers.get('Content-Type', 'image/jpeg'))
    except Exception as e:
        logger.error(f"Error proxying image: {str(e)}")
        return str(e), 500

@app.route('/', defaults={'path': ''}, methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'])
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'])
def proxy(path):
    # Construct target URL with path and query parameters
    if path and path.strip():
        # If path is provided, use it
        url = f"{TARGET_BASE_URL}/{path}"
    else:
        # Default to wap.php if no path is provided
        url = f"{TARGET_BASE_URL}/etka/wap.php"
    
    # If there are query parameters, append them to the URL
    if request.query_string:
        url = f"{url}?{request.query_string.decode('utf-8')}"
    
    # Log the constructed URL
    logger.info(f"Constructed URL: {url}")
    
    # Log the request
    logger.info(f"Received request: {request.method} {request.url}")
    logger.info(f"Forwarding to: {url}")
    
    # Get headers from the incoming request
    headers = {key: value for key, value in request.headers if key.lower() != 'host'}
    
    try:
        # Forward the request to the target server
        # Don't pass params separately as they're already in the URL
        resp = requests.request(
            method=request.method,
            url=url,
            headers=headers,
            data=request.get_data(),
            cookies=request.cookies,
            allow_redirects=False,
            timeout=30
        )
        
        # Log the response
        logger.info(f"Received response from target: {resp.status_code}")
        
        # Check if response is HTML and filter content if needed
        content_type = resp.headers.get('Content-Type', '')
        if 'text/html' in content_type:
            # Parse HTML content
            html_content = resp.content.decode('utf-8', errors='ignore')
            soup = BeautifulSoup(html_content, 'html.parser')
            
            # Filter out "Полная версия ETKA"
            for element in soup.find_all(string=re.compile('Полная версия ETKA')):
                # Replace the text with empty string
                element.replace_with('')
            
            # Filter out README content
            for element in soup.find_all(string=re.compile('README', re.IGNORECASE)):
                element.replace_with('')
                
            # Redirect part number links to Google search
            # Look for links that contain part numbers (typically in the second column of the table)
            part_number_links = soup.select('td:nth-child(2) a')
            for link in part_number_links:
                # Get the part number from the link text
                part_number = link.text.strip()
                # Check if it matches a part number pattern (alphanumeric with possible spaces)
                if re.match(r'^[A-Z0-9 ]+$', part_number):
                    # Create a Google search URL for this part number with avto.pro
                    google_search_url = f"https://www.google.com/search?q={part_number} avto.pro"
                    # Update the link's href attribute
                    link['href'] = google_search_url
            
            # Replace images with buttons
            for img in soup.find_all('img'):
                # Create button element
                button = soup.new_tag('button')
                img_src = img['src']
                if not img_src.startswith('http'):
                    img_src = urljoin(TARGET_BASE_URL, img_src)
                from urllib.parse import quote
                proxy_url = f"/proxy_image?url={quote(img_src)}"
                button['onclick'] = f'window.open("{proxy_url}", "_blank")'
                button['style'] = 'margin: 5px;'
                button.string = 'Показать изображение'
                # Replace image with button
                img.replace_with(button)

            # Create a Flask response object with filtered content
            response = Response(
                soup.encode(),
                status=resp.status_code
            )
        else:
            # Create a Flask response object with original content
            response = Response(
                resp.content,
                status=resp.status_code
            )
        
        # Copy headers from the target response
        for key, value in resp.headers.items():
            if key.lower() not in ('transfer-encoding', 'content-encoding', 'content-length'):
                response.headers[key] = value
                
        # Copy cookies from target response
        for cookie in resp.cookies:
            response.set_cookie(
                key=cookie.name,
                value=cookie.value,
                # domain=cookie.domain,
                path=cookie.path,
                expires=cookie.expires,
                secure=cookie.secure,
                httponly=cookie.httponly
            )
        
        return response
        
    except requests.RequestException as e:
        logger.error(f"Error forwarding request: {str(e)}")
        return Response(f"Error forwarding request: {str(e)}", status=500)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)