-
Notifications
You must be signed in to change notification settings - Fork 13
/
endpoint.go
109 lines (91 loc) · 1.95 KB
/
endpoint.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
package mixin
import (
"net/http"
"net/url"
"os"
"strings"
"time"
)
const (
DefaultApiHost = "https://api.mixin.one"
DefaultBlazeHost = "blaze.mixin.one"
ZeromeshApiHost = "https://mixin-api.zeromesh.net"
ZeromeshBlazeHost = "mixin-blaze.zeromesh.net"
EchoApiHost = "https://echo.yiplee.com"
)
func UseApiHost(host string) {
httpClient.HostURL = host
}
var (
blazeURL = buildBlazeURL(DefaultBlazeHost)
)
func buildBlazeURL(host string) string {
u := url.URL{Scheme: "wss", Host: host}
return u.String()
}
func UseBlazeHost(host string) {
blazeURL = buildBlazeURL(host)
}
func UseBlazeURL(rawURL string) {
u, err := url.Parse(rawURL)
if err != nil {
panic(err)
}
blazeURL = u.String()
}
func useApi(url string) <-chan string {
r := make(chan string)
go func() {
defer close(r)
_, err := http.Get(url)
if err == nil {
r <- url
}
}()
return r
}
func timer() <-chan string {
r := make(chan string)
go func() {
defer close(r)
time.Sleep(time.Second * 30)
r <- ""
}()
return r
}
func UseAutoFasterRoute() {
for {
var r string
select {
case r = <-useApi(DefaultApiHost):
case r = <-useApi(ZeromeshApiHost):
case r = <-timer():
}
if r == DefaultApiHost {
UseApiHost(DefaultApiHost)
UseBlazeHost(DefaultBlazeHost)
} else if r == ZeromeshApiHost {
UseApiHost(ZeromeshApiHost)
UseBlazeHost(ZeromeshBlazeHost)
}
time.Sleep(time.Minute * 5)
}
}
func init() {
if _, ok := os.LookupEnv("MIXIN_SDK_USE_ZEROMESH"); ok {
UseApiHost(ZeromeshApiHost)
UseBlazeHost(ZeromeshBlazeHost)
}
if host, ok := os.LookupEnv("MIXIN_SDK_API_HOST"); ok && host != "" {
UseApiHost(host)
}
if host, ok := os.LookupEnv("MIXIN_SDK_BLAZE_HOST"); ok && host != "" {
UseBlazeHost(host)
}
if rawURL, ok := os.LookupEnv("MIXIN_SDK_BLAZE_URL"); ok && rawURL != "" {
UseBlazeURL(rawURL)
}
if hosts, ok := os.LookupEnv("MIXIN_SDK_MIXINNET_HOSTS"); ok && hosts != "" {
UseMixinNetHosts(strings.Split(hosts, ","))
}
}