h2non/toxy

Network throttling in iOS app programatically for automation.

amit1990sumit opened this issue · 11 comments

Hi Team,

Is it possible that I can throttle network on my iOS app or specific URL?
Please help me to understand this, what are the things required for this if possible.

Thanks

h2non commented

Yes, as long as you put toxy in the middle of the communication:

iOS --> toxy proxy --> target server

Your iOS should communicate with the toxy proxy directly and there is where the throttling would happen.

Thanks for reply. Since I am new to node js, could you please guide me how to hit api to localhost using node js?

Thanks again

h2non commented

See some examples, such as: https://github.com/h2non/toxy/blob/master/examples/bandwidth-limit.js

Run it locally from the console via: $ node example.js

You need to replace the httpbin.org domain with the server you want to consume. Afterwards, you need to consume the toxy server running in localhost in your iOS app. Assuming you can't consume it via local IP address/hostname in your LAN, you can use this node.js package to create a tunnel in order to expose the toxy server to the Internet, and consume from everywhere, including your iOS app:

$ npm install -g localtunnel

Then:

$ lt --port 3000 --subdomain my-toxy-proxy-server

That would give you an URL like: https://my-toxy-proxy-server.localtunnel.me that you can use in your iOS app.

That's all. Enjoy it.

Thank you so much for ur reply.
One last question, can I add proxy ip and port inside iphone proxy setting so that we can track all the network call which go through iphone. I want same as charles proxy, only thing it should be programmatically.
Please help me.

h2non commented

As far as I know, you don't need to explicitly use the "proxy" mode in the iPhone. You just need to communicate to the target IP/hostname of toxy, and then you can programmatically inspect, log, modify the incoming HTTP traffic from the phone. You can see the middleware/intercept/logging examples.

Ok, but i do not want to proxy specific url, In my case, my framework itself hit all the network call and i am unable to access the urls. I want to add proxy so that any network call able to track which go through my iphone.

Please help and thank you again

Today, I tried all the steps what you mentioned,

  1. I create the tunnel and run "lt --port 3000 --subdomain my-toxy-proxy-server" which gave URL = "https://my-toxy-proxy-server.localtunnel.me"
  2. Replaced the same URL in example project "bandwidth-limit.js".
  3. run the "bandwidth-limit.js" which gives "Server listening on port: 3000".

but found that no call goes through a proxy.

Currently, I am trying this in a browser. Please let me know if anything missing from my end. Please help me.

h2non commented

This looks like an XY problem. I'm not actually sure that you know what you want or you actually understand how the network works. Please, make sure you understand these things. Otherwise I can't help.

Thanks for reply and sorry to bother you.
Actually I am new to server side script and most of the thing I undertood now.
Only prob I am facing now, In readme file there are serveral API mentioned.

API
Hierarchy:

Servers - Managed toxy instances
Rules - Globally applied rules
Poisons - Globally applied poisons
Rules - Poison-specific rules
Routes - List of configured routes
Route - Object for each specific route
Rules - Route-level registered rules
Poisons - Route-level registered poisons
Rules - Route-level poison-specific rules
GET /
Servers
GET /servers
GET /servers/:id
Rules
GET /servers/:id/rules
POST /servers/:id/rules
Accepts: application/json

Example payload:

{
  "name": "method",
  "options": "GET"
}

Just want to know how I can access these API from client end.

h2non commented

You can take a look at the instructions on the Readme > Admin API section. Basically, you need to start 2 HTTP servers running on different ports, one for the proxy itself, and another for the HTTP Admin API. You can see an example as well on the repository. Note that this is apposite to your initial intention about having programmatic control of the proxy.

Thanks for the reply.

I think HTTP API is the only way to achieve throttled at runtime from the iOS app otherwise no other way to access proxy rules and poison.

I run admin server on port 9000 and proxy server in 3000 port.
Now, I hit my media URL using "http://localhost:3000/iphone/samples/bipbop/bipbopall.m3u8" and I start playing the video then I am setting poison and rules using HTTP API given below.
POST: http://localhost:9001/servers/8dd/poisons
{
"name": "inject",
"option": {
"code": 404,
"body": {"error": "toxy injected error"},
"headers": {"Content-Type": "application/json"}
},
"probability": 100
}
And I am able to generate an error in between and player through error.

Now the only issue, when I hit HTTP API to limit bandwidth, it is not setting the actual bandwidth what I am passing to in json request mentioned below.
Using: POST http://localhost:9000/servers/8dd/poisons (I am using the simulator for now)
{
"name": "bandwidth",
"phase": "outgoing",
"options": { "bps": 512000}
}

Thanks again for helping me throughout.