-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
136 lines (114 loc) · 2.82 KB
/
data.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"encoding/json"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)
type proxiesApi struct {
Message string `json:"message"`
Status int `json:"status"`
Data []struct {
Targets []string `json:"targets"`
ID string `json:"_id"`
User string `json:"user"`
Options map[string]string `json:"options"`
Hostname string `json:"hostname"`
Expiry time.Time `json:"expiry"`
Plan string `json:"plan"`
} `json:"data"`
}
type plansApi struct {
Message string `json:"message"`
Status int `json:"status"`
Data []struct {
ID string `json:"_id"`
Connections int `json:"connections"`
Custom bool `json:"custom"`
} `json:"data"`
}
type analyticsApi struct {
Message string `json:"message"`
Status int `json:"status"`
Data []struct {
Connections map[string]int `json:"connections"`
ID string `json:"_id"`
ProxyID string `json:"proxy_id"`
} `json:"data"`
}
type serversApi struct {
Message string `json:"message"`
Status int `json:"status"`
Data []struct {
ID string `json:"_id"`
IPAddress string `json:"ip_address"`
APIKey string `json:"api_key"`
} `json:"data"`
}
type Proxies map[string]Server
type Target struct {
IPAddress string
Port string
Connections int
Online bool
}
type Server struct {
Hostname string
Targets []Target
Options map[string]interface{}
Plan string
}
func Update(proxies *Proxies, signer Signer) {
var proxiesTemp proxiesApi
getProxies(&proxiesTemp)
for _, p := range proxiesTemp.Data {
(*proxies)[strings.ToLower(p.Hostname)] = Server{Hostname: p.Hostname, Targets: createTargets(p.Targets, signer)}
}
}
func createTargets(t []string, signer Signer) []Target {
var targets []Target
for _, target := range t {
tStr := strings.Split(target, ":")
tLen := len(tStr)
tPort := "25565"
if tLen > 1 {
tPort = tStr[1]
}
online := true
hostStr := tStr[0] + ":" + tPort
// TODO make it figure out if it is online lolz
if !QueryStatus(hostStr, 150*time.Millisecond, signer) {
online = false
}
targets = append(targets, Target{
IPAddress: tStr[0],
Port: tPort,
Connections: 0,
Online: online,
})
}
return targets
}
func getProxies(proxies *proxiesApi) {
p, err := apiPull("proxies")
if err != nil {
log.Fatal(err)
}
json.Unmarshal(p, &proxies)
}
func apiPull(path string) ([]byte, error) {
var apiURL = strings.ReplaceAll(os.Getenv("API_URL"), "%key%", os.Getenv("API_KEY"))
resp, err := http.Get(strings.ReplaceAll(apiURL, "%path%", path))
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
return body, err
}