Skip to content

Commit 40fd57a

Browse files
committed
add testcase for masque/http and HTTP proxytunnel
1 parent af00e73 commit 40fd57a

6 files changed

Lines changed: 86 additions & 5 deletions

File tree

g3proxy/ci/python3+curl/test_httpbin.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import unittest
66
import base64
7+
import urllib
78
from io import BytesIO
89
from urllib.parse import urlencode
910

@@ -16,6 +17,8 @@
1617
local_resolve = None
1718
request_target_prefix = None
1819
no_auth = False
20+
proxy_tunnel = False
21+
proxy_masque = False
1922

2023
ACCEPT_JSON = 'Accept: application/json'
2124
ACCEPT_HTML = 'Accept: text/html'
@@ -32,17 +35,37 @@ def setUp(self):
3235
if target_ca_cert is not None:
3336
self.c.setopt(pycurl.CAINFO, target_ca_cert)
3437
if target_proxy is not None:
35-
self.c.setopt(pycurl.PROXY, target_proxy)
36-
if proxy_ca_cert is not None:
37-
self.c.setopt(pycurl.PROXY_CAINFO, proxy_ca_cert)
38+
if proxy_masque:
39+
proxy_uri = urllib.parse.urlparse(target_proxy)
40+
if proxy_uri.username is not None and proxy_uri.password is not None:
41+
s = f"{proxy_uri.username}:{proxy_uri.password}"
42+
auth_header = "Proxy-Authorization: Basic {}".format(
43+
base64.standard_b64encode(s.encode("utf-8")).decode('utf-8'))
44+
self.c.setopt(pycurl.HTTPHEADER, [auth_header])
45+
else:
46+
self.c.setopt(pycurl.PROXY, target_proxy)
47+
if proxy_ca_cert is not None:
48+
self.c.setopt(pycurl.PROXY_CAINFO, proxy_ca_cert)
49+
if proxy_tunnel:
50+
self.c.setopt(pycurl.HTTPPROXYTUNNEL, 1)
3851
if local_resolve is not None:
3952
self.c.setopt(pycurl.RESOLVE, [local_resolve])
4053

4154
def tearDown(self):
4255
self.c.close()
4356

4457
def set_url_and_request_target(self, path: str):
45-
self.c.setopt(pycurl.URL, f"{target_site}{path}")
58+
target_uri = f"{target_site}{path}"
59+
if target_proxy is not None and proxy_masque:
60+
proxy_uri = urllib.parse.urlparse(target_proxy)
61+
encoded_target_uri = urllib.parse.quote(target_uri, safe='') # use safe parameter to also escape '/'
62+
if ':' in proxy_uri.hostname:
63+
uri = f"{proxy_uri.scheme}://[{proxy_uri.hostname}]:{proxy_uri.port}/.well-known/masque/http/{encoded_target_uri}"
64+
else:
65+
uri = f"{proxy_uri.scheme}://{proxy_uri.hostname}:{proxy_uri.port}/.well-known/masque/http/{encoded_target_uri}"
66+
self.c.setopt(pycurl.URL, uri)
67+
else:
68+
self.c.setopt(pycurl.URL, target_uri)
4669
if request_target_prefix is not None:
4770
self.c.setopt(pycurl.REQUEST_TARGET, f"{request_target_prefix}{path}")
4871

@@ -187,6 +210,8 @@ def test_put_file_chunked(self):
187210
parser.add_argument('--resolve', nargs='?', help='Local Resolve Record for curl')
188211
parser.add_argument('--request-target-prefix', nargs='?', help='Set request target')
189212
parser.add_argument('--no-auth', action='store_true', help='No http auth tests')
213+
parser.add_argument('--proxy-tunnel', action='store_true', help='Tunnel the traffic through the proxy')
214+
parser.add_argument('--proxy-masque', action='store_true', help='Use masque/http well known uri')
190215

191216
(args, left_args) = parser.parse_known_args()
192217

@@ -202,6 +227,8 @@ def test_put_file_chunked(self):
202227
request_target_prefix = args.request_target_prefix
203228
target_site = args.site
204229
no_auth = args.no_auth
230+
proxy_tunnel = args.proxy_tunnel
231+
proxy_masque = args.proxy_masque
205232

206233
left_args.insert(0, sys.argv[0])
207234

g3proxy/examples/simple_http_proxy/g3proxy.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ server:
1414
listen:
1515
address: "[::]:10086"
1616
tls_client: { }
17-
local_server_name: 127.0.0.1
17+
local_server_name:
18+
- 127.0.0.1
19+
- ::1
1820
tcp_sock_speed_limit: 100M
1921
- name: test-tls
2022
tls_server:

scripts/coverage/g3proxy/0001_base_http_proxy/testcases.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ test_http_proxy_http_forward()
1313
}
1414

