-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathicon.go
69 lines (52 loc) · 1.23 KB
/
icon.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package mcstatus
import (
"bytes"
_ "embed"
"fmt"
"image"
"image/png"
"net/http"
"net/url"
"strconv"
)
var (
//go:embed icon.png
defaultIconBytes []byte
defaultIcon image.Image = nil
)
func init() {
var err error
if defaultIcon, err = png.Decode(bytes.NewReader(defaultIconBytes)); err != nil {
panic(err)
}
}
type IconOptions struct {
Timeout float64
}
func GetIcon(host string, port uint16, options ...IconOptions) (image.Image, error) {
opts := IconOptions{
Timeout: 5.0,
}
if len(options) > 0 {
opts = options[0]
}
params := &url.Values{}
params.Set("timeout", strconv.FormatFloat(opts.Timeout, 'f', 1, 64))
req, err := http.NewRequest("GET", fmt.Sprintf("%s/icon/%s:%s?%s", baseURL, url.PathEscape(host), url.PathEscape(strconv.FormatUint(uint64(port), 10)), params.Encode()), nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", fmt.Sprintf("go-mcstatus %s", version))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("mcstatus: unexpected status code: %d", resp.StatusCode)
}
defer resp.Body.Close()
return png.Decode(resp.Body)
}
func GetDefaultIcon() image.Image {
return defaultIcon
}