From ddd5ca4cba40a532f5fbb39013c27baa99cbfd31 Mon Sep 17 00:00:00 2001 From: Maksim Kutin Date: Wed, 11 Jul 2018 00:56:16 +0300 Subject: [PATCH 1/3] 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 From 3f11f21832b66187932b316b019c710ecef16bfe Mon Sep 17 00:00:00 2001 From: Maksim Kutin Date: Wed, 11 Jul 2018 07:43:33 +0300 Subject: [PATCH 2/3] fixed import directory --- bot_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot_test.go b/bot_test.go index 635343e..7b4678a 100644 --- a/bot_test.go +++ b/bot_test.go @@ -8,7 +8,7 @@ import ( "testing" "time" - "github.com/mkutin/telegram-bot-api" + "github.com/go-telegram-bot-api/telegram-bot-api/" ) const ( From cb85dc91269b4abae714ba0023dade069e7f6380 Mon Sep 17 00:00:00 2001 From: Maksim Kutin Date: Wed, 11 Jul 2018 08:03:07 +0300 Subject: [PATCH 3/3] removed test & added ability to connect without credentials --- bot.go | 9 ++++++++- bot_test.go | 17 +---------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/bot.go b/bot.go index 15247ea..ddd1dcb 100644 --- a/bot.go +++ b/bot.go @@ -67,7 +67,14 @@ func NewBotAPIViaProxy(token string, proxySettings ProxyCredentials) (*BotAPI, e 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)) + var fixedURL *url.URL + var err error + + if proxySettings.Username != "" && proxySettings.Password != "" { + fixedURL, err = url.Parse(fmt.Sprintf("%s://%s:%s@%s:%s", proxySettings.Protocol, proxySettings.Username, proxySettings.Password, proxySettings.IP, proxySettings.Port)) + } else { + fixedURL, err = url.Parse(fmt.Sprintf("%s://%s:%s", proxySettings.Protocol, proxySettings.IP, proxySettings.Port)) + } if err == nil { tr := &http.Transport{Proxy: http.ProxyURL(fixedURL)} diff --git a/bot_test.go b/bot_test.go index 7b4678a..9bfb4c7 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,12 +23,6 @@ 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) { @@ -43,15 +37,6 @@ 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("")