Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Titanium-Web-Proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Titanium-Web-Proxy
Commits
89710bf8
Commit
89710bf8
authored
Jun 06, 2017
by
justcoding121
Committed by
justcoding121
Jun 06, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #257 from honfika/develop
Configure system proxy in one step #256
parents
d42864ec
29a3ecc1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
165 additions
and
91 deletions
+165
-91
ProxyTestController.cs
.../Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
+4
-2
SystemProxy.cs
Titanium.Web.Proxy/Helpers/SystemProxy.cs
+85
-49
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+76
-40
No files found.
Examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
View file @
89710bf8
...
...
@@ -4,6 +4,7 @@ using System.Net;
using
System.Net.Security
;
using
System.Threading.Tasks
;
using
Titanium.Web.Proxy.EventArguments
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Models
;
namespace
Titanium.Web.Proxy.Examples.Basic
...
...
@@ -92,8 +93,9 @@ namespace Titanium.Web.Proxy.Examples.Basic
endPoint
.
GetType
().
Name
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
//Only explicit proxies can be set as system proxy!
proxyServer
.
SetAsSystemHttpProxy
(
explicitEndPoint
);
proxyServer
.
SetAsSystemHttpsProxy
(
explicitEndPoint
);
//proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
//proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
proxyServer
.
SetAsSystemProxy
(
explicitEndPoint
,
ProxyProtocolType
.
AllHttp
);
}
public
void
Stop
()
...
...
Titanium.Web.Proxy/Helpers/SystemProxy.cs
View file @
89710bf8
...
...
@@ -8,10 +8,28 @@ using Microsoft.Win32;
// Helper classes for setting system proxy settings
namespace
Titanium.Web.Proxy.Helpers
{
internal
enum
ProxyProtocolType
[
Flags
]
public
enum
ProxyProtocolType
{
Http
,
Https
,
/// <summary>
/// The none
/// </summary>
None
=
0
,
/// <summary>
/// HTTP
/// </summary>
Http
=
1
,
/// <summary>
/// HTTPS
/// </summary>
Https
=
2
,
/// <summary>
/// Both HTTP and HTTPS
/// </summary>
AllHttp
=
Http
|
Https
,
}
internal
partial
class
NativeMethods
...
...
@@ -25,11 +43,23 @@ namespace Titanium.Web.Proxy.Helpers
{
internal
string
HostName
{
get
;
set
;
}
internal
int
Port
{
get
;
set
;
}
internal
bool
IsHttps
{
get
;
set
;
}
internal
ProxyProtocolType
ProtocolType
{
get
;
set
;
}
public
override
string
ToString
()
{
return
$"
{(
IsHttps
?
"https"
:
"http"
)}
=
{
HostName
}
:
{
Port
}
"
;
string
protocol
=
null
;
switch
(
ProtocolType
)
{
case
ProxyProtocolType
.
Http
:
protocol
=
"http"
;
break
;
case
ProxyProtocolType
.
Https
:
protocol
=
"https"
;
break
;
default
:
throw
new
Exception
(
"Unsupported protocol type"
);
}
return
$"
{
protocol
}
=
{
HostName
}
:
{
Port
}
"
;
}
}
...
...
@@ -41,38 +71,13 @@ namespace Titanium.Web.Proxy.Helpers
internal
const
int
InternetOptionSettingsChanged
=
39
;
internal
const
int
InternetOptionRefresh
=
37
;
internal
void
SetHttpProxy
(
string
hostname
,
int
port
)
{
SetProxy
(
hostname
,
port
,
ProxyProtocolType
.
Http
);
}
/// <summary>
/// Remove the http proxy setting from current machine
/// </summary>
internal
void
RemoveHttpProxy
()
{
RemoveProxy
(
ProxyProtocolType
.
Http
);
}
/// <summary>
/// Set the HTTPS proxy server for current machine
/// Set the HTTP
and/or HTTP
S proxy server for current machine
/// </summary>
/// <param name="hostname"></param>
/// <param name="port"></param>
internal
void
SetHttpsProxy
(
string
hostname
,
int
port
)
{
SetProxy
(
hostname
,
port
,
ProxyProtocolType
.
Https
);
}
/// <summary>
/// Removes the https proxy setting to nothing
/// </summary>
internal
void
RemoveHttpsProxy
()
{
RemoveProxy
(
ProxyProtocolType
.
Https
);
}
private
void
SetProxy
(
string
hostname
,
int
port
,
ProxyProtocolType
protocolType
)
/// <param name="protocolType"></param>
internal
void
SetProxy
(
string
hostname
,
int
port
,
ProxyProtocolType
protocolType
)
{
var
reg
=
Registry
.
CurrentUser
.
OpenSubKey
(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
,
true
);
...
...
@@ -83,13 +88,26 @@ namespace Titanium.Web.Proxy.Helpers
var
exisitingContent
=
reg
.
GetValue
(
"ProxyServer"
)
as
string
;
var
existingSystemProxyValues
=
GetSystemProxyValues
(
exisitingContent
);
existingSystemProxyValues
.
RemoveAll
(
x
=>
protocolType
==
ProxyProtocolType
.
Https
?
x
.
IsHttps
:
!
x
.
IsHttps
);
existingSystemProxyValues
.
RemoveAll
(
x
=>
(
protocolType
&
x
.
ProtocolType
)
!=
0
);
if
((
protocolType
&
ProxyProtocolType
.
Http
)
!=
0
)
{
existingSystemProxyValues
.
Add
(
new
HttpSystemProxyValue
{
HostName
=
hostname
,
ProtocolType
=
ProxyProtocolType
.
Http
,
Port
=
port
});
}
if
((
protocolType
&
ProxyProtocolType
.
Https
)
!=
0
)
{
existingSystemProxyValues
.
Add
(
new
HttpSystemProxyValue
{
HostName
=
hostname
,
IsHttps
=
protocolType
=
=
ProxyProtocolType
.
Https
,
ProtocolType
=
ProxyProtocolType
.
Https
,
Port
=
port
});
}
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyServer"
,
string
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
...
...
@@ -98,7 +116,10 @@ namespace Titanium.Web.Proxy.Helpers
Refresh
();
}
private
void
RemoveProxy
(
ProxyProtocolType
protocolType
)
/// <summary>
/// Remove the HTTP and/or HTTPS proxy setting from current machine
/// </summary>
internal
void
RemoveProxy
(
ProxyProtocolType
protocolType
)
{
var
reg
=
Registry
.
CurrentUser
.
OpenSubKey
(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
,
true
);
...
...
@@ -107,7 +128,7 @@ namespace Titanium.Web.Proxy.Helpers
var
exisitingContent
=
reg
.
GetValue
(
"ProxyServer"
)
as
string
;
var
existingSystemProxyValues
=
GetSystemProxyValues
(
exisitingContent
);
existingSystemProxyValues
.
RemoveAll
(
x
=>
protocolType
==
ProxyProtocolType
.
Https
?
x
.
IsHttps
:
!
x
.
IsHttps
);
existingSystemProxyValues
.
RemoveAll
(
x
=>
(
protocolType
&
x
.
ProtocolType
)
!=
0
);
if
(
existingSystemProxyValues
.
Count
!=
0
)
{
...
...
@@ -176,18 +197,33 @@ namespace Titanium.Web.Proxy.Helpers
/// <returns></returns>
private
HttpSystemProxyValue
ParseProxyValue
(
string
value
)
{
var
tmp
=
Regex
.
Replace
(
value
,
@"\s+"
,
" "
).
Trim
().
ToLower
();
var
tmp
=
Regex
.
Replace
(
value
,
@"\s+"
,
" "
).
Trim
();
int
equalsIndex
=
tmp
.
IndexOf
(
"="
,
StringComparison
.
InvariantCulture
);
if
(
equalsIndex
>=
0
)
{
string
protocolTypeStr
=
tmp
.
Substring
(
0
,
equalsIndex
);
ProxyProtocolType
?
protocolType
=
null
;
if
(
protocolTypeStr
.
Equals
(
"http"
,
StringComparison
.
InvariantCultureIgnoreCase
))
{
protocolType
=
ProxyProtocolType
.
Http
;
}
else
if
(
protocolTypeStr
.
Equals
(
"https"
,
StringComparison
.
InvariantCultureIgnoreCase
))
{
protocolType
=
ProxyProtocolType
.
Https
;
}
if
(
tmp
.
StartsWith
(
"http="
)
||
tmp
.
StartsWith
(
"https="
)
)
if
(
protocolType
.
HasValue
)
{
var
endPoint
=
tmp
.
Substring
(
5
);
var
endPointParts
=
tmp
.
Substring
(
equalsIndex
+
1
).
Split
(
':'
);
return
new
HttpSystemProxyValue
{
HostName
=
endPoint
.
Split
(
':'
)
[
0
],
Port
=
int
.
Parse
(
endPoint
.
Split
(
':'
)
[
1
]),
IsHttps
=
tmp
.
StartsWith
(
"https="
)
HostName
=
endPointParts
[
0
],
Port
=
int
.
Parse
(
endPointParts
[
1
]),
ProtocolType
=
protocolType
.
Value
,
};
}
}
return
null
;
}
...
...
Titanium.Web.Proxy/ProxyServer.cs
View file @
89710bf8
...
...
@@ -348,32 +348,25 @@ namespace Titanium.Web.Proxy
/// <param name="endPoint"></param>
public
void
SetAsSystemHttpProxy
(
ExplicitProxyEndPoint
endPoint
)
{
if
(
RunTime
.
IsRunningOnMono
)
{
throw
new
Exception
(
"Mono Runtime do not support system proxy settings."
);
SetAsSystemProxy
(
endPoint
,
ProxyProtocolType
.
Http
);
}
ValidateEndPointAsSystemProxy
(
endPoint
);
//clear any settings previously added
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>().
ToList
().
ForEach
(
x
=>
x
.
IsSystemHttpProxy
=
false
);
systemProxySettingsManager
.
SetHttpProxy
(
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Any
)
|
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Loopback
)
?
"127.0.0.1"
:
endPoint
.
IpAddress
.
ToString
(),
endPoint
.
Port
);
endPoint
.
IsSystemHttpProxy
=
true
;
firefoxProxySettingsManager
.
UseSystemProxy
();
Console
.
WriteLine
(
"Set endpoint at Ip {0} and port: {1} as System HTTP Proxy"
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
/// <summary>
/// Set the given explicit end point as the default proxy server for current machine
/// </summary>
/// <param name="endPoint"></param>
public
void
SetAsSystemHttpsProxy
(
ExplicitProxyEndPoint
endPoint
)
{
SetAsSystemProxy
(
endPoint
,
ProxyProtocolType
.
Https
);
}
/// <summary>
/// Set the given explicit end point as the default proxy server for current machine
/// </summary>
/// <param name="endPoint"></param>
public
void
SetAsSystemHttpsProxy
(
ExplicitProxyEndPoint
endPoint
)
/// <param name="protocolType"></param>
public
void
SetAsSystemProxy
(
ExplicitProxyEndPoint
endPoint
,
ProxyProtocolType
protocolType
)
{
if
(
RunTime
.
IsRunningOnMono
)
{
...
...
@@ -382,33 +375,73 @@ namespace Titanium.Web.Proxy
ValidateEndPointAsSystemProxy
(
endPoint
);
bool
isHttp
=
(
protocolType
&
ProxyProtocolType
.
Http
)
>
0
;
bool
isHttps
=
(
protocolType
&
ProxyProtocolType
.
Https
)
>
0
;
if
(
isHttps
)
{
if
(!
endPoint
.
EnableSsl
)
{
throw
new
Exception
(
"Endpoint do not support Https connections"
);
}
//clear any settings previously added
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>()
.
ToList
()
.
ForEach
(
x
=>
x
.
IsSystemHttpsProxy
=
false
);
EnsureRootCertificate
();
//If certificate was trusted by the machine
if
(
CertificateManager
.
CertValidated
)
if
(!
CertificateManager
.
CertValidated
)
{
protocolType
=
protocolType
&
~
ProxyProtocolType
.
Https
;
isHttps
=
false
;
}
}
//clear any settings previously added
if
(
isHttp
)
{
systemProxySettingsManager
.
SetHttpsProxy
(
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>().
ToList
().
ForEach
(
x
=>
x
.
IsSystemHttpProxy
=
false
);
}
if
(
isHttps
)
{
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>().
ToList
().
ForEach
(
x
=>
x
.
IsSystemHttpsProxy
=
false
);
}
systemProxySettingsManager
.
SetProxy
(
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Any
)
|
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Loopback
)
?
"127.0.0.1"
:
endPoint
.
IpAddress
.
ToString
(),
endPoint
.
Port
);
}
endPoint
.
Port
,
protocolType
);
if
(
isHttp
)
{
endPoint
.
IsSystemHttpsProxy
=
true
;
}
if
(
isHttps
)
{
endPoint
.
IsSystemHttpsProxy
=
true
;
}
firefoxProxySettingsManager
.
UseSystemProxy
();
Console
.
WriteLine
(
"Set endpoint at Ip {0} and port: {1} as System HTTPS Proxy"
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
string
proxyType
=
null
;
switch
(
protocolType
)
{
case
ProxyProtocolType
.
Http
:
proxyType
=
"HTTP"
;
break
;
case
ProxyProtocolType
.
Https
:
proxyType
=
"HTTPS"
;
break
;
case
ProxyProtocolType
.
AllHttp
:
proxyType
=
"HTTP and HTTPS"
;
break
;
}
if
(
protocolType
!=
ProxyProtocolType
.
None
)
{
Console
.
WriteLine
(
"Set endpoint at Ip {0} and port: {1} as System {2} Proxy"
,
endPoint
.
IpAddress
,
endPoint
.
Port
,
proxyType
);
}
}
/// <summary>
...
...
@@ -416,25 +449,28 @@ namespace Titanium.Web.Proxy
/// </summary>
public
void
DisableSystemHttpProxy
()
{
if
(
RunTime
.
IsRunningOnMono
)
{
throw
new
Exception
(
"Mono Runtime do not support system proxy settings."
);
}
systemProxySettingsManager
.
RemoveHttpProxy
();
DisableSystemProxy
(
ProxyProtocolType
.
Http
);
}
/// <summary>
/// Remove any HTTPS proxy setting for current machine
/// </summary>
public
void
DisableSystemHttpsProxy
()
{
DisableSystemProxy
(
ProxyProtocolType
.
Https
);
}
/// <summary>
/// Remove the specified proxy settings for current machine
/// </summary>
public
void
DisableSystemProxy
(
ProxyProtocolType
protocolType
)
{
if
(
RunTime
.
IsRunningOnMono
)
{
throw
new
Exception
(
"Mono Runtime do not support system proxy settings."
);
}
systemProxySettingsManager
.
Remove
HttpsProxy
(
);
systemProxySettingsManager
.
Remove
Proxy
(
protocolType
);
}
/// <summary>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment