2013-11-03 23:53:49 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2014-05-13 21:06:40 +12:00
|
|
|
|
Copyright (C) 2007-2014 ShareX Developers
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endregion License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
using HelpersLib;
|
|
|
|
|
using Starksoft.Net.Proxy;
|
2013-12-05 01:55:58 +13:00
|
|
|
|
using System;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Net;
|
2013-12-05 01:55:58 +13:00
|
|
|
|
using System.Reflection;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-05-10 13:52:09 +12:00
|
|
|
|
namespace HelpersLib
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
public class ProxyInfo
|
|
|
|
|
{
|
2013-12-04 21:54:32 +13:00
|
|
|
|
private static ProxyInfo current;
|
|
|
|
|
|
|
|
|
|
public static ProxyInfo Current
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (current == null)
|
|
|
|
|
{
|
|
|
|
|
current = new ProxyInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return current;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
current = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public ProxyMethod ProxyMethod { get; set; }
|
2013-12-05 01:55:58 +13:00
|
|
|
|
public ProxyType ProxyType { get; set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public string Host { get; set; }
|
|
|
|
|
public int Port { get; set; }
|
2013-12-05 01:55:58 +13:00
|
|
|
|
public string Username { get; set; }
|
|
|
|
|
public string Password { get; set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
public ProxyInfo()
|
|
|
|
|
{
|
|
|
|
|
ProxyMethod = ProxyMethod.Manual;
|
|
|
|
|
ProxyType = ProxyType.HTTP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsValidProxy()
|
|
|
|
|
{
|
|
|
|
|
if (ProxyMethod == ProxyMethod.Manual)
|
|
|
|
|
{
|
|
|
|
|
return !string.IsNullOrEmpty(Host) && Port > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ProxyMethod == ProxyMethod.Automatic)
|
|
|
|
|
{
|
2013-12-05 01:55:58 +13:00
|
|
|
|
WebProxy systemProxy = GetDefaultWebProxy();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
if (systemProxy != null && systemProxy.Address != null && !string.IsNullOrEmpty(systemProxy.Address.Host) && systemProxy.Address.Port > 0)
|
|
|
|
|
{
|
|
|
|
|
Host = systemProxy.Address.Host;
|
|
|
|
|
Port = systemProxy.Address.Port;
|
|
|
|
|
ProxyType = ProxyType.HTTP;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-05 01:55:58 +13:00
|
|
|
|
// Proxy for HTTP
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public IWebProxy GetWebProxy()
|
|
|
|
|
{
|
|
|
|
|
if (IsValidProxy())
|
|
|
|
|
{
|
|
|
|
|
NetworkCredential credentials = new NetworkCredential(Username, Password);
|
|
|
|
|
string address = string.Format("{0}:{1}", Host, Port);
|
|
|
|
|
return new WebProxy(address, true, null, credentials);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-05 01:55:58 +13:00
|
|
|
|
// Proxy for FTP
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public IProxyClient GetProxyClient()
|
|
|
|
|
{
|
|
|
|
|
if (IsValidProxy())
|
|
|
|
|
{
|
|
|
|
|
Starksoft.Net.Proxy.ProxyType proxyType;
|
|
|
|
|
|
|
|
|
|
switch (ProxyType)
|
|
|
|
|
{
|
|
|
|
|
case ProxyType.HTTP:
|
|
|
|
|
proxyType = Starksoft.Net.Proxy.ProxyType.Http;
|
|
|
|
|
break;
|
|
|
|
|
case ProxyType.SOCKS4:
|
|
|
|
|
proxyType = Starksoft.Net.Proxy.ProxyType.Socks4;
|
|
|
|
|
break;
|
|
|
|
|
case ProxyType.SOCKS4a:
|
|
|
|
|
proxyType = Starksoft.Net.Proxy.ProxyType.Socks4a;
|
|
|
|
|
break;
|
|
|
|
|
case ProxyType.SOCKS5:
|
|
|
|
|
proxyType = Starksoft.Net.Proxy.ProxyType.Socks5;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
proxyType = Starksoft.Net.Proxy.ProxyType.None;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ProxyClientFactory proxy = new ProxyClientFactory();
|
|
|
|
|
return proxy.CreateProxyClient(proxyType, Host, Port, Username, Password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-05 01:55:58 +13:00
|
|
|
|
private WebProxy GetDefaultWebProxy()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-05-20 14:14:03 +12:00
|
|
|
|
// Need better solution
|
2013-12-05 01:55:58 +13:00
|
|
|
|
return (WebProxy)typeof(WebProxy).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,
|
|
|
|
|
null, new Type[] { typeof(bool) }, null).Invoke(new object[] { true });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e, "Reflection failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2013-12-05 01:55:58 +13:00
|
|
|
|
return string.Format("{0} - {1}:{2} ({3})", Username, Host, Port, ProxyType);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|