Init
This commit is contained in:
@@ -0,0 +1,69 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func File_put_contents(filename string, text string) {
|
||||||
|
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
f.WriteString(text)
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func File_append_contents(filename string, text string) {
|
||||||
|
f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
f.WriteString(text)
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fs_exists(filename string) bool {
|
||||||
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func File_get_lines(path string) ([]string, error) {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
var lines []string
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
lines = append(lines, scanner.Text())
|
||||||
|
}
|
||||||
|
return lines, scanner.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindLine(filename string, tofind string) (string, error) {
|
||||||
|
file, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
text := scanner.Text()
|
||||||
|
if strings.Contains(text, tofind) {
|
||||||
|
return text, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return "", errors.New("Nothnig found")
|
||||||
|
}
|
||||||
@@ -0,0 +1,193 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var agents = []string{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9",
|
||||||
|
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
|
||||||
|
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1"}
|
||||||
|
|
||||||
|
func randInt(min int, max int) int {
|
||||||
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
|
return min + rand.Intn(max-min)
|
||||||
|
}
|
||||||
|
|
||||||
|
func randomUserAgent() string {
|
||||||
|
return agents[randInt(0, len(agents))]
|
||||||
|
}
|
||||||
|
|
||||||
|
func HttpGet(url string, client *http.Client) string {
|
||||||
|
req, _ := http.NewRequest("GET", url, nil)
|
||||||
|
req.Header.Set("User-Agent", randomUserAgent())
|
||||||
|
req.Header.Set("Accept-Language", "en-us")
|
||||||
|
// req.Header.Set("X-Requested-With", "XMLHttpRequest")
|
||||||
|
// req.Header.Set("Referer", "https://cart.tinydeal.com/index.php?__page_id=checkout&order_source=shopping_cart")
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Cannot get url %s, err: %s", url, err.Error())
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
contents, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
return string(contents)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HttpGetDump(url string, client *http.Client, file string) (string, string) {
|
||||||
|
req, _ := http.NewRequest("GET", url, nil)
|
||||||
|
req.Header.Set("User-Agent", randomUserAgent())
|
||||||
|
req.Header.Set("Accept-Language", "en-us")
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Cannot get url %s, err: %s", url, err.Error())
|
||||||
|
return "", ""
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
dump, err := httputil.DumpResponse(resp, true)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
dumpStr := string(dump)
|
||||||
|
File_put_contents(file, dumpStr)
|
||||||
|
|
||||||
|
contents, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
return string(contents), dumpStr
|
||||||
|
}
|
||||||
|
|
||||||
|
func HttpPostJSON(url string, data string, referer_optional string, client *http.Client, isAjax bool) string {
|
||||||
|
req, _ := http.NewRequest("POST", url, bytes.NewBuffer([]byte(data)))
|
||||||
|
req.Header.Set("User-Agent", randomUserAgent())
|
||||||
|
if isAjax {
|
||||||
|
req.Header.Set("X-Requested-With", "XMLHttpRequest")
|
||||||
|
}
|
||||||
|
// req.Header.Set("Origin", "https://www.topcashback.com")
|
||||||
|
req.Header.Set("Accept-Language", "en-us")
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
if referer_optional != "" {
|
||||||
|
req.Header.Set("Referer", referer_optional)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Cannot post url %s, err: %s", url, err.Error())
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
contents, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
return string(contents)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HttpPostString(url string, data string, referer_optional string, client *http.Client, isAjax bool) string {
|
||||||
|
req, _ := http.NewRequest("POST", url, bytes.NewBuffer([]byte(data)))
|
||||||
|
req.Header.Set("User-Agent", randomUserAgent())
|
||||||
|
if isAjax {
|
||||||
|
req.Header.Set("X-Requested-With", "XMLHttpRequest")
|
||||||
|
}
|
||||||
|
req.Header.Set("Accept-Language", "en-us")
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
||||||
|
if referer_optional != "" {
|
||||||
|
req.Header.Set("Referer", referer_optional)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Cannot post url %s, err: %s", url, err.Error())
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
contents, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
return string(contents)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HttpPostStringToken(url string, data string, referer_optional string, token string, client *http.Client, isAjax bool) string {
|
||||||
|
req, _ := http.NewRequest("POST", url, bytes.NewBuffer([]byte(data)))
|
||||||
|
req.Header.Set("User-Agent", randomUserAgent())
|
||||||
|
if isAjax {
|
||||||
|
req.Header.Set("X-Requested-With", "XMLHttpRequest")
|
||||||
|
}
|
||||||
|
req.Header.Set("Accept-Language", "en-us")
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
||||||
|
if referer_optional != "" {
|
||||||
|
req.Header.Set("Referer", referer_optional)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("X-CSRF-Token", token)
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Cannot post url %s, err: %s", url, err.Error())
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
contents, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
return string(contents)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HttpPost(url string, data url.Values, client *http.Client) string {
|
||||||
|
req, _ := http.NewRequest("POST", url, bytes.NewBuffer([]byte(data.Encode())))
|
||||||
|
req.Header.Set("User-Agent", randomUserAgent())
|
||||||
|
req.Header.Set("X-Requested-With", "XMLHttpRequest")
|
||||||
|
req.Header.Set("Accept-Language", "en-us")
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
||||||
|
req.Header.Set("Referer", "http://user.everbuying.net/m-users-a-sign.htm")
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Cannot post url %s, err: %s", url, err.Error())
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode == 302 && client.CheckRedirect != nil {
|
||||||
|
return resp.Header.Get("Location")
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
contents, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
return string(contents)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HttpDownloadFile(url string, filepath string, client *http.Client) error {
|
||||||
|
// Create the file
|
||||||
|
out, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer out.Close()
|
||||||
|
|
||||||
|
// Get the data
|
||||||
|
req, _ := http.NewRequest("GET", url, nil)
|
||||||
|
req.Header.Set("User-Agent", randomUserAgent())
|
||||||
|
req.Header.Set("Accept-Language", "en-us")
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Cannot get url %s, err: %s", url, err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// Writer the body to file
|
||||||
|
_, err = io.Copy(out, resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getmyip() {
|
||||||
|
client := &http.Client{}
|
||||||
|
fmt.Print(HttpGet("https://api.ipify.org?format=json", client))
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Random(min, max int) int {
|
||||||
|
rand.Seed(time.Now().Unix())
|
||||||
|
return rand.Intn(max-min) + min
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user