luxysiv / Cloudflare-Gateway-Pihole

Make ad blocking dns using Cloudflare Gateway Zero Trust
80 stars 125 forks source link

Asyncio migration ? #2

Closed nhubaotruong closed 1 year ago

nhubaotruong commented 1 year ago

Hi bạn,

Bên bản fork của mình vừa migrate sang dùng asyncio để chạy requests song song thay vì tuần tự. Giảm thời gian execute script từ hơn 5 phút xuống tầm 15 giây.

Pipeline gần đây: https://github.com/nhubaotruong/Cloudflare-Gateway-Pihole/actions/runs/6112962003

Ưu điểm là chạy nhanh

Nhược điểm là sẽ khó maintain hơn về sau.

Bạn có muốn mình làm cái PR migrate asynio lên upstream không ?

Thanks.

luxysiv commented 1 year ago

Quá hay bạn nhưng từ sau sẽ do bạn bảo trì còn mình đúng là ngoài tầm với. Nếu bạn tâm huyết thì cứ upstream. Nó có liên quan tới phần convert_to_domain_list không? Tớ đang suy nghĩ xem làm cách nào ổn thoả nhất

nhubaotruong commented 1 year ago

Không liên quan tới phần convert_to_domain_list nha bạn, mình chỉ parallel cho http call (phần chậm nhất của cái script này) thôi

luxysiv commented 1 year ago

Ok bạn ơi, bạn có phương pháp convert nào đỉnh không. Mình mới nghĩ ra 1 phương pháp

nhubaotruong commented 1 year ago

hiện mình dùng regex để lấy domains từ 2 format là domainhost domain thôi à, cũng chưa có idea nào khác

luxysiv commented 1 year ago

Cái mình đang sử dụng là nhiều hơn. Nó là của repository kia convert qua

nhubaotruong commented 1 year ago

Với mình domain là đủ rồi nên mình ko can thiệp, tối mình làm cái PR asyncio

luxysiv commented 1 year ago

Ok thanks bạn nhiều nha

luxysiv commented 1 year ago

Tốc độ quá nhanh @nhubaotruong

luxysiv commented 1 year ago

Nãy bị lỗi download whitelist kiểu này

# https://raw.githubusercontent.com/anudeepND/whitelist/master/domains/optional-list.txt
# https://raw.githubusercontent.com/AdguardTeam/HttpsExclusions/master/exclusions/sensitive.txt
https://raw.githubusercontent.com/DandelionSprout/AdGuard-Home-Whitelist/master/whitelist.txt

mà tớ fix rồi

nhubaotruong commented 1 year ago