1515

16+
test_http_proxy_http_connect()
17+
{
18+
date
19+
20+
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTP_PROXY} --proxy-tunnel -T http://httpbin.local
21+
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTP_PROXY} --proxy-tunnel -T http://127.0.0.1
22+
}
23+
24+
1625
test_http_easy_proxy_http_forward()
1726
{
1827
date
@@ -25,6 +34,15 @@ test_http_easy_proxy_http_forward()
2534
}
2635

2736

37+
test_http_masque_http_forward()
38+
{
39+
date
40+
41+
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTP_PROXY} --proxy-masque -T http://httpbin.local/
42+
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTP_PROXY} --proxy-masque -T http://127.0.0.1/
43+
}
44+
45+
2846
test_http_proxy_ftp_over_http()
2947
{
3048
date
@@ -53,6 +71,14 @@ test_https_easy_proxy_http_forward()
5371
}
5472

5573

74+
test_https_masque_http_forward()
75+
{
76+
date
77+
78+
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTPS_PROXY} --proxy-masque -T http://httpbin.local/ --ca-cert "${TEST_CA_CERT_FILE}"
79+
}
80+
81+
5682
test_https_proxy_ftp_over_http()
5783
{
5884
date
@@ -63,14 +89,17 @@ test_https_proxy_ftp_over_http()
6389

6490
HTTP_PROXY="http://127.0.0.1:8080"
6591
test_http_proxy_http_forward
92+
test_http_proxy_http_connect
6693
test_http_proxy_ftp_over_http
6794
test_http_easy_proxy_http_forward
95+
test_http_masque_http_forward
6896

6997

7098
HTTP_PROXY="http://[::1]:8080"
7199
test_http_proxy_http_forward
72100
test_http_proxy_ftp_over_http
73101
test_http_easy_proxy_http_forward
102+
test_http_masque_http_forward
74103

75104

76105
for port in 8443 8444 9443
@@ -79,4 +108,5 @@ do
79108
test_https_proxy_http_forward
80109
test_https_proxy_ftp_over_http
81110
test_https_easy_proxy_http_forward
111+
test_https_masque_http_forward
82112
done

scripts/coverage/g3proxy/0006_chain_http_proxy/testcases.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ test_http_easy_proxy_https_forward()
3131
}
3232

3333

34+
test_http_masque_https_forward()
35+
{
36+
date
37+
38+
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTP_PROXY} --proxy-masque -T https://httpbin.local:9443/ --no-auth
39+
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTP_PROXY} --proxy-masque -T https://httpbin.local:2443/ --no-auth
40+
}
41+
42+
3443
test_http_proxy_h2()
3544
{
3645
date
@@ -69,6 +78,15 @@ test_https_easy_proxy_https_forward()
6978
}
7079

7180

81+
test_https_masque_https_forward()
82+
{
83+
date
84+
85+
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTPS_PROXY} --proxy-masque -T https://httpbin.local:9443/ --no-auth --ca-cert "${TEST_CA_CERT_FILE}"
86+
python3 "${PROJECT_DIR}/g3proxy/ci/python3+curl/test_httpbin.py" -x ${HTTPS_PROXY} --proxy-masque -T https://httpbin.local:2443/ --no-auth --ca-cert "${TEST_CA_CERT_FILE}"
87+
}
88+
89+
7290
test_https_proxy_h2()
7391
{
7492
date
@@ -103,6 +121,7 @@ test_http_proxy_http_forward
103121
# FTP not supported in proxy escaper
104122
#test_http_proxy_ftp_over_http
105123
test_http_easy_proxy_https_forward
124+
test_http_masque_https_forward
106125
test_http_proxy_https_connect
107126
test_http_proxy_https_forward
108127

@@ -111,6 +130,7 @@ HTTPS_PROXY="https://g3proxy.local:8443"
111130
test_https_proxy_http_forward
112131
test_https_proxy_ftp_over_http
113132
test_https_easy_proxy_https_forward
133+
test_https_masque_https_forward
114134
test_https_proxy_https_connect
115135
test_https_proxy_https_forward
116136

scripts/coverage/g3proxy/0021_audit_icap_preview/testcases.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
HTTP_PROXY="http://127.0.0.1:8080"
55
test_http_proxy_http_forward
6+
test_http_proxy_http_connect
67
test_http_proxy_ftp_over_http
78
test_http_proxy_https_connect
89
test_http_proxy_https_forward

scripts/coverage/g3proxy/0022_audit_icap_no_preview/testcases.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
HTTP_PROXY="http://127.0.0.1:8080"
55
test_http_proxy_http_forward
6+
test_http_proxy_http_connect
67
test_http_proxy_ftp_over_http
78
test_http_proxy_https_connect
89
test_http_proxy_https_forward

0 commit comments

Comments
 (0)