From 96a0862f1ea45bf4cbc58c997e99c759608f6be8 Mon Sep 17 00:00:00 2001 From: Vansh Comar Date: Wed, 20 Oct 2021 17:48:29 +0530 Subject: [PATCH] Implemented the changes requested --- app/routes.py | 51 +++------------------------------- app/static/css/dark-theme.css | 4 +++ app/static/css/light-theme.css | 4 +++ app/static/css/search.css | 8 ++++++ app/utils/results.py | 42 ++++++++++++++++++++++++++++ app/utils/search.py | 17 ++++++++++++ 6 files changed, 79 insertions(+), 47 deletions(-) diff --git a/app/routes.py b/app/routes.py index 8f289bc..955d785 100644 --- a/app/routes.py +++ b/app/routes.py @@ -3,7 +3,6 @@ import base64 import io import json import pickle -import re import urllib.parse as urlparse import uuid from functools import wraps @@ -14,6 +13,7 @@ from app.models.config import Config from app.request import Request, TorError from app.utils.bangs import resolve_bang from app.utils.misc import read_config_bool +from app.utils.results import add_ip from app.utils.search import * from app.utils.session import generate_user_key, valid_user_session from bs4 import BeautifulSoup as bsoup @@ -252,52 +252,9 @@ def search(): resp_code = 503 if has_captcha(str(response)) else 200 # Feature to display IP address - html_soup = bsoup(response, 'html.parser') - if (not html_soup.select_one(".EY24We") - and html_soup.select_one(".OXXup").get_text().lower() == "all"): - if re.search("([^a-z0-9]|^)my *[^a-z0-9] *(ip|internet protocol)" + - "($|( *[^a-z0-9] *(((addres|address|adres|adress)|a)?" + - " *$)))", query.lower()): - # HTML IP card tag - ip_tag = html_soup.new_tag("div") - ip_tag["class"] = "ZINbbc xpd O9g5cc uUPGi" - - # What is my IP tag - q_tag = html_soup.new_tag("div") - q_tag["class"] = "kCrYT" - q_tag.string = "What's my IP" - - # Distinction boundary - line = html_soup.new_tag("div") - line["class"] = "x54gtf" - - # For IP Address html tag - ip_address = html_soup.new_tag("div") - ip_address["class"] = "kCrYT" - ip_address["style"] = "padding-bottom:0;" - ip_address.string = request.remote_addr - - # Text below the IP address - ip_text = html_soup.new_tag("div") - ip_text.string = "Your public IP address" - ip_text["class"] = "kCrYT" - ip_text["style"] = "padding-top: 0; color: #9e9e9e!important;" - - # Adding all the above html tags to the IP card - ip_tag.append(q_tag) - ip_tag.append(line) - ip_tag.append(ip_address) - ip_tag.append(ip_text) - - # Finding the element before which the IP card would be placed - f_link = html_soup.select_one(".BNeawe.vvjwJb.AP7Wnd") - ref_element = f_link.find_parent(class_="ZINbbc xpd O9g5cc" + - " uUPGi") - - # Inserting the element - ref_element.insert_before(ip_tag) - - response = html_soup + if search_util.check_kw_ip(): + html_soup = bsoup(response,"html.parser") + response = add_ip(html_soup, request.remote_addr) return render_template( 'display.html', diff --git a/app/static/css/dark-theme.css b/app/static/css/dark-theme.css index d0a42c8..980da0b 100644 --- a/app/static/css/dark-theme.css +++ b/app/static/css/dark-theme.css @@ -186,3 +186,7 @@ path { .search-bar-desktop { color: var(--whoogle-dark-text) !important; } + +.ip-text-div{ + color: var(--whoogle-dark-secondary-text) !important; +} \ No newline at end of file diff --git a/app/static/css/light-theme.css b/app/static/css/light-theme.css index a083f73..e419621 100644 --- a/app/static/css/light-theme.css +++ b/app/static/css/light-theme.css @@ -174,3 +174,7 @@ path { color: var(--whoogle-text); border-bottom: 0px; } + +.ip-text-div{ + color: var(--whoogle-secondary-text) !important; +} \ No newline at end of file diff --git a/app/static/css/search.css b/app/static/css/search.css index 699d6c7..d5207a0 100644 --- a/app/static/css/search.css +++ b/app/static/css/search.css @@ -31,3 +31,11 @@ details summary { height: 650px; border: 0; } + +.ip-address-div{ + padding-bottom:0!important; +} + +.ip-text-div{ + padding-top:0!important; +} \ No newline at end of file diff --git a/app/utils/results.py b/app/utils/results.py index 60a4f6b..496c3e5 100644 --- a/app/utils/results.py +++ b/app/utils/results.py @@ -138,3 +138,45 @@ def append_nojs(result: BeautifulSoup) -> None: nojs_link.string = 'NoJS Link: ' + nojs_link['href'] result.append(BeautifulSoup('


', 'html.parser')) result.append(nojs_link) + + +def add_ip(html_soup: BeautifulSoup, ip: str) -> BeautifulSoup: + """Adds the client's IP address to the search results if query contains keywords + + Args: + query: Query of the client + response: The search result containing the keywords + ip: ip address of the client + + Returns: + BeautifulSoup + + """ + if (not html_soup.select_one(".EY24We") + and html_soup.select_one(".OXXup").get_text().lower() == "all"): + # HTML IP card tag + ip_tag = html_soup.new_tag("div") + ip_tag["class"] = "ZINbbc xpd O9g5cc uUPGi" + + # For IP Address html tag + ip_address = html_soup.new_tag("div") + ip_address["class"] = "kCrYT ip-address-div" + ip_address.string = ip + + # Text below the IP address + ip_text = html_soup.new_tag("div") + ip_text.string = "Your public IP address" + ip_text["class"] = "kCrYT ip-text-div" + + # Adding all the above html tags to the IP card + ip_tag.append(ip_address) + ip_tag.append(ip_text) + + # Finding the element before which the IP card would be placed + f_link = html_soup.select_one(".BNeawe.vvjwJb.AP7Wnd") + ref_element = f_link.find_parent(class_="ZINbbc xpd O9g5cc" + + " uUPGi") + + # Inserting the element + ref_element.insert_before(ip_tag) + return html_soup \ No newline at end of file diff --git a/app/utils/search.py b/app/utils/search.py index 561ec2f..3edb471 100644 --- a/app/utils/search.py +++ b/app/utils/search.py @@ -1,5 +1,6 @@ import os from typing import Any +import re from bs4 import BeautifulSoup as bsoup from cryptography.fernet import Fernet, InvalidToken @@ -161,3 +162,19 @@ class Search: link['href'] += param_str return str(formatted_results) + + def check_kw_ip(self) -> bool: + """Checks for keywords related to 'my ip' in the query + + Args: + query: Query of the client + + Returns: + bool + + """ + if re.search("([^a-z0-9]|^)my *[^a-z0-9] *(ip|internet protocol)" + + "($|( *[^a-z0-9] *(((addres|address|adres|adress)|a)?" + + " *$)))", self.query.lower()): + return True + return False