thanks bạn, tối nay đi chơi ko có máy để fix :((

luxysiv commented 1 year ago

thanks bạn, tối nay đi chơi ko có máy để fix :((

Không sao bạn ơi. Phải cảm ơn bạn rất nhiều vì tâm huyết

luxysiv commented 1 year ago

@nhubaotruong bạn có code nào tầm soát lỗi không? Có một ông fork mà code cứ up báo lỗi

luxysiv commented 1 year ago

Whitelists như sau

https://raw.githubusercontent.com/im-sm/Pi-hole-Torrent-Blocklist/main/all-torrent-trackres.txt
https://raw.githubusercontent.com/AdguardTeam/HttpsExclusions/master/exclusions/banks.txt
https://raw.githubusercontent.com/hagezi/dns-blocklists/main/whitelist.txt
https://raw.githubusercontent.com/TogoFire-Home/AD-Settings/main/Filters/whitelist.txt
https://raw.githubusercontent.com/freekers/whitelist/master/domains/whitelist.txt
https://raw.githubusercontent.com/DandelionSprout/AdGuard-Home-Whitelist/master/whitelist.txt
https://raw.githubusercontent.com/AdguardTeam/AdGuardSDNSFilter/master/Filters/exclusions.txt
https://raw.githubusercontent.com/anudeepND/whitelist/master/domains/optional-list.txt
https://raw.githubusercontent.com/AdguardTeam/HttpsExclusions/master/exclusions/issues.txt
https://raw.githubusercontent.com/hagezi/dns-blocklists/main/whitelist-referral.txt
https://raw.githubusercontent.com/mawenjian/china-cdn-domain-whitelist/master/china-cdn-domain-whitelist.txt
https://raw.githubusercontent.com/notracking/hosts-blocklists-scripts/master/hostnames.whitelist.txt
https://raw.githubusercontent.com/AdguardTeam/HttpsExclusions/master/exclusions/mac.txt
https://raw.githubusercontent.com/boutetnico/url-shorteners/master/list.txt
https://raw.githubusercontent.com/AdguardTeam/HttpsExclusions/master/exclusions/windows.txt
https://raw.githubusercontent.com/Dogino/Discord-Phishing-URLs/main/official-domains.txt
https://raw.githubusercontent.com/ookangzheng/blahdns/master/hosts/whitelist.txt
https://raw.githubusercontent.com/AdguardTeam/HttpsExclusions/master/exclusions/android.txt
https://raw.githubusercontent.com/AdguardTeam/HttpsExclusions/master/exclusions/sensitive.txt
https://raw.githubusercontent.com/anudeepND/whitelist/master/domains/whitelist.txt
https://raw.githubusercontent.com/AdguardTeam/HttpsExclusions/master/exclusions/firefox.txt

Blacklists như sau

https://raw.githubusercontent.com/bigdargon/hostsVN/master/option/domain.txt
https://justdomains.github.io/blocklists/lists/adguarddns-justdomains.txt
https://raw.githubusercontent.com/hagezi/dns-blocklists/main/wildcard/light-onlydomains.txt
https://o0.pages.dev/Lite/domains.txt
https://raw.githubusercontent.com/Yhonay/antipopads/master/hosts
https://raw.githubusercontent.com/mullvad/dns-blocklists/main/output/doh/doh_adblock.txt
https://raw.githubusercontent.com/mullvad/dns-blocklists/main/output/doh/doh_gambling.txt
https://raw.githubusercontent.com/mullvad/dns-blocklists/main/output/doh/doh_privacy.txt
https://raw.githubusercontent.com/FadeMind/hosts.extras/master/add.Risk/hosts
https://raw.githubusercontent.com/DandelionSprout/adfilt/master/Alternate%20versions%20Anti-Malware%20List/AntiMalwareHosts.txt
https://adaway.org/hosts.txt

Lỗi báo như sau

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/runner/work/Cloudflare-Gateway-Pihole/Cloudflare-Gateway-Pihole/src/__main__.py", line 61, in <module>
    asyncio.run(main())
  File "/opt/hostedtoolcache/Python/3.11.5/x64/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.5/x64/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.5/x64/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/runner/work/Cloudflare-Gateway-Pihole/Cloudflare-Gateway-Pihole/src/__main__.py", line 57, in main
    await app.run()
  File "/home/runner/work/Cloudflare-Gateway-Pihole/Cloudflare-Gateway-Pihole/src/utils.py", line 74, in run
    cf_lists = await asyncio.gather(*create_list_tasks)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/Cloudflare-Gateway-Pihole/Cloudflare-Gateway-Pihole/src/cloudflare.py", line 23, in wrapper
    return await func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/Cloudflare-Gateway-Pihole/Cloudflare-Gateway-Pihole/src/cloudflare.py", line 52, in create_list
    raise Exception("Failed to create Cloudflare list")
Exception: Failed to create Cloudflare list
nhubaotruong commented 1 year ago

Này là lỗi do rule của ổng violate check gì đó của cloudflare rồi =)), để thử debug

luxysiv commented 1 year ago

Này là lỗi do rule của ổng violate check gì đó của cloudflare rồi =)), để thử debug

Mình thử fork lại vẫn bị. Thay regex vẫn bị

luxysiv commented 1 year ago

@nhubaotruong hình như script up vượt 300k domains. Có lẽ nên thêm tính năng giới hạn domains script

nhubaotruong commented 1 year ago

Đúng ra là chỉ được 300k rules thôi, nhưng mà nếu gọi đủ nhanh thì có thể race condition bên cloudflare á =))). T đang xài cỡ 407k rules lận =)))

luxysiv commented 1 year ago

Đúng ra là chỉ được 300k rules thôi, nhưng mà nếu gọi đủ nhanh thì có thể race condition bên cloudflare á =))). T đang xài cỡ 407k rules lận =)))

Tớ thấy có người nói + xem action của bạn hôm qua. Phải block lại không lại giống wireguard mất

nhubaotruong commented 1 year ago

Giống wireguard là bị gì ấy nhỉ

luxysiv commented 1 year ago

Giống wireguard là bị gì ấy nhỉ

Cloudflare chặn Wireguard IP VN. Bạn không biết vụ đó hả.

luxysiv commented 1 year ago

Mình update chặn quá 300k rồi

nhubaotruong commented 1 year ago

Cloudflare chặn Wireguard IP VN. Bạn không biết vụ đó hả.

ủa vụ này mình ko biết, cái vụ refer Warp nhận bandwidth đó hả

luxysiv commented 1 year ago

Cloudflare chặn Wireguard IP VN. Bạn không biết vụ đó hả.

ủa vụ này mình ko biết, cái vụ refer Warp nhận bandwidth đó hả

Vụ Generate lấy key wireguard làm VPN ấy.

nhubaotruong commented 1 year ago

@luxysiv Mình mới fix 1 cái lỗi khá ngu xuẩn, lúc encode('idna') có thể exception. Nhớ wrap nó lại nha

try:
    domain = domain.encode("idna").decode("utf-8", "replace")
except Exception:
    continue

Với lại domain nhớ lowercase, có nhiều filter nó duplicate rule mà nó khác ghi hoa ghi thường thôi

luxysiv commented 1 year ago

Ok thanks bạn. Bạn pro quá

luxysiv commented 1 year ago

Bạn coi xem đã đúng chưa ? @nhubaotruong . Mình cóp luôn từ bạn ra. Cái regex đó làm khá chuẩn đấy. Hên. Tớ test 2 cái không bị regex thiếu

