liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
 * Module dependencies.
 */
 
const fs = require('fs');
const net = require('net');
const path = require('path');
const http = require('http');
const https = require('https');
const assert = require('assert');
const setup = require('../');
 
describe('proxy', function() {
    var proxy;
    var proxyPort;
 
    var server;
    var serverPort;
 
    before(function(done) {
        // setup proxy server
        proxy = setup(http.createServer());
        proxy.listen(function() {
            proxyPort = proxy.address().port;
            done();
        });
    });
 
    before(function(done) {
        // setup target server
        server = http.createServer();
        server.listen(function() {
            serverPort = server.address().port;
            done();
        });
    });
 
    after(function(done) {
        proxy.once('close', function() {
            done();
        });
        proxy.close();
    });
 
    after(function(done) {
        server.once('close', function() {
            done();
        });
        server.close();
    });
 
    it('should proxy HTTP GET requests', function(done) {
        var gotData = false;
        var gotRequest = false;
        var host = '127.0.0.1:' + serverPort;
        server.once('request', function(req, res) {
            gotRequest = true;
            // ensure headers are being proxied
            assert(req.headers['user-agent'] == 'curl/7.30.0');
            assert(req.headers.host == host);
            assert(req.headers.accept == '*/*');
            res.end();
        });
 
        var socket = net.connect({ port: proxyPort });
        socket.once('close', function() {
            assert(gotData);
            assert(gotRequest);
            done();
        });
        socket.once('connect', function() {
            socket.write(
                'GET http://' +
                    host +
                    '/ HTTP/1.1\r\n' +
                    'User-Agent: curl/7.30.0\r\n' +
                    'Host: ' +
                    host +
                    '\r\n' +
                    'Accept: */*\r\n' +
                    'Proxy-Connection: Keep-Alive\r\n' +
                    '\r\n'
            );
        });
        socket.setEncoding('utf8');
        socket.once('data', function(data) {
            assert(0 == data.indexOf('HTTP/1.1 200 OK\r\n'));
            gotData = true;
            socket.destroy();
        });
    });
 
    it('should establish connection for CONNECT requests', function(done) {
        var gotData = false;
        var socket = net.connect({ port: proxyPort });
        socket.once('close', function() {
            assert(gotData);
            done();
        });
        socket.once('connect', function() {
            var host = '127.0.0.1:' + serverPort;
            socket.write(
                'CONNECT ' +
                    host +
                    ' HTTP/1.1\r\n' +
                    'Host: ' +
                    host +
                    '\r\n' +
                    'User-Agent: curl/7.30.0\r\n' +
                    'Proxy-Connection: Keep-Alive\r\n' +
                    '\r\n'
            );
        });
        socket.setEncoding('utf8');
        socket.once('data', function(data) {
            assert(
                0 == data.indexOf('HTTP/1.1 200 Connection established\r\n')
            );
            gotData = true;
            socket.destroy();
        });
    });
 
    describe('authentication', function() {
        function clearAuth() {
            delete proxy.authenticate;
        }
 
        before(clearAuth);
        after(clearAuth);
 
        it('should invoke the `server.authenticate()` function when set', function(done) {
            var auth = 'Basic Zm9vOmJhcg==';
            var called = false;
            proxy.authenticate = function(req, fn) {
                assert(auth == req.headers['proxy-authorization']);
                socket.destroy();
                called = true;
            };
            var socket = net.connect({ port: proxyPort });
            socket.once('close', function() {
                assert(called);
                done();
            });
            socket.once('connect', function() {
                socket.write(
                    'GET / HTTP/1.1\r\n' +
                        'Proxy-Authorization: ' +
                        auth +
                        '\r\n' +
                        '\r\n'
                );
            });
        });
 
        it('should provide the HTTP client with a 407 response status code', function(done) {
            proxy.authenticate = function(req, fn) {
                // reject everything
                fn(null, false);
            };
            var gotData = false;
            var socket = net.connect({ port: proxyPort });
            socket.once('close', function() {
                assert(gotData);
                done();
            });
            socket.once('connect', function() {
                socket.write('GET / HTTP/1.1\r\n\r\n');
            });
            socket.setEncoding('utf8');
            socket.once('data', function(data) {
                assert(0 == data.indexOf('HTTP/1.1 407'));
                gotData = true;
                socket.destroy();
            });
        });
 
        it("should close the socket after a CONNECT request's 407 response status code", function(done) {
            proxy.authenticate = function(req, fn) {
                // reject everything
                fn(null, false);
            };
            var gotData = false;
            var socket = net.connect({ port: proxyPort });
            socket.once('close', function() {
                assert(gotData);
                done();
            });
            socket.once('connect', function() {
                socket.write('CONNECT 127.0.0.1:80 HTTP/1.1\r\n\r\n');
            });
            socket.setEncoding('utf8');
            socket.once('data', function(data) {
                assert(0 == data.indexOf('HTTP/1.1 407'));
                gotData = true;
            });
        });
    });
});