Browse Source

WebProxyForAPI

HOME 3 years ago
parent
commit
6fe6252e7e

+ 2 - 1
D3NsCore/D3NsClient.cs

@@ -19,6 +19,7 @@ namespace D3NsCore
         {
             _conf = new ConfigAdapter(new DataAccess(dbFile).GetConfigs());
             _http = new HttpAccess();
+            _http.ProxyServer = _conf.WebProxy;
         }
 
         public override void Start()
@@ -94,7 +95,7 @@ namespace D3NsCore
 
         // ---- utilitys ----
 
-        private string GetMyIp() => _http.GetString(_conf.GetMyIp);
+        private string GetMyIp() => _http.GetStringWithoutProxy(_conf.GetMyIp);
 
         private string GetDnsIp() => _http.GetJsonAnon(new[] { new { data = "" } }, $"https://api.godaddy.com/v1/domains/{_conf.Domain}/records/A/{_conf.DnsRecordName}", new HttpHeader("Accept", "application/json"), new HttpHeader("Authorization", $"sso-key {_conf.Key}:{_conf.Secret}")).Select(p => p.data).Single();
 

+ 2 - 1
D3NsCore/D3NsCore.csproj

@@ -10,7 +10,7 @@
     <OutputType>Exe</OutputType>
     <RootNamespace>D3NsCore</RootNamespace>
     <AssemblyName>D3NsCore</AssemblyName>
-    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
     <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
     <NuGetPackageImportStamp>
@@ -86,6 +86,7 @@
     <Compile Include="Tools\Utility.cs" />
   </ItemGroup>
   <ItemGroup>
+    <None Include="app.config" />
     <None Include="packages.config" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

+ 1 - 0
D3NsCore/Tools/ConfigAdapter.cs

@@ -45,5 +45,6 @@ namespace D3NsCore.Tools
         public string GetMyIp => GetValue();
         public string Domain => GetValue();
         public string DnsRecordName => GetValue();
+        public string WebProxy => GetValue();
     }
 }

+ 1 - 0
D3NsCore/Tools/DataAccess.cs

@@ -39,6 +39,7 @@ namespace D3NsCore.Tools
                     + $"INSERT INTO Configs ({nameof(ConfigEntry.Key)}) VALUES ('{nameof(ConfigAdapter.GetMyIp)}');"
                     + $"INSERT INTO Configs ({nameof(ConfigEntry.Key)}) VALUES ('{nameof(ConfigAdapter.Domain)}');"
                     + $"INSERT INTO Configs ({nameof(ConfigEntry.Key)}) VALUES ('{nameof(ConfigAdapter.DnsRecordName)}');"
+                    + $"INSERT INTO Configs ({nameof(ConfigEntry.Key)}) VALUES ('{nameof(ConfigAdapter.WebProxy)}');"
                 );
             }
         }

+ 12 - 26
D3NsCore/Tools/HttpAccess.cs

@@ -14,25 +14,23 @@ namespace D3NsCore.Tools
 
         static HttpAccess()
         {
-            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
+            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
         }
 
-        public string GetString(string url, params HttpHeader[] headers)
+        public string GetStringWithoutProxy(string url, params HttpHeader[] headers)
         {
-            {
-                //var client = new HttpClient();
-                //client.DefaultRequestHeaders.Add("UserAgent", UserAgent);
-                //foreach (var header in headers)
-                //{
-                //    client.DefaultRequestHeaders.Add(header.Name, header.Value);
-                //}
-                //var str = client.GetStringAsync(url).Result;
-                //return str;
-            }
+            var wc = new WebClient { Headers = { ["User-Agent"] = UserAgent } };
+            wc.Proxy = new WebProxy();
+            foreach (var header in headers) wc.Headers.Add(header.Name, header.Value);
+            var str = wc.DownloadString(url);
+            return str;
+        }
 
+        public string GetString(string url, params HttpHeader[] headers)
+        {
             var wc = new WebClient { Headers = { ["User-Agent"] = UserAgent } };
             wc.Proxy = new WebProxy();
-            if (ProxyServer != null) wc.Proxy = new WebProxy(new Uri(ProxyServer));
+            if (string.IsNullOrWhiteSpace(ProxyServer) == false) wc.Proxy = new WebProxy(new Uri(ProxyServer));
             foreach (var header in headers) wc.Headers.Add(header.Name, header.Value);
             var str = wc.DownloadString(url);
             return str;
@@ -40,20 +38,8 @@ namespace D3NsCore.Tools
 
         public string PutString(string url, string content, string contentType = "application/x-www-form-urlencoded;charset=UTF-8", params HttpHeader[] headers)
         {
-            {
-                //var client = new HttpClient();
-                //client.DefaultRequestHeaders.Add("UserAgent", UserAgent);
-                //foreach (var header in headers)
-                //{
-                //    client.DefaultRequestHeaders.Add(header.Name, header.Value);
-                //}
-                //var r = client.PutAsync(url, new StringContent(content, Encoding.UTF8, contentType));
-                //var str = r.Result.Content.ReadAsStringAsync().Result;
-                //return str;
-            }
-
             var wc = new WebClient { Headers = { ["User-Agent"] = UserAgent, ["Content-Type"] = contentType } };
-            if (ProxyServer != null) wc.Proxy = new WebProxy(new Uri(ProxyServer));
+            if (string.IsNullOrWhiteSpace(ProxyServer)==false) wc.Proxy = new WebProxy(new Uri(ProxyServer));
             foreach (var header in headers) wc.Headers.Add(header.Name, header.Value);
 
             try

+ 3 - 0
D3NsCore/app.config

@@ -0,0 +1,3 @@
+<?xml version="1.0" encoding="utf-8"?>
+<configuration>
+<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/></startup></configuration>

+ 1 - 1
D3NsCore/packages.config

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
-  <package id="Dapper" version="1.50.2" targetFramework="net45" />
+  <package id="Dapper" version="1.50.2" targetFramework="net45" requireReinstallation="true" />
   <package id="ILRepack" version="2.0.13" targetFramework="net45" />
   <package id="Mono.Data.Sqlite.Portable" version="1.0.3.5" targetFramework="net45" />
   <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />