Skip to content

Commit

Permalink
Merge pull request #24 from xiaomakuaiz/fix-sample-count
Browse files Browse the repository at this point in the history
Fix sample count
  • Loading branch information
phxa1 authored Jun 7, 2024
2 parents 174a6e0 + dde33c9 commit 99841bd
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 90 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/release-docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: release blazehttp docker image

permissions:
contents: write

on:
push:
tags:
- 'v*'

env:
DOCKERHUB_REPO: chaitin/blazehttp

jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
-
name: Set up QEMU
uses: docker/setup-qemu-action@v3
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
-
name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Generate App Version
run: echo APP_VERSION=`git describe --tags --always` >> $GITHUB_ENV
-
name: Build and push
uses: docker/build-push-action@v5
with:
platforms: linux/amd64,linux/arm64
push: true
build-args: |
APP_VERSION=${{ env.APP_VERSION }}
tags: |
${{ env.DOCKERHUB_REPO }}:latest
${{ env.DOCKERHUB_REPO }}:${{ env.APP_VERSION }}
2 changes: 1 addition & 1 deletion .github/workflows/release-gui-macos.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: release blasehttp macos gui
name: release blazehttp macos gui

permissions:
contents: write
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-gui-windows.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: release blasehttp windows gui
name: release blazehttp windows gui

permissions:
contents: write
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: release blasehttp cli
name: release blazehttp cli

permissions:
contents: write
Expand Down
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM --platform=$BUILDPLATFORM golang:1.22 as builder

WORKDIR /src
ENV CGO_ENABLED=0

COPY go.* .

ARG TARGETOS TARGETARCH

RUN --mount=type=bind,target=. \
GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /build/blazehttp cmd/blazehttp/main.go


FROM --platform=$BUILDPLATFORM alpine:latest as binary

RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& apk del tzdata

WORKDIR /app

COPY --from=builder /build/blazehttp /app/blazehttp

CMD [ "/app/blazehttp" ]
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,22 @@ Connection: close

## 安装使用

**Docker 容器运行**

```bash
# 下载镜像
docker pull chaitin/blazehttp:latest
# 开始测试 http://127.0.0.1:9444 是 WAF 的地址 (根据实际情况修改)
docker run --rm --net=host chaitin/blazehttp:latest /app/blazehttp -t <http://127.0.0.1:9444>
```

