I want Transparent Proxy(macOS Catalina) work below.
The detailed operation example is as follows.
program <--> Transparent Proxy <--> remote connect
thank you
The detailed operation example is as follows.
The user tries to connect to 10.10.10.10:1000.
At the same time as this process, change the destination to 20.20.20.20:2000 to connect.
program <--> Transparent Proxy <--> remote connect
how to connect remote server
how to read data from flow
how to write data from flow to remote connection
Code Block swift override func handleNewFlow(_ flow: NEAppProxyFlow) -> Bool { if let tcpflow = flow as? NEAppProxyTCPFlow { // 1. how to connect remote server // 2. how to read data from flow // 3. how to write data from flow to remote connection } }
thank you
Ultimately, the nitty gritty details of doing this come down to how you want this to work in your environment, and with the destination endpoint, but a very general workflow could look something like:
1) Decide if the flow is one you want your transparent proxy to handle and return true/YES in handleNewFlow.
2) Cast the flow to the appropriate flow object for NEAppProxyTCPFlow or NEAppProxyUDPFlow as you are doing in your example.
3) Create the remote side of the connection with something like NWConnection, NWTCPConnection, nw_connection_t. This is where you could optional add logic to point at 20.20.20.20:2000 if needed. This is also where you could add additional logic to make these decisions.
4) Once your remote connection object is created I strongly advise you to create a management class for this object and for the flow copying operations. Pass your remote connection into this management class and start the remote connection. After the remote side of the connection has gone into the .ready state, open the flow you are handling.
5) Using the flow copying operations you defined in the management class, read from the flow and write the remote connection. Likewise, read from the remote connection and write to the flow. For NWConnection and NEAppProxyTCPFlow, take a look at these methods for doing so:
a) (Flow data) func write(Data, withCompletionHandler: (Error?) -> Void)
b) (Flow data) func readData(completionHandler: (Data?, Error?) -> Void)
c) (Remote data) [func send(content: Data?, contentContext: NWConnection.ContentContext, isComplete: Bool, completion:
NWConnection.SendCompletion)](https://developer.apple.com/documentation/network/nwconnection/3003626-send)
d) (Remote data) [func receive(minimumIncompleteLength: Int, maximumLength: Int, completion: (Data?,
NWConnection.ContentContext?, Bool, NWError?) -> Void)](https://developer.apple.com/documentation/network/nwconnection/2998572-receive)
Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
1) Decide if the flow is one you want your transparent proxy to handle and return true/YES in handleNewFlow.
2) Cast the flow to the appropriate flow object for NEAppProxyTCPFlow or NEAppProxyUDPFlow as you are doing in your example.
3) Create the remote side of the connection with something like NWConnection, NWTCPConnection, nw_connection_t. This is where you could optional add logic to point at 20.20.20.20:2000 if needed. This is also where you could add additional logic to make these decisions.
4) Once your remote connection object is created I strongly advise you to create a management class for this object and for the flow copying operations. Pass your remote connection into this management class and start the remote connection. After the remote side of the connection has gone into the .ready state, open the flow you are handling.
5) Using the flow copying operations you defined in the management class, read from the flow and write the remote connection. Likewise, read from the remote connection and write to the flow. For NWConnection and NEAppProxyTCPFlow, take a look at these methods for doing so:
a) (Flow data) func write(Data, withCompletionHandler: (Error?) -> Void)
b) (Flow data) func readData(completionHandler: (Data?, Error?) -> Void)
c) (Remote data) [func send(content: Data?, contentContext: NWConnection.ContentContext, isComplete: Bool, completion:
NWConnection.SendCompletion)](https://developer.apple.com/documentation/network/nwconnection/3003626-send)
d) (Remote data) [func receive(minimumIncompleteLength: Int, maximumLength: Int, completion: (Data?,
NWConnection.ContentContext?, Bool, NWError?) -> Void)](https://developer.apple.com/documentation/network/nwconnection/2998572-receive)
Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com