env: supports for http and socks5 proxies

This commit is contained in:
stefanodvx 2025-04-14 21:50:49 +02:00
parent 386c3991cd
commit 10c113f400
10 changed files with 51 additions and 60 deletions

View file

@ -8,3 +8,8 @@ DB_USER=govd
DB_PASSWORD=password DB_PASSWORD=password
REPO_URL=https://github.com/govdbot/govd REPO_URL=https://github.com/govdbot/govd
# proxy
HTTP_PROXY=
HTTPS_PROXY=
NO_PROXY=

View file

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"net/http" "net/http"
"net/url"
"os" "os"
"strings" "strings"
"time" "time"
@ -45,7 +46,14 @@ func NewBotClient() BotClient {
} }
return BotClient{ return BotClient{
BotClient: &gotgbot.BaseBotClient{ BotClient: &gotgbot.BaseBotClient{
Client: http.Client{}, Client: http.Client{
Transport: &http.Transport{
// avoid using proxy for telegram
Proxy: func(r *http.Request) (*url.URL, error) {
return nil, nil
},
},
},
UseTestEnvironment: false, UseTestEnvironment: false,
DefaultRequestOpts: &gotgbot.RequestOpts{ DefaultRequestOpts: &gotgbot.RequestOpts{
Timeout: 10 * time.Minute, Timeout: 10 * time.Minute,

View file

@ -1,7 +1,6 @@
package instagram package instagram
import ( import (
"crypto/tls"
"fmt" "fmt"
"govd/enums" "govd/enums"
"govd/models" "govd/models"
@ -9,9 +8,6 @@ import (
"io" "io"
"net/http" "net/http"
"regexp" "regexp"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
) )
// as a public service, we can't use the official API // as a public service, we can't use the official API
@ -46,17 +42,7 @@ var igHeaders = map[string]string{
"User-Agent": util.ChromeUA, "User-Agent": util.ChromeUA,
} }
var HTTPClient = &http.Client{ var HTTPSession = util.NewHTTPSession()
Transport: &http3.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
QUICConfig: &quic.Config{
MaxIncomingStreams: -1,
EnableDatagrams: true,
},
},
}
var Extractor = &models.Extractor{ var Extractor = &models.Extractor{
Name: "Instagram", Name: "Instagram",
@ -110,7 +96,7 @@ var ShareURLExtractor = &models.Extractor{
for k, v := range igHeaders { for k, v := range igHeaders {
req.Header.Set(k, v) req.Header.Set(k, v)
} }
resp, err := HTTPClient.Do(req) resp, err := HTTPSession.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err) return nil, fmt.Errorf("failed to send request: %w", err)
} }
@ -197,7 +183,7 @@ func GetVideoAPI(contentURL string) (*IGramResponse, error) {
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", util.ChromeUA) req.Header.Set("User-Agent", util.ChromeUA)
resp, err := HTTPClient.Do(req) resp, err := HTTPSession.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err) return nil, fmt.Errorf("failed to send request: %w", err)
} }

View file

@ -115,7 +115,7 @@ func GetPostCaption(
req.Header.Set("Cache-Control", "no-cache") req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("TE", "trailers") req.Header.Set("TE", "trailers")
resp, err := HTTPClient.Do(req) resp, err := HTTPSession.Do(req)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to send request: %w", err) return "", fmt.Errorf("failed to send request: %w", err)
} }

View file

@ -17,6 +17,8 @@ const (
shortenerAPIFormat = "https://api.pinterest.com/url_shortener/%s/redirect/" shortenerAPIFormat = "https://api.pinterest.com/url_shortener/%s/redirect/"
) )
var HTTPSession = util.NewHTTPSession()
var ShortExtractor = &models.Extractor{ var ShortExtractor = &models.Extractor{
Name: "Pinterest (Short)", Name: "Pinterest (Short)",
CodeName: "pinterest:short", CodeName: "pinterest:short",
@ -146,8 +148,7 @@ func GetPinData(pinID string) (*PinData, error) {
// fix 403 error // fix 403 error
req.Header.Set("X-Pinterest-PWS-Handler", "www/[username].js") req.Header.Set("X-Pinterest-PWS-Handler", "www/[username].js")
client := &http.Client{} resp, err := HTTPSession.Do(req)
resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err) return nil, fmt.Errorf("failed to send request: %w", err)
} }

View file

@ -12,14 +12,7 @@ import (
"govd/util" "govd/util"
) )
var HTTPClient = &http.Client{ var HTTPSession = util.NewHTTPSession()
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return fmt.Errorf("stopped after 10 redirects")
}
return nil
},
}
var ShortExtractor = &models.Extractor{ var ShortExtractor = &models.Extractor{
Name: "Reddit (Short)", Name: "Reddit (Short)",
@ -44,7 +37,7 @@ var ShortExtractor = &models.Extractor{
req.AddCookie(cookie) req.AddCookie(cookie)
} }
res, err := HTTPClient.Do(req) res, err := HTTPSession.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err) return nil, fmt.Errorf("failed to send request: %w", err)
} }
@ -236,7 +229,7 @@ func GetRedditData(host string, slug string) (RedditResponse, error) {
req.AddCookie(cookie) req.AddCookie(cookie)
} }
res, err := HTTPClient.Do(req) res, err := HTTPSession.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err) return nil, fmt.Errorf("failed to send request: %w", err)
} }

