Compare commits
340 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4bd7ec9871 | ||
|
|
95cfad60c4 | ||
|
|
7f51d451f7 | ||
|
|
a4fb0fd795 | ||
|
|
c02928dd44 | ||
|
|
e70c49d407 | ||
|
|
e1c1984fd4 | ||
|
|
f801d304c6 | ||
|
|
178a38c6d9 | ||
|
|
e356707db7 | ||
|
|
8d3043d0fe | ||
|
|
1e494bd1fd | ||
|
|
b726c8d589 | ||
|
|
e71e80703d | ||
|
|
c9b666e5bc | ||
|
|
874139ea07 | ||
|
|
05d79ad606 | ||
|
|
13c78a5fec | ||
|
|
f3e3824b7d | ||
|
|
67473c2dcf | ||
|
|
3921c547be | ||
|
|
3e0d2fda6a | ||
|
|
7ff6c0c18b | ||
|
|
e76464673a | ||
|
|
1ab849d9b0 | ||
|
|
781c851571 | ||
|
|
0b19d93a47 | ||
|
|
2856f7716b | ||
|
|
c9ba24dc96 | ||
|
|
a660fb1f42 | ||
|
|
8586ad6478 | ||
|
|
0fab6fecfe | ||
|
|
d752bb08c7 | ||
|
|
6f845f36c9 | ||
|
|
66bc60a47c | ||
|
|
1c048da1f0 | ||
|
|
da8aa20f83 | ||
|
|
14315923d8 | ||
|
|
3d3228fe96 | ||
|
|
173ac5a8aa | ||
|
|
e9c5d7e7cf | ||
|
|
194127dce9 | ||
|
|
86cb7e9d41 | ||
|
|
b2774de6a2 | ||
|
|
3a4722b701 | ||
|
|
028aea4e3d | ||
|
|
c3dd97a7c1 | ||
|
|
d527fcdd78 | ||
|
|
d6f311e057 | ||
|
|
991e08fa71 | ||
|
|
06b44dc101 | ||
|
|
2d94b994fa | ||
|
|
c036186dde | ||
|
|
bc2ad13037 | ||
|
|
bfa04856aa | ||
|
|
feb1068441 | ||
|
|
181a4d05b0 | ||
|
|
c449265e05 | ||
|
|
e778616b5c | ||
|
|
dad5be2670 | ||
|
|
414afd17b8 | ||
|
|
86a806bca2 | ||
|
|
faed7420a7 | ||
|
|
ae14ff4f9f | ||
|
|
99e1658fdf | ||
|
|
e79264eefc | ||
|
|
dd0e26e7bc | ||
|
|
342c4bfbc2 | ||
|
|
7d2b8fd4c8 | ||
|
|
6426622992 | ||
|
|
3d154411de | ||
|
|
241f41e900 | ||
|
|
6c3d24d895 | ||
|
|
b2a0204f6b | ||
|
|
a9d71652b7 | ||
|
|
1b6bfb33d6 | ||
|
|
aec0d8d681 | ||
|
|
e38871b52d | ||
|
|
c1b4551dd1 | ||
|
|
d479140f87 | ||
|
|
881bb89ac0 | ||
|
|
0e4158f600 | ||
|
|
57bbd77ae5 | ||
|
|
6906b3094b | ||
|
|
aec28b5087 | ||
|
|
b59ed1f73e | ||
|
|
2a83f61bdd | ||
|
|
4f21d60ca4 | ||
|
|
0a6111b2e5 | ||
|
|
4b425e1698 | ||
|
|
bee7da807b | ||
|
|
a906a7db06 | ||
|
|
ea62cf0ff7 | ||
|
|
72e0c55f5d | ||
|
|
3a88f23181 | ||
|
|
967c9080fb | ||
|
|
f44b9434ad | ||
|
|
e21a3c5f8c | ||
|
|
88dae56b6c | ||
|
|
40c3475306 | ||
|
|
318c8dd566 | ||
|
|
a5f30b1573 | ||
|
|
a8ec959c70 | ||
|
|
c393b2f480 | ||
|
|
548f56f8f0 | ||
|
|
9aa71365b9 | ||
|
|
292fb72a26 | ||
|
|
e5315c3b8d | ||
|
|
a4dd7bb75a | ||
|
|
53ef0f3fb2 | ||
|
|
6af8bff802 | ||
|
|
d849f7440a | ||
|
|
425c0ec44c | ||
|
|
aad0b01581 | ||
|
|
bc2c3dfa0b | ||
|
|
0fe76430c6 | ||
|
|
979ff4c44e | ||
|
|
b0b6b5984f | ||
|
|
5d4da6cccb | ||
|
|
912ffa062f | ||
|
|
1db4661c75 | ||
|
|
f7dd9e3f39 | ||
|
|
0cc74b920e | ||
|
|
51c3807d01 | ||
|
|
c2c42ca2b7 | ||
|
|
2a6db6ebfe | ||
|
|
30d8edbdcf | ||
|
|
177afafe12 | ||
|
|
98765b6e2a | ||
|
|
e4e0aab010 | ||
|
|
ed87b4e2a9 | ||
|
|
337eb36d25 | ||
|
|
c44e40d72d | ||
|
|
2e8ba831a1 | ||
|
|
a706c2a5a5 | ||
|
|
093826222a | ||
|
|
0d7b487afc | ||
|
|
8de17b6d9c | ||
|
|
49d217a883 | ||
|
|
4827555899 | ||
|
|
41f3825ee2 | ||
|
|
096aa153ab | ||
|
|
8f8b611ac1 | ||
|
|
08b1c038f1 | ||
|
|
934b10a254 | ||
|
|
4947f13416 | ||
|
|
84198d5948 | ||
|
|
d4a04bc798 | ||
|
|
c94b3c26c1 | ||
|
|
5655f89ba6 | ||
|
|
c568ad3e9a | ||
|
|
d2f532447d | ||
|
|
ec97feab28 | ||
|
|
316cd36f71 | ||
|
|
702a0f1ecf | ||
|
|
2d2b7b7bff | ||
|
|
6f8c5a8e99 | ||
|
|
afb35953e7 | ||
|
|
2a1f78a440 | ||
|
|
fb084a9f48 | ||
|
|
e7f620d28f | ||
|
|
2f64d713e8 | ||
|
|
86bf316468 | ||
|
|
bf320271d4 | ||
|
|
150a63fe98 | ||
|
|
1d1ff11eb7 | ||
|
|
55eecce416 | ||
|
|
5f822062da | ||
|
|
ee2c253e7d | ||
|
|
5b0adb4b84 | ||
|
|
8c72540a6e | ||
|
|
7f811997a9 | ||
|
|
ca94e31451 | ||
|
|
320d381bd9 | ||
|
|
c8057457cc | ||
|
|
c78c4d58ff | ||
|
|
d1f8e7e757 | ||
|
|
5386b30eba | ||
|
|
8ceb1334cd | ||
|
|
023e563de1 | ||
|
|
39e6d11d71 | ||
|
|
a5a9117ce0 | ||
|
|
e95b0bd9a6 | ||
|
|
9699a9adad | ||
|
|
67729abd13 | ||
|
|
4c7ebce97a | ||
|
|
44e7ce9f79 | ||
|
|
3468fcf8a6 | ||
|
|
3522c22a28 | ||
|
|
333a9c6611 | ||
|
|
e3426a84e2 | ||
|
|
a25b0e6c9d | ||
|
|
c271cadabd | ||
|
|
428bb7eb0f | ||
|
|
6ae9f447b6 | ||
|
|
7cc503b698 | ||
|
|
c66e28cb9d | ||
|
|
e5109b24d4 | ||
|
|
695b8482de | ||
|
|
d0b908bcaa | ||
|
|
3de25d4fe1 | ||
|
|
07194855bf | ||
|
|
d0f1eb13ee | ||
|
|
a0930bfd74 | ||
|
|
08cff8affc | ||
|
|
02132e9262 | ||
|
|
61b6a49885 | ||
|
|
896e54ebe8 | ||
|
|
1b36bad60a | ||
|
|
fc14800d70 | ||
|
|
fa61f277e4 | ||
|
|
9117309472 | ||
|
|
6bb2977d59 | ||
|
|
df9dce76cb | ||
|
|
4cb9c85a1c | ||
|
|
f4f5389ffb | ||
|
|
5d336eb77d | ||
|
|
c552eb3b0e | ||
|
|
455952e9e4 | ||
|
|
7132401c7f | ||
|
|
a4dddfb139 | ||
|
|
7ef32bad97 | ||
|
|
732513a644 | ||
|
|
756cf4a9ae | ||
|
|
a15a630265 | ||
|
|
3fab1b8294 | ||
|
|
215635f429 | ||
|
|
dbb1ae180b | ||
|
|
e8d4d01d85 | ||
|
|
6593989a84 | ||
|
|
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 |
1
.gitignore
vendored
1
.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.
|
||||||
100
build/build.sh
100
build/build.sh
@@ -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,48 @@ 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/
|
|
||||||
rm -f $DIST/resources/ipdata/ip2region/global_region.csv
|
|
||||||
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 ..."
|
if [ $TAG = "plus" ]; then
|
||||||
architects=("amd64" "386" "arm64")
|
echo "building dns node installer ..."
|
||||||
for arch in "${architects[@]}"; do
|
architects=("amd64" "arm64")
|
||||||
# TODO support arm, mips ...
|
for arch in "${architects[@]}"; do
|
||||||
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
|
# TODO support arm, mips ...
|
||||||
done
|
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
|
||||||
|
fi
|
||||||
|
|
||||||
# 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 +135,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"
|
||||||
|
|||||||
@@ -9,3 +9,7 @@ dbs:
|
|||||||
prefix: "edge"
|
prefix: "edge"
|
||||||
models:
|
models:
|
||||||
package: internal/web/models
|
package: internal/web/models
|
||||||
|
|
||||||
|
|
||||||
|
fields:
|
||||||
|
bool: [ "uamIsOn", "followPort", "requestHostExcludingPort", "autoRemoteStart", "autoInstallNftables" ]
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@@ -2,8 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/apps"
|
"github.com/TeaOSLab/EdgeAPI/internal/apps"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/nodes"
|
"github.com/TeaOSLab/EdgeAPI/internal/nodes"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/setup"
|
"github.com/TeaOSLab/EdgeAPI/internal/setup"
|
||||||
@@ -21,14 +23,15 @@ 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{}
|
var result = maps.Map{}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result["isOk"] = false
|
result["isOk"] = false
|
||||||
result["error"] = err.Error()
|
result["error"] = err.Error()
|
||||||
@@ -70,6 +73,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
fmt.Println("done")
|
fmt.Println("done")
|
||||||
})
|
})
|
||||||
|
app.On("reset", func() {
|
||||||
|
err := configs.ResetAPIConfig()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("[ERROR]reset failed: " + err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println("done")
|
||||||
|
})
|
||||||
app.On("goman", func() {
|
app.On("goman", func() {
|
||||||
var sock = gosock.NewTmpSock(teaconst.ProcessName)
|
var sock = gosock.NewTmpSock(teaconst.ProcessName)
|
||||||
reply, err := sock.Send(&gosock.Command{Code: "goman"})
|
reply, err := sock.Send(&gosock.Command{Code: "goman"})
|
||||||
@@ -122,6 +133,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 := os.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()
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/installers/helpers"
|
||||||
"github.com/iwind/gosock/pkg/gosock"
|
"github.com/iwind/gosock/pkg/gosock"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -51,7 +51,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
unzip := utils.NewUnzip(zipPath, targetPath)
|
unzip := helpers.NewUnzip(zipPath, targetPath)
|
||||||
err := unzip.Run()
|
err := unzip.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
stderr("ERROR: " + err.Error())
|
stderr("ERROR: " + err.Error())
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
// 注意这里的依赖文件应该最小化,从而使编译后的文件最小化
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/installers/helpers"
|
||||||
"github.com/iwind/gosock/pkg/gosock"
|
"github.com/iwind/gosock/pkg/gosock"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -51,7 +52,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
unzip := utils.NewUnzip(zipPath, targetPath)
|
unzip := helpers.NewUnzip(zipPath, targetPath)
|
||||||
err := unzip.Run()
|
err := unzip.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
stderr("ERROR: " + err.Error())
|
stderr("ERROR: " + err.Error())
|
||||||
|
|||||||
@@ -1,193 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"github.com/iwind/TeaGo/lists"
|
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// 导入数据
|
|
||||||
if lists.ContainsString(os.Args, "import") {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
data, err := ioutil.ReadFile(Tea.Root + "/resources/ipdata/ip2region/global_region.csv")
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(data) == 0 {
|
|
||||||
logs.Println("[ERROR]file content should not be empty")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
lines := bytes.Split(data, []byte{'\n'})
|
|
||||||
for _, line := range lines {
|
|
||||||
line = bytes.TrimSpace(line)
|
|
||||||
if len(line) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
s := string(line)
|
|
||||||
reg := regexp.MustCompile(`(?U)(\d+),(\d+),(.+),(\d+),`)
|
|
||||||
if !reg.MatchString(s) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
result := reg.FindStringSubmatch(s)
|
|
||||||
dataId := result[1]
|
|
||||||
parentDataId := result[2]
|
|
||||||
name := result[3]
|
|
||||||
level := result[4]
|
|
||||||
|
|
||||||
switch level {
|
|
||||||
case "1": // 国家|地区
|
|
||||||
countryId, err := regions.SharedRegionCountryDAO.FindCountryIdWithDataId(nil, dataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if countryId == 0 {
|
|
||||||
logs.Println("creating country or region ", name)
|
|
||||||
_, err = regions.SharedRegionCountryDAO.CreateCountry(nil, name, dataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case "2": // 省份|地区
|
|
||||||
provinceId, err := regions.SharedRegionProvinceDAO.FindProvinceIdWithDataId(nil, dataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if provinceId == 0 {
|
|
||||||
logs.Println("creating province", name)
|
|
||||||
|
|
||||||
countryId, err := regions.SharedRegionCountryDAO.FindCountryIdWithDataId(nil, parentDataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if countryId == 0 {
|
|
||||||
logs.Println("[ERROR]can not find country from data id '" + parentDataId + "'")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = regions.SharedRegionProvinceDAO.CreateProvince(nil, countryId, name, dataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case "3": // 城市
|
|
||||||
cityId, err := regions.SharedRegionCityDAO.FindCityWithDataId(nil, dataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if cityId == 0 {
|
|
||||||
logs.Println("creating city", name)
|
|
||||||
|
|
||||||
provinceId, err := regions.SharedRegionProvinceDAO.FindProvinceIdWithDataId(nil, parentDataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_, err = regions.SharedRegionCityDAO.CreateCity(nil, provinceId, name, dataId)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logs.Println("done")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查数据
|
|
||||||
if lists.ContainsString(os.Args, "check") {
|
|
||||||
dbs.NotifyReady()
|
|
||||||
|
|
||||||
data, err := ioutil.ReadFile(Tea.Root + "/resources/ipdata/ip2region/ip.merge.txt")
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(data) == 0 {
|
|
||||||
logs.Println("[ERROR]file should not be empty")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
lines := bytes.Split(data, []byte("\n"))
|
|
||||||
for index, line := range lines {
|
|
||||||
s := string(bytes.TrimSpace(line))
|
|
||||||
if len(s) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
pieces := strings.Split(s, "|")
|
|
||||||
countryName := pieces[2]
|
|
||||||
provinceName := pieces[4]
|
|
||||||
providerName := pieces[6]
|
|
||||||
|
|
||||||
// 记录provider
|
|
||||||
if len(providerName) > 0 && providerName != "0" {
|
|
||||||
providerId, err := regions.SharedRegionProviderDAO.FindProviderIdWithNameCacheable(nil, providerName)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]find provider id failed: " + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if providerId == 0 {
|
|
||||||
logs.Println("creating new provider '"+providerName+"' ... ", index, "line")
|
|
||||||
_, err = regions.SharedRegionProviderDAO.CreateProvider(nil, providerName)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("create new provider failed: " + providerName)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logs.Println("created new provider '" + providerName + "'")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if lists.ContainsString([]string{"0", "欧洲", "北美地区", "法国南部领地", "非洲地区", "亚太地区"}, countryName) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查国家
|
|
||||||
countryId, err := regions.SharedRegionCountryDAO.FindCountryIdWithNameCacheable(nil, countryName)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if countryId == 0 {
|
|
||||||
logs.Println("[ERROR]can not find country '"+countryName+"', index: ", index, "data: "+s)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查省份
|
|
||||||
if countryName == "中国" {
|
|
||||||
if lists.ContainsString([]string{"0"}, provinceName) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
provinceId, err := regions.SharedRegionProvinceDAO.FindProvinceIdWithNameCacheable(nil, countryId, provinceName)
|
|
||||||
if err != nil {
|
|
||||||
logs.Println("[ERROR]" + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if provinceId == 0 {
|
|
||||||
logs.Println("[ERROR]can not find province '"+provinceName+"', index: ", index, "data: "+s)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logs.Println("done")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"go/format"
|
"go/format"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -19,7 +18,7 @@ func main() {
|
|||||||
fmt.Println("[ERROR]" + err.Error())
|
fmt.Println("[ERROR]" + err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
results, err := setup.NewSQLDump().Dump(db)
|
results, err := setup.NewSQLDump().Dump(db, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("[ERROR]" + err.Error())
|
fmt.Println("[ERROR]" + err.Error())
|
||||||
return
|
return
|
||||||
@@ -67,7 +66,7 @@ func init() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ioutil.WriteFile(sqlFile, dst, 0666)
|
err = os.WriteFile(sqlFile, dst, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("[ERROR]write file failed: " + err.Error())
|
fmt.Println("[ERROR]write file failed: " + err.Error())
|
||||||
return
|
return
|
||||||
|
|||||||
1
dist/.gitignore
vendored
1
dist/.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
*.zip
|
*.zip
|
||||||
|
edge-api
|
||||||
38
go.mod
38
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,15 +12,39 @@ 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/miekg/dns v1.1.43
|
||||||
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
|
||||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292
|
github.com/smartwalle/alipay/v3 v3.1.7
|
||||||
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8
|
golang.org/x/crypto v0.1.0
|
||||||
|
golang.org/x/net v0.1.0
|
||||||
|
golang.org/x/sys v0.1.0
|
||||||
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/cespare/xxhash v1.1.0 // 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/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/smartwalle/crypto4go v1.0.2 // 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/text v0.4.0 // 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
|
||||||
)
|
)
|
||||||
|
|||||||
40
go.sum
40
go.sum
@@ -237,13 +237,10 @@ github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df/go.mod h1:QMZY7/J/KSQEhK
|
|||||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||||
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-20220811034530-657e3f15b79e h1:cw4b6ecXdXvLd45YSstD8r9ClcnVK4ljZMZCept2aOk=
|
||||||
github.com/iwind/TeaGo v0.0.0-20220322141208-22f88d04004d h1:e8fkTKras/RXQWECApM9fKlFWujjYjEClpshkmZmtYg=
|
github.com/iwind/TeaGo v0.0.0-20220811034530-657e3f15b79e/go.mod h1:fi/Pq+/5m2HZoseM+39dMF57ANXRt6w4PkGu3NXPc5s=
|
||||||
github.com/iwind/TeaGo v0.0.0-20220322141208-22f88d04004d/go.mod h1:HRHK0zoC/og3c9/hKosD9yYVMTnnzm3PgXUdhRYHaLc=
|
github.com/iwind/gosock v0.0.0-20220505115348-f88412125a62 h1:HJH6RDheAY156DnIfJSD/bEvqyXzsZuE2gzs8PuUjoo=
|
||||||
github.com/iwind/TeaGo v0.0.0-20220408064305-92be81dc2f7c h1:ugjYZ74FJGWlfDKKraNgMyDTeS4vbXHe89JGUVQIJMo=
|
github.com/iwind/gosock v0.0.0-20220505115348-f88412125a62/go.mod h1:H5Q7SXwbx3a97ecJkaS2sD77gspzE7HFUafBO0peEyA=
|
||||||
github.com/iwind/TeaGo v0.0.0-20220408064305-92be81dc2f7c/go.mod h1:HRHK0zoC/og3c9/hKosD9yYVMTnnzm3PgXUdhRYHaLc=
|
|
||||||
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/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=
|
||||||
@@ -408,6 +405,10 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
|
|||||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||||
github.com/skratchdot/open-golang v0.0.0-20160302144031-75fb7ed4208c/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
|
github.com/skratchdot/open-golang v0.0.0-20160302144031-75fb7ed4208c/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
|
||||||
|
github.com/smartwalle/alipay/v3 v3.1.7 h1:J4U5slABafKVD/b9gPCZe/3HAPB8Pa2NOYOPcugEJBo=
|
||||||
|
github.com/smartwalle/alipay/v3 v3.1.7/go.mod h1:cZUMCCnsux9YAxA0/f3PWUR+7wckWtE1BqxbVRtGij0=
|
||||||
|
github.com/smartwalle/crypto4go v1.0.2 h1:9DUEOOsPhmp00438L4oBdcL8EZG1zumecft5bWj5phI=
|
||||||
|
github.com/smartwalle/crypto4go v1.0.2/go.mod h1:LQ7vCZIb7BE5+MuMtJBuO8ORkkQ01m4DXDBWPzLbkMY=
|
||||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||||
github.com/smartystreets/assertions v1.0.1 h1:voD4ITNjPL5jjBfgR/r8fPIIBrliWrWHeiJApdr3r4w=
|
github.com/smartystreets/assertions v1.0.1 h1:voD4ITNjPL5jjBfgR/r8fPIIBrliWrWHeiJApdr3r4w=
|
||||||
github.com/smartystreets/assertions v1.0.1/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM=
|
github.com/smartystreets/assertions v1.0.1/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM=
|
||||||
@@ -480,6 +481,7 @@ golang.org/x/crypto v0.0.0-20180621125126-a49355c7e3f8/go.mod h1:6SG95UA2DQfeDnf
|
|||||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||||
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
@@ -491,8 +493,8 @@ golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPh
|
|||||||
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
||||||
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
|
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
|
||||||
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 h1:f+lwQ+GtmgoY+A2YaQxlSOnDjXcQ7ZRLWOHbC6HtRqE=
|
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
|
||||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
|
||||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||||
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||||
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
|
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
|
||||||
@@ -559,9 +561,8 @@ 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.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
|
||||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
|
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
|
||||||
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=
|
||||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||||
@@ -630,14 +631,12 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 h1:OH54vjqzRWmbJ62fjuhxy7AxFFgoHN0/DPc/UrL8cAs=
|
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
|
||||||
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
|
||||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
@@ -646,8 +645,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|||||||
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||||
@@ -789,8 +788,9 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
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/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)
|
||||||
|
|||||||
@@ -5,12 +5,18 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
|
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/go-acme/lego/v4/challenge/dns01"
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
||||||
|
"github.com/iwind/TeaGo/lists"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DNSProvider struct {
|
type DNSProvider struct {
|
||||||
raw dnsclients.ProviderInterface
|
raw dnsclients.ProviderInterface
|
||||||
dnsDomain string
|
dnsDomain string
|
||||||
|
|
||||||
|
locker sync.Mutex
|
||||||
|
deletedRecordNames []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDNSProvider(raw dnsclients.ProviderInterface, dnsDomain string) *DNSProvider {
|
func NewDNSProvider(raw dnsclients.ProviderInterface, dnsDomain string) *DNSProvider {
|
||||||
@@ -21,39 +27,47 @@ func NewDNSProvider(raw dnsclients.ProviderInterface, dnsDomain string) *DNSProv
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *DNSProvider) Present(domain, token, keyAuth string) error {
|
func (this *DNSProvider) Present(domain, token, keyAuth string) error {
|
||||||
|
_ = os.Setenv("LEGO_DISABLE_CNAME_SUPPORT", "true")
|
||||||
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
||||||
|
|
||||||
// 设置记录
|
// 设置记录
|
||||||
index := strings.Index(fqdn, "."+this.dnsDomain)
|
var index = strings.Index(fqdn, "."+this.dnsDomain)
|
||||||
if index < 0 {
|
if index < 0 {
|
||||||
return errors.New("invalid fqdn value")
|
return errors.New("invalid fqdn value")
|
||||||
}
|
}
|
||||||
recordName := fqdn[:index]
|
var recordName = fqdn[:index]
|
||||||
record, err := this.raw.QueryRecord(this.dnsDomain, recordName, dnstypes.RecordTypeTXT)
|
|
||||||
if err != nil {
|
// 先删除老的
|
||||||
return errors.New("query DNS record failed: " + err.Error())
|
this.locker.Lock()
|
||||||
|
var wasDeleted = lists.ContainsString(this.deletedRecordNames, recordName)
|
||||||
|
this.locker.Unlock()
|
||||||
|
|
||||||
|
if !wasDeleted {
|
||||||
|
records, err := this.raw.QueryRecords(this.dnsDomain, recordName, dnstypes.RecordTypeTXT)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("query DNS record failed: " + err.Error())
|
||||||
|
}
|
||||||
|
for _, record := range records {
|
||||||
|
err = this.raw.DeleteRecord(this.dnsDomain, record)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.locker.Lock()
|
||||||
|
this.deletedRecordNames = append(this.deletedRecordNames, recordName)
|
||||||
|
this.locker.Unlock()
|
||||||
}
|
}
|
||||||
if record == nil {
|
|
||||||
err = this.raw.AddRecord(this.dnsDomain, &dnstypes.Record{
|
// 添加新的
|
||||||
Id: "",
|
err := this.raw.AddRecord(this.dnsDomain, &dnstypes.Record{
|
||||||
Name: recordName,
|
Id: "",
|
||||||
Type: dnstypes.RecordTypeTXT,
|
Name: recordName,
|
||||||
Value: value,
|
Type: dnstypes.RecordTypeTXT,
|
||||||
Route: this.raw.DefaultRoute(),
|
Value: value,
|
||||||
})
|
Route: this.raw.DefaultRoute(),
|
||||||
if err != nil {
|
})
|
||||||
return errors.New("create DNS record failed: " + err.Error())
|
if err != nil {
|
||||||
}
|
return errors.New("create DNS record failed: " + err.Error())
|
||||||
} else {
|
|
||||||
err = this.raw.UpdateRecord(this.dnsDomain, record, &dnstypes.Record{
|
|
||||||
Name: recordName,
|
|
||||||
Type: dnstypes.RecordTypeTXT,
|
|
||||||
Value: value,
|
|
||||||
Route: this.raw.DefaultRoute(),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("update DNS record failed: " + err.Error())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -9,30 +9,11 @@ type Provider struct {
|
|||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
APIURL string `json:"apiURL"`
|
APIURL string `json:"apiURL"`
|
||||||
|
TestAPIURL string `json:"testAPIURL"`
|
||||||
RequireEAB bool `json:"requireEAB"`
|
RequireEAB bool `json:"requireEAB"`
|
||||||
EABDescription string `json:"eabDescription"`
|
EABDescription string `json:"eabDescription"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindAllProviders() []*Provider {
|
|
||||||
return []*Provider{
|
|
||||||
{
|
|
||||||
Name: "Let's Encrypt",
|
|
||||||
Code: DefaultProviderCode,
|
|
||||||
Description: "非盈利组织Let's Encrypt提供的免费证书。",
|
|
||||||
APIURL: "https://acme-v02.api.letsencrypt.org/directory",
|
|
||||||
RequireEAB: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "ZeroSSL",
|
|
||||||
Code: "zerossl",
|
|
||||||
Description: "相关文档 <a href=\"https://zerossl.com/documentation/acme/\" target=\"_blank\">https://zerossl.com/documentation/acme/</a>。",
|
|
||||||
APIURL: "https://acme.zerossl.com/v2/DV90",
|
|
||||||
RequireEAB: true,
|
|
||||||
EABDescription: "在官网<a href=\"https://app.zerossl.com/developer\" target=\"_blank\">[Developer]</a>页面底部点击\"Generate\"按钮生成。",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func FindProviderWithCode(code string) *Provider {
|
func FindProviderWithCode(code string) *Provider {
|
||||||
for _, provider := range FindAllProviders() {
|
for _, provider := range FindAllProviders() {
|
||||||
if provider.Code == code {
|
if provider.Code == code {
|
||||||
|
|||||||
24
internal/acme/providers_ext.go
Normal file
24
internal/acme/providers_ext.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||||
|
//go:build !plus
|
||||||
|
|
||||||
|
package acme
|
||||||
|
|
||||||
|
func FindAllProviders() []*Provider {
|
||||||
|
return []*Provider{
|
||||||
|
{
|
||||||
|
Name: "Let's Encrypt",
|
||||||
|
Code: DefaultProviderCode,
|
||||||
|
Description: "非盈利组织Let's Encrypt提供的免费证书。",
|
||||||
|
APIURL: "https://acme-v02.api.letsencrypt.org/directory",
|
||||||
|
RequireEAB: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "ZeroSSL",
|
||||||
|
Code: "zerossl",
|
||||||
|
Description: "相关文档 <a href=\"https://zerossl.com/documentation/acme/\" target=\"_blank\">https://zerossl.com/documentation/acme/</a>。",
|
||||||
|
APIURL: "https://acme.zerossl.com/v2/DV90",
|
||||||
|
RequireEAB: true,
|
||||||
|
EABDescription: "在官网<a href=\"https://app.zerossl.com/developer\" target=\"_blank\">[Developer]</a>页面底部点击\"Generate\"按钮生成。",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,7 +8,8 @@ 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"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -40,6 +41,7 @@ func (this *Request) Run() (certData []byte, keyData []byte, err error) {
|
|||||||
}
|
}
|
||||||
if this.task.Provider.RequireEAB && this.task.Account == nil {
|
if this.task.Provider.RequireEAB && this.task.Account == nil {
|
||||||
err = errors.New("account should not be nil when provider require EAB")
|
err = errors.New("account should not be nil when provider require EAB")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch this.task.AuthType {
|
switch this.task.AuthType {
|
||||||
@@ -55,7 +57,9 @@ 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)
|
if !Tea.IsTesting() {
|
||||||
|
acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.task.User == nil {
|
if this.task.User == nil {
|
||||||
@@ -75,7 +79,7 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
config := lego.NewConfig(this.task.User)
|
var config = lego.NewConfig(this.task.User)
|
||||||
config.Certificate.KeyType = certcrypto.RSA2048
|
config.Certificate.KeyType = certcrypto.RSA2048
|
||||||
config.CADirURL = this.task.Provider.APIURL
|
config.CADirURL = this.task.Provider.APIURL
|
||||||
config.UserAgent = teaconst.ProductName + "/" + teaconst.Version
|
config.UserAgent = teaconst.ProductName + "/" + teaconst.Version
|
||||||
@@ -86,7 +90,7 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 注册用户
|
// 注册用户
|
||||||
resource := this.task.User.GetRegistration()
|
var resource = this.task.User.GetRegistration()
|
||||||
if resource != nil {
|
if resource != nil {
|
||||||
resource, err = client.Registration.QueryRegistration()
|
resource, err = client.Registration.QueryRegistration()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -124,7 +128,7 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 申请证书
|
// 申请证书
|
||||||
request := certificate.ObtainRequest{
|
var request = certificate.ObtainRequest{
|
||||||
Domains: this.task.Domains,
|
Domains: this.task.Domains,
|
||||||
Bundle: true,
|
Bundle: true,
|
||||||
}
|
}
|
||||||
@@ -138,7 +142,9 @@ 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)
|
if !Tea.IsTesting() {
|
||||||
|
acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if this.task.User == nil {
|
if this.task.User == nil {
|
||||||
@@ -146,7 +152,7 @@ func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
config := lego.NewConfig(this.task.User)
|
var config = lego.NewConfig(this.task.User)
|
||||||
config.Certificate.KeyType = certcrypto.RSA2048
|
config.Certificate.KeyType = certcrypto.RSA2048
|
||||||
config.CADirURL = this.task.Provider.APIURL
|
config.CADirURL = this.task.Provider.APIURL
|
||||||
config.UserAgent = teaconst.ProductName + "/" + teaconst.Version
|
config.UserAgent = teaconst.ProductName + "/" + teaconst.Version
|
||||||
@@ -157,7 +163,7 @@ func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 注册用户
|
// 注册用户
|
||||||
resource := this.task.User.GetRegistration()
|
var resource = this.task.User.GetRegistration()
|
||||||
if resource != nil {
|
if resource != nil {
|
||||||
resource, err = client.Registration.QueryRegistration()
|
resource, err = client.Registration.QueryRegistration()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -195,7 +201,7 @@ func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 申请证书
|
// 申请证书
|
||||||
request := certificate.ObtainRequest{
|
var request = certificate.ObtainRequest{
|
||||||
Domains: this.task.Domains,
|
Domains: this.task.Domains,
|
||||||
Bundle: true,
|
Bundle: true,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,58 @@ 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResetAPIConfig 重置配置
|
||||||
|
func ResetAPIConfig() error {
|
||||||
|
for _, filename := range []string{"api.yaml", "db.yaml"} {
|
||||||
|
// 重置 configs/api.yaml
|
||||||
|
{
|
||||||
|
var configFile = Tea.ConfigFile(filename)
|
||||||
|
stat, err := os.Stat(configFile)
|
||||||
|
if err == nil && !stat.IsDir() {
|
||||||
|
err = os.Remove(configFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重置 ~/.edge-api/api.yaml
|
||||||
|
homeDir, homeErr := os.UserHomeDir()
|
||||||
|
if homeErr == nil {
|
||||||
|
var configFile = homeDir + "/." + teaconst.ProcessName + "/" + filename
|
||||||
|
stat, err := os.Stat(configFile)
|
||||||
|
if err == nil && !stat.IsDir() {
|
||||||
|
err = os.Remove(configFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重置 /etc/edge-api/api.yaml
|
||||||
|
{
|
||||||
|
var configFile = "/etc/" + teaconst.ProcessName + "/" + filename
|
||||||
|
stat, err := os.Stat(configFile)
|
||||||
|
if err == nil && !stat.IsDir() {
|
||||||
|
err = os.Remove(configFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package teaconst
|
package teaconst
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = "0.4.7"
|
Version = "0.6.0"
|
||||||
|
|
||||||
ProductName = "Edge API"
|
ProductName = "Edge API"
|
||||||
ProcessName = "edge-api"
|
ProcessName = "edge-api"
|
||||||
@@ -18,13 +18,8 @@ const (
|
|||||||
|
|
||||||
// 其他节点版本号,用来检测是否有需要升级的节点
|
// 其他节点版本号,用来检测是否有需要升级的节点
|
||||||
|
|
||||||
NodeVersion = "0.4.7"
|
NodeVersion = "0.6.0"
|
||||||
UserNodeVersion = "0.3.3"
|
|
||||||
AuthorityNodeVersion = "0.0.2"
|
|
||||||
MonitorNodeVersion = "0.0.3"
|
|
||||||
DNSNodeVersion = "0.2.2"
|
|
||||||
ReportNodeVersion = "0.1.0"
|
|
||||||
|
|
||||||
// SQLVersion SQL版本号
|
// SQLVersion SQL版本号
|
||||||
SQLVersion = "8"
|
SQLVersion = "9"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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()
|
||||||
|
})
|
||||||
|
}
|
||||||
6
internal/db/models/accounts/order_method_dao_test.go
Normal file
6
internal/db/models/accounts/order_method_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package accounts_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
)
|
||||||
40
internal/db/models/accounts/order_method_model.go
Normal file
40
internal/db/models/accounts/order_method_model.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
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"` // 参数
|
||||||
|
ClientType string `field:"clientType"` // 客户端类型
|
||||||
|
QrcodeTitle string `field:"qrcodeTitle"` // 二维码标题
|
||||||
|
Order uint32 `field:"order"` // 排序
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderMethodOperator struct {
|
||||||
|
Id any // ID
|
||||||
|
Name any // 名称
|
||||||
|
IsOn any // 是否启用
|
||||||
|
Description any // 描述
|
||||||
|
ParentCode any // 内置的父级代号
|
||||||
|
Code any // 代号
|
||||||
|
Url any // URL
|
||||||
|
Secret any // 密钥
|
||||||
|
Params any // 参数
|
||||||
|
ClientType any // 客户端类型
|
||||||
|
QrcodeTitle any // 二维码标题
|
||||||
|
Order any // 排序
|
||||||
|
State any // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
package accounts
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type UserAccountDailyStatDAO dbs.DAO
|
|
||||||
|
|
||||||
func NewUserAccountDailyStatDAO() *UserAccountDailyStatDAO {
|
|
||||||
return dbs.NewDAO(&UserAccountDailyStatDAO{
|
|
||||||
DAOObject: dbs.DAOObject{
|
|
||||||
DB: Tea.Env,
|
|
||||||
Table: "edgeUserAccountDailyStats",
|
|
||||||
Model: new(UserAccountDailyStat),
|
|
||||||
PkName: "id",
|
|
||||||
},
|
|
||||||
}).(*UserAccountDailyStatDAO)
|
|
||||||
}
|
|
||||||
|
|
||||||
var SharedUserAccountDailyStatDAO *UserAccountDailyStatDAO
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReady(func() {
|
|
||||||
SharedUserAccountDailyStatDAO = NewUserAccountDailyStatDAO()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateDailyStat 更新当天统计数据
|
|
||||||
func (this *UserAccountDailyStatDAO) UpdateDailyStat(tx *dbs.Tx) error {
|
|
||||||
var day = timeutil.Format("Ymd")
|
|
||||||
var month = timeutil.Format("Ym")
|
|
||||||
income, err := SharedUserAccountLogDAO.SumDailyEventTypes(tx, day, userconfigs.AccountIncomeEventTypes)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
expense, err := SharedUserAccountLogDAO.SumDailyEventTypes(tx, day, userconfigs.AccountExpenseEventTypes)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if expense < 0 {
|
|
||||||
expense = -expense
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.Query(tx).
|
|
||||||
InsertOrUpdateQuickly(maps.Map{
|
|
||||||
"day": day,
|
|
||||||
"month": month,
|
|
||||||
"income": income,
|
|
||||||
"expense": expense,
|
|
||||||
}, maps.Map{
|
|
||||||
"income": income,
|
|
||||||
"expense": expense,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindDailyStats 查看按天统计
|
|
||||||
func (this *UserAccountDailyStatDAO) FindDailyStats(tx *dbs.Tx, dayFrom string, dayTo string) (result []*UserAccountDailyStat, err error) {
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Between("day", dayFrom, dayTo).
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindMonthlyStats 查看某月统计
|
|
||||||
func (this *UserAccountDailyStatDAO) FindMonthlyStats(tx *dbs.Tx, dayFrom string, dayTo string) (result []*UserAccountDailyStat, err error) {
|
|
||||||
_, err = this.Query(tx).
|
|
||||||
Result("SUM(income) AS income", "SUM(expense) AS expense", "month").
|
|
||||||
Between("day", dayFrom, dayTo).
|
|
||||||
Group("month").
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@@ -1,253 +0,0 @@
|
|||||||
package accounts
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"github.com/iwind/TeaGo/lists"
|
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReadyDone(func() {
|
|
||||||
goman.New(func() {
|
|
||||||
// 自动支付账单任务
|
|
||||||
var ticker = time.NewTicker(12 * time.Hour)
|
|
||||||
for range ticker.C {
|
|
||||||
if SharedUserAccountDAO.Instance != nil {
|
|
||||||
err := SharedUserAccountDAO.Instance.RunTx(func(tx *dbs.Tx) error {
|
|
||||||
return SharedUserAccountDAO.PayBills(tx)
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("USER_ACCOUNT_DAO", "pay bills task failed: "+err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
type UserAccountDAO dbs.DAO
|
|
||||||
|
|
||||||
func NewUserAccountDAO() *UserAccountDAO {
|
|
||||||
return dbs.NewDAO(&UserAccountDAO{
|
|
||||||
DAOObject: dbs.DAOObject{
|
|
||||||
DB: Tea.Env,
|
|
||||||
Table: "edgeUserAccounts",
|
|
||||||
Model: new(UserAccount),
|
|
||||||
PkName: "id",
|
|
||||||
},
|
|
||||||
}).(*UserAccountDAO)
|
|
||||||
}
|
|
||||||
|
|
||||||
var SharedUserAccountDAO *UserAccountDAO
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReady(func() {
|
|
||||||
SharedUserAccountDAO = NewUserAccountDAO()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindUserAccountWithUserId 根据用户ID查找用户账户
|
|
||||||
func (this *UserAccountDAO) FindUserAccountWithUserId(tx *dbs.Tx, userId int64) (*UserAccount, error) {
|
|
||||||
if userId <= 0 {
|
|
||||||
return nil, errors.New("invalid userId '" + types.String(userId) + "'")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 用户是否存在
|
|
||||||
user, err := models.SharedUserDAO.FindEnabledUser(tx, userId, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if user == nil {
|
|
||||||
return nil, errors.New("invalid userId '" + types.String(userId) + "'")
|
|
||||||
}
|
|
||||||
|
|
||||||
account, err := this.Query(tx).
|
|
||||||
Attr("userId", userId).
|
|
||||||
Find()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if account != nil {
|
|
||||||
return account.(*UserAccount), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var op = NewUserAccountOperator()
|
|
||||||
op.UserId = userId
|
|
||||||
_, err = this.SaveInt64(tx, op)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return this.FindUserAccountWithUserId(tx, userId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindUserAccountWithAccountId 根据ID查找用户账户
|
|
||||||
func (this *UserAccountDAO) FindUserAccountWithAccountId(tx *dbs.Tx, accountId int64) (*UserAccount, error) {
|
|
||||||
one, err := this.Query(tx).
|
|
||||||
Pk(accountId).
|
|
||||||
Find()
|
|
||||||
if one != nil {
|
|
||||||
return one.(*UserAccount), nil
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateUserAccount 操作用户账户
|
|
||||||
func (this *UserAccountDAO) UpdateUserAccount(tx *dbs.Tx, accountId int64, delta float32, eventType userconfigs.AccountEventType, description string, params maps.Map) error {
|
|
||||||
account, err := this.FindUserAccountWithAccountId(tx, accountId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if account == nil {
|
|
||||||
return errors.New("invalid account id '" + types.String(accountId) + "'")
|
|
||||||
}
|
|
||||||
var userId = int64(account.UserId)
|
|
||||||
var deltaFloat64 = float64(delta)
|
|
||||||
if deltaFloat64 < 0 && account.Total < -deltaFloat64 {
|
|
||||||
return errors.New("not enough account quota to decrease")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 操作账户
|
|
||||||
err = this.Query(tx).
|
|
||||||
Pk(account.Id).
|
|
||||||
Set("total", dbs.SQL("total+:delta")).
|
|
||||||
Param("delta", delta).
|
|
||||||
UpdateQuickly()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成日志
|
|
||||||
err = SharedUserAccountLogDAO.CreateAccountLog(tx, userId, accountId, delta, 0, eventType, description, params)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateUserAccountFrozen 操作用户账户冻结余额
|
|
||||||
func (this *UserAccountDAO) UpdateUserAccountFrozen(tx *dbs.Tx, userId int64, delta float32, eventType userconfigs.AccountEventType, description string, params maps.Map) error {
|
|
||||||
account, err := this.FindUserAccountWithUserId(tx, userId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
var deltaFloat64 = float64(delta)
|
|
||||||
if deltaFloat64 < 0 && account.TotalFrozen < -deltaFloat64 {
|
|
||||||
return errors.New("not enough account frozen quota to decrease")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 操作账户
|
|
||||||
err = this.Query(tx).
|
|
||||||
Pk(account.Id).
|
|
||||||
Set("totalFrozen", dbs.SQL("total+:delta")).
|
|
||||||
Param("delta", delta).
|
|
||||||
UpdateQuickly()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成日志
|
|
||||||
err = SharedUserAccountLogDAO.CreateAccountLog(tx, userId, int64(account.Id), 0, delta, eventType, description, params)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CountAllAccounts 计算所有账户数量
|
|
||||||
func (this *UserAccountDAO) CountAllAccounts(tx *dbs.Tx, keyword string) (int64, error) {
|
|
||||||
var query = this.Query(tx)
|
|
||||||
if len(keyword) > 0 {
|
|
||||||
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword))")
|
|
||||||
query.Param("keyword", keyword)
|
|
||||||
} else {
|
|
||||||
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1)")
|
|
||||||
}
|
|
||||||
return query.Count()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListAccounts 列出单页账户
|
|
||||||
func (this *UserAccountDAO) ListAccounts(tx *dbs.Tx, keyword string, offset int64, size int64) (result []*UserAccount, err error) {
|
|
||||||
var query = this.Query(tx)
|
|
||||||
if len(keyword) > 0 {
|
|
||||||
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword))")
|
|
||||||
query.Param("keyword", keyword)
|
|
||||||
} else {
|
|
||||||
query.Where("userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1)")
|
|
||||||
}
|
|
||||||
_, err = query.
|
|
||||||
DescPk().
|
|
||||||
Offset(offset).
|
|
||||||
Limit(size).
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// PayBills 尝试自动支付账单
|
|
||||||
func (this *UserAccountDAO) PayBills(tx *dbs.Tx) error {
|
|
||||||
bills, err := models.SharedUserBillDAO.FindUnpaidBills(tx, 10000)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 先支付久远的
|
|
||||||
lists.Reverse(bills)
|
|
||||||
|
|
||||||
for _, bill := range bills {
|
|
||||||
if bill.Amount <= 0 {
|
|
||||||
err = models.SharedUserBillDAO.UpdateUserBillIsPaid(tx, int64(bill.Id), true)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
account, err := SharedUserAccountDAO.FindUserAccountWithUserId(tx, int64(bill.UserId))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if account == nil || account.Total < bill.Amount {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 扣款
|
|
||||||
err = SharedUserAccountDAO.UpdateUserAccount(tx, int64(account.Id), -float32(bill.Amount), userconfigs.AccountEventTypePayBill, "支付账单"+bill.Code, maps.Map{"billId": bill.Id})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 改为已支付
|
|
||||||
err = models.SharedUserBillDAO.UpdateUserBillIsPaid(tx, int64(bill.Id), true)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CheckUserAccount 检查用户账户
|
|
||||||
func (this *UserAccountDAO) CheckUserAccount(tx *dbs.Tx, userId int64, accountId int64) error {
|
|
||||||
exists, err := this.Query(tx).
|
|
||||||
Pk(accountId).
|
|
||||||
Attr("userId", userId).
|
|
||||||
Exist()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !exists {
|
|
||||||
return models.ErrNotFound
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,129 +0,0 @@
|
|||||||
package accounts
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
||||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"github.com/iwind/TeaGo/maps"
|
|
||||||
"github.com/iwind/TeaGo/types"
|
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type UserAccountLogDAO dbs.DAO
|
|
||||||
|
|
||||||
func NewUserAccountLogDAO() *UserAccountLogDAO {
|
|
||||||
return dbs.NewDAO(&UserAccountLogDAO{
|
|
||||||
DAOObject: dbs.DAOObject{
|
|
||||||
DB: Tea.Env,
|
|
||||||
Table: "edgeUserAccountLogs",
|
|
||||||
Model: new(UserAccountLog),
|
|
||||||
PkName: "id",
|
|
||||||
},
|
|
||||||
}).(*UserAccountLogDAO)
|
|
||||||
}
|
|
||||||
|
|
||||||
var SharedUserAccountLogDAO *UserAccountLogDAO
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReady(func() {
|
|
||||||
SharedUserAccountLogDAO = NewUserAccountLogDAO()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateAccountLog 生成用户账户日志
|
|
||||||
func (this *UserAccountLogDAO) CreateAccountLog(tx *dbs.Tx, userId int64, accountId int64, delta float32, deltaFrozen float32, eventType userconfigs.AccountEventType, description string, params maps.Map) error {
|
|
||||||
var op = NewUserAccountLogOperator()
|
|
||||||
op.UserId = userId
|
|
||||||
op.AccountId = accountId
|
|
||||||
op.Delta = delta
|
|
||||||
op.DeltaFrozen = deltaFrozen
|
|
||||||
|
|
||||||
account, err := SharedUserAccountDAO.FindUserAccountWithAccountId(tx, accountId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if account == nil {
|
|
||||||
return errors.New("invalid account id '" + types.String(accountId) + "'")
|
|
||||||
}
|
|
||||||
op.Total = account.Total
|
|
||||||
op.TotalFrozen = account.TotalFrozen
|
|
||||||
|
|
||||||
op.EventType = eventType
|
|
||||||
op.Description = description
|
|
||||||
|
|
||||||
if params == nil {
|
|
||||||
params = maps.Map{}
|
|
||||||
}
|
|
||||||
op.Params = params.AsJSON()
|
|
||||||
|
|
||||||
op.Day = timeutil.Format("Ymd")
|
|
||||||
err = this.Save(tx, op)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return SharedUserAccountDailyStatDAO.UpdateDailyStat(tx)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CountAccountLogs 计算日志数量
|
|
||||||
func (this *UserAccountLogDAO) CountAccountLogs(tx *dbs.Tx, userId int64, accountId int64, keyword string, eventType string) (int64, error) {
|
|
||||||
var query = this.Query(tx)
|
|
||||||
if userId > 0 {
|
|
||||||
query.Attr("userId", userId)
|
|
||||||
}
|
|
||||||
if accountId > 0 {
|
|
||||||
query.Attr("accountId", accountId)
|
|
||||||
}
|
|
||||||
if len(keyword) > 0 {
|
|
||||||
query.Where("(userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword)) OR description LIKE :keyword)")
|
|
||||||
query.Param("keyword", dbutils.QuoteLike(keyword))
|
|
||||||
}
|
|
||||||
if len(eventType) > 0 {
|
|
||||||
query.Attr("eventType", eventType)
|
|
||||||
}
|
|
||||||
return query.Count()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListAccountLogs 列出单页日志
|
|
||||||
func (this *UserAccountLogDAO) ListAccountLogs(tx *dbs.Tx, userId int64, accountId int64, keyword string, eventType string, offset int64, size int64) (result []*UserAccountLog, err error) {
|
|
||||||
var query = this.Query(tx)
|
|
||||||
if userId > 0 {
|
|
||||||
query.Attr("userId", userId)
|
|
||||||
}
|
|
||||||
if accountId > 0 {
|
|
||||||
query.Attr("accountId", accountId)
|
|
||||||
}
|
|
||||||
if len(keyword) > 0 {
|
|
||||||
query.Where("(userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1 AND (username LIKE :keyword OR fullname LIKE :keyword)) OR description LIKE :keyword)")
|
|
||||||
query.Param("keyword", dbutils.QuoteLike(keyword))
|
|
||||||
}
|
|
||||||
if len(eventType) > 0 {
|
|
||||||
query.Attr("eventType", eventType)
|
|
||||||
}
|
|
||||||
_, err = query.
|
|
||||||
DescPk().
|
|
||||||
Offset(offset).
|
|
||||||
Limit(size).
|
|
||||||
Slice(&result).
|
|
||||||
FindAll()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// SumDailyEventTypes 统计某天数据总和
|
|
||||||
func (this *UserAccountLogDAO) SumDailyEventTypes(tx *dbs.Tx, day string, eventTypes []userconfigs.AccountEventType) (float32, error) {
|
|
||||||
if len(eventTypes) == 0 {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
result, err := this.Query(tx).
|
|
||||||
Attr("day", day).
|
|
||||||
Attr("eventType", eventTypes).
|
|
||||||
Sum("delta", 0)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return types.Float32(result), nil
|
|
||||||
}
|
|
||||||
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()
|
||||||
|
})
|
||||||
|
}
|
||||||
6
internal/db/models/accounts/user_order_dao_test.go
Normal file
6
internal/db/models/accounts/user_order_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_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
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package acme
|
package acme
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
@@ -72,8 +73,9 @@ func (this *ACMEProviderAccountDAO) FindACMEProviderAccountName(tx *dbs.Tx, id i
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateAccount 创建账号
|
// CreateAccount 创建账号
|
||||||
func (this *ACMEProviderAccountDAO) CreateAccount(tx *dbs.Tx, name string, providerCode string, eabKid string, eabKey string) (int64, error) {
|
func (this *ACMEProviderAccountDAO) CreateAccount(tx *dbs.Tx, userId int64, name string, providerCode string, eabKid string, eabKey string) (int64, error) {
|
||||||
var op = NewACMEProviderAccountOperator()
|
var op = NewACMEProviderAccountOperator()
|
||||||
|
op.UserId = userId
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.ProviderCode = providerCode
|
op.ProviderCode = providerCode
|
||||||
op.EabKid = eabKid
|
op.EabKid = eabKid
|
||||||
@@ -98,15 +100,18 @@ func (this *ACMEProviderAccountDAO) UpdateAccount(tx *dbs.Tx, accountId int64, n
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CountAllEnabledAccounts 计算账号数量
|
// CountAllEnabledAccounts 计算账号数量
|
||||||
func (this *ACMEProviderAccountDAO) CountAllEnabledAccounts(tx *dbs.Tx) (int64, error) {
|
func (this *ACMEProviderAccountDAO) CountAllEnabledAccounts(tx *dbs.Tx, userId int64) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
|
State(ACMEProviderAccountStateEnabled).
|
||||||
|
Attr("userId", userId).
|
||||||
Count()
|
Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListEnabledAccounts 查找单页账号
|
// ListEnabledAccounts 查找单页账号
|
||||||
func (this *ACMEProviderAccountDAO) ListEnabledAccounts(tx *dbs.Tx, offset int64, size int64) (result []*ACMEProviderAccount, err error) {
|
func (this *ACMEProviderAccountDAO) ListEnabledAccounts(tx *dbs.Tx, userId int64, offset int64, size int64) (result []*ACMEProviderAccount, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(ACMEProviderAccountStateEnabled).
|
State(ACMEProviderAccountStateEnabled).
|
||||||
|
Attr("userId", userId).
|
||||||
Offset(offset).
|
Offset(offset).
|
||||||
Limit(size).
|
Limit(size).
|
||||||
DescPk().
|
DescPk().
|
||||||
@@ -116,12 +121,34 @@ func (this *ACMEProviderAccountDAO) ListEnabledAccounts(tx *dbs.Tx, offset int64
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindAllEnabledAccountsWithProviderCode 根据服务商代号查找账号
|
// FindAllEnabledAccountsWithProviderCode 根据服务商代号查找账号
|
||||||
func (this *ACMEProviderAccountDAO) FindAllEnabledAccountsWithProviderCode(tx *dbs.Tx, providerCode string) (result []*ACMEProviderAccount, err error) {
|
func (this *ACMEProviderAccountDAO) FindAllEnabledAccountsWithProviderCode(tx *dbs.Tx, userId int64, providerCode string) (result []*ACMEProviderAccount, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(ACMEProviderAccountStateEnabled).
|
State(ACMEProviderAccountStateEnabled).
|
||||||
Attr("providerCode", providerCode).
|
Attr("providerCode", providerCode).
|
||||||
|
Attr("userId", userId).
|
||||||
DescPk().
|
DescPk().
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckUserAccount 检查是否为用户的服务商账号
|
||||||
|
func (this *ACMEProviderAccountDAO) CheckUserAccount(tx *dbs.Tx, userId int64, accountId int64) error {
|
||||||
|
if userId <= 0 || accountId <= 0 {
|
||||||
|
return models.ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := this.Query(tx).
|
||||||
|
Pk(accountId).
|
||||||
|
State(ACMEProviderAccountStateEnabled).
|
||||||
|
Attr("userId", userId).
|
||||||
|
Exist()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !b {
|
||||||
|
return models.ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,24 +3,26 @@ package acme
|
|||||||
// ACMEProviderAccount ACME提供商
|
// ACMEProviderAccount ACME提供商
|
||||||
type ACMEProviderAccount struct {
|
type ACMEProviderAccount struct {
|
||||||
Id uint64 `field:"id"` // ID
|
Id uint64 `field:"id"` // ID
|
||||||
|
UserId uint64 `field:"userId"` // 用户ID
|
||||||
IsOn bool `field:"isOn"` // 是否启用
|
IsOn bool `field:"isOn"` // 是否启用
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
ProviderCode string `field:"providerCode"` // 代号
|
ProviderCode string `field:"providerCode"` // 代号
|
||||||
Error string `field:"error"` // 最后一条错误信息
|
|
||||||
EabKid string `field:"eabKid"` // KID
|
EabKid string `field:"eabKid"` // KID
|
||||||
EabKey string `field:"eabKey"` // Key
|
EabKey string `field:"eabKey"` // Key
|
||||||
|
Error string `field:"error"` // 最后一条错误信息
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
type ACMEProviderAccountOperator struct {
|
type ACMEProviderAccountOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
IsOn interface{} // 是否启用
|
UserId any // 用户ID
|
||||||
Name interface{} // 名称
|
IsOn any // 是否启用
|
||||||
ProviderCode interface{} // 代号
|
Name any // 名称
|
||||||
Error interface{} // 最后一条错误信息
|
ProviderCode any // 代号
|
||||||
EabKid interface{} // KID
|
EabKid any // KID
|
||||||
EabKey interface{} // Key
|
EabKey any // Key
|
||||||
State interface{} // 状态
|
Error any // 最后一条错误信息
|
||||||
|
State any // 状态
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewACMEProviderAccountOperator() *ACMEProviderAccountOperator {
|
func NewACMEProviderAccountOperator() *ACMEProviderAccountOperator {
|
||||||
|
|||||||
@@ -106,8 +106,9 @@ func (this *ACMETaskDAO) DisableAllTasksWithCertId(tx *dbs.Tx, certId int64) err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CountAllEnabledACMETasks 计算所有任务数量
|
// CountAllEnabledACMETasks 计算所有任务数量
|
||||||
func (this *ACMETaskDAO) CountAllEnabledACMETasks(tx *dbs.Tx, adminId int64, userId int64, isAvailable bool, isExpired bool, expiringDays int64, keyword string) (int64, error) {
|
func (this *ACMETaskDAO) CountAllEnabledACMETasks(tx *dbs.Tx, userId int64, isAvailable bool, isExpired bool, expiringDays int64, keyword string) (int64, error) {
|
||||||
query := dbutils.NewQuery(tx, this, adminId, userId)
|
var query = this.Query(tx)
|
||||||
|
query.Attr("userId", userId) // 这个条件必须加上
|
||||||
if isAvailable || isExpired || expiringDays > 0 {
|
if isAvailable || isExpired || expiringDays > 0 {
|
||||||
query.Gt("certId", 0)
|
query.Gt("certId", 0)
|
||||||
|
|
||||||
@@ -137,8 +138,9 @@ func (this *ACMETaskDAO) CountAllEnabledACMETasks(tx *dbs.Tx, adminId int64, use
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListEnabledACMETasks 列出单页任务
|
// ListEnabledACMETasks 列出单页任务
|
||||||
func (this *ACMETaskDAO) ListEnabledACMETasks(tx *dbs.Tx, adminId int64, userId int64, isAvailable bool, isExpired bool, expiringDays int64, keyword string, offset int64, size int64) (result []*ACMETask, err error) {
|
func (this *ACMETaskDAO) ListEnabledACMETasks(tx *dbs.Tx, userId int64, isAvailable bool, isExpired bool, expiringDays int64, keyword string, offset int64, size int64) (result []*ACMETask, err error) {
|
||||||
query := dbutils.NewQuery(tx, this, adminId, userId)
|
var query = this.Query(tx)
|
||||||
|
query.Attr("userId", userId) // 这个条件必须加上
|
||||||
if isAvailable || isExpired || expiringDays > 0 {
|
if isAvailable || isExpired || expiringDays > 0 {
|
||||||
query.Gt("certId", 0)
|
query.Gt("certId", 0)
|
||||||
|
|
||||||
@@ -169,7 +171,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 +206,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
|
||||||
@@ -227,8 +229,13 @@ func (this *ACMETaskDAO) UpdateACMETask(tx *dbs.Tx, acmeTaskId int64, acmeUserId
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CheckACMETask 检查权限
|
// CheckACMETask 检查权限
|
||||||
func (this *ACMETaskDAO) CheckACMETask(tx *dbs.Tx, adminId int64, userId int64, acmeTaskId int64) (bool, error) {
|
func (this *ACMETaskDAO) CheckACMETask(tx *dbs.Tx, userId int64, acmeTaskId int64) (bool, error) {
|
||||||
return dbutils.NewQuery(tx, this, adminId, userId).
|
var query = this.Query(tx)
|
||||||
|
if userId > 0 {
|
||||||
|
query.Attr("userId", userId)
|
||||||
|
}
|
||||||
|
|
||||||
|
return query.
|
||||||
State(ACMETaskStateEnabled).
|
State(ACMETaskStateEnabled).
|
||||||
Pk(acmeTaskId).
|
Pk(acmeTaskId).
|
||||||
Exist()
|
Exist()
|
||||||
@@ -240,7 +247,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)
|
||||||
@@ -319,7 +326,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteUser := acmeutils.NewUser(user.Email, privateKey, func(resource *registration.Resource) error {
|
var remoteUser = acmeutils.NewUser(user.Email, privateKey, func(resource *registration.Resource) error {
|
||||||
resourceJSON, err := json.Marshal(resource)
|
resourceJSON, err := json.Marshal(resource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -349,7 +356,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
|
|||||||
errMsg = "找不到DNS服务商账号"
|
errMsg = "找不到DNS服务商账号"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
providerInterface := dnsclients.FindProvider(dnsProvider.Type)
|
providerInterface := dnsclients.FindProvider(dnsProvider.Type, int64(dnsProvider.Id))
|
||||||
if providerInterface == nil {
|
if providerInterface == nil {
|
||||||
errMsg = "暂不支持此类型的DNS服务商 '" + dnsProvider.Type + "'"
|
errMsg = "暂不支持此类型的DNS服务商 '" + dnsProvider.Type + "'"
|
||||||
return
|
return
|
||||||
@@ -382,7 +389,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
|
|||||||
acmeTask.Provider = acmeProvider
|
acmeTask.Provider = acmeProvider
|
||||||
acmeTask.Account = acmeAccount
|
acmeTask.Account = acmeAccount
|
||||||
|
|
||||||
acmeRequest := acmeutils.NewRequest(acmeTask)
|
var acmeRequest = acmeutils.NewRequest(acmeTask)
|
||||||
acmeRequest.OnAuth(func(domain, token, keyAuth string) {
|
acmeRequest.OnAuth(func(domain, token, keyAuth string) {
|
||||||
err := SharedACMEAuthenticationDAO.CreateAuth(tx, taskId, domain, token, keyAuth)
|
err := SharedACMEAuthenticationDAO.CreateAuth(tx, taskId, domain, token, keyAuth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -398,7 +405,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("ACME", "encode auth data failed: '"+task.AuthURL+"'")
|
remotelogs.Error("ACME", "encode auth data failed: '"+task.AuthURL+"'")
|
||||||
} else {
|
} else {
|
||||||
client := utils.SharedHttpClient(5 * time.Second)
|
var client = utils.SharedHttpClient(10 * time.Second)
|
||||||
req, err := http.NewRequest(http.MethodPost, task.AuthURL, bytes.NewReader(authJSON))
|
req, err := http.NewRequest(http.MethodPost, task.AuthURL, bytes.NewReader(authJSON))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("User-Agent", teaconst.ProductName+"/"+teaconst.Version)
|
req.Header.Set("User-Agent", teaconst.ProductName+"/"+teaconst.Version)
|
||||||
@@ -423,7 +430,7 @@ func (this *ACMETaskDAO) runTaskWithoutLog(tx *dbs.Tx, taskId int64) (isOk bool,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 分析证书
|
// 分析证书
|
||||||
sslConfig := &sslconfigs.SSLCertConfig{
|
var sslConfig = &sslconfigs.SSLCertConfig{
|
||||||
CertData: certData,
|
CertData: certData,
|
||||||
KeyData: keyData,
|
KeyData: keyData,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,9 +27,9 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成日志
|
// CreateACMETaskLog 生成日志
|
||||||
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
|
||||||
@@ -37,7 +37,7 @@ func (this *ACMETaskLogDAO) CreateACMETaskLog(tx *dbs.Tx, taskId int64, isOk boo
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 取得任务的最后一条执行日志
|
// FindLatestACMETasKLog 取得任务的最后一条执行日志
|
||||||
func (this *ACMETaskLogDAO) FindLatestACMETasKLog(tx *dbs.Tx, taskId int64) (*ACMETaskLog, error) {
|
func (this *ACMETaskLogDAO) FindLatestACMETasKLog(tx *dbs.Tx, taskId int64) (*ACMETaskLog, error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
Attr("taskId", taskId).
|
Attr("taskId", taskId).
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -44,11 +44,17 @@ func (this *AdminDAO) EnableAdmin(tx *dbs.Tx, id int64) (rowsAffected int64, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DisableAdmin 禁用条目
|
// DisableAdmin 禁用条目
|
||||||
func (this *AdminDAO) DisableAdmin(tx *dbs.Tx, id int64) (rowsAffected int64, err error) {
|
func (this *AdminDAO) DisableAdmin(tx *dbs.Tx, adminId int64) error {
|
||||||
return this.Query(tx).
|
err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(adminId).
|
||||||
Set("state", AdminStateDisabled).
|
Set("state", AdminStateDisabled).
|
||||||
Update()
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除AccessTokens
|
||||||
|
return SharedAPIAccessTokenDAO.DeleteAccessTokens(tx, adminId, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledAdmin 查找启用中的条目
|
// FindEnabledAdmin 查找启用中的条目
|
||||||
@@ -63,6 +69,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 +134,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 +143,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 +168,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 +180,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
|
||||||
@@ -177,7 +196,19 @@ func (this *AdminDAO) UpdateAdmin(tx *dbs.Tx, adminId int64, username string, ca
|
|||||||
}
|
}
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isOn {
|
||||||
|
// 删除AccessTokens
|
||||||
|
err = SharedAPIAccessTokenDAO.DeleteAccessTokens(tx, adminId, 0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckAdminUsername 检查用户名是否存在
|
// CheckAdminUsername 检查用户名是否存在
|
||||||
@@ -198,7 +229,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 +244,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
|
||||||
@@ -81,3 +81,16 @@ func (this *APIAccessTokenDAO) FindAccessToken(tx *dbs.Tx, token string) (*APIAc
|
|||||||
}
|
}
|
||||||
return one.(*APIAccessToken), nil
|
return one.(*APIAccessToken), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteAccessTokens 删除用户的令牌
|
||||||
|
func (this *APIAccessTokenDAO) DeleteAccessTokens(tx *dbs.Tx, adminId int64, userId int64) error {
|
||||||
|
var query = this.Query(tx)
|
||||||
|
if adminId > 0 {
|
||||||
|
query.Attr("adminId", adminId)
|
||||||
|
} else if userId > 0 {
|
||||||
|
query.Attr("userId", userId)
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return query.DeleteQuickly()
|
||||||
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -43,9 +43,9 @@ func (this *APINode) DecodeHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*serverc
|
|||||||
}
|
}
|
||||||
|
|
||||||
if config.SSLPolicyRef != nil {
|
if config.SSLPolicyRef != nil {
|
||||||
policyId := config.SSLPolicyRef.SSLPolicyId
|
var policyId = config.SSLPolicyRef.SSLPolicyId
|
||||||
if policyId > 0 {
|
if policyId > 0 {
|
||||||
sslPolicy, err := SharedSSLPolicyDAO.ComposePolicyConfig(tx, policyId, cacheMap)
|
sslPolicy, err := SharedSSLPolicyDAO.ComposePolicyConfig(tx, policyId, false, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -143,7 +143,7 @@ func (this *APINode) DecodeRestHTTPS(tx *dbs.Tx, cacheMap *utils.CacheMap) (*ser
|
|||||||
if config.SSLPolicyRef != nil {
|
if config.SSLPolicyRef != nil {
|
||||||
policyId := config.SSLPolicyRef.SSLPolicyId
|
policyId := config.SSLPolicyRef.SSLPolicyId
|
||||||
if policyId > 0 {
|
if policyId > 0 {
|
||||||
sslPolicy, err := SharedSSLPolicyDAO.ComposePolicyConfig(tx, policyId, cacheMap)
|
sslPolicy, err := SharedSSLPolicyDAO.ComposePolicyConfig(tx, policyId, false, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ func (this *ApiTokenDAO) FindEnabledTokenWithNodeCacheable(tx *dbs.Tx, nodeId st
|
|||||||
State(ApiTokenStateEnabled).
|
State(ApiTokenStateEnabled).
|
||||||
Find()
|
Find()
|
||||||
if one != nil {
|
if one != nil {
|
||||||
token := one.(*ApiToken)
|
token = one.(*ApiToken)
|
||||||
SharedCacheLocker.Lock()
|
SharedCacheLocker.Lock()
|
||||||
apiTokenCacheMap[nodeId] = token
|
apiTokenCacheMap[nodeId] = token
|
||||||
SharedCacheLocker.Unlock()
|
SharedCacheLocker.Unlock()
|
||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,138 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
ClientBrowserStateEnabled = 1 // 已启用
|
|
||||||
ClientBrowserStateDisabled = 0 // 已禁用
|
|
||||||
)
|
|
||||||
|
|
||||||
var clientBrowserNameAndIdCacheMap = map[string]int64{}
|
|
||||||
|
|
||||||
type ClientBrowserDAO dbs.DAO
|
|
||||||
|
|
||||||
func NewClientBrowserDAO() *ClientBrowserDAO {
|
|
||||||
return dbs.NewDAO(&ClientBrowserDAO{
|
|
||||||
DAOObject: dbs.DAOObject{
|
|
||||||
DB: Tea.Env,
|
|
||||||
Table: "edgeClientBrowsers",
|
|
||||||
Model: new(ClientBrowser),
|
|
||||||
PkName: "id",
|
|
||||||
},
|
|
||||||
}).(*ClientBrowserDAO)
|
|
||||||
}
|
|
||||||
|
|
||||||
var SharedClientBrowserDAO *ClientBrowserDAO
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReady(func() {
|
|
||||||
SharedClientBrowserDAO = NewClientBrowserDAO()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// EnableClientBrowser 启用条目
|
|
||||||
func (this *ClientBrowserDAO) EnableClientBrowser(tx *dbs.Tx, id uint32) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Set("state", ClientBrowserStateEnabled).
|
|
||||||
Update()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// DisableClientBrowser 禁用条目
|
|
||||||
func (this *ClientBrowserDAO) DisableClientBrowser(tx *dbs.Tx, id uint32) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Set("state", ClientBrowserStateDisabled).
|
|
||||||
Update()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindEnabledClientBrowser 查找启用中的条目
|
|
||||||
func (this *ClientBrowserDAO) FindEnabledClientBrowser(tx *dbs.Tx, id int64) (*ClientBrowser, error) {
|
|
||||||
result, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Attr("state", ClientBrowserStateEnabled).
|
|
||||||
Find()
|
|
||||||
if result == nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return result.(*ClientBrowser), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindClientBrowserName 根据主键查找名称
|
|
||||||
func (this *ClientBrowserDAO) FindClientBrowserName(tx *dbs.Tx, id uint32) (string, error) {
|
|
||||||
return this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Result("name").
|
|
||||||
FindStringCol("")
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindBrowserIdWithNameCacheable 根据浏览器名称查找浏览器ID
|
|
||||||
func (this *ClientBrowserDAO) FindBrowserIdWithNameCacheable(tx *dbs.Tx, browserName string) (int64, error) {
|
|
||||||
SharedCacheLocker.RLock()
|
|
||||||
browserId, ok := clientBrowserNameAndIdCacheMap[browserName]
|
|
||||||
if ok {
|
|
||||||
SharedCacheLocker.RUnlock()
|
|
||||||
return browserId, nil
|
|
||||||
}
|
|
||||||
SharedCacheLocker.RUnlock()
|
|
||||||
|
|
||||||
browserId, err := this.Query(tx).
|
|
||||||
Where("JSON_CONTAINS(codes, :browserName)").
|
|
||||||
Param("browserName", strconv.Quote(browserName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
|
||||||
ResultPk().
|
|
||||||
FindInt64Col(0)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if browserId > 0 {
|
|
||||||
// 只有找到的时候才放入缓存,以便于我们可以在不存在的时候创建一条新的记录
|
|
||||||
SharedCacheLocker.Lock()
|
|
||||||
clientBrowserNameAndIdCacheMap[browserName] = browserId
|
|
||||||
SharedCacheLocker.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
return browserId, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateBrowser 创建浏览器
|
|
||||||
func (this *ClientBrowserDAO) CreateBrowser(tx *dbs.Tx, browserName string) (int64, error) {
|
|
||||||
var maxlength = 50
|
|
||||||
if len(browserName) > maxlength {
|
|
||||||
browserName = browserName[:50]
|
|
||||||
}
|
|
||||||
|
|
||||||
SharedCacheLocker.Lock()
|
|
||||||
defer SharedCacheLocker.Unlock()
|
|
||||||
|
|
||||||
// 检查是否已经创建
|
|
||||||
browserId, err := this.Query(tx).
|
|
||||||
Attr("name", browserName).
|
|
||||||
ResultPk().
|
|
||||||
FindInt64Col(0)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if browserId > 0 {
|
|
||||||
return browserId, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
op := NewClientBrowserOperator()
|
|
||||||
op.Name = browserName
|
|
||||||
codes := []string{browserName}
|
|
||||||
codesJSON, err := json.Marshal(codes)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
op.Codes = codesJSON
|
|
||||||
op.State = ClientBrowserStateEnabled
|
|
||||||
return this.SaveInt64(tx, op)
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
)
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import "github.com/iwind/TeaGo/dbs"
|
|
||||||
|
|
||||||
// ClientBrowser 终端浏览器信息
|
|
||||||
type ClientBrowser struct {
|
|
||||||
Id uint32 `field:"id"` // ID
|
|
||||||
Name string `field:"name"` // 浏览器名称
|
|
||||||
Codes dbs.JSON `field:"codes"` // 代号
|
|
||||||
State uint8 `field:"state"` // 状态
|
|
||||||
}
|
|
||||||
|
|
||||||
type ClientBrowserOperator struct {
|
|
||||||
Id interface{} // ID
|
|
||||||
Name interface{} // 浏览器名称
|
|
||||||
Codes interface{} // 代号
|
|
||||||
State interface{} // 状态
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewClientBrowserOperator() *ClientBrowserOperator {
|
|
||||||
return &ClientBrowserOperator{}
|
|
||||||
}
|
|
||||||
@@ -1,140 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/dbs"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
ClientSystemStateEnabled = 1 // 已启用
|
|
||||||
ClientSystemStateDisabled = 0 // 已禁用
|
|
||||||
)
|
|
||||||
|
|
||||||
var clientSystemNameAndIdCacheMap = map[string]int64{} // system name => id
|
|
||||||
|
|
||||||
type ClientSystemDAO dbs.DAO
|
|
||||||
|
|
||||||
func NewClientSystemDAO() *ClientSystemDAO {
|
|
||||||
return dbs.NewDAO(&ClientSystemDAO{
|
|
||||||
DAOObject: dbs.DAOObject{
|
|
||||||
DB: Tea.Env,
|
|
||||||
Table: "edgeClientSystems",
|
|
||||||
Model: new(ClientSystem),
|
|
||||||
PkName: "id",
|
|
||||||
},
|
|
||||||
}).(*ClientSystemDAO)
|
|
||||||
}
|
|
||||||
|
|
||||||
var SharedClientSystemDAO *ClientSystemDAO
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
dbs.OnReady(func() {
|
|
||||||
SharedClientSystemDAO = NewClientSystemDAO()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// EnableClientSystem 启用条目
|
|
||||||
func (this *ClientSystemDAO) EnableClientSystem(tx *dbs.Tx, id uint32) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Set("state", ClientSystemStateEnabled).
|
|
||||||
Update()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// DisableClientSystem 禁用条目
|
|
||||||
func (this *ClientSystemDAO) DisableClientSystem(tx *dbs.Tx, id uint32) error {
|
|
||||||
_, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Set("state", ClientSystemStateDisabled).
|
|
||||||
Update()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindEnabledClientSystem 查找启用中的条目
|
|
||||||
func (this *ClientSystemDAO) FindEnabledClientSystem(tx *dbs.Tx, id int64) (*ClientSystem, error) {
|
|
||||||
result, err := this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Attr("state", ClientSystemStateEnabled).
|
|
||||||
Find()
|
|
||||||
if result == nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return result.(*ClientSystem), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindClientSystemName 根据主键查找名称
|
|
||||||
func (this *ClientSystemDAO) FindClientSystemName(tx *dbs.Tx, id uint32) (string, error) {
|
|
||||||
return this.Query(tx).
|
|
||||||
Pk(id).
|
|
||||||
Result("name").
|
|
||||||
FindStringCol("")
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindSystemIdWithNameCacheable 根据操作系统名称查找系统ID
|
|
||||||
func (this *ClientSystemDAO) FindSystemIdWithNameCacheable(tx *dbs.Tx, systemName string) (int64, error) {
|
|
||||||
SharedCacheLocker.RLock()
|
|
||||||
systemId, ok := clientSystemNameAndIdCacheMap[systemName]
|
|
||||||
if ok {
|
|
||||||
SharedCacheLocker.RUnlock()
|
|
||||||
return systemId, nil
|
|
||||||
}
|
|
||||||
SharedCacheLocker.RUnlock()
|
|
||||||
|
|
||||||
systemId, err := this.Query(tx).
|
|
||||||
Where("JSON_CONTAINS(codes, :systemName)").
|
|
||||||
Param("systemName", strconv.Quote(systemName)). // 查询的需要是个JSON字符串,所以这里加双引号
|
|
||||||
ResultPk().
|
|
||||||
FindInt64Col(0)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if systemId > 0 {
|
|
||||||
// 只有找到的时候才放入缓存,以便于我们可以在不存在的时候创建一条新的记录
|
|
||||||
SharedCacheLocker.Lock()
|
|
||||||
clientSystemNameAndIdCacheMap[systemName] = systemId
|
|
||||||
SharedCacheLocker.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
return systemId, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateSystem 创建浏览器
|
|
||||||
func (this *ClientSystemDAO) CreateSystem(tx *dbs.Tx, systemName string) (int64, error) {
|
|
||||||
var maxlength = 50
|
|
||||||
if len(systemName) > maxlength {
|
|
||||||
systemName = systemName[:50]
|
|
||||||
}
|
|
||||||
|
|
||||||
SharedCacheLocker.Lock()
|
|
||||||
defer SharedCacheLocker.Unlock()
|
|
||||||
|
|
||||||
// 检查是否已经创建
|
|
||||||
systemId, err := this.Query(tx).
|
|
||||||
Attr("name", systemName).
|
|
||||||
ResultPk().
|
|
||||||
FindInt64Col(0)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if systemId > 0 {
|
|
||||||
return systemId, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
op := NewClientSystemOperator()
|
|
||||||
op.Name = systemName
|
|
||||||
|
|
||||||
codes := []string{systemName}
|
|
||||||
codesJSON, err := json.Marshal(codes)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
op.Codes = codesJSON
|
|
||||||
|
|
||||||
op.State = ClientSystemStateEnabled
|
|
||||||
return this.SaveInt64(tx, op)
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
)
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import "github.com/iwind/TeaGo/dbs"
|
|
||||||
|
|
||||||
// ClientSystem 终端操作系统信息
|
|
||||||
type ClientSystem struct {
|
|
||||||
Id uint32 `field:"id"` // ID
|
|
||||||
Name string `field:"name"` // 系统名称
|
|
||||||
Codes dbs.JSON `field:"codes"` // 代号
|
|
||||||
State uint8 `field:"state"` //
|
|
||||||
}
|
|
||||||
|
|
||||||
type ClientSystemOperator struct {
|
|
||||||
Id interface{} // ID
|
|
||||||
Name interface{} // 系统名称
|
|
||||||
Codes interface{} // 代号
|
|
||||||
State interface{} //
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewClientSystemOperator() *ClientSystemOperator {
|
|
||||||
return &ClientSystemOperator{}
|
|
||||||
}
|
|
||||||
98
internal/db/models/clients/client_agent_dao.go
Normal file
98
internal/db/models/clients/client_agent_dao.go
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
package clients
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ClientAgentDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewClientAgentDAO() *ClientAgentDAO {
|
||||||
|
return dbs.NewDAO(&ClientAgentDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeClientAgents",
|
||||||
|
Model: new(ClientAgent),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*ClientAgentDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedClientAgentDAO *ClientAgentDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedClientAgentDAO = NewClientAgentDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindClientAgentName 根据主键查找名称
|
||||||
|
func (this *ClientAgentDAO) FindClientAgentName(tx *dbs.Tx, id int64) (string, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Result("name").
|
||||||
|
FindStringCol("")
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAgent 查找Agent
|
||||||
|
func (this *ClientAgentDAO) FindAgent(tx *dbs.Tx, agentId int64) (*ClientAgent, error) {
|
||||||
|
if agentId <= 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
one, err := this.Query(tx).
|
||||||
|
Pk(agentId).
|
||||||
|
Find()
|
||||||
|
if err != nil || one == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return one.(*ClientAgent), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAgentIdWithCode 根据代号查找ID
|
||||||
|
func (this *ClientAgentDAO) FindAgentIdWithCode(tx *dbs.Tx, code string) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
ResultPk().
|
||||||
|
Attr("code", code).
|
||||||
|
FindInt64Col(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAgentNameWithCode 根据代号查找Agent名称
|
||||||
|
func (this *ClientAgentDAO) FindAgentNameWithCode(tx *dbs.Tx, code string) (string, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Result("name").
|
||||||
|
Attr("code", code).
|
||||||
|
FindStringCol("")
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateAgentCountIPs 修改Agent拥有的IP数量
|
||||||
|
func (this *ClientAgentDAO) UpdateAgentCountIPs(tx *dbs.Tx, agentId int64, countIPs int64) error {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(agentId).
|
||||||
|
Set("countIPs", countIPs).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAllAgents 查找所有Agents
|
||||||
|
func (this *ClientAgentDAO) FindAllAgents(tx *dbs.Tx) (result []*ClientAgent, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Desc("order").
|
||||||
|
AscPk().
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindAllNSAgents 查找所有DNS可以使用的Agents
|
||||||
|
func (this *ClientAgentDAO) FindAllNSAgents(tx *dbs.Tx) (result []*ClientAgent, err error) {
|
||||||
|
// 注意:允许NS使用所有的Agent,不管有没有IP数据
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Result("id", "name", "code").
|
||||||
|
Desc("order").
|
||||||
|
AscPk().
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
6
internal/db/models/clients/client_agent_dao_test.go
Normal file
6
internal/db/models/clients/client_agent_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package clients_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
)
|
||||||
105
internal/db/models/clients/client_agent_ip_dao.go
Normal file
105
internal/db/models/clients/client_agent_ip_dao.go
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
package clients
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO 需要定时对所有IP的PTR进行检查,剔除已经变更的IP
|
||||||
|
|
||||||
|
type ClientAgentIPDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewClientAgentIPDAO() *ClientAgentIPDAO {
|
||||||
|
return dbs.NewDAO(&ClientAgentIPDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeClientAgentIPs",
|
||||||
|
Model: new(ClientAgentIP),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*ClientAgentIPDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedClientAgentIPDAO *ClientAgentIPDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedClientAgentIPDAO = NewClientAgentIPDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateIP 写入IP
|
||||||
|
func (this *ClientAgentIPDAO) CreateIP(tx *dbs.Tx, agentId int64, ip string, ptr string) error {
|
||||||
|
// 检查数据有效性
|
||||||
|
if agentId <= 0 || len(ip) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 限制ptr长度
|
||||||
|
if len(ptr) > 100 {
|
||||||
|
ptr = ptr[:100]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否存在
|
||||||
|
exists, err := this.Query(tx).
|
||||||
|
Attr("agentId", agentId).
|
||||||
|
Attr("ip", ip).
|
||||||
|
Exist()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if exists {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var op = NewClientAgentIPOperator()
|
||||||
|
op.AgentId = agentId
|
||||||
|
op.IP = ip
|
||||||
|
op.Ptr = ptr
|
||||||
|
err = this.Save(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
// 忽略duplicate错误
|
||||||
|
if models.CheckSQLDuplicateErr(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新Agent IP数量
|
||||||
|
countIPs, err := this.CountAgentIPs(tx, agentId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = SharedClientAgentDAO.UpdateAgentCountIPs(tx, agentId, countIPs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListIPsAfterId 列出某个ID之后的IP
|
||||||
|
func (this *ClientAgentIPDAO) ListIPsAfterId(tx *dbs.Tx, id int64, size int64) (result []*ClientAgentIP, err error) {
|
||||||
|
if id < 0 {
|
||||||
|
id = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
Result("id", "ip", "ptr", "agentId").
|
||||||
|
Gt("id", id).
|
||||||
|
AscPk().
|
||||||
|
Limit(size). // 限制单次读取个数
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountAgentIPs 计算Agent IP数量
|
||||||
|
func (this *ClientAgentIPDAO) CountAgentIPs(tx *dbs.Tx, agentId int64) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Attr("agentId", agentId).
|
||||||
|
Count()
|
||||||
|
}
|
||||||
16
internal/db/models/clients/client_agent_ip_dao_test.go
Normal file
16
internal/db/models/clients/client_agent_ip_dao_test.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package clients_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models/clients"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestClientAgentIPDAO_CreateIP(t *testing.T) {
|
||||||
|
var dao = clients.NewClientAgentIPDAO()
|
||||||
|
err := dao.CreateIP(nil, 1, "127.0.0.1", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
20
internal/db/models/clients/client_agent_ip_model.go
Normal file
20
internal/db/models/clients/client_agent_ip_model.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package clients
|
||||||
|
|
||||||
|
// ClientAgentIP Agent IP
|
||||||
|
type ClientAgentIP struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
AgentId uint32 `field:"agentId"` // Agent ID
|
||||||
|
IP string `field:"ip"` // IP地址
|
||||||
|
Ptr string `field:"ptr"` // PTR值
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClientAgentIPOperator struct {
|
||||||
|
Id any // ID
|
||||||
|
AgentId any // Agent ID
|
||||||
|
IP any // IP地址
|
||||||
|
Ptr any // PTR值
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClientAgentIPOperator() *ClientAgentIPOperator {
|
||||||
|
return &ClientAgentIPOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/clients/client_agent_ip_model_ext.go
Normal file
1
internal/db/models/clients/client_agent_ip_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package clients
|
||||||
24
internal/db/models/clients/client_agent_model.go
Normal file
24
internal/db/models/clients/client_agent_model.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package clients
|
||||||
|
|
||||||
|
// ClientAgent Agent库
|
||||||
|
type ClientAgent struct {
|
||||||
|
Id uint32 `field:"id"` // ID
|
||||||
|
Name string `field:"name"` // 名称
|
||||||
|
Code string `field:"code"` // 代号
|
||||||
|
Description string `field:"description"` // 介绍
|
||||||
|
Order uint32 `field:"order"` // 排序
|
||||||
|
CountIPs uint32 `field:"countIPs"` // IP数量
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClientAgentOperator struct {
|
||||||
|
Id any // ID
|
||||||
|
Name any // 名称
|
||||||
|
Code any // 代号
|
||||||
|
Description any // 介绍
|
||||||
|
Order any // 排序
|
||||||
|
CountIPs any // IP数量
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClientAgentOperator() *ClientAgentOperator {
|
||||||
|
return &ClientAgentOperator{}
|
||||||
|
}
|
||||||
6
internal/db/models/clients/client_agent_model_ext.go
Normal file
6
internal/db/models/clients/client_agent_model_ext.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package clients
|
||||||
|
|
||||||
|
// NSRouteCode NS线路代号
|
||||||
|
func (this *ClientAgent) NSRouteCode() string {
|
||||||
|
return "agent:" + this.Code
|
||||||
|
}
|
||||||
155
internal/db/models/clients/client_browser_dao.go
Normal file
155
internal/db/models/clients/client_browser_dao.go
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
package clients
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/ttlcache"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ClientBrowserStateEnabled = 1 // 已启用
|
||||||
|
ClientBrowserStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
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 := SharedClientBrowserDAO.Clean(nil, 7) // 只保留N天
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("SharedClientBrowserDAO", "clean expired data failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClientBrowserDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewClientBrowserDAO() *ClientBrowserDAO {
|
||||||
|
return dbs.NewDAO(&ClientBrowserDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeClientBrowsers",
|
||||||
|
Model: new(ClientBrowser),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*ClientBrowserDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedClientBrowserDAO *ClientBrowserDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedClientBrowserDAO = NewClientBrowserDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableClientBrowser 启用条目
|
||||||
|
func (this *ClientBrowserDAO) EnableClientBrowser(tx *dbs.Tx, id uint32) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", ClientBrowserStateEnabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableClientBrowser 禁用条目
|
||||||
|
func (this *ClientBrowserDAO) DisableClientBrowser(tx *dbs.Tx, id uint32) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", ClientBrowserStateDisabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledClientBrowser 查找启用中的条目
|
||||||
|
func (this *ClientBrowserDAO) FindEnabledClientBrowser(tx *dbs.Tx, id int64) (*ClientBrowser, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Attr("state", ClientBrowserStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*ClientBrowser), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindClientBrowserName 根据主键查找名称
|
||||||
|
func (this *ClientBrowserDAO) FindClientBrowserName(tx *dbs.Tx, id uint32) (string, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Result("name").
|
||||||
|
FindStringCol("")
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateBrowserIfNotExists 创建浏览器信息
|
||||||
|
func (this *ClientBrowserDAO) CreateBrowserIfNotExists(tx *dbs.Tx, browserName string) error {
|
||||||
|
const maxlength = 50
|
||||||
|
if len(browserName) > maxlength {
|
||||||
|
browserName = browserName[:50]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查缓存
|
||||||
|
var cacheKey = "clientBrowser:" + browserName
|
||||||
|
var cacheItem = ttlcache.SharedCache.Read(cacheKey)
|
||||||
|
if cacheItem != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否已经存在
|
||||||
|
// 不需要加状态条件
|
||||||
|
browserId, err := this.Query(tx).
|
||||||
|
Attr("name", browserName).
|
||||||
|
ResultPk().
|
||||||
|
FindInt64Col(0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if browserId > 0 {
|
||||||
|
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
|
||||||
|
ttlcache.SharedCache.Write(cacheKey, browserId, time.Now().Unix()+3600)
|
||||||
|
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(browserId).
|
||||||
|
Set("createdDay", timeutil.Format("Ymd")).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果不存在,则创建之
|
||||||
|
var op = NewClientBrowserOperator()
|
||||||
|
op.Name = browserName
|
||||||
|
op.CreatedDay = timeutil.Format("Ymd")
|
||||||
|
op.State = ClientBrowserStateEnabled
|
||||||
|
browserId, err = this.SaveInt64(tx, op)
|
||||||
|
if err != nil && models.CheckSQLErrCode(err, 1062 /** duplicate entry **/) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
|
||||||
|
if browserId > 0 {
|
||||||
|
ttlcache.SharedCache.Write(cacheKey, browserId, time.Now().Unix()+3600)
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean 清理
|
||||||
|
func (this *ClientBrowserDAO) Clean(tx *dbs.Tx, days int) error {
|
||||||
|
if days <= 0 {
|
||||||
|
days = 30
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.Query(tx).
|
||||||
|
Lt("createdDay", timeutil.Format("Ymd", time.Now().AddDate(0, 0, -days))).
|
||||||
|
DeleteQuickly()
|
||||||
|
}
|
||||||
33
internal/db/models/clients/client_browser_dao_test.go
Normal file
33
internal/db/models/clients/client_browser_dao_test.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package clients_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models/clients"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestClientBrowserDAO_CreateBrowser(t *testing.T) {
|
||||||
|
var dao = clients.NewClientBrowserDAO()
|
||||||
|
err := dao.CreateBrowserIfNotExists(nil, "Hello")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = dao.CreateBrowserIfNotExists(nil, "Hello")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = dao.CreateBrowserIfNotExists(nil, "Hello")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClientBrowserDAO_Clean(t *testing.T) {
|
||||||
|
var dao = clients.NewClientBrowserDAO()
|
||||||
|
err := dao.Clean(nil, 30)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
24
internal/db/models/clients/client_browser_model.go
Normal file
24
internal/db/models/clients/client_browser_model.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package clients
|
||||||
|
|
||||||
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
|
// ClientBrowser 终端浏览器信息
|
||||||
|
type ClientBrowser struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
Name string `field:"name"` // 浏览器名称
|
||||||
|
Codes dbs.JSON `field:"codes"` // 代号
|
||||||
|
CreatedDay string `field:"createdDay"` // 创建日期YYYYMMDD
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClientBrowserOperator struct {
|
||||||
|
Id any // ID
|
||||||
|
Name any // 浏览器名称
|
||||||
|
Codes any // 代号
|
||||||
|
CreatedDay any // 创建日期YYYYMMDD
|
||||||
|
State any // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClientBrowserOperator() *ClientBrowserOperator {
|
||||||
|
return &ClientBrowserOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/clients/client_browser_model_ext.go
Normal file
1
internal/db/models/clients/client_browser_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package clients
|
||||||
154
internal/db/models/clients/client_system_dao.go
Normal file
154
internal/db/models/clients/client_system_dao.go
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
package clients
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/ttlcache"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ClientSystemStateEnabled = 1 // 已启用
|
||||||
|
ClientSystemStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
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 := SharedClientSystemDAO.Clean(nil, 7) // 只保留N天
|
||||||
|
if err != nil {
|
||||||
|
remotelogs.Error("SharedClientSystemDAO", "clean expired data failed: "+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClientSystemDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewClientSystemDAO() *ClientSystemDAO {
|
||||||
|
return dbs.NewDAO(&ClientSystemDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeClientSystems",
|
||||||
|
Model: new(ClientSystem),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*ClientSystemDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedClientSystemDAO *ClientSystemDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedClientSystemDAO = NewClientSystemDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableClientSystem 启用条目
|
||||||
|
func (this *ClientSystemDAO) EnableClientSystem(tx *dbs.Tx, id uint32) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", ClientSystemStateEnabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableClientSystem 禁用条目
|
||||||
|
func (this *ClientSystemDAO) DisableClientSystem(tx *dbs.Tx, id uint32) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", ClientSystemStateDisabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledClientSystem 查找启用中的条目
|
||||||
|
func (this *ClientSystemDAO) FindEnabledClientSystem(tx *dbs.Tx, id int64) (*ClientSystem, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Attr("state", ClientSystemStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*ClientSystem), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindClientSystemName 根据主键查找名称
|
||||||
|
func (this *ClientSystemDAO) FindClientSystemName(tx *dbs.Tx, id uint32) (string, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Result("name").
|
||||||
|
FindStringCol("")
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateSystemIfNotExists 创建系统信息
|
||||||
|
func (this *ClientSystemDAO) CreateSystemIfNotExists(tx *dbs.Tx, systemName string) error {
|
||||||
|
const maxlength = 50
|
||||||
|
if len(systemName) > maxlength {
|
||||||
|
systemName = systemName[:50]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查缓存
|
||||||
|
var cacheKey = "clientSystem:" + systemName
|
||||||
|
var cacheItem = ttlcache.SharedCache.Read(cacheKey)
|
||||||
|
if cacheItem != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否已经存在
|
||||||
|
// 不需要加状态条件
|
||||||
|
systemId, err := this.Query(tx).
|
||||||
|
Attr("name", systemName).
|
||||||
|
ResultPk().
|
||||||
|
FindInt64Col(0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if systemId > 0 {
|
||||||
|
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
|
||||||
|
ttlcache.SharedCache.Write(cacheKey, systemId, time.Now().Unix()+3600)
|
||||||
|
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(systemId).
|
||||||
|
Set("createdDay", timeutil.Format("Ymd")).
|
||||||
|
UpdateQuickly()
|
||||||
|
}
|
||||||
|
|
||||||
|
var op = NewClientSystemOperator()
|
||||||
|
op.Name = systemName
|
||||||
|
op.CreatedDay = timeutil.Format("Ymd")
|
||||||
|
op.State = ClientSystemStateEnabled
|
||||||
|
systemId, err = this.SaveInt64(tx, op)
|
||||||
|
if err != nil && models.CheckSQLErrCode(err, 1062 /** duplicate entry **/) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
|
||||||
|
if systemId > 0 {
|
||||||
|
ttlcache.SharedCache.Write(cacheKey, systemId, time.Now().Unix()+3600)
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean 清理
|
||||||
|
func (this *ClientSystemDAO) Clean(tx *dbs.Tx, days int) error {
|
||||||
|
if days <= 0 {
|
||||||
|
days = 30
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.Query(tx).
|
||||||
|
Lt("createdDay", timeutil.Format("Ymd", time.Now().AddDate(0, 0, -days))).
|
||||||
|
DeleteQuickly()
|
||||||
|
}
|
||||||
31
internal/db/models/clients/client_system_dao_test.go
Normal file
31
internal/db/models/clients/client_system_dao_test.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package clients_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models/clients"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestClientSystemDAO_CreateSystemIfNotExists(t *testing.T) {
|
||||||
|
var dao = clients.NewClientSystemDAO()
|
||||||
|
{
|
||||||
|
err := dao.CreateSystemIfNotExists(nil, "Mac OS X")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
err := dao.CreateSystemIfNotExists(nil, "Mac OS X 2")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClientSystemDAO_Clean(t *testing.T) {
|
||||||
|
var dao = clients.NewClientSystemDAO()
|
||||||
|
err := dao.Clean(nil, 30)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
24
internal/db/models/clients/client_system_model.go
Normal file
24
internal/db/models/clients/client_system_model.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package clients
|
||||||
|
|
||||||
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
|
// ClientSystem 终端操作系统信息
|
||||||
|
type ClientSystem struct {
|
||||||
|
Id uint64 `field:"id"` // ID
|
||||||
|
Name string `field:"name"` // 系统名称
|
||||||
|
Codes dbs.JSON `field:"codes"` // 代号
|
||||||
|
CreatedDay string `field:"createdDay"` // 创建日期YYYYMMDD
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClientSystemOperator struct {
|
||||||
|
Id any // ID
|
||||||
|
Name any // 系统名称
|
||||||
|
Codes any // 代号
|
||||||
|
CreatedDay any // 创建日期YYYYMMDD
|
||||||
|
State any // 状态
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClientSystemOperator() *ClientSystemOperator {
|
||||||
|
return &ClientSystemOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/clients/client_system_model_ext.go
Normal file
1
internal/db/models/clients/client_system_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package clients
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -2,18 +2,15 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/lists"
|
"github.com/iwind/TeaGo/lists"
|
||||||
|
"github.com/iwind/TeaGo/rands"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"hash/crc32"
|
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -31,20 +28,11 @@ type httpAccessLogDefinition struct {
|
|||||||
// HTTP服务访问
|
// HTTP服务访问
|
||||||
var httpAccessLogDAOMapping = map[int64]*HTTPAccessLogDAOWrapper{} // dbNodeId => DAO
|
var httpAccessLogDAOMapping = map[int64]*HTTPAccessLogDAOWrapper{} // dbNodeId => DAO
|
||||||
|
|
||||||
// DNS服务访问
|
|
||||||
var nsAccessLogDAOMapping = map[int64]*NSAccessLogDAOWrapper{} // dbNodeId => DAO
|
|
||||||
var nsAccessLogTableMapping = map[string]bool{} // tableName_crc(dsn) => true
|
|
||||||
|
|
||||||
// HTTPAccessLogDAOWrapper HTTP访问日志DAO
|
// HTTPAccessLogDAOWrapper HTTP访问日志DAO
|
||||||
type HTTPAccessLogDAOWrapper struct {
|
type HTTPAccessLogDAOWrapper struct {
|
||||||
DAO *HTTPAccessLogDAO
|
DAO *HTTPAccessLogDAO
|
||||||
NodeId int64
|
NodeId int64
|
||||||
}
|
IsLocal bool
|
||||||
|
|
||||||
// NSAccessLogDAOWrapper NS访问日志DAO
|
|
||||||
type NSAccessLogDAOWrapper struct {
|
|
||||||
DAO *NSAccessLogDAO
|
|
||||||
NodeId int64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -78,102 +66,28 @@ func AllAccessLogDBs() []*dbs.DB {
|
|||||||
// 获取获取DAO
|
// 获取获取DAO
|
||||||
func randomHTTPAccessLogDAO() (dao *HTTPAccessLogDAOWrapper) {
|
func randomHTTPAccessLogDAO() (dao *HTTPAccessLogDAOWrapper) {
|
||||||
accessLogLocker.RLock()
|
accessLogLocker.RLock()
|
||||||
|
defer accessLogLocker.RUnlock()
|
||||||
if len(httpAccessLogDAOMapping) == 0 {
|
if len(httpAccessLogDAOMapping) == 0 {
|
||||||
dao = nil
|
dao = nil
|
||||||
} else {
|
|
||||||
for _, d := range httpAccessLogDAOMapping {
|
|
||||||
dao = d
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
accessLogLocker.RUnlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func randomNSAccessLogDAO() (dao *NSAccessLogDAOWrapper) {
|
|
||||||
accessLogLocker.RLock()
|
|
||||||
if len(nsAccessLogDAOMapping) == 0 {
|
|
||||||
dao = nil
|
|
||||||
} else {
|
|
||||||
for _, d := range nsAccessLogDAOMapping {
|
|
||||||
dao = d
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
accessLogLocker.RUnlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func findNSAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool, err error) {
|
|
||||||
if !regexp.MustCompile(`^\d{8}$`).MatchString(day) {
|
|
||||||
err = errors.New("invalid day '" + day + "', should be YYYYMMDD")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
config, err := db.Config()
|
var daoList = []*HTTPAccessLogDAOWrapper{}
|
||||||
if err != nil {
|
|
||||||
return "", false, err
|
for _, d := range httpAccessLogDAOMapping {
|
||||||
|
daoList = append(daoList, d)
|
||||||
}
|
}
|
||||||
|
|
||||||
tableName = "edgeNSAccessLogs_" + day
|
var l = len(daoList)
|
||||||
cacheKey := tableName + "_" + fmt.Sprintf("%d", crc32.ChecksumIEEE([]byte(config.Dsn)))
|
if l == 0 {
|
||||||
|
return
|
||||||
accessLogLocker.RLock()
|
|
||||||
_, ok = nsAccessLogTableMapping[cacheKey]
|
|
||||||
accessLogLocker.RUnlock()
|
|
||||||
if ok {
|
|
||||||
return tableName, true, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tableNames, err := db.TableNames()
|
if l == 1 {
|
||||||
if err != nil {
|
return daoList[0]
|
||||||
return tableName, false, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return tableName, utils.ContainsStringInsensitive(tableNames, tableName), nil
|
return daoList[rands.Int(0, l-1)]
|
||||||
}
|
|
||||||
|
|
||||||
func findNSAccessLogTable(db *dbs.DB, day string, force bool) (string, error) {
|
|
||||||
config, err := db.Config()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
tableName := "edgeNSAccessLogs_" + day
|
|
||||||
cacheKey := tableName + "_" + fmt.Sprintf("%d", crc32.ChecksumIEEE([]byte(config.Dsn)))
|
|
||||||
|
|
||||||
if !force {
|
|
||||||
accessLogLocker.RLock()
|
|
||||||
_, ok := nsAccessLogTableMapping[cacheKey]
|
|
||||||
accessLogLocker.RUnlock()
|
|
||||||
if ok {
|
|
||||||
return tableName, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tableNames, err := db.TableNames()
|
|
||||||
if err != nil {
|
|
||||||
return tableName, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if utils.ContainsStringInsensitive(tableNames, tableName) {
|
|
||||||
accessLogLocker.Lock()
|
|
||||||
nsAccessLogTableMapping[cacheKey] = true
|
|
||||||
accessLogLocker.Unlock()
|
|
||||||
return tableName, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建表格
|
|
||||||
_, err = db.Exec("CREATE TABLE `" + tableName + "` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `nodeId` int(11) unsigned DEFAULT '0' COMMENT '节点ID',\n `domainId` int(11) unsigned DEFAULT '0' COMMENT '域名ID',\n `recordId` int(11) unsigned DEFAULT '0' COMMENT '记录ID',\n `content` json DEFAULT NULL COMMENT '访问数据',\n `requestId` varchar(128) DEFAULT NULL COMMENT '请求ID',\n `createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',\n `remoteAddr` varchar(128) DEFAULT NULL COMMENT 'IP',\n PRIMARY KEY (`id`),\n KEY `nodeId` (`nodeId`),\n KEY `domainId` (`domainId`),\n KEY `recordId` (`recordId`),\n KEY `requestId` (`requestId`),\n KEY `remoteAddr` (`remoteAddr`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='域名服务访问日志';")
|
|
||||||
if err != nil {
|
|
||||||
return tableName, err
|
|
||||||
}
|
|
||||||
|
|
||||||
accessLogLocker.Lock()
|
|
||||||
nsAccessLogTableMapping[cacheKey] = true
|
|
||||||
accessLogLocker.Unlock()
|
|
||||||
|
|
||||||
return tableName, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DBNodeInitializer 初始化数据库连接
|
// DBNodeInitializer 初始化数据库连接
|
||||||
@@ -209,14 +123,14 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
nodeIds := []int64{}
|
var nodeIds = []int64{}
|
||||||
for _, node := range dbNodes {
|
for _, node := range dbNodes {
|
||||||
nodeIds = append(nodeIds, int64(node.Id))
|
nodeIds = append(nodeIds, int64(node.Id))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 关掉老的
|
// 关掉老的
|
||||||
accessLogLocker.Lock()
|
accessLogLocker.Lock()
|
||||||
closingDbs := []*dbs.DB{}
|
var closingDbs = []*dbs.DB{}
|
||||||
for nodeId, db := range accessLogDBMapping {
|
for nodeId, db := range accessLogDBMapping {
|
||||||
if !lists.ContainsInt64(nodeIds, nodeId) {
|
if !lists.ContainsInt64(nodeIds, nodeId) {
|
||||||
closingDbs = append(closingDbs, db)
|
closingDbs = append(closingDbs, db)
|
||||||
@@ -233,12 +147,12 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
|
|
||||||
// 启动新的
|
// 启动新的
|
||||||
for _, node := range dbNodes {
|
for _, node := range dbNodes {
|
||||||
nodeId := int64(node.Id)
|
var nodeId = int64(node.Id)
|
||||||
accessLogLocker.Lock()
|
accessLogLocker.Lock()
|
||||||
db, ok := accessLogDBMapping[nodeId]
|
db, ok := accessLogDBMapping[nodeId]
|
||||||
accessLogLocker.Unlock()
|
accessLogLocker.Unlock()
|
||||||
|
|
||||||
dsn := node.Username + ":" + node.Password + "@tcp(" + node.Host + ":" + fmt.Sprintf("%d", node.Port) + ")/" + node.Database + "?charset=utf8mb4&timeout=10s"
|
var dsn = node.Username + ":" + node.Password + "@tcp(" + node.Host + ":" + fmt.Sprintf("%d", node.Port) + ")/" + node.Database + "?charset=utf8mb4&timeout=10s"
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
// 检查配置是否有变化
|
// 检查配置是否有变化
|
||||||
@@ -283,7 +197,7 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
daoObject := dbs.DAOObject{
|
var daoObject = dbs.DAOObject{
|
||||||
Instance: db,
|
Instance: db,
|
||||||
DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")",
|
DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")",
|
||||||
Table: tableDef.Name,
|
Table: tableDef.Name,
|
||||||
@@ -298,59 +212,19 @@ func (this *DBNodeInitializer) loop() error {
|
|||||||
|
|
||||||
accessLogLocker.Lock()
|
accessLogLocker.Lock()
|
||||||
accessLogDBMapping[nodeId] = db
|
accessLogDBMapping[nodeId] = db
|
||||||
dao := &HTTPAccessLogDAO{
|
var dao = &HTTPAccessLogDAO{
|
||||||
DAOObject: daoObject,
|
DAOObject: daoObject,
|
||||||
}
|
}
|
||||||
httpAccessLogDAOMapping[nodeId] = &HTTPAccessLogDAOWrapper{
|
httpAccessLogDAOMapping[nodeId] = &HTTPAccessLogDAOWrapper{
|
||||||
DAO: dao,
|
DAO: dao,
|
||||||
NodeId: nodeId,
|
NodeId: nodeId,
|
||||||
|
IsLocal: dbutils.IsLocalAddr(node.Host),
|
||||||
}
|
}
|
||||||
accessLogLocker.Unlock()
|
accessLogLocker.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// nsAccessLog
|
// 扩展
|
||||||
{
|
initAccessLogDAO(db, node)
|
||||||
tableName, err := findNSAccessLogTable(db, timeutil.Format("Ymd"), false)
|
|
||||||
if err != nil {
|
|
||||||
if !strings.Contains(err.Error(), "1050") { // 非表格已存在错误
|
|
||||||
remotelogs.Error("DB_NODE", "create first table in database node failed: "+err.Error())
|
|
||||||
|
|
||||||
// 创建节点日志
|
|
||||||
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix(), "", nil)
|
|
||||||
if createLogErr != nil {
|
|
||||||
remotelogs.Error("NODE_LOG", createLogErr.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
daoObject := dbs.DAOObject{
|
|
||||||
Instance: db,
|
|
||||||
DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")",
|
|
||||||
Table: tableName,
|
|
||||||
PkName: "id",
|
|
||||||
Model: new(NSAccessLog),
|
|
||||||
}
|
|
||||||
err = daoObject.Init()
|
|
||||||
if err != nil {
|
|
||||||
remotelogs.Error("DB_NODE", "initialize dao failed: "+err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
accessLogLocker.Lock()
|
|
||||||
accessLogDBMapping[nodeId] = db
|
|
||||||
dao := &NSAccessLogDAO{
|
|
||||||
DAOObject: daoObject,
|
|
||||||
}
|
|
||||||
nsAccessLogDAOMapping[nodeId] = &NSAccessLogDAOWrapper{
|
|
||||||
DAO: dao,
|
|
||||||
NodeId: nodeId,
|
|
||||||
}
|
|
||||||
accessLogLocker.Unlock()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
11
internal/db/models/db_node_initializer_ext.go
Normal file
11
internal/db/models/db_node_initializer_ext.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||||
|
//go:build !plus
|
||||||
|
|
||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/iwind/TeaGo/dbs"
|
||||||
|
|
||||||
|
var nsAccessLogDAOMapping = map[int64]any{} // dbNodeId => DAO
|
||||||
|
|
||||||
|
func initAccessLogDAO(db *dbs.DB, node *DBNode) {
|
||||||
|
}
|
||||||
@@ -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
|
||||||
@@ -133,11 +133,28 @@ func (this *DNSDomainDAO) FindAllEnabledDomainsWithProviderId(tx *dbs.Tx, provid
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListDomains 列出单页域名
|
||||||
|
func (this *DNSDomainDAO) ListDomains(tx *dbs.Tx, providerId int64, isDeleted bool, isUp bool, offset int64, size int64) (result []*DNSDomain, err error) {
|
||||||
|
_, err = this.Query(tx).
|
||||||
|
State(DNSDomainStateEnabled).
|
||||||
|
Attr("providerId", providerId).
|
||||||
|
Attr("isDeleted", isDeleted).
|
||||||
|
Attr("isUp", isUp).
|
||||||
|
AscPk().
|
||||||
|
Offset(offset).
|
||||||
|
Limit(size).
|
||||||
|
Slice(&result).
|
||||||
|
FindAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// CountAllEnabledDomainsWithProviderId 计算某个服务商下的域名数量
|
// CountAllEnabledDomainsWithProviderId 计算某个服务商下的域名数量
|
||||||
func (this *DNSDomainDAO) CountAllEnabledDomainsWithProviderId(tx *dbs.Tx, providerId int64) (int64, error) {
|
func (this *DNSDomainDAO) CountAllEnabledDomainsWithProviderId(tx *dbs.Tx, providerId int64, isDeleted bool, isUp bool) (int64, error) {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
State(DNSDomainStateEnabled).
|
State(DNSDomainStateEnabled).
|
||||||
Attr("providerId", providerId).
|
Attr("providerId", providerId).
|
||||||
|
Attr("isDeleted", isDeleted).
|
||||||
|
Attr("isUp", isUp).
|
||||||
Count()
|
Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +163,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 +175,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 +188,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).
|
||||||
|
|||||||
@@ -56,11 +56,13 @@ func (this *DNSTaskDAO) CreateDNSTask(tx *dbs.Tx, clusterId int64, serverId int6
|
|||||||
"isDone": false,
|
"isDone": false,
|
||||||
"isOk": false,
|
"isOk": false,
|
||||||
"error": "",
|
"error": "",
|
||||||
|
"version": time.Now().UnixNano(),
|
||||||
}, maps.Map{
|
}, maps.Map{
|
||||||
"updatedAt": time.Now().Unix(),
|
"updatedAt": time.Now().Unix(),
|
||||||
"isDone": false,
|
"isDone": false,
|
||||||
"isOk": false,
|
"isOk": false,
|
||||||
"error": "",
|
"error": "",
|
||||||
|
"version": time.Now().UnixNano(),
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -76,8 +78,8 @@ func (this *DNSTaskDAO) CreateClusterRemoveTask(tx *dbs.Tx, clusterId int64, dom
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateNodeTask 生成节点任务
|
// CreateNodeTask 生成节点任务
|
||||||
func (this *DNSTaskDAO) CreateNodeTask(tx *dbs.Tx, nodeId int64, taskType DNSTaskType) error {
|
func (this *DNSTaskDAO) CreateNodeTask(tx *dbs.Tx, clusterId int64, nodeId int64, taskType DNSTaskType) error {
|
||||||
return this.CreateDNSTask(tx, 0, 0, nodeId, 0, "", taskType)
|
return this.CreateDNSTask(tx, clusterId, 0, nodeId, 0, "", taskType)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateServerTask 生成服务任务
|
// CreateServerTask 生成服务任务
|
||||||
@@ -94,6 +96,7 @@ func (this *DNSTaskDAO) CreateDomainTask(tx *dbs.Tx, domainId int64, taskType DN
|
|||||||
func (this *DNSTaskDAO) FindAllDoingTasks(tx *dbs.Tx) (result []*DNSTask, err error) {
|
func (this *DNSTaskDAO) FindAllDoingTasks(tx *dbs.Tx) (result []*DNSTask, err error) {
|
||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
Attr("isDone", 0).
|
Attr("isDone", 0).
|
||||||
|
Asc("version").
|
||||||
AscPk().
|
AscPk().
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
@@ -109,6 +112,7 @@ func (this *DNSTaskDAO) FindAllDoingOrErrorTasks(tx *dbs.Tx, nodeClusterId int64
|
|||||||
_, err = query.
|
_, err = query.
|
||||||
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
Where("(isDone=0 OR (isDone=1 AND isOk=0))").
|
||||||
Asc("updatedAt").
|
Asc("updatedAt").
|
||||||
|
Asc("version").
|
||||||
AscPk().
|
AscPk().
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
FindAll()
|
FindAll()
|
||||||
@@ -143,7 +147,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,10 +160,20 @@ 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
|
||||||
op.Error = ""
|
op.Error = ""
|
||||||
return this.Save(tx, op)
|
return this.Save(tx, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteDNSTasksWithClusterId 删除集群相关任务
|
||||||
|
func (this *DNSTaskDAO) DeleteDNSTasksWithClusterId(tx *dbs.Tx, clusterId int64) error {
|
||||||
|
if clusterId <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return this.Query(tx).
|
||||||
|
Attr("clusterId", clusterId).
|
||||||
|
DeleteQuickly()
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
func TestDNSTaskDAO_CreateDNSTask(t *testing.T) {
|
func TestDNSTaskDAO_CreateDNSTask(t *testing.T) {
|
||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
err := SharedDNSTaskDAO.CreateDNSTask(nil, 1, 2, 3, 0, "taskType")
|
err := SharedDNSTaskDAO.CreateDNSTask(nil, 1, 2, 3, 0, "cdn", "taskType")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,20 +13,22 @@ type DNSTask struct {
|
|||||||
IsDone bool `field:"isDone"` // 是否已完成
|
IsDone bool `field:"isDone"` // 是否已完成
|
||||||
IsOk bool `field:"isOk"` // 是否成功
|
IsOk bool `field:"isOk"` // 是否成功
|
||||||
Error string `field:"error"` // 错误信息
|
Error string `field:"error"` // 错误信息
|
||||||
|
Version uint64 `field:"version"` // 版本
|
||||||
}
|
}
|
||||||
|
|
||||||
type DNSTaskOperator struct {
|
type DNSTaskOperator struct {
|
||||||
Id interface{} // ID
|
Id any // ID
|
||||||
ClusterId interface{} // 集群ID
|
ClusterId any // 集群ID
|
||||||
ServerId interface{} // 服务ID
|
ServerId any // 服务ID
|
||||||
NodeId interface{} // 节点ID
|
NodeId any // 节点ID
|
||||||
DomainId interface{} // 域名ID
|
DomainId any // 域名ID
|
||||||
RecordName interface{} // 记录名
|
RecordName any // 记录名
|
||||||
Type interface{} // 任务类型
|
Type any // 任务类型
|
||||||
UpdatedAt interface{} // 更新时间
|
UpdatedAt any // 更新时间
|
||||||
IsDone interface{} // 是否已完成
|
IsDone any // 是否已完成
|
||||||
IsOk interface{} // 是否成功
|
IsOk any // 是否成功
|
||||||
Error interface{} // 错误信息
|
Error any // 错误信息
|
||||||
|
Version any // 版本
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDNSTaskOperator() *DNSTaskOperator {
|
func NewDNSTaskOperator() *DNSTaskOperator {
|
||||||
|
|||||||
@@ -15,9 +15,12 @@ import (
|
|||||||
|
|
||||||
// CheckClusterDNS 检查集群的DNS问题
|
// CheckClusterDNS 检查集群的DNS问题
|
||||||
// 藏这么深是避免package循环引用的问题
|
// 藏这么深是避免package循环引用的问题
|
||||||
func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSIssue, err error) {
|
func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster, checkNodeIssues bool) (issues []*pb.DNSIssue, err error) {
|
||||||
clusterId := int64(cluster.Id)
|
var clusterId = int64(cluster.Id)
|
||||||
domainId := int64(cluster.DnsDomainId)
|
var domainId = int64(cluster.DnsDomainId)
|
||||||
|
|
||||||
|
// 集群DNS设置
|
||||||
|
var clusterDNSConfig, _ = cluster.DecodeDNSConfig()
|
||||||
|
|
||||||
// 检查域名
|
// 检查域名
|
||||||
domain, err := dns.SharedDNSDomainDAO.FindEnabledDNSDomain(tx, domainId, nil)
|
domain, err := dns.SharedDNSDomainDAO.FindEnabledDNSDomain(tx, domainId, nil)
|
||||||
@@ -64,7 +67,7 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSI
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var dnsProvider = dnsclients.FindProvider(provider.Type)
|
var dnsProvider = dnsclients.FindProvider(provider.Type, int64(provider.Id))
|
||||||
if dnsProvider == nil {
|
if dnsProvider == nil {
|
||||||
issues = append(issues, &pb.DNSIssue{
|
issues = append(issues, &pb.DNSIssue{
|
||||||
Target: cluster.Name,
|
Target: cluster.Name,
|
||||||
@@ -101,47 +104,27 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSI
|
|||||||
// TODO 检查域名是否已解析
|
// TODO 检查域名是否已解析
|
||||||
|
|
||||||
// 检查节点
|
// 检查节点
|
||||||
nodes, err := models.SharedNodeDAO.FindAllEnabledNodesDNSWithClusterId(tx, clusterId, true)
|
if checkNodeIssues {
|
||||||
if err != nil {
|
nodes, err := models.SharedNodeDAO.FindAllEnabledNodesDNSWithClusterId(tx, clusterId, true, clusterDNSConfig != nil && clusterDNSConfig.IncludingLnNodes)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO 检查节点数量不能为0
|
|
||||||
|
|
||||||
for _, node := range nodes {
|
|
||||||
nodeId := int64(node.Id)
|
|
||||||
|
|
||||||
routeCodes, err := node.DNSRouteCodesForDomainId(domainId)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(routeCodes) == 0 && !hasDefaultRoute {
|
|
||||||
issues = append(issues, &pb.DNSIssue{
|
|
||||||
Target: node.Name,
|
|
||||||
TargetId: nodeId,
|
|
||||||
Type: "node",
|
|
||||||
Description: "没有选择节点所属线路",
|
|
||||||
Params: map[string]string{
|
|
||||||
"clusterName": cluster.Name,
|
|
||||||
"clusterId": numberutils.FormatInt64(clusterId),
|
|
||||||
},
|
|
||||||
MustFix: true,
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查线路是否在已有线路中
|
// TODO 检查节点数量不能为0
|
||||||
for _, routeCode := range routeCodes {
|
|
||||||
routeOk, err := domain.ContainsRouteCode(routeCode)
|
for _, node := range nodes {
|
||||||
|
nodeId := int64(node.Id)
|
||||||
|
|
||||||
|
routeCodes, err := node.DNSRouteCodesForDomainId(domainId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if !routeOk {
|
if len(routeCodes) == 0 && !hasDefaultRoute {
|
||||||
issues = append(issues, &pb.DNSIssue{
|
issues = append(issues, &pb.DNSIssue{
|
||||||
Target: node.Name,
|
Target: node.Name,
|
||||||
TargetId: nodeId,
|
TargetId: nodeId,
|
||||||
Type: "node",
|
Type: "node",
|
||||||
Description: "线路已经失效,请重新选择",
|
Description: "没有选择节点所属线路",
|
||||||
Params: map[string]string{
|
Params: map[string]string{
|
||||||
"clusterName": cluster.Name,
|
"clusterName": cluster.Name,
|
||||||
"clusterId": numberutils.FormatInt64(clusterId),
|
"clusterId": numberutils.FormatInt64(clusterId),
|
||||||
@@ -150,29 +133,51 @@ func CheckClusterDNS(tx *dbs.Tx, cluster *models.NodeCluster) (issues []*pb.DNSI
|
|||||||
})
|
})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 检查IP地址
|
// 检查线路是否在已有线路中
|
||||||
ipAddr, _, err := models.SharedNodeIPAddressDAO.FindFirstNodeAccessIPAddress(tx, nodeId, true, nodeconfigs.NodeRoleNode)
|
for _, routeCode := range routeCodes {
|
||||||
if err != nil {
|
routeOk, err := domain.ContainsRouteCode(routeCode)
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
if len(ipAddr) == 0 {
|
}
|
||||||
issues = append(issues, &pb.DNSIssue{
|
if !routeOk {
|
||||||
Target: node.Name,
|
issues = append(issues, &pb.DNSIssue{
|
||||||
TargetId: nodeId,
|
Target: node.Name,
|
||||||
Type: "node",
|
TargetId: nodeId,
|
||||||
Description: "没有设置IP地址",
|
Type: "node",
|
||||||
Params: map[string]string{
|
Description: "线路已经失效,请重新选择",
|
||||||
"clusterName": cluster.Name,
|
Params: map[string]string{
|
||||||
"clusterId": numberutils.FormatInt64(clusterId),
|
"clusterName": cluster.Name,
|
||||||
},
|
"clusterId": numberutils.FormatInt64(clusterId),
|
||||||
MustFix: true,
|
},
|
||||||
})
|
MustFix: true,
|
||||||
continue
|
})
|
||||||
}
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO 检查是否有解析记录
|
// 检查IP地址
|
||||||
|
ipAddr, _, err := models.SharedNodeIPAddressDAO.FindFirstNodeAccessIPAddress(tx, nodeId, true, nodeconfigs.NodeRoleNode)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(ipAddr) == 0 {
|
||||||
|
issues = append(issues, &pb.DNSIssue{
|
||||||
|
Target: node.Name,
|
||||||
|
TargetId: nodeId,
|
||||||
|
Type: "node",
|
||||||
|
Description: "没有设置IP地址",
|
||||||
|
Params: map[string]string{
|
||||||
|
"clusterName": cluster.Name,
|
||||||
|
"clusterId": numberutils.FormatInt64(clusterId),
|
||||||
|
},
|
||||||
|
MustFix: true,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO 检查是否有解析记录
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -195,7 +200,7 @@ func FindDefaultDomainRoute(tx *dbs.Tx, domain *dns.DNSDomain) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New("decode provider params failed: " + err.Error())
|
return "", errors.New("decode provider params failed: " + err.Error())
|
||||||
}
|
}
|
||||||
var dnsProvider = dnsclients.FindProvider(provider.Type)
|
var dnsProvider = dnsclients.FindProvider(provider.Type, int64(provider.Id))
|
||||||
if dnsProvider == nil {
|
if dnsProvider == nil {
|
||||||
return "", errors.New("not supported provider type '" + provider.Type + "'")
|
return "", errors.New("not supported provider type '" + provider.Type + "'")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ func TestNodeClusterDAO_CheckClusterDNS(t *testing.T) {
|
|||||||
t.Log("cluster not found, skip the test")
|
t.Log("cluster not found, skip the test")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
issues, err := CheckClusterDNS(tx, cluster)
|
issues, err := CheckClusterDNS(tx, cluster, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user