您好,登錄后才能下訂單哦!
run.go:
package main
import (
"./uas"
"flag"
"math/rand"
"net/url"
"os"
"strconv"
"strings"
"time"
)
type resource struct {
url string
target string
start int
end int
}
func ruleResource() []resource{
var res []resource
//首頁
r1 := resource{
url: "http://localhost/",
target: "",
start: 0,
end: 0,
}
//列表頁
r2 := resource{
url: "http://localhost/list/{$id}.html",
target: "{$id}",
start: 1,
end: 21,
}
//詳情頁
r3 := resource{
url: "http://localhost/movie/{$id}.html",
target: "{$id}",
start: 1,
end: 12924,
}
res = append(res, r1, r2, r3)
return res
}
func buildUrl(res []resource) []string {
var list []string
for _, resItem := range res {
if len(resItem.target) == 0 {
list = append(list, resItem.url)
}else {
for i := resItem.start; i <= resItem.end; i++ {
urlStr := strings.Replace(resItem.url, resItem.target, strconv.Itoa(i), -1)
list = append(list, urlStr)
}
}
}
return list
}
func makeLog(current, refer, ua string) string {
u := url.Values{}
u.Set("time", "1")
u.Set("url", current)
u.Set("refer", refer)
u.Set("ua", ua)
paramsStr := u.Encode()
logTemplate := "127.0.0.1 - - [14/Apr/2019:20:07:42 +0800] \"OPTIONS /dig?{$paramsStr} HTTP/1.1\" 499 0 \"-\" \"{$ua}\" \"-\""
log := strings.Replace(logTemplate, "{$paramsStr}", paramsStr, -1)
log = strings.Replace(log, "{$ua}", ua, -1)
return log
}
//隨機
func randInt(min, max int) int {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
if min > max {
return max
}
return r.Intn(max-min) + min
}
func main() {
total := flag.Int("total", 100, "創建多少行日志")
filePath := flag.String("filePath", "F:/phpStudy/PHPTutorial/nginx/logs/access.log", "日志文件路徑")
//想生效還要調用解析方法
flag.Parse()
//fmt.Println(*total, *filePath)
//構造出真實的網站url集合
res := ruleResource()
list := buildUrl(res)
//按照要求,生成$total行日志內容
logStr := ""
for i := 1; i <= *total; i++ {
//隨機選
currentUrl := list[randInt(0, len(list)-1)]
referUrl := list[randInt(0, len(list)-1)]
ua := uas.UaList[randInt(0, len(uas.UaList)-1)]
logStr = logStr + makeLog(currentUrl, referUrl, ua) + "\n"
//和隨機種子一樣的間隔,盡可能保證隨機性
time.Sleep(time.Nanosecond)
}
//放到循環外一次寫入
fd, _ := os.OpenFile(*filePath, os.O_RDWR|os.O_APPEND, 0644 )
fd.Write([]byte(logStr))
fd.Close()
}
uas.go(各種瀏覽器信息):
package uas
var UaList = []string {
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;",
" Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv,2.0.1) Gecko/20100101 Firefox/4.0.1",
"Mozilla/5.0 (Windows NT 6.1; rv,2.0.1) Gecko/20100101 Firefox/4.0.1",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4094.1 Safari/537.36",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
" Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",
}
運行程序(隨機生成10000條日志):
go run run.go --total=10000 --filePath="F:/phpStudy/PHPTutorial/nginx/logs/access.log"
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。