question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Completely faking connection to server in script

See original GitHub issue

Is your feature request related to a problem? Please describe. I haven’t found a good way to completely intercept connections (and all requests) to a server from a script.

Describe the solution you’d like

Perhaps there should be a FakeLayer or some other mechanism which would simulate the procedure of connecting to an upstream server, without actually doing so.

Describe alternatives you’ve considered

Here is the best solution I found so far:

from mitmproxy import http
from mitmproxy.proxy.protocol import TlsLayer

# The server to which we wish to intercept connections.
# When the hostname doesn't exist, this becomes a hard requirement.
_redirect_source = 'nonexisting.example.net'

# The server we will be redirecting the connection to.
# Can be any server which listens on the same ports as _redirect_source.
_redirect_target = 'www.google.com'


# Change the server address at connection time.
def serverconnect(cc):
    if cc.address[0] == _redirect_source:
        cc._my_real_address = cc.address  # Save original address
        cc.address = (_redirect_target, cc.address[1])
        return True


# For TLS, we must also patch the SNI hostname.
# Otherwise, certificate validation will fail.
def next_layer(next_layer):
    if isinstance(next_layer, TlsLayer):
        if next_layer.server_conn.address[0] == _redirect_source:
            next_layer._custom_server_sni = _redirect_target


def request(flow: http.HTTPFlow) -> None:
    # Now, undo the hostname patch in serverconnect,
    # so that mitmproxy's UI shows the right URL.
    try:
        flow.server_conn.address = flow.server_conn._my_real_address
        flow.request.host = flow.server_conn.address[0]
        flow.request.port = flow.server_conn.address[1]
    except AttributeError:
        pass
    # Now send our fake response
    flow.response = ...

Additional context

The above workaround is not perfect as it still does require a target server to redirect the connection to. (Using a local server poses difficulties as it would have to have a valid SSL certificate for the target redirect domain name.)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mhilscommented, Nov 3, 2021

Yes, setting connection_strategy to lazy is exactly what should be done here. Thanks for the heads-up, @yufengzjj! 🍰

0reactions
petromoldovancommented, Nov 3, 2021

@yufengzjj thanks! i will try that!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Faking a FTP service - nmap - Stack Overflow
1 Answer 1 ... First, you're missing a \r\n at the end of your fake response that the match line needs. The other...
Read more >
How to build a TCP proxy #2: Fake DNS Server | Robert Heaton
Next, find your laptop's local IP address by running a terminal command like ifconfig | grep 'en0' -A 2 | grep netmask (try...
Read more >
3 Free Tools to Fake DNS Responses for Malware Analysis
If you don't want to configure a full-blown DNS server, you can use specialized tools such as ApateDNS, FakeDNS and fakeDNS.py. ApateDNS in ......
Read more >
How to Fake or Mock an API with JSON Server - Atomic Spin
JSON Server is an easy-to-use module for mocking an API for most of your API needs, including custom routes, POST routes, and faking...
Read more >
Faking SQL Server in Haskell - FP Complete
In order to implement a fake SQL Server, we need: A server program that accepts connections from clients and talks their language. A...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found