From ddd5ca4cba40a532f5fbb39013c27baa99cbfd31 Mon Sep 17 00:00:00 2001 From: Maksim Kutin Date: Wed, 11 Jul 2018 00:56:16 +0300 Subject: [PATCH] added proxy credentials for easy way usefull exchange data with APIEndpoint via proxy --- bot.go | 21 +++++++++++++++++++++ bot_test.go | 17 ++++++++++++++++- helpers.go | 30 ++++++++++++++++++++++++++++++ types.go | 11 +++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/bot.go b/bot.go index 8fb6200..15247ea 100644 --- a/bot.go +++ b/bot.go @@ -28,6 +28,7 @@ type BotAPI struct { Self User `json:"-"` Client *http.Client `json:"-"` + ProxySettings ProxyCredentials `json:"-"` } // NewBotAPI creates a new BotAPI instance. @@ -58,6 +59,26 @@ func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { return bot, nil } +// NewBotAPIViaProxy creates a new BotAPI instance and allows you +// exchange data with APIEndpoint via proxy-server. +// +// It requires a token and proxy credentials. +func NewBotAPIViaProxy(token string, proxySettings ProxyCredentials) (*BotAPI, error) { + client := &http.Client{} + + if proxySettings.UseProxy { + fixedURL, err := url.Parse(fmt.Sprintf("%s://%s:%s@%s:%s", proxySettings.Protocol, proxySettings.Username, proxySettings.Password, proxySettings.IP, proxySettings.Port)) + + if err == nil { + tr := &http.Transport{Proxy: http.ProxyURL(fixedURL)} + + client.Transport = tr + } + } + + return NewBotAPIWithClient(token, client) +} + // MakeRequest makes a request to a specific endpoint with our token. func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) { method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) diff --git a/bot_test.go b/bot_test.go index e7aa7ac..635343e 100644 --- a/bot_test.go +++ b/bot_test.go @@ -8,7 +8,7 @@ import ( "testing" "time" - "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/mkutin/telegram-bot-api" ) const ( @@ -23,6 +23,12 @@ const ( ExistingVideoFileID = "BAADAgADZgADjMcoCav432kYe0FRAg" ExistingVideoNoteFileID = "DQADAgADdQAD70cQSUK41dLsRMqfAg" ExistingStickerFileID = "BQADAgADcwADjMcoCbdl-6eB--YPAg" + UseProxy = true + ProxyProtocol = "SOCKS5" + ProxyUsername = "YGko8h" + ProxyPassword = "VofoA7" + ProxyIP = "185.179.113.223" + ProxyPort = "8000" ) func getBot(t *testing.T) (*tgbotapi.BotAPI, error) { @@ -37,6 +43,15 @@ func getBot(t *testing.T) (*tgbotapi.BotAPI, error) { return bot, err } +func TestNewBotAPIViaProxy(t *testing.T) { + _, err := tgbotapi.NewBotAPIViaProxy(TestToken, tgbotapi.NewProxyCredentials(UseProxy, ProxyProtocol, ProxyUsername, ProxyPassword, ProxyIP, ProxyPort)) + + if err != nil { + t.Error(err) + t.Fail() + } +} + func TestNewBotAPI_notoken(t *testing.T) { _, err := tgbotapi.NewBotAPI("") diff --git a/helpers.go b/helpers.go index b5480ea..1ccc211 100644 --- a/helpers.go +++ b/helpers.go @@ -3,6 +3,7 @@ package tgbotapi import ( "log" "net/url" + "strings" ) // NewMessage creates a new Message. @@ -691,3 +692,32 @@ func NewSetChatPhotoShare(chatID int64, fileID string) SetChatPhotoConfig { }, } } + +// NewProxyCredentials create a new proxy credentials. +// You may use this to create new credentials for exchange data +// via proxy-server. +// +// useProxy tells you to use a proxy, protocol is one of the allowed protocols, +// username and password are credentials for authentication user on proxy-server, +// ip and port are parameters for connection to proxy-server. +func NewProxyCredentials(useProxy bool, protocol, username, password, ip, port string) ProxyCredentials { + switch strings.ToLower(protocol) { + case "socks5": + protocol = "socks5" + case "http": + protocol = "http" + case "https": + protocol = "https" + default: + useProxy = false + } + + return ProxyCredentials{ + UseProxy: useProxy, + Protocol: protocol, + Username: username, + Password: password, + IP: ip, + Port: port, + } +} diff --git a/types.go b/types.go index 0843ab9..32417cc 100644 --- a/types.go +++ b/types.go @@ -773,6 +773,17 @@ type PreCheckoutQuery struct { OrderInfo *OrderInfo `json:"order_info,omitempty"` } +// ProxyCredentials are credentials parameters for exchange data with +// APIEndpoint via proxy-server. +type ProxyCredentials struct { + UseProxy bool + Protocol string + Username string + Password string + IP string + Port string +} + // Error is an error containing extra information returned by the Telegram API. type Error struct { Message string