-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgoHAProxy.go
140 lines (127 loc) · 3.67 KB
/
goHAProxy.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"time"
)
var version string = "0.0.2"
var proxyServer ProxyServer
var configInfo ConfigInfo
var help *bool
func main() {
configInfo.FileName = flag.String("config", "./config.json", "set config file path.")
configInfo.Debug = flag.Bool("debug", false, "show debug trace message.")
configInfo.Version = flag.Bool("version", false, "GoHAProxy version.")
help = flag.Bool("help", false, "Show help information.")
flag.Parse()
if *help {
fmt.Printf("GoHAProxy is simple HAProxy by Golang,You can use this to load balance TCP or check health status.\n")
fmt.Println("GoHAProxy Version", version)
fmt.Printf("Monitor server:local ip:8080, Default value:[:8080]\n")
fmt.Printf("-config Set cofing file path. Default value:%v\n", *configInfo.FileName)
fmt.Printf("-debug Show debug trace message. Default value:%v\n", *configInfo.Debug)
fmt.Printf("-version Show GoHAProxy version.\n")
fmt.Printf("-help Show help information.\n")
os.Exit(0)
}
if *configInfo.Version {
fmt.Println("GoHAProxy Version", version)
os.Exit(0)
}
ok,haConfig := loadConfigs()
if ok {
for _, proxy := range haConfig.Configs.ProxyList {
FS := new(ForwardServer)
proxyServer.ServerList = append(proxyServer.ServerList, FS)
go FS.Listen(proxy)
}
}
go Monitor()
for {
configWatcher()
time.Sleep(500 * time.Millisecond)
}
}
func configWatcher() {
file, err := os.Open(*configInfo.FileName) // For read access.
if err != nil {
fmt.Println(err)
}
info, err := file.Stat()
if err != nil {
fmt.Println(err)
}
if configInfo.Size == 0 {
configInfo.Size = info.Size()
configInfo.ModTime = info.ModTime()
}
if info.Size() != configInfo.Size || info.ModTime() != configInfo.ModTime {
fmt.Printf("Config changed.Reolad.\n")
configInfo.Size = info.Size()
configInfo.ModTime = info.ModTime()
ok,haConfig := loadConfigs()
if ok {
//檢查有沒有移除掉的設定有的話就移除
for k, sProxy := range proxyServer.ServerList {
oldProxy := true
for _, proxy := range haConfig.Configs.ProxyList {
if *configInfo.Debug {
fmt.Printf("Delete PName:%s srvProxyName:%s \n", proxy.Name, sProxy.srvProxy.Name)
}
if proxy.Name == sProxy.srvProxy.Name {
oldProxy = false
break
}
}
if oldProxy {
if *configInfo.Debug {
fmt.Printf("Delete Proxy[%v]: %v\n", k, sProxy.srvProxy.Name)
}
sProxy.Stop()
proxyServer.ServerList = proxyServer.ServerList[:k+copy(proxyServer.ServerList[k:], proxyServer.ServerList[k+1:])]
}
}
//檢查有沒有新的設定
for _, proxy := range haConfig.Configs.ProxyList {
newProxy := true
for k, sProxy := range proxyServer.ServerList {
if *configInfo.Debug {
fmt.Printf("Check Add PName[%d]:%s srvProxyName:%s \n", k, proxy.Name, sProxy.srvProxy.Name)
}
if proxy.Name == sProxy.srvProxy.Name {
proxyServer.ServerList[k].Reload(proxy)
newProxy = false
break
}
}
if newProxy {
FS := new(ForwardServer)
proxyServer.ServerList = append(proxyServer.ServerList, FS)
if *configInfo.Debug {
fmt.Printf("Add New Proxy: %v\n", proxy)
}
go FS.Listen(proxy)
}
}
}
}
defer file.Close()
}
func loadConfigs() (bool,HAConfig) {
fmt.Printf("GoHAProxy load config file:%s\n", *configInfo.FileName)
file, e := ioutil.ReadFile(*configInfo.FileName)
if e != nil {
fmt.Printf("Load GoHAProxy config error: %v\n", e)
os.Exit(1)
}
var haConfig HAConfig
err := json.Unmarshal(file, &haConfig)
if err != nil {
fmt.Printf("Config load error:%v \n",err)
return false,haConfig
}
return true,haConfig
}