luxysiv commented 1 year ago

Bạn muốn sửa gì cứ PR mình sẽ merge

nhubaotruong commented 1 year ago

@luxysiv Thanks bạn, đang ở cty nên ko submit MR được, mới fix lúc trưa thôi

luxysiv commented 1 year ago

@luxysiv Thanks bạn, đang ở cty nên ko submit MR được, mới fix lúc trưa thôi

Bạn quá nhiệt tình cảm ơn còn không hết

luxysiv commented 12 months ago

@nhubaotruong cái Regex domains của tớ xài chuẩn đấy. Tớ thử cái Hagezi và Lists của tớ nó chính xác 100%

nhubaotruong commented 12 months ago

@luxysiv ngon

luxysiv commented 12 months ago

Ủa bạn có biết gì về Github Action Variables không? Kế bên cái Secrets ấy? Sử dụng nó thế nào? Nghe ông Việt cải tiến cái Github này rất nhiều

nhubaotruong commented 12 months ago

@luxysiv Mình ko rành github actions lắm, cũng hay copy paste config của người khác thôi à =))

luxysiv commented 12 months ago

@luxysiv Mình ko rành github actions lắm, cũng hay copy paste config của người khác thôi à =))

Ờm haha, mình cũng copy người ta

luxysiv commented 12 months ago

@nhubaotruong hình như sau khi tăng khả năng up thì phần create policy dễ bị fail

nhubaotruong commented 12 months ago

@luxysiv là sao nhỉ, có log ko b

luxysiv commented 12 months ago

@luxysiv là sao nhỉ, có log ko b

À không,hoặc do mình chỉnh sửa linh tinh, bắt chước bạn chạy 3 lần có vẻ không ổn

nhubaotruong commented 12 months ago

@luxysiv à, mình revert cái đó rồi, tại ban đầu tính là để tự retry, đỡ phiền, mà api limit ko đủ để chạy 3 lần liên tục

luxysiv commented 12 months ago

@luxysiv à, mình revert cái đó rồi, tại ban đầu tính là để tự retry, đỡ phiền, mà api limit ko đủ để chạy 3 lần liên tục

Mình cũng thấy nó lỗi. Bạn có cách nào để loại bỏ những subdomains nếu đã có domain cấp cao hơn không?

nhubaotruong commented 12 months ago

@luxysiv Có á, nhưng mà kiểu ko biết có đáng tin ko. Vd như cái domain vn-libs.top. Nó ko phải là xấu, mà gần đây lên vtv thời sự lừa đảo vì subdomain vietcombank.vn-libs.top. Cái list vn-bad-site mình đang xài cũng xóa cái domain đó ra rồi, vì cái subdomain ko còn tồn tại nữa

luxysiv commented 12 months ago

Ý mình là nếu có ads.google.com rồi thì loại bỏ sub.ads.google.com á bạn

nhubaotruong commented 12 months ago

@luxysiv Function đây nha bạn

from collections import defaultdict
def get_least_specific_subdomains(domains):
        # Create a dictionary where the keys are the base domains and the values are lists of subdomains
        domain_dict = defaultdict(list)
        for domain in domains:
            # Split the domain into its constituent parts
            split_domain = domain.split(".")
            # The base domain is the last two parts of the domain (e.g., 'example.com')
            base_domain = ".".join(split_domain[-2:])
            # Add the domain to the dictionary
            domain_dict[base_domain].append(split_domain)
        least_specific_domains = []
        # For each base domain, find the least specific subdomain(s)
        for base_domain, subdomains in domain_dict.items():
            # Sort the subdomains by length (number of parts)
            sorted_subdomains = sorted(subdomains, key=len)
            # Get the length of the shortest subdomain
            min_length = len(sorted_subdomains[0])
            # Filter out the subdomains that are not the least specific
            least_specific_subdomains = [
                ".".join(subdomain)
                for subdomain in sorted_subdomains
                if len(subdomain) == min_length
            ]
            # Add the least specific subdomains to the result list
            least_specific_domains.extend(least_specific_subdomains)
        return least_specific_domains
luxysiv commented 12 months ago

Cám ơn bạn, để mình nghiên cứu

luxysiv commented 12 months ago

Ủa mình thêm như này đúng không

domains = self.get_least_specific_subdomains(domains)
nhubaotruong commented 12 months ago

đúng rồi bạn

luxysiv commented 12 months ago

Hình như nó loop lâu lắm

nhubaotruong commented 12 months ago

yeah =))) nó phải check mọi line mà :((

luxysiv commented 12 months ago

Tí tớ update code bạn xem có khả năng tăng tốc xử lý không nhé

luxysiv commented 12 months ago

@nhubaotruong bạn tính sang code go hả

nhubaotruong commented 12 months ago

@luxysiv Code cho vui thôi b, reimplement cái mình đã biết bằng language khác là cách học tốt mà =)))

Mà lúc chạy thử thấy có vẻ nhanh hơn python tầm 2s thôi, tại cái chậm ở đây là I/O network