Skip to content

Commit ed5e3c9

Browse files
authored
Update README.md
1 parent cf63ea8 commit ed5e3c9

File tree

1 file changed

+22
-51
lines changed

1 file changed

+22
-51
lines changed

README.md

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ $ npm install proxy-supervisor
1717
- High performance
1818
- High test coverage
1919

20-
## How to play
20+
## How to
2121

22-
If you want a simple standalone proxy balancer from command line check out [proxy-supervisor-cli](https://github.com/vladislao/proxy-supervisor-cli) or [dockerized proxy-supervisor](https://hub.docker.com/r/vladislaosan/proxy-supervisor).
22+
For a straightforward standalone proxy balancer accessible via command line, explore [proxy-supervisor-cli](https://github.com/vladislao/proxy-supervisor-cli) or [dockerized proxy-supervisor](https://hub.docker.com/r/vladislaosan/proxy-supervisor).
2323

2424
## Usage
2525

26-
Just initialize a balancer and add some proxies.
26+
Start by initializing a balancer and adding your proxies:
2727

2828
```javascript
2929
const http = require("http");
@@ -33,68 +33,35 @@ const awesomeBalancer = balancer().add([
3333
"http://SOME_PROXY:38403",
3434
"http://OTHER_PROXY:61637"
3535
]);
36-
```
3736

38-
Great! Now let's get it to work. Create a middleware and put it in your route. To simplify example, we will use plain http server.
37+
// Now, integrate it into your application. Below, we set up a basic HTTP server using the balancer as middleware.
3938

40-
```javascript
4139
http
4240
.createServer(awesomeBalancer.proxy())
4341
.on("connect", awesomeBalancer.connect())
4442
.listen(3000);
4543
```
4644

47-
Awesome! Next step is to set your balancing server as a proxy server wherever you want to use proxies. This server will proxy requests using specified list of proxies. The final trace will look like that _(you) -> (balancer) -> (proxy) -> (endpoint)_.
48-
49-
Finding proxies and adding them by hand is painful. Even more, you will probably want to remove dead ones. To simplify that process you can use _sources_. Let's add a few sources.
45+
Great! The next step is to configure your balancing server as the proxy server in any application that needs to use proxies. This setup will channel requests through the specified proxies, forming a path like _(you) -> (balancer) -> (proxy) -> (endpoint)_.
5046

51-
```javascript
52-
const { balancer } = require("proxy-supervisor");
53-
const source = require("ps-free-proxy-list");
47+
#### Authentication
5448

55-
const awesomeBalancer = balancer().subscribe(source);
56-
```
57-
58-
Done! Sources will automatically replenish your balancer with new proxies. You should be able to find more sources on [github](https://github.com/). So, what about unreachable proxies? Let's add a monitor to filter them out!
49+
In scenarios where a proxy requires authorization, use the _formatHeaders_ function. This function enables you to embed proxy credentials in the URL (e.g., https://login:password@MY_PROXY:3123) and set the appropriate authorization header. Here's how to implement it:
5950

6051
```javascript
61-
const { monitor } = require("proxy-supervisor");
62-
awesomeBalancer.subscribe(monitor({ target: "http://YOUR_IP:3001" }));
63-
```
64-
65-
Monitor will trigger for every 5 minutes and remove proxies, that didn't respond with successful status code. Best practice would be to specify your own server, and make sure port is open.
52+
const formatHeaders = (proxy, headers) => {
53+
if (!proxy.url.auth) return headers;
54+
return {
55+
...headers,
56+
"Auth-Proxy":
57+
"Basic " + Buffer.from(proxy.url.auth).toString("base64"),
58+
};
59+
};
6660

67-
```javascript
68-
const http = require("http");
6961
http
70-
.createServer((req, res) => {
71-
res.writeHead(200);
72-
res.end();
73-
})
74-
.listen(3001);
75-
```
76-
77-
You are not limited in the way you can use balancers. For example, you can have different balancers on different routes. Sources designed to work with multiple balancers.
78-
79-
```javascript
80-
const express = require("experss");
81-
const { balancer, monitor } = require("proxy-supervisor");
82-
const source = require("ps-nordvpn");
83-
84-
const freeBalancer = balancer()
85-
.subscribe(source)
86-
.subscribe(monitor({ target: "http://YOUR_IP:3001" }));
87-
88-
const privateBalancer = balancer()
89-
.add(["http://SOME_PROXY:38403", "http://OTHER_PROXY:61637"])
90-
.subscribe(monitor);
91-
92-
const app = express()
93-
.use("/free", freeBalancer.proxy())
94-
.use("/private", privateBalancer.proxy())
95-
.on("connect", privateBalancer.connect());
96-
97-
app.listen(3000);
62+
.createServer(balancer.proxy({ formatHeaders }))
63+
.on("connect", balancer.connect({ formatHeaders }))
64+
.listen(3000);
9865
```
9966

10067
## Design
@@ -131,6 +98,8 @@ Subscribes to the specified source.
13198
- **options** _\<Object\>_ Configuration details.
13299

133100
- **timeout** _\<Integer\>_ Sets the socket to timeout after timeout milliseconds of inactivity. Note that increasing the timeout beyond the OS-wide TCP connection timeout will not have any effect ([the default in Linux can be anywhere from 20-120 seconds](http://www.sekuda.com/overriding_the_default_linux_kernel_20_second_tcp_socket_connect_timeout)). Defaults to 30 seconds.
101+
102+
- **formatHeaders** _\<Function\>_ This function is designed to modify headers before a request is sent through your proxy. It is commonly used for handling proxy authorization. The function signature is _(proxy, headers)_, and it must return an updated headers object.
134103

135104
- Returns: _\<Function\>_
136105

@@ -142,6 +111,8 @@ Creates a middleware function. Middleware has a signature of _(req, res, next)_.
142111

143112
- **timeout** _\<Integer\>_ Sets the socket to timeout after timeout milliseconds of inactivity. Defaults to 30 seconds.
144113

114+
- **formatHeaders** _\<Function\>_ This function is designed to modify headers before a request is sent through your proxy. It is commonly used for handling proxy authorization. The function signature is _(proxy, headers)_, and it must return an updated headers object.
115+
145116
- Returns: _\<Function\>_
146117

147118
Creates a handler for HTTP CONNECT method. [It is used to open a tunnel between client and proxy server](https://tools.ietf.org/html/rfc2817#section-5.2).

0 commit comments

Comments
 (0)