I am trying to write Python code that will set an invisible relay between two TCP sockets. My current technique is to configure two streams, each of which reads and subsequently writes 1kb of data at a time in a certain direction (i.e. 1 stream for AB, 1 stream for BA).
This works for some applications and protocols, but is not reliable - sometimes individual applications will behave differently when working through this Python relay. Some even crash.
I think this is due to the fact that when I finish reading on socket A, the program running there believes that its data has already reached B, when in fact I am an insidious person in the middle - it has not yet been sent to B. B situations where B is not ready to receive data (while send() blocks for a while), we are in a state where A considers that it successfully sent data to B, but I still keep data waiting for send() to be called fulfillment. I think this is the reason for the difference in behavior that I found in some applications using my current relay code. Am I missing something, or does it sound right?
If so, my real question is: is there a way to solve this problem? Is it possible to read only socket A, when we know that B is ready to receive data? Or is there another method that I can use to establish a truly "invisible" two-way relay between [already open and installed] TCP sockets?
source share