-
Notifications
You must be signed in to change notification settings - Fork 87
/
sas.go
120 lines (109 loc) · 1.98 KB
/
sas.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
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
package libv2ray
import (
"io/ioutil"
"log"
"net/http"
"time"
)
type StabilityAssist struct {
Engaged bool
Forground bool
intr chan int
sharedClient *http.Client
SASS StabilityAssistSupportSet
armed bool
}
func (sa *StabilityAssist) monitor() {
select {
case <-sa.intr:
default:
}
log.Println("Monitor Engaged")
first := true
waitsec := 15
for sa.Engaged {
if first {
waitsec = 30
first = false
} else {
if !sa.Forground {
waitsec = (waitsec * 3) / 2
if waitsec > 500 {
waitsec = 500
}
} else {
waitsec = 15
}
}
currentWait := time.Tick(time.Duration(waitsec) * time.Second)
select {
case <-currentWait:
sa.probe()
case s := <-sa.intr:
if s != 0 {
sa.probe()
}
continue
}
}
}
func (sa *StabilityAssist) ProbeNowOpi() {
select {
case sa.intr <- 1:
default:
}
}
func (sa *StabilityAssist) probe() {
if sa.sharedClient == nil {
tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 600 * time.Second,
}
sa.sharedClient = &http.Client{Transport: tr, Timeout: 20 * time.Second}
}
log.Println("Probing")
res, err := sa.sharedClient.Get("https://static.kkdev.org/CONNTEST")
if err != nil {
log.Println("Probing fail1", err)
sa.probeFail()
return
}
_, err = ioutil.ReadAll(res.Body)
if err != nil {
log.Println("Probing fail2", err)
sa.probeFail()
return
}
res.Body.Close()
sa.armed = true
log.Println("Armed")
}
func (sa *StabilityAssist) probeFail() {
if sa.armed {
sa.Engaged = false
sa.SASS.OnProbeFailed()
}
}
func (sa *StabilityAssist) Start() {
sa.Engaged = true
sa.armed = false
select {
case sa.intr <- 0:
default:
}
go sa.monitor()
}
func (sa *StabilityAssist) Stop() {
sa.Engaged = false
select {
case sa.intr <- 0:
default:
}
}
func GetStabilityAssist() *StabilityAssist {
log.SetPrefix("SAS")
return &StabilityAssist{intr: make(chan int, 1), Forground: true}
}
type StabilityAssistSupportSet interface {
OnProbeFailed()
}