parent
538f9974ab
commit
436b5f3885
8 changed files with 308 additions and 0 deletions
@ -0,0 +1,78 @@ |
||||
/*! @file
|
||||
|
||||
@id $Id$ |
||||
*/ |
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#ifndef PROXYFACE_HXX |
||||
#define PROXYFACE_HXX |
||||
|
||||
#include <list> |
||||
#include <string> |
||||
|
||||
//! auto proxy configuration
|
||||
namespace proxy { |
||||
|
||||
//! exceptions
|
||||
namespace exc { |
||||
|
||||
//! unspecific error
|
||||
class error: std::exception { |
||||
public: |
||||
virtual const char* what() const throw() { |
||||
return "Auto Proxy Detection Error"; |
||||
} |
||||
}; |
||||
|
||||
} |
||||
|
||||
//! Proxy types
|
||||
enum Type { |
||||
DIRECT, //< direct connection, no proxy
|
||||
DEFAULT, //< default proxy
|
||||
HTTP, //< HTTP proxy host
|
||||
SOCKS //<SOCKS5 proxy
|
||||
}; |
||||
|
||||
//! List of proxies, type -> url and port
|
||||
typedef std::list< std::pair<Type, std::pair<std::string, std::string> > > |
||||
List; |
||||
|
||||
//! Unified Interface for accessing proxy::Face
|
||||
/*! Abstract interface, impementation for Unix and Windoze differs.
|
||||
|
||||
Instanciate proxy::Face, which is a typedef to your |
||||
platform's implementation. |
||||
|
||||
@code |
||||
proxy::Auto pf; // keep for program life time (because of caching)
|
||||
proxy::List pf.proxies("http://swisssign.com"); |
||||
[...] // set proxy, read from http://swisssign.com
|
||||
@endcode */ |
||||
class Interface { |
||||
public: |
||||
//! Keep your instance as long as possible, because of caching.
|
||||
Interface() {} |
||||
virtual ~Interface() {} |
||||
//! Get list of proxies for a given URL.
|
||||
/*! @throw a child of std::exception if anything fails */ |
||||
virtual List proxies(const std::string& url) = 0; |
||||
}; |
||||
} |
||||
|
||||
#ifdef WIN32 |
||||
// use windoze proprietary winhttp
|
||||
#include <proxyface/windoze.hxx> |
||||
namespace proxy { |
||||
typedef Windoze Face; |
||||
} |
||||
#else |
||||
// normal operating systems: use http://code.google.com/p/libproxy
|
||||
#include <proxyface/unix.hxx> |
||||
namespace proxy { |
||||
typedef Unix Face; |
||||
} |
||||
#endif |
||||
|
||||
#endif |
@ -0,0 +1,19 @@ |
||||
#! /bin/sh |
||||
|
||||
## @file |
||||
## |
||||
## $Id$ |
||||
## |
||||
## $Date: 2004/08/31 15:57:19 $ |
||||
## $Author: marc $ |
||||
## |
||||
## @copy © Marc Wäckerlin |
||||
## @license LGPL, see file <a href="license.html">COPYING</a> |
||||
## |
||||
## $Log: bootstrap.sh,v $ |
||||
## Revision 1.3 2004/08/31 15:57:19 marc |
||||
## added file header |
||||
## |
||||
|
||||
test -f makefile && make distclean |
||||
aclocal && libtoolize --force && automake -a && autoconf |
@ -0,0 +1,3 @@ |
||||
AC_INIT |
||||
AC_CONFIG_FILES([Makefile]) |
||||
AC_OUTPUT |
@ -0,0 +1 @@ |
||||
include_HEADERS = proxyface.hxx proxyface/unix.hxx proxyface/windoze.hxx |
@ -0,0 +1,62 @@ |
||||
/*! @file
|
||||
|
||||
@id $Id$ |
||||
*/ |
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#ifndef PROXY_LINUX |
||||
#define PROXY_LINUX |
||||
|
||||
extern "C" { |
||||
#include <proxy.h> |
||||
} |
||||
|
||||
#include <iostream> // debug |
||||
|
||||
namespace proxy { |
||||
|
||||
class Unix: Interface { |
||||
|
||||
public: |
||||
|
||||
Unix(): _factory(px_proxy_factory_new()) {} |
||||
|
||||
virtual ~Unix() { |
||||
px_proxy_factory_free(_factory); |
||||
} |
||||
|
||||
//! Implemented using http://code.google.com/p/libproxy/
|
||||
virtual List proxies(const std::string& url) { |
||||
List res; |
||||
char** proxies(px_proxy_factory_get_proxies |
||||
(_factory, const_cast<char*>(url.data()))); |
||||
for (int i(0); proxies[i]; ++i) { |
||||
std::string proxy(proxies[i]); |
||||
Type type(proxy.find("http://")==0?HTTP: |
||||
(proxy.find("socks://")==0?SOCKS:DIRECT)); |
||||
if (proxy.find("://")!=std::string::npos) |
||||
proxy=proxy.substr(proxy.find("://")+3); |
||||
std::string::size_type oldpos(0); |
||||
std::string port((oldpos=proxy.rfind(":"))!=std::string::npos |
||||
?proxy.substr(oldpos+1):std::string()); |
||||
std::string host(proxy.substr(0, proxy.rfind(":"))); |
||||
std::cout<<"Proxy found: "<<host<<", port="<<port<<", type"<<type |
||||
<<std::endl; |
||||
res.push_back(std::make_pair(type, std::make_pair(host, port))); |
||||
} |
||||
for (int i(0); proxies[i]; ++i) |
||||
delete proxies[i]; |
||||
delete proxies; |
||||
return res; |
||||
} |
||||
|
||||
private: |
||||
|
||||
pxProxyFactory* _factory; |
||||
|
||||
}; |
||||
|
||||
} |
||||
|
||||
#endif |
@ -0,0 +1,92 @@ |
||||
/*! @file
|
||||
|
||||
@id $Id$ |
||||
*/ |
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#ifndef PROXY_WINDOZE_HXX |
||||
#define PROXY_WINDOZE_HXX |
||||
|
||||
#include <windows.h> |
||||
#include <winhttp.h> |
||||
|
||||
#include <iostream> // debug |
||||
|
||||
namespace proxy { |
||||
|
||||
class Windoze: Interface { |
||||
|
||||
public: |
||||
|
||||
Windoze() {} |
||||
|
||||
virtual ~Windoze() {} |
||||
|
||||
//! Implemented using M$ WinHTTP
|
||||
virtual List proxies(const std::string& url) { |
||||
|
||||
List res; |
||||
WINHTTP_PROXY_INFO proxyInfo; |
||||
ZeroMemory(&proxyInfo, sizeof(proxyInfo)); |
||||
HINTERNET hHttpSession(WinHttpOpen(L"WinHTTP AutoProxy/1.0", |
||||
WINHTTP_ACCESS_TYPE_NO_PROXY, |
||||
WINHTTP_NO_PROXY_NAME, |
||||
WINHTTP_NO_PROXY_BYPASS, |
||||
0)); |
||||
try { |
||||
if (!hHttpSession) throw exc::error(); |
||||
|
||||
WINHTTP_AUTOPROXY_OPTIONS AutoProxyOptions; |
||||
ZeroMemory(&AutoProxyOptions, sizeof(AutoProxyOptions)); |
||||
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT; |
||||
AutoProxyOptions.dwAutoDetectFlags = |
||||
WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A; |
||||
AutoProxyOptions.fAutoLogonIfChallenged = FALSE; |
||||
|
||||
if (!WinHttpGetProxyForUrl(hHttpSession, |
||||
std::wstring(url.begin(), url.end()).data(), |
||||
&AutoProxyOptions, &proxyInfo)) |
||||
throw exc::error(); |
||||
|
||||
std::wstring list(proxyInfo.lpszProxy); |
||||
std::wcout<<"LIST=\""<<list<<"\""<<std::endl; |
||||
for (std::wstring::size_type oldpos(0), pos(0); |
||||
(pos=list.find(L";", oldpos))!=std::wstring::npos; |
||||
oldpos=++pos) { |
||||
std::wstring proxy(list.substr(oldpos, pos-oldpos)); |
||||
std::wstring port((oldpos=proxy.rfind(L":"))!=std::wstring::npos |
||||
?proxy.substr(oldpos+1):std::wstring()); |
||||
std::wstring host(proxy.substr(0, proxy.rfind(L":"))); |
||||
Type type(DIRECT); |
||||
switch (proxyInfo.dwAccessType) { //! @todo use or remove
|
||||
case WINHTTP_ACCESS_TYPE_NO_PROXY: type=DIRECT; break; |
||||
case WINHTTP_ACCESS_TYPE_DEFAULT_PROXY: type=DEFAULT; break; |
||||
case WINHTTP_ACCESS_TYPE_NAMED_PROXY: type=HTTP; break; |
||||
} |
||||
res.push_back |
||||
(std::make_pair(type, |
||||
std::make_pair |
||||
(std::string(host.begin(), host.end()), |
||||
std::string(port.begin(), port.end())))); |
||||
} |
||||
if (!res.size()) |
||||
res.push_back |
||||
(std::make_pair(DIRECT, |
||||
std::make_pair(std::string(), std::string()))); |
||||
|
||||
if (proxyInfo.lpszProxy) GlobalFree(proxyInfo.lpszProxy); |
||||
if (proxyInfo.lpszProxyBypass) GlobalFree(proxyInfo.lpszProxyBypass); |
||||
if (hHttpSession) WinHttpCloseHandle(hHttpSession); |
||||
return res; |
||||
} catch (...) { |
||||
if (proxyInfo.lpszProxy) GlobalFree(proxyInfo.lpszProxy); |
||||
if (proxyInfo.lpszProxyBypass) GlobalFree(proxyInfo.lpszProxyBypass); |
||||
if (hHttpSession) WinHttpCloseHandle(hHttpSession); |
||||
throw; |
||||
} |
||||
} |
||||
}; |
||||
} |
||||
|
||||
#endif |
@ -0,0 +1,53 @@ |
||||
/*! @file
|
||||
|
||||
|
||||
Compile for Linux: |
||||
|
||||
@code |
||||
g++ test.cxx -I. -lproxy -ldl |
||||
@endcode |
||||
|
||||
On Linux: install http://code.google.com/p/libproxy/
|
||||
|
||||
@code |
||||
./configure --without-gnome --without-kde \
|
||||
--without-webkit --without-mozjs \
|
||||
--without-networkmanager --without-python |
||||
make && sudo make install |
||||
@endcode |
||||
|
||||
Cross-Compile for Windoze: |
||||
|
||||
@code |
||||
i586-mingw32msvc-g++ test.cxx \
|
||||
_I . -I ~/.wine/drive_c/MicrosoftSDK/include \
|
||||
winhttp.a |
||||
@endcode |
||||
|
||||
@id $Id$ |
||||
*/ |
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#include <autoproxy.hxx> |
||||
#include <iostream> |
||||
|
||||
int main(int argc, char** argv) try { |
||||
proxy::Face detect; |
||||
while (++argv, --argc) { |
||||
std::cout<<"detecting proxies for url: "<<*argv<<std::endl; |
||||
for (proxy::List p(detect.proxies(*argv)); |
||||
p.size(); |
||||
p.pop_front()) |
||||
std::cout<<" -> found " |
||||
<<(p.begin()->first==proxy::DIRECT?"DIRECT" |
||||
:(p.begin()->first==proxy::DEFAULT?"DEFAULT" |
||||
:(p.begin()->first==proxy::HTTP?"HTTP" |
||||
:(p.begin()->first==proxy::SOCKS?"SOCKS":"**ERROR**")))) |
||||
<<" proxy host: "<<p.begin()->second.first |
||||
<<" (Port: "<<p.begin()->second.second<<")"<<std::endl; |
||||
} |
||||
return 0; |
||||
} catch(std::exception& x) { |
||||
std::cerr<<" **** Exception: "<<x.what()<<std::endl; |
||||
} |
Loading…
Reference in new issue