-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmain.go
89 lines (73 loc) · 1.67 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"strings"
"sync"
)
var (
flags struct {
Path string
VerCode bool
}
Data struct {
API int `json:"api"`
Secret string `json:"secret"`
Domain []string `json:"domains"`
Address string `json:"address"`
Upstream string `json:"upstream"`
AllowedIPs []string `json:"allowedips"`
}
Mutex sync.RWMutex
version = "1.0.0"
)
func main() {
flag.StringVar(&flags.Path, "c", "", "Path")
flag.BoolVar(&flags.VerCode, "v", false, "VerCode")
flag.Parse()
if flags.VerCode {
fmt.Println(version)
return
}
data, err := ioutil.ReadFile(flags.Path)
if err != nil {
log.Fatalf("[APP][ioutil.ReadFile] %v", err)
}
if err := json.Unmarshal(data, &Data); err != nil {
log.Fatalf("[APP][json.Unmarshal] %v", err)
}
go startHTTP()
go startTLS()
go beginDNS()
mux := http.NewServeMux()
mux.HandleFunc("/aio", func(w http.ResponseWriter, r *http.Request) {
secret := r.URL.Query().Get("secret")
if secret == "" {
fmt.Fprintf(w, "FAIL: No Secret")
return
}
if strings.EqualFold(Data.Secret, secret) {
Mutex.Lock()
defer Mutex.Unlock()
address, _, _ := net.SplitHostPort(r.RemoteAddr)
fmt.Fprintf(w, "DONE: %s", address)
for i := 0; i < len(Data.AllowedIPs); i++ {
if Data.AllowedIPs[i] == address {
return
}
}
Data.AllowedIPs = append(Data.AllowedIPs, address)
return
}
fmt.Fprintf(w, "FAIL: Unknown Secret")
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "OK")
})
log.Fatalf("[APP][API] %v", http.ListenAndServe(fmt.Sprintf(":%d", Data.API), mux))
}