-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
122 lines (105 loc) · 2.21 KB
/
utils.go
File metadata and controls
122 lines (105 loc) · 2.21 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package fcbreak
import (
"context"
"crypto/tls"
"io"
"net"
"net/http"
"regexp"
"strconv"
"strings"
"sync"
ua "github.com/mileusna/useragent"
)
type contextKey struct {
key string
}
var connContextKey = &contextKey{"http-conn"}
var bufferPool = sync.Pool{
New: func() interface{} {
return make([]byte, 32*1024)
},
}
func saveConnInContext(ctx context.Context, c net.Conn) context.Context {
return context.WithValue(ctx, connContextKey, c)
}
func getConn(r *http.Request) net.Conn {
return r.Context().Value(connContextKey).(net.Conn)
}
func getConnUnwarpTLS(r *http.Request) net.Conn {
conn := getConn(r)
if tc, ok := conn.(*tls.Conn); ok {
return tc.NetConn()
}
return conn
}
func transport(rw1, rw2 io.ReadWriter) error {
errc := make(chan error, 1)
go func() {
errc <- copyBuffer(rw1, rw2)
}()
go func() {
errc <- copyBuffer(rw2, rw1)
}()
if err := <-errc; err != nil && err != io.EOF {
return err
}
return nil
}
func copyBuffer(dst io.Writer, src io.Reader) error {
buf := bufferPool.Get().([]byte)
defer bufferPool.Put(buf)
_, err := io.CopyBuffer(dst, src, buf)
return err
}
func supportAltSvc(useragent string) bool {
a := ua.Parse(useragent)
ver_str := strings.Split(a.Version, ".")
var v []int
for _, ver := range ver_str {
v_int, err := strconv.Atoi(ver)
if err != nil {
v_int = 0
}
v = append(v, v_int)
}
if a.IsChrome() && v[0] > 79 {
return true
}
if a.IsEdge() && v[0] > 79 {
return true
}
if a.IsSafari() {
return true
}
if a.IsFirefox() && v[0] > 38 {
return true
}
if a.IsOpera() && v[0] > 81 {
return true
}
return false
}
// onceCloseListener wraps a net.Listener, protecting it from
// multiple Close calls.
type onceCloseListener struct {
net.Listener
once sync.Once
}
func (oc *onceCloseListener) Close() (err error) {
oc.once.Do(func() {
err = oc.Listener.Close()
})
return
}
func verifyHostname(hostname string) bool {
var ok bool
if strings.HasPrefix(hostname, "*") {
ok, _ = regexp.MatchString(`^[a-zA-Z0-9\.]+$`, hostname[1:])
} else if strings.HasSuffix(hostname, "*") {
ok, _ = regexp.MatchString(`^[a-zA-Z0-9\.]+$`, hostname[:len(hostname)-1])
} else {
ok, _ = regexp.MatchString(`^[a-zA-Z0-9\.]+$`, hostname)
}
return ok
}