-
Notifications
You must be signed in to change notification settings - Fork 6
/
proto.go
61 lines (49 loc) · 1.04 KB
/
proto.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
package prox5
import (
"sync"
"sync/atomic"
"git.tcp.direct/kayos/common/pool"
)
type ProxyProtocol int8
const (
// ProtoNull is a null value for ProxyProtocol.
ProtoNull ProxyProtocol = iota
ProtoSOCKS4
ProtoSOCKS4a
ProtoSOCKS5
ProtoHTTP
)
var protoMap = map[ProxyProtocol]string{
ProtoSOCKS5: "socks5", ProtoNull: "unknown", ProtoSOCKS4: "socks4", ProtoSOCKS4a: "socks4a",
}
func (p ProxyProtocol) String() string {
return protoMap[p]
}
type proto struct {
proto *atomic.Value
// immutable
*sync.Once
}
func newImmutableProto() proto {
p := proto{
proto: &atomic.Value{},
Once: &sync.Once{},
}
p.proto.Store(ProtoNull)
return p
}
func (p *proto) Get() ProxyProtocol {
return p.proto.Load().(ProxyProtocol)
}
func (p *proto) set(proxyproto ProxyProtocol) {
p.Do(func() {
p.proto.Store(proxyproto)
})
}
func (p ProxyProtocol) writeProtoString(builder *pool.String) {
builder.MustWriteString(p.String())
}
func (p ProxyProtocol) writeProtoURI(builder *pool.String) {
p.writeProtoString(builder)
builder.MustWriteString("://")
}