forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxyConfig.cs
More file actions
77 lines (65 loc) · 2.18 KB
/
ProxyConfig.cs
File metadata and controls
77 lines (65 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Net;
namespace SpotifyAPI
{
public class ProxyConfig
{
public string Host { get; set; }
public int Port { get; set; } = 80;
public string Username { get; set; }
public string Password { get; set; }
/// <summary>
/// Whether to bypass the proxy server for local addresses.
/// </summary>
public bool BypassProxyOnLocal { get; set; }
public void Set(ProxyConfig proxyConfig)
{
Host = proxyConfig?.Host;
Port = proxyConfig?.Port ?? 80;
Username = proxyConfig?.Username;
Password = proxyConfig?.Password;
BypassProxyOnLocal = proxyConfig?.BypassProxyOnLocal ?? false;
}
/// <summary>
/// Whether both <see cref="Host"/> and <see cref="Port"/> have valid values.
/// </summary>
/// <returns></returns>
public bool IsValid()
{
return !string.IsNullOrWhiteSpace(Host) && Port > 0;
}
/// <summary>
/// Create a <see cref="Uri"/> from the host and port number
/// </summary>
/// <returns>A URI</returns>
public Uri GetUri()
{
UriBuilder uriBuilder = new UriBuilder(Host)
{
Port = Port
};
return uriBuilder.Uri;
}
/// <summary>
/// Creates a <see cref="WebProxy"/> from the proxy details of this object.
/// </summary>
/// <returns>A <see cref="WebProxy"/> or <code>null</code> if the proxy details are invalid.</returns>
public WebProxy CreateWebProxy()
{
if (!IsValid())
return null;
WebProxy proxy = new WebProxy
{
Address = GetUri(),
UseDefaultCredentials = true,
BypassProxyOnLocal = BypassProxyOnLocal
};
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
{
proxy.UseDefaultCredentials = false;
proxy.Credentials = new NetworkCredential(Username, Password);
}
return proxy;
}
}
}