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
|
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
*/
#include "proxy_int.h"
#include "proxy_http_int.h"
#include "qemu-common.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#define HTTP_VERSION "1.1"
static void
http_service_free( HttpService* service )
{
PROXY_LOG("%s", __FUNCTION__);
if (service->footer != service->footer0)
qemu_free(service->footer);
qemu_free(service);
}
static ProxyConnection*
http_service_connect( HttpService* service,
SocketType sock_type,
SockAddress* address )
{
/* the HTTP proxy can only handle TCP connections */
if (sock_type != SOCKET_STREAM)
return NULL;
/* if the client tries to directly connect to the proxy, let it do so */
if (sock_address_equal( address, &service->server_addr ))
return NULL;
PROXY_LOG("%s: trying to connect to %s",
__FUNCTION__, sock_address_to_string(address));
if (sock_address_get_port(address) == 80) {
/* use the rewriter for HTTP */
PROXY_LOG("%s: using HTTP rewriter", __FUNCTION__);
return http_rewriter_connect(service, address);
} else {
PROXY_LOG("%s: using HTTP rewriter", __FUNCTION__);
return http_connector_connect(service, address);
}
}
int
proxy_http_setup( const char* servername,
int servernamelen,
int serverport,
int num_options,
const ProxyOption* options )
{
HttpService* service;
SockAddress server_addr;
const ProxyOption* opt_nocache = NULL;
const ProxyOption* opt_keepalive = NULL;
const ProxyOption* opt_auth_user = NULL;
const ProxyOption* opt_auth_pass = NULL;
const ProxyOption* opt_user_agent = NULL;
if (servernamelen < 0)
servernamelen = strlen(servername);
PROXY_LOG( "%s: creating http proxy service connecting to: %.*s:%d",
__FUNCTION__, servernamelen, servername, serverport );
/* resolve server address */
if (proxy_resolve_server(&server_addr, servername,
servernamelen, serverport) < 0)
{
return -1;
}
/* create service object */
service = qemu_mallocz(sizeof(*service));
if (service == NULL) {
PROXY_LOG("%s: not enough memory to allocate new proxy service", __FUNCTION__);
return -1;
}
service->server_addr = server_addr;
/* parse options */
{
const ProxyOption* opt = options;
const ProxyOption* end = opt + num_options;
for ( ; opt < end; opt++ ) {
switch (opt->type) {
case PROXY_OPTION_HTTP_NOCACHE: opt_nocache = opt; break;
case PROXY_OPTION_HTTP_KEEPALIVE: opt_keepalive = opt; break;
case PROXY_OPTION_AUTH_USERNAME: opt_auth_user = opt; break;
case PROXY_OPTION_AUTH_PASSWORD: opt_auth_pass = opt; break;
case PROXY_OPTION_HTTP_USER_AGENT: opt_user_agent = opt; break;
default: ;
}
}
}
/* prepare footer */
{
int wlen;
char* p = service->footer0;
char* end = p + sizeof(service->footer0);
/* no-cache */
if (opt_nocache) {
p += snprintf(p, end-p, "Pragma: no-cache\r\nCache-Control: no-cache\r\n");
if (p >= end) goto FooterOverflow;
}
/* keep-alive */
if (opt_keepalive) {
p += snprintf(p, end-p, "Connection: Keep-Alive\r\nProxy-Connection: Keep-Alive\r\n");
if (p >= end) goto FooterOverflow;
}
/* authentication */
if (opt_auth_user && opt_auth_pass) {
char user_pass[256];
char encoded[512];
int uplen;
uplen = snprintf( user_pass, sizeof(user_pass), "%.*s:%.*s",
opt_auth_user->string_len, opt_auth_user->string,
opt_auth_pass->string_len, opt_auth_pass->string );
if (uplen >= sizeof(user_pass)) goto FooterOverflow;
wlen = proxy_base64_encode(user_pass, uplen, encoded, (int)sizeof(encoded));
if (wlen < 0) {
PROXY_LOG( "could not base64 encode '%.*s'", uplen, user_pass);
goto FooterOverflow;
}
p += snprintf(p, end-p, "Proxy-authorization: Basic %.*s\r\n", wlen, encoded);
if (p >= end) goto FooterOverflow;
}
/* user agent */
if (opt_user_agent) {
p += snprintf(p, end-p, "User-Agent: %.*s\r\n",
opt_user_agent->string_len,
opt_user_agent->string);
if (p >= end) goto FooterOverflow;
}
p += snprintf(p, end-p, "\r\n");
if (p >= end) {
FooterOverflow:
PROXY_LOG( "%s: buffer overflow when creating connection footer",
__FUNCTION__);
http_service_free(service);
return -1;
}
service->footer = service->footer0;
service->footer_len = (p - service->footer);
}
PROXY_LOG( "%s: creating HTTP Proxy Service Footer is (len=%d):\n'%.*s'",
__FUNCTION__, service->footer_len,
service->footer_len, service->footer );
service->root->opaque = service;
service->root->serv_free = (ProxyServiceFreeFunc) http_service_free;
service->root->serv_connect = (ProxyServiceConnectFunc) http_service_connect;
if (proxy_manager_add_service( service->root ) < 0) {
PROXY_LOG("%s: could not register service ?", __FUNCTION__);
http_service_free(service);
return -1;
}
return 0;
}
|