-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.go
225 lines (198 loc) · 5.34 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/valyala/fasthttp"
"log"
"os"
"strconv"
"time"
"github.com/dbldqt/wechatTokenServer/config"
"github.com/dbldqt/wechatTokenServer/wechat"
)
var configFile string
var test bool
func init(){
flag.StringVar(&configFile,"conf","./config.toml","assign the config file path")
flag.BoolVar(&test,"test",false,"is test config file")
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}
func main(){
flag.Parse()
conf,err := config.LoadConfig(configFile)
if err != nil{
log.Panicln("config file error:"+err.Error())
}
file,err := os.OpenFile(conf.LogFile,os.O_CREATE | os.O_APPEND | os.O_RDWR,0666)
if err != nil{
log.Println("open log file error "+err.Error())
}else{
log.SetOutput(file)
}
config.GetConfigMan().SetConfig(conf)
//测试配置文件成功
if test{
fmt.Println("config file is right")
return
}
wechatman,err := wechat.BuildWechatMan(conf.GetAheadTime(),conf.GetLoopTime(),conf.GetWechatConfigs()...)
if err != nil{
log.Panicln("get wehcatman error "+err.Error())
}
err = wechatman.Run()
if err != nil{
log.Panicln("wechatman run error "+err.Error())
}
err = fasthttp.ListenAndServe(":"+strconv.Itoa(conf.GetPort()),requesthandler)
if err != nil{
log.Panicln("fasthttp error "+err.Error())
return
}
}
type Result struct{
AccessToken string `json:"accessToken"`
Msg string `json:"msg"`
ServerTime int64 `json:"serverTime"`
ExpireAt int64 `json:"expireAt"`
}
func requesthandler(ctx *fasthttp.RequestCtx){
log.Println(ctx.RemoteIP().String()+" request "+ctx.URI().String())
if !ctx.IsGet(){
ctx.Response.SetBody([]byte("only get supported"))
return
}
switch(string(ctx.Path())){
case "/query":
if !ctx.QueryArgs().Has("appid") || !ctx.QueryArgs().Has("token"){
ctx.Response.SetBody([]byte("param not enough"))
return
}
if !QueryIpAuth(ctx.RemoteIP().String()){
ctx.Response.SetBody([]byte("ip not in white list"))
return
}
appid := ctx.QueryArgs().Peek("appid")
token := ctx.QueryArgs().Peek("token")
result := Result{
ServerTime:time.Now().Unix(),
}
wechatman,err := wechat.GetWechatMan()
if err != nil{
log.Panicln("get wechatman error "+err.Error())
}
accessToken,expireAt,err := wechatman.QueryAccessToken(string(appid),string(token))
if err != nil{
log.Println(string(appid)+"query accesstoken error "+err.Error())
result.Msg = err.Error()
}else{
log.Println(string(appid)+"query accesstoken success")
result.Msg = "success"
}
result.ExpireAt = expireAt
result.AccessToken = accessToken
res,err := json.Marshal(result)
if err !=nil{
log.Panicln("marshal error"+err.Error())
ctx.Response.SetBody([]byte(err.Error()))
}else{
ctx.Response.SetBody(res)
}
break
case "/update":
if !ctx.QueryArgs().Has("appid") || !ctx.QueryArgs().Has("token"){
ctx.Response.SetBody([]byte("param not enough"))
return
}
if !QueryIpAuth(ctx.RemoteIP().String()){
ctx.Response.SetBody([]byte("ip not in white list"))
return
}
appid := ctx.QueryArgs().Peek("appid")
token := ctx.QueryArgs().Peek("token")
wechatman,err := wechat.GetWechatMan()
if err != nil{
log.Panicln("get wechatman error "+err.Error())
return
}
_,_,err = wechatman.QueryAccessToken(string(appid),string(token))
if err != nil{
log.Println(string(appid)+"update accesstoken error "+err.Error())
ctx.Response.SetBody([]byte("{\"msg\":\""+err.Error()+"\"}"))
return
}
log.Println(string(appid)+"update success")
wechatman.ForceRefreshAccessToken(string(appid))
ctx.Response.SetBody([]byte("{\"msg\":\"success\"}"))
break
case "/reload":
if !ctx.QueryArgs().Has("token"){
ctx.Response.SetBody([]byte("param not enough"))
return
}
if !ReloadIpAuth(ctx.RemoteIP().String()){
ctx.Response.SetBody([]byte("ip not in white list"))
return
}
token := string(ctx.QueryArgs().Peek("token"))
adminToken := config.GetConfigMan().GetConfig().GetAdminToken()
if string(token) != adminToken{
ctx.Response.SetBody([]byte("token error"))
return
}
conf,err := config.LoadConfig(configFile)
if err != nil{
ctx.Response.SetBody([]byte(err.Error()))
return
}
config.GetConfigMan().SetConfig(conf)
wechatMan,err := wechat.GetWechatMan()
if err != nil {
log.Panicln("get wechatman error "+err.Error())
ctx.Response.SetBody([]byte(err.Error()))
return
}
go func (){
err = wechatMan.Rebuild(conf.GetAheadTime(),conf.GetLoopTime(),conf.GetWechatConfigs()...)
if err != nil{
log.Panicln("rebuild error "+err.Error())
}
log.Println("reload success")
}()
ctx.Response.SetBody([]byte("config is reloading"))
break
default:
ctx.Response.SetBody([]byte("no this route"))
}
}
func QueryIpAuth(ip string) bool{
conf := config.GetConfigMan().GetConfig()
conf.RLock()
if !conf.UseIpWhiteList{
return true
}
iplist := conf.GetIpList()
for _,ipSet := range iplist{
if ipSet == ip {
conf.RUnlock()
return true
}
}
conf.RUnlock()
log.Println(ip+"not in ip list")
return false
}
func ReloadIpAuth(ip string) bool{
conf := config.GetConfigMan().GetConfig()
conf.RLock()
iplist := conf.GetAdminIpList()
for _,ipSet := range iplist{
if ipSet == ip {
conf.RUnlock()
return true
}
}
conf.RUnlock()
log.Println(ip+"not in admin ip list")
return false
}