root / trunk / Pithos.Client.WPF / Proxy.cs @ 8f44fd3a
History | View | Annotate | Download (4.1 kB)
1 | 255f5f86 | Panagiotis Kanavos | #region |
---|---|---|---|
2 | 255f5f86 | Panagiotis Kanavos | /* ----------------------------------------------------------------------- |
3 | 255f5f86 | Panagiotis Kanavos | * <copyright file="Proxy.cs" company="GRNet"> |
4 | 255f5f86 | Panagiotis Kanavos | * |
5 | 255f5f86 | Panagiotis Kanavos | * Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 | 255f5f86 | Panagiotis Kanavos | * |
7 | 255f5f86 | Panagiotis Kanavos | * Redistribution and use in source and binary forms, with or |
8 | 255f5f86 | Panagiotis Kanavos | * without modification, are permitted provided that the following |
9 | 255f5f86 | Panagiotis Kanavos | * conditions are met: |
10 | 255f5f86 | Panagiotis Kanavos | * |
11 | 255f5f86 | Panagiotis Kanavos | * 1. Redistributions of source code must retain the above |
12 | 255f5f86 | Panagiotis Kanavos | * copyright notice, this list of conditions and the following |
13 | 255f5f86 | Panagiotis Kanavos | * disclaimer. |
14 | 255f5f86 | Panagiotis Kanavos | * |
15 | 255f5f86 | Panagiotis Kanavos | * 2. Redistributions in binary form must reproduce the above |
16 | 255f5f86 | Panagiotis Kanavos | * copyright notice, this list of conditions and the following |
17 | 255f5f86 | Panagiotis Kanavos | * disclaimer in the documentation and/or other materials |
18 | 255f5f86 | Panagiotis Kanavos | * provided with the distribution. |
19 | 255f5f86 | Panagiotis Kanavos | * |
20 | 255f5f86 | Panagiotis Kanavos | * |
21 | 255f5f86 | Panagiotis Kanavos | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
22 | 255f5f86 | Panagiotis Kanavos | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | 255f5f86 | Panagiotis Kanavos | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
24 | 255f5f86 | Panagiotis Kanavos | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
25 | 255f5f86 | Panagiotis Kanavos | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 | 255f5f86 | Panagiotis Kanavos | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 | 255f5f86 | Panagiotis Kanavos | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
28 | 255f5f86 | Panagiotis Kanavos | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
29 | 255f5f86 | Panagiotis Kanavos | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | 255f5f86 | Panagiotis Kanavos | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
31 | 255f5f86 | Panagiotis Kanavos | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 | 255f5f86 | Panagiotis Kanavos | * POSSIBILITY OF SUCH DAMAGE. |
33 | 255f5f86 | Panagiotis Kanavos | * |
34 | 255f5f86 | Panagiotis Kanavos | * The views and conclusions contained in the software and |
35 | 255f5f86 | Panagiotis Kanavos | * documentation are those of the authors and should not be |
36 | 255f5f86 | Panagiotis Kanavos | * interpreted as representing official policies, either expressed |
37 | 255f5f86 | Panagiotis Kanavos | * or implied, of GRNET S.A. |
38 | 255f5f86 | Panagiotis Kanavos | * </copyright> |
39 | 255f5f86 | Panagiotis Kanavos | * ----------------------------------------------------------------------- |
40 | 255f5f86 | Panagiotis Kanavos | */ |
41 | 255f5f86 | Panagiotis Kanavos | #endregion |
42 | 34bdb91d | Panagiotis Kanavos | using System.Net; |
43 | 34bdb91d | Panagiotis Kanavos | using Pithos.Interfaces; |
44 | 34bdb91d | Panagiotis Kanavos | |
45 | 34bdb91d | Panagiotis Kanavos | namespace Pithos.Client.WPF |
46 | 34bdb91d | Panagiotis Kanavos | { |
47 | 34bdb91d | Panagiotis Kanavos | using System; |
48 | 34bdb91d | Panagiotis Kanavos | using System.Collections.Generic; |
49 | 34bdb91d | Panagiotis Kanavos | using System.Linq; |
50 | 34bdb91d | Panagiotis Kanavos | using System.Text; |
51 | 34bdb91d | Panagiotis Kanavos | |
52 | 34bdb91d | Panagiotis Kanavos | /// <summary> |
53 | 34bdb91d | Panagiotis Kanavos | /// TODO: Update summary. |
54 | 34bdb91d | Panagiotis Kanavos | /// </summary> |
55 | 34bdb91d | Panagiotis Kanavos | public class Proxy |
56 | 34bdb91d | Panagiotis Kanavos | { |
57 | 34bdb91d | Panagiotis Kanavos | //or the proxy changes, not when the monitor starts |
58 | 34bdb91d | Panagiotis Kanavos | public static void SetFromSettings(IPithosSettings pithosSettings) |
59 | 34bdb91d | Panagiotis Kanavos | { |
60 | 34bdb91d | Panagiotis Kanavos | IWebProxy proxy; |
61 | 34bdb91d | Panagiotis Kanavos | if (pithosSettings.UseNoProxy) |
62 | 34bdb91d | Panagiotis Kanavos | { |
63 | 34bdb91d | Panagiotis Kanavos | proxy = null; |
64 | 34bdb91d | Panagiotis Kanavos | } |
65 | 34bdb91d | Panagiotis Kanavos | else if (pithosSettings.UseManualProxy) |
66 | 34bdb91d | Panagiotis Kanavos | { |
67 | 34bdb91d | Panagiotis Kanavos | proxy = new WebProxy(pithosSettings.ProxyServer, pithosSettings.ProxyPort); |
68 | 34bdb91d | Panagiotis Kanavos | //If the proxy requires specific authentication settings, use them |
69 | 34bdb91d | Panagiotis Kanavos | if (pithosSettings.ProxyAuthentication) |
70 | 34bdb91d | Panagiotis Kanavos | { |
71 | 34bdb91d | Panagiotis Kanavos | proxy.Credentials = new NetworkCredential(pithosSettings.ProxyUsername, pithosSettings.ProxyPassword, pithosSettings.ProxyDomain); |
72 | 34bdb91d | Panagiotis Kanavos | } |
73 | 34bdb91d | Panagiotis Kanavos | //Otherwise, if there are generic authentication settings, use them |
74 | 34bdb91d | Panagiotis Kanavos | if (!String.IsNullOrWhiteSpace(CredentialCache.DefaultNetworkCredentials.UserName)) |
75 | 34bdb91d | Panagiotis Kanavos | { |
76 | 34bdb91d | Panagiotis Kanavos | proxy.Credentials = CredentialCache.DefaultNetworkCredentials; |
77 | 34bdb91d | Panagiotis Kanavos | } |
78 | 34bdb91d | Panagiotis Kanavos | } |
79 | 34bdb91d | Panagiotis Kanavos | //In all other cases, use the default proxy |
80 | 34bdb91d | Panagiotis Kanavos | else |
81 | 34bdb91d | Panagiotis Kanavos | { |
82 | 34bdb91d | Panagiotis Kanavos | proxy = WebRequest.GetSystemWebProxy(); |
83 | 84a200dc | Panagiotis Kanavos | //We can't use any previous proxy credentials when we pick the default proxy |
84 | 84a200dc | Panagiotis Kanavos | //If we set the proxy due to a change in settings, the credentials will only be |
85 | 84a200dc | Panagiotis Kanavos | //valid for the previous proxy. |
86 | 84a200dc | Panagiotis Kanavos | proxy.Credentials = CredentialCache.DefaultNetworkCredentials; |
87 | 34bdb91d | Panagiotis Kanavos | } |
88 | 34bdb91d | Panagiotis Kanavos | WebRequest.DefaultWebProxy = proxy; |
89 | 34bdb91d | Panagiotis Kanavos | } |
90 | 34bdb91d | Panagiotis Kanavos | |
91 | 34bdb91d | Panagiotis Kanavos | |
92 | 34bdb91d | Panagiotis Kanavos | public static void SetCredentials(string proxyUsername, string proxyPassword, string proxyDomain) |
93 | 34bdb91d | Panagiotis Kanavos | { |
94 | 34bdb91d | Panagiotis Kanavos | CredentialCache.DefaultNetworkCredentials.Password = proxyPassword; |
95 | 34bdb91d | Panagiotis Kanavos | CredentialCache.DefaultNetworkCredentials.UserName = proxyUsername; |
96 | 34bdb91d | Panagiotis Kanavos | CredentialCache.DefaultNetworkCredentials.Domain = proxyDomain; |
97 | 34bdb91d | Panagiotis Kanavos | if (WebRequest.DefaultWebProxy!=null) |
98 | 34bdb91d | Panagiotis Kanavos | WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials; |
99 | 34bdb91d | Panagiotis Kanavos | } |
100 | 34bdb91d | Panagiotis Kanavos | } |
101 | 34bdb91d | Panagiotis Kanavos | } |