GitHub CI 预编译的产物已上传 Release,可以[直接下载](https://github.com/chaitin/blazehttp/releases)最新的版本使用。

**命令行**
**命令行运行**

![blazehttp_cmd](https://github.com/chaitin/blazehttp/assets/30664688/7be052e9-2dfb-4f96-a6f2-eb2a0251910e)

**GUI** (MacOS & Windows)
**GUI 运行** (MacOS & Windows)

> 如果 MacOS 双击打开报错**不受信任**或者**移到垃圾箱**,执行下面命令后再启动即可:
> ``` bash
Expand Down
9 changes: 9 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ Connection: close

## Installation and Usage

** Docker Container**

```bash
# pull latest image from DockerHub
docker pull chaitin/blazehttp:latest
# run test
docker run --rm --net=host chaitin/blazehttp:latest /app/blazehttp -t <URL>
```

Precompiled artifacts from GitHub CI have been uploaded to Releases for direct downloads of the latest version [here](https://github.com/chaitin/blazehttp/releases).

**Command Line**
Expand Down
3 changes: 2 additions & 1 deletion gui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ func main() {
m := lo.CountValuesBy(rawTestCaseData, func(item []string) string {
if item[1] == "正常" {
return "正常"
} else {
} else if item[1] == "恶意" {
return "恶意"
}
return "unknown"
})

_ = r.Total.Set(fmt.Sprintf("总样本: %d", len(allTestData)))
Expand Down
158 changes: 74 additions & 84 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,106 +181,96 @@ func (w *Worker) Run() {

func (w *Worker) runWorker() {
for job := range w.jobs {
select {
case <-w.ctx.Done():
return
default:
func() {
defer func() {
w.jobResult <- job
}()
filePath := job.FilePath
req := new(blazehttp.Request)
if w.useEmbedFS {
if err := req.ReadFileFromFS(testcases.EmbedTestCasesFS, filePath); err != nil {
job.Result.Err = fmt.Sprintf("read request file: %s from embed fs error: %s\n", filePath, err)
return
}
} else {
if err := req.ReadFile(filePath); err != nil {
job.Result.Err = fmt.Sprintf("read request file: %s error: %s\n", filePath, err)
return
}
func() {
defer func() {
w.jobResult <- job
}()
filePath := job.FilePath
req := new(blazehttp.Request)
if w.useEmbedFS {
if err := req.ReadFileFromFS(testcases.EmbedTestCasesFS, filePath); err != nil {
job.Result.Err = fmt.Sprintf("read request file: %s from embed fs error: %s\n", filePath, err)
return
}

if w.reqHost != "" {
req.SetHost(w.reqHost)
} else {
req.SetHost(w.addr)
} else {
if err := req.ReadFile(filePath); err != nil {
job.Result.Err = fmt.Sprintf("read request file: %s error: %s\n", filePath, err)
return
}
}

if w.reqPerSession {
// one http request one connection
req.SetHeader("Connection", "close")
}
if w.reqHost != "" {
req.SetHost(w.reqHost)
} else {
req.SetHost(w.addr)
}

req.CalculateContentLength()
if w.reqPerSession {
// one http request one connection
req.SetHeader("Connection", "close")
}

start := time.Now()
conn := blazehttp.Connect(w.addr, w.isHttps, w.timeout)
if conn == nil {
job.Result.Err = fmt.Sprintf("connect to %s failed!\n", w.addr)
return
}
nWrite, err := req.WriteTo(*conn)
if err != nil {
job.Result.Err = fmt.Sprintf("send request poc: %s length: %d error: %s", filePath, nWrite, err)
return
}
req.CalculateContentLength()

rsp := new(blazehttp.Response)
if err = rsp.ReadConn(*conn); err != nil {
job.Result.Err = fmt.Sprintf("read poc file: %s response, error: %s", filePath, err)
return
}
elap := time.Since(start).Nanoseconds()
(*conn).Close()
job.Result.Success = true
if strings.HasSuffix(job.FilePath, "white") {
job.Result.IsWhite = true // white case
}
start := time.Now()
conn := blazehttp.Connect(w.addr, w.isHttps, w.timeout)
if conn == nil {
job.Result.Err = fmt.Sprintf("connect to %s failed!\n", w.addr)
return
}
nWrite, err := req.WriteTo(*conn)
if err != nil {
job.Result.Err = fmt.Sprintf("send request poc: %s length: %d error: %s", filePath, nWrite, err)
return
}

code := rsp.GetStatusCode()
job.Result.StatusCode = code
if code != w.blockStatusCode {
job.Result.IsPass = true
}
job.Result.TimeCost = elap
}()
}
rsp := new(blazehttp.Response)
if err = rsp.ReadConn(*conn); err != nil {
job.Result.Err = fmt.Sprintf("read poc file: %s response, error: %s", filePath, err)
return
}
elap := time.Since(start).Nanoseconds()
(*conn).Close()
job.Result.Success = true
if strings.HasSuffix(job.FilePath, "white") {
job.Result.IsWhite = true // white case
}

code := rsp.GetStatusCode()
job.Result.StatusCode = code
if code != w.blockStatusCode {
job.Result.IsPass = true
}
job.Result.TimeCost = elap
}()
}
}

func (w *Worker) processJobResult() {
for job := range w.jobResult {
select {
case <-w.ctx.Done():
return
default:
if job.Result.Success {
w.result.Success++
w.result.SuccessTimeCost += job.Result.TimeCost
if job.Result.IsWhite {
if job.Result.IsPass {
w.result.TN++
} else {
w.result.FP++
}
if job.Result.Success {
w.result.Success++
w.result.SuccessTimeCost += job.Result.TimeCost
if job.Result.IsWhite {
if job.Result.IsPass {
w.result.TN++
} else {
if job.Result.IsPass {
w.result.FN++
} else {
w.result.TP++
}
w.result.FP++
}
} else {
w.result.Error++
}
if w.resultCh != nil {
r := *w.result
r.Job = job
w.resultCh <- &r
if job.Result.IsPass {
w.result.FN++
} else {
w.result.TP++
}
}
} else {
w.result.Error++
}
if w.resultCh != nil {
r := *w.result
r.Job = job
w.resultCh <- &r
}
}
}
Expand Down

0 comments on commit 99841bd

Please sign in to comment.