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
5402cb90
Commit
5402cb90
authored
Jun 07, 2017
by
justcoding121
Committed by
justcoding121
Jun 07, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #260 from honfika/develop
Restore proxy settings on proxy stop/exit
parents
89710bf8
cdc148c0
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
120 additions
and
60 deletions
+120
-60
Program.cs
Examples/Titanium.Web.Proxy.Examples.Basic/Program.cs
+0
-33
SystemProxy.cs
Titanium.Web.Proxy/Helpers/SystemProxy.cs
+108
-20
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+12
-7
No files found.
Examples/Titanium.Web.Proxy.Examples.Basic/Program.cs
View file @
5402cb90
...
@@ -14,10 +14,6 @@ namespace Titanium.Web.Proxy.Examples.Basic
...
@@ -14,10 +14,6 @@ namespace Titanium.Web.Proxy.Examples.Basic
//fix console hang due to QuickEdit mode
//fix console hang due to QuickEdit mode
ConsoleHelper
.
DisableQuickEditMode
();
ConsoleHelper
.
DisableQuickEditMode
();
//On Console exit make sure we also exit the proxy
NativeMethods
.
Handler
=
ConsoleEventCallback
;
NativeMethods
.
SetConsoleCtrlHandler
(
NativeMethods
.
Handler
,
true
);
//Start proxy controller
//Start proxy controller
controller
.
StartProxy
();
controller
.
StartProxy
();
...
@@ -27,34 +23,5 @@ namespace Titanium.Web.Proxy.Examples.Basic
...
@@ -27,34 +23,5 @@ namespace Titanium.Web.Proxy.Examples.Basic
controller
.
Stop
();
controller
.
Stop
();
}
}
private
static
bool
ConsoleEventCallback
(
int
eventType
)
{
if
(
eventType
!=
2
)
return
false
;
try
{
controller
.
Stop
();
}
catch
{
// ignored
}
return
false
;
}
}
internal
static
class
NativeMethods
{
// Keeps it from getting garbage collected
internal
static
ConsoleEventDelegate
Handler
;
[
DllImport
(
"kernel32.dll"
,
SetLastError
=
true
)]
internal
static
extern
bool
SetConsoleCtrlHandler
(
ConsoleEventDelegate
callback
,
bool
add
);
// Pinvoke
internal
delegate
bool
ConsoleEventDelegate
(
int
eventType
);
}
}
}
}
Titanium.Web.Proxy/Helpers/SystemProxy.cs
View file @
5402cb90
...
@@ -37,6 +37,18 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -37,6 +37,18 @@ namespace Titanium.Web.Proxy.Helpers
[
DllImport
(
"wininet.dll"
)]
[
DllImport
(
"wininet.dll"
)]
internal
static
extern
bool
InternetSetOption
(
IntPtr
hInternet
,
int
dwOption
,
IntPtr
lpBuffer
,
internal
static
extern
bool
InternetSetOption
(
IntPtr
hInternet
,
int
dwOption
,
IntPtr
lpBuffer
,
int
dwBufferLength
);
int
dwBufferLength
);
[
DllImport
(
"kernel32.dll"
)]
internal
static
extern
IntPtr
GetConsoleWindow
();
// Keeps it from getting garbage collected
internal
static
ConsoleEventDelegate
Handler
;
[
DllImport
(
"kernel32.dll"
,
SetLastError
=
true
)]
internal
static
extern
bool
SetConsoleCtrlHandler
(
ConsoleEventDelegate
callback
,
bool
add
);
// Pinvoke
internal
delegate
bool
ConsoleEventDelegate
(
int
eventType
);
}
}
internal
class
HttpSystemProxyValue
internal
class
HttpSystemProxyValue
...
@@ -47,7 +59,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -47,7 +59,7 @@ namespace Titanium.Web.Proxy.Helpers
public
override
string
ToString
()
public
override
string
ToString
()
{
{
string
protocol
=
null
;
string
protocol
;
switch
(
ProtocolType
)
switch
(
ProtocolType
)
{
{
case
ProxyProtocolType
.
Http
:
case
ProxyProtocolType
.
Http
:
...
@@ -59,6 +71,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -59,6 +71,7 @@ namespace Titanium.Web.Proxy.Helpers
default
:
default
:
throw
new
Exception
(
"Unsupported protocol type"
);
throw
new
Exception
(
"Unsupported protocol type"
);
}
}
return
$"
{
protocol
}
=
{
HostName
}
:
{
Port
}
"
;
return
$"
{
protocol
}
=
{
HostName
}
:
{
Port
}
"
;
}
}
}
}
...
@@ -71,6 +84,31 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -71,6 +84,31 @@ namespace Titanium.Web.Proxy.Helpers
internal
const
int
InternetOptionSettingsChanged
=
39
;
internal
const
int
InternetOptionSettingsChanged
=
39
;
internal
const
int
InternetOptionRefresh
=
37
;
internal
const
int
InternetOptionRefresh
=
37
;
private
bool
originalValuesLoaded
;
private
int
?
originalProxyEnable
;
private
string
originalProxyServer
;
public
SystemProxyManager
()
{
AppDomain
.
CurrentDomain
.
ProcessExit
+=
(
o
,
args
)
=>
RestoreOriginalSettings
();
if
(
Environment
.
UserInteractive
&&
NativeMethods
.
GetConsoleWindow
()
!=
IntPtr
.
Zero
)
{
NativeMethods
.
Handler
=
eventType
=>
{
if
(
eventType
!=
2
)
{
return
false
;
}
RestoreOriginalSettings
();
return
false
;
};
//On Console exit make sure we also exit the proxy
NativeMethods
.
SetConsoleCtrlHandler
(
NativeMethods
.
Handler
,
true
);
}
}
/// <summary>
/// <summary>
/// Set the HTTP and/or HTTPS proxy server for current machine
/// Set the HTTP and/or HTTPS proxy server for current machine
/// </summary>
/// </summary>
...
@@ -84,6 +122,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -84,6 +122,7 @@ namespace Titanium.Web.Proxy.Helpers
if
(
reg
!=
null
)
if
(
reg
!=
null
)
{
{
SaveOriginalProxyConfiguration
(
reg
);
PrepareRegistry
(
reg
);
PrepareRegistry
(
reg
);
var
exisitingContent
=
reg
.
GetValue
(
"ProxyServer"
)
as
string
;
var
exisitingContent
=
reg
.
GetValue
(
"ProxyServer"
)
as
string
;
...
@@ -111,9 +150,9 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -111,9 +150,9 @@ namespace Titanium.Web.Proxy.Helpers
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyServer"
,
string
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
reg
.
SetValue
(
"ProxyServer"
,
string
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
}
Refresh
();
Refresh
();
}
}
}
/// <summary>
/// <summary>
...
@@ -123,26 +162,31 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -123,26 +162,31 @@ namespace Titanium.Web.Proxy.Helpers
{
{
var
reg
=
Registry
.
CurrentUser
.
OpenSubKey
(
var
reg
=
Registry
.
CurrentUser
.
OpenSubKey
(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
,
true
);
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
,
true
);
if
(
reg
?.
GetValue
(
"ProxyServer"
)
!=
null
)
if
(
reg
!=
null
)
{
{
var
exisitingContent
=
reg
.
GetValue
(
"ProxyServer"
)
as
string
;
SaveOriginalProxyConfiguration
(
reg
);
var
existingSystemProxyValues
=
GetSystemProxyValues
(
exisitingContent
);
existingSystemProxyValues
.
RemoveAll
(
x
=>
(
protocolType
&
x
.
ProtocolType
)
!=
0
);
if
(
existingSystemProxyValues
.
Count
!=
0
)
if
(
reg
.
GetValue
(
"ProxyServer"
)
!=
null
)
{
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyServer"
,
string
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
}
else
{
{
reg
.
SetValue
(
"ProxyEnable"
,
0
);
var
exisitingContent
=
reg
.
GetValue
(
"ProxyServer"
)
as
string
;
reg
.
SetValue
(
"ProxyServer"
,
string
.
Empty
);
var
existingSystemProxyValues
=
GetSystemProxyValues
(
exisitingContent
);
existingSystemProxyValues
.
RemoveAll
(
x
=>
(
protocolType
&
x
.
ProtocolType
)
!=
0
);
if
(
existingSystemProxyValues
.
Count
!=
0
)
{
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyServer"
,
string
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
}
else
{
reg
.
SetValue
(
"ProxyEnable"
,
0
);
reg
.
SetValue
(
"ProxyServer"
,
string
.
Empty
);
}
}
}
}
Refresh
();
Refresh
();
}
}
}
/// <summary>
/// <summary>
...
@@ -155,11 +199,55 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -155,11 +199,55 @@ namespace Titanium.Web.Proxy.Helpers
if
(
reg
!=
null
)
if
(
reg
!=
null
)
{
{
SaveOriginalProxyConfiguration
(
reg
);
reg
.
SetValue
(
"ProxyEnable"
,
0
);
reg
.
SetValue
(
"ProxyEnable"
,
0
);
reg
.
SetValue
(
"ProxyServer"
,
string
.
Empty
);
reg
.
SetValue
(
"ProxyServer"
,
string
.
Empty
);
Refresh
();
}
}
internal
void
RestoreOriginalSettings
()
{
if
(!
originalValuesLoaded
)
{
return
;
}
var
reg
=
Registry
.
CurrentUser
.
OpenSubKey
(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
,
true
);
if
(
reg
!=
null
)
{
if
(
originalProxyEnable
.
HasValue
)
{
reg
.
SetValue
(
"ProxyEnable"
,
originalProxyEnable
.
Value
);
}
else
if
(
reg
.
GetValue
(
"ProxyEnable"
)
!=
null
)
{
reg
.
DeleteValue
(
"ProxyEnable"
);
}
if
(
originalProxyServer
!=
null
)
{
reg
.
SetValue
(
"ProxyServer"
,
originalProxyServer
);
}
else
if
(
reg
.
GetValue
(
"ProxyServer"
)
!=
null
)
{
reg
.
DeleteValue
(
"ProxyServer"
);
}
originalValuesLoaded
=
false
;
Refresh
();
}
}
}
Refresh
();
private
void
SaveOriginalProxyConfiguration
(
RegistryKey
reg
)
{
originalProxyServer
=
reg
.
GetValue
(
"ProxyServer"
)
as
string
;
originalProxyEnable
=
reg
.
GetValue
(
"ProxyEnable"
)
as
int
?;
originalValuesLoaded
=
true
;
}
}
/// <summary>
/// <summary>
...
@@ -239,7 +327,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -239,7 +327,7 @@ namespace Titanium.Web.Proxy.Helpers
reg
.
SetValue
(
"ProxyEnable"
,
0
);
reg
.
SetValue
(
"ProxyEnable"
,
0
);
}
}
if
(
reg
.
GetValue
(
"ProxyServer"
)
==
null
||
reg
.
GetValue
(
"ProxyEnable"
)
as
string
==
"0"
)
if
(
reg
.
GetValue
(
"ProxyServer"
)
==
null
||
reg
.
GetValue
(
"ProxyEnable"
)
as
int
?
==
0
)
{
{
reg
.
SetValue
(
"ProxyServer"
,
string
.
Empty
);
reg
.
SetValue
(
"ProxyServer"
,
string
.
Empty
);
}
}
...
...
Titanium.Web.Proxy/ProxyServer.cs
View file @
5402cb90
...
@@ -287,9 +287,11 @@ namespace Titanium.Web.Proxy
...
@@ -287,9 +287,11 @@ namespace Titanium.Web.Proxy
ProxyEndPoints
=
new
List
<
ProxyEndPoint
>();
ProxyEndPoints
=
new
List
<
ProxyEndPoint
>();
tcpConnectionFactory
=
new
TcpConnectionFactory
();
tcpConnectionFactory
=
new
TcpConnectionFactory
();
systemProxySettingsManager
=
new
SystemProxyManager
();
if
(!
RunTime
.
IsRunningOnMono
)
{
systemProxySettingsManager
=
new
SystemProxyManager
();
}
CertificateManager
=
new
CertificateManager
(
ExceptionFunc
);
CertificateManager
=
new
CertificateManager
(
ExceptionFunc
);
if
(
rootCertificateName
!=
null
)
if
(
rootCertificateName
!=
null
)
{
{
...
@@ -530,11 +532,14 @@ namespace Titanium.Web.Proxy
...
@@ -530,11 +532,14 @@ namespace Titanium.Web.Proxy
throw
new
Exception
(
"Proxy is not running."
);
throw
new
Exception
(
"Proxy is not running."
);
}
}
var
setAsSystemProxy
=
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>().
Any
(
x
=>
x
.
IsSystemHttpProxy
||
x
.
IsSystemHttpsProxy
);
if
(!
RunTime
.
IsRunningOnMono
)
if
(
setAsSystemProxy
)
{
{
systemProxySettingsManager
.
DisableAllProxy
();
var
setAsSystemProxy
=
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>().
Any
(
x
=>
x
.
IsSystemHttpProxy
||
x
.
IsSystemHttpsProxy
);
if
(
setAsSystemProxy
)
{
systemProxySettingsManager
.
RestoreOriginalSettings
();
}
}
}
foreach
(
var
endPoint
in
ProxyEndPoints
)
foreach
(
var
endPoint
in
ProxyEndPoints
)
...
...
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