-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathutil.go
More file actions
102 lines (92 loc) · 2.16 KB
/
util.go
File metadata and controls
102 lines (92 loc) · 2.16 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// wrp utility functions
package main
import (
"encoding/json"
"image"
"image/color"
"log"
"net"
"net/http"
"strings"
"time"
"github.com/MaxHalford/halfgone"
"github.com/ericpauley/go-quantize/quantize"
)
func printMyIPs(b string) {
ap := strings.Split(b, ":")
if len(ap) < 2 {
log.Fatal("Wrong format of ipaddress:port")
}
port := ap[len(ap)-1]
if ap[0] != "" && ap[0] != "0.0.0.0" {
log.Printf("Listen address: %v", b)
return
}
a, err := net.InterfaceAddrs()
if err != nil {
log.Printf("Listen address: %v", b)
return
}
var m string
for _, i := range a {
n, ok := i.(*net.IPNet)
if !ok || n.IP.IsLoopback() || strings.Contains(n.IP.String(), ":") {
continue
}
m += n.IP.String() + ":" + port + " "
}
log.Printf("Listen address: %v", m)
}
func gifPalette(i image.Image, n int64) image.Image {
switch n {
case 2:
i = halfgone.FloydSteinbergDitherer{}.Apply(halfgone.ImageToGray(i))
default:
q := quantize.MedianCutQuantizer{}
p := q.Quantize(make([]color.Color, 0, int(n)), i)
bounds := i.Bounds()
quantized := image.NewPaletted(bounds, p)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
quantized.Set(x, y, i.At(x, y))
}
}
i = quantized
}
return i
}
func asciify(s []byte) []byte {
a := make([]byte, len(s))
for i := 0; i < len(s); i++ {
if s[i] > 127 {
a[i] = '.'
continue
}
a[i] = s[i]
}
return a
}
func fetchJnrbsnUserAgent() string {
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Get("https://jnrbsn.github.io/user-agents/user-agents.json")
if err != nil {
log.Printf("Failed to fetch user agents from jnrbsn: %v", err)
return ""
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Printf("jnrbsn API returned status: %d", resp.StatusCode)
return ""
}
var userAgents []string
if err := json.NewDecoder(resp.Body).Decode(&userAgents); err != nil {
log.Printf("Failed to decode jnrbsn user agents JSON: %v", err)
return ""
}
if len(userAgents) == 0 {
log.Printf("jnrbsn API returned no user agents")
return ""
}
log.Printf("Fetched user agent from jnrbsn: %s", userAgents[0])
return userAgents[0]
}