gnh1201 / welsonjs

WelsonJS - Build a Windows app on the Windows built-in JavaScript engine
https://catswords.social/@catswords_oss
GNU General Public License v3.0
134 stars 11 forks source link

[Uncategorized] Web(e.g. HTTPS) fail-over simulation #94

Open gnh1201 opened 9 months ago

gnh1201 commented 9 months ago

Summary

Recently, a request came in from a company with prior experience in adopting WelsonJS, expressing the desire to create a tool for web fail-over simulation (e.g., simulating HTTPS connection failures). They mentioned having used the Windivert library alongside WelsonJS before, suggesting that it should be possible to utilize these components to develop a fail-over simulator. I am currently looking into this issue.

Keywords: High Availablity, Disaster Recovery, Emergency Response Training

gnh1201 commented 9 months ago

I queried ChatGPT for details on this issue and was able to obtain the example code using pydivert as follows:

import pydivert

def is_port_443(packet):
    # Implement the logic to check if the destination port is 443
    # For example, check the TCP header in the packet
    return True

def modify_packet(packet):
    # Modify the packet to send a fake "Bad Request" response
    fake_response = "HTTP/1.1 400 Bad Request\r\nContent-Type: text/plain\r\n\r\nBad Request"

    response_bytes = bytes(fake_response, 'utf-8')

    # Replace the original payload with the fake response
    packet.payload = response_bytes

def main():
    with pydivert.WinDivert("tcp.DstPort == 443 or tcp.SrcPort == 443") as w:
        print("Intercepting traffic on port 443...")

        for packet in w:
            if is_port_443(packet):
                modify_packet(packet)
                w.send(packet)
                print("Sent fake 'Bad Request' response to port 443.")
            else:
                w.send(packet)

if __name__ == "__main__":
    main()

I will review this code snippet.