-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
138 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,104 @@ | ||
using System.Net; | ||
using Autofac; | ||
using MihaZupan; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Telegram.Bot; | ||
using Telegrom.Core; | ||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
|
||
namespace Telegrom.TelegramService | ||
namespace Telegrom.TelegramService; | ||
|
||
public sealed class TelegramServiceModule : Module | ||
{ | ||
public class TelegramServiceModule : Module | ||
private const string HttpClientName = "typicode"; | ||
|
||
protected override void Load(ContainerBuilder builder) | ||
{ | ||
protected override void Load(ContainerBuilder builder) | ||
var proxy = CreateProxy(); | ||
|
||
builder.Register(ctx => | ||
{ | ||
if (!string.IsNullOrEmpty(TelegramOptions.Current.ProxyAddress)) | ||
{ | ||
builder.RegisterInstance(new TelegramBotClient( | ||
TelegramOptions.Current.TelegramApiToken, | ||
new WebProxy(TelegramOptions.Current.ProxyAddress) | ||
{ | ||
UseDefaultCredentials = true | ||
})) | ||
.AsImplementedInterfaces() | ||
.SingleInstance(); | ||
} | ||
else if (!string.IsNullOrEmpty(TelegramOptions.Current.Socks5HostName) | ||
&& TelegramOptions.Current.Socks5Port.HasValue) | ||
{ | ||
HttpToSocks5Proxy proxy; | ||
if (!string.IsNullOrEmpty(TelegramOptions.Current.Socks5Username) | ||
&& !string.IsNullOrEmpty(TelegramOptions.Current.Socks5Password)) | ||
{ | ||
proxy = new HttpToSocks5Proxy( | ||
TelegramOptions.Current.Socks5HostName, | ||
TelegramOptions.Current.Socks5Port.Value, | ||
TelegramOptions.Current.Socks5Username, | ||
TelegramOptions.Current.Socks5Password); | ||
} | ||
else | ||
var services = new ServiceCollection(); | ||
services.AddHttpClient(HttpClientName) | ||
.ConfigurePrimaryHttpMessageHandler(() => | ||
{ | ||
proxy = new HttpToSocks5Proxy(TelegramOptions.Current.Socks5HostName, TelegramOptions.Current.Socks5Port.Value); | ||
} | ||
|
||
builder.RegisterInstance(new TelegramBotClient(TelegramOptions.Current.TelegramApiToken, proxy)) | ||
.AsImplementedInterfaces() | ||
.SingleInstance(); | ||
} | ||
else | ||
var handler = new SocketsHttpHandler(); | ||
if (proxy != null) | ||
{ | ||
handler.Proxy = proxy; | ||
handler.UseProxy = true; | ||
} | ||
return handler; | ||
}); | ||
|
||
var provider = services.BuildServiceProvider(); | ||
return provider.GetRequiredService<IHttpClientFactory>(); | ||
}).SingleInstance(); | ||
|
||
builder.Register(c => | ||
{ | ||
builder.RegisterInstance(new TelegramBotClient(TelegramOptions.Current.TelegramApiToken)) | ||
.AsImplementedInterfaces() | ||
.SingleInstance(); | ||
} | ||
|
||
builder.RegisterType<TelegramUpdateReceiver>() | ||
.As<ITelegramUpdateReceiver>() | ||
.SingleInstance(); | ||
var httpClientFactory = c.Resolve<IHttpClientFactory>(); | ||
var httpClient = httpClientFactory.CreateClient(HttpClientName); | ||
return new TelegramBotClient(TelegramOptions.Current.TelegramApiToken, httpClient); | ||
}) | ||
.AsImplementedInterfaces() | ||
.SingleInstance(); | ||
|
||
builder.RegisterType<TelegramUpdateReceiver>() | ||
.As<ITelegramUpdateReceiver>() | ||
.SingleInstance(); | ||
} | ||
|
||
private static WebProxy? CreateProxy() | ||
{ | ||
if (IsHttpProxyConfigured()) | ||
{ | ||
return CreateHttpProxy(); | ||
} | ||
|
||
if (IsSocks5ProxyConfigured()) | ||
{ | ||
return CreateSocks5Proxy(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static bool IsHttpProxyConfigured() | ||
{ | ||
return !string.IsNullOrEmpty(TelegramOptions.Current.ProxyAddress); | ||
} | ||
|
||
private static WebProxy CreateHttpProxy() | ||
{ | ||
return new WebProxy(TelegramOptions.Current.ProxyAddress) | ||
{ | ||
UseDefaultCredentials = true | ||
}; | ||
} | ||
|
||
private static bool IsSocks5ProxyConfigured() | ||
{ | ||
return !string.IsNullOrEmpty(TelegramOptions.Current.Socks5HostName) | ||
&& TelegramOptions.Current.Socks5Port.HasValue; | ||
} | ||
|
||
private static WebProxy CreateSocks5Proxy() | ||
{ | ||
var socks5Proxy = new WebProxy(TelegramOptions.Current.Socks5HostName, | ||
TelegramOptions.Current.Socks5Port.Value); | ||
|
||
if (IsSocks5CredentialsConfigured()) | ||
{ | ||
socks5Proxy.Credentials = new NetworkCredential(TelegramOptions.Current.Socks5Username, | ||
TelegramOptions.Current.Socks5Password); | ||
} | ||
|
||
return socks5Proxy; | ||
} | ||
|
||
private static bool IsSocks5CredentialsConfigured() | ||
{ | ||
return !string.IsNullOrEmpty(TelegramOptions.Current.Socks5Username) | ||
&& !string.IsNullOrEmpty(TelegramOptions.Current.Socks5Password); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters