-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingfetchergofood.go
53 lines (47 loc) · 1.12 KB
/
pingfetchergofood.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"bytes"
"compress/gzip"
"io"
"net/http"
"github.com/andybalholm/brotli"
)
func getGofoodStatus(url string) (string, http.Header, int, []byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", nil, 0, nil, err
}
emulateBrowser(req)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", nil, 0, nil, err
}
defer resp.Body.Close()
var body []byte
encoding := resp.Header.Get("Content-Encoding")
switch encoding {
case "br":
bodyReader := brotli.NewReader(resp.Body)
body, err = io.ReadAll(bodyReader)
case "gzip":
bodyReader, readerErr := gzip.NewReader(resp.Body)
if readerErr == nil {
body, err = io.ReadAll(bodyReader)
} else {
err = readerErr
}
default:
body, err = io.ReadAll(resp.Body)
}
if err != nil {
return "", resp.Header, resp.StatusCode, body, err
}
if resp.StatusCode >= 400 {
return "unknown", resp.Header, resp.StatusCode, body, nil
}
if bytes.Contains(body, []byte(">Tutup")) {
return "closed", resp.Header, resp.StatusCode, body, nil
}
return "open", resp.Header, resp.StatusCode, body, nil
}