/
admin.go
196 lines (183 loc) · 5.06 KB
/
admin.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package local
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
//_ "net/http/pprof"
"os"
"time"
"github.com/yinqiwen/gotoolkit/iotools"
"github.com/yinqiwen/gotoolkit/ots"
"github.com/yinqiwen/gsnova/common/channel"
"github.com/yinqiwen/gsnova/common/helper"
"github.com/yinqiwen/gsnova/common/logger"
"github.com/yinqiwen/gsnova/common/netx"
)
var httpDumpLog *iotools.RotateFile
func getConfigList(w http.ResponseWriter, r *http.Request) {
var confs []string
files, _ := ioutil.ReadDir(GConf.Admin.ConfigDir)
for _, f := range files {
if f.IsDir() {
confs = append(confs, f.Name())
}
}
w.Header().Set("Content-Type", "application/json")
js, _ := json.Marshal(confs)
w.Write(js)
}
func statCallback(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprintf(w, "Version: %s\n", channel.Version)
//fmt.Fprintf(w, "NumSession: %d\n", getProxySessionSize())
ots.Handle("stat", w)
fmt.Fprintf(w, "RunningProxyStreamNum: %d\n", runningProxyStreamCount)
channel.DumpLoaclChannelStat(w)
}
func stackdumpCallback(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200)
ots.Handle("stackdump", w)
}
func memdumpCallback(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200)
ots.Handle("MemProfile", w)
}
func gcCallback(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200)
ots.Handle("gc", w)
}
func httpDumpCallback(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
if nil != httpDumpLog {
io.Copy(httpDumpLog, r.Body)
} else {
r.Body.Close()
}
}
func startAdminServer() {
if len(GConf.Admin.Listen) == 0 {
return
}
if len(GConf.Admin.ConfigDir) == 0 {
logger.Error("[WARN]The ConfigDir's Dir is empty, use current dir instead")
GConf.Admin.ConfigDir = "./"
}
if len(GConf.Admin.BroadcastAddr) > 0 {
ticker := time.NewTicker(3 * time.Second)
localIP := helper.GetLocalIPv4()
_, adminPort, _ := net.SplitHostPort(GConf.Admin.Listen)
go func() {
for _ = range ticker.C {
addr, err := net.ResolveUDPAddr("udp", GConf.Admin.BroadcastAddr)
var c *net.UDPConn
if nil == err {
c, err = net.DialUDP("udp", nil, addr)
}
if err != nil {
logger.Error("Failed to resolve multicast addr.")
} else {
for _, ip := range localIP {
c.Write([]byte(net.JoinHostPort(ip, adminPort)))
}
}
}
}()
}
httpDumpLog = &iotools.RotateFile{
Path: proxyHome + "httpdump.log",
MaxBackupIndex: 2,
MaxFileSize: 1024 * 1024,
SyncBytesPeriod: 1024 * 1024,
}
mux := http.NewServeMux()
fs := http.FileServer(http.Dir(GConf.Admin.ConfigDir))
mux.Handle("/", fs)
mux.HandleFunc("/_conflist", getConfigList)
mux.HandleFunc("/stat", statCallback)
mux.HandleFunc("/stackdump", stackdumpCallback)
mux.HandleFunc("/gc", gcCallback)
mux.HandleFunc("/memdump", memdumpCallback)
mux.HandleFunc("/httpdump", httpDumpCallback)
err := http.ListenAndServe(GConf.Admin.Listen, mux)
if nil != err {
logger.Error("Failed to start config store server:%v", err)
}
}
var syncClient *http.Client
func syncConfigFile(addr string, localDir, remoteDir string, fileName string) (bool, error) {
filePath := remoteDir + "/" + fileName
localFilePath := localDir + "/" + filePath
localInfo, err := os.Stat(localFilePath)
fileURL := "http://" + addr + "/" + filePath
if err == nil {
localFileModTime := localInfo.ModTime()
headResp, err := syncClient.Head(fileURL)
if nil != err {
return false, err
}
lastModifiedHeader, _ := http.ParseTime(headResp.Header.Get("Last-Modified"))
if lastModifiedHeader.Before(localFileModTime) {
//log.Printf("Config file:%v is not update.", localFilePath)
return false, nil
}
}
resp, err := syncClient.Get(fileURL)
if nil != err {
return false, err
}
if resp.StatusCode != 200 || nil == resp.Body {
if nil != resp.Body {
resp.Body.Close()
}
if resp.StatusCode == 404 {
return false, nil
}
return false, fmt.Errorf("Invalid response:%v for %s", resp, filePath)
}
data, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
os.Mkdir(localDir+"/"+remoteDir, 0775)
ioutil.WriteFile(localFilePath, data, 0660)
log.Printf("Persistent config:%s.", localDir+"/"+filePath)
return true, nil
}
func SyncConfig(addr string, localDir string) (bool, error) {
if nil == syncClient {
tr := &http.Transport{}
tr.ResponseHeaderTimeout = 15 * time.Second
tr.Dial = netx.Dial
syncClient = &http.Client{}
syncClient.Timeout = 15 * time.Second
syncClient.Transport = tr
}
resp, err := syncClient.Get("http://" + addr + "/_conflist")
if nil != err {
log.Printf("Error %v with local ip:%v", err, helper.GetLocalIPv4())
return false, err
}
data, _ := ioutil.ReadAll(resp.Body)
var confList []string
json.Unmarshal(data, &confList)
update := false
for _, conf := range confList {
u := false
files := []string{"client.json", "hosts.json", "cnipset.txt"}
for _, file := range files {
u, err = syncConfigFile(addr, localDir, conf, file)
if nil != err {
return false, err
}
if u {
update = true
}
if u {
log.Printf("Synced config:%s success", conf)
}
}
}
return update, nil
}