Commit a82440a7 by 盛威豪

add

parent 57d0fdcf
package utils
import (
"encoding/json"
"fmt"
"github.com/zjswh/go-tool/utils"
"net/url"
)
const CronHost = "http://cron.guangdianyun.tv"
type Cron struct {
Id int `json:"id"`
Name string `json:"name"`
Level int `json:"level"`
DependencyStatus int `json:"dependency_status"`
Spec string `json:"spec"`
Protocol int `json:"protocol"`
HttpMethod string `json:"http_method"`
Command string `json:"command"`
Timeout int `json:"timeout"`
Multi int `json:"multi"` // 是否单例 2是 1否
NotifyStatus int `json:"notify_status"`
NotifyType int `json:"notify_type"`
RetryTimes int `json:"retry_times"`
RetryInterval int `json:"retry_interval"`
}
type CronResult struct {
Code int `json:"code"`
Message string `json:"message"`
Data int `json:"data"`
}
func CreateCron(name string, date string, callback string,taskId int) (CronResult, error) {
callback = url.QueryEscape(callback)
cronUrl := CronHost + "/api/v1/store"
param := map[string]interface{} {
"name" : name,
"spec" : date,
"command" : callback,
"id" : taskId,
"level" : 1,
"dependency_status" : 1,
"protocol" : 1,
"timeout" : 10,
"multi" : 2,
"notify_status" : 1,
"notify_type" : 2,
"retry_times" : 0,
"retry_interval" : 1,
"http_method" : 1,
}
body, err := utils.Request(cronUrl, param, map[string]interface{}{
"Content-Type" : "multipart/form-data",
}, "POST", "form")
var cronResult CronResult
json.Unmarshal(body, &cronResult)
return cronResult, err
}
func DeleteCron(sTaskId int) (CronResult, error){
cronUrl := fmt.Sprintf("%s/%d", CronHost + "/api/v1/remove", sTaskId)
body, err := utils.Request(cronUrl, map[string]interface{}{}, map[string]interface{}{}, "GET", "form")
var cronResult CronResult
json.Unmarshal(body, &cronResult)
return cronResult, err
}
package ipSearch
import (
"io/ioutil"
"log"
"strconv"
"strings"
)
/**
* @author xiao.luo
* @description This is the go version for IpSearch
*/
type ipIndex struct {
startip, endip uint32
local_offset, local_length uint32
}
type prefixIndex struct {
start_index, end_index uint32
}
type ipSearch struct {
data []byte
prefixMap map[uint32]prefixIndex
firstStartIpOffset uint32
prefixStartOffset uint32
prefixEndOffset uint32
prefixCount uint32
}
var ips *ipSearch = nil
func New() (ipSearch, error) {
if ips == nil {
var err error
ips, err = loadIpDat()
if err != nil {
log.Fatal("the IP Dat loaded failed!")
return *ips, err
}
}
return *ips, nil
}
func loadIpDat() (*ipSearch, error) {
p := ipSearch{}
//加载ip地址库信息
data, err := ioutil.ReadFile("./utils/ipSearch/qqzeng-ip-utf8.dat")
if err != nil {
log.Fatal(err)
}
p.data = data
p.prefixMap = make(map[uint32]prefixIndex)
p.firstStartIpOffset = bytesToLong(data[0], data[1], data[2], data[3])
p.prefixStartOffset = bytesToLong(data[8], data[9], data[10], data[11])
p.prefixEndOffset = bytesToLong(data[12], data[13], data[14], data[15])
p.prefixCount = (p.prefixEndOffset-p.prefixStartOffset)/9 + 1 // 前缀区块每组
// 初始化前缀对应索引区区间
indexBuffer := p.data[p.prefixStartOffset:(p.prefixEndOffset + 9)]
for k := uint32(0); k < p.prefixCount; k++ {
i := k * 9
prefix := uint32(indexBuffer[i] & 0xFF)
pf := prefixIndex{}
pf.start_index = bytesToLong(indexBuffer[i+1], indexBuffer[i+2], indexBuffer[i+3], indexBuffer[i+4])
pf.end_index = bytesToLong(indexBuffer[i+5], indexBuffer[i+6], indexBuffer[i+7], indexBuffer[i+8])
p.prefixMap[prefix] = pf
}
return &p, nil
}
func (p ipSearch) Get(ip string) string {
ips := strings.Split(ip, ".")
x, _ := strconv.Atoi(ips[0])
prefix := uint32(x)
intIP := ipToLong(ip)
var high uint32 = 0
var low uint32 = 0
if _, ok := p.prefixMap[prefix]; ok {
low = p.prefixMap[prefix].start_index
high = p.prefixMap[prefix].end_index
} else {
return ""
}
var my_index uint32
if low == high {
my_index = low
} else {
my_index = p.binarySearch(low, high, intIP)
}
ipindex := ipIndex{}
ipindex.getIndex(my_index, &p)
if ipindex.startip <= intIP && ipindex.endip >= intIP {
return ipindex.getLocal(&p)
} else {
return ""
}
}
// 二分逼近算法
func (p ipSearch) binarySearch(low uint32, high uint32, k uint32) uint32 {
var M uint32 = 0
for low <= high {
mid := (low + high) / 2
endipNum := p.getEndIp(mid)
if endipNum >= k {
M = mid
if mid == 0 {
break // 防止溢出
}
high = mid - 1
} else {
low = mid + 1
}
}
return M
}
// 只获取结束ip的数值
// 索引区第left个索引
// 返回结束ip的数值
func (p ipSearch) getEndIp(left uint32) uint32 {
left_offset := p.firstStartIpOffset + left*12
return bytesToLong(p.data[4+left_offset], p.data[5+left_offset], p.data[6+left_offset], p.data[7+left_offset])
}
func (p *ipIndex) getIndex(left uint32, ips *ipSearch) {
left_offset := ips.firstStartIpOffset + left*12
p.startip = bytesToLong(ips.data[left_offset], ips.data[1+left_offset], ips.data[2+left_offset], ips.data[3+left_offset])
p.endip = bytesToLong(ips.data[4+left_offset], ips.data[5+left_offset], ips.data[6+left_offset], ips.data[7+left_offset])
p.local_offset = bytesToLong3(ips.data[8+left_offset], ips.data[9+left_offset], ips.data[10+left_offset])
p.local_length = uint32(ips.data[11+left_offset])
}
// / 返回地址信息
// / 地址信息的流位置
// / 地址信息的流长度
func (p *ipIndex) getLocal(ips *ipSearch) string {
bytes := ips.data[p.local_offset : p.local_offset+p.local_length]
return string(bytes)
}
func ipToLong(ip string) uint32 {
quads := strings.Split(ip, ".")
var result uint32 = 0
a, _ := strconv.Atoi(quads[3])
result += uint32(a)
b, _ := strconv.Atoi(quads[2])
result += uint32(b) << 8
c, _ := strconv.Atoi(quads[1])
result += uint32(c) << 16
d, _ := strconv.Atoi(quads[0])
result += uint32(d) << 24
return result
}
//字节转整形
func bytesToLong(a, b, c, d byte) uint32 {
a1 := uint32(a)
b1 := uint32(b)
c1 := uint32(c)
d1 := uint32(d)
return (a1 & 0xFF) | ((b1 << 8) & 0xFF00) | ((c1 << 16) & 0xFF0000) | ((d1 << 24) & 0xFF000000)
}
func bytesToLong3(a, b, c byte) uint32 {
a1 := uint32(a)
b1 := uint32(b)
c1 := uint32(c)
return (a1 & 0xFF) | ((b1 << 8) & 0xFF00) | ((c1 << 16) & 0xFF0000)
}
\ No newline at end of file
package timex
import "time"
func TimeStampToDate(date int64, sType string) string {
tm := time.Unix(date, 0)
layout := "2006-01-02"
switch sType {
case "h":
layout = "2006-01-02 15"
case "m":
layout = "2006-01-02 15:04"
case "ymd":
layout = "20060102"
case "y-m-dTH:i:sZ":
layout = "2006-01-02T15:04:05Z"
default:
layout = "2006-01-02 15:04:05"
}
return tm.Format(layout)
}
func DateToTimeStamp(date string, sType string) int64 {
loc, _ := time.LoadLocation("Asia/Shanghai")
layout := "2006-01-02"
switch sType {
case "h":
layout = "2006-01-02 15"
case "m":
layout = "2006-01-02 15:04"
case "y/m/d":
layout = "2006/01/02 15:04:05"
default:
layout = "2006-01-02 15:04:05"
}
tt, _ := time.ParseInLocation(layout, date, loc)
return tt.Unix()
}
func DateToTime(date string, sType string) time.Time {
loc, _ := time.LoadLocation("Asia/Shanghai")
layout := "2006-01-02"
switch sType {
case "h":
layout = "2006-01-02 15"
case "m":
layout = "2006-01-02 15:04"
case "ymd":
layout = "20060102"
case "y/m/d":
layout = "2006/01/02 15:04:05"
default:
layout = "2006-01-02 15:04:05"
}
tt, _ := time.ParseInLocation(layout, date, loc)
return tt
}
func GetCurrentTimeStamp() int64 {
return time.Now().Unix()
}
func DateStringToTimeStamp(date string) int64 {
loc, _ := time.LoadLocation("Asia/Shanghai")
tt, _ := time.ParseInLocation("2006-01-02T15:04:05+08:00", date, loc)
return tt.Unix()
}
package utils
import (
"crypto/md5"
"crypto/sha1"
"encoding/hex"
"fmt"
)
func Sha1(str string) string {
//产生一个散列值得方式是 sha1.New(),sha1.Write(bytes),然后 sha1.Sum([]byte{})。这里我们从一个新的散列开始。
h := sha1.New() // md5加密类似md5.New()
//写入要处理的字节。如果是一个字符串,需要使用[]byte(s) 来强制转换成字节数组。
h.Write([]byte(str))
//这个用来得到最终的散列值的字符切片。Sum 的参数可以用来对现有的字符切片追加额外的字节切片:一般不需要要。
bs := h.Sum(nil)
//SHA1 值经常以 16 进制输出,使用%x 来将散列结果格式化为 16 进制字符串。
return fmt.Sprintf("%x\n", bs)
}
func MD5(str []byte) string {
h := md5.New()
h.Write(str)
return hex.EncodeToString(h.Sum(nil))
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment