Compare commits
110 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
004e640321 | ||
|
|
7ad315ae4b | ||
|
|
ba938e5361 | ||
|
|
9ddf02a0e6 | ||
|
|
ebcbd5690d | ||
|
|
bbca766fa4 | ||
|
|
99c7819d3a | ||
|
|
08bb3e66f8 | ||
|
|
9159820742 | ||
|
|
1a565b2ebb | ||
|
|
98847c53ea | ||
|
|
14bafc8f20 | ||
|
|
58a5bd0092 | ||
|
|
4f1ce52f6a | ||
|
|
14ba7f6899 | ||
|
|
e582e37c06 | ||
|
|
6a3fa9f0ca | ||
|
|
e0a9965fed | ||
|
|
481fa8cd2d | ||
|
|
95349dc457 | ||
|
|
fc839f96d2 | ||
|
|
0414cc02e8 | ||
|
|
b8babaae39 | ||
|
|
285ce1b312 | ||
|
|
c309da81ae | ||
|
|
c325fde52b | ||
|
|
0f69b45d25 | ||
|
|
e02084ba5d | ||
|
|
642b23dbb7 | ||
|
|
b1dc385c87 | ||
|
|
89a69e3165 | ||
|
|
530954dd6c | ||
|
|
33635f7a1b | ||
|
|
8ac964e805 | ||
|
|
a1519baf0f | ||
|
|
e6e32a39bb | ||
|
|
d828b7f8a4 | ||
|
|
07b377c2fb | ||
|
|
33a3795773 | ||
|
|
bddb3cae96 | ||
|
|
6a525c2b82 | ||
|
|
3a137c1c3f | ||
|
|
19867568a9 | ||
|
|
c2600b911b | ||
|
|
fe511ae7e5 | ||
|
|
876c631c85 | ||
|
|
a4cc138ef3 | ||
|
|
3f9a7a8a49 | ||
|
|
09d8ef00c2 | ||
|
|
b4f77ddc63 | ||
|
|
6a9045ba44 | ||
|
|
bc7ee19962 | ||
|
|
2484cd4ae6 | ||
|
|
57b8a96c6d | ||
|
|
6c8ab7cb94 | ||
|
|
b2c2bd247b | ||
|
|
29d9e6db81 | ||
|
|
24fcb48c75 | ||
|
|
4911198fe7 | ||
|
|
c2af796992 | ||
|
|
51c8572e53 | ||
|
|
c15cc6d75c | ||
|
|
c0a99a4ba3 | ||
|
|
62e26bed5a | ||
|
|
9e68a2915c | ||
|
|
5b809fda1f | ||
|
|
88b782940a | ||
|
|
85c596644d | ||
|
|
ffa2d884bd | ||
|
|
813ed18610 | ||
|
|
564b11eae7 | ||
|
|
d2af0307b9 | ||
|
|
4fa6b03238 | ||
|
|
4bda78aa8c | ||
|
|
6002cc96d9 | ||
|
|
56c09f5be7 | ||
|
|
e1fb8e4c74 | ||
|
|
14f055ce7c | ||
|
|
7b7f2b0a00 | ||
|
|
75662586fa | ||
|
|
c024331fa0 | ||
|
|
b01ea79c5c | ||
|
|
d59f80b3ec | ||
|
|
b87e48c1f9 | ||
|
|
0e7a7f168f | ||
|
|
2bbc09d3af | ||
|
|
97b50fab28 | ||
|
|
83c867cb65 | ||
|
|
3c4b7ca57b | ||
|
|
a2f98d2f25 | ||
|
|
71677a8638 | ||
|
|
95a2187f95 | ||
|
|
86bec813bf | ||
|
|
0d7bd6f04e | ||
|
|
d7117209b2 | ||
|
|
7a340ac68b | ||
|
|
353b1b4ad1 | ||
|
|
fdac8beb40 | ||
|
|
f098723a41 | ||
|
|
6ded627903 | ||
|
|
ed2d5ee5cc | ||
|
|
c677368482 | ||
|
|
51ccd614a7 | ||
|
|
9be7f61b8c | ||
|
|
14ad97c937 | ||
|
|
94609d8ef4 | ||
|
|
e34e38bcb2 | ||
|
|
b10d9fe842 | ||
|
|
992e725378 | ||
|
|
346de7ca7a |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
*_plus.go
|
*_plus.go
|
||||||
*-plus.sh
|
*-plus.sh
|
||||||
|
*_plus_test.go
|
||||||
29
LICENSE
Normal file
29
LICENSE
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
BSD 3-Clause License
|
||||||
|
|
||||||
|
Copyright (c) 2020, LiuXiangChao
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
3. Neither the name of the copyright holder nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
@@ -1,76 +1,76 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
function build() {
|
function build() {
|
||||||
ROOT=$(dirname $0)
|
ROOT=$(dirname "$0")
|
||||||
NAME="edge-api"
|
NAME="edge-api"
|
||||||
DIST=$ROOT/"../dist/${NAME}"
|
DIST=$ROOT/"../dist/${NAME}"
|
||||||
OS=${1}
|
OS=${1}
|
||||||
ARCH=${2}
|
ARCH=${2}
|
||||||
TAG=${3}
|
TAG=${3}
|
||||||
NODE_ARCHITECTS=("amd64" "386" "arm64" "mips64" "mips64le")
|
NODE_ARCHITECTS=("amd64" "arm64")
|
||||||
|
|
||||||
if [ -z $OS ]; then
|
if [ -z "$OS" ]; then
|
||||||
echo "usage: build.sh OS ARCH"
|
echo "usage: build.sh OS ARCH"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
if [ -z $ARCH ]; then
|
if [ -z "$ARCH" ]; then
|
||||||
echo "usage: build.sh OS ARCH"
|
echo "usage: build.sh OS ARCH"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
if [ -z $TAG ]; then
|
if [ -z "$TAG" ]; then
|
||||||
TAG="community"
|
TAG="community"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
VERSION=$(lookup-version $ROOT/../internal/const/const.go)
|
VERSION=$(lookup-version "$ROOT"/../internal/const/const.go)
|
||||||
ZIP="${NAME}-${OS}-${ARCH}-${TAG}-v${VERSION}.zip"
|
ZIP="${NAME}-${OS}-${ARCH}-${TAG}-v${VERSION}.zip"
|
||||||
|
|
||||||
# build edge-node
|
# build edge-node
|
||||||
NodeVersion=$(lookup-version $ROOT"/../../EdgeNode/internal/const/const.go")
|
NodeVersion=$(lookup-version "$ROOT""/../../EdgeNode/internal/const/const.go")
|
||||||
echo "building edge-node v${NodeVersion} ..."
|
echo "building edge-node v${NodeVersion} ..."
|
||||||
EDGE_NODE_BUILD_SCRIPT=$ROOT"/../../EdgeNode/build/build.sh"
|
EDGE_NODE_BUILD_SCRIPT=$ROOT"/../../EdgeNode/build/build.sh"
|
||||||
if [ ! -f $EDGE_NODE_BUILD_SCRIPT ]; then
|
if [ ! -f "$EDGE_NODE_BUILD_SCRIPT" ]; then
|
||||||
echo "unable to find edge-node build script 'EdgeNode/build/build.sh'"
|
echo "unable to find edge-node build script 'EdgeNode/build/build.sh'"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
cd $ROOT"/../../EdgeNode/build"
|
cd "$ROOT""/../../EdgeNode/build" || exit
|
||||||
echo "=============================="
|
echo "=============================="
|
||||||
for arch in "${NODE_ARCHITECTS[@]}"; do
|
for arch in "${NODE_ARCHITECTS[@]}"; do
|
||||||
if [ ! -f $ROOT"/../../EdgeNode/dist/edge-node-linux-${arch}-${TAG}-v${NodeVersion}.zip" ]; then
|
if [ ! -f "$ROOT""/../../EdgeNode/dist/edge-node-linux-${arch}-${TAG}-v${NodeVersion}.zip" ]; then
|
||||||
./build.sh linux $arch $TAG
|
./build.sh linux "$arch" $TAG
|
||||||
else
|
else
|
||||||
echo "use built node linux/$arch/v${NodeVersion}"
|
echo "use built node linux/$arch/v${NodeVersion}"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
echo "=============================="
|
echo "=============================="
|
||||||
cd -
|
cd - || exit
|
||||||
|
|
||||||
rm -f $ROOT/deploy/*.zip
|
rm -f "$ROOT"/deploy/*.zip
|
||||||
for arch in "${NODE_ARCHITECTS[@]}"; do
|
for arch in "${NODE_ARCHITECTS[@]}"; do
|
||||||
cp $ROOT"/../../EdgeNode/dist/edge-node-linux-${arch}-${TAG}-v${NodeVersion}.zip" $ROOT/deploy/edge-node-linux-${arch}-v${NodeVersion}.zip
|
cp "$ROOT""/../../EdgeNode/dist/edge-node-linux-${arch}-${TAG}-v${NodeVersion}.zip" "$ROOT"/deploy/edge-node-linux-"${arch}"-v"${NodeVersion}".zip
|
||||||
done
|
done
|
||||||
|
|
||||||
# build edge-dns
|
# build edge-dns
|
||||||
if [ "$TAG" = "plus" ]; then
|
if [ "$TAG" = "plus" ]; then
|
||||||
DNS_ROOT=$ROOT"/../../EdgeDNS"
|
DNS_ROOT=$ROOT"/../../EdgeDNS"
|
||||||
if [ -d $DNS_ROOT ]; then
|
if [ -d "$DNS_ROOT" ]; then
|
||||||
DNSNodeVersion=$(lookup-version $ROOT"/../../EdgeDNS/internal/const/const.go")
|
DNSNodeVersion=$(lookup-version "$ROOT""/../../EdgeDNS/internal/const/const.go")
|
||||||
echo "building edge-dns ${DNSNodeVersion} ..."
|
echo "building edge-dns ${DNSNodeVersion} ..."
|
||||||
EDGE_DNS_NODE_BUILD_SCRIPT=$ROOT"/../../EdgeDNS/build/build.sh"
|
EDGE_DNS_NODE_BUILD_SCRIPT=$ROOT"/../../EdgeDNS/build/build.sh"
|
||||||
if [ ! -f $EDGE_DNS_NODE_BUILD_SCRIPT ]; then
|
if [ ! -f "$EDGE_DNS_NODE_BUILD_SCRIPT" ]; then
|
||||||
echo "unable to find edge-dns build script 'EdgeDNS/build/build.sh'"
|
echo "unable to find edge-dns build script 'EdgeDNS/build/build.sh'"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
cd $ROOT"/../../EdgeDNS/build"
|
cd "$ROOT""/../../EdgeDNS/build" || exit
|
||||||
echo "=============================="
|
echo "=============================="
|
||||||
architects=("amd64")
|
architects=("amd64" "arm64")
|
||||||
for arch in "${architects[@]}"; do
|
for arch in "${architects[@]}"; do
|
||||||
./build.sh linux $arch $TAG
|
./build.sh linux "$arch" $TAG
|
||||||
done
|
done
|
||||||
echo "=============================="
|
echo "=============================="
|
||||||
cd -
|
cd - || exit
|
||||||
|
|
||||||
for arch in "${architects[@]}"; do
|
for arch in "${architects[@]}"; do
|
||||||
cp $ROOT"/../../EdgeDNS/dist/edge-dns-linux-${arch}-v${DNSNodeVersion}.zip" $ROOT/deploy/edge-dns-linux-${arch}-v${DNSNodeVersion}.zip
|
cp "$ROOT""/../../EdgeDNS/dist/edge-dns-linux-${arch}-v${DNSNodeVersion}.zip" "$ROOT"/deploy/edge-dns-linux-"${arch}"-v"${DNSNodeVersion}".zip
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@@ -78,48 +78,49 @@ function build() {
|
|||||||
# build sql
|
# build sql
|
||||||
if [ $TAG = "plus" ]; then
|
if [ $TAG = "plus" ]; then
|
||||||
echo "building sql ..."
|
echo "building sql ..."
|
||||||
${ROOT}/sql.sh
|
"${ROOT}"/sql.sh
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# copy files
|
# copy files
|
||||||
echo "copying ..."
|
echo "copying ..."
|
||||||
if [ ! -d $DIST ]; then
|
if [ ! -d "$DIST" ]; then
|
||||||
mkdir $DIST
|
mkdir "$DIST"
|
||||||
mkdir $DIST/bin
|
mkdir "$DIST"/bin
|
||||||
mkdir $DIST/configs
|
mkdir "$DIST"/configs
|
||||||
mkdir $DIST/logs
|
mkdir "$DIST"/logs
|
||||||
|
mkdir "$DIST"/data
|
||||||
fi
|
fi
|
||||||
cp $ROOT/configs/api.template.yaml $DIST/configs/
|
cp "$ROOT"/configs/api.template.yaml "$DIST"/configs/
|
||||||
cp $ROOT/configs/db.template.yaml $DIST/configs/
|
cp "$ROOT"/configs/db.template.yaml "$DIST"/configs/
|
||||||
cp -R $ROOT/deploy $DIST/
|
cp -R "$ROOT"/deploy "$DIST/"
|
||||||
rm -f $dist/deploy/.gitignore
|
rm -f "$DIST"/deploy/.gitignore
|
||||||
cp -R $ROOT/installers $DIST/
|
cp -R "$ROOT"/installers "$DIST"/
|
||||||
cp -R $ROOT/resources $DIST/
|
cp -R "$ROOT"/resources "$DIST"/
|
||||||
rm -f $DIST/resources/ipdata/ip2region/global_region.csv
|
rm -f "$DIST"/resources/ipdata/ip2region/global_region.csv
|
||||||
rm -f $DIST/resources/ipdata/ip2region/ip.merge.txt
|
rm -f "$DIST"/resources/ipdata/ip2region/ip.merge.txt
|
||||||
|
|
||||||
# building edge installer
|
# building edge installer
|
||||||
echo "building node installer ..."
|
echo "building node installer ..."
|
||||||
architects=("amd64" "386" "arm64")
|
architects=("amd64" "arm64")
|
||||||
for arch in "${architects[@]}"; do
|
for arch in "${architects[@]}"; do
|
||||||
# TODO support arm, mips ...
|
# TODO support arm, mips ...
|
||||||
env GOOS=linux GOARCH=${arch} go build -tags $TAG --ldflags="-s -w" -o $ROOT/installers/edge-installer-helper-linux-${arch} $ROOT/../cmd/installer-helper/main.go
|
env GOOS=linux GOARCH="${arch}" go build -trimpath -tags $TAG --ldflags="-s -w" -o "$ROOT"/installers/edge-installer-helper-linux-"${arch}" "$ROOT"/../cmd/installer-helper/main.go
|
||||||
done
|
done
|
||||||
|
|
||||||
# building edge dns installer
|
# building edge dns installer
|
||||||
echo "building dns node installer ..."
|
echo "building dns node installer ..."
|
||||||
architects=("amd64" "386" "arm64")
|
architects=("amd64" "arm64")
|
||||||
for arch in "${architects[@]}"; do
|
for arch in "${architects[@]}"; do
|
||||||
# TODO support arm, mips ...
|
# TODO support arm, mips ...
|
||||||
env GOOS=linux GOARCH=${arch} go build -tags $TAG --ldflags="-s -w" -o $ROOT/installers/edge-installer-dns-helper-linux-${arch} $ROOT/../cmd/installer-dns-helper/main.go
|
env GOOS=linux GOARCH="${arch}" go build -trimpath -tags $TAG --ldflags="-s -w" -o "$ROOT"/installers/edge-installer-dns-helper-linux-"${arch}" "$ROOT"/../cmd/installer-dns-helper/main.go
|
||||||
done
|
done
|
||||||
|
|
||||||
# building api node
|
# building api node
|
||||||
env GOOS=$OS GOARCH=$ARCH go build -tags $TAG --ldflags="-s -w" -o $DIST/bin/edge-api $ROOT/../cmd/edge-api/main.go
|
env GOOS="$OS" GOARCH="$ARCH" go build -trimpath -tags $TAG --ldflags="-s -w" -o "$DIST"/bin/edge-api "$ROOT"/../cmd/edge-api/main.go
|
||||||
|
|
||||||
# delete hidden files
|
# delete hidden files
|
||||||
find $DIST -name ".DS_Store" -delete
|
find "$DIST" -name ".DS_Store" -delete
|
||||||
find $DIST -name ".gitignore" -delete
|
find "$DIST" -name ".gitignore" -delete
|
||||||
|
|
||||||
echo "zip files"
|
echo "zip files"
|
||||||
cd "${DIST}/../" || exit
|
cd "${DIST}/../" || exit
|
||||||
@@ -135,15 +136,15 @@ function build() {
|
|||||||
|
|
||||||
function lookup-version() {
|
function lookup-version() {
|
||||||
FILE=$1
|
FILE=$1
|
||||||
VERSION_DATA=$(cat $FILE)
|
VERSION_DATA=$(cat "$FILE")
|
||||||
re="Version[ ]+=[ ]+\"([0-9.]+)\""
|
re="Version[ ]+=[ ]+\"([0-9.]+)\""
|
||||||
if [[ $VERSION_DATA =~ $re ]]; then
|
if [[ $VERSION_DATA =~ $re ]]; then
|
||||||
VERSION=${BASH_REMATCH[1]}
|
VERSION=${BASH_REMATCH[1]}
|
||||||
echo $VERSION
|
echo "$VERSION"
|
||||||
else
|
else
|
||||||
echo "could not match version"
|
echo "could not match version"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
build $1 $2 $3
|
build "$1" "$2" "$3"
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/apps"
|
"github.com/TeaOSLab/EdgeAPI/internal/apps"
|
||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
@@ -13,6 +14,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"github.com/iwind/gosock/pkg/gosock"
|
"github.com/iwind/gosock/pkg/gosock"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@@ -21,12 +23,12 @@ func main() {
|
|||||||
if !Tea.IsTesting() {
|
if !Tea.IsTesting() {
|
||||||
Tea.Env = "prod"
|
Tea.Env = "prod"
|
||||||
}
|
}
|
||||||
app := apps.NewAppCmd()
|
var app = apps.NewAppCmd()
|
||||||
app.Version(teaconst.Version)
|
app.Version(teaconst.Version)
|
||||||
app.Product(teaconst.ProductName)
|
app.Product(teaconst.ProductName)
|
||||||
app.Usage(teaconst.ProcessName + " [start|stop|restart|setup|upgrade|service|daemon]")
|
app.Usage(teaconst.ProcessName + " [start|stop|restart|setup|upgrade|service|daemon|issues]")
|
||||||
app.On("setup", func() {
|
app.On("setup", func() {
|
||||||
setupCmd := setup.NewSetupFromCmd()
|
var setupCmd = setup.NewSetupFromCmd()
|
||||||
err := setupCmd.Run()
|
err := setupCmd.Run()
|
||||||
result := maps.Map{}
|
result := maps.Map{}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -122,6 +124,57 @@ func main() {
|
|||||||
fmt.Println("prepared statements count: " + types.String(count))
|
fmt.Println("prepared statements count: " + types.String(count))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
app.On("issues", func() {
|
||||||
|
var flagSet = flag.NewFlagSet("issues", flag.ExitOnError)
|
||||||
|
var formatJSON = false
|
||||||
|
flagSet.BoolVar(&formatJSON, "json", false, "")
|
||||||
|
_ = flagSet.Parse(os.Args[2:])
|
||||||
|
|
||||||
|
data, err := ioutil.ReadFile(Tea.LogFile("issues.log"))
|
||||||
|
if err != nil {
|
||||||
|
if formatJSON {
|
||||||
|
fmt.Print("[]")
|
||||||
|
} else {
|
||||||
|
fmt.Println("no issues yet")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var issueMaps = []maps.Map{}
|
||||||
|
err = json.Unmarshal(data, &issueMaps)
|
||||||
|
if err != nil {
|
||||||
|
if formatJSON {
|
||||||
|
fmt.Print("[]")
|
||||||
|
} else {
|
||||||
|
fmt.Println("no issues yet")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if formatJSON {
|
||||||
|
fmt.Print(string(data))
|
||||||
|
} else {
|
||||||
|
if len(issueMaps) == 0 {
|
||||||
|
fmt.Println("no issues yet")
|
||||||
|
} else {
|
||||||
|
for i, issue := range issueMaps {
|
||||||
|
fmt.Println("issue " + types.String(i+1) + ": " + issue.GetString("message"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
app.On("instance", func() {
|
||||||
|
var sock = gosock.NewTmpSock(teaconst.ProcessName)
|
||||||
|
reply, err := sock.Send(&gosock.Command{Code: "instance"})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("[ERROR]" + err.Error())
|
||||||
|
} else {
|
||||||
|
replyJSON, err := json.MarshalIndent(reply.Params, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("[ERROR]marshal result failed: " + err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Println(string(replyJSON))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
app.Run(func() {
|
app.Run(func() {
|
||||||
nodes.NewAPINode().Start()
|
nodes.NewAPINode().Start()
|
||||||
|
|||||||
3
dist/.gitignore
vendored
3
dist/.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
*.zip
|
*.zip
|
||||||
|
edge-api
|
||||||
31
go.mod
31
go.mod
@@ -1,6 +1,6 @@
|
|||||||
module github.com/TeaOSLab/EdgeAPI
|
module github.com/TeaOSLab/EdgeAPI
|
||||||
|
|
||||||
go 1.16
|
go 1.18
|
||||||
|
|
||||||
replace github.com/TeaOSLab/EdgeCommon => ../EdgeCommon
|
replace github.com/TeaOSLab/EdgeCommon => ../EdgeCommon
|
||||||
|
|
||||||
@@ -12,9 +12,8 @@ require (
|
|||||||
github.com/go-acme/lego/v4 v4.5.2
|
github.com/go-acme/lego/v4 v4.5.2
|
||||||
github.com/go-sql-driver/mysql v1.5.0
|
github.com/go-sql-driver/mysql v1.5.0
|
||||||
github.com/golang/protobuf v1.5.2
|
github.com/golang/protobuf v1.5.2
|
||||||
github.com/iwind/TeaGo v0.0.0-20220408064305-92be81dc2f7c
|
github.com/iwind/TeaGo v0.0.0-20220811034530-657e3f15b79e
|
||||||
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3
|
github.com/iwind/gosock v0.0.0-20220505115348-f88412125a62
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
|
||||||
github.com/mozillazg/go-pinyin v0.18.0
|
github.com/mozillazg/go-pinyin v0.18.0
|
||||||
github.com/pkg/sftp v1.12.0
|
github.com/pkg/sftp v1.12.0
|
||||||
github.com/shirou/gopsutil/v3 v3.22.2
|
github.com/shirou/gopsutil/v3 v3.22.2
|
||||||
@@ -22,5 +21,27 @@ require (
|
|||||||
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8
|
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8
|
||||||
google.golang.org/grpc v1.45.0
|
google.golang.org/grpc v1.45.0
|
||||||
google.golang.org/protobuf v1.27.1
|
google.golang.org/protobuf v1.27.1
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/cenkalti/backoff/v4 v4.1.1 // indirect
|
||||||
|
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||||
|
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||||
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
|
github.com/kr/fs v0.1.0 // indirect
|
||||||
|
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
|
||||||
|
github.com/miekg/dns v1.1.43 // indirect
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
|
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
|
||||||
|
github.com/tklauser/go-sysconf v0.3.9 // indirect
|
||||||
|
github.com/tklauser/numcpus v0.3.0 // indirect
|
||||||
|
github.com/yusufpapurcu/wmi v1.2.2 // indirect
|
||||||
|
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
|
||||||
|
golang.org/x/text v0.3.7 // indirect
|
||||||
|
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e // indirect
|
||||||
|
gopkg.in/ini.v1 v1.62.0 // indirect
|
||||||
|
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
14
go.sum
14
go.sum
@@ -66,7 +66,6 @@ github.com/c-bata/go-prompt v0.2.5/go.mod h1:vFnjEGDIIA/Lib7giyE4E9c50Lvl8j0S+7F
|
|||||||
github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ=
|
github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ=
|
||||||
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
|
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
|
||||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||||
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
|
|
||||||
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
|
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
|
||||||
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
|
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
|
||||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
@@ -238,12 +237,14 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
|
|||||||
github.com/infobloxopen/infoblox-go-client v1.1.1/go.mod h1:BXiw7S2b9qJoM8MS40vfgCNB2NLHGusk1DtO16BD9zI=
|
github.com/infobloxopen/infoblox-go-client v1.1.1/go.mod h1:BXiw7S2b9qJoM8MS40vfgCNB2NLHGusk1DtO16BD9zI=
|
||||||
github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||||
github.com/iwind/TeaGo v0.0.0-20220304043459-0dd944a5b475/go.mod h1:HRHK0zoC/og3c9/hKosD9yYVMTnnzm3PgXUdhRYHaLc=
|
github.com/iwind/TeaGo v0.0.0-20220304043459-0dd944a5b475/go.mod h1:HRHK0zoC/og3c9/hKosD9yYVMTnnzm3PgXUdhRYHaLc=
|
||||||
github.com/iwind/TeaGo v0.0.0-20220322141208-22f88d04004d h1:e8fkTKras/RXQWECApM9fKlFWujjYjEClpshkmZmtYg=
|
github.com/iwind/TeaGo v0.0.0-20220408111647-f36b9bba3570 h1:zqz2FiMMkSHXWO1EsTRJDPTwX9xQ4uuyD5GAE4JGlhM=
|
||||||
github.com/iwind/TeaGo v0.0.0-20220322141208-22f88d04004d/go.mod h1:HRHK0zoC/og3c9/hKosD9yYVMTnnzm3PgXUdhRYHaLc=
|
github.com/iwind/TeaGo v0.0.0-20220408111647-f36b9bba3570/go.mod h1:HRHK0zoC/og3c9/hKosD9yYVMTnnzm3PgXUdhRYHaLc=
|
||||||
github.com/iwind/TeaGo v0.0.0-20220408064305-92be81dc2f7c h1:ugjYZ74FJGWlfDKKraNgMyDTeS4vbXHe89JGUVQIJMo=
|
github.com/iwind/TeaGo v0.0.0-20220811034530-657e3f15b79e h1:cw4b6ecXdXvLd45YSstD8r9ClcnVK4ljZMZCept2aOk=
|
||||||
github.com/iwind/TeaGo v0.0.0-20220408064305-92be81dc2f7c/go.mod h1:HRHK0zoC/og3c9/hKosD9yYVMTnnzm3PgXUdhRYHaLc=
|
github.com/iwind/TeaGo v0.0.0-20220811034530-657e3f15b79e/go.mod h1:fi/Pq+/5m2HZoseM+39dMF57ANXRt6w4PkGu3NXPc5s=
|
||||||
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3 h1:aBSonas7vFcgTj9u96/bWGILGv1ZbUSTLiOzcI1ZT6c=
|
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3 h1:aBSonas7vFcgTj9u96/bWGILGv1ZbUSTLiOzcI1ZT6c=
|
||||||
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3/go.mod h1:H5Q7SXwbx3a97ecJkaS2sD77gspzE7HFUafBO0peEyA=
|
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3/go.mod h1:H5Q7SXwbx3a97ecJkaS2sD77gspzE7HFUafBO0peEyA=
|
||||||
|
github.com/iwind/gosock v0.0.0-20220505115348-f88412125a62 h1:HJH6RDheAY156DnIfJSD/bEvqyXzsZuE2gzs8PuUjoo=
|
||||||
|
github.com/iwind/gosock v0.0.0-20220505115348-f88412125a62/go.mod h1:H5Q7SXwbx3a97ecJkaS2sD77gspzE7HFUafBO0peEyA=
|
||||||
github.com/jarcoal/httpmock v1.0.5/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
|
github.com/jarcoal/httpmock v1.0.5/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
|
||||||
github.com/jarcoal/httpmock v1.0.6/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
|
github.com/jarcoal/httpmock v1.0.6/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
|
||||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
|
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
|
||||||
@@ -559,7 +560,6 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b
|
|||||||
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
|
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
|
||||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
|
||||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
|
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
|
||||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||||
@@ -791,6 +791,8 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
|||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
|||||||
@@ -1,71 +0,0 @@
|
|||||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
||||||
|
|
||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type BaseStorage struct {
|
|
||||||
isOk bool
|
|
||||||
version int
|
|
||||||
firewallOnly bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *BaseStorage) SetVersion(version int) {
|
|
||||||
this.version = version
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *BaseStorage) Version() int {
|
|
||||||
return this.version
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *BaseStorage) IsOk() bool {
|
|
||||||
return this.isOk
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *BaseStorage) SetOk(isOk bool) {
|
|
||||||
this.isOk = isOk
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *BaseStorage) SetFirewallOnly(firewallOnly bool) {
|
|
||||||
this.firewallOnly = firewallOnly
|
|
||||||
}
|
|
||||||
|
|
||||||
// Marshal 对日志进行编码
|
|
||||||
func (this *BaseStorage) Marshal(accessLog *pb.HTTPAccessLog) ([]byte, error) {
|
|
||||||
return json.Marshal(accessLog)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FormatVariables 格式化字符串中的变量
|
|
||||||
func (this *BaseStorage) FormatVariables(s string) string {
|
|
||||||
now := time.Now()
|
|
||||||
return configutils.ParseVariables(s, func(varName string) (value string) {
|
|
||||||
switch varName {
|
|
||||||
case "year":
|
|
||||||
return strconv.Itoa(now.Year())
|
|
||||||
case "month":
|
|
||||||
return fmt.Sprintf("%02d", now.Month())
|
|
||||||
case "week":
|
|
||||||
_, week := now.ISOWeek()
|
|
||||||
return fmt.Sprintf("%02d", week)
|
|
||||||
case "day":
|
|
||||||
return fmt.Sprintf("%02d", now.Day())
|
|
||||||
case "hour":
|
|
||||||
return fmt.Sprintf("%02d", now.Hour())
|
|
||||||
case "minute":
|
|
||||||
return fmt.Sprintf("%02d", now.Minute())
|
|
||||||
case "second":
|
|
||||||
return fmt.Sprintf("%02d", now.Second())
|
|
||||||
case "date":
|
|
||||||
return fmt.Sprintf("%d%02d%02d", now.Year(), now.Month(), now.Day())
|
|
||||||
}
|
|
||||||
|
|
||||||
return varName
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,99 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"os/exec"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// CommandStorage 通过命令行存储
|
|
||||||
type CommandStorage struct {
|
|
||||||
BaseStorage
|
|
||||||
|
|
||||||
config *serverconfigs.AccessLogCommandStorageConfig
|
|
||||||
|
|
||||||
writeLocker sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCommandStorage(config *serverconfigs.AccessLogCommandStorageConfig) *CommandStorage {
|
|
||||||
return &CommandStorage{config: config}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *CommandStorage) Config() interface{} {
|
|
||||||
return this.config
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start 启动
|
|
||||||
func (this *CommandStorage) Start() error {
|
|
||||||
if len(this.config.Command) == 0 {
|
|
||||||
return errors.New("'command' should not be empty")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 写入日志
|
|
||||||
func (this *CommandStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
|
||||||
if len(accessLogs) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
this.writeLocker.Lock()
|
|
||||||
defer this.writeLocker.Unlock()
|
|
||||||
|
|
||||||
cmd := exec.Command(this.config.Command, this.config.Args...)
|
|
||||||
if len(this.config.Dir) > 0 {
|
|
||||||
cmd.Dir = this.config.Dir
|
|
||||||
}
|
|
||||||
|
|
||||||
stdout := bytes.NewBuffer([]byte{})
|
|
||||||
cmd.Stdout = stdout
|
|
||||||
|
|
||||||
w, err := cmd.StdinPipe()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = cmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, accessLog := range accessLogs {
|
|
||||||
if this.firewallOnly && accessLog.FirewallPolicyId == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := this.Marshal(accessLog)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
_, err = w.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = w.Write([]byte("\n"))
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ = w.Close()
|
|
||||||
err = cmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
|
|
||||||
if stdout.Len() > 0 {
|
|
||||||
logs.Error(errors.New(string(stdout.Bytes())))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close 关闭
|
|
||||||
func (this *CommandStorage) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestCommandStorage_Write(t *testing.T) {
|
|
||||||
php, err := exec.LookPath("php")
|
|
||||||
if err != nil { // not found php, so we can not test
|
|
||||||
t.Log("php:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
before := time.Now()
|
|
||||||
|
|
||||||
storage := NewCommandStorage(&serverconfigs.AccessLogCommandStorageConfig{
|
|
||||||
Command: php,
|
|
||||||
Args: []string{cwd + "/tests/command_storage.php"},
|
|
||||||
})
|
|
||||||
err = storage.Start()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = storage.Write([]*pb.HTTPAccessLog{
|
|
||||||
{
|
|
||||||
RequestMethod: "GET",
|
|
||||||
RequestPath: "/hello",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestMethod: "GET",
|
|
||||||
RequestPath: "/world",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestMethod: "GET",
|
|
||||||
RequestPath: "/lu",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestMethod: "GET",
|
|
||||||
RequestPath: "/ping",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = storage.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Log(time.Since(before).Seconds(), "seconds")
|
|
||||||
}
|
|
||||||
@@ -1,131 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ESStorage ElasticSearch存储策略
|
|
||||||
type ESStorage struct {
|
|
||||||
BaseStorage
|
|
||||||
|
|
||||||
config *serverconfigs.AccessLogESStorageConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewESStorage(config *serverconfigs.AccessLogESStorageConfig) *ESStorage {
|
|
||||||
return &ESStorage{config: config}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *ESStorage) Config() interface{} {
|
|
||||||
return this.config
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start 开启
|
|
||||||
func (this *ESStorage) Start() error {
|
|
||||||
if len(this.config.Endpoint) == 0 {
|
|
||||||
return errors.New("'endpoint' should not be nil")
|
|
||||||
}
|
|
||||||
if !regexp.MustCompile(`(?i)^(http|https)://`).MatchString(this.config.Endpoint) {
|
|
||||||
this.config.Endpoint = "http://" + this.config.Endpoint
|
|
||||||
}
|
|
||||||
if len(this.config.Index) == 0 {
|
|
||||||
return errors.New("'index' should not be nil")
|
|
||||||
}
|
|
||||||
if len(this.config.MappingType) == 0 {
|
|
||||||
return errors.New("'mappingType' should not be nil")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 写入日志
|
|
||||||
func (this *ESStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
|
||||||
if len(accessLogs) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
bulk := &strings.Builder{}
|
|
||||||
indexName := this.FormatVariables(this.config.Index)
|
|
||||||
typeName := this.FormatVariables(this.config.MappingType)
|
|
||||||
for _, accessLog := range accessLogs {
|
|
||||||
if this.firewallOnly && accessLog.FirewallPolicyId == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(accessLog.RequestId) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
opData, err := json.Marshal(map[string]interface{}{
|
|
||||||
"index": map[string]interface{}{
|
|
||||||
"_index": indexName,
|
|
||||||
"_type": typeName,
|
|
||||||
"_id": accessLog.RequestId,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_ES_STORAGE", "write failed: "+err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := this.Marshal(accessLog)
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_ES_STORAGE", "marshal data failed: "+err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
bulk.Write(opData)
|
|
||||||
bulk.WriteString("\n")
|
|
||||||
bulk.Write(data)
|
|
||||||
bulk.WriteString("\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
if bulk.Len() == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, this.config.Endpoint+"/_bulk", strings.NewReader(bulk.String()))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("User-Agent", strings.ReplaceAll(teaconst.ProductName, " ", "-")+"/"+teaconst.Version)
|
|
||||||
if len(this.config.Username) > 0 || len(this.config.Password) > 0 {
|
|
||||||
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(this.config.Username+":"+this.config.Password)))
|
|
||||||
}
|
|
||||||
client := utils.SharedHttpClient(10 * time.Second)
|
|
||||||
defer func() {
|
|
||||||
_ = req.Body.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
_ = resp.Body.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
bodyData, _ := ioutil.ReadAll(resp.Body)
|
|
||||||
return errors.New("ElasticSearch response status code: " + fmt.Sprintf("%d", resp.StatusCode) + " content: " + string(bodyData))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close 关闭
|
|
||||||
func (this *ESStorage) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestESStorage_Write(t *testing.T) {
|
|
||||||
storage := NewESStorage(&serverconfigs.AccessLogESStorageConfig{
|
|
||||||
Endpoint: "http://127.0.0.1:9200",
|
|
||||||
Index: "logs",
|
|
||||||
MappingType: "accessLogs",
|
|
||||||
Username: "hello",
|
|
||||||
Password: "world",
|
|
||||||
})
|
|
||||||
err := storage.Start()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
err = storage.Write([]*pb.HTTPAccessLog{
|
|
||||||
{
|
|
||||||
RequestMethod: "POST",
|
|
||||||
RequestPath: "/1",
|
|
||||||
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
|
||||||
TimeISO8601: "2018-07-23T22:23:35+08:00",
|
|
||||||
Header: map[string]*pb.Strings{
|
|
||||||
"Content-Type": {Values: []string{"text/html"}},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestMethod: "GET",
|
|
||||||
RequestPath: "/2",
|
|
||||||
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
|
||||||
TimeISO8601: "2018-07-23T22:23:35+08:00",
|
|
||||||
Header: map[string]*pb.Strings{
|
|
||||||
"Content-Type": {Values: []string{"text/css"}},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = storage.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,130 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// FileStorage 文件存储策略
|
|
||||||
type FileStorage struct {
|
|
||||||
BaseStorage
|
|
||||||
|
|
||||||
config *serverconfigs.AccessLogFileStorageConfig
|
|
||||||
|
|
||||||
writeLocker sync.Mutex
|
|
||||||
|
|
||||||
files map[string]*os.File // path => *File
|
|
||||||
filesLocker sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewFileStorage(config *serverconfigs.AccessLogFileStorageConfig) *FileStorage {
|
|
||||||
return &FileStorage{
|
|
||||||
config: config,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *FileStorage) Config() interface{} {
|
|
||||||
return this.config
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start 开启
|
|
||||||
func (this *FileStorage) Start() error {
|
|
||||||
if len(this.config.Path) == 0 {
|
|
||||||
return errors.New("'path' should not be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
this.files = map[string]*os.File{}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write 写入日志
|
|
||||||
func (this *FileStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
|
||||||
if len(accessLogs) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
fp := this.fp()
|
|
||||||
if fp == nil {
|
|
||||||
return errors.New("file pointer should not be nil")
|
|
||||||
}
|
|
||||||
this.writeLocker.Lock()
|
|
||||||
defer this.writeLocker.Unlock()
|
|
||||||
|
|
||||||
for _, accessLog := range accessLogs {
|
|
||||||
if this.firewallOnly && accessLog.FirewallPolicyId == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
data, err := this.Marshal(accessLog)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
_, err = fp.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
_ = this.Close()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
_, _ = fp.WriteString("\n")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close 关闭
|
|
||||||
func (this *FileStorage) Close() error {
|
|
||||||
this.filesLocker.Lock()
|
|
||||||
defer this.filesLocker.Unlock()
|
|
||||||
|
|
||||||
var resultErr error
|
|
||||||
for _, f := range this.files {
|
|
||||||
err := f.Close()
|
|
||||||
if err != nil {
|
|
||||||
resultErr = err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return resultErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *FileStorage) fp() *os.File {
|
|
||||||
path := this.FormatVariables(this.config.Path)
|
|
||||||
|
|
||||||
this.filesLocker.Lock()
|
|
||||||
defer this.filesLocker.Unlock()
|
|
||||||
fp, ok := this.files[path]
|
|
||||||
if ok {
|
|
||||||
return fp
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关闭其他的文件
|
|
||||||
for _, f := range this.files {
|
|
||||||
_ = f.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 是否创建文件目录
|
|
||||||
if this.config.AutoCreate {
|
|
||||||
dir := filepath.Dir(path)
|
|
||||||
_, err := os.Stat(dir)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
err = os.MkdirAll(dir, 0777)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 打开新文件
|
|
||||||
fp, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
this.files[path] = fp
|
|
||||||
|
|
||||||
return fp
|
|
||||||
}
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestFileStorage_Write(t *testing.T) {
|
|
||||||
storage := NewFileStorage(&serverconfigs.AccessLogFileStorageConfig{
|
|
||||||
Path: Tea.Root + "/logs/access-${date}.log",
|
|
||||||
})
|
|
||||||
err := storage.Start()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
err = storage.Write([]*pb.HTTPAccessLog{
|
|
||||||
{
|
|
||||||
RequestPath: "/hello",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestPath: "/world",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
err = storage.Write([]*pb.HTTPAccessLog{
|
|
||||||
{
|
|
||||||
RequestPath: "/1",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestPath: "/2",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
err = storage.Write([]*pb.HTTPAccessLog{
|
|
||||||
{
|
|
||||||
RequestMethod: "POST",
|
|
||||||
RequestPath: "/1",
|
|
||||||
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestMethod: "GET",
|
|
||||||
RequestPath: "/2",
|
|
||||||
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = storage.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
||||||
|
|
||||||
package accesslogs
|
|
||||||
|
|
||||||
import "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
|
|
||||||
// StorageInterface 日志存储接口
|
|
||||||
type StorageInterface interface {
|
|
||||||
// Version 获取版本
|
|
||||||
Version() int
|
|
||||||
|
|
||||||
// SetVersion 设置版本
|
|
||||||
SetVersion(version int)
|
|
||||||
|
|
||||||
// SetFirewallOnly 设置是否只处理防火墙相关的访问日志
|
|
||||||
SetFirewallOnly(firewallOnly bool)
|
|
||||||
|
|
||||||
IsOk() bool
|
|
||||||
|
|
||||||
SetOk(ok bool)
|
|
||||||
|
|
||||||
// Config 获取配置
|
|
||||||
Config() interface{}
|
|
||||||
|
|
||||||
// Start 开启
|
|
||||||
Start() error
|
|
||||||
|
|
||||||
// Write 写入日志
|
|
||||||
Write(accessLogs []*pb.HTTPAccessLog) error
|
|
||||||
|
|
||||||
// Close 关闭
|
|
||||||
Close() error
|
|
||||||
}
|
|
||||||
@@ -1,185 +0,0 @@
|
|||||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
||||||
|
|
||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/lists"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
var SharedStorageManager = NewStorageManager()
|
|
||||||
|
|
||||||
type StorageManager struct {
|
|
||||||
storageMap map[int64]StorageInterface // policyId => Storage
|
|
||||||
|
|
||||||
locker sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewStorageManager() *StorageManager {
|
|
||||||
return &StorageManager{
|
|
||||||
storageMap: map[int64]StorageInterface{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *StorageManager) Start() {
|
|
||||||
var ticker = time.NewTicker(1 * time.Minute)
|
|
||||||
if Tea.IsTesting() {
|
|
||||||
ticker = time.NewTicker(5 * time.Second)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 启动时执行一次
|
|
||||||
var err = this.Loop()
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "update error: "+err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// 循环执行
|
|
||||||
for range ticker.C {
|
|
||||||
err := this.Loop()
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "update error: "+err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Loop 更新
|
|
||||||
func (this *StorageManager) Loop() error {
|
|
||||||
policies, err := models.SharedHTTPAccessLogPolicyDAO.FindAllEnabledAndOnPolicies(nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
var policyIds = []int64{}
|
|
||||||
for _, policy := range policies {
|
|
||||||
if policy.IsOn {
|
|
||||||
policyIds = append(policyIds, int64(policy.Id))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.locker.Lock()
|
|
||||||
defer this.locker.Unlock()
|
|
||||||
|
|
||||||
// 关闭不用的
|
|
||||||
for policyId, storage := range this.storageMap {
|
|
||||||
if !lists.ContainsInt64(policyIds, policyId) {
|
|
||||||
err := storage.Close()
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "close '"+types.String(policyId)+"' failed: "+err.Error())
|
|
||||||
}
|
|
||||||
delete(this.storageMap, policyId)
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "remove '"+types.String(policyId)+"'")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, policy := range policies {
|
|
||||||
var policyId = int64(policy.Id)
|
|
||||||
storage, ok := this.storageMap[policyId]
|
|
||||||
if ok {
|
|
||||||
// 检查配置是否有变更
|
|
||||||
if types.Int(policy.Version) != storage.Version() {
|
|
||||||
err = storage.Close()
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "close policy '"+types.String(policyId)+"' failed: "+err.Error())
|
|
||||||
|
|
||||||
// 继续往下执行
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(policy.Options) > 0 {
|
|
||||||
err = json.Unmarshal(policy.Options, storage.Config())
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "unmarshal policy '"+types.String(policyId)+"' config failed: "+err.Error())
|
|
||||||
storage.SetOk(false)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
storage.SetVersion(types.Int(policy.Version))
|
|
||||||
storage.SetFirewallOnly(policy.FirewallOnly == 1)
|
|
||||||
err := storage.Start()
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "start policy '"+types.String(policyId)+"' failed: "+err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
storage.SetOk(true)
|
|
||||||
remotelogs.Println("ACCESS_LOG_STORAGE_MANAGER", "restart policy '"+types.String(policyId)+"'")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
storage, err := this.createStorage(policy.Type, policy.Options)
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "create policy '"+types.String(policyId)+"' failed: "+err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
storage.SetVersion(types.Int(policy.Version))
|
|
||||||
storage.SetFirewallOnly(policy.FirewallOnly == 1)
|
|
||||||
this.storageMap[policyId] = storage
|
|
||||||
err = storage.Start()
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("ACCESS_LOG_STORAGE_MANAGER", "start policy '"+types.String(policyId)+"' failed: "+err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
storage.SetOk(true)
|
|
||||||
remotelogs.Println("ACCESS_LOG_STORAGE_MANAGER", "start policy '"+types.String(policyId)+"'")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *StorageManager) createStorage(storageType string, optionsJSON []byte) (StorageInterface, error) {
|
|
||||||
switch storageType {
|
|
||||||
case serverconfigs.AccessLogStorageTypeFile:
|
|
||||||
var config = &serverconfigs.AccessLogFileStorageConfig{}
|
|
||||||
if len(optionsJSON) > 0 {
|
|
||||||
err := json.Unmarshal(optionsJSON, config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NewFileStorage(config), nil
|
|
||||||
case serverconfigs.AccessLogStorageTypeES:
|
|
||||||
var config = &serverconfigs.AccessLogESStorageConfig{}
|
|
||||||
if len(optionsJSON) > 0 {
|
|
||||||
err := json.Unmarshal(optionsJSON, config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NewESStorage(config), nil
|
|
||||||
case serverconfigs.AccessLogStorageTypeTCP:
|
|
||||||
var config = &serverconfigs.AccessLogTCPStorageConfig{}
|
|
||||||
if len(optionsJSON) > 0 {
|
|
||||||
err := json.Unmarshal(optionsJSON, config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NewTCPStorage(config), nil
|
|
||||||
case serverconfigs.AccessLogStorageTypeSyslog:
|
|
||||||
var config = &serverconfigs.AccessLogSyslogStorageConfig{}
|
|
||||||
if len(optionsJSON) > 0 {
|
|
||||||
err := json.Unmarshal(optionsJSON, config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NewSyslogStorage(config), nil
|
|
||||||
case serverconfigs.AccessLogStorageTypeCommand:
|
|
||||||
var config = &serverconfigs.AccessLogCommandStorageConfig{}
|
|
||||||
if len(optionsJSON) > 0 {
|
|
||||||
err := json.Unmarshal(optionsJSON, config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NewCommandStorage(config), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, errors.New("invalid policy type '" + storageType + "'")
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestStorageManager_Loop(t *testing.T) {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
var storage = NewStorageManager()
|
|
||||||
err := storage.Loop()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(storage.storageMap)
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
||||||
|
|
||||||
//go:build !plus
|
|
||||||
// +build !plus
|
|
||||||
|
|
||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 写入日志
|
|
||||||
func (this *StorageManager) Write(policyId int64, accessLogs []*pb.HTTPAccessLog) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,140 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"os/exec"
|
|
||||||
"runtime"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
type SyslogStorageProtocol = string
|
|
||||||
|
|
||||||
const (
|
|
||||||
SyslogStorageProtocolTCP SyslogStorageProtocol = "tcp"
|
|
||||||
SyslogStorageProtocolUDP SyslogStorageProtocol = "udp"
|
|
||||||
SyslogStorageProtocolNone SyslogStorageProtocol = "none"
|
|
||||||
SyslogStorageProtocolSocket SyslogStorageProtocol = "socket"
|
|
||||||
)
|
|
||||||
|
|
||||||
type SyslogStoragePriority = int
|
|
||||||
|
|
||||||
// SyslogStorage syslog存储策略
|
|
||||||
type SyslogStorage struct {
|
|
||||||
BaseStorage
|
|
||||||
|
|
||||||
config *serverconfigs.AccessLogSyslogStorageConfig
|
|
||||||
|
|
||||||
exe string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSyslogStorage(config *serverconfigs.AccessLogSyslogStorageConfig) *SyslogStorage {
|
|
||||||
return &SyslogStorage{config: config}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *SyslogStorage) Config() interface{} {
|
|
||||||
return this.config
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start 开启
|
|
||||||
func (this *SyslogStorage) Start() error {
|
|
||||||
if runtime.GOOS != "linux" {
|
|
||||||
return errors.New("'syslog' storage only works on linux")
|
|
||||||
}
|
|
||||||
|
|
||||||
exe, err := exec.LookPath("logger")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
this.exe = exe
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 写入日志
|
|
||||||
func (this *SyslogStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
|
||||||
if len(accessLogs) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
args := []string{}
|
|
||||||
if len(this.config.Tag) > 0 {
|
|
||||||
args = append(args, "-t", this.config.Tag)
|
|
||||||
}
|
|
||||||
|
|
||||||
if this.config.Priority >= 0 {
|
|
||||||
args = append(args, "-p", strconv.Itoa(this.config.Priority))
|
|
||||||
}
|
|
||||||
|
|
||||||
switch this.config.Protocol {
|
|
||||||
case SyslogStorageProtocolTCP:
|
|
||||||
args = append(args, "-T")
|
|
||||||
if len(this.config.ServerAddr) > 0 {
|
|
||||||
args = append(args, "-n", this.config.ServerAddr)
|
|
||||||
}
|
|
||||||
if this.config.ServerPort > 0 {
|
|
||||||
args = append(args, "-P", strconv.Itoa(this.config.ServerPort))
|
|
||||||
}
|
|
||||||
case SyslogStorageProtocolUDP:
|
|
||||||
args = append(args, "-d")
|
|
||||||
if len(this.config.ServerAddr) > 0 {
|
|
||||||
args = append(args, "-n", this.config.ServerAddr)
|
|
||||||
}
|
|
||||||
if this.config.ServerPort > 0 {
|
|
||||||
args = append(args, "-P", strconv.Itoa(this.config.ServerPort))
|
|
||||||
}
|
|
||||||
case SyslogStorageProtocolSocket:
|
|
||||||
args = append(args, "-u")
|
|
||||||
args = append(args, this.config.Socket)
|
|
||||||
case SyslogStorageProtocolNone:
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
args = append(args, "-S", "10240")
|
|
||||||
|
|
||||||
cmd := exec.Command(this.exe, args...)
|
|
||||||
w, err := cmd.StdinPipe()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = cmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, accessLog := range accessLogs {
|
|
||||||
if this.firewallOnly && accessLog.FirewallPolicyId == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
data, err := this.Marshal(accessLog)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
_, err = w.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = w.Write([]byte("\n"))
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = w.Close()
|
|
||||||
err = cmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close 关闭
|
|
||||||
func (this *SyslogStorage) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"net"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TCPStorage TCP存储策略
|
|
||||||
type TCPStorage struct {
|
|
||||||
BaseStorage
|
|
||||||
|
|
||||||
config *serverconfigs.AccessLogTCPStorageConfig
|
|
||||||
|
|
||||||
writeLocker sync.Mutex
|
|
||||||
|
|
||||||
connLocker sync.Mutex
|
|
||||||
conn net.Conn
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTCPStorage(config *serverconfigs.AccessLogTCPStorageConfig) *TCPStorage {
|
|
||||||
return &TCPStorage{config: config}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TCPStorage) Config() interface{} {
|
|
||||||
return this.config
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start 开启
|
|
||||||
func (this *TCPStorage) Start() error {
|
|
||||||
if len(this.config.Network) == 0 {
|
|
||||||
return errors.New("'network' should not be empty")
|
|
||||||
}
|
|
||||||
if len(this.config.Addr) == 0 {
|
|
||||||
return errors.New("'addr' should not be empty")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 写入日志
|
|
||||||
func (this *TCPStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
|
|
||||||
if len(accessLogs) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
err := this.connect()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
conn := this.conn
|
|
||||||
if conn == nil {
|
|
||||||
return errors.New("connection should not be nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
this.writeLocker.Lock()
|
|
||||||
defer this.writeLocker.Unlock()
|
|
||||||
|
|
||||||
for _, accessLog := range accessLogs {
|
|
||||||
if this.firewallOnly && accessLog.FirewallPolicyId == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
data, err := this.Marshal(accessLog)
|
|
||||||
if err != nil {
|
|
||||||
logs.Error(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
_, err = conn.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
_ = this.Close()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
_, err = conn.Write([]byte("\n"))
|
|
||||||
if err != nil {
|
|
||||||
_ = this.Close()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close 关闭
|
|
||||||
func (this *TCPStorage) Close() error {
|
|
||||||
this.connLocker.Lock()
|
|
||||||
defer this.connLocker.Unlock()
|
|
||||||
|
|
||||||
if this.conn != nil {
|
|
||||||
err := this.conn.Close()
|
|
||||||
this.conn = nil
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TCPStorage) connect() error {
|
|
||||||
this.connLocker.Lock()
|
|
||||||
defer this.connLocker.Unlock()
|
|
||||||
|
|
||||||
if this.conn != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
conn, err := net.Dial(this.config.Network, this.config.Addr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
this.conn = conn
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
package accesslogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
||||||
"net"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestTCPStorage_Write(t *testing.T) {
|
|
||||||
go func() {
|
|
||||||
server, err := net.Listen("tcp", "127.0.0.1:9981")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for {
|
|
||||||
conn, err := server.Accept()
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
buf := make([]byte, 1024)
|
|
||||||
for {
|
|
||||||
n, err := conn.Read(buf)
|
|
||||||
if n > 0 {
|
|
||||||
t.Log(string(buf[:n]))
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
_ = server.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
storage := NewTCPStorage(&serverconfigs.AccessLogTCPStorageConfig{
|
|
||||||
Network: "tcp",
|
|
||||||
Addr: "127.0.0.1:9981",
|
|
||||||
})
|
|
||||||
err := storage.Start()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
err = storage.Write([]*pb.HTTPAccessLog{
|
|
||||||
{
|
|
||||||
RequestMethod: "POST",
|
|
||||||
RequestPath: "/1",
|
|
||||||
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
RequestMethod: "GET",
|
|
||||||
RequestPath: "/2",
|
|
||||||
TimeLocal: time.Now().Format("2/Jan/2006:15:04:05 -0700"),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
|
|
||||||
err = storage.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
// test command storage
|
|
||||||
|
|
||||||
// open access log file
|
|
||||||
$fp = fopen("/tmp/goedge-command-storage.log", "a+");
|
|
||||||
|
|
||||||
// read access logs from stdin
|
|
||||||
$stdin = fopen("php://stdin", "r");
|
|
||||||
while(true) {
|
|
||||||
if (feof($stdin)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$line = fgets($stdin);
|
|
||||||
|
|
||||||
// write to access log file
|
|
||||||
fwrite($fp, $line);
|
|
||||||
}
|
|
||||||
|
|
||||||
// close file pointers
|
|
||||||
fclose($fp);
|
|
||||||
fclose($stdin);
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/go-acme/lego/v4/challenge/dns01"
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
||||||
"github.com/go-acme/lego/v4/lego"
|
"github.com/go-acme/lego/v4/lego"
|
||||||
acmelog "github.com/go-acme/lego/v4/log"
|
acmelog "github.com/go-acme/lego/v4/log"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ func (this *MyProvider) CleanUp(domain, token, keyAuth string) error {
|
|||||||
|
|
||||||
// 参考 https://go-acme.github.io/lego/usage/library/
|
// 参考 https://go-acme.github.io/lego/usage/library/
|
||||||
func TestGenerate(t *testing.T) {
|
func TestGenerate(t *testing.T) {
|
||||||
acmelog.Logger = log.New(ioutil.Discard, "", log.LstdFlags)
|
acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
|
||||||
|
|
||||||
// 生成私钥
|
// 生成私钥
|
||||||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||||
@@ -94,7 +94,7 @@ func TestGenerate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGenerate_EAB(t *testing.T) {
|
func TestGenerate_EAB(t *testing.T) {
|
||||||
acmelog.Logger = log.New(ioutil.Discard, "", log.LstdFlags)
|
acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
|
||||||
|
|
||||||
// 生成私钥
|
// 生成私钥
|
||||||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/go-acme/lego/v4/lego"
|
"github.com/go-acme/lego/v4/lego"
|
||||||
acmelog "github.com/go-acme/lego/v4/log"
|
acmelog "github.com/go-acme/lego/v4/log"
|
||||||
"github.com/go-acme/lego/v4/registration"
|
"github.com/go-acme/lego/v4/registration"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ func (this *Request) Run() (certData []byte, keyData []byte, err error) {
|
|||||||
|
|
||||||
func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
||||||
if !this.debug {
|
if !this.debug {
|
||||||
acmelog.Logger = log.New(ioutil.Discard, "", log.LstdFlags)
|
acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.task.User == nil {
|
if this.task.User == nil {
|
||||||
@@ -138,7 +138,7 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
|||||||
|
|
||||||
func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
|
func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
|
||||||
if !this.debug {
|
if !this.debug {
|
||||||
acmelog.Logger = log.New(ioutil.Discard, "", log.LstdFlags)
|
acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.task.User == nil {
|
if this.task.User == nil {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
@@ -42,7 +41,7 @@ func SharedAPIConfig() (*APIConfig, error) {
|
|||||||
var data []byte
|
var data []byte
|
||||||
var err error
|
var err error
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
data, err = ioutil.ReadFile(path)
|
data, err = os.ReadFile(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if path == localFile {
|
if path == localFile {
|
||||||
isFromLocal = true
|
isFromLocal = true
|
||||||
@@ -63,7 +62,7 @@ func SharedAPIConfig() (*APIConfig, error) {
|
|||||||
|
|
||||||
if !isFromLocal {
|
if !isFromLocal {
|
||||||
// 恢复文件
|
// 恢复文件
|
||||||
_ = ioutil.WriteFile(localFile, data, 0666)
|
_ = os.WriteFile(localFile, data, 0666)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 恢复数据库文件
|
// 恢复数据库文件
|
||||||
@@ -80,9 +79,9 @@ func SharedAPIConfig() (*APIConfig, error) {
|
|||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
data, err := ioutil.ReadFile(path)
|
data, err := os.ReadFile(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
_ = ioutil.WriteFile(dbConfigFile, data, 0666)
|
_ = os.WriteFile(dbConfigFile, data, 0666)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -122,14 +121,14 @@ func (this *APIConfig) WriteFile(path string) error {
|
|||||||
for _, backupDir := range backupDirs {
|
for _, backupDir := range backupDirs {
|
||||||
stat, err := os.Stat(backupDir)
|
stat, err := os.Stat(backupDir)
|
||||||
if err == nil && stat.IsDir() {
|
if err == nil && stat.IsDir() {
|
||||||
_ = ioutil.WriteFile(backupDir+"/"+filename, data, 0666)
|
_ = os.WriteFile(backupDir+"/"+filename, data, 0666)
|
||||||
} else if err != nil && os.IsNotExist(err) {
|
} else if err != nil && os.IsNotExist(err) {
|
||||||
err = os.Mkdir(backupDir, 0777)
|
err = os.Mkdir(backupDir, 0777)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
_ = ioutil.WriteFile(backupDir+"/"+filename, data, 0666)
|
_ = os.WriteFile(backupDir+"/"+filename, data, 0666)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ioutil.WriteFile(path, data, 0666)
|
return os.WriteFile(path, data, 0666)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package teaconst
|
package teaconst
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = "0.4.7"
|
Version = "0.5.0"
|
||||||
|
|
||||||
ProductName = "Edge API"
|
ProductName = "Edge API"
|
||||||
ProcessName = "edge-api"
|
ProcessName = "edge-api"
|
||||||
@@ -18,13 +18,13 @@ const (
|
|||||||
|
|
||||||
// 其他节点版本号,用来检测是否有需要升级的节点
|
// 其他节点版本号,用来检测是否有需要升级的节点
|
||||||
|
|
||||||
NodeVersion = "0.4.7"
|
NodeVersion = "0.5.0"
|
||||||
UserNodeVersion = "0.3.3"
|
UserNodeVersion = "0.4.0"
|
||||||
AuthorityNodeVersion = "0.0.2"
|
AuthorityNodeVersion = "0.0.2"
|
||||||
MonitorNodeVersion = "0.0.3"
|
MonitorNodeVersion = "0.0.4"
|
||||||
DNSNodeVersion = "0.2.2"
|
DNSNodeVersion = "0.2.5"
|
||||||
ReportNodeVersion = "0.1.0"
|
ReportNodeVersion = "0.1.1"
|
||||||
|
|
||||||
// SQLVersion SQL版本号
|
// SQLVersion SQL版本号
|
||||||
SQLVersion = "8"
|
SQLVersion = "2"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,9 +2,18 @@
|
|||||||
|
|
||||||
package teaconst
|
package teaconst
|
||||||
|
|
||||||
var (
|
import (
|
||||||
IsPlus = false
|
"crypto/sha1"
|
||||||
MaxNodes int32 = 0
|
"fmt"
|
||||||
NodeId int64 = 0
|
"github.com/iwind/TeaGo/rands"
|
||||||
Debug = false
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
IsPlus = false
|
||||||
|
MaxNodes int32 = 0
|
||||||
|
NodeId int64 = 0
|
||||||
|
Debug = false
|
||||||
|
InstanceCode = fmt.Sprintf("%x", sha1.Sum([]byte("INSTANCE"+types.String(time.Now().UnixNano())+"@"+types.String(rands.Int64()))))
|
||||||
)
|
)
|
||||||
|
|||||||
33
internal/db/models/accounts/order_method_dao.go
Normal file
33
internal/db/models/accounts/order_method_dao.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
OrderMethodStateEnabled = 1 // 已启用
|
||||||
|
OrderMethodStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
type OrderMethodDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewOrderMethodDAO() *OrderMethodDAO {
|
||||||
|
return dbs.NewDAO(&OrderMethodDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeOrderMethods",
|
||||||
|
Model: new(OrderMethod),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*OrderMethodDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedOrderMethodDAO *OrderMethodDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedOrderMethodDAO = NewOrderMethodDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package nameservers
|
package accounts_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
36
internal/db/models/accounts/order_method_model.go
Normal file
36
internal/db/models/accounts/order_method_model.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
|
// OrderMethod 订单支付方式
|
||||||
|
type OrderMethod struct {
|
||||||
|
Id uint32 `field:"id"` // ID
|
||||||
|
Name string `field:"name"` // 名称
|
||||||
|
IsOn bool `field:"isOn"` // 是否启用
|
||||||
|
Description string `field:"description"` // 描述
|
||||||
|
ParentCode string `field:"parentCode"` // 内置的父级代号
|
||||||
|
Code string `field:"code"` // 代号
|
||||||
|
Url string `field:"url"` // URL
|
||||||
|
Secret string `field:"secret"` // 密钥
|
||||||
|
Params dbs.JSON `field:"params"` // 参数
|
||||||
|
Order uint32 `field:"order"` // 排序
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderMethodOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
Name interface{} // 名称
|
||||||
|
IsOn interface{} // 是否启用
|
||||||
|
Description interface{} // 描述
|
||||||
|
ParentCode interface{} // 内置的父级代号
|
||||||
|
Code interface{} // 代号
|
||||||
|
Url interface{} // URL
|
||||||
|
Secret interface{} // 密钥
|
||||||
|
Params interface{} // 参数
|
||||||
|
Order interface{} // 排序
|
||||||
|
State interface{} // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewOrderMethodOperator() *OrderMethodOperator {
|
||||||
|
return &OrderMethodOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/accounts/order_method_model_ext.go
Normal file
1
internal/db/models/accounts/order_method_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package accounts
|
||||||
33
internal/db/models/accounts/user_order_dao.go
Normal file
33
internal/db/models/accounts/user_order_dao.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
UserOrderStateEnabled = 1 // 已启用
|
||||||
|
UserOrderStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserOrderDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewUserOrderDAO() *UserOrderDAO {
|
||||||
|
return dbs.NewDAO(&UserOrderDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeUserOrders",
|
||||||
|
Model: new(UserOrder),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*UserOrderDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedUserOrderDAO *UserOrderDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedUserOrderDAO = NewUserOrderDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package nameservers
|
package accounts_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
28
internal/db/models/accounts/user_order_log_dao.go
Normal file
28
internal/db/models/accounts/user_order_log_dao.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserOrderLogDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewUserOrderLogDAO() *UserOrderLogDAO {
|
||||||
|
return dbs.NewDAO(&UserOrderLogDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeUserOrderLogs",
|
||||||
|
Model: new(UserOrderLog),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*UserOrderLogDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedUserOrderLogDAO *UserOrderLogDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedUserOrderLogDAO = NewUserOrderLogDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
6
internal/db/models/accounts/user_order_log_dao_test.go
Normal file
6
internal/db/models/accounts/user_order_log_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package accounts_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
)
|
||||||
28
internal/db/models/accounts/user_order_log_model.go
Normal file
28
internal/db/models/accounts/user_order_log_model.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
|
// UserOrderLog 订单日志
|
||||||
|
type UserOrderLog struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
AdminId uint64 `field:"adminId"` // 管理员ID
|
||||||
|
UserId uint64 `field:"userId"` // 用户ID
|
||||||
|
OrderId uint64 `field:"orderId"` // 订单ID
|
||||||
|
Status string `field:"status"` // 状态
|
||||||
|
Snapshot dbs.JSON `field:"snapshot"` // 状态快照
|
||||||
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserOrderLogOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
AdminId interface{} // 管理员ID
|
||||||
|
UserId interface{} // 用户ID
|
||||||
|
OrderId interface{} // 订单ID
|
||||||
|
Status interface{} // 状态
|
||||||
|
Snapshot interface{} // 状态快照
|
||||||
|
CreatedAt interface{} // 创建时间
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserOrderLogOperator() *UserOrderLogOperator {
|
||||||
|
return &UserOrderLogOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/accounts/user_order_log_model_ext.go
Normal file
1
internal/db/models/accounts/user_order_log_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package accounts
|
||||||
40
internal/db/models/accounts/user_order_model.go
Normal file
40
internal/db/models/accounts/user_order_model.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package accounts
|
||||||
|
|
||||||
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
|
// UserOrder 用户订单
|
||||||
|
type UserOrder struct {
|
||||||
|
Id uint64 `field:"id"` // 用户订单
|
||||||
|
UserId uint64 `field:"userId"` // 用户ID
|
||||||
|
Code string `field:"code"` // 订单号
|
||||||
|
Type string `field:"type"` // 订单类型
|
||||||
|
MethodId uint32 `field:"methodId"` // 支付方式
|
||||||
|
Status string `field:"status"` // 订单状态
|
||||||
|
Amount float64 `field:"amount"` // 金额
|
||||||
|
Params dbs.JSON `field:"params"` // 附加参数
|
||||||
|
ExpiredAt uint64 `field:"expiredAt"` // 过期时间
|
||||||
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
|
CancelledAt uint64 `field:"cancelledAt"` // 取消时间
|
||||||
|
FinishedAt uint64 `field:"finishedAt"` // 结束时间
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserOrderOperator struct {
|
||||||
|
Id interface{} // 用户订单
|
||||||
|
UserId interface{} // 用户ID
|
||||||
|
Code interface{} // 订单号
|
||||||
|
Type interface{} // 订单类型
|
||||||
|
MethodId interface{} // 支付方式
|
||||||
|
Status interface{} // 订单状态
|
||||||
|
Amount interface{} // 金额
|
||||||
|
Params interface{} // 附加参数
|
||||||
|
ExpiredAt interface{} // 过期时间
|
||||||
|
CreatedAt interface{} // 创建时间
|
||||||
|
CancelledAt interface{} // 取消时间
|
||||||
|
FinishedAt interface{} // 结束时间
|
||||||
|
State interface{} // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserOrderOperator() *UserOrderOperator {
|
||||||
|
return &UserOrderOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/accounts/user_order_model_ext.go
Normal file
1
internal/db/models/accounts/user_order_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package accounts
|
||||||
@@ -29,7 +29,7 @@ func init() {
|
|||||||
|
|
||||||
// 创建认证信息
|
// 创建认证信息
|
||||||
func (this *ACMEAuthenticationDAO) CreateAuth(tx *dbs.Tx, taskId int64, domain string, token string, key string) error {
|
func (this *ACMEAuthenticationDAO) CreateAuth(tx *dbs.Tx, taskId int64, domain string, token string, key string) error {
|
||||||
op := NewACMEAuthenticationOperator()
|
var op = NewACMEAuthenticationOperator()
|
||||||
op.TaskId = taskId
|
op.TaskId = taskId
|
||||||
op.Domain = domain
|
op.Domain = domain
|
||||||
op.Token = token
|
op.Token = token
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ func (this *ACMETaskDAO) ListEnabledACMETasks(tx *dbs.Tx, adminId int64, userId
|
|||||||
|
|
||||||
// CreateACMETask 创建任务
|
// CreateACMETask 创建任务
|
||||||
func (this *ACMETaskDAO) CreateACMETask(tx *dbs.Tx, adminId int64, userId int64, authType acmeutils.AuthType, acmeUserId int64, dnsProviderId int64, dnsDomain string, domains []string, autoRenew bool, authURL string) (int64, error) {
|
func (this *ACMETaskDAO) CreateACMETask(tx *dbs.Tx, adminId int64, userId int64, authType acmeutils.AuthType, acmeUserId int64, dnsProviderId int64, dnsDomain string, domains []string, autoRenew bool, authURL string) (int64, error) {
|
||||||
op := NewACMETaskOperator()
|
var op = NewACMETaskOperator()
|
||||||
op.AdminId = adminId
|
op.AdminId = adminId
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
op.AuthType = authType
|
op.AuthType = authType
|
||||||
@@ -204,7 +204,7 @@ func (this *ACMETaskDAO) UpdateACMETask(tx *dbs.Tx, acmeTaskId int64, acmeUserId
|
|||||||
return errors.New("invalid acmeTaskId")
|
return errors.New("invalid acmeTaskId")
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewACMETaskOperator()
|
var op = NewACMETaskOperator()
|
||||||
op.Id = acmeTaskId
|
op.Id = acmeTaskId
|
||||||
op.AcmeUserId = acmeUserId
|
op.AcmeUserId = acmeUserId
|
||||||
op.DnsProviderId = dnsProviderId
|
op.DnsProviderId = dnsProviderId
|
||||||
@@ -240,7 +240,7 @@ func (this *ACMETaskDAO) UpdateACMETaskCert(tx *dbs.Tx, taskId int64, certId int
|
|||||||
return errors.New("invalid taskId")
|
return errors.New("invalid taskId")
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewACMETaskOperator()
|
var op = NewACMETaskOperator()
|
||||||
op.Id = taskId
|
op.Id = taskId
|
||||||
op.CertId = certId
|
op.CertId = certId
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func init() {
|
|||||||
|
|
||||||
// 生成日志
|
// 生成日志
|
||||||
func (this *ACMETaskLogDAO) CreateACMETaskLog(tx *dbs.Tx, taskId int64, isOk bool, errMsg string) error {
|
func (this *ACMETaskLogDAO) CreateACMETaskLog(tx *dbs.Tx, taskId int64, isOk bool, errMsg string) error {
|
||||||
op := NewACMETaskLogOperator()
|
var op = NewACMETaskLogOperator()
|
||||||
op.TaskId = taskId
|
op.TaskId = taskId
|
||||||
op.Error = errMsg
|
op.Error = errMsg
|
||||||
op.IsOk = isOk
|
op.IsOk = isOk
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ func (this *ACMEUserDAO) CreateACMEUser(tx *dbs.Tx, adminId int64, userId int64,
|
|||||||
}
|
}
|
||||||
privateKeyText := base64.StdEncoding.EncodeToString(privateKeyData)
|
privateKeyText := base64.StdEncoding.EncodeToString(privateKeyData)
|
||||||
|
|
||||||
op := NewACMEUserOperator()
|
var op = NewACMEUserOperator()
|
||||||
op.AdminId = adminId
|
op.AdminId = adminId
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
op.ProviderCode = providerCode
|
op.ProviderCode = providerCode
|
||||||
@@ -104,7 +104,7 @@ func (this *ACMEUserDAO) UpdateACMEUser(tx *dbs.Tx, acmeUserId int64, descriptio
|
|||||||
if acmeUserId <= 0 {
|
if acmeUserId <= 0 {
|
||||||
return errors.New("invalid acmeUserId")
|
return errors.New("invalid acmeUserId")
|
||||||
}
|
}
|
||||||
op := NewACMEUserOperator()
|
var op = NewACMEUserOperator()
|
||||||
op.Id = acmeUserId
|
op.Id = acmeUserId
|
||||||
op.Description = description
|
op.Description = description
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -116,7 +116,7 @@ func (this *ACMEUserDAO) UpdateACMEUserRegistration(tx *dbs.Tx, acmeUserId int64
|
|||||||
if acmeUserId <= 0 {
|
if acmeUserId <= 0 {
|
||||||
return errors.New("invalid acmeUserId")
|
return errors.New("invalid acmeUserId")
|
||||||
}
|
}
|
||||||
op := NewACMEUserOperator()
|
var op = NewACMEUserOperator()
|
||||||
op.Id = acmeUserId
|
op.Id = acmeUserId
|
||||||
op.Registration = registrationJSON
|
op.Registration = registrationJSON
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
|
|||||||
@@ -63,6 +63,19 @@ func (this *AdminDAO) FindEnabledAdmin(tx *dbs.Tx, id int64) (*Admin, error) {
|
|||||||
return result.(*Admin), err
|
return result.(*Admin), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindBasicAdmin 查找管理员基本信息
|
||||||
|
func (this *AdminDAO) FindBasicAdmin(tx *dbs.Tx, id int64) (*Admin, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Result("id", "username", "fullname").
|
||||||
|
Pk(id).
|
||||||
|
Attr("state", AdminStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*Admin), err
|
||||||
|
}
|
||||||
|
|
||||||
// ExistEnabledAdmin 检查管理员是否存在
|
// ExistEnabledAdmin 检查管理员是否存在
|
||||||
func (this *AdminDAO) ExistEnabledAdmin(tx *dbs.Tx, adminId int64) (bool, error) {
|
func (this *AdminDAO) ExistEnabledAdmin(tx *dbs.Tx, adminId int64) (bool, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
@@ -115,7 +128,7 @@ func (this *AdminDAO) UpdateAdminPassword(tx *dbs.Tx, adminId int64, password st
|
|||||||
if adminId <= 0 {
|
if adminId <= 0 {
|
||||||
return errors.New("invalid adminId")
|
return errors.New("invalid adminId")
|
||||||
}
|
}
|
||||||
op := NewAdminOperator()
|
var op = NewAdminOperator()
|
||||||
op.Id = adminId
|
op.Id = adminId
|
||||||
op.Password = stringutil.Md5(password)
|
op.Password = stringutil.Md5(password)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -124,7 +137,7 @@ func (this *AdminDAO) UpdateAdminPassword(tx *dbs.Tx, adminId int64, password st
|
|||||||
|
|
||||||
// CreateAdmin 创建管理员
|
// CreateAdmin 创建管理员
|
||||||
func (this *AdminDAO) CreateAdmin(tx *dbs.Tx, username string, canLogin bool, password string, fullname string, isSuper bool, modulesJSON []byte) (int64, error) {
|
func (this *AdminDAO) CreateAdmin(tx *dbs.Tx, username string, canLogin bool, password string, fullname string, isSuper bool, modulesJSON []byte) (int64, error) {
|
||||||
op := NewAdminOperator()
|
var op = NewAdminOperator()
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = AdminStateEnabled
|
op.State = AdminStateEnabled
|
||||||
op.Username = username
|
op.Username = username
|
||||||
@@ -149,7 +162,7 @@ func (this *AdminDAO) UpdateAdminInfo(tx *dbs.Tx, adminId int64, fullname string
|
|||||||
if adminId <= 0 {
|
if adminId <= 0 {
|
||||||
return errors.New("invalid adminId")
|
return errors.New("invalid adminId")
|
||||||
}
|
}
|
||||||
op := NewAdminOperator()
|
var op = NewAdminOperator()
|
||||||
op.Id = adminId
|
op.Id = adminId
|
||||||
op.Fullname = fullname
|
op.Fullname = fullname
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -161,7 +174,7 @@ func (this *AdminDAO) UpdateAdmin(tx *dbs.Tx, adminId int64, username string, ca
|
|||||||
if adminId <= 0 {
|
if adminId <= 0 {
|
||||||
return errors.New("invalid adminId")
|
return errors.New("invalid adminId")
|
||||||
}
|
}
|
||||||
op := NewAdminOperator()
|
var op = NewAdminOperator()
|
||||||
op.Id = adminId
|
op.Id = adminId
|
||||||
op.Fullname = fullname
|
op.Fullname = fullname
|
||||||
op.Username = username
|
op.Username = username
|
||||||
@@ -198,7 +211,7 @@ func (this *AdminDAO) UpdateAdminLogin(tx *dbs.Tx, adminId int64, username strin
|
|||||||
if adminId <= 0 {
|
if adminId <= 0 {
|
||||||
return errors.New("invalid adminId")
|
return errors.New("invalid adminId")
|
||||||
}
|
}
|
||||||
op := NewAdminOperator()
|
var op = NewAdminOperator()
|
||||||
op.Id = adminId
|
op.Id = adminId
|
||||||
op.Username = username
|
op.Username = username
|
||||||
if len(password) > 0 {
|
if len(password) > 0 {
|
||||||
@@ -213,7 +226,7 @@ func (this *AdminDAO) UpdateAdminModules(tx *dbs.Tx, adminId int64, allowModules
|
|||||||
if adminId <= 0 {
|
if adminId <= 0 {
|
||||||
return errors.New("invalid adminId")
|
return errors.New("invalid adminId")
|
||||||
}
|
}
|
||||||
op := NewAdminOperator()
|
var op = NewAdminOperator()
|
||||||
op.Id = adminId
|
op.Id = adminId
|
||||||
op.Modules = allowModulesJSON
|
op.Modules = allowModulesJSON
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
|
|||||||
@@ -14,23 +14,23 @@ type Admin struct {
|
|||||||
UpdatedAt uint64 `field:"updatedAt"` // 修改时间
|
UpdatedAt uint64 `field:"updatedAt"` // 修改时间
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
Modules dbs.JSON `field:"modules"` // 允许的模块
|
Modules dbs.JSON `field:"modules"` // 允许的模块
|
||||||
CanLogin uint8 `field:"canLogin"` // 是否可以登录
|
CanLogin bool `field:"canLogin"` // 是否可以登录
|
||||||
Theme string `field:"theme"` // 模板设置
|
Theme string `field:"theme"` // 模板设置
|
||||||
}
|
}
|
||||||
|
|
||||||
type AdminOperator struct {
|
type AdminOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn any // 是否启用
|
||||||
Username interface{} // 用户名
|
Username any // 用户名
|
||||||
Password interface{} // 密码
|
Password any // 密码
|
||||||
Fullname interface{} // 全名
|
Fullname any // 全名
|
||||||
IsSuper interface{} // 是否为超级管理员
|
IsSuper any // 是否为超级管理员
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt any // 创建时间
|
||||||
UpdatedAt interface{} // 修改时间
|
UpdatedAt any // 修改时间
|
||||||
State interface{} // 状态
|
State any // 状态
|
||||||
Modules interface{} // 允许的模块
|
Modules any // 允许的模块
|
||||||
CanLogin interface{} // 是否可以登录
|
CanLogin any // 是否可以登录
|
||||||
Theme interface{} // 模板设置
|
Theme any // 模板设置
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAdminOperator() *AdminOperator {
|
func NewAdminOperator() *AdminOperator {
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ func (this *APIAccessTokenDAO) GenerateAccessToken(tx *dbs.Tx, adminId int64, us
|
|||||||
token = rands.String(128) // TODO 增强安全性,将来使用 base64_encode(encrypt(salt+random)) 算法来代替
|
token = rands.String(128) // TODO 增强安全性,将来使用 base64_encode(encrypt(salt+random)) 算法来代替
|
||||||
expiresAt = time.Now().Unix() + 7200
|
expiresAt = time.Now().Unix() + 7200
|
||||||
|
|
||||||
op := NewAPIAccessTokenOperator()
|
var op = NewAPIAccessTokenOperator()
|
||||||
|
|
||||||
if accessToken != nil {
|
if accessToken != nil {
|
||||||
op.Id = accessToken.(*APIAccessToken).Id
|
op.Id = accessToken.(*APIAccessToken).Id
|
||||||
|
|||||||
@@ -58,15 +58,22 @@ func (this *APINodeDAO) EnableAPINode(tx *dbs.Tx, id int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DisableAPINode 禁用条目
|
// DisableAPINode 禁用条目
|
||||||
func (this *APINodeDAO) DisableAPINode(tx *dbs.Tx, id int64) error {
|
func (this *APINodeDAO) DisableAPINode(tx *dbs.Tx, nodeId int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(nodeId).
|
||||||
Set("state", APINodeStateDisabled).
|
Set("state", APINodeStateDisabled).
|
||||||
Update()
|
Update()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return this.NotifyUpdate(tx, id)
|
|
||||||
|
err = this.NotifyUpdate(tx, nodeId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除运行日志
|
||||||
|
return SharedNodeLogDAO.DeleteNodeLogs(tx, nodeconfigs.NodeRoleAPI, nodeId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledAPINode 查找启用中的条目
|
// FindEnabledAPINode 查找启用中的条目
|
||||||
@@ -127,7 +134,7 @@ func (this *APINodeDAO) CreateAPINode(tx *dbs.Tx, name string, description strin
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewAPINodeOperator()
|
var op = NewAPINodeOperator()
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.UniqueId = uniqueId
|
op.UniqueId = uniqueId
|
||||||
op.Secret = secret
|
op.Secret = secret
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ func (this *ApiTokenDAO) FindEnabledTokenWithRole(tx *dbs.Tx, role string) (*Api
|
|||||||
|
|
||||||
// CreateAPIToken 保存API Token
|
// CreateAPIToken 保存API Token
|
||||||
func (this *ApiTokenDAO) CreateAPIToken(tx *dbs.Tx, nodeId string, secret string, role nodeconfigs.NodeRole) error {
|
func (this *ApiTokenDAO) CreateAPIToken(tx *dbs.Tx, nodeId string, secret string, role nodeconfigs.NodeRole) error {
|
||||||
op := NewApiTokenOperator()
|
var op = NewApiTokenOperator()
|
||||||
op.NodeId = nodeId
|
op.NodeId = nodeId
|
||||||
op.Secret = secret
|
op.Secret = secret
|
||||||
op.Role = role
|
op.Role = role
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
package authority
|
package authority
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type AuthorityKeyDAO dbs.DAO
|
type AuthorityKeyDAO dbs.DAO
|
||||||
@@ -33,63 +29,3 @@ func init() {
|
|||||||
_, _ = SharedAuthorityKeyDAO.IsPlus(nil)
|
_, _ = SharedAuthorityKeyDAO.IsPlus(nil)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateKey 设置Key
|
|
||||||
func (this *AuthorityKeyDAO) UpdateKey(tx *dbs.Tx, value string, dayFrom string, dayTo string, hostname string, macAddresses []string, company string) error {
|
|
||||||
one, err := this.Query(tx).
|
|
||||||
AscPk().
|
|
||||||
Find()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
op := NewAuthorityKeyOperator()
|
|
||||||
if one != nil {
|
|
||||||
op.Id = one.(*AuthorityKey).Id
|
|
||||||
}
|
|
||||||
op.Value = value
|
|
||||||
op.DayFrom = dayFrom
|
|
||||||
op.DayTo = dayTo
|
|
||||||
op.Hostname = hostname
|
|
||||||
|
|
||||||
if len(macAddresses) == 0 {
|
|
||||||
macAddresses = []string{}
|
|
||||||
}
|
|
||||||
macAddressesJSON, err := json.Marshal(macAddresses)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
op.MacAddresses = macAddressesJSON
|
|
||||||
op.Company = company
|
|
||||||
op.UpdatedAt = time.Now().Unix()
|
|
||||||
|
|
||||||
return this.Save(tx, op)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadKey 读取Key
|
|
||||||
func (this *AuthorityKeyDAO) ReadKey(tx *dbs.Tx) (key *AuthorityKey, err error) {
|
|
||||||
one, err := this.Query(tx).
|
|
||||||
AscPk().
|
|
||||||
Find()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if one == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
key = one.(*AuthorityKey)
|
|
||||||
|
|
||||||
// 顺便更新相关变量
|
|
||||||
if key.DayTo >= timeutil.Format("Y-m-d") {
|
|
||||||
teaconst.IsPlus = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResetKey 重置Key
|
|
||||||
func (this *AuthorityKeyDAO) ResetKey(tx *dbs.Tx) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Delete()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
package authority
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestAuthorityKeyDAO_UpdateValue(t *testing.T) {
|
|
||||||
err := NewAuthorityKeyDAO().UpdateKey(nil, "12345678", "", "", "", []string{}, "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log("ok")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAuthorityKeyDAO_ReadValue(t *testing.T) {
|
|
||||||
value, err := NewAuthorityKeyDAO().ReadKey(nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Log(value)
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package authority
|
package authority
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
@@ -119,7 +120,7 @@ func (this *AuthorityNodeDAO) CreateAuthorityNode(tx *dbs.Tx, name string, descr
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewAuthorityNodeOperator()
|
var op = NewAuthorityNodeOperator()
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.UniqueId = uniqueId
|
op.UniqueId = uniqueId
|
||||||
op.Secret = secret
|
op.Secret = secret
|
||||||
@@ -140,7 +141,7 @@ func (this *AuthorityNodeDAO) UpdateAuthorityNode(tx *dbs.Tx, nodeId int64, name
|
|||||||
return errors.New("invalid nodeId")
|
return errors.New("invalid nodeId")
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewAuthorityNodeOperator()
|
var op = NewAuthorityNodeOperator()
|
||||||
op.Id = nodeId
|
op.Id = nodeId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.Description = description
|
op.Description = description
|
||||||
@@ -188,13 +189,19 @@ func (this *AuthorityNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateNodeStatus 更改节点状态
|
// UpdateNodeStatus 更改节点状态
|
||||||
func (this *AuthorityNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error {
|
func (this *AuthorityNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, nodeStatus *nodeconfigs.NodeStatus) error {
|
||||||
if statusJSON == nil {
|
if nodeStatus == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
_, err := this.Query(tx).
|
|
||||||
|
nodeStatusJSON, err := json.Marshal(nodeStatus)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = this.Query(tx).
|
||||||
Pk(nodeId).
|
Pk(nodeId).
|
||||||
Set("status", string(statusJSON)).
|
Set("status", nodeStatusJSON).
|
||||||
Update()
|
Update()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ func (this *ClientBrowserDAO) CreateBrowser(tx *dbs.Tx, browserName string) (int
|
|||||||
return browserId, nil
|
return browserId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewClientBrowserOperator()
|
var op = NewClientBrowserOperator()
|
||||||
op.Name = browserName
|
op.Name = browserName
|
||||||
codes := []string{browserName}
|
codes := []string{browserName}
|
||||||
codesJSON, err := json.Marshal(codes)
|
codesJSON, err := json.Marshal(codes)
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ func (this *ClientSystemDAO) CreateSystem(tx *dbs.Tx, systemName string) (int64,
|
|||||||
return systemId, nil
|
return systemId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewClientSystemOperator()
|
var op = NewClientSystemOperator()
|
||||||
op.Name = systemName
|
op.Name = systemName
|
||||||
|
|
||||||
codes := []string{systemName}
|
codes := []string{systemName}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/encrypt"
|
"github.com/TeaOSLab/EdgeAPI/internal/encrypt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -49,12 +50,17 @@ func (this *DBNodeDAO) EnableDBNode(tx *dbs.Tx, id int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DisableDBNode 禁用条目
|
// DisableDBNode 禁用条目
|
||||||
func (this *DBNodeDAO) DisableDBNode(tx *dbs.Tx, id int64) error {
|
func (this *DBNodeDAO) DisableDBNode(tx *dbs.Tx, nodeId int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(nodeId).
|
||||||
Set("state", DBNodeStateDisabled).
|
Set("state", DBNodeStateDisabled).
|
||||||
Update()
|
Update()
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除运行日志
|
||||||
|
return SharedNodeLogDAO.DeleteNodeLogs(tx, nodeconfigs.NodeRoleDatabase, nodeId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledDBNode 查找启用中的条目
|
// FindEnabledDBNode 查找启用中的条目
|
||||||
@@ -103,7 +109,7 @@ func (this *DBNodeDAO) ListEnabledNodes(tx *dbs.Tx, offset int64, size int64) (r
|
|||||||
|
|
||||||
// CreateDBNode 创建节点
|
// CreateDBNode 创建节点
|
||||||
func (this *DBNodeDAO) CreateDBNode(tx *dbs.Tx, isOn bool, name string, description string, host string, port int32, database string, username string, password string, charset string) (int64, error) {
|
func (this *DBNodeDAO) CreateDBNode(tx *dbs.Tx, isOn bool, name string, description string, host string, port int32, database string, username string, password string, charset string) (int64, error) {
|
||||||
op := NewDBNodeOperator()
|
var op = NewDBNodeOperator()
|
||||||
op.State = NodeStateEnabled
|
op.State = NodeStateEnabled
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.Name = name
|
op.Name = name
|
||||||
@@ -126,7 +132,7 @@ func (this *DBNodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, isOn bool, name stri
|
|||||||
if nodeId <= 0 {
|
if nodeId <= 0 {
|
||||||
return errors.New("invalid nodeId")
|
return errors.New("invalid nodeId")
|
||||||
}
|
}
|
||||||
op := NewDBNodeOperator()
|
var op = NewDBNodeOperator()
|
||||||
op.Id = nodeId
|
op.Id = nodeId
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.Name = name
|
op.Name = name
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ func (this *DNSDomainDAO) FindDNSDomainName(tx *dbs.Tx, id int64) (string, error
|
|||||||
|
|
||||||
// CreateDomain 创建域名
|
// CreateDomain 创建域名
|
||||||
func (this *DNSDomainDAO) CreateDomain(tx *dbs.Tx, adminId int64, userId int64, providerId int64, name string) (int64, error) {
|
func (this *DNSDomainDAO) CreateDomain(tx *dbs.Tx, adminId int64, userId int64, providerId int64, name string) (int64, error) {
|
||||||
op := NewDNSDomainOperator()
|
var op = NewDNSDomainOperator()
|
||||||
op.ProviderId = providerId
|
op.ProviderId = providerId
|
||||||
op.AdminId = adminId
|
op.AdminId = adminId
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
@@ -111,7 +111,7 @@ func (this *DNSDomainDAO) UpdateDomain(tx *dbs.Tx, domainId int64, name string,
|
|||||||
if domainId <= 0 {
|
if domainId <= 0 {
|
||||||
return errors.New("invalid domainId")
|
return errors.New("invalid domainId")
|
||||||
}
|
}
|
||||||
op := NewDNSDomainOperator()
|
var op = NewDNSDomainOperator()
|
||||||
op.Id = domainId
|
op.Id = domainId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
@@ -146,7 +146,7 @@ func (this *DNSDomainDAO) UpdateDomainData(tx *dbs.Tx, domainId int64, data stri
|
|||||||
if domainId <= 0 {
|
if domainId <= 0 {
|
||||||
return errors.New("invalid domainId")
|
return errors.New("invalid domainId")
|
||||||
}
|
}
|
||||||
op := NewDNSDomainOperator()
|
var op = NewDNSDomainOperator()
|
||||||
op.Id = domainId
|
op.Id = domainId
|
||||||
op.Data = data
|
op.Data = data
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -158,7 +158,7 @@ func (this *DNSDomainDAO) UpdateDomainRecords(tx *dbs.Tx, domainId int64, record
|
|||||||
if domainId <= 0 {
|
if domainId <= 0 {
|
||||||
return errors.New("invalid domainId")
|
return errors.New("invalid domainId")
|
||||||
}
|
}
|
||||||
op := NewDNSDomainOperator()
|
var op = NewDNSDomainOperator()
|
||||||
op.Id = domainId
|
op.Id = domainId
|
||||||
op.Records = recordsJSON
|
op.Records = recordsJSON
|
||||||
op.DataUpdatedAt = time.Now().Unix()
|
op.DataUpdatedAt = time.Now().Unix()
|
||||||
@@ -171,7 +171,7 @@ func (this *DNSDomainDAO) UpdateDomainRoutes(tx *dbs.Tx, domainId int64, routesJ
|
|||||||
if domainId <= 0 {
|
if domainId <= 0 {
|
||||||
return errors.New("invalid domainId")
|
return errors.New("invalid domainId")
|
||||||
}
|
}
|
||||||
op := NewDNSDomainOperator()
|
var op = NewDNSDomainOperator()
|
||||||
op.Id = domainId
|
op.Id = domainId
|
||||||
op.Routes = routesJSON
|
op.Routes = routesJSON
|
||||||
op.DataUpdatedAt = time.Now().Unix()
|
op.DataUpdatedAt = time.Now().Unix()
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ func (this *DNSProviderDAO) FindEnabledDNSProvider(tx *dbs.Tx, id int64) (*DNSPr
|
|||||||
|
|
||||||
// CreateDNSProvider 创建服务商
|
// CreateDNSProvider 创建服务商
|
||||||
func (this *DNSProviderDAO) CreateDNSProvider(tx *dbs.Tx, adminId int64, userId int64, providerType string, name string, apiParamsJSON []byte) (int64, error) {
|
func (this *DNSProviderDAO) CreateDNSProvider(tx *dbs.Tx, adminId int64, userId int64, providerType string, name string, apiParamsJSON []byte) (int64, error) {
|
||||||
op := NewDNSProviderOperator()
|
var op = NewDNSProviderOperator()
|
||||||
op.AdminId = adminId
|
op.AdminId = adminId
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
op.Type = providerType
|
op.Type = providerType
|
||||||
@@ -90,7 +90,7 @@ func (this *DNSProviderDAO) UpdateDNSProvider(tx *dbs.Tx, dnsProviderId int64, n
|
|||||||
return errors.New("invalid dnsProviderId")
|
return errors.New("invalid dnsProviderId")
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewDNSProviderOperator()
|
var op = NewDNSProviderOperator()
|
||||||
op.Id = dnsProviderId
|
op.Id = dnsProviderId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
|
|
||||||
@@ -107,7 +107,7 @@ func (this *DNSProviderDAO) UpdateDNSProvider(tx *dbs.Tx, dnsProviderId int64, n
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CountAllEnabledDNSProviders 计算服务商数量
|
// CountAllEnabledDNSProviders 计算服务商数量
|
||||||
func (this *DNSProviderDAO) CountAllEnabledDNSProviders(tx *dbs.Tx, adminId int64, userId int64, keyword string, domain string) (int64, error) {
|
func (this *DNSProviderDAO) CountAllEnabledDNSProviders(tx *dbs.Tx, adminId int64, userId int64, keyword string, domain string, providerType string) (int64, error) {
|
||||||
var query = dbutils.NewQuery(tx, this, adminId, userId)
|
var query = dbutils.NewQuery(tx, this, adminId, userId)
|
||||||
if len(keyword) > 0 {
|
if len(keyword) > 0 {
|
||||||
query.Where("(name LIKE :keyword)").
|
query.Where("(name LIKE :keyword)").
|
||||||
@@ -119,12 +119,16 @@ func (this *DNSProviderDAO) CountAllEnabledDNSProviders(tx *dbs.Tx, adminId int6
|
|||||||
query.Param("domain", domain)
|
query.Param("domain", domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(providerType) > 0 {
|
||||||
|
query.Attr("type", providerType)
|
||||||
|
}
|
||||||
|
|
||||||
return query.State(DNSProviderStateEnabled).
|
return query.State(DNSProviderStateEnabled).
|
||||||
Count()
|
Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListEnabledDNSProviders 列出单页服务商
|
// ListEnabledDNSProviders 列出单页服务商
|
||||||
func (this *DNSProviderDAO) ListEnabledDNSProviders(tx *dbs.Tx, adminId int64, userId int64, keyword string, domain string, offset int64, size int64) (result []*DNSProvider, err error) {
|
func (this *DNSProviderDAO) ListEnabledDNSProviders(tx *dbs.Tx, adminId int64, userId int64, keyword string, domain string, providerType string, offset int64, size int64) (result []*DNSProvider, err error) {
|
||||||
var query = dbutils.NewQuery(tx, this, adminId, userId)
|
var query = dbutils.NewQuery(tx, this, adminId, userId)
|
||||||
if len(keyword) > 0 {
|
if len(keyword) > 0 {
|
||||||
query.Where("(name LIKE :keyword)").
|
query.Where("(name LIKE :keyword)").
|
||||||
@@ -134,6 +138,9 @@ func (this *DNSProviderDAO) ListEnabledDNSProviders(tx *dbs.Tx, adminId int64, u
|
|||||||
query.Where("id IN (SELECT providerId FROM " + SharedDNSDomainDAO.Table + " WHERE state=1 AND name=:domain)")
|
query.Where("id IN (SELECT providerId FROM " + SharedDNSDomainDAO.Table + " WHERE state=1 AND name=:domain)")
|
||||||
query.Param("domain", domain)
|
query.Param("domain", domain)
|
||||||
}
|
}
|
||||||
|
if len(providerType) > 0 {
|
||||||
|
query.Attr("type", providerType)
|
||||||
|
}
|
||||||
_, err = query.
|
_, err = query.
|
||||||
State(DNSProviderStateEnabled).
|
State(DNSProviderStateEnabled).
|
||||||
Offset(offset).
|
Offset(offset).
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ func (this *DNSTaskDAO) UpdateDNSTaskError(tx *dbs.Tx, taskId int64, err string)
|
|||||||
if taskId <= 0 {
|
if taskId <= 0 {
|
||||||
return errors.New("invalid taskId")
|
return errors.New("invalid taskId")
|
||||||
}
|
}
|
||||||
op := NewDNSTaskOperator()
|
var op = NewDNSTaskOperator()
|
||||||
op.Id = taskId
|
op.Id = taskId
|
||||||
op.IsDone = true
|
op.IsDone = true
|
||||||
op.Error = err
|
op.Error = err
|
||||||
@@ -156,7 +156,7 @@ func (this *DNSTaskDAO) UpdateDNSTaskDone(tx *dbs.Tx, taskId int64) error {
|
|||||||
if taskId <= 0 {
|
if taskId <= 0 {
|
||||||
return errors.New("invalid taskId")
|
return errors.New("invalid taskId")
|
||||||
}
|
}
|
||||||
op := NewDNSTaskOperator()
|
var op = NewDNSTaskOperator()
|
||||||
op.Id = taskId
|
op.Id = taskId
|
||||||
op.IsDone = true
|
op.IsDone = true
|
||||||
op.IsOk = true
|
op.IsOk = true
|
||||||
|
|||||||
@@ -29,9 +29,9 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建文件Chunk
|
// CreateFileChunk 创建文件Chunk
|
||||||
func (this *FileChunkDAO) CreateFileChunk(tx *dbs.Tx, fileId int64, data []byte) (int64, error) {
|
func (this *FileChunkDAO) CreateFileChunk(tx *dbs.Tx, fileId int64, data []byte) (int64, error) {
|
||||||
op := NewFileChunkOperator()
|
var op = NewFileChunkOperator()
|
||||||
op.FileId = fileId
|
op.FileId = fileId
|
||||||
op.Data = data
|
op.Data = data
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -41,7 +41,7 @@ func (this *FileChunkDAO) CreateFileChunk(tx *dbs.Tx, fileId int64, data []byte)
|
|||||||
return types.Int64(op.Id), nil
|
return types.Int64(op.Id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 列出所有的文件Chunk
|
// FindAllFileChunks 列出所有的文件Chunk
|
||||||
func (this *FileChunkDAO) FindAllFileChunks(tx *dbs.Tx, fileId int64) (result []*FileChunk, err error) {
|
func (this *FileChunkDAO) FindAllFileChunks(tx *dbs.Tx, fileId int64) (result []*FileChunk, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
Attr("fileId", fileId).
|
Attr("fileId", fileId).
|
||||||
@@ -51,7 +51,7 @@ func (this *FileChunkDAO) FindAllFileChunks(tx *dbs.Tx, fileId int64) (result []
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取文件的所有片段ID
|
// FindAllFileChunkIds 读取文件的所有片段ID
|
||||||
func (this *FileChunkDAO) FindAllFileChunkIds(tx *dbs.Tx, fileId int64) ([]int64, error) {
|
func (this *FileChunkDAO) FindAllFileChunkIds(tx *dbs.Tx, fileId int64) ([]int64, error) {
|
||||||
ones, err := this.Query(tx).
|
ones, err := this.Query(tx).
|
||||||
Attr("fileId", fileId).
|
Attr("fileId", fileId).
|
||||||
@@ -68,7 +68,7 @@ func (this *FileChunkDAO) FindAllFileChunkIds(tx *dbs.Tx, fileId int64) ([]int64
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除以前的文件
|
// DeleteFileChunks 删除以前的文件
|
||||||
func (this *FileChunkDAO) DeleteFileChunks(tx *dbs.Tx, fileId int64) error {
|
func (this *FileChunkDAO) DeleteFileChunks(tx *dbs.Tx, fileId int64) error {
|
||||||
if fileId <= 0 {
|
if fileId <= 0 {
|
||||||
return errors.New("invalid fileId")
|
return errors.New("invalid fileId")
|
||||||
@@ -79,7 +79,7 @@ func (this *FileChunkDAO) DeleteFileChunks(tx *dbs.Tx, fileId int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据ID查找片段
|
// FindFileChunk 根据ID查找片段
|
||||||
func (this *FileChunkDAO) FindFileChunk(tx *dbs.Tx, chunkId int64) (*FileChunk, error) {
|
func (this *FileChunkDAO) FindFileChunk(tx *dbs.Tx, chunkId int64) (*FileChunk, error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
Pk(chunkId).
|
Pk(chunkId).
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -33,7 +34,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启用条目
|
// EnableFile 启用条目
|
||||||
func (this *FileDAO) EnableFile(tx *dbs.Tx, id int64) error {
|
func (this *FileDAO) EnableFile(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -42,7 +43,7 @@ func (this *FileDAO) EnableFile(tx *dbs.Tx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁用条目
|
// DisableFile 禁用条目
|
||||||
func (this *FileDAO) DisableFile(tx *dbs.Tx, id int64) error {
|
func (this *FileDAO) DisableFile(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -51,7 +52,7 @@ func (this *FileDAO) DisableFile(tx *dbs.Tx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找启用中的条目
|
// FindEnabledFile 查找启用中的条目
|
||||||
func (this *FileDAO) FindEnabledFile(tx *dbs.Tx, id int64) (*File, error) {
|
func (this *FileDAO) FindEnabledFile(tx *dbs.Tx, id int64) (*File, error) {
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -63,9 +64,9 @@ func (this *FileDAO) FindEnabledFile(tx *dbs.Tx, id int64) (*File, error) {
|
|||||||
return result.(*File), err
|
return result.(*File), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建文件
|
// CreateFile 创建文件
|
||||||
func (this *FileDAO) CreateFile(tx *dbs.Tx, adminId int64, userId int64, businessType, description string, filename string, size int64, isPublic bool) (int64, error) {
|
func (this *FileDAO) CreateFile(tx *dbs.Tx, adminId int64, userId int64, businessType string, description string, filename string, size int64, mimeType string, isPublic bool) (int64, error) {
|
||||||
op := NewFileOperator()
|
var op = NewFileOperator()
|
||||||
op.AdminId = adminId
|
op.AdminId = adminId
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
op.Type = businessType
|
op.Type = businessType
|
||||||
@@ -74,6 +75,8 @@ func (this *FileDAO) CreateFile(tx *dbs.Tx, adminId int64, userId int64, busines
|
|||||||
op.Size = size
|
op.Size = size
|
||||||
op.Filename = filename
|
op.Filename = filename
|
||||||
op.IsPublic = isPublic
|
op.IsPublic = isPublic
|
||||||
|
op.Code = utils.Sha1RandomString()
|
||||||
|
op.MimeType = mimeType
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -82,7 +85,22 @@ func (this *FileDAO) CreateFile(tx *dbs.Tx, adminId int64, userId int64, busines
|
|||||||
return types.Int64(op.Id), nil
|
return types.Int64(op.Id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将文件置为已完成
|
// CheckUserFile 检查用户ID
|
||||||
|
func (this *FileDAO) CheckUserFile(tx *dbs.Tx, userId int64, fileId int64) error {
|
||||||
|
b, err := this.Query(tx).
|
||||||
|
Pk(fileId).
|
||||||
|
Attr("userId", userId).
|
||||||
|
Exist()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !b {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateFileIsFinished 将文件置为已完成
|
||||||
func (this *FileDAO) UpdateFileIsFinished(tx *dbs.Tx, fileId int64) error {
|
func (this *FileDAO) UpdateFileIsFinished(tx *dbs.Tx, fileId int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(fileId).
|
Pk(fileId).
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
// 文件管理
|
// File 文件管理
|
||||||
type File struct {
|
type File struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
|
Code string `field:"code"` // 代号
|
||||||
UserId uint32 `field:"userId"` // 用户ID
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
Description string `field:"description"` // 文件描述
|
Description string `field:"description"` // 文件描述
|
||||||
Filename string `field:"filename"` // 文件名
|
Filename string `field:"filename"` // 文件名
|
||||||
Size uint32 `field:"size"` // 文件尺寸
|
Size uint32 `field:"size"` // 文件尺寸
|
||||||
|
MimeType string `field:"mimeType"` // Mime类型
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
Order uint32 `field:"order"` // 排序
|
Order uint32 `field:"order"` // 排序
|
||||||
Type string `field:"type"` // 类型
|
Type string `field:"type"` // 类型
|
||||||
@@ -19,10 +21,12 @@ type File struct {
|
|||||||
type FileOperator struct {
|
type FileOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId interface{} // 管理员ID
|
||||||
|
Code interface{} // 代号
|
||||||
UserId interface{} // 用户ID
|
UserId interface{} // 用户ID
|
||||||
Description interface{} // 文件描述
|
Description interface{} // 文件描述
|
||||||
Filename interface{} // 文件名
|
Filename interface{} // 文件名
|
||||||
Size interface{} // 文件尺寸
|
Size interface{} // 文件尺寸
|
||||||
|
MimeType interface{} // Mime类型
|
||||||
CreatedAt interface{} // 创建时间
|
CreatedAt interface{} // 创建时间
|
||||||
Order interface{} // 排序
|
Order interface{} // 排序
|
||||||
Type interface{} // 类型
|
Type interface{} // 类型
|
||||||
|
|||||||
@@ -93,7 +93,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPAccessLogDAO() *HTTPAccessLogDAO {
|
func NewHTTPAccessLogDAO() *HTTPAccessLogDAO {
|
||||||
@@ -395,9 +394,14 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx,
|
|||||||
|
|
||||||
var locker = sync.Mutex{}
|
var locker = sync.Mutex{}
|
||||||
|
|
||||||
|
// 这里正则表达式中的括号不能轻易变更,因为后面有引用
|
||||||
|
// TODO 支持多个查询条件的组合,比如 status:200 proto:HTTP/1.1
|
||||||
var statusPrefixReg = regexp.MustCompile(`status:\s*(\d{3})\b`)
|
var statusPrefixReg = regexp.MustCompile(`status:\s*(\d{3})\b`)
|
||||||
var statusRangeReg = regexp.MustCompile(`status:\s*(\d{3})-(\d{3})\b`)
|
var statusRangeReg = regexp.MustCompile(`status:\s*(\d{3})-(\d{3})\b`)
|
||||||
var urlReg = regexp.MustCompile(`^(http|https)://`)
|
var urlReg = regexp.MustCompile(`^(http|https)://`)
|
||||||
|
var requestPathReg = regexp.MustCompile(`requestPath:(\S+)`)
|
||||||
|
var protoReg = regexp.MustCompile(`proto:(\S+)`)
|
||||||
|
var schemeReg = regexp.MustCompile(`scheme:(\S+)`)
|
||||||
|
|
||||||
var count = len(tableQueries)
|
var count = len(tableQueries)
|
||||||
var wg = &sync.WaitGroup{}
|
var wg = &sync.WaitGroup{}
|
||||||
@@ -509,13 +513,25 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx,
|
|||||||
isSpecialKeyword = true
|
isSpecialKeyword = true
|
||||||
var matches = statusRangeReg.FindStringSubmatch(keyword)
|
var matches = statusRangeReg.FindStringSubmatch(keyword)
|
||||||
query.Between("status", types.Int(matches[1]), types.Int(matches[2]))
|
query.Between("status", types.Int(matches[1]), types.Int(matches[2]))
|
||||||
|
|
||||||
// TODO 处理剩余的关键词
|
|
||||||
} else if statusPrefixReg.MatchString(keyword) { // status:200
|
} else if statusPrefixReg.MatchString(keyword) { // status:200
|
||||||
isSpecialKeyword = true
|
isSpecialKeyword = true
|
||||||
var matches = statusPrefixReg.FindStringSubmatch(keyword)
|
var matches = statusPrefixReg.FindStringSubmatch(keyword)
|
||||||
query.Attr("status", matches[1])
|
query.Attr("status", matches[1])
|
||||||
// TODO 处理剩余的关键词
|
} else if requestPathReg.MatchString(keyword) {
|
||||||
|
isSpecialKeyword = true
|
||||||
|
var matches = requestPathReg.FindStringSubmatch(keyword)
|
||||||
|
query.Where("JSON_EXTRACT(content, '$.requestPath')=:keyword").
|
||||||
|
Param("keyword", matches[1])
|
||||||
|
} else if protoReg.MatchString(keyword) {
|
||||||
|
isSpecialKeyword = true
|
||||||
|
var matches = protoReg.FindStringSubmatch(keyword)
|
||||||
|
query.Where("JSON_EXTRACT(content, '$.proto')=:keyword").
|
||||||
|
Param("keyword", strings.ToUpper(matches[1]))
|
||||||
|
} else if schemeReg.MatchString(keyword) {
|
||||||
|
isSpecialKeyword = true
|
||||||
|
var matches = schemeReg.FindStringSubmatch(keyword)
|
||||||
|
query.Where("JSON_EXTRACT(content, '$.scheme')=:keyword").
|
||||||
|
Param("keyword", strings.ToLower(matches[1]))
|
||||||
} else if urlReg.MatchString(keyword) { // https://xxx/yyy
|
} else if urlReg.MatchString(keyword) { // https://xxx/yyy
|
||||||
u, err := url.Parse(keyword)
|
u, err := url.Parse(keyword)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|||||||
@@ -87,6 +87,7 @@ func (this *HTTPAccessLogPolicyDAO) CountAllEnabledPolicies(tx *dbs.Tx) (int64,
|
|||||||
func (this *HTTPAccessLogPolicyDAO) ListEnabledPolicies(tx *dbs.Tx, offset int64, size int64) (result []*HTTPAccessLogPolicy, err error) {
|
func (this *HTTPAccessLogPolicyDAO) ListEnabledPolicies(tx *dbs.Tx, offset int64, size int64) (result []*HTTPAccessLogPolicy, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(HTTPAccessLogPolicyStateEnabled).
|
State(HTTPAccessLogPolicyStateEnabled).
|
||||||
|
Desc("isOn").
|
||||||
DescPk().
|
DescPk().
|
||||||
Offset(offset).
|
Offset(offset).
|
||||||
Limit(size).
|
Limit(size).
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ func (this *HTTPAuthPolicyDAO) FindEnabledHTTPAuthPolicy(tx *dbs.Tx, id int64) (
|
|||||||
|
|
||||||
// CreateHTTPAuthPolicy 创建策略
|
// CreateHTTPAuthPolicy 创建策略
|
||||||
func (this *HTTPAuthPolicyDAO) CreateHTTPAuthPolicy(tx *dbs.Tx, name string, methodType string, paramsJSON []byte) (int64, error) {
|
func (this *HTTPAuthPolicyDAO) CreateHTTPAuthPolicy(tx *dbs.Tx, name string, methodType string, paramsJSON []byte) (int64, error) {
|
||||||
op := NewHTTPAuthPolicyOperator()
|
var op = NewHTTPAuthPolicyOperator()
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.Type = methodType
|
op.Type = methodType
|
||||||
op.Params = paramsJSON
|
op.Params = paramsJSON
|
||||||
@@ -83,7 +83,7 @@ func (this *HTTPAuthPolicyDAO) UpdateHTTPAuthPolicy(tx *dbs.Tx, policyId int64,
|
|||||||
if policyId <= 0 {
|
if policyId <= 0 {
|
||||||
return errors.New("invalid policyId")
|
return errors.New("invalid policyId")
|
||||||
}
|
}
|
||||||
op := NewHTTPAuthPolicyOperator()
|
var op = NewHTTPAuthPolicyOperator()
|
||||||
op.Id = policyId
|
op.Id = policyId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.Params = paramsJSON
|
op.Params = paramsJSON
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ func (this *HTTPBrotliPolicyDAO) ComposeBrotliConfig(tx *dbs.Tx, policyId int64)
|
|||||||
|
|
||||||
// CreatePolicy 创建策略
|
// CreatePolicy 创建策略
|
||||||
func (this *HTTPBrotliPolicyDAO) CreatePolicy(tx *dbs.Tx, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) (int64, error) {
|
func (this *HTTPBrotliPolicyDAO) CreatePolicy(tx *dbs.Tx, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) (int64, error) {
|
||||||
op := NewHTTPBrotliPolicyOperator()
|
var op = NewHTTPBrotliPolicyOperator()
|
||||||
op.State = HTTPBrotliPolicyStateEnabled
|
op.State = HTTPBrotliPolicyStateEnabled
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.Level = level
|
op.Level = level
|
||||||
@@ -138,7 +138,7 @@ func (this *HTTPBrotliPolicyDAO) UpdatePolicy(tx *dbs.Tx, policyId int64, level
|
|||||||
if policyId <= 0 {
|
if policyId <= 0 {
|
||||||
return errors.New("invalid policyId")
|
return errors.New("invalid policyId")
|
||||||
}
|
}
|
||||||
op := NewHTTPBrotliPolicyOperator()
|
var op = NewHTTPBrotliPolicyOperator()
|
||||||
op.Id = policyId
|
op.Id = policyId
|
||||||
op.Level = level
|
op.Level = level
|
||||||
if len(minLengthJSON) > 0 {
|
if len(minLengthJSON) > 0 {
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ func (this *HTTPCachePolicyDAO) FindAllEnabledCachePolicies(tx *dbs.Tx) (result
|
|||||||
|
|
||||||
// CreateCachePolicy 创建缓存策略
|
// CreateCachePolicy 创建缓存策略
|
||||||
func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name string, description string, capacityJSON []byte, maxKeys int64, maxSizeJSON []byte, storageType string, storageOptionsJSON []byte, syncCompressionCache bool) (int64, error) {
|
func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name string, description string, capacityJSON []byte, maxKeys int64, maxSizeJSON []byte, storageType string, storageOptionsJSON []byte, syncCompressionCache bool) (int64, error) {
|
||||||
op := NewHTTPCachePolicyOperator()
|
var op = NewHTTPCachePolicyOperator()
|
||||||
op.State = HTTPCachePolicyStateEnabled
|
op.State = HTTPCachePolicyStateEnabled
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.Name = name
|
op.Name = name
|
||||||
@@ -209,7 +209,7 @@ func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, is
|
|||||||
return errors.New("invalid policyId")
|
return errors.New("invalid policyId")
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewHTTPCachePolicyOperator()
|
var op = NewHTTPCachePolicyOperator()
|
||||||
op.Id = policyId
|
op.Id = policyId
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.Name = name
|
op.Name = name
|
||||||
|
|||||||
254
internal/db/models/http_cache_task_dao.go
Normal file
254
internal/db/models/http_cache_task_dao.go
Normal file
@@ -0,0 +1,254 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
HTTPCacheTaskStateEnabled = 1 // 已启用
|
||||||
|
HTTPCacheTaskStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
type HTTPCacheTaskType = string
|
||||||
|
|
||||||
|
const (
|
||||||
|
HTTPCacheTaskTypePurge HTTPCacheTaskType = "purge"
|
||||||
|
HTTPCacheTaskTypeFetch HTTPCacheTaskType = "fetch"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HTTPCacheTaskDAO dbs.DAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReadyDone(func() {
|
||||||
|
// 清理数据任务
|
||||||
|
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||||
|
goman.New(func() {
|
||||||
|
for range ticker.C {
|
||||||
|
err := SharedHTTPCacheTaskDAO.Clean(nil, 30) // 只保留N天
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("HTTPCacheTaskDAO", "clean expired data failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHTTPCacheTaskDAO() *HTTPCacheTaskDAO {
|
||||||
|
return dbs.NewDAO(&HTTPCacheTaskDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeHTTPCacheTasks",
|
||||||
|
Model: new(HTTPCacheTask),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*HTTPCacheTaskDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedHTTPCacheTaskDAO *HTTPCacheTaskDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedHTTPCacheTaskDAO = NewHTTPCacheTaskDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableHTTPCacheTask 启用条目
|
||||||
|
func (this *HTTPCacheTaskDAO) EnableHTTPCacheTask(tx *dbs.Tx, taskId int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(taskId).
|
||||||
|
Set("state", HTTPCacheTaskStateEnabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableHTTPCacheTask 禁用条目
|
||||||
|
func (this *HTTPCacheTaskDAO) DisableHTTPCacheTask(tx *dbs.Tx, taskId int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(taskId).
|
||||||
|
Set("state", HTTPCacheTaskStateDisabled).
|
||||||
|
Update()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyChange(tx, taskId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledHTTPCacheTask 查找启用中的条目
|
||||||
|
func (this *HTTPCacheTaskDAO) FindEnabledHTTPCacheTask(tx *dbs.Tx, taskId int64) (*HTTPCacheTask, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Pk(taskId).
|
||||||
|
Attr("state", HTTPCacheTaskStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*HTTPCacheTask), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateTask 创建任务
|
||||||
|
func (this *HTTPCacheTaskDAO) CreateTask(tx *dbs.Tx, userId int64, taskType HTTPCacheTaskType, keyType string, description string) (int64, error) {
|
||||||
|
var op = NewHTTPCacheTaskOperator()
|
||||||
|
op.UserId = userId
|
||||||
|
op.Type = taskType
|
||||||
|
op.KeyType = keyType
|
||||||
|
op.IsOk = false
|
||||||
|
op.IsDone = false
|
||||||
|
op.IsReady = false
|
||||||
|
op.Description = description
|
||||||
|
op.Day = timeutil.Format("Ymd")
|
||||||
|
op.State = HTTPCacheTaskStateEnabled
|
||||||
|
taskId, err := this.SaveInt64(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = this.NotifyChange(tx, taskId)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return taskId, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResetTask 重置服务状态
|
||||||
|
func (this *HTTPCacheTaskDAO) ResetTask(tx *dbs.Tx, taskId int64) error {
|
||||||
|
if taskId <= 0 {
|
||||||
|
return errors.New("invalid 'taskId'")
|
||||||
|
}
|
||||||
|
|
||||||
|
var op = NewHTTPCacheTaskOperator()
|
||||||
|
op.Id = taskId
|
||||||
|
op.IsOk = false
|
||||||
|
op.IsDone = false
|
||||||
|
op.DoneAt = 0
|
||||||
|
return this.Save(tx, op)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateTaskReady 设置任务为已准备
|
||||||
|
func (this *HTTPCacheTaskDAO) UpdateTaskReady(tx *dbs.Tx, taskId int64) error {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(taskId).
|
||||||
|
Set("isReady", true).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountTasks 查询所有任务数量
|
||||||
|
func (this *HTTPCacheTaskDAO) CountTasks(tx *dbs.Tx, userId int64) (int64, error) {
|
||||||
|
var query = this.Query(tx).
|
||||||
|
State(HTTPCacheTaskStateEnabled).
|
||||||
|
Attr("isReady", true)
|
||||||
|
if userId > 0 {
|
||||||
|
query.Attr("userId", userId)
|
||||||
|
}
|
||||||
|
return query.Count()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountDoingTasks 查询正在执行的任务数量
|
||||||
|
func (this *HTTPCacheTaskDAO) CountDoingTasks(tx *dbs.Tx, userId int64) (int64, error) {
|
||||||
|
var query = this.Query(tx).
|
||||||
|
State(HTTPCacheTaskStateEnabled).
|
||||||
|
Attr("isReady", true).
|
||||||
|
Attr("isDone", false)
|
||||||
|
if userId > 0 {
|
||||||
|
query.Attr("userId", userId)
|
||||||
|
}
|
||||||
|
|
||||||
|
return query.Count()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListTasks 列出单页任务
|
||||||
|
func (this *HTTPCacheTaskDAO) ListTasks(tx *dbs.Tx, userId int64, offset int64, size int64) (result []*HTTPCacheTask, err error) {
|
||||||
|
var query = this.Query(tx).
|
||||||
|
State(HTTPCacheTaskStateEnabled).
|
||||||
|
Attr("isReady", true)
|
||||||
|
if userId > 0 {
|
||||||
|
query.Attr("userId", userId)
|
||||||
|
}
|
||||||
|
_, err = query.
|
||||||
|
Offset(offset).
|
||||||
|
Limit(size).
|
||||||
|
Slice(&result).
|
||||||
|
DescPk().
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListDoingTasks 列出需要执行的任务
|
||||||
|
func (this *HTTPCacheTaskDAO) ListDoingTasks(tx *dbs.Tx, size int64) (result []*HTTPCacheTask, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
State(HTTPCacheTaskStateEnabled).
|
||||||
|
Attr("isDone", false).
|
||||||
|
Attr("isReady", true).
|
||||||
|
Limit(size).
|
||||||
|
AscPk(). // 按照先创建先执行的原则
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateTaskStatus 标记任务已完成
|
||||||
|
func (this *HTTPCacheTaskDAO) UpdateTaskStatus(tx *dbs.Tx, taskId int64, isDone bool, isOk bool) error {
|
||||||
|
if taskId <= 0 {
|
||||||
|
return errors.New("invalid taskId '" + types.String(taskId) + "'")
|
||||||
|
}
|
||||||
|
var op = NewHTTPCacheTaskOperator()
|
||||||
|
op.Id = taskId
|
||||||
|
op.IsDone = isDone
|
||||||
|
op.IsOk = isOk
|
||||||
|
|
||||||
|
if isDone {
|
||||||
|
op.DoneAt = time.Now().Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.Save(tx, op)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckUserTask 检查用户任务
|
||||||
|
func (this *HTTPCacheTaskDAO) CheckUserTask(tx *dbs.Tx, userId int64, taskId int64) error {
|
||||||
|
b, err := this.Query(tx).
|
||||||
|
Pk(taskId).
|
||||||
|
Attr("userId", userId).
|
||||||
|
State(HTTPCacheTaskStateEnabled).
|
||||||
|
Exist()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !b {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean 清理以往的任务
|
||||||
|
func (this *HTTPCacheTaskDAO) Clean(tx *dbs.Tx, days int) error {
|
||||||
|
if days <= 0 {
|
||||||
|
days = 30
|
||||||
|
}
|
||||||
|
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -days))
|
||||||
|
|
||||||
|
// 删除Key
|
||||||
|
err := SharedHTTPCacheTaskKeyDAO.Clean(tx, days)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除任务
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Lte("day", day).
|
||||||
|
Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotifyChange 发送通知
|
||||||
|
func (this *HTTPCacheTaskDAO) NotifyChange(tx *dbs.Tx, taskId int64) error {
|
||||||
|
// TODO
|
||||||
|
return nil
|
||||||
|
}
|
||||||
19
internal/db/models/http_cache_task_dao_test.go
Normal file
19
internal/db/models/http_cache_task_dao_test.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package models_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHTTPCacheTaskDAO_Clean(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
err := models.SharedHTTPCacheTaskDAO.Clean(nil, 30)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
218
internal/db/models/http_cache_task_key_dao.go
Normal file
218
internal/db/models/http_cache_task_key_dao.go
Normal file
@@ -0,0 +1,218 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HTTPCacheTaskKeyDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewHTTPCacheTaskKeyDAO() *HTTPCacheTaskKeyDAO {
|
||||||
|
return dbs.NewDAO(&HTTPCacheTaskKeyDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeHTTPCacheTaskKeys",
|
||||||
|
Model: new(HTTPCacheTaskKey),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*HTTPCacheTaskKeyDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedHTTPCacheTaskKeyDAO *HTTPCacheTaskKeyDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedHTTPCacheTaskKeyDAO = NewHTTPCacheTaskKeyDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateKey 创建Key
|
||||||
|
// 参数:
|
||||||
|
// - clusterId 集群ID
|
||||||
|
// - nodeMapJSON 集群下节点映射,格式类似于 `{ "节点1":true, ... }`
|
||||||
|
func (this *HTTPCacheTaskKeyDAO) CreateKey(tx *dbs.Tx, taskId int64, key string, taskType HTTPCacheTaskType, keyType string, clusterId int64) (int64, error) {
|
||||||
|
var op = NewHTTPCacheTaskKeyOperator()
|
||||||
|
op.TaskId = taskId
|
||||||
|
op.Key = key
|
||||||
|
op.Type = taskType
|
||||||
|
op.KeyType = keyType
|
||||||
|
op.ClusterId = clusterId
|
||||||
|
|
||||||
|
op.Nodes = "{}"
|
||||||
|
op.Errors = "{}"
|
||||||
|
|
||||||
|
return this.SaveInt64(tx, op)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateKeyStatus 修改Key状态
|
||||||
|
func (this *HTTPCacheTaskKeyDAO) UpdateKeyStatus(tx *dbs.Tx, keyId int64, nodeId int64, errString string, nodesJSON []byte) error {
|
||||||
|
if keyId <= 0 {
|
||||||
|
return errors.New("invalid 'keyId'")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(nodesJSON) == 0 {
|
||||||
|
nodesJSON = []byte("{}")
|
||||||
|
}
|
||||||
|
|
||||||
|
taskId, err := this.Query(tx).
|
||||||
|
Pk(keyId).
|
||||||
|
Result("taskId").
|
||||||
|
FindInt64Col(0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var jsonPath = "$.\"" + types.String(nodeId) + "\""
|
||||||
|
|
||||||
|
var query = this.Query(tx).
|
||||||
|
Pk(keyId).
|
||||||
|
Set("nodes", dbs.SQL("JSON_SET(nodes, :jsonPath1, true)")).
|
||||||
|
Param("jsonPath1", jsonPath)
|
||||||
|
|
||||||
|
if len(errString) > 0 {
|
||||||
|
query.Set("errors", dbs.SQL("JSON_SET(errors, :jsonPath2, :jsonValue2)")).
|
||||||
|
Param("jsonPath2", jsonPath).
|
||||||
|
Param("jsonValue2", errString)
|
||||||
|
} else {
|
||||||
|
query.Set("errors", dbs.SQL("JSON_REMOVE(errors, :jsonPath2)")).
|
||||||
|
Param("jsonPath2", jsonPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = query.
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否已完成
|
||||||
|
isDone, err := this.Query(tx).
|
||||||
|
Pk(keyId).
|
||||||
|
Where("JSON_CONTAINS(nodes, :nodesJSON)").
|
||||||
|
Param("nodesJSON", nodesJSON).
|
||||||
|
Exist()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if isDone {
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(keyId).
|
||||||
|
Set("isDone", isDone).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查任务是否已经完成
|
||||||
|
taskIsNotDone, err := this.Query(tx).
|
||||||
|
Attr("taskId", taskId).
|
||||||
|
Attr("isDone", false).
|
||||||
|
Exist()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var taskIsDone = !taskIsNotDone
|
||||||
|
var hasErrors = true
|
||||||
|
if taskIsDone {
|
||||||
|
// 已经完成,是否有错误
|
||||||
|
hasErrors, err = this.Query(tx).
|
||||||
|
Attr("taskId", taskId).
|
||||||
|
Where("JSON_LENGTH(errors)>0").
|
||||||
|
Exist()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = SharedHTTPCacheTaskDAO.UpdateTaskStatus(tx, taskId, taskIsDone, !hasErrors)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAllTaskKeys 查询某个任务下的所有Key
|
||||||
|
func (this *HTTPCacheTaskKeyDAO) FindAllTaskKeys(tx *dbs.Tx, taskId int64) (result []*HTTPCacheTaskKey, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Attr("taskId", taskId).
|
||||||
|
AscPk().
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindDoingTaskKeys 查询要执行的任务
|
||||||
|
func (this *HTTPCacheTaskKeyDAO) FindDoingTaskKeys(tx *dbs.Tx, nodeId int64, size int64) (result []*HTTPCacheTaskKey, err error) {
|
||||||
|
// 集群ID
|
||||||
|
clusterIds, err := SharedNodeDAO.FindEnabledAndOnNodeClusterIds(tx, nodeId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(clusterIds) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Attr("clusterId", clusterIds).
|
||||||
|
Attr("isDone", false).
|
||||||
|
Where("NOT JSON_CONTAINS_PATH(nodes, 'one', :jsonPath1)").
|
||||||
|
Param("jsonPath1", "$.\""+types.String(nodeId)+"\"").
|
||||||
|
Where("taskId IN (SELECT id FROM " + SharedHTTPCacheTaskDAO.Table + " WHERE state=1 AND isReady=1 AND isDone=0)").
|
||||||
|
Limit(size).
|
||||||
|
AscPk().
|
||||||
|
Reuse(false).
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResetCacheKeysWithTaskId 重置任务下的Key状态
|
||||||
|
func (this *HTTPCacheTaskKeyDAO) ResetCacheKeysWithTaskId(tx *dbs.Tx, taskId int64) error {
|
||||||
|
return this.Query(tx).
|
||||||
|
Attr("taskId", taskId).
|
||||||
|
Set("isDone", false).
|
||||||
|
Set("nodes", "{}").
|
||||||
|
Set("errors", "{}").
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountUserTasksInDay 读取某个用户当前数量
|
||||||
|
// day YYYYMMDD
|
||||||
|
func (this *HTTPCacheTaskKeyDAO) CountUserTasksInDay(tx *dbs.Tx, userId int64, day string, taskType HTTPCacheTaskType) (int64, error) {
|
||||||
|
if userId <= 0 {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 这里需要包含已删除的
|
||||||
|
return this.Query(tx).
|
||||||
|
Where("taskId IN (SELECT id FROM "+SharedHTTPCacheTaskDAO.Table+" WHERE userId=:userId AND day=:day AND type=:type)").
|
||||||
|
Param("userId", userId).
|
||||||
|
Param("day", day).
|
||||||
|
Param("type", taskType).
|
||||||
|
Count()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean 清理以往的任务
|
||||||
|
func (this *HTTPCacheTaskKeyDAO) Clean(tx *dbs.Tx, days int) error {
|
||||||
|
if days <= 0 {
|
||||||
|
days = 30
|
||||||
|
}
|
||||||
|
|
||||||
|
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -days))
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Where("taskId IN (SELECT id FROM "+SharedHTTPCacheTaskDAO.Table+" WHERE day<=:day)").
|
||||||
|
Param("day", day).
|
||||||
|
Delete()
|
||||||
|
return err
|
||||||
|
}
|
||||||
54
internal/db/models/http_cache_task_key_dao_test.go
Normal file
54
internal/db/models/http_cache_task_key_dao_test.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package models_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHTTPCacheTaskKeyDAO_CreateKey(t *testing.T) {
|
||||||
|
var dao = models.NewHTTPCacheTaskKeyDAO()
|
||||||
|
var tx *dbs.Tx
|
||||||
|
_, err := dao.CreateKey(tx, 1, "a", "purge", "key", 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHTTPCacheTaskKeyDAO_UpdateKeyStatus(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var dao = models.NewHTTPCacheTaskKeyDAO()
|
||||||
|
var tx *dbs.Tx
|
||||||
|
var errString = "" // "this is error"
|
||||||
|
err := dao.UpdateKeyStatus(tx, 3, 1, errString, []byte(`{"1":true}`))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHTTPCacheTaskKeyDAO_CountUserTasksInDay(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var dao = models.NewHTTPCacheTaskKeyDAO()
|
||||||
|
var tx *dbs.Tx
|
||||||
|
{
|
||||||
|
count, err := dao.CountUserTasksInDay(tx, 1, timeutil.Format("Ymd"), models.HTTPCacheTaskTypePurge)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("count:", count)
|
||||||
|
}
|
||||||
|
{
|
||||||
|
count, err := dao.CountUserTasksInDay(tx, 1, timeutil.Format("Ymd"), models.HTTPCacheTaskTypeFetch)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("count:", count)
|
||||||
|
}
|
||||||
|
}
|
||||||
32
internal/db/models/http_cache_task_key_model.go
Normal file
32
internal/db/models/http_cache_task_key_model.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
|
// HTTPCacheTaskKey 缓存任务Key
|
||||||
|
type HTTPCacheTaskKey struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
TaskId uint64 `field:"taskId"` // 任务ID
|
||||||
|
Key string `field:"key"` // Key
|
||||||
|
KeyType string `field:"keyType"` // Key类型:key|prefix
|
||||||
|
Type string `field:"type"` // 操作类型
|
||||||
|
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||||
|
Nodes dbs.JSON `field:"nodes"` // 节点
|
||||||
|
Errors dbs.JSON `field:"errors"` // 错误信息
|
||||||
|
IsDone bool `field:"isDone"` // 是否已完成
|
||||||
|
}
|
||||||
|
|
||||||
|
type HTTPCacheTaskKeyOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
TaskId interface{} // 任务ID
|
||||||
|
Key interface{} // Key
|
||||||
|
KeyType interface{} // Key类型:key|prefix
|
||||||
|
Type interface{} // 操作类型
|
||||||
|
ClusterId interface{} // 集群ID
|
||||||
|
Nodes interface{} // 节点
|
||||||
|
Errors interface{} // 错误信息
|
||||||
|
IsDone interface{} // 是否已完成
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHTTPCacheTaskKeyOperator() *HTTPCacheTaskKeyOperator {
|
||||||
|
return &HTTPCacheTaskKeyOperator{}
|
||||||
|
}
|
||||||
20
internal/db/models/http_cache_task_key_model_ext.go
Normal file
20
internal/db/models/http_cache_task_key_model_ext.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
|
||||||
|
// DecodeNodes 解析已完成节点信息
|
||||||
|
func (this *HTTPCacheTaskKey) DecodeNodes() map[string]bool {
|
||||||
|
var result = map[string]bool{}
|
||||||
|
var nodesJSON = this.Nodes
|
||||||
|
if IsNull(nodesJSON) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
err := json.Unmarshal(nodesJSON, &result)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
36
internal/db/models/http_cache_task_model.go
Normal file
36
internal/db/models/http_cache_task_model.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
// HTTPCacheTask 缓存相关任务
|
||||||
|
type HTTPCacheTask struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
|
Type string `field:"type"` // 任务类型:purge|fetch
|
||||||
|
KeyType string `field:"keyType"` // Key类型
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
|
DoneAt uint64 `field:"doneAt"` // 完成时间
|
||||||
|
Day string `field:"day"` // 创建日期YYYYMMDD
|
||||||
|
IsDone bool `field:"isDone"` // 是否已完成
|
||||||
|
IsOk bool `field:"isOk"` // 是否完全成功
|
||||||
|
IsReady bool `field:"isReady"` // 是否已准备好
|
||||||
|
Description string `field:"description"` // 描述
|
||||||
|
}
|
||||||
|
|
||||||
|
type HTTPCacheTaskOperator struct {
|
||||||
|
Id any // ID
|
||||||
|
UserId any // 用户ID
|
||||||
|
Type any // 任务类型:purge|fetch
|
||||||
|
KeyType any // Key类型
|
||||||
|
State any // 状态
|
||||||
|
CreatedAt any // 创建时间
|
||||||
|
DoneAt any // 完成时间
|
||||||
|
Day any // 创建日期YYYYMMDD
|
||||||
|
IsDone any // 是否已完成
|
||||||
|
IsOk any // 是否完全成功
|
||||||
|
IsReady any // 是否已准备好
|
||||||
|
Description any // 描述
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHTTPCacheTaskOperator() *HTTPCacheTaskOperator {
|
||||||
|
return &HTTPCacheTaskOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/http_cache_task_model_ext.go
Normal file
1
internal/db/models/http_cache_task_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package models
|
||||||
@@ -113,7 +113,7 @@ func (this *HTTPDeflatePolicyDAO) ComposeDeflateConfig(tx *dbs.Tx, policyId int6
|
|||||||
|
|
||||||
// CreatePolicy 创建策略
|
// CreatePolicy 创建策略
|
||||||
func (this *HTTPDeflatePolicyDAO) CreatePolicy(tx *dbs.Tx, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) (int64, error) {
|
func (this *HTTPDeflatePolicyDAO) CreatePolicy(tx *dbs.Tx, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) (int64, error) {
|
||||||
op := NewHTTPDeflatePolicyOperator()
|
var op = NewHTTPDeflatePolicyOperator()
|
||||||
op.State = HTTPDeflatePolicyStateEnabled
|
op.State = HTTPDeflatePolicyStateEnabled
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.Level = level
|
op.Level = level
|
||||||
@@ -138,7 +138,7 @@ func (this *HTTPDeflatePolicyDAO) UpdatePolicy(tx *dbs.Tx, policyId int64, level
|
|||||||
if policyId <= 0 {
|
if policyId <= 0 {
|
||||||
return errors.New("invalid policyId")
|
return errors.New("invalid policyId")
|
||||||
}
|
}
|
||||||
op := NewHTTPDeflatePolicyOperator()
|
var op = NewHTTPDeflatePolicyOperator()
|
||||||
op.Id = policyId
|
op.Id = policyId
|
||||||
op.Level = level
|
op.Level = level
|
||||||
if len(minLengthJSON) > 0 {
|
if len(minLengthJSON) > 0 {
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ func (this *HTTPFastcgiDAO) ComposeFastcgiConfig(tx *dbs.Tx, fastcgiId int64) (*
|
|||||||
|
|
||||||
// CreateFastcgi 创建Fastcgi
|
// CreateFastcgi 创建Fastcgi
|
||||||
func (this *HTTPFastcgiDAO) CreateFastcgi(tx *dbs.Tx, adminId int64, userId int64, isOn bool, address string, paramsJSON []byte, readTimeoutJSON []byte, connTimeoutJSON []byte, poolSize int32, pathInfoPattern string) (int64, error) {
|
func (this *HTTPFastcgiDAO) CreateFastcgi(tx *dbs.Tx, adminId int64, userId int64, isOn bool, address string, paramsJSON []byte, readTimeoutJSON []byte, connTimeoutJSON []byte, poolSize int32, pathInfoPattern string) (int64, error) {
|
||||||
op := NewHTTPFastcgiOperator()
|
var op = NewHTTPFastcgiOperator()
|
||||||
op.AdminId = adminId
|
op.AdminId = adminId
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
@@ -147,7 +147,7 @@ func (this *HTTPFastcgiDAO) UpdateFastcgi(tx *dbs.Tx, fastcgiId int64, isOn bool
|
|||||||
if fastcgiId <= 0 {
|
if fastcgiId <= 0 {
|
||||||
return errors.New("invalid 'fastcgiId'")
|
return errors.New("invalid 'fastcgiId'")
|
||||||
}
|
}
|
||||||
op := NewHTTPFastcgiOperator()
|
var op = NewHTTPFastcgiOperator()
|
||||||
op.Id = fastcgiId
|
op.Id = fastcgiId
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.Address = address
|
op.Address = address
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ func (this *HTTPFirewallPolicyDAO) FindAllEnabledFirewallPolicies(tx *dbs.Tx) (r
|
|||||||
|
|
||||||
// CreateFirewallPolicy 创建策略
|
// CreateFirewallPolicy 创建策略
|
||||||
func (this *HTTPFirewallPolicyDAO) CreateFirewallPolicy(tx *dbs.Tx, userId int64, serverGroupId int64, serverId int64, isOn bool, name string, description string, inboundJSON []byte, outboundJSON []byte) (int64, error) {
|
func (this *HTTPFirewallPolicyDAO) CreateFirewallPolicy(tx *dbs.Tx, userId int64, serverGroupId int64, serverId int64, isOn bool, name string, description string, inboundJSON []byte, outboundJSON []byte) (int64, error) {
|
||||||
op := NewHTTPFirewallPolicyOperator()
|
var op = NewHTTPFirewallPolicyOperator()
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
op.GroupId = serverGroupId
|
op.GroupId = serverGroupId
|
||||||
op.ServerId = serverId
|
op.ServerId = serverId
|
||||||
@@ -131,14 +131,31 @@ func (this *HTTPFirewallPolicyDAO) CreateFirewallPolicy(tx *dbs.Tx, userId int64
|
|||||||
if len(outboundJSON) > 0 {
|
if len(outboundJSON) > 0 {
|
||||||
op.Outbound = outboundJSON
|
op.Outbound = outboundJSON
|
||||||
}
|
}
|
||||||
op.UseLocalFirewall = true
|
|
||||||
|
|
||||||
{
|
if userId <= 0 && serverGroupId <=0 && serverId <= 0 {
|
||||||
synFloodJSON, err := json.Marshal(firewallconfigs.DefaultSYNFloodConfig())
|
// synFlood
|
||||||
|
var synFloodConfig = firewallconfigs.DefaultSYNFloodConfig()
|
||||||
|
synFloodJSON, err := json.Marshal(synFloodConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
op.SynFlood = synFloodJSON
|
op.SynFlood = synFloodJSON
|
||||||
|
|
||||||
|
// block options
|
||||||
|
var blockOptions = firewallconfigs.DefaultHTTPFirewallBlockAction()
|
||||||
|
blockOptionsJSON, err := json.Marshal(blockOptions)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.BlockOptions = blockOptionsJSON
|
||||||
|
|
||||||
|
// captcha options
|
||||||
|
var captchaOptions = firewallconfigs.DefaultHTTPFirewallCaptchaAction()
|
||||||
|
captchaOptionsJSON, err := json.Marshal(captchaOptions)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.CaptchaOptions = captchaOptionsJSON
|
||||||
}
|
}
|
||||||
|
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -160,8 +177,8 @@ func (this *HTTPFirewallPolicyDAO) CreateDefaultFirewallPolicy(tx *dbs.Tx, name
|
|||||||
groupCodes = append(groupCodes, group.Code)
|
groupCodes = append(groupCodes, group.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
inboundConfig := &firewallconfigs.HTTPFirewallInboundConfig{IsOn: true}
|
var inboundConfig = &firewallconfigs.HTTPFirewallInboundConfig{IsOn: true}
|
||||||
outboundConfig := &firewallconfigs.HTTPFirewallOutboundConfig{IsOn: true}
|
var outboundConfig = &firewallconfigs.HTTPFirewallOutboundConfig{IsOn: true}
|
||||||
if templatePolicy.Inbound != nil {
|
if templatePolicy.Inbound != nil {
|
||||||
for _, group := range templatePolicy.Inbound.Groups {
|
for _, group := range templatePolicy.Inbound.Groups {
|
||||||
isOn := lists.ContainsString(groupCodes, group.Code)
|
isOn := lists.ContainsString(groupCodes, group.Code)
|
||||||
@@ -207,6 +224,7 @@ func (this *HTTPFirewallPolicyDAO) CreateDefaultFirewallPolicy(tx *dbs.Tx, name
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return policyId, nil
|
return policyId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +233,7 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyInboundAndOutbound(tx *db
|
|||||||
if policyId <= 0 {
|
if policyId <= 0 {
|
||||||
return errors.New("invalid policyId")
|
return errors.New("invalid policyId")
|
||||||
}
|
}
|
||||||
op := NewHTTPFirewallPolicyOperator()
|
var op = NewHTTPFirewallPolicyOperator()
|
||||||
op.Id = policyId
|
op.Id = policyId
|
||||||
if len(inboundJSON) > 0 {
|
if len(inboundJSON) > 0 {
|
||||||
op.Inbound = inboundJSON
|
op.Inbound = inboundJSON
|
||||||
@@ -244,7 +262,7 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyInbound(tx *dbs.Tx, polic
|
|||||||
if policyId <= 0 {
|
if policyId <= 0 {
|
||||||
return errors.New("invalid policyId")
|
return errors.New("invalid policyId")
|
||||||
}
|
}
|
||||||
op := NewHTTPFirewallPolicyOperator()
|
var op = NewHTTPFirewallPolicyOperator()
|
||||||
op.Id = policyId
|
op.Id = policyId
|
||||||
if len(inboundJSON) > 0 {
|
if len(inboundJSON) > 0 {
|
||||||
op.Inbound = inboundJSON
|
op.Inbound = inboundJSON
|
||||||
@@ -268,6 +286,7 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicy(tx *dbs.Tx,
|
|||||||
inboundJSON []byte,
|
inboundJSON []byte,
|
||||||
outboundJSON []byte,
|
outboundJSON []byte,
|
||||||
blockOptionsJSON []byte,
|
blockOptionsJSON []byte,
|
||||||
|
captchaOptionsJSON []byte,
|
||||||
mode firewallconfigs.FirewallMode,
|
mode firewallconfigs.FirewallMode,
|
||||||
useLocalFirewall bool,
|
useLocalFirewall bool,
|
||||||
synFloodConfig *firewallconfigs.SYNFloodConfig,
|
synFloodConfig *firewallconfigs.SYNFloodConfig,
|
||||||
@@ -275,7 +294,7 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicy(tx *dbs.Tx,
|
|||||||
if policyId <= 0 {
|
if policyId <= 0 {
|
||||||
return errors.New("invalid policyId")
|
return errors.New("invalid policyId")
|
||||||
}
|
}
|
||||||
op := NewHTTPFirewallPolicyOperator()
|
var op = NewHTTPFirewallPolicyOperator()
|
||||||
op.Id = policyId
|
op.Id = policyId
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.Name = name
|
op.Name = name
|
||||||
@@ -291,9 +310,12 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicy(tx *dbs.Tx,
|
|||||||
} else {
|
} else {
|
||||||
op.Outbound = "null"
|
op.Outbound = "null"
|
||||||
}
|
}
|
||||||
if len(blockOptionsJSON) > 0 {
|
if IsNotNull(blockOptionsJSON) {
|
||||||
op.BlockOptions = blockOptionsJSON
|
op.BlockOptions = blockOptionsJSON
|
||||||
}
|
}
|
||||||
|
if IsNotNull(captchaOptionsJSON) {
|
||||||
|
op.CaptchaOptions = captchaOptionsJSON
|
||||||
|
}
|
||||||
|
|
||||||
if synFloodConfig != nil {
|
if synFloodConfig != nil {
|
||||||
synFloodConfigJSON, err := json.Marshal(synFloodConfig)
|
synFloodConfigJSON, err := json.Marshal(synFloodConfig)
|
||||||
@@ -456,7 +478,7 @@ func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId in
|
|||||||
|
|
||||||
// Block动作配置
|
// Block动作配置
|
||||||
if IsNotNull(policy.BlockOptions) {
|
if IsNotNull(policy.BlockOptions) {
|
||||||
blockAction := &firewallconfigs.HTTPFirewallBlockAction{}
|
var blockAction = &firewallconfigs.HTTPFirewallBlockAction{}
|
||||||
err = json.Unmarshal(policy.BlockOptions, blockAction)
|
err = json.Unmarshal(policy.BlockOptions, blockAction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return config, err
|
return config, err
|
||||||
@@ -464,6 +486,16 @@ func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId in
|
|||||||
config.BlockOptions = blockAction
|
config.BlockOptions = blockAction
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Captcha动作配置
|
||||||
|
if IsNotNull(policy.CaptchaOptions) {
|
||||||
|
var captchaAction = &firewallconfigs.HTTPFirewallCaptchaAction{}
|
||||||
|
err = json.Unmarshal(policy.CaptchaOptions, captchaAction)
|
||||||
|
if err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
config.CaptchaOptions = captchaAction
|
||||||
|
}
|
||||||
|
|
||||||
// syn flood
|
// syn flood
|
||||||
if IsNotNull(policy.SynFlood) {
|
if IsNotNull(policy.SynFlood) {
|
||||||
var synFloodConfig = &firewallconfigs.SYNFloodConfig{}
|
var synFloodConfig = &firewallconfigs.SYNFloodConfig{}
|
||||||
@@ -526,6 +558,7 @@ func (this *HTTPFirewallPolicyDAO) CheckUserFirewallPolicy(tx *dbs.Tx, userId in
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledFirewallPolicyIdsWithIPListId 查找包含某个IPList的所有策略
|
// FindEnabledFirewallPolicyIdsWithIPListId 查找包含某个IPList的所有策略
|
||||||
|
// TODO 改成通过 serverId 查询
|
||||||
func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyIdsWithIPListId(tx *dbs.Tx, ipListId int64) ([]int64, error) {
|
func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyIdsWithIPListId(tx *dbs.Tx, ipListId int64) ([]int64, error) {
|
||||||
ones, err := this.Query(tx).
|
ones, err := this.Query(tx).
|
||||||
ResultPk().
|
ResultPk().
|
||||||
@@ -544,6 +577,7 @@ func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyIdsWithIPListId(tx *
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledFirewallPolicyWithIPListId 查找使用某个IPList的策略
|
// FindEnabledFirewallPolicyWithIPListId 查找使用某个IPList的策略
|
||||||
|
// TODO 改成通过 serverId 查询
|
||||||
func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyWithIPListId(tx *dbs.Tx, ipListId int64) (*HTTPFirewallPolicy, error) {
|
func (this *HTTPFirewallPolicyDAO) FindEnabledFirewallPolicyWithIPListId(tx *dbs.Tx, ipListId int64) (*HTTPFirewallPolicy, error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
State(HTTPFirewallPolicyStateEnabled).
|
State(HTTPFirewallPolicyStateEnabled).
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ type HTTPFirewallPolicy struct {
|
|||||||
Inbound dbs.JSON `field:"inbound"` // 入站规则
|
Inbound dbs.JSON `field:"inbound"` // 入站规则
|
||||||
Outbound dbs.JSON `field:"outbound"` // 出站规则
|
Outbound dbs.JSON `field:"outbound"` // 出站规则
|
||||||
BlockOptions dbs.JSON `field:"blockOptions"` // BLOCK选项
|
BlockOptions dbs.JSON `field:"blockOptions"` // BLOCK选项
|
||||||
|
CaptchaOptions dbs.JSON `field:"captchaOptions"` // 验证码选项
|
||||||
Mode string `field:"mode"` // 模式
|
Mode string `field:"mode"` // 模式
|
||||||
UseLocalFirewall uint8 `field:"useLocalFirewall"` // 是否自动使用本地防火墙
|
UseLocalFirewall uint8 `field:"useLocalFirewall"` // 是否自动使用本地防火墙
|
||||||
SynFlood dbs.JSON `field:"synFlood"` // SynFlood防御设置
|
SynFlood dbs.JSON `field:"synFlood"` // SynFlood防御设置
|
||||||
@@ -39,6 +40,7 @@ type HTTPFirewallPolicyOperator struct {
|
|||||||
Inbound interface{} // 入站规则
|
Inbound interface{} // 入站规则
|
||||||
Outbound interface{} // 出站规则
|
Outbound interface{} // 出站规则
|
||||||
BlockOptions interface{} // BLOCK选项
|
BlockOptions interface{} // BLOCK选项
|
||||||
|
CaptchaOptions interface{} // 验证码选项
|
||||||
Mode interface{} // 模式
|
Mode interface{} // 模式
|
||||||
UseLocalFirewall interface{} // 是否自动使用本地防火墙
|
UseLocalFirewall interface{} // 是否自动使用本地防火墙
|
||||||
SynFlood interface{} // SynFlood防御设置
|
SynFlood interface{} // SynFlood防御设置
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ func (this *HTTPFirewallRuleDAO) ComposeFirewallRule(tx *dbs.Tx, ruleId int64) (
|
|||||||
|
|
||||||
// CreateOrUpdateRuleFromConfig 从配置中配置规则
|
// CreateOrUpdateRuleFromConfig 从配置中配置规则
|
||||||
func (this *HTTPFirewallRuleDAO) CreateOrUpdateRuleFromConfig(tx *dbs.Tx, ruleConfig *firewallconfigs.HTTPFirewallRule) (int64, error) {
|
func (this *HTTPFirewallRuleDAO) CreateOrUpdateRuleFromConfig(tx *dbs.Tx, ruleConfig *firewallconfigs.HTTPFirewallRule) (int64, error) {
|
||||||
op := NewHTTPFirewallRuleOperator()
|
var op = NewHTTPFirewallRuleOperator()
|
||||||
op.Id = ruleConfig.Id
|
op.Id = ruleConfig.Id
|
||||||
op.State = HTTPFirewallRuleStateEnabled
|
op.State = HTTPFirewallRuleStateEnabled
|
||||||
op.IsOn = ruleConfig.IsOn
|
op.IsOn = ruleConfig.IsOn
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ func (this *HTTPFirewallRuleGroupDAO) ComposeFirewallRuleGroup(tx *dbs.Tx, group
|
|||||||
|
|
||||||
// CreateGroupFromConfig 从配置中创建分组
|
// CreateGroupFromConfig 从配置中创建分组
|
||||||
func (this *HTTPFirewallRuleGroupDAO) CreateGroupFromConfig(tx *dbs.Tx, groupConfig *firewallconfigs.HTTPFirewallRuleGroup) (int64, error) {
|
func (this *HTTPFirewallRuleGroupDAO) CreateGroupFromConfig(tx *dbs.Tx, groupConfig *firewallconfigs.HTTPFirewallRuleGroup) (int64, error) {
|
||||||
op := NewHTTPFirewallRuleGroupOperator()
|
var op = NewHTTPFirewallRuleGroupOperator()
|
||||||
op.IsOn = groupConfig.IsOn
|
op.IsOn = groupConfig.IsOn
|
||||||
op.Name = groupConfig.Name
|
op.Name = groupConfig.Name
|
||||||
op.Description = groupConfig.Description
|
op.Description = groupConfig.Description
|
||||||
@@ -166,7 +166,7 @@ func (this *HTTPFirewallRuleGroupDAO) UpdateGroupIsOn(tx *dbs.Tx, groupId int64,
|
|||||||
|
|
||||||
// CreateGroup 创建分组
|
// CreateGroup 创建分组
|
||||||
func (this *HTTPFirewallRuleGroupDAO) CreateGroup(tx *dbs.Tx, isOn bool, name string, code string, description string) (int64, error) {
|
func (this *HTTPFirewallRuleGroupDAO) CreateGroup(tx *dbs.Tx, isOn bool, name string, code string, description string) (int64, error) {
|
||||||
op := NewHTTPFirewallRuleGroupOperator()
|
var op = NewHTTPFirewallRuleGroupOperator()
|
||||||
op.State = HTTPFirewallRuleStateEnabled
|
op.State = HTTPFirewallRuleStateEnabled
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.Name = name
|
op.Name = name
|
||||||
@@ -184,7 +184,7 @@ func (this *HTTPFirewallRuleGroupDAO) UpdateGroup(tx *dbs.Tx, groupId int64, isO
|
|||||||
if groupId <= 0 {
|
if groupId <= 0 {
|
||||||
return errors.New("invalid groupId")
|
return errors.New("invalid groupId")
|
||||||
}
|
}
|
||||||
op := NewHTTPFirewallRuleGroupOperator()
|
var op = NewHTTPFirewallRuleGroupOperator()
|
||||||
op.Id = groupId
|
op.Id = groupId
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.Name = name
|
op.Name = name
|
||||||
@@ -202,7 +202,7 @@ func (this *HTTPFirewallRuleGroupDAO) UpdateGroupSets(tx *dbs.Tx, groupId int64,
|
|||||||
if groupId <= 0 {
|
if groupId <= 0 {
|
||||||
return errors.New("invalid groupId")
|
return errors.New("invalid groupId")
|
||||||
}
|
}
|
||||||
op := NewHTTPFirewallRuleGroupOperator()
|
var op = NewHTTPFirewallRuleGroupOperator()
|
||||||
op.Id = groupId
|
op.Id = groupId
|
||||||
op.Sets = setRefsJSON
|
op.Sets = setRefsJSON
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ func (this *HTTPFirewallRuleSetDAO) ComposeFirewallRuleSet(tx *dbs.Tx, setId int
|
|||||||
|
|
||||||
// CreateOrUpdateSetFromConfig 从配置中创建规则集
|
// CreateOrUpdateSetFromConfig 从配置中创建规则集
|
||||||
func (this *HTTPFirewallRuleSetDAO) CreateOrUpdateSetFromConfig(tx *dbs.Tx, setConfig *firewallconfigs.HTTPFirewallRuleSet) (int64, error) {
|
func (this *HTTPFirewallRuleSetDAO) CreateOrUpdateSetFromConfig(tx *dbs.Tx, setConfig *firewallconfigs.HTTPFirewallRuleSet) (int64, error) {
|
||||||
op := NewHTTPFirewallRuleSetOperator()
|
var op = NewHTTPFirewallRuleSetOperator()
|
||||||
op.State = HTTPFirewallRuleSetStateEnabled
|
op.State = HTTPFirewallRuleSetStateEnabled
|
||||||
op.Id = setConfig.Id
|
op.Id = setConfig.Id
|
||||||
op.IsOn = setConfig.IsOn
|
op.IsOn = setConfig.IsOn
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ func (this *HTTPGzipDAO) ComposeGzipConfig(tx *dbs.Tx, gzipId int64) (*servercon
|
|||||||
|
|
||||||
// CreateGzip 创建Gzip
|
// CreateGzip 创建Gzip
|
||||||
func (this *HTTPGzipDAO) CreateGzip(tx *dbs.Tx, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) (int64, error) {
|
func (this *HTTPGzipDAO) CreateGzip(tx *dbs.Tx, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) (int64, error) {
|
||||||
op := NewHTTPGzipOperator()
|
var op = NewHTTPGzipOperator()
|
||||||
op.State = HTTPGzipStateEnabled
|
op.State = HTTPGzipStateEnabled
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.Level = level
|
op.Level = level
|
||||||
@@ -146,7 +146,7 @@ func (this *HTTPGzipDAO) UpdateGzip(tx *dbs.Tx, gzipId int64, level int, minLeng
|
|||||||
if gzipId <= 0 {
|
if gzipId <= 0 {
|
||||||
return errors.New("invalid gzipId")
|
return errors.New("invalid gzipId")
|
||||||
}
|
}
|
||||||
op := NewHTTPGzipOperator()
|
var op = NewHTTPGzipOperator()
|
||||||
op.Id = gzipId
|
op.Id = gzipId
|
||||||
op.Level = level
|
op.Level = level
|
||||||
if len(minLengthJSON) > 0 {
|
if len(minLengthJSON) > 0 {
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ func (this *HTTPHeaderDAO) FindHTTPHeaderName(tx *dbs.Tx, id int64) (string, err
|
|||||||
|
|
||||||
// CreateHeader 创建Header
|
// CreateHeader 创建Header
|
||||||
func (this *HTTPHeaderDAO) CreateHeader(tx *dbs.Tx, userId int64, name string, value string, status []int, disableRedirect bool, shouldAppend bool, shouldReplace bool, replaceValues []*shared.HTTPHeaderReplaceValue, methods []string, domains []string) (int64, error) {
|
func (this *HTTPHeaderDAO) CreateHeader(tx *dbs.Tx, userId int64, name string, value string, status []int, disableRedirect bool, shouldAppend bool, shouldReplace bool, replaceValues []*shared.HTTPHeaderReplaceValue, methods []string, domains []string) (int64, error) {
|
||||||
op := NewHTTPHeaderOperator()
|
var op = NewHTTPHeaderOperator()
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
op.State = HTTPHeaderStateEnabled
|
op.State = HTTPHeaderStateEnabled
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
@@ -156,7 +156,7 @@ func (this *HTTPHeaderDAO) UpdateHeader(tx *dbs.Tx, headerId int64, name string,
|
|||||||
return errors.New("invalid headerId")
|
return errors.New("invalid headerId")
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewHTTPHeaderOperator()
|
var op = NewHTTPHeaderOperator()
|
||||||
op.Id = headerId
|
op.Id = headerId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.Value = value
|
op.Value = value
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ func (this *HTTPHeaderPolicyDAO) FindEnabledHTTPHeaderPolicy(tx *dbs.Tx, id int6
|
|||||||
|
|
||||||
// CreateHeaderPolicy 创建策略
|
// CreateHeaderPolicy 创建策略
|
||||||
func (this *HTTPHeaderPolicyDAO) CreateHeaderPolicy(tx *dbs.Tx) (int64, error) {
|
func (this *HTTPHeaderPolicyDAO) CreateHeaderPolicy(tx *dbs.Tx) (int64, error) {
|
||||||
op := NewHTTPHeaderPolicyOperator()
|
var op = NewHTTPHeaderPolicyOperator()
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = HTTPHeaderPolicyStateEnabled
|
op.State = HTTPHeaderPolicyStateEnabled
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -93,7 +93,7 @@ func (this *HTTPHeaderPolicyDAO) UpdateAddingHeaders(tx *dbs.Tx, policyId int64,
|
|||||||
return errors.New("invalid policyId")
|
return errors.New("invalid policyId")
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewHTTPHeaderPolicyOperator()
|
var op = NewHTTPHeaderPolicyOperator()
|
||||||
op.Id = policyId
|
op.Id = policyId
|
||||||
op.AddHeaders = headersJSON
|
op.AddHeaders = headersJSON
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -109,7 +109,7 @@ func (this *HTTPHeaderPolicyDAO) UpdateSettingHeaders(tx *dbs.Tx, policyId int64
|
|||||||
return errors.New("invalid policyId")
|
return errors.New("invalid policyId")
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewHTTPHeaderPolicyOperator()
|
var op = NewHTTPHeaderPolicyOperator()
|
||||||
op.Id = policyId
|
op.Id = policyId
|
||||||
op.SetHeaders = headersJSON
|
op.SetHeaders = headersJSON
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -125,7 +125,7 @@ func (this *HTTPHeaderPolicyDAO) UpdateReplacingHeaders(tx *dbs.Tx, policyId int
|
|||||||
return errors.New("invalid policyId")
|
return errors.New("invalid policyId")
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewHTTPHeaderPolicyOperator()
|
var op = NewHTTPHeaderPolicyOperator()
|
||||||
op.Id = policyId
|
op.Id = policyId
|
||||||
op.ReplaceHeaders = headersJSON
|
op.ReplaceHeaders = headersJSON
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -141,7 +141,7 @@ func (this *HTTPHeaderPolicyDAO) UpdateAddingTrailers(tx *dbs.Tx, policyId int64
|
|||||||
return errors.New("invalid policyId")
|
return errors.New("invalid policyId")
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewHTTPHeaderPolicyOperator()
|
var op = NewHTTPHeaderPolicyOperator()
|
||||||
op.Id = policyId
|
op.Id = policyId
|
||||||
op.AddTrailers = headersJSON
|
op.AddTrailers = headersJSON
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -162,7 +162,7 @@ func (this *HTTPHeaderPolicyDAO) UpdateDeletingHeaders(tx *dbs.Tx, policyId int6
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewHTTPHeaderPolicyOperator()
|
var op = NewHTTPHeaderPolicyOperator()
|
||||||
op.Id = policyId
|
op.Id = policyId
|
||||||
op.DeleteHeaders = string(namesJSON)
|
op.DeleteHeaders = string(namesJSON)
|
||||||
err = this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ func (this *HTTPLocationDAO) FindHTTPLocationName(tx *dbs.Tx, id int64) (string,
|
|||||||
|
|
||||||
// CreateLocation 创建路由规则
|
// CreateLocation 创建路由规则
|
||||||
func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name string, pattern string, description string, isBreak bool, condsJSON []byte, domains []string) (int64, error) {
|
func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name string, pattern string, description string, isBreak bool, condsJSON []byte, domains []string) (int64, error) {
|
||||||
op := NewHTTPLocationOperator()
|
var op = NewHTTPLocationOperator()
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = HTTPLocationStateEnabled
|
op.State = HTTPLocationStateEnabled
|
||||||
op.ParentId = parentId
|
op.ParentId = parentId
|
||||||
@@ -121,7 +121,7 @@ func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name s
|
|||||||
if locationId <= 0 {
|
if locationId <= 0 {
|
||||||
return errors.New("invalid locationId")
|
return errors.New("invalid locationId")
|
||||||
}
|
}
|
||||||
op := NewHTTPLocationOperator()
|
var op = NewHTTPLocationOperator()
|
||||||
op.Id = locationId
|
op.Id = locationId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.Pattern = pattern
|
op.Pattern = pattern
|
||||||
@@ -257,7 +257,7 @@ func (this *HTTPLocationDAO) UpdateLocationReverseProxy(tx *dbs.Tx, locationId i
|
|||||||
if locationId <= 0 {
|
if locationId <= 0 {
|
||||||
return errors.New("invalid locationId")
|
return errors.New("invalid locationId")
|
||||||
}
|
}
|
||||||
op := NewHTTPLocationOperator()
|
var op = NewHTTPLocationOperator()
|
||||||
op.Id = locationId
|
op.Id = locationId
|
||||||
op.ReverseProxy = JSONBytes(reverseProxyJSON)
|
op.ReverseProxy = JSONBytes(reverseProxyJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -281,7 +281,7 @@ func (this *HTTPLocationDAO) UpdateLocationWeb(tx *dbs.Tx, locationId int64, web
|
|||||||
if locationId <= 0 {
|
if locationId <= 0 {
|
||||||
return errors.New("invalid locationId")
|
return errors.New("invalid locationId")
|
||||||
}
|
}
|
||||||
op := NewHTTPLocationOperator()
|
var op = NewHTTPLocationOperator()
|
||||||
op.Id = locationId
|
op.Id = locationId
|
||||||
op.WebId = webId
|
op.WebId = webId
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ func (this *HTTPPageDAO) FindEnabledHTTPPage(tx *dbs.Tx, id int64) (*HTTPPage, e
|
|||||||
|
|
||||||
// CreatePage 创建Page
|
// CreatePage 创建Page
|
||||||
func (this *HTTPPageDAO) CreatePage(tx *dbs.Tx, userId int64, statusList []string, bodyType shared.BodyType, url string, body string, newStatus int) (pageId int64, err error) {
|
func (this *HTTPPageDAO) CreatePage(tx *dbs.Tx, userId int64, statusList []string, bodyType shared.BodyType, url string, body string, newStatus int) (pageId int64, err error) {
|
||||||
op := NewHTTPPageOperator()
|
var op = NewHTTPPageOperator()
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = HTTPPageStateEnabled
|
op.State = HTTPPageStateEnabled
|
||||||
@@ -108,7 +108,7 @@ func (this *HTTPPageDAO) UpdatePage(tx *dbs.Tx, pageId int64, statusList []strin
|
|||||||
return errors.New("invalid pageId")
|
return errors.New("invalid pageId")
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewHTTPPageOperator()
|
var op = NewHTTPPageOperator()
|
||||||
op.Id = pageId
|
op.Id = pageId
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = HTTPPageStateEnabled
|
op.State = HTTPPageStateEnabled
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(tx *dbs.Tx, rewriteRuleId int
|
|||||||
|
|
||||||
// CreateRewriteRule 创建规则
|
// CreateRewriteRule 创建规则
|
||||||
func (this *HTTPRewriteRuleDAO) CreateRewriteRule(tx *dbs.Tx, pattern string, replace string, mode string, redirectStatus int, isBreak bool, proxyHost string, withQuery bool, isOn bool, condsJSON []byte) (int64, error) {
|
func (this *HTTPRewriteRuleDAO) CreateRewriteRule(tx *dbs.Tx, pattern string, replace string, mode string, redirectStatus int, isBreak bool, proxyHost string, withQuery bool, isOn bool, condsJSON []byte) (int64, error) {
|
||||||
op := NewHTTPRewriteRuleOperator()
|
var op = NewHTTPRewriteRuleOperator()
|
||||||
op.State = HTTPRewriteRuleStateEnabled
|
op.State = HTTPRewriteRuleStateEnabled
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
|
|
||||||
@@ -150,7 +150,7 @@ func (this *HTTPRewriteRuleDAO) UpdateRewriteRule(tx *dbs.Tx, rewriteRuleId int6
|
|||||||
if rewriteRuleId <= 0 {
|
if rewriteRuleId <= 0 {
|
||||||
return errors.New("invalid rewriteRuleId")
|
return errors.New("invalid rewriteRuleId")
|
||||||
}
|
}
|
||||||
op := NewHTTPRewriteRuleOperator()
|
var op = NewHTTPRewriteRuleOperator()
|
||||||
op.Id = rewriteRuleId
|
op.Id = rewriteRuleId
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.Pattern = pattern
|
op.Pattern = pattern
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
||||||
@@ -446,6 +447,16 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UAM
|
||||||
|
if teaconst.IsPlus && IsNotNull(web.Uam) {
|
||||||
|
var uamConfig = &serverconfigs.UAMConfig{}
|
||||||
|
err = json.Unmarshal(web.Uam, uamConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.UAM = uamConfig
|
||||||
|
}
|
||||||
|
|
||||||
if cacheMap != nil {
|
if cacheMap != nil {
|
||||||
cacheMap.Put(cacheKey, config)
|
cacheMap.Put(cacheKey, config)
|
||||||
}
|
}
|
||||||
@@ -455,7 +466,7 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
|
|||||||
|
|
||||||
// CreateWeb 创建Web配置
|
// CreateWeb 创建Web配置
|
||||||
func (this *HTTPWebDAO) CreateWeb(tx *dbs.Tx, adminId int64, userId int64, rootJSON []byte) (int64, error) {
|
func (this *HTTPWebDAO) CreateWeb(tx *dbs.Tx, adminId int64, userId int64, rootJSON []byte) (int64, error) {
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.State = HTTPWebStateEnabled
|
op.State = HTTPWebStateEnabled
|
||||||
op.AdminId = adminId
|
op.AdminId = adminId
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
@@ -474,7 +485,7 @@ func (this *HTTPWebDAO) UpdateWeb(tx *dbs.Tx, webId int64, rootJSON []byte) erro
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.Root = JSONBytes(rootJSON)
|
op.Root = JSONBytes(rootJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -490,7 +501,7 @@ func (this *HTTPWebDAO) UpdateWebCompression(tx *dbs.Tx, webId int64, compressio
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.Compression = JSONBytes(compressionConfig)
|
op.Compression = JSONBytes(compressionConfig)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -506,7 +517,7 @@ func (this *HTTPWebDAO) UpdateWebWebP(tx *dbs.Tx, webId int64, webpConfig []byte
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.Webp = JSONBytes(webpConfig)
|
op.Webp = JSONBytes(webpConfig)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -537,7 +548,7 @@ func (this *HTTPWebDAO) UpdateWebCharset(tx *dbs.Tx, webId int64, charsetJSON []
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.Charset = JSONBytes(charsetJSON)
|
op.Charset = JSONBytes(charsetJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -553,7 +564,7 @@ func (this *HTTPWebDAO) UpdateWebRequestHeaderPolicy(tx *dbs.Tx, webId int64, he
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.RequestHeader = JSONBytes(headerPolicyJSON)
|
op.RequestHeader = JSONBytes(headerPolicyJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -569,7 +580,7 @@ func (this *HTTPWebDAO) UpdateWebResponseHeaderPolicy(tx *dbs.Tx, webId int64, h
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.ResponseHeader = JSONBytes(headerPolicyJSON)
|
op.ResponseHeader = JSONBytes(headerPolicyJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -585,7 +596,7 @@ func (this *HTTPWebDAO) UpdateWebPages(tx *dbs.Tx, webId int64, pagesJSON []byte
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.Pages = JSONBytes(pagesJSON)
|
op.Pages = JSONBytes(pagesJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -601,7 +612,7 @@ func (this *HTTPWebDAO) UpdateWebShutdown(tx *dbs.Tx, webId int64, shutdownJSON
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.Shutdown = JSONBytes(shutdownJSON)
|
op.Shutdown = JSONBytes(shutdownJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -617,7 +628,7 @@ func (this *HTTPWebDAO) UpdateWebAccessLogConfig(tx *dbs.Tx, webId int64, access
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.AccessLog = JSONBytes(accessLogJSON)
|
op.AccessLog = JSONBytes(accessLogJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -633,7 +644,7 @@ func (this *HTTPWebDAO) UpdateWebStat(tx *dbs.Tx, webId int64, statJSON []byte)
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.Stat = JSONBytes(statJSON)
|
op.Stat = JSONBytes(statJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -649,7 +660,7 @@ func (this *HTTPWebDAO) UpdateWebCache(tx *dbs.Tx, webId int64, cacheJSON []byte
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.Cache = JSONBytes(cacheJSON)
|
op.Cache = JSONBytes(cacheJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -665,7 +676,7 @@ func (this *HTTPWebDAO) UpdateWebFirewall(tx *dbs.Tx, webId int64, firewallJSON
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.Firewall = JSONBytes(firewallJSON)
|
op.Firewall = JSONBytes(firewallJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -681,7 +692,7 @@ func (this *HTTPWebDAO) UpdateWebLocations(tx *dbs.Tx, webId int64, locationsJSO
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.Locations = JSONBytes(locationsJSON)
|
op.Locations = JSONBytes(locationsJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -697,7 +708,7 @@ func (this *HTTPWebDAO) UpdateWebRedirectToHTTPS(tx *dbs.Tx, webId int64, redire
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.RedirectToHttps = JSONBytes(redirectToHTTPSJSON)
|
op.RedirectToHttps = JSONBytes(redirectToHTTPSJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -713,7 +724,7 @@ func (this *HTTPWebDAO) UpdateWebsocket(tx *dbs.Tx, webId int64, websocketJSON [
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.Websocket = JSONBytes(websocketJSON)
|
op.Websocket = JSONBytes(websocketJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -729,7 +740,7 @@ func (this *HTTPWebDAO) UpdateWebFastcgi(tx *dbs.Tx, webId int64, fastcgiJSON []
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.Fastcgi = JSONBytes(fastcgiJSON)
|
op.Fastcgi = JSONBytes(fastcgiJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -745,7 +756,7 @@ func (this *HTTPWebDAO) UpdateWebRewriteRules(tx *dbs.Tx, webId int64, rewriteRu
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.RewriteRules = JSONBytes(rewriteRulesJSON)
|
op.RewriteRules = JSONBytes(rewriteRulesJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -761,7 +772,7 @@ func (this *HTTPWebDAO) UpdateWebAuth(tx *dbs.Tx, webId int64, authJSON []byte)
|
|||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
var op = NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.Auth = JSONBytes(authJSON)
|
op.Auth = JSONBytes(authJSON)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
@@ -1168,6 +1179,35 @@ func (this *HTTPWebDAO) FindWebRequestScripts(tx *dbs.Tx, webId int64) (*serverc
|
|||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateWebUAM 开启UAM
|
||||||
|
func (this *HTTPWebDAO) UpdateWebUAM(tx *dbs.Tx, webId int64, uamConfig *serverconfigs.UAMConfig) error {
|
||||||
|
if uamConfig == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
configJSON, err := json.Marshal(uamConfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(webId).
|
||||||
|
Set("uam", configJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.NotifyUpdate(tx, webId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindWebUAM 查找服务的UAM配置
|
||||||
|
func (this *HTTPWebDAO) FindWebUAM(tx *dbs.Tx, webId int64) ([]byte, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(webId).
|
||||||
|
Result("uam").
|
||||||
|
FindJSONCol()
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
|
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
|
||||||
// server
|
// server
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ type HTTPWeb struct {
|
|||||||
MergeSlashes uint8 `field:"mergeSlashes"` // 是否合并路径中的斜杠
|
MergeSlashes uint8 `field:"mergeSlashes"` // 是否合并路径中的斜杠
|
||||||
RequestLimit dbs.JSON `field:"requestLimit"` // 请求限制
|
RequestLimit dbs.JSON `field:"requestLimit"` // 请求限制
|
||||||
RequestScripts dbs.JSON `field:"requestScripts"` // 请求脚本
|
RequestScripts dbs.JSON `field:"requestScripts"` // 请求脚本
|
||||||
|
Uam dbs.JSON `field:"uam"` // UAM设置
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPWebOperator struct {
|
type HTTPWebOperator struct {
|
||||||
@@ -73,6 +74,7 @@ type HTTPWebOperator struct {
|
|||||||
MergeSlashes interface{} // 是否合并路径中的斜杠
|
MergeSlashes interface{} // 是否合并路径中的斜杠
|
||||||
RequestLimit interface{} // 请求限制
|
RequestLimit interface{} // 请求限制
|
||||||
RequestScripts interface{} // 请求脚本
|
RequestScripts interface{} // 请求脚本
|
||||||
|
Uam interface{} // UAM设置
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPWebOperator() *HTTPWebOperator {
|
func NewHTTPWebOperator() *HTTPWebOperator {
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ func (this *HTTPWebsocketDAO) ComposeWebsocketConfig(tx *dbs.Tx, websocketId int
|
|||||||
|
|
||||||
// CreateWebsocket 创建Websocket配置
|
// CreateWebsocket 创建Websocket配置
|
||||||
func (this *HTTPWebsocketDAO) CreateWebsocket(tx *dbs.Tx, handshakeTimeoutJSON []byte, allowAllOrigins bool, allowedOrigins []string, requestSameOrigin bool, requestOrigin string) (websocketId int64, err error) {
|
func (this *HTTPWebsocketDAO) CreateWebsocket(tx *dbs.Tx, handshakeTimeoutJSON []byte, allowAllOrigins bool, allowedOrigins []string, requestSameOrigin bool, requestOrigin string) (websocketId int64, err error) {
|
||||||
op := NewHTTPWebsocketOperator()
|
var op = NewHTTPWebsocketOperator()
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = HTTPWebsocketStateEnabled
|
op.State = HTTPWebsocketStateEnabled
|
||||||
if len(handshakeTimeoutJSON) > 0 {
|
if len(handshakeTimeoutJSON) > 0 {
|
||||||
@@ -135,7 +135,7 @@ func (this *HTTPWebsocketDAO) UpdateWebsocket(tx *dbs.Tx, websocketId int64, han
|
|||||||
if websocketId <= 0 {
|
if websocketId <= 0 {
|
||||||
return errors.New("invalid websocketId")
|
return errors.New("invalid websocketId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebsocketOperator()
|
var op = NewHTTPWebsocketOperator()
|
||||||
op.Id = websocketId
|
op.Id = websocketId
|
||||||
if len(handshakeTimeoutJSON) > 0 {
|
if len(handshakeTimeoutJSON) > 0 {
|
||||||
op.HandshakeTimeout = handshakeTimeoutJSON
|
op.HandshakeTimeout = handshakeTimeoutJSON
|
||||||
|
|||||||
@@ -93,6 +93,71 @@ func (this *IPItemDAO) DisableIPItem(tx *dbs.Tx, id int64) error {
|
|||||||
return this.NotifyUpdate(tx, id)
|
return this.NotifyUpdate(tx, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DisableIPItemsWithIP 禁用某个IP相关条目
|
||||||
|
func (this *IPItemDAO) DisableIPItemsWithIP(tx *dbs.Tx, ipFrom string, ipTo string, userId int64, listId int64) error {
|
||||||
|
if len(ipFrom) == 0 {
|
||||||
|
return errors.New("invalid 'ipFrom'")
|
||||||
|
}
|
||||||
|
|
||||||
|
var query = this.Query(tx).
|
||||||
|
Result("id", "listId").
|
||||||
|
Attr("ipFrom", ipFrom).
|
||||||
|
Attr("ipTo", ipTo).
|
||||||
|
State(IPItemStateEnabled)
|
||||||
|
|
||||||
|
if listId > 0 {
|
||||||
|
if userId > 0 {
|
||||||
|
err := SharedIPListDAO.CheckUserIPList(tx, userId, listId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
query.Attr("listId", listId)
|
||||||
|
}
|
||||||
|
|
||||||
|
ones, err := query.FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var itemIds = []int64{}
|
||||||
|
for _, one := range ones {
|
||||||
|
var item = one.(*IPItem)
|
||||||
|
var itemId = int64(item.Id)
|
||||||
|
var itemListId = int64(item.ListId)
|
||||||
|
if itemListId != listId && userId > 0 {
|
||||||
|
err = SharedIPListDAO.CheckUserIPList(tx, userId, itemListId)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
itemIds = append(itemIds, itemId)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, itemId := range itemIds {
|
||||||
|
version, err := SharedIPListDAO.IncreaseVersion(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Pk(itemId).
|
||||||
|
Set("state", IPItemStateDisabled).
|
||||||
|
Set("version", version).
|
||||||
|
Update()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(itemIds) > 0 {
|
||||||
|
return this.NotifyUpdate(tx, itemIds[len(itemIds)-1])
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// DisableIPItemsWithListId 禁用某个IP名单内的所有IP
|
// DisableIPItemsWithListId 禁用某个IP名单内的所有IP
|
||||||
func (this *IPItemDAO) DisableIPItemsWithListId(tx *dbs.Tx, listId int64) error {
|
func (this *IPItemDAO) DisableIPItemsWithListId(tx *dbs.Tx, listId int64) error {
|
||||||
for {
|
for {
|
||||||
@@ -142,14 +207,35 @@ func (this *IPItemDAO) FindEnabledIPItem(tx *dbs.Tx, id int64) (*IPItem, error)
|
|||||||
|
|
||||||
// DeleteOldItem 根据IP删除以前的旧记录
|
// DeleteOldItem 根据IP删除以前的旧记录
|
||||||
func (this *IPItemDAO) DeleteOldItem(tx *dbs.Tx, listId int64, ipFrom string, ipTo string) error {
|
func (this *IPItemDAO) DeleteOldItem(tx *dbs.Tx, listId int64, ipFrom string, ipTo string) error {
|
||||||
_, err := this.Query(tx).
|
ones, err := this.Query(tx).
|
||||||
|
ResultPk().
|
||||||
UseIndex("ipFrom").
|
UseIndex("ipFrom").
|
||||||
Attr("listId", listId).
|
Attr("listId", listId).
|
||||||
Attr("ipFrom", ipFrom).
|
Attr("ipFrom", ipFrom).
|
||||||
Attr("ipTo", ipTo).
|
Attr("ipTo", ipTo).
|
||||||
Delete()
|
Set("state", IPItemStateEnabled).
|
||||||
// 这里不通知更新
|
FindAll()
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, one := range ones {
|
||||||
|
var itemId = int64(one.(*IPItem).Id)
|
||||||
|
version, err := SharedIPListDAO.IncreaseVersion(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(itemId).
|
||||||
|
Set("version", version).
|
||||||
|
Set("state", IPItemStateDisabled).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateIPItem 创建IP
|
// CreateIPItem 创建IP
|
||||||
@@ -202,6 +288,8 @@ func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx,
|
|||||||
}
|
}
|
||||||
|
|
||||||
op.State = IPItemStateEnabled
|
op.State = IPItemStateEnabled
|
||||||
|
op.UpdatedAt = time.Now().Unix()
|
||||||
|
|
||||||
err = this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -242,7 +330,7 @@ func (this *IPItemDAO) UpdateIPItem(tx *dbs.Tx, itemId int64, ipFrom string, ipT
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
op := NewIPItemOperator()
|
var op = NewIPItemOperator()
|
||||||
op.Id = itemId
|
op.Id = itemId
|
||||||
op.IpFrom = ipFrom
|
op.IpFrom = ipFrom
|
||||||
op.IpTo = ipTo
|
op.IpTo = ipTo
|
||||||
@@ -453,7 +541,7 @@ func (this *IPItemDAO) UpdateItemsRead(tx *dbs.Tx) error {
|
|||||||
func (this *IPItemDAO) CleanExpiredIPItems(tx *dbs.Tx) error {
|
func (this *IPItemDAO) CleanExpiredIPItems(tx *dbs.Tx) error {
|
||||||
// 删除 N 天之前过期的数据
|
// 删除 N 天之前过期的数据
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Where("expiredAt<=:timestamp").
|
Where("(createdAt<=:timestamp AND updatedAt<=:timestamp)").
|
||||||
State(IPItemStateDisabled).
|
State(IPItemStateDisabled).
|
||||||
Param("timestamp", time.Now().Unix()-7*86400). // N 天之前过期的
|
Param("timestamp", time.Now().Unix()-7*86400). // N 天之前过期的
|
||||||
Limit(10000). // 限制条数,防止数量过多导致超时
|
Limit(10000). // 限制条数,防止数量过多导致超时
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package models
|
package models_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -14,7 +15,7 @@ func TestIPItemDAO_NotifyClustersUpdate(t *testing.T) {
|
|||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
err := SharedIPItemDAO.NotifyUpdate(tx, 28)
|
err := models.SharedIPItemDAO.NotifyUpdate(tx, 28)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -25,7 +26,7 @@ func TestIPItemDAO_DisableIPItemsWithListId(t *testing.T) {
|
|||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
err := SharedIPItemDAO.DisableIPItemsWithListId(tx, 67)
|
err := models.SharedIPItemDAO.DisableIPItemsWithListId(tx, 67)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -36,7 +37,7 @@ func TestIPItemDAO_ListIPItemsAfterVersion(t *testing.T) {
|
|||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
_, err := SharedIPItemDAO.ListIPItemsAfterVersion(tx, 0, 100)
|
_, err := models.SharedIPItemDAO.ListIPItemsAfterVersion(tx, 0, 100)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -47,10 +48,10 @@ func TestIPItemDAO_CreateManyIPs(t *testing.T) {
|
|||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
|
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
var dao = NewIPItemDAO()
|
var dao = models.NewIPItemDAO()
|
||||||
var n = 10
|
var n = 10
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
itemId, err := dao.CreateIPItem(tx, firewallconfigs.GlobalListId, "192."+types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255)), "", time.Now().Unix()+86400, "test", IPItemTypeIPv4, "warning", 0, 0, 0, 0, 0, 0, 0)
|
itemId, err := dao.CreateIPItem(tx, firewallconfigs.GlobalListId, "192."+types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255)), "", time.Now().Unix()+86400, "test", models.IPItemTypeIPv4, "warning", 0, 0, 0, 0, 0, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -62,3 +63,14 @@ func TestIPItemDAO_CreateManyIPs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
t.Log("ok")
|
t.Log("ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIPItemDAO_DisableIPItemsWithIP(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var tx *dbs.Tx
|
||||||
|
err := models.SharedIPItemDAO.DisableIPItemsWithIP(tx, "192.168.1.100", "", 0, 0)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log("ok")
|
||||||
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启用条目
|
// EnableIPLibrary 启用条目
|
||||||
func (this *IPLibraryDAO) EnableIPLibrary(tx *dbs.Tx, id int64) error {
|
func (this *IPLibraryDAO) EnableIPLibrary(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -42,7 +42,7 @@ func (this *IPLibraryDAO) EnableIPLibrary(tx *dbs.Tx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁用条目
|
// DisableIPLibrary 禁用条目
|
||||||
func (this *IPLibraryDAO) DisableIPLibrary(tx *dbs.Tx, id int64) error {
|
func (this *IPLibraryDAO) DisableIPLibrary(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -51,7 +51,7 @@ func (this *IPLibraryDAO) DisableIPLibrary(tx *dbs.Tx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找启用中的条目
|
// FindEnabledIPLibrary 查找启用中的条目
|
||||||
func (this *IPLibraryDAO) FindEnabledIPLibrary(tx *dbs.Tx, id int64) (*IPLibrary, error) {
|
func (this *IPLibraryDAO) FindEnabledIPLibrary(tx *dbs.Tx, id int64) (*IPLibrary, error) {
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -63,7 +63,7 @@ func (this *IPLibraryDAO) FindEnabledIPLibrary(tx *dbs.Tx, id int64) (*IPLibrary
|
|||||||
return result.(*IPLibrary), err
|
return result.(*IPLibrary), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找某个类型的IP库列表
|
// FindAllEnabledIPLibrariesWithType 查找某个类型的IP库列表
|
||||||
func (this *IPLibraryDAO) FindAllEnabledIPLibrariesWithType(tx *dbs.Tx, libraryType string) (result []*IPLibrary, err error) {
|
func (this *IPLibraryDAO) FindAllEnabledIPLibrariesWithType(tx *dbs.Tx, libraryType string) (result []*IPLibrary, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(IPLibraryStateEnabled).
|
State(IPLibraryStateEnabled).
|
||||||
@@ -74,7 +74,7 @@ func (this *IPLibraryDAO) FindAllEnabledIPLibrariesWithType(tx *dbs.Tx, libraryT
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找某个类型的最新的IP库
|
// FindLatestIPLibraryWithType 查找某个类型的最新的IP库
|
||||||
func (this *IPLibraryDAO) FindLatestIPLibraryWithType(tx *dbs.Tx, libraryType string) (*IPLibrary, error) {
|
func (this *IPLibraryDAO) FindLatestIPLibraryWithType(tx *dbs.Tx, libraryType string) (*IPLibrary, error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
State(IPLibraryStateEnabled).
|
State(IPLibraryStateEnabled).
|
||||||
@@ -90,9 +90,9 @@ func (this *IPLibraryDAO) FindLatestIPLibraryWithType(tx *dbs.Tx, libraryType st
|
|||||||
return one.(*IPLibrary), nil
|
return one.(*IPLibrary), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建新的IP库
|
// CreateIPLibrary 创建新的IP库
|
||||||
func (this *IPLibraryDAO) CreateIPLibrary(tx *dbs.Tx, libraryType string, fileId int64) (int64, error) {
|
func (this *IPLibraryDAO) CreateIPLibrary(tx *dbs.Tx, libraryType string, fileId int64) (int64, error) {
|
||||||
op := NewIPLibraryOperator()
|
var op = NewIPLibraryOperator()
|
||||||
op.Type = libraryType
|
op.Type = libraryType
|
||||||
op.FileId = fileId
|
op.FileId = fileId
|
||||||
op.State = IPLibraryStateEnabled
|
op.State = IPLibraryStateEnabled
|
||||||
|
|||||||
572
internal/db/models/ip_library_file_dao.go
Normal file
572
internal/db/models/ip_library_file_dao.go
Normal file
@@ -0,0 +1,572 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
IPLibraryFileStateEnabled = 1 // 已启用
|
||||||
|
IPLibraryFileStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
type IPLibraryFileDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewIPLibraryFileDAO() *IPLibraryFileDAO {
|
||||||
|
return dbs.NewDAO(&IPLibraryFileDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeIPLibraryFiles",
|
||||||
|
Model: new(IPLibraryFile),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*IPLibraryFileDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedIPLibraryFileDAO *IPLibraryFileDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedIPLibraryFileDAO = NewIPLibraryFileDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableIPLibraryFile 启用条目
|
||||||
|
func (this *IPLibraryFileDAO) EnableIPLibraryFile(tx *dbs.Tx, id uint64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", IPLibraryFileStateEnabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableIPLibraryFile 禁用条目
|
||||||
|
func (this *IPLibraryFileDAO) DisableIPLibraryFile(tx *dbs.Tx, id int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", IPLibraryFileStateDisabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledIPLibraryFile 查找启用中的条目
|
||||||
|
func (this *IPLibraryFileDAO) FindEnabledIPLibraryFile(tx *dbs.Tx, id int64) (*IPLibraryFile, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
State(IPLibraryFileStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*IPLibraryFile), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateLibraryFile 创建文件
|
||||||
|
func (this *IPLibraryFileDAO) CreateLibraryFile(tx *dbs.Tx, template string, emptyValues []string, fileId int64, countries []string, provinces [][2]string, cities [][3]string, towns [][4]string, providers []string) (int64, error) {
|
||||||
|
var op = NewIPLibraryFileOperator()
|
||||||
|
op.Template = template
|
||||||
|
|
||||||
|
if emptyValues == nil {
|
||||||
|
emptyValues = []string{}
|
||||||
|
}
|
||||||
|
emptyValuesJSON, err := json.Marshal(emptyValues)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.EmptyValues = emptyValuesJSON
|
||||||
|
|
||||||
|
op.FileId = fileId
|
||||||
|
|
||||||
|
if countries == nil {
|
||||||
|
countries = []string{}
|
||||||
|
}
|
||||||
|
countriesJSON, err := json.Marshal(countries)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Countries = countriesJSON
|
||||||
|
|
||||||
|
if provinces == nil {
|
||||||
|
provinces = [][2]string{}
|
||||||
|
}
|
||||||
|
provincesJSON, err := json.Marshal(provinces)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Provinces = provincesJSON
|
||||||
|
|
||||||
|
if cities == nil {
|
||||||
|
cities = [][3]string{}
|
||||||
|
}
|
||||||
|
citiesJSON, err := json.Marshal(cities)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Cities = citiesJSON
|
||||||
|
|
||||||
|
if towns == nil {
|
||||||
|
towns = [][4]string{}
|
||||||
|
}
|
||||||
|
townsJSON, err := json.Marshal(towns)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Towns = townsJSON
|
||||||
|
|
||||||
|
if providers == nil {
|
||||||
|
providers = []string{}
|
||||||
|
}
|
||||||
|
providersJSON, err := json.Marshal(providers)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
op.Providers = providersJSON
|
||||||
|
|
||||||
|
op.IsFinished = false
|
||||||
|
op.State = IPLibraryFileStateEnabled
|
||||||
|
return this.SaveInt64(tx, op)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAllUnfinishedLibraryFiles 查找所有未完成的文件
|
||||||
|
func (this *IPLibraryFileDAO) FindAllUnfinishedLibraryFiles(tx *dbs.Tx) (result []*IPLibraryFile, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
State(IPLibraryFileStateEnabled).
|
||||||
|
Result("id", "fileId", "createdAt"). // 这里不需要其他信息
|
||||||
|
Attr("isFinished", false).
|
||||||
|
DescPk().
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateLibraryFileIsFinished 设置文件为已完成
|
||||||
|
func (this *IPLibraryFileDAO) UpdateLibraryFileIsFinished(tx *dbs.Tx, fileId int64) error {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(fileId).
|
||||||
|
Set("isFinished", true).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindLibraryFileCountries 获取IP库中的国家/地区
|
||||||
|
func (this *IPLibraryFileDAO) FindLibraryFileCountries(tx *dbs.Tx, fileId int64) ([]string, error) {
|
||||||
|
countriesJSON, err := this.Query(tx).
|
||||||
|
Result("countries").
|
||||||
|
Pk(fileId).
|
||||||
|
FindJSONCol()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsNull(countriesJSON) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = []string{}
|
||||||
|
err = json.Unmarshal(countriesJSON, &result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindLibraryFileProvinces 获取IP库中的省份
|
||||||
|
func (this *IPLibraryFileDAO) FindLibraryFileProvinces(tx *dbs.Tx, fileId int64) ([][2]string, error) {
|
||||||
|
provincesJSON, err := this.Query(tx).
|
||||||
|
Result("provinces").
|
||||||
|
Pk(fileId).
|
||||||
|
FindJSONCol()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsNull(provincesJSON) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = [][2]string{}
|
||||||
|
err = json.Unmarshal(provincesJSON, &result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindLibraryFileCities 获取IP库中的城市
|
||||||
|
func (this *IPLibraryFileDAO) FindLibraryFileCities(tx *dbs.Tx, fileId int64) ([][3]string, error) {
|
||||||
|
citiesJSON, err := this.Query(tx).
|
||||||
|
Result("cities").
|
||||||
|
Pk(fileId).
|
||||||
|
FindJSONCol()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsNull(citiesJSON) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = [][3]string{}
|
||||||
|
err = json.Unmarshal(citiesJSON, &result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindLibraryFileTowns 获取IP库中的区县
|
||||||
|
func (this *IPLibraryFileDAO) FindLibraryFileTowns(tx *dbs.Tx, fileId int64) ([][4]string, error) {
|
||||||
|
townsJSON, err := this.Query(tx).
|
||||||
|
Result("towns").
|
||||||
|
Pk(fileId).
|
||||||
|
FindJSONCol()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsNull(townsJSON) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = [][4]string{}
|
||||||
|
err = json.Unmarshal(townsJSON, &result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindLibraryFileProviders 获取IP库中的ISP运营商
|
||||||
|
func (this *IPLibraryFileDAO) FindLibraryFileProviders(tx *dbs.Tx, fileId int64) ([]string, error) {
|
||||||
|
providersJSON, err := this.Query(tx).
|
||||||
|
Result("providers").
|
||||||
|
Pk(fileId).
|
||||||
|
FindJSONCol()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsNull(providersJSON) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = []string{}
|
||||||
|
err = json.Unmarshal(providersJSON, &result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64) error {
|
||||||
|
one, err := this.Query(tx).Pk(libraryFileId).Find()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if one == nil {
|
||||||
|
return errors.New("the library file not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
var libraryFile = one.(*IPLibraryFile)
|
||||||
|
template, err := iplibrary.NewTemplate(libraryFile.Template)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("create template from '" + libraryFile.Template + "' failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
var fileId = int64(libraryFile.FileId)
|
||||||
|
if fileId == 0 {
|
||||||
|
return errors.New("the library file has not been uploaded yet")
|
||||||
|
}
|
||||||
|
|
||||||
|
var dir = Tea.Root + "/data"
|
||||||
|
stat, err := os.Stat(dir)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = os.Mkdir(dir, 0777)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("can not open dir '" + dir + "' to write: " + err.Error())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return errors.New("can not open dir '" + dir + "' to write: " + err.Error())
|
||||||
|
}
|
||||||
|
} else if !stat.IsDir() {
|
||||||
|
_ = os.Remove(dir)
|
||||||
|
|
||||||
|
err = os.Mkdir(dir, 0777)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("can not open dir '" + dir + "' to write: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO 删除以往生成的文件,但要考虑到文件正在被别的任务所使用
|
||||||
|
|
||||||
|
// 国家
|
||||||
|
dbCountries, err := regions.SharedRegionCountryDAO.FindAllCountries(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var countries = []*iplibrary.Country{}
|
||||||
|
for _, country := range dbCountries {
|
||||||
|
countries = append(countries, &iplibrary.Country{
|
||||||
|
Id: int64(country.Id),
|
||||||
|
Name: country.DisplayName(),
|
||||||
|
Codes: country.AllCodes(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 省份
|
||||||
|
dbProvinces, err := regions.SharedRegionProvinceDAO.FindAllEnabledProvinces(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var provinces = []*iplibrary.Province{}
|
||||||
|
for _, province := range dbProvinces {
|
||||||
|
provinces = append(provinces, &iplibrary.Province{
|
||||||
|
Id: int64(province.Id),
|
||||||
|
Name: province.DisplayName(),
|
||||||
|
Codes: province.AllCodes(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 城市
|
||||||
|
dbCities, err := regions.SharedRegionCityDAO.FindAllEnabledCities(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var cities = []*iplibrary.City{}
|
||||||
|
for _, city := range dbCities {
|
||||||
|
cities = append(cities, &iplibrary.City{
|
||||||
|
Id: int64(city.Id),
|
||||||
|
Name: city.DisplayName(),
|
||||||
|
Codes: city.AllCodes(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 区县
|
||||||
|
dbTowns, err := regions.SharedRegionTownDAO.FindAllRegionTowns(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var towns = []*iplibrary.Town{}
|
||||||
|
for _, town := range dbTowns {
|
||||||
|
towns = append(towns, &iplibrary.Town{
|
||||||
|
Id: int64(town.Id),
|
||||||
|
Name: town.DisplayName(),
|
||||||
|
Codes: town.AllCodes(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ISP运营商
|
||||||
|
dbProviders, err := regions.SharedRegionProviderDAO.FindAllEnabledProviders(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var providers = []*iplibrary.Provider{}
|
||||||
|
for _, provider := range dbProviders {
|
||||||
|
providers = append(providers, &iplibrary.Provider{
|
||||||
|
Id: int64(provider.Id),
|
||||||
|
Name: provider.DisplayName(),
|
||||||
|
Codes: provider.AllCodes(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var libraryCode = utils.Sha1RandomString() // 每次都生成新的code
|
||||||
|
var filePath = dir + "/" + this.composeFilename(libraryFileId, libraryCode)
|
||||||
|
writer, err := iplibrary.NewFileWriter(filePath, &iplibrary.Meta{
|
||||||
|
Author: "", // 将来用户可以自行填写
|
||||||
|
CreatedAt: time.Now().Unix(),
|
||||||
|
Countries: countries,
|
||||||
|
Provinces: provinces,
|
||||||
|
Cities: cities,
|
||||||
|
Towns: towns,
|
||||||
|
Providers: providers,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
_ = writer.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
err = writer.WriteMeta()
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("write meta failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
chunkIds, err := SharedFileChunkDAO.FindAllFileChunkIds(tx, fileId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// countries etc ...
|
||||||
|
var countryMap = map[string]int64{} // countryName => countryId
|
||||||
|
for _, country := range dbCountries {
|
||||||
|
for _, code := range country.AllCodes() {
|
||||||
|
countryMap[code] = int64(country.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var provinceMap = map[string]int64{} // countryId_provinceName => provinceId
|
||||||
|
for _, province := range dbProvinces {
|
||||||
|
for _, code := range province.AllCodes() {
|
||||||
|
provinceMap[types.String(province.CountryId)+"_"+code] = int64(province.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var cityMap = map[string]int64{} // provinceId_cityName => cityId
|
||||||
|
for _, city := range dbCities {
|
||||||
|
for _, code := range city.AllCodes() {
|
||||||
|
cityMap[types.String(city.ProvinceId)+"_"+code] = int64(city.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var townMap = map[string]int64{} // cityId_townName => townId
|
||||||
|
for _, town := range dbTowns {
|
||||||
|
for _, code := range town.AllCodes() {
|
||||||
|
townMap[types.String(town.CityId)+"_"+code] = int64(town.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var providerMap = map[string]int64{} // providerName => providerId
|
||||||
|
for _, provider := range dbProviders {
|
||||||
|
for _, code := range provider.AllCodes() {
|
||||||
|
providerMap[code] = int64(provider.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dataParser, err := iplibrary.NewParser(&iplibrary.ParserConfig{
|
||||||
|
Template: template,
|
||||||
|
EmptyValues: libraryFile.DecodeEmptyValues(),
|
||||||
|
Iterator: func(values map[string]string) error {
|
||||||
|
var ipFrom = values["ipFrom"]
|
||||||
|
var ipTo = values["ipTo"]
|
||||||
|
|
||||||
|
var countryName = values["country"]
|
||||||
|
var provinceName = values["province"]
|
||||||
|
var cityName = values["city"]
|
||||||
|
var townName = values["town"]
|
||||||
|
var providerName = values["provider"]
|
||||||
|
|
||||||
|
var countryId = countryMap[countryName]
|
||||||
|
var provinceId int64
|
||||||
|
var cityId int64 = 0
|
||||||
|
var townId int64 = 0
|
||||||
|
var providerId = providerMap[providerName]
|
||||||
|
|
||||||
|
if countryId > 0 {
|
||||||
|
provinceId = provinceMap[types.String(countryId)+"_"+provinceName]
|
||||||
|
if provinceId > 0 {
|
||||||
|
cityId = cityMap[types.String(provinceId)+"_"+cityName]
|
||||||
|
if cityId > 0 {
|
||||||
|
townId = townMap[types.String(cityId)+"_"+townName]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = writer.Write(ipFrom, ipTo, countryId, provinceId, cityId, townId, providerId)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("write failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, chunkId := range chunkIds {
|
||||||
|
chunk, err := SharedFileChunkDAO.FindFileChunk(tx, chunkId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if chunk == nil {
|
||||||
|
return errors.New("invalid chunk file, please upload again")
|
||||||
|
}
|
||||||
|
dataParser.Write(chunk.Data)
|
||||||
|
err = dataParser.Parse()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = writer.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将生成的内容写入到文件
|
||||||
|
stat, err = os.Stat(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("stat generated file failed: " + err.Error())
|
||||||
|
}
|
||||||
|
generatedFileId, err := SharedFileDAO.CreateFile(tx, 0, 0, "ipLibraryFile", "", libraryCode+".db", stat.Size(), "", false)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fp, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("open generated file failed: " + err.Error())
|
||||||
|
}
|
||||||
|
var buf = make([]byte, 256*1024)
|
||||||
|
for {
|
||||||
|
n, err := fp.Read(buf)
|
||||||
|
if n > 0 {
|
||||||
|
_, err = SharedFileChunkDAO.CreateFileChunk(tx, generatedFileId, buf[:n])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
if err != io.EOF {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = SharedFileDAO.UpdateFileIsFinished(tx, generatedFileId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置code
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(libraryFileId).
|
||||||
|
Set("code", libraryCode).
|
||||||
|
Set("isFinished", true).
|
||||||
|
Set("generatedFileId", generatedFileId).
|
||||||
|
Set("generatedAt", time.Now().Unix()).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 组合IP库文件名
|
||||||
|
func (this *IPLibraryFileDAO) composeFilename(libraryFileId int64, code string) string {
|
||||||
|
return "ip-library-" + types.String(libraryFileId) + "-" + code + ".db"
|
||||||
|
}
|
||||||
19
internal/db/models/ip_library_file_dao_test.go
Normal file
19
internal/db/models/ip_library_file_dao_test.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package models_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIPLibraryFileDAO_GenerateIPLibrary(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var tx *dbs.Tx
|
||||||
|
err := models.SharedIPLibraryFileDAO.GenerateIPLibrary(tx, 3)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
44
internal/db/models/ip_library_file_model.go
Normal file
44
internal/db/models/ip_library_file_model.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
|
// IPLibraryFile IP库上传的文件
|
||||||
|
type IPLibraryFile struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
FileId uint64 `field:"fileId"` // 原始文件ID
|
||||||
|
Template string `field:"template"` // 模板
|
||||||
|
EmptyValues dbs.JSON `field:"emptyValues"` // 空值列表
|
||||||
|
GeneratedFileId uint64 `field:"generatedFileId"` // 生成的文件ID
|
||||||
|
GeneratedAt uint64 `field:"generatedAt"` // 生成时间
|
||||||
|
IsFinished bool `field:"isFinished"` // 是否已经完成
|
||||||
|
Countries dbs.JSON `field:"countries"` // 国家/地区
|
||||||
|
Provinces dbs.JSON `field:"provinces"` // 省份
|
||||||
|
Cities dbs.JSON `field:"cities"` // 城市
|
||||||
|
Towns dbs.JSON `field:"towns"` // 区县
|
||||||
|
Providers dbs.JSON `field:"providers"` // ISP服务商
|
||||||
|
Code string `field:"code"` // 文件代号
|
||||||
|
CreatedAt uint64 `field:"createdAt"` // 上传时间
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
type IPLibraryFileOperator struct {
|
||||||
|
Id any // ID
|
||||||
|
FileId any // 原始文件ID
|
||||||
|
Template any // 模板
|
||||||
|
EmptyValues any // 空值列表
|
||||||
|
GeneratedFileId any // 生成的文件ID
|
||||||
|
GeneratedAt any // 生成时间
|
||||||
|
IsFinished any // 是否已经完成
|
||||||
|
Countries any // 国家/地区
|
||||||
|
Provinces any // 省份
|
||||||
|
Cities any // 城市
|
||||||
|
Towns any // 区县
|
||||||
|
Providers any // ISP服务商
|
||||||
|
Code any // 文件代号
|
||||||
|
CreatedAt any // 上传时间
|
||||||
|
State any // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewIPLibraryFileOperator() *IPLibraryFileOperator {
|
||||||
|
return &IPLibraryFileOperator{}
|
||||||
|
}
|
||||||
69
internal/db/models/ip_library_file_model_ext.go
Normal file
69
internal/db/models/ip_library_file_model_ext.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
|
||||||
|
func (this *IPLibraryFile) DecodeCountries() []string {
|
||||||
|
var countries = []string{}
|
||||||
|
if IsNotNull(this.Countries) {
|
||||||
|
err := json.Unmarshal(this.Countries, &countries)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return countries
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IPLibraryFile) DecodeProvinces() [][2]string {
|
||||||
|
var provinces = [][2]string{}
|
||||||
|
if IsNotNull(this.Provinces) {
|
||||||
|
err := json.Unmarshal(this.Provinces, &provinces)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return provinces
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IPLibraryFile) DecodeCities() [][3]string {
|
||||||
|
var cities = [][3]string{}
|
||||||
|
if IsNotNull(this.Cities) {
|
||||||
|
err := json.Unmarshal(this.Cities, &cities)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cities
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IPLibraryFile) DecodeTowns() [][4]string {
|
||||||
|
var towns = [][4]string{}
|
||||||
|
if IsNotNull(this.Towns) {
|
||||||
|
err := json.Unmarshal(this.Towns, &towns)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return towns
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IPLibraryFile) DecodeProviders() []string {
|
||||||
|
var providers = []string{}
|
||||||
|
if IsNotNull(this.Providers) {
|
||||||
|
err := json.Unmarshal(this.Providers, &providers)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return providers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IPLibraryFile) DecodeEmptyValues() []string {
|
||||||
|
var result = []string{}
|
||||||
|
if IsNotNull(this.EmptyValues) {
|
||||||
|
err := json.Unmarshal(this.EmptyValues, &result)
|
||||||
|
if err != nil {
|
||||||
|
// ignore error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -1,22 +1,26 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
// IP库
|
// IPLibrary IP库
|
||||||
type IPLibrary struct {
|
type IPLibrary struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
FileId uint32 `field:"fileId"` // 文件ID
|
FileId uint32 `field:"fileId"` // 文件ID
|
||||||
Type string `field:"type"` // 类型
|
Type string `field:"type"` // 类型
|
||||||
|
Name string `field:"name"` // 名称
|
||||||
|
IsPublic bool `field:"isPublic"` // 是否公用
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
}
|
}
|
||||||
|
|
||||||
type IPLibraryOperator struct {
|
type IPLibraryOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId any // 管理员ID
|
||||||
FileId interface{} // 文件ID
|
FileId any // 文件ID
|
||||||
Type interface{} // 类型
|
Type any // 类型
|
||||||
State interface{} // 状态
|
Name any // 名称
|
||||||
CreatedAt interface{} // 创建时间
|
IsPublic any // 是否公用
|
||||||
|
State any // 状态
|
||||||
|
CreatedAt any // 创建时间
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIPLibraryOperator() *IPLibraryOperator {
|
func NewIPLibraryOperator() *IPLibraryOperator {
|
||||||
|
|||||||
@@ -138,10 +138,11 @@ func (this *IPListDAO) FindIPListCacheable(tx *dbs.Tx, listId int64) (*IPList, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateIPList 创建名单
|
// CreateIPList 创建名单
|
||||||
func (this *IPListDAO) CreateIPList(tx *dbs.Tx, userId int64, listType ipconfigs.IPListType, name string, code string, timeoutJSON []byte, description string, isPublic bool, isGlobal bool) (int64, error) {
|
func (this *IPListDAO) CreateIPList(tx *dbs.Tx, userId int64, serverId int64, listType ipconfigs.IPListType, name string, code string, timeoutJSON []byte, description string, isPublic bool, isGlobal bool) (int64, error) {
|
||||||
op := NewIPListOperator()
|
var op = NewIPListOperator()
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
|
op.ServerId = serverId
|
||||||
op.State = IPListStateEnabled
|
op.State = IPListStateEnabled
|
||||||
op.Type = listType
|
op.Type = listType
|
||||||
op.Name = name
|
op.Name = name
|
||||||
@@ -164,7 +165,7 @@ func (this *IPListDAO) UpdateIPList(tx *dbs.Tx, listId int64, name string, code
|
|||||||
if listId <= 0 {
|
if listId <= 0 {
|
||||||
return errors.New("invalid listId")
|
return errors.New("invalid listId")
|
||||||
}
|
}
|
||||||
op := NewIPListOperator()
|
var op = NewIPListOperator()
|
||||||
op.Id = listId
|
op.Id = listId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.Code = code
|
op.Code = code
|
||||||
@@ -189,26 +190,25 @@ func (this *IPListDAO) CheckUserIPList(tx *dbs.Tx, userId int64, listId int64) e
|
|||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
ok, err := this.Query(tx).
|
// 获取名单信息
|
||||||
|
listOne, err := this.Query(tx).
|
||||||
Pk(listId).
|
Pk(listId).
|
||||||
Attr("userId", userId).
|
Result("userId", "serverId").
|
||||||
Exist()
|
Find()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if ok {
|
if listOne == nil {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
var list = listOne.(*IPList)
|
||||||
|
if int64(list.UserId) == userId {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否被用户的服务所使用
|
var serverId = int64(list.ServerId)
|
||||||
policyIds, err := SharedHTTPFirewallPolicyDAO.FindEnabledFirewallPolicyIdsWithIPListId(tx, listId)
|
if serverId > 0 {
|
||||||
if err != nil {
|
return SharedServerDAO.CheckUserServer(tx, userId, serverId)
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, policyId := range policyIds {
|
|
||||||
if SharedHTTPFirewallPolicyDAO.CheckUserFirewallPolicy(tx, userId, policyId) == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
|
|||||||
@@ -20,6 +20,39 @@ func TestIPListDAO_IncreaseVersion(t *testing.T) {
|
|||||||
t.Log("version:", version)
|
t.Log("version:", version)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIPListDAO_CheckUserIPList(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var tx *dbs.Tx
|
||||||
|
|
||||||
|
{
|
||||||
|
err := NewIPListDAO().CheckUserIPList(tx, 1, 100)
|
||||||
|
if err == ErrNotFound {
|
||||||
|
t.Log("not found")
|
||||||
|
} else {
|
||||||
|
t.Log(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
err := NewIPListDAO().CheckUserIPList(tx, 1, 85)
|
||||||
|
if err == ErrNotFound {
|
||||||
|
t.Log("not found")
|
||||||
|
} else {
|
||||||
|
t.Log(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
err := NewIPListDAO().CheckUserIPList(tx, 1, 17)
|
||||||
|
if err == ErrNotFound {
|
||||||
|
t.Log("not found")
|
||||||
|
} else {
|
||||||
|
t.Log(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkIPListDAO_IncreaseVersion(b *testing.B) {
|
func BenchmarkIPListDAO_IncreaseVersion(b *testing.B) {
|
||||||
runtime.GOMAXPROCS(1)
|
runtime.GOMAXPROCS(1)
|
||||||
|
|
||||||
@@ -32,3 +65,4 @@ func BenchmarkIPListDAO_IncreaseVersion(b *testing.B) {
|
|||||||
_, _ = dao.IncreaseVersion(tx)
|
_, _ = dao.IncreaseVersion(tx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user