初始化项目
This commit is contained in:
93
trunk/framework/ipMgr/ipCheck.go
Normal file
93
trunk/framework/ipMgr/ipCheck.go
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
提供统一的ip验证的逻辑;包括两部分:
|
||||
1、ManageCenter中配置到ServerGroup中的IP;系统内部自动处理,外部无需关注(暂时不用);
|
||||
2、各个应用程序中配置的ip;通过调用Init或InitString方法进行初始化;
|
||||
*/
|
||||
package ipMgr
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"goutil/stringUtil"
|
||||
)
|
||||
|
||||
var (
|
||||
ipMap = make(map[string]struct{}, 32) // 本地ip集合
|
||||
ipCheckFuncList = make([]func(string) bool, 0, 16) // ip检查函数列表
|
||||
mutex sync.RWMutex
|
||||
)
|
||||
|
||||
// 初始化IP列表
|
||||
func Init(ipList []string) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
// 先清空再初始化
|
||||
ipMap = make(map[string]struct{}, 32)
|
||||
for _, item := range ipList {
|
||||
ipMap[item] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化ip字符串(以分隔符分割的,分隔符为:",", ";", ":", "|", "||")
|
||||
func InitString(ipStr string) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
// 先清空再初始化
|
||||
ipMap = make(map[string]struct{}, 32)
|
||||
for _, item := range stringUtil.Split(ipStr, nil) {
|
||||
ipMap[item] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
func AddString(ipStr string) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
// 先清空再初始化
|
||||
if ipMap == nil {
|
||||
ipMap = make(map[string]struct{}, 32)
|
||||
}
|
||||
|
||||
for _, item := range stringUtil.Split(ipStr, nil) {
|
||||
ipMap[item] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterIpCheckFunc 注册Ip检查函数
|
||||
func RegisterIpCheckFunc(funcName string, funcItem func(string) bool) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
ipCheckFuncList = append(ipCheckFuncList, funcItem)
|
||||
}
|
||||
|
||||
func isLocalValid(ip string) bool {
|
||||
mutex.RLock()
|
||||
defer mutex.RUnlock()
|
||||
|
||||
_, exist := ipMap[ip]
|
||||
return exist
|
||||
}
|
||||
|
||||
func isCheckFuncValid(ip string) bool {
|
||||
mutex.RLock()
|
||||
defer mutex.RUnlock()
|
||||
|
||||
for _, funcItem := range ipCheckFuncList {
|
||||
if funcItem(ip) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// 判断传入的Ip是否有效
|
||||
// ip:ip
|
||||
// 返回值:
|
||||
// 是否有效
|
||||
func IsIpValid(ip string) bool {
|
||||
return isLocalValid(ip) || isCheckFuncValid(ip)
|
||||
}
|
||||
44
trunk/framework/ipMgr/ipCheck_test.go
Normal file
44
trunk/framework/ipMgr/ipCheck_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package ipMgr
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsIpValid(t *testing.T) {
|
||||
ip := "10.255.0.7"
|
||||
if IsIpValid(ip) {
|
||||
t.Errorf("%s应该无效,但是现在却有效", ip)
|
||||
}
|
||||
|
||||
ipList := []string{"10.255.0.7", "10.1.0.21"}
|
||||
Init(ipList)
|
||||
|
||||
if IsIpValid(ip) == false {
|
||||
t.Errorf("%s应该有效,但是现在却无效", ip)
|
||||
}
|
||||
|
||||
ipStr := "10.255.0.7,10.1.0.21;10.255.0.6|10.1.0.30||"
|
||||
InitString(ipStr)
|
||||
if IsIpValid(ip) == false {
|
||||
t.Errorf("%s应该有效,但是现在却无效", ip)
|
||||
}
|
||||
|
||||
ip = "192.168.1.1"
|
||||
RegisterIpCheckFunc("", alwaysFalse)
|
||||
if IsIpValid(ip) {
|
||||
t.Errorf("%s应该无效,但是现在却有效", ip)
|
||||
}
|
||||
|
||||
RegisterIpCheckFunc("", alwaysTrue)
|
||||
if IsIpValid(ip) == false {
|
||||
t.Errorf("%s应该有效,但是现在却无效", ip)
|
||||
}
|
||||
}
|
||||
|
||||
func alwaysTrue(ip string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func alwaysFalse(ip string) bool {
|
||||
return false
|
||||
}
|
||||
93
trunk/framework/ipMgr/ipQuery.go
Normal file
93
trunk/framework/ipMgr/ipQuery.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package ipMgr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"goutil/securityUtil"
|
||||
"goutil/webUtil"
|
||||
)
|
||||
|
||||
var (
|
||||
IP_SERVICE_URL = "http://ipip.7qule.com/query"
|
||||
)
|
||||
|
||||
// 服务器的响应对象
|
||||
type QueryResponse struct {
|
||||
// 响应结果的状态值
|
||||
ResultStatus string
|
||||
|
||||
// 响应结果的数据
|
||||
Data *IPInfo
|
||||
}
|
||||
|
||||
// IP信息对象
|
||||
type IPInfo struct {
|
||||
// 所属大陆块
|
||||
Continent string
|
||||
|
||||
// 国家
|
||||
Country string
|
||||
|
||||
// 省份
|
||||
Region string
|
||||
|
||||
// 城市
|
||||
City string
|
||||
|
||||
// 网络服务提供商
|
||||
Isp string
|
||||
}
|
||||
|
||||
// 查询ip地址的详细信息
|
||||
// appId: 为应用分配的唯一标识
|
||||
// appSecret: 为应用分配的密钥
|
||||
// ip: 待查询的ip地址
|
||||
// isDomestic: 是否为国内的IP
|
||||
// timeout:超时时间(单位:秒)
|
||||
// 返回值:
|
||||
// ipInfoObj: IP地址信息对象
|
||||
// err: 错误对象
|
||||
func Query(appId, appSecret, ip string, isDomestic bool, timeout int) (ipInfoObj *IPInfo, err error) {
|
||||
timeStamp := fmt.Sprintf("%d", time.Now().Unix())
|
||||
rawString := fmt.Sprintf("AppId=%s&IP=%s&Timestamp=%s&AppSecret=%s", appId, ip, timeStamp, appSecret)
|
||||
sign := securityUtil.Md5String(rawString, true)
|
||||
|
||||
postData := make(map[string]string, 5)
|
||||
postData["AppId"] = appId
|
||||
postData["IP"] = ip
|
||||
postData["IsDomestic"] = fmt.Sprintf("%t", isDomestic)
|
||||
postData["Timestamp"] = timeStamp
|
||||
postData["Sign"] = sign
|
||||
|
||||
header := webUtil.GetFormHeader()
|
||||
transport := webUtil.NewTransport()
|
||||
transport.DisableKeepAlives = true
|
||||
transport = webUtil.GetTimeoutTransport(transport, timeout)
|
||||
|
||||
statusCode, result, err := webUtil.PostMapData(IP_SERVICE_URL, postData, header, transport)
|
||||
//statusCode, result, err := webUtil.PostMapData(IP_SERVICE_URL, postData, header, transport)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if statusCode != 200 {
|
||||
err = fmt.Errorf("StatusCode:%d is wrong.", statusCode)
|
||||
return
|
||||
}
|
||||
|
||||
var queryResponseObj *QueryResponse
|
||||
err = json.Unmarshal(result, &queryResponseObj)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if queryResponseObj.ResultStatus != "" {
|
||||
err = fmt.Errorf("Query result:%s", queryResponseObj.ResultStatus)
|
||||
return
|
||||
}
|
||||
|
||||
ipInfoObj = queryResponseObj.Data
|
||||
|
||||
return
|
||||
}
|
||||
98
trunk/framework/ipMgr/ipQuery_test.go
Normal file
98
trunk/framework/ipMgr/ipQuery_test.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package ipMgr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestQuery(t *testing.T) {
|
||||
IP_SERVICE_URL = "http://ipip.7qule.com/query"
|
||||
|
||||
appId := "unittest"
|
||||
appId_wrong := "wrong"
|
||||
appSecret := "c5746980-5d52-4ba9-834f-13d0066ad1d0"
|
||||
appSecret_wrong := "wrong"
|
||||
ip := "117.139.247.210"
|
||||
ip_wrong := "wrong"
|
||||
isDomestic := true
|
||||
continent := ""
|
||||
country := "中国"
|
||||
region := "四川"
|
||||
city := "成都"
|
||||
timeout := 3
|
||||
|
||||
// Test with wrong AppId
|
||||
ipInfoObj, err := Query(appId_wrong, appSecret, ip, isDomestic, timeout)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there is no error")
|
||||
return
|
||||
}
|
||||
|
||||
// Test with wrong AppSecret
|
||||
ipInfoObj, err = Query(appId, appSecret_wrong, ip, isDomestic, timeout)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there is no error")
|
||||
return
|
||||
}
|
||||
|
||||
// Test with wrong IP
|
||||
ipInfoObj, err = Query(appId, appSecret, ip_wrong, isDomestic, timeout)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there is no error")
|
||||
return
|
||||
}
|
||||
|
||||
// Test with correct information and domestic ip
|
||||
ipInfoObj, err = Query(appId, appSecret, ip, isDomestic, timeout)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is one:%s", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("ipInfoObj:%v\n", ipInfoObj)
|
||||
|
||||
if ipInfoObj.Continent != continent {
|
||||
t.Errorf("Expected continent %s, but got %s", continent, ipInfoObj.Continent)
|
||||
}
|
||||
|
||||
if ipInfoObj.Country != country {
|
||||
t.Errorf("Expected country %s, but got %s", country, ipInfoObj.Country)
|
||||
}
|
||||
|
||||
if ipInfoObj.Region != region {
|
||||
t.Errorf("Expected region %s, but got %s", region, ipInfoObj.Region)
|
||||
}
|
||||
|
||||
if ipInfoObj.City != city {
|
||||
t.Errorf("Expected city %s, but got %s", city, ipInfoObj.City)
|
||||
}
|
||||
|
||||
// Test with correct information and foreign ip
|
||||
isDomestic = false
|
||||
continent = "亚洲"
|
||||
country = "中国"
|
||||
region = "四川省"
|
||||
city = "成都"
|
||||
|
||||
ipInfoObj, err = Query(appId, appSecret, ip, isDomestic, timeout)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is one:%s", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("ipInfoObj:%v\n", ipInfoObj)
|
||||
|
||||
if ipInfoObj.Continent != continent {
|
||||
t.Errorf("Expected continent %s, but got %s", continent, ipInfoObj.Continent)
|
||||
}
|
||||
|
||||
if ipInfoObj.Country != country {
|
||||
t.Errorf("Expected country %s, but got %s", country, ipInfoObj.Country)
|
||||
}
|
||||
|
||||
if ipInfoObj.Region != region {
|
||||
t.Errorf("Expected region %s, but got %s", region, ipInfoObj.Region)
|
||||
}
|
||||
|
||||
if ipInfoObj.City != city {
|
||||
t.Errorf("Expected city %s, but got %s", city, ipInfoObj.City)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user