View file

@ -1,16 +1,12 @@
package tiktok package tiktok
import ( import (
"crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"regexp" "regexp"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
"govd/enums" "govd/enums"
"govd/models" "govd/models"
"govd/util" "govd/util"
@ -27,17 +23,7 @@ const (
appUserAgent = packageID + " (Linux; U; Android 13; en_US; Pixel 7; Build/TD1A.220804.031; Cronet/58.0.2991.0)" appUserAgent = packageID + " (Linux; U; Android 13; en_US; Pixel 7; Build/TD1A.220804.031; Cronet/58.0.2991.0)"
) )
var HTTPClient = &http.Client{ var HTTPSession = util.NewHTTPSession()
Transport: &http3.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
QUICConfig: &quic.Config{
MaxIncomingStreams: -1,
EnableDatagrams: true,
},
},
}
var VMExtractor = &models.Extractor{ var VMExtractor = &models.Extractor{
Name: "TikTok VM", Name: "TikTok VM",
@ -161,7 +147,7 @@ func GetVideoAPI(awemeID string) (*AwemeDetails, error) {
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
req.Header.Set("X-Argus", "") req.Header.Set("X-Argus", "")
resp, err := HTTPClient.Do(req) resp, err := HTTPSession.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err) return nil, fmt.Errorf("failed to send request: %w", err)
} }

View file

@ -17,7 +17,7 @@ const (
apiEndpoint = "https://x.com/i/api/graphql/zZXycP0V6H7m-2r0mOnFcA/TweetDetail" apiEndpoint = "https://x.com/i/api/graphql/zZXycP0V6H7m-2r0mOnFcA/TweetDetail"
) )
var HTTPClient = &http.Client{} var HTTPSession = util.NewHTTPSession()
var ShortExtractor = &models.Extractor{ var ShortExtractor = &models.Extractor{
Name: "Twitter (Short)", Name: "Twitter (Short)",
@ -33,7 +33,7 @@ var ShortExtractor = &models.Extractor{
return nil, fmt.Errorf("failed to create req: %w", err) return nil, fmt.Errorf("failed to create req: %w", err)
} }
req.Header.Set("User-Agent", util.ChromeUA) req.Header.Set("User-Agent", util.ChromeUA)
res, err := HTTPClient.Do(req) res, err := HTTPSession.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err) return nil, fmt.Errorf("failed to send request: %w", err)
} }
@ -151,7 +151,7 @@ func GetTweetAPI(tweetID string) (*Tweet, error) {
} }
req.URL.RawQuery = q.Encode() req.URL.RawQuery = q.Encode()
resp, err := HTTPClient.Do(req) resp, err := HTTPSession.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err) return nil, fmt.Errorf("failed to send request: %w", err)
} }

View file

@ -45,7 +45,7 @@ func DownloadFile(
return "", ctx.Err() return "", ctx.Err()
default: default:
// create the download directory if it doesn't exist // create the download directory if it doesn't exist
if err := ensureDownloadDir(config.DownloadDir); err != nil { if err := EnsureDownloadDir(config.DownloadDir); err != nil {
return "", err return "", err
} }
@ -78,7 +78,7 @@ func DownloadFileWithSegments(
if config == nil { if config == nil {
config = DefaultConfig() config = DefaultConfig()
} }
if err := ensureDownloadDir(config.DownloadDir); err != nil { if err := EnsureDownloadDir(config.DownloadDir); err != nil {
return "", err return "", err
} }
tempDir := filepath.Join(config.DownloadDir, "segments_"+time.Now().Format("20060102_150405")) tempDir := filepath.Join(config.DownloadDir, "segments_"+time.Now().Format("20060102_150405"))
@ -151,11 +151,15 @@ func downloadInMemory(ctx context.Context, fileURL string, timeout time.Duration
return io.ReadAll(resp.Body) return io.ReadAll(resp.Body)
} }
func ensureDownloadDir(dir string) error { func EnsureDownloadDir(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) { if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0755); err != nil { if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create downloads directory: %w", err) return fmt.Errorf("failed to create downloads directory: %w", err)
} }
} else {
return fmt.Errorf("error accessing directory: %w", err)
}
} }
return nil return nil
} }
@ -453,7 +457,7 @@ func MergeSegmentFiles(
config = DefaultConfig() config = DefaultConfig()
} }
if err := ensureDownloadDir(config.DownloadDir); err != nil { if err := EnsureDownloadDir(config.DownloadDir); err != nil {
return "", err return "", err
} }

View file

@ -5,8 +5,16 @@ import (
"time" "time"
) )
var httpSession = &http.Client{ var httpSession = NewHTTPSession()
func NewHTTPSession() *http.Client {
session := &http.Client{
Timeout: 20 * time.Second, Timeout: 20 * time.Second,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
}
return session
} }
func GetHTTPSession() *http.Client { func GetHTTPSession() *http.Client {