add http proxy support

This commit is contained in:
JustHumanz 2020-12-03 21:42:16 +07:00
parent 111942f1b9
commit 341e1de6d0
4 changed files with 64 additions and 3 deletions

20
util.go
View file

@ -2,11 +2,31 @@ package twitterscraper
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
)
//HttpProxy Public variable for Http proxy
var HTTPProxy *url.URL
//SetProxy set http proxy
func SetProxy(Proxy string) error {
match, _ := regexp.MatchString("http.+", Proxy)
if !match {
return errors.New("only support http protocol")
}
urlproxy, err := url.Parse(Proxy)
if err != nil {
return err
}
HTTPProxy = urlproxy
return nil
}
func newRequest(url string) (*http.Request, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {