Skip to content
This repository has been archived by the owner on Jun 14, 2020. It is now read-only.

Commit

Permalink
Add TryParse method
Browse files Browse the repository at this point in the history
  • Loading branch information
extremecodetv committed Aug 25, 2017
1 parent d6a41bf commit 1fa72f4
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion src/SocksSharp/Proxy/ProxySettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;

namespace SocksSharp.Proxy
{
Expand Down Expand Up @@ -106,5 +107,85 @@ public IProxySettingsFluent SetConnectionTimeout(int connectionTimeout)
}

#endregion

/// <summary>
/// Converts the string representation of a <see cref="ProxySettings"/>.
/// A return value indicates whether the conversion succeeded.
/// </summary>
/// <param name="proxy">A string containing proxy settings</param>
/// <param name="proxySettings">When this method returns,
/// contains the instance of the <see cref="ProxySettings"/> value equivalent of the number contained in proxy,
/// if the conversion succeeded, or <see cref="null"/> if the conversion failed.</param>
/// <returns><see cref="true"/> if s was converted successfully; otherwise, <see cref="false"/>.</returns>
/// <remarks>String must be in one of this format
/// host:port
/// - or -
/// host:port:username
/// - or -
/// host:port:username:password
/// </remarks>
public static bool TryParse(string proxy, out ProxySettings proxySettings)
{
NetworkCredential credential = null;

proxySettings = null;

#region Parse Address

if (String.IsNullOrEmpty(proxy))
{
return false;
}

string[] values = proxy.Split(':');

int port = 1080;
string host = values[0];

if (values.Length >= 2)
{
if (!int.TryParse(values[1], out port))
{
return false;
}
}
#endregion

#region Parse Credential

string username = String.Empty;
string password = String.Empty;

if (values.Length >= 3)
{
credential = new NetworkCredential();

username = values[2];

if (values.Length >= 4)
{
password = values[3];
}

if (!String.IsNullOrEmpty(username))
{
credential.UserName = username;
}

if (!String.IsNullOrEmpty(password))
{
credential.Password = password;
}
}

#endregion

proxySettings = new ProxySettings();
proxySettings.Host = host;
proxySettings.Port = port;
proxySettings.Credentials = credential;

return true;
}
}
}

0 comments on commit 1fa72f4

Please sign in to comment.