Compare commits
499 Commits
v0.1.0
...
v0.9.2-bet
Author | SHA1 | Date | |
---|---|---|---|
|
3eb2af6155 | ||
|
5fabefd315 | ||
|
b84d10d64f | ||
|
b0f5456d68 | ||
|
1c846b90b8 | ||
|
c267553287 | ||
|
c8bde076d8 | ||
|
cff97d746c | ||
|
1582b46bb9 | ||
|
a3d5945611 | ||
|
b63b7936bc | ||
|
337ffeb1a2 | ||
|
279b6f87a0 | ||
|
59e3c9347b | ||
|
d51bc63419 | ||
|
d3f51a5af3 | ||
|
7f4b668dbf | ||
|
e8c121c6b1 | ||
|
7299c0e761 | ||
|
ea26eb1e24 | ||
|
5e109091f5 | ||
|
d00293aaf9 | ||
|
0aacc244c4 | ||
|
d7bcc7988c | ||
|
14eed7265c | ||
|
4f9a1c0484 | ||
|
62554cfba0 | ||
|
9809b407d5 | ||
|
9e53ec289c | ||
|
a600be6949 | ||
|
e909ca552c | ||
|
ef5cad6c7a | ||
|
7ac34935a6 | ||
|
42de009e30 | ||
|
ae7fe993f7 | ||
|
b741f58189 | ||
|
f12ac2f4d8 | ||
|
68fa1110f1 | ||
|
ce232937ec | ||
|
094d19be00 | ||
|
32026eff2c | ||
|
6b6e963458 | ||
|
7a290fe807 | ||
|
fdb16a4cb6 | ||
|
b06247201d | ||
|
d3ac346c85 | ||
|
8c9e371df8 | ||
|
a8b5bb4a68 | ||
|
ce1a6f2d8c | ||
|
73096eae13 | ||
|
24fc03f378 | ||
|
c26675206e | ||
|
ff018a718e | ||
|
5a60398435 | ||
|
32ce3f04d0 | ||
|
5ef13b1bff | ||
|
2bc2ade5d8 | ||
|
ea5e31fd39 | ||
|
50c30657ac | ||
|
b1ae25ae2f | ||
|
c8b34bd772 | ||
|
87dcc63b6f | ||
|
eb8d55040c | ||
|
46721b1453 | ||
|
36cb865bad | ||
|
4a7d534981 | ||
|
45ed1c34f9 | ||
|
c2ddb7331d | ||
|
2d375869df | ||
|
890ff5753f | ||
|
54be85ef03 | ||
|
2e47957f7f | ||
|
2b715774d8 | ||
|
69926a4ba1 | ||
|
6dcb79b30a | ||
|
16b0236c6f | ||
|
6fdc0e54d5 | ||
|
9f40bb40ef | ||
|
7de9d9f16d | ||
|
86ee88a20c | ||
|
5f5ab3fbca | ||
|
88dd3c8860 | ||
|
7032ebdc85 | ||
|
9fe31212e4 | ||
|
7b19010892 | ||
|
77518a3a0c | ||
|
c68374f4f0 | ||
|
a8d0a39ca0 | ||
|
a77fe77e79 | ||
|
9e58f3121f | ||
|
6915d94f31 | ||
|
a83999013e | ||
|
7e8eea5f80 | ||
|
6f7fcfc6ee | ||
|
8536e2f1d4 | ||
|
28489dcbc6 | ||
|
84286de8a4 | ||
|
522f723b51 | ||
|
c6ef0a28b4 | ||
|
ea7a487c26 | ||
|
e66a842e77 | ||
|
ef30451515 | ||
|
42f60a4d9d | ||
|
97840bef98 | ||
|
cad824d53c | ||
|
59d343f17c | ||
|
d6154bc621 | ||
|
d277f1084f | ||
|
e3b23c5391 | ||
|
b1890a2c8a | ||
|
bcd4fa1872 | ||
|
3a2482f1d6 | ||
|
adebd4b844 | ||
|
bf7491346a | ||
|
201a62144e | ||
|
3dbc369e6b | ||
|
812eb587bb | ||
|
ec1cf3fcda | ||
|
a533ee0a78 | ||
|
7b83154245 | ||
|
c74ba43cb0 | ||
|
7aa8dc0294 | ||
|
509a7f1ab1 | ||
|
8c6173d3d8 | ||
|
6a1aa35f18 | ||
|
ea5bc1088c | ||
|
3ba9ed8266 | ||
|
f71616c4eb | ||
|
2093bd0bef | ||
|
b8f0f4c618 | ||
|
df1fe6f838 | ||
|
82dfb5d04f | ||
|
2c8474a478 | ||
|
82ad948b08 | ||
|
ac0b9ee4ee | ||
|
3d88c617bf | ||
|
d1b209f18f | ||
|
219a74b118 | ||
|
83263ff635 | ||
|
6f875001f2 | ||
|
4a412fe0ee | ||
|
adb8b45985 | ||
|
a2a77d0852 | ||
|
3aecbda5f4 | ||
|
e8ec65dec2 | ||
|
e03bd60b1d | ||
|
c2642bc183 | ||
|
50fa86d780 | ||
|
427ad7f7f5 | ||
|
f6fa2fe46b | ||
|
290f78d7f3 | ||
|
edb2d2e19b | ||
|
e483da10e1 | ||
|
4c37ee5550 | ||
|
92f39dfc96 | ||
|
3c5acd31fb | ||
|
4e2ce12f6c | ||
|
19bda654ff | ||
|
8b1098917f | ||
|
68bb5d24b6 | ||
|
10ca7c9c85 | ||
|
a48f071e0a | ||
|
816dadae29 | ||
|
157837e100 | ||
|
56d3b16a10 | ||
|
11feb6d4a5 | ||
|
49db8ccff8 | ||
|
d5a729b4e4 | ||
|
48bebadc6b | ||
|
5586b27319 | ||
|
a5b4463280 | ||
|
38afab2440 | ||
|
0126333605 | ||
|
00281dbfa5 | ||
|
7b3507d962 | ||
|
f2c649d65b | ||
|
b5c14e5f29 | ||
|
5b220838d9 | ||
|
89e1828b5e | ||
|
b9b0fc811d | ||
|
091dc69f1d | ||
|
1547201049 | ||
|
af52cc21bc | ||
|
e72994c0b8 | ||
|
0dad4429a3 | ||
|
047e478a5a | ||
|
9ccaa039d0 | ||
|
ab9a1b97a7 | ||
|
b8d3eedd9d | ||
|
86a4ed8948 | ||
|
ffb529897f | ||
|
d5f88affcd | ||
|
6722541fa5 | ||
|
b9bd74ed1f | ||
|
b8c3d33f2e | ||
|
e761c1e111 | ||
|
1dceffc327 | ||
|
9f8601221d | ||
|
6a69377568 | ||
|
8837a07fac | ||
|
75a8358457 | ||
|
cd21dec605 | ||
|
1efd007e94 | ||
|
973541c132 | ||
|
dd8c114632 | ||
|
ab3b8b34d9 | ||
|
ecb5094780 | ||
|
a9e31a0a3b | ||
|
88e5546a3b | ||
|
5c41367383 | ||
|
b0569b33cc | ||
|
e90c2aae6c | ||
|
1ad19fe06b | ||
|
50121b8ff2 | ||
|
d1414c6b5d | ||
|
06931551d0 | ||
|
3aff1daaf2 | ||
|
918859cc62 | ||
|
9f98f71210 | ||
|
116c04f8b1 | ||
|
4b36577b00 | ||
|
f208be9126 | ||
|
2b9c0fdca8 | ||
|
04d025c68b | ||
|
7f1e769377 | ||
|
c76b074893 | ||
|
54f7a59a91 | ||
|
25cb1fc19a | ||
|
6a997bfd46 | ||
|
68076f58eb | ||
|
e24aa2d900 | ||
|
9dad2c08a5 | ||
|
8ede613488 | ||
|
f46a30488a | ||
|
b06c035cce | ||
|
730c34b2a4 | ||
|
c38f1e9b8c | ||
|
a450fe3eef | ||
|
e9c55a0853 | ||
|
f560fc459b | ||
|
d4f266de66 | ||
|
3e491d33c6 | ||
|
77e507fab7 | ||
|
9d5b9c4ded | ||
|
a0830f0440 | ||
|
3de75285de | ||
|
cde46c5765 | ||
|
f325970089 | ||
|
f6996363e3 | ||
|
90aa6c272d | ||
|
2964b34087 | ||
|
625e4eecf4 | ||
|
522be82a31 | ||
|
ccce143f85 | ||
|
01431b2230 | ||
|
7a92aa8dff | ||
|
2b580067a2 | ||
|
500a11612c | ||
|
64fc4d082e | ||
|
cf507d204b | ||
|
fd7242dbba | ||
|
9a712ca489 | ||
|
e44e1dcf2a | ||
|
fce96753c5 | ||
|
77efe7583d | ||
|
1cbe9bfc30 | ||
|
2e7afa9c35 | ||
|
a58507f5a6 | ||
|
1989b235fe | ||
|
21b047b307 | ||
|
2daad88159 | ||
|
7c86513b0c | ||
|
4b41e6c5b9 | ||
|
684fea69d0 | ||
|
85e2ba0b0f | ||
|
3fff44dde5 | ||
|
8b2b058450 | ||
|
f0305415ae | ||
|
ea84a29350 | ||
|
6c5969c5ea | ||
|
f267891f19 | ||
|
d1167650a3 | ||
|
f7c8fb8d9d | ||
|
e780afe18c | ||
|
aadfa7b70c | ||
|
73d5a1be69 | ||
|
3cb22172b1 | ||
|
f42176299d | ||
|
edcfd5d8f2 | ||
|
3697227f7a | ||
|
11cbfbc91d | ||
|
4518959bf9 | ||
|
4f1d345256 | ||
|
7fc9f73512 | ||
|
5c3dfeb3af | ||
|
ca4626879d | ||
|
a0065ade42 | ||
|
a47cdc9a00 | ||
|
c07914d568 | ||
|
df94afdfd0 | ||
|
b4f88e154c | ||
|
e4270f724b | ||
|
e9a27fd833 | ||
|
9865d54f5c | ||
|
8453052d80 | ||
|
e0d5638c17 | ||
|
44adbe71ed | ||
|
246efe430d | ||
|
80ca45c6ea | ||
|
dfe61667cb | ||
|
d9a3b0152b | ||
|
6dcdc9766a | ||
|
a65d3d1054 | ||
|
831b6786c5 | ||
|
b4e2550911 | ||
|
60019f3cf4 | ||
|
d4f1793675 | ||
|
b2c171c39c | ||
|
35dd1dc4e1 | ||
|
bc64a9f8f6 | ||
|
65463fee6c | ||
|
8187fb548f | ||
|
5bd55d1fa8 | ||
|
42de4e6fd0 | ||
|
970b7d557a | ||
|
58cdcbe192 | ||
|
0ffa9578cf | ||
|
903554533b | ||
|
5010d2ecbd | ||
|
cc99a936a6 | ||
|
fd54783d48 | ||
|
af9845a012 | ||
|
d663c43f6d | ||
|
b9b852123e | ||
|
0d4d2bb3c4 | ||
|
1bb76e5d60 | ||
|
bf898ff34b | ||
|
0a35ee18f4 | ||
|
2e764f7ee7 | ||
|
d91d1a127e | ||
|
fef25da35c | ||
|
f3d6d23c3e | ||
|
88c1d09c8f | ||
|
12febf3723 | ||
|
c77a765ae9 | ||
|
6daf4e1b03 | ||
|
2a66df55b0 | ||
|
0f783cfaf6 | ||
|
afe447ada7 | ||
|
981f39799a | ||
|
60084d499a | ||
|
583ca4d635 | ||
|
3937d1eb65 | ||
|
c943b9ee9b | ||
|
d78deafc0c | ||
|
2bdaf4a52e | ||
|
d065a1f97f | ||
|
ba446b8a5c | ||
|
73467a39d9 | ||
|
96276b8cac | ||
|
d17271da2e | ||
|
dd16d52c95 | ||
|
eec3006b35 | ||
|
35c6d80c8d | ||
|
88a3d35784 | ||
|
712f57940c | ||
|
2ad65039f3 | ||
|
1940fcca87 | ||
|
e4f959cb69 | ||
|
8ff77626d0 | ||
|
ea4dda0fca | ||
|
5ffea2339e | ||
|
a8038f8fe9 | ||
|
e7ef29c4ad | ||
|
884f41d2f8 | ||
|
7e95b1261a | ||
|
6daf9cbfa3 | ||
|
31a5337ddf | ||
|
5bb44385b1 | ||
|
66ef639956 | ||
|
f6fd01d1e5 | ||
|
f50f00f464 | ||
|
1b4fe6e450 | ||
|
5289aa92eb | ||
|
d9af1254a4 | ||
|
94dfb8bc44 | ||
|
3243cb98ad | ||
|
67d44805ce | ||
|
22b42fa8b6 | ||
|
b0ff8ba1be | ||
|
1611f795d1 | ||
|
440b178232 | ||
|
42c898cfea | ||
|
1564f6e56c | ||
|
993cd40c41 | ||
|
0ef18ee2f9 | ||
|
a616e42432 | ||
|
cd8922237f | ||
|
4f3f22e6d3 | ||
|
44a7a4659f | ||
|
2b507206e6 | ||
|
b6ed5b14f2 | ||
|
ce2b657ab3 | ||
|
65bbb216fc | ||
|
2f593cc533 | ||
|
d6aa586349 | ||
|
ab58f21b3b | ||
|
b704e089b6 | ||
|
0512ce197a | ||
|
0fa92e8665 | ||
|
af6f87ee0f | ||
|
0863313eff | ||
|
061f625448 | ||
|
f342b12372 | ||
|
c744983cbc | ||
|
33b30ecb21 | ||
|
28b5119f50 | ||
|
54a0cb7928 | ||
|
c8f090c9e3 | ||
|
07163aa33b | ||
|
3368eced2a | ||
|
a9144c9766 | ||
|
3c3b3110a2 | ||
|
afdbe678ce | ||
|
0ab4304445 | ||
|
59acda4706 | ||
|
cf8487f8f4 | ||
|
fa4c122c11 | ||
|
eb5c5b8da2 | ||
|
eacb4eace7 | ||
|
b0756c961b | ||
|
d9d2a0d087 | ||
|
e8fd96029e | ||
|
bba2690b3e | ||
|
ed569dfba1 | ||
|
d4b8cc3576 | ||
|
b376552085 | ||
|
9992089155 | ||
|
9cf5b5f38e | ||
|
0d59b333a7 | ||
|
dd32bd4899 | ||
|
c98cacc242 | ||
|
05202c8524 | ||
|
4b00c12ef3 | ||
|
8c70cc2e10 | ||
|
fae0d269d9 | ||
|
d8eb8ab7ed | ||
|
9b509c33f3 | ||
|
157001be18 | ||
|
7fae5423b7 | ||
|
53c7fee5be | ||
|
ae246ed420 | ||
|
130ec45f2a | ||
|
5e5395dabe | ||
|
fdca297900 | ||
|
ac412ea6c7 | ||
|
656eca93a7 | ||
|
bbb35d2138 | ||
|
b314b8baa6 | ||
|
71c9dc2eb5 | ||
|
0ceea0fb57 | ||
|
04278286ee | ||
|
df2f56817a | ||
|
d9bc819108 | ||
|
63175cadb5 | ||
|
3994d3032a | ||
|
cb6813e922 | ||
|
66aeaefcd8 | ||
|
7342f1f1a9 | ||
|
7a2d8a3ad0 | ||
|
b5927a11d3 | ||
|
273b3ee1eb | ||
|
016a43b792 | ||
|
ddba116fbf | ||
|
dd1eae5d32 | ||
|
879b9114ac | ||
|
e5c4a47a37 | ||
|
a0669e1e32 | ||
|
ea7d27b4f0 | ||
|
9f2e9de49a | ||
|
8709dbaba1 | ||
|
4928ad5f62 | ||
|
0f91c779e3 | ||
|
3464d1d10e | ||
|
1579e92ba1 | ||
|
48327fe8d3 | ||
|
ef7723b03b | ||
|
0baab68bb2 | ||
|
665732fc03 | ||
|
edb0fe2dc9 | ||
|
1c6572f5e3 | ||
|
103329c3d0 | ||
|
d40b753871 | ||
|
fa5a58e98d | ||
|
62f30c05ff | ||
|
c02ffc27c0 | ||
|
a4e09e7719 | ||
|
631e49bd41 | ||
|
ef95b1f927 |
5
.codecov.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
ignore:
|
||||||
|
- "screenshot"
|
||||||
|
- "web"
|
||||||
|
- "server/conf"
|
||||||
|
- "server/files"
|
35
.github/ISSUE_TEMPLATE/00-question.md
vendored
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
---
|
||||||
|
name: 问题描述
|
||||||
|
about: 问题描述
|
||||||
|
title: "affected/package: "
|
||||||
|
---
|
||||||
|
|
||||||
|
<!--
|
||||||
|
请先填写下面的问题,再填写具体遇到的问题,感谢!
|
||||||
|
-->
|
||||||
|
|
||||||
|
### 使用的anylink版本 ?
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
./anylink tool -v
|
||||||
|
管理后台也可以查看
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
### 使用操作系统的类型和版本?
|
||||||
|
如: centos 7.9
|
||||||
|
<pre>
|
||||||
|
cat /etc/issue
|
||||||
|
cat /etc/redhat-release
|
||||||
|
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
### 使用linux 内核版本?
|
||||||
|
<pre>
|
||||||
|
uname -a
|
||||||
|
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
### 具体遇到的问题,可上传截图
|
||||||
|
|
||||||
|
|
71
.github/workflows/codeql-analysis.yml
vendored
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
# For most projects, this workflow file will not need changing; you simply need
|
||||||
|
# to commit it to your repository.
|
||||||
|
#
|
||||||
|
# You may wish to alter this file to override the set of languages analyzed,
|
||||||
|
# or to provide custom queries or build logic.
|
||||||
|
#
|
||||||
|
# ******** NOTE ********
|
||||||
|
# We have attempted to detect the languages in your repository. Please check
|
||||||
|
# the `language` matrix defined below to confirm you have the correct set of
|
||||||
|
# supported CodeQL languages.
|
||||||
|
#
|
||||||
|
name: "CodeQL"
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ "main", "dev" ]
|
||||||
|
pull_request:
|
||||||
|
# The branches below must be a subset of the branches above
|
||||||
|
branches: [ "main", "dev" ]
|
||||||
|
schedule:
|
||||||
|
- cron: '32 12 * * 5'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
analyze:
|
||||||
|
name: Analyze
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
actions: read
|
||||||
|
contents: read
|
||||||
|
security-events: write
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
language: [ 'go', 'javascript' ]
|
||||||
|
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
||||||
|
# Learn more:
|
||||||
|
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
# Initializes the CodeQL tools for scanning.
|
||||||
|
- name: Initialize CodeQL
|
||||||
|
uses: github/codeql-action/init@v2
|
||||||
|
with:
|
||||||
|
languages: ${{ matrix.language }}
|
||||||
|
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||||
|
# By default, queries listed here will override any specified in a config file.
|
||||||
|
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||||
|
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
||||||
|
|
||||||
|
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||||
|
# If this step fails, then you should remove it and run the build manually (see below)
|
||||||
|
- name: Autobuild
|
||||||
|
uses: github/codeql-action/autobuild@v2
|
||||||
|
|
||||||
|
# ℹ️ Command-line programs to run using the OS shell.
|
||||||
|
# 📚 https://git.io/JvXDl
|
||||||
|
|
||||||
|
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
||||||
|
# and modify them (or add more) to build your code if your project
|
||||||
|
# uses a compiled language
|
||||||
|
|
||||||
|
#- run: |
|
||||||
|
# make bootstrap
|
||||||
|
# make release
|
||||||
|
|
||||||
|
- name: Perform CodeQL Analysis
|
||||||
|
uses: github/codeql-action/analyze@v2
|
25
.github/workflows/go.yml
vendored
@@ -2,9 +2,9 @@ name: Go
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ master ]
|
branches: [ "main", "dev" ]
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [ master ]
|
branches: [ "main", "dev" ]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ jobs:
|
|||||||
- name: Set up Go 1.x
|
- name: Set up Go 1.x
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.15
|
go-version: 1.18
|
||||||
id: go
|
id: go
|
||||||
|
|
||||||
- name: Check out code into the Go module directory
|
- name: Check out code into the Go module directory
|
||||||
@@ -24,10 +24,23 @@ jobs:
|
|||||||
|
|
||||||
- name: Get dependencies
|
- name: Get dependencies
|
||||||
run: |
|
run: |
|
||||||
|
cd server
|
||||||
go get -v -t -d ./...
|
go get -v -t -d ./...
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: go build -v .
|
run: |
|
||||||
|
cd server
|
||||||
|
mkdir ui
|
||||||
|
touch ui/index.html
|
||||||
|
go build -v -o anylink -ldflags "-X main.CommitId=`git rev-parse HEAD`"
|
||||||
|
./anylink tool -v
|
||||||
|
|
||||||
- name: Test
|
- name: Test coverage
|
||||||
run: go test -v .
|
run: |
|
||||||
|
cd server
|
||||||
|
go test -race -coverprofile=coverage.txt -covermode=atomic -v ./...
|
||||||
|
|
||||||
|
- name: Upload coverage to Codecov
|
||||||
|
run: |
|
||||||
|
cd server
|
||||||
|
bash <(curl -s https://codecov.io/bash)
|
||||||
|
20
.gitignore
vendored
@@ -1,19 +1,5 @@
|
|||||||
# Binaries for programs and plugins
|
# Binaries for programs and plugins
|
||||||
*.exe
|
|
||||||
*.exe~
|
|
||||||
*.dll
|
|
||||||
*.so
|
|
||||||
*.dylib
|
|
||||||
|
|
||||||
# Test binary, built with `go test -c`
|
|
||||||
*.test
|
|
||||||
|
|
||||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
|
||||||
*.out
|
|
||||||
|
|
||||||
# Dependency directories (remove the comment below to include it)
|
|
||||||
vendor/
|
|
||||||
|
|
||||||
ui/
|
|
||||||
.idea/
|
.idea/
|
||||||
anylink
|
anylink-deploy
|
||||||
|
anylink-deploy.tar.gz
|
||||||
|
|
||||||
|
674
LICENSE
@@ -1,21 +1,661 @@
|
|||||||
MIT License
|
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 19 November 2007
|
||||||
|
|
||||||
Copyright (c) 2020 bjdgyc
|
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Preamble
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
The GNU Affero General Public License is a free, copyleft license for
|
||||||
copies or substantial portions of the Software.
|
software and other kinds of works, specifically designed to ensure
|
||||||
|
cooperation with the community in the case of network server software.
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
The licenses for most software and other practical works are designed
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
to take away your freedom to share and change the works. By contrast,
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
our General Public Licenses are intended to guarantee your freedom to
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
share and change all versions of a program--to make sure it remains free
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
software for all its users.
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
SOFTWARE.
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
|
have the freedom to distribute copies of free software (and charge for
|
||||||
|
them if you wish), that you receive source code or can get it if you
|
||||||
|
want it, that you can change the software or use pieces of it in new
|
||||||
|
free programs, and that you know you can do these things.
|
||||||
|
|
||||||
|
Developers that use our General Public Licenses protect your rights
|
||||||
|
with two steps: (1) assert copyright on the software, and (2) offer
|
||||||
|
you this License which gives you legal permission to copy, distribute
|
||||||
|
and/or modify the software.
|
||||||
|
|
||||||
|
A secondary benefit of defending all users' freedom is that
|
||||||
|
improvements made in alternate versions of the program, if they
|
||||||
|
receive widespread use, become available for other developers to
|
||||||
|
incorporate. Many developers of free software are heartened and
|
||||||
|
encouraged by the resulting cooperation. However, in the case of
|
||||||
|
software used on network servers, this result may fail to come about.
|
||||||
|
The GNU General Public License permits making a modified version and
|
||||||
|
letting the public access it on a server without ever releasing its
|
||||||
|
source code to the public.
|
||||||
|
|
||||||
|
The GNU Affero General Public License is designed specifically to
|
||||||
|
ensure that, in such cases, the modified source code becomes available
|
||||||
|
to the community. It requires the operator of a network server to
|
||||||
|
provide the source code of the modified version running there to the
|
||||||
|
users of that server. Therefore, public use of a modified version, on
|
||||||
|
a publicly accessible server, gives the public access to the source
|
||||||
|
code of the modified version.
|
||||||
|
|
||||||
|
An older license, called the Affero General Public License and
|
||||||
|
published by Affero, was designed to accomplish similar goals. This is
|
||||||
|
a different license, not a version of the Affero GPL, but Affero has
|
||||||
|
released a new version of the Affero GPL which permits relicensing under
|
||||||
|
this license.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
0. Definitions.
|
||||||
|
|
||||||
|
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||||
|
|
||||||
|
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||||
|
works, such as semiconductor masks.
|
||||||
|
|
||||||
|
"The Program" refers to any copyrightable work licensed under this
|
||||||
|
License. Each licensee is addressed as "you". "Licensees" and
|
||||||
|
"recipients" may be individuals or organizations.
|
||||||
|
|
||||||
|
To "modify" a work means to copy from or adapt all or part of the work
|
||||||
|
in a fashion requiring copyright permission, other than the making of an
|
||||||
|
exact copy. The resulting work is called a "modified version" of the
|
||||||
|
earlier work or a work "based on" the earlier work.
|
||||||
|
|
||||||
|
A "covered work" means either the unmodified Program or a work based
|
||||||
|
on the Program.
|
||||||
|
|
||||||
|
To "propagate" a work means to do anything with it that, without
|
||||||
|
permission, would make you directly or secondarily liable for
|
||||||
|
infringement under applicable copyright law, except executing it on a
|
||||||
|
computer or modifying a private copy. Propagation includes copying,
|
||||||
|
distribution (with or without modification), making available to the
|
||||||
|
public, and in some countries other activities as well.
|
||||||
|
|
||||||
|
To "convey" a work means any kind of propagation that enables other
|
||||||
|
parties to make or receive copies. Mere interaction with a user through
|
||||||
|
a computer network, with no transfer of a copy, is not conveying.
|
||||||
|
|
||||||
|
An interactive user interface displays "Appropriate Legal Notices"
|
||||||
|
to the extent that it includes a convenient and prominently visible
|
||||||
|
feature that (1) displays an appropriate copyright notice, and (2)
|
||||||
|
tells the user that there is no warranty for the work (except to the
|
||||||
|
extent that warranties are provided), that licensees may convey the
|
||||||
|
work under this License, and how to view a copy of this License. If
|
||||||
|
the interface presents a list of user commands or options, such as a
|
||||||
|
menu, a prominent item in the list meets this criterion.
|
||||||
|
|
||||||
|
1. Source Code.
|
||||||
|
|
||||||
|
The "source code" for a work means the preferred form of the work
|
||||||
|
for making modifications to it. "Object code" means any non-source
|
||||||
|
form of a work.
|
||||||
|
|
||||||
|
A "Standard Interface" means an interface that either is an official
|
||||||
|
standard defined by a recognized standards body, or, in the case of
|
||||||
|
interfaces specified for a particular programming language, one that
|
||||||
|
is widely used among developers working in that language.
|
||||||
|
|
||||||
|
The "System Libraries" of an executable work include anything, other
|
||||||
|
than the work as a whole, that (a) is included in the normal form of
|
||||||
|
packaging a Major Component, but which is not part of that Major
|
||||||
|
Component, and (b) serves only to enable use of the work with that
|
||||||
|
Major Component, or to implement a Standard Interface for which an
|
||||||
|
implementation is available to the public in source code form. A
|
||||||
|
"Major Component", in this context, means a major essential component
|
||||||
|
(kernel, window system, and so on) of the specific operating system
|
||||||
|
(if any) on which the executable work runs, or a compiler used to
|
||||||
|
produce the work, or an object code interpreter used to run it.
|
||||||
|
|
||||||
|
The "Corresponding Source" for a work in object code form means all
|
||||||
|
the source code needed to generate, install, and (for an executable
|
||||||
|
work) run the object code and to modify the work, including scripts to
|
||||||
|
control those activities. However, it does not include the work's
|
||||||
|
System Libraries, or general-purpose tools or generally available free
|
||||||
|
programs which are used unmodified in performing those activities but
|
||||||
|
which are not part of the work. For example, Corresponding Source
|
||||||
|
includes interface definition files associated with source files for
|
||||||
|
the work, and the source code for shared libraries and dynamically
|
||||||
|
linked subprograms that the work is specifically designed to require,
|
||||||
|
such as by intimate data communication or control flow between those
|
||||||
|
subprograms and other parts of the work.
|
||||||
|
|
||||||
|
The Corresponding Source need not include anything that users
|
||||||
|
can regenerate automatically from other parts of the Corresponding
|
||||||
|
Source.
|
||||||
|
|
||||||
|
The Corresponding Source for a work in source code form is that
|
||||||
|
same work.
|
||||||
|
|
||||||
|
2. Basic Permissions.
|
||||||
|
|
||||||
|
All rights granted under this License are granted for the term of
|
||||||
|
copyright on the Program, and are irrevocable provided the stated
|
||||||
|
conditions are met. This License explicitly affirms your unlimited
|
||||||
|
permission to run the unmodified Program. The output from running a
|
||||||
|
covered work is covered by this License only if the output, given its
|
||||||
|
content, constitutes a covered work. This License acknowledges your
|
||||||
|
rights of fair use or other equivalent, as provided by copyright law.
|
||||||
|
|
||||||
|
You may make, run and propagate covered works that you do not
|
||||||
|
convey, without conditions so long as your license otherwise remains
|
||||||
|
in force. You may convey covered works to others for the sole purpose
|
||||||
|
of having them make modifications exclusively for you, or provide you
|
||||||
|
with facilities for running those works, provided that you comply with
|
||||||
|
the terms of this License in conveying all material for which you do
|
||||||
|
not control copyright. Those thus making or running the covered works
|
||||||
|
for you must do so exclusively on your behalf, under your direction
|
||||||
|
and control, on terms that prohibit them from making any copies of
|
||||||
|
your copyrighted material outside their relationship with you.
|
||||||
|
|
||||||
|
Conveying under any other circumstances is permitted solely under
|
||||||
|
the conditions stated below. Sublicensing is not allowed; section 10
|
||||||
|
makes it unnecessary.
|
||||||
|
|
||||||
|
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||||
|
|
||||||
|
No covered work shall be deemed part of an effective technological
|
||||||
|
measure under any applicable law fulfilling obligations under article
|
||||||
|
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||||
|
similar laws prohibiting or restricting circumvention of such
|
||||||
|
measures.
|
||||||
|
|
||||||
|
When you convey a covered work, you waive any legal power to forbid
|
||||||
|
circumvention of technological measures to the extent such circumvention
|
||||||
|
is effected by exercising rights under this License with respect to
|
||||||
|
the covered work, and you disclaim any intention to limit operation or
|
||||||
|
modification of the work as a means of enforcing, against the work's
|
||||||
|
users, your or third parties' legal rights to forbid circumvention of
|
||||||
|
technological measures.
|
||||||
|
|
||||||
|
4. Conveying Verbatim Copies.
|
||||||
|
|
||||||
|
You may convey verbatim copies of the Program's source code as you
|
||||||
|
receive it, in any medium, provided that you conspicuously and
|
||||||
|
appropriately publish on each copy an appropriate copyright notice;
|
||||||
|
keep intact all notices stating that this License and any
|
||||||
|
non-permissive terms added in accord with section 7 apply to the code;
|
||||||
|
keep intact all notices of the absence of any warranty; and give all
|
||||||
|
recipients a copy of this License along with the Program.
|
||||||
|
|
||||||
|
You may charge any price or no price for each copy that you convey,
|
||||||
|
and you may offer support or warranty protection for a fee.
|
||||||
|
|
||||||
|
5. Conveying Modified Source Versions.
|
||||||
|
|
||||||
|
You may convey a work based on the Program, or the modifications to
|
||||||
|
produce it from the Program, in the form of source code under the
|
||||||
|
terms of section 4, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) The work must carry prominent notices stating that you modified
|
||||||
|
it, and giving a relevant date.
|
||||||
|
|
||||||
|
b) The work must carry prominent notices stating that it is
|
||||||
|
released under this License and any conditions added under section
|
||||||
|
7. This requirement modifies the requirement in section 4 to
|
||||||
|
"keep intact all notices".
|
||||||
|
|
||||||
|
c) You must license the entire work, as a whole, under this
|
||||||
|
License to anyone who comes into possession of a copy. This
|
||||||
|
License will therefore apply, along with any applicable section 7
|
||||||
|
additional terms, to the whole of the work, and all its parts,
|
||||||
|
regardless of how they are packaged. This License gives no
|
||||||
|
permission to license the work in any other way, but it does not
|
||||||
|
invalidate such permission if you have separately received it.
|
||||||
|
|
||||||
|
d) If the work has interactive user interfaces, each must display
|
||||||
|
Appropriate Legal Notices; however, if the Program has interactive
|
||||||
|
interfaces that do not display Appropriate Legal Notices, your
|
||||||
|
work need not make them do so.
|
||||||
|
|
||||||
|
A compilation of a covered work with other separate and independent
|
||||||
|
works, which are not by their nature extensions of the covered work,
|
||||||
|
and which are not combined with it such as to form a larger program,
|
||||||
|
in or on a volume of a storage or distribution medium, is called an
|
||||||
|
"aggregate" if the compilation and its resulting copyright are not
|
||||||
|
used to limit the access or legal rights of the compilation's users
|
||||||
|
beyond what the individual works permit. Inclusion of a covered work
|
||||||
|
in an aggregate does not cause this License to apply to the other
|
||||||
|
parts of the aggregate.
|
||||||
|
|
||||||
|
6. Conveying Non-Source Forms.
|
||||||
|
|
||||||
|
You may convey a covered work in object code form under the terms
|
||||||
|
of sections 4 and 5, provided that you also convey the
|
||||||
|
machine-readable Corresponding Source under the terms of this License,
|
||||||
|
in one of these ways:
|
||||||
|
|
||||||
|
a) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by the
|
||||||
|
Corresponding Source fixed on a durable physical medium
|
||||||
|
customarily used for software interchange.
|
||||||
|
|
||||||
|
b) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by a
|
||||||
|
written offer, valid for at least three years and valid for as
|
||||||
|
long as you offer spare parts or customer support for that product
|
||||||
|
model, to give anyone who possesses the object code either (1) a
|
||||||
|
copy of the Corresponding Source for all the software in the
|
||||||
|
product that is covered by this License, on a durable physical
|
||||||
|
medium customarily used for software interchange, for a price no
|
||||||
|
more than your reasonable cost of physically performing this
|
||||||
|
conveying of source, or (2) access to copy the
|
||||||
|
Corresponding Source from a network server at no charge.
|
||||||
|
|
||||||
|
c) Convey individual copies of the object code with a copy of the
|
||||||
|
written offer to provide the Corresponding Source. This
|
||||||
|
alternative is allowed only occasionally and noncommercially, and
|
||||||
|
only if you received the object code with such an offer, in accord
|
||||||
|
with subsection 6b.
|
||||||
|
|
||||||
|
d) Convey the object code by offering access from a designated
|
||||||
|
place (gratis or for a charge), and offer equivalent access to the
|
||||||
|
Corresponding Source in the same way through the same place at no
|
||||||
|
further charge. You need not require recipients to copy the
|
||||||
|
Corresponding Source along with the object code. If the place to
|
||||||
|
copy the object code is a network server, the Corresponding Source
|
||||||
|
may be on a different server (operated by you or a third party)
|
||||||
|
that supports equivalent copying facilities, provided you maintain
|
||||||
|
clear directions next to the object code saying where to find the
|
||||||
|
Corresponding Source. Regardless of what server hosts the
|
||||||
|
Corresponding Source, you remain obligated to ensure that it is
|
||||||
|
available for as long as needed to satisfy these requirements.
|
||||||
|
|
||||||
|
e) Convey the object code using peer-to-peer transmission, provided
|
||||||
|
you inform other peers where the object code and Corresponding
|
||||||
|
Source of the work are being offered to the general public at no
|
||||||
|
charge under subsection 6d.
|
||||||
|
|
||||||
|
A separable portion of the object code, whose source code is excluded
|
||||||
|
from the Corresponding Source as a System Library, need not be
|
||||||
|
included in conveying the object code work.
|
||||||
|
|
||||||
|
A "User Product" is either (1) a "consumer product", which means any
|
||||||
|
tangible personal property which is normally used for personal, family,
|
||||||
|
or household purposes, or (2) anything designed or sold for incorporation
|
||||||
|
into a dwelling. In determining whether a product is a consumer product,
|
||||||
|
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||||
|
product received by a particular user, "normally used" refers to a
|
||||||
|
typical or common use of that class of product, regardless of the status
|
||||||
|
of the particular user or of the way in which the particular user
|
||||||
|
actually uses, or expects or is expected to use, the product. A product
|
||||||
|
is a consumer product regardless of whether the product has substantial
|
||||||
|
commercial, industrial or non-consumer uses, unless such uses represent
|
||||||
|
the only significant mode of use of the product.
|
||||||
|
|
||||||
|
"Installation Information" for a User Product means any methods,
|
||||||
|
procedures, authorization keys, or other information required to install
|
||||||
|
and execute modified versions of a covered work in that User Product from
|
||||||
|
a modified version of its Corresponding Source. The information must
|
||||||
|
suffice to ensure that the continued functioning of the modified object
|
||||||
|
code is in no case prevented or interfered with solely because
|
||||||
|
modification has been made.
|
||||||
|
|
||||||
|
If you convey an object code work under this section in, or with, or
|
||||||
|
specifically for use in, a User Product, and the conveying occurs as
|
||||||
|
part of a transaction in which the right of possession and use of the
|
||||||
|
User Product is transferred to the recipient in perpetuity or for a
|
||||||
|
fixed term (regardless of how the transaction is characterized), the
|
||||||
|
Corresponding Source conveyed under this section must be accompanied
|
||||||
|
by the Installation Information. But this requirement does not apply
|
||||||
|
if neither you nor any third party retains the ability to install
|
||||||
|
modified object code on the User Product (for example, the work has
|
||||||
|
been installed in ROM).
|
||||||
|
|
||||||
|
The requirement to provide Installation Information does not include a
|
||||||
|
requirement to continue to provide support service, warranty, or updates
|
||||||
|
for a work that has been modified or installed by the recipient, or for
|
||||||
|
the User Product in which it has been modified or installed. Access to a
|
||||||
|
network may be denied when the modification itself materially and
|
||||||
|
adversely affects the operation of the network or violates the rules and
|
||||||
|
protocols for communication across the network.
|
||||||
|
|
||||||
|
Corresponding Source conveyed, and Installation Information provided,
|
||||||
|
in accord with this section must be in a format that is publicly
|
||||||
|
documented (and with an implementation available to the public in
|
||||||
|
source code form), and must require no special password or key for
|
||||||
|
unpacking, reading or copying.
|
||||||
|
|
||||||
|
7. Additional Terms.
|
||||||
|
|
||||||
|
"Additional permissions" are terms that supplement the terms of this
|
||||||
|
License by making exceptions from one or more of its conditions.
|
||||||
|
Additional permissions that are applicable to the entire Program shall
|
||||||
|
be treated as though they were included in this License, to the extent
|
||||||
|
that they are valid under applicable law. If additional permissions
|
||||||
|
apply only to part of the Program, that part may be used separately
|
||||||
|
under those permissions, but the entire Program remains governed by
|
||||||
|
this License without regard to the additional permissions.
|
||||||
|
|
||||||
|
When you convey a copy of a covered work, you may at your option
|
||||||
|
remove any additional permissions from that copy, or from any part of
|
||||||
|
it. (Additional permissions may be written to require their own
|
||||||
|
removal in certain cases when you modify the work.) You may place
|
||||||
|
additional permissions on material, added by you to a covered work,
|
||||||
|
for which you have or can give appropriate copyright permission.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, for material you
|
||||||
|
add to a covered work, you may (if authorized by the copyright holders of
|
||||||
|
that material) supplement the terms of this License with terms:
|
||||||
|
|
||||||
|
a) Disclaiming warranty or limiting liability differently from the
|
||||||
|
terms of sections 15 and 16 of this License; or
|
||||||
|
|
||||||
|
b) Requiring preservation of specified reasonable legal notices or
|
||||||
|
author attributions in that material or in the Appropriate Legal
|
||||||
|
Notices displayed by works containing it; or
|
||||||
|
|
||||||
|
c) Prohibiting misrepresentation of the origin of that material, or
|
||||||
|
requiring that modified versions of such material be marked in
|
||||||
|
reasonable ways as different from the original version; or
|
||||||
|
|
||||||
|
d) Limiting the use for publicity purposes of names of licensors or
|
||||||
|
authors of the material; or
|
||||||
|
|
||||||
|
e) Declining to grant rights under trademark law for use of some
|
||||||
|
trade names, trademarks, or service marks; or
|
||||||
|
|
||||||
|
f) Requiring indemnification of licensors and authors of that
|
||||||
|
material by anyone who conveys the material (or modified versions of
|
||||||
|
it) with contractual assumptions of liability to the recipient, for
|
||||||
|
any liability that these contractual assumptions directly impose on
|
||||||
|
those licensors and authors.
|
||||||
|
|
||||||
|
All other non-permissive additional terms are considered "further
|
||||||
|
restrictions" within the meaning of section 10. If the Program as you
|
||||||
|
received it, or any part of it, contains a notice stating that it is
|
||||||
|
governed by this License along with a term that is a further
|
||||||
|
restriction, you may remove that term. If a license document contains
|
||||||
|
a further restriction but permits relicensing or conveying under this
|
||||||
|
License, you may add to a covered work material governed by the terms
|
||||||
|
of that license document, provided that the further restriction does
|
||||||
|
not survive such relicensing or conveying.
|
||||||
|
|
||||||
|
If you add terms to a covered work in accord with this section, you
|
||||||
|
must place, in the relevant source files, a statement of the
|
||||||
|
additional terms that apply to those files, or a notice indicating
|
||||||
|
where to find the applicable terms.
|
||||||
|
|
||||||
|
Additional terms, permissive or non-permissive, may be stated in the
|
||||||
|
form of a separately written license, or stated as exceptions;
|
||||||
|
the above requirements apply either way.
|
||||||
|
|
||||||
|
8. Termination.
|
||||||
|
|
||||||
|
You may not propagate or modify a covered work except as expressly
|
||||||
|
provided under this License. Any attempt otherwise to propagate or
|
||||||
|
modify it is void, and will automatically terminate your rights under
|
||||||
|
this License (including any patent licenses granted under the third
|
||||||
|
paragraph of section 11).
|
||||||
|
|
||||||
|
However, if you cease all violation of this License, then your
|
||||||
|
license from a particular copyright holder is reinstated (a)
|
||||||
|
provisionally, unless and until the copyright holder explicitly and
|
||||||
|
finally terminates your license, and (b) permanently, if the copyright
|
||||||
|
holder fails to notify you of the violation by some reasonable means
|
||||||
|
prior to 60 days after the cessation.
|
||||||
|
|
||||||
|
Moreover, your license from a particular copyright holder is
|
||||||
|
reinstated permanently if the copyright holder notifies you of the
|
||||||
|
violation by some reasonable means, this is the first time you have
|
||||||
|
received notice of violation of this License (for any work) from that
|
||||||
|
copyright holder, and you cure the violation prior to 30 days after
|
||||||
|
your receipt of the notice.
|
||||||
|
|
||||||
|
Termination of your rights under this section does not terminate the
|
||||||
|
licenses of parties who have received copies or rights from you under
|
||||||
|
this License. If your rights have been terminated and not permanently
|
||||||
|
reinstated, you do not qualify to receive new licenses for the same
|
||||||
|
material under section 10.
|
||||||
|
|
||||||
|
9. Acceptance Not Required for Having Copies.
|
||||||
|
|
||||||
|
You are not required to accept this License in order to receive or
|
||||||
|
run a copy of the Program. Ancillary propagation of a covered work
|
||||||
|
occurring solely as a consequence of using peer-to-peer transmission
|
||||||
|
to receive a copy likewise does not require acceptance. However,
|
||||||
|
nothing other than this License grants you permission to propagate or
|
||||||
|
modify any covered work. These actions infringe copyright if you do
|
||||||
|
not accept this License. Therefore, by modifying or propagating a
|
||||||
|
covered work, you indicate your acceptance of this License to do so.
|
||||||
|
|
||||||
|
10. Automatic Licensing of Downstream Recipients.
|
||||||
|
|
||||||
|
Each time you convey a covered work, the recipient automatically
|
||||||
|
receives a license from the original licensors, to run, modify and
|
||||||
|
propagate that work, subject to this License. You are not responsible
|
||||||
|
for enforcing compliance by third parties with this License.
|
||||||
|
|
||||||
|
An "entity transaction" is a transaction transferring control of an
|
||||||
|
organization, or substantially all assets of one, or subdividing an
|
||||||
|
organization, or merging organizations. If propagation of a covered
|
||||||
|
work results from an entity transaction, each party to that
|
||||||
|
transaction who receives a copy of the work also receives whatever
|
||||||
|
licenses to the work the party's predecessor in interest had or could
|
||||||
|
give under the previous paragraph, plus a right to possession of the
|
||||||
|
Corresponding Source of the work from the predecessor in interest, if
|
||||||
|
the predecessor has it or can get it with reasonable efforts.
|
||||||
|
|
||||||
|
You may not impose any further restrictions on the exercise of the
|
||||||
|
rights granted or affirmed under this License. For example, you may
|
||||||
|
not impose a license fee, royalty, or other charge for exercise of
|
||||||
|
rights granted under this License, and you may not initiate litigation
|
||||||
|
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||||
|
any patent claim is infringed by making, using, selling, offering for
|
||||||
|
sale, or importing the Program or any portion of it.
|
||||||
|
|
||||||
|
11. Patents.
|
||||||
|
|
||||||
|
A "contributor" is a copyright holder who authorizes use under this
|
||||||
|
License of the Program or a work on which the Program is based. The
|
||||||
|
work thus licensed is called the contributor's "contributor version".
|
||||||
|
|
||||||
|
A contributor's "essential patent claims" are all patent claims
|
||||||
|
owned or controlled by the contributor, whether already acquired or
|
||||||
|
hereafter acquired, that would be infringed by some manner, permitted
|
||||||
|
by this License, of making, using, or selling its contributor version,
|
||||||
|
but do not include claims that would be infringed only as a
|
||||||
|
consequence of further modification of the contributor version. For
|
||||||
|
purposes of this definition, "control" includes the right to grant
|
||||||
|
patent sublicenses in a manner consistent with the requirements of
|
||||||
|
this License.
|
||||||
|
|
||||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||||
|
patent license under the contributor's essential patent claims, to
|
||||||
|
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||||
|
propagate the contents of its contributor version.
|
||||||
|
|
||||||
|
In the following three paragraphs, a "patent license" is any express
|
||||||
|
agreement or commitment, however denominated, not to enforce a patent
|
||||||
|
(such as an express permission to practice a patent or covenant not to
|
||||||
|
sue for patent infringement). To "grant" such a patent license to a
|
||||||
|
party means to make such an agreement or commitment not to enforce a
|
||||||
|
patent against the party.
|
||||||
|
|
||||||
|
If you convey a covered work, knowingly relying on a patent license,
|
||||||
|
and the Corresponding Source of the work is not available for anyone
|
||||||
|
to copy, free of charge and under the terms of this License, through a
|
||||||
|
publicly available network server or other readily accessible means,
|
||||||
|
then you must either (1) cause the Corresponding Source to be so
|
||||||
|
available, or (2) arrange to deprive yourself of the benefit of the
|
||||||
|
patent license for this particular work, or (3) arrange, in a manner
|
||||||
|
consistent with the requirements of this License, to extend the patent
|
||||||
|
license to downstream recipients. "Knowingly relying" means you have
|
||||||
|
actual knowledge that, but for the patent license, your conveying the
|
||||||
|
covered work in a country, or your recipient's use of the covered work
|
||||||
|
in a country, would infringe one or more identifiable patents in that
|
||||||
|
country that you have reason to believe are valid.
|
||||||
|
|
||||||
|
If, pursuant to or in connection with a single transaction or
|
||||||
|
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||||
|
covered work, and grant a patent license to some of the parties
|
||||||
|
receiving the covered work authorizing them to use, propagate, modify
|
||||||
|
or convey a specific copy of the covered work, then the patent license
|
||||||
|
you grant is automatically extended to all recipients of the covered
|
||||||
|
work and works based on it.
|
||||||
|
|
||||||
|
A patent license is "discriminatory" if it does not include within
|
||||||
|
the scope of its coverage, prohibits the exercise of, or is
|
||||||
|
conditioned on the non-exercise of one or more of the rights that are
|
||||||
|
specifically granted under this License. You may not convey a covered
|
||||||
|
work if you are a party to an arrangement with a third party that is
|
||||||
|
in the business of distributing software, under which you make payment
|
||||||
|
to the third party based on the extent of your activity of conveying
|
||||||
|
the work, and under which the third party grants, to any of the
|
||||||
|
parties who would receive the covered work from you, a discriminatory
|
||||||
|
patent license (a) in connection with copies of the covered work
|
||||||
|
conveyed by you (or copies made from those copies), or (b) primarily
|
||||||
|
for and in connection with specific products or compilations that
|
||||||
|
contain the covered work, unless you entered into that arrangement,
|
||||||
|
or that patent license was granted, prior to 28 March 2007.
|
||||||
|
|
||||||
|
Nothing in this License shall be construed as excluding or limiting
|
||||||
|
any implied license or other defenses to infringement that may
|
||||||
|
otherwise be available to you under applicable patent law.
|
||||||
|
|
||||||
|
12. No Surrender of Others' Freedom.
|
||||||
|
|
||||||
|
If conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot convey a
|
||||||
|
covered work so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you may
|
||||||
|
not convey it at all. For example, if you agree to terms that obligate you
|
||||||
|
to collect a royalty for further conveying from those to whom you convey
|
||||||
|
the Program, the only way you could satisfy both those terms and this
|
||||||
|
License would be to refrain entirely from conveying the Program.
|
||||||
|
|
||||||
|
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, if you modify the
|
||||||
|
Program, your modified version must prominently offer all users
|
||||||
|
interacting with it remotely through a computer network (if your version
|
||||||
|
supports such interaction) an opportunity to receive the Corresponding
|
||||||
|
Source of your version by providing access to the Corresponding Source
|
||||||
|
from a network server at no charge, through some standard or customary
|
||||||
|
means of facilitating copying of software. This Corresponding Source
|
||||||
|
shall include the Corresponding Source for any work covered by version 3
|
||||||
|
of the GNU General Public License that is incorporated pursuant to the
|
||||||
|
following paragraph.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, you have
|
||||||
|
permission to link or combine any covered work with a work licensed
|
||||||
|
under version 3 of the GNU General Public License into a single
|
||||||
|
combined work, and to convey the resulting work. The terms of this
|
||||||
|
License will continue to apply to the part which is the covered work,
|
||||||
|
but the work with which it is combined will remain governed by version
|
||||||
|
3 of the GNU General Public License.
|
||||||
|
|
||||||
|
14. Revised Versions of this License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of
|
||||||
|
the GNU Affero General Public License from time to time. Such new versions
|
||||||
|
will be similar in spirit to the present version, but may differ in detail to
|
||||||
|
address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the
|
||||||
|
Program specifies that a certain numbered version of the GNU Affero General
|
||||||
|
Public License "or any later version" applies to it, you have the
|
||||||
|
option of following the terms and conditions either of that numbered
|
||||||
|
version or of any later version published by the Free Software
|
||||||
|
Foundation. If the Program does not specify a version number of the
|
||||||
|
GNU Affero General Public License, you may choose any version ever published
|
||||||
|
by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Program specifies that a proxy can decide which future
|
||||||
|
versions of the GNU Affero General Public License can be used, that proxy's
|
||||||
|
public statement of acceptance of a version permanently authorizes you
|
||||||
|
to choose that version for the Program.
|
||||||
|
|
||||||
|
Later license versions may give you additional or different
|
||||||
|
permissions. However, no additional obligations are imposed on any
|
||||||
|
author or copyright holder as a result of your choosing to follow a
|
||||||
|
later version.
|
||||||
|
|
||||||
|
15. Disclaimer of Warranty.
|
||||||
|
|
||||||
|
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||||
|
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||||
|
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||||
|
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||||
|
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||||
|
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
16. Limitation of Liability.
|
||||||
|
|
||||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||||
|
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||||
|
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||||
|
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||||
|
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||||
|
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||||
|
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||||
|
SUCH DAMAGES.
|
||||||
|
|
||||||
|
17. Interpretation of Sections 15 and 16.
|
||||||
|
|
||||||
|
If the disclaimer of warranty and limitation of liability provided
|
||||||
|
above cannot be given local legal effect according to their terms,
|
||||||
|
reviewing courts shall apply local law that most closely approximates
|
||||||
|
an absolute waiver of all civil liability in connection with the
|
||||||
|
Program, unless a warranty or assumption of liability accompanies a
|
||||||
|
copy of the Program in return for a fee.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest
|
||||||
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
free software which everyone can redistribute and change under these terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest
|
||||||
|
to attach them to the start of each source file to most effectively
|
||||||
|
state the exclusion of warranty; and each file should have at least
|
||||||
|
the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Affero General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If your software can interact with users remotely through a computer
|
||||||
|
network, you should also make sure that it provides a way for users to
|
||||||
|
get its source. For example, if your program is a web application, its
|
||||||
|
interface could display a "Source" link that leads users to an archive
|
||||||
|
of the code. There are many ways you could offer source, and different
|
||||||
|
solutions will be better for different programs; see section 13 for the
|
||||||
|
specific requirements.
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or school,
|
||||||
|
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||||
|
For more information on this, and how to apply and follow the GNU AGPL, see
|
||||||
|
<https://www.gnu.org/licenses/>.
|
315
README.md
@@ -1,8 +1,15 @@
|
|||||||
# AnyLink
|
# AnyLink
|
||||||
|
|
||||||
|
[](https://github.com/bjdgyc/anylink/actions)
|
||||||
[](https://pkg.go.dev/github.com/bjdgyc/anylink)
|
[](https://pkg.go.dev/github.com/bjdgyc/anylink)
|
||||||
|
[](https://goreportcard.com/report/github.com/bjdgyc/anylink)
|
||||||
|
[](https://codecov.io/gh/bjdgyc/anylink)
|
||||||
|

|
||||||
|

|
||||||
|
[](https://hub.docker.com/r/bjdgyc/anylink)
|
||||||
|

|
||||||
|
|
||||||
AnyLink 是一个企业级远程办公ssl vpn软件,可以支持多人同时在线使用。
|
AnyLink 是一个企业级远程办公 sslvpn 的软件,可以支持多人同时在线使用。
|
||||||
|
|
||||||
## Repo
|
## Repo
|
||||||
|
|
||||||
@@ -15,141 +22,315 @@ AnyLink 是一个企业级远程办公ssl vpn软件,可以支持多人同时
|
|||||||
AnyLink 基于 [ietf-openconnect](https://tools.ietf.org/html/draft-mavrogiannopoulos-openconnect-02)
|
AnyLink 基于 [ietf-openconnect](https://tools.ietf.org/html/draft-mavrogiannopoulos-openconnect-02)
|
||||||
协议开发,并且借鉴了 [ocserv](http://ocserv.gitlab.io/www/index.html) 的开发思路,使其可以同时兼容 AnyConnect 客户端。
|
协议开发,并且借鉴了 [ocserv](http://ocserv.gitlab.io/www/index.html) 的开发思路,使其可以同时兼容 AnyConnect 客户端。
|
||||||
|
|
||||||
AnyLink 使用TLS/DTLS进行数据加密,因此需要RSA或ECC证书,可以通过 Let's Encrypt 和 TrustAsia 申请免费的SSL证书。
|
AnyLink 使用 TLS/DTLS 进行数据加密,因此需要 RSA 或 ECC 证书,可以通过 Let's Encrypt 和 TrustAsia 申请免费的 SSL 证书。
|
||||||
|
|
||||||
AnyLink 服务端仅在CentOS7测试通过,如需要安装在其他系统,需要服务端支持tun/tap功能、ip设置命令。
|
AnyLink 服务端仅在 CentOS 7、CentOS 8、Ubuntu 18.04、Ubuntu 20.04 测试通过,如需要安装在其他系统,需要服务端支持 tun/tap 功能、ip 设置命令。
|
||||||
|
|
||||||
## Screenshot
|
## Screenshot
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
## Donate
|
||||||
|
|
||||||
|
> 如果您觉得 anylink 对你有帮助,欢迎给我们打赏,也是帮助 anylink 更好的发展。
|
||||||
|
>
|
||||||
|
> [查看打赏列表](doc/README.md)
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<img src="doc/screenshot/wxpay2.png" width="400" />
|
||||||
|
</p>
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
> 升级 go version = 1.15
|
> 没有编程基础的同学建议直接下载 release 包,从下面的地址下载 anylink-deploy.tar.gz
|
||||||
|
>
|
||||||
|
> https://github.com/bjdgyc/anylink/releases
|
||||||
|
|
||||||
|
### 使用问题
|
||||||
|
|
||||||
|
> 对于测试环境,可以使用 vpn.test.vqilu.cn 绑定host进行测试
|
||||||
|
>
|
||||||
|
> 对于线上环境,必须申请安全的 https 证书,不支持私有证书连接
|
||||||
|
>
|
||||||
|
> 客户端请使用群共享文件的版本,其他版本没有测试过,不保证使用正常
|
||||||
|
>
|
||||||
|
> 首次使用,请在浏览器访问 https://域名:443,浏览器提示安全后,在客户端输入 【域名:443】 即可
|
||||||
|
|
||||||
|
### 自行编译安装
|
||||||
|
|
||||||
|
> 需要提前安装好 golang >= 1.18 和 nodejs >= 14.x 和 yarn >= v1.22.x
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
git clone https://github.com/bjdgyc/anylink.git
|
git clone https://github.com/bjdgyc/anylink.git
|
||||||
|
|
||||||
cd anylink
|
cd anylink
|
||||||
sh deploy.sh
|
sh build.sh
|
||||||
|
|
||||||
#注意使用root权限运行
|
# 注意使用root权限运行
|
||||||
cd anylink-deploy
|
cd anylink-deploy
|
||||||
sudo ./anylink -conf="conf/server.toml"
|
sudo ./anylink
|
||||||
|
|
||||||
|
# 默认管理后台访问地址
|
||||||
|
# 注意该host为anylink的内网ip,不能跟客户端请求的ip一样
|
||||||
|
# https://host:8800
|
||||||
|
# 默认账号 密码
|
||||||
|
# admin 123456
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Feature
|
## Feature
|
||||||
|
|
||||||
- [x] IP分配(实现IP、MAC映射信息的持久化)
|
- [x] IP 分配(实现 IP、MAC 映射信息的持久化)
|
||||||
- [x] TLS-TCP通道
|
- [x] TLS-TCP 通道
|
||||||
- [x] 兼容AnyConnect
|
- [x] DTLS-UDP 通道
|
||||||
- [x] 基于tun设备的nat访问模式
|
- [x] 兼容 AnyConnect
|
||||||
- [x] 基于tap设备的桥接访问模式
|
- [x] 兼容 OpenConnect
|
||||||
- [x] 支持 [proxy protocol v1](http://www.haproxy.org/download/2.2/doc/proxy-protocol.txt) 协议
|
- [x] 基于 tun 设备的 nat 访问模式
|
||||||
|
- [x] 基于 tap 设备的桥接访问模式
|
||||||
|
- [x] 基于 macvtap 设备的桥接访问模式
|
||||||
|
- [x] 支持 [proxy protocol v1&v2](http://www.haproxy.org/download/2.2/doc/proxy-protocol.txt) 协议
|
||||||
- [x] 用户组支持
|
- [x] 用户组支持
|
||||||
- [x] 多用户支持
|
- [x] 多用户支持
|
||||||
- [x] TOTP令牌支持
|
- [x] 用户策略支持
|
||||||
- [x] 流量控制
|
- [x] TOTP 令牌支持
|
||||||
|
- [x] TOTP 令牌开关
|
||||||
|
- [x] 流量速率限制
|
||||||
- [x] 后台管理界面
|
- [x] 后台管理界面
|
||||||
|
- [x] 访问权限管理
|
||||||
- [ ] 访问权限管理
|
- [x] IP 访问审计功能
|
||||||
- [ ] DTLS-UDP通道
|
- [x] 域名动态拆分隧道(域名路由功能)
|
||||||
|
- [x] radius认证支持
|
||||||
|
- [x] LDAP认证支持
|
||||||
|
- [ ] 基于 ipvtap 设备的桥接访问模式
|
||||||
|
|
||||||
## Config
|
## Config
|
||||||
|
|
||||||
默认配置文件内有详细的注释,根据注释填写配置即可。
|
> 示例配置文件内有详细的注释,根据注释填写配置即可。
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
# 生成后台密码
|
# 生成后台密码
|
||||||
./anylink -passwd 123456
|
./anylink tool -p 123456
|
||||||
|
|
||||||
# 生成jwt密钥
|
# 生成jwt密钥
|
||||||
./anylink -secret
|
./anylink tool -s
|
||||||
```
|
```
|
||||||
|
|
||||||
[conf/server.toml](https://github.com/bjdgyc/anylink/blob/master/conf/server.toml)
|
> 数据库配置示例
|
||||||
|
|
||||||
|
| db_type | db_source |
|
||||||
|
| -------- | ------------------------------------------------------ |
|
||||||
|
| sqlite3 | ./conf/anylink.db |
|
||||||
|
| mysql | user:password@tcp(127.0.0.1:3306)/anylink?charset=utf8 |
|
||||||
|
| postgres | user:password@localhost/anylink?sslmode=verify-full |
|
||||||
|
|
||||||
|
> 示例配置文件
|
||||||
|
>
|
||||||
|
> [conf/server-sample.toml](server/conf/server-sample.toml)
|
||||||
|
|
||||||
## Setting
|
## Setting
|
||||||
|
|
||||||
网络模式选择,需要配置 `link_mode` 参数,如 `link_mode="tun"`,`link_mode="tap"` 两种参数。 不同的参数需要对服务器做相应的设置。
|
> 以下参数必须设置其中之一
|
||||||
|
|
||||||
建议优先选择tun模式,因客户端传输的是IP层数据,无须进行数据转换。 tap模式是在用户态做的链路层到IP层的数据互相转换,性能会有所下降。 如果需要在虚拟机内开启tap模式,请确认虚拟机的网卡开启混杂模式。
|
网络模式选择,需要配置 `link_mode` 参数,如 `link_mode="tun"`,`link_mode="macvtap"`,`link_mode="tap"(不推荐)` 等参数。 不同的参数需要对服务器做相应的设置。
|
||||||
|
|
||||||
### tun设置
|
建议优先选择 tun 模式,其次选择 macvtap 模式,因客户端传输的是 IP 层数据,无须进行数据转换。 tap 模式是在用户态做的链路层到 IP 层的数据互相转换,性能会有所下降。 如果需要在虚拟机内开启 tap
|
||||||
|
模式,请确认虚拟机的网卡开启混杂模式。
|
||||||
|
|
||||||
|
### tun 设置
|
||||||
|
|
||||||
1. 开启服务器转发
|
1. 开启服务器转发
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
# flie: /etc/sysctl.conf
|
# flie: /etc/sysctl.conf
|
||||||
net.ipv4.ip_forward = 1
|
net.ipv4.ip_forward = 1
|
||||||
|
|
||||||
#执行如下命令
|
#执行如下命令
|
||||||
sysctl -w net.ipv4.ip_forward=1
|
sysctl -w net.ipv4.ip_forward=1
|
||||||
```
|
|
||||||
|
|
||||||
2. 设置nat转发规则
|
# 查看设置是否生效
|
||||||
|
cat /proc/sys/net/ipv4/ip_forward
|
||||||
|
```
|
||||||
|
|
||||||
|
2.1 设置 nat 转发规则(二选一)
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
# eth0为服务器内网网卡
|
systemctl stop firewalld.service
|
||||||
iptables -t nat -A POSTROUTING -s 192.168.10.0/255.255.255.0 -o eth0 -j MASQUERADE
|
systemctl disable firewalld.service
|
||||||
|
|
||||||
|
# 新版本支持自动设置nat转发,如有其他需求可以参考下面的命令配置
|
||||||
|
|
||||||
|
# 请根据服务器内网网卡替换 eth0
|
||||||
|
# iptables -t nat -A POSTROUTING -s 192.168.90.0/24 -o eth0 -j MASQUERADE
|
||||||
|
# 如果执行第一个命令不生效,可以继续执行下面的命令
|
||||||
|
# iptables -A FORWARD -i eth0 -s 192.168.90.0/24 -j ACCEPT
|
||||||
|
# 查看设置是否生效
|
||||||
|
# iptables -nL -t nat
|
||||||
```
|
```
|
||||||
|
|
||||||
3. 使用AnyConnect客户端连接即可
|
2.2 使用全局路由转发(二选一)
|
||||||
|
|
||||||
### tap设置
|
```shell
|
||||||
|
# 假设anylink所在服务器的内网ip: 10.1.2.10
|
||||||
|
|
||||||
1. 创建桥接网卡
|
# 首先关闭nat转发功能
|
||||||
|
iptables_nat = false
|
||||||
|
|
||||||
|
# 传统网络架构,在华三交换机添加以下静态路由规则
|
||||||
|
ip route-static 192.168.90.0 255.255.255.0 10.1.2.10
|
||||||
|
# 其他品牌的交换机命令,请参考以下地址
|
||||||
|
https://cloud.tencent.com/document/product/216/62007
|
||||||
|
|
||||||
|
# 公有云环境下,需设置vpc下的路由表,添加以下路由策略
|
||||||
|
目的端: 192.168.90.0/24
|
||||||
|
下一跳类型: 云服务器
|
||||||
|
下一跳: 10.1.2.10
|
||||||
|
|
||||||
```
|
|
||||||
注意 server.toml 的ip参数,需要与 bridge-init.sh 的配置参数一致
|
|
||||||
```
|
```
|
||||||
|
|
||||||
2. 修改 bridge-init.sh 内的参数
|
|
||||||
|
3. 使用 AnyConnect 客户端连接即可
|
||||||
|
|
||||||
|
### macvtap 设置
|
||||||
|
|
||||||
|
1. 设置配置文件
|
||||||
|
|
||||||
|
> macvtap 设置相对比较简单,只需要配置相应的参数即可。
|
||||||
|
> 以下参数可以通过执行 `ip a` 查看
|
||||||
|
|
||||||
```
|
```
|
||||||
eth="eth0"
|
# 首先关闭nat转发功能
|
||||||
eth_ip="192.168.1.4"
|
iptables_nat = false
|
||||||
eth_netmask="255.255.255.0"
|
|
||||||
eth_broadcast="192.168.1.255"
|
#内网主网卡名称
|
||||||
eth_gateway="192.168.1.1"
|
ipv4_master = "eth0"
|
||||||
|
#以下网段需要跟ipv4_master网卡设置成一样
|
||||||
|
ipv4_cidr = "10.1.2.0/24"
|
||||||
|
ipv4_gateway = "10.1.2.1"
|
||||||
|
ipv4_start = "10.1.2.100"
|
||||||
|
ipv4_end = "10.1.2.200"
|
||||||
```
|
```
|
||||||
|
|
||||||
3. 执行 bridge-init.sh 文件
|
|
||||||
|
|
||||||
```
|
## Systemd
|
||||||
sh bridge-init.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
## Soft
|
1. 添加 anylink 程序
|
||||||
|
|
||||||
相关软件下载: https://gitee.com/bjdgyc/anylink-soft
|
- anylink 程序目录放入 `/usr/local/anylink-deploy`
|
||||||
|
- 添加执行权限 `chmod +x /usr/local/anylink-deploy/anylink`
|
||||||
|
|
||||||
|
2. systemd/anylink.service 脚本放入:
|
||||||
|
|
||||||
|
- centos: `/usr/lib/systemd/system/`
|
||||||
|
- ubuntu: `/lib/systemd/system/`
|
||||||
|
|
||||||
|
3. 操作命令:
|
||||||
|
|
||||||
|
- 启动: `systemctl start anylink`
|
||||||
|
- 停止: `systemctl stop anylink`
|
||||||
|
- 开机自启: `systemctl enable anylink`
|
||||||
|
|
||||||
|
## Docker
|
||||||
|
|
||||||
|
1. 获取镜像
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 具体tag可以从docker hub获取
|
||||||
|
# https://hub.docker.com/r/bjdgyc/anylink/tags
|
||||||
|
docker pull bjdgyc/anylink:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
2. 查看命令信息
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -it --rm bjdgyc/anylink -h
|
||||||
|
```
|
||||||
|
|
||||||
|
3. 生成密码
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -it --rm bjdgyc/anylink tool -p 123456
|
||||||
|
#Passwd:$2a$10$lCWTCcGmQdE/4Kb1wabbLelu4vY/cUwBwN64xIzvXcihFgRzUvH2a
|
||||||
|
```
|
||||||
|
|
||||||
|
4. 生成 jwt secret
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -it --rm bjdgyc/anylink tool -s
|
||||||
|
#Secret:9qXoIhY01jqhWIeIluGliOS4O_rhcXGGGu422uRZ1JjZxIZmh17WwzW36woEbA
|
||||||
|
```
|
||||||
|
|
||||||
|
5. 启动容器
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -itd --name anylink --privileged \
|
||||||
|
-p 443:443 -p 8800:8800 \
|
||||||
|
--restart=always \
|
||||||
|
bjdgyc/anylink
|
||||||
|
```
|
||||||
|
|
||||||
|
6. 使用自定义参数启动容器
|
||||||
|
```bash
|
||||||
|
# 参数可以参考 -h 命令
|
||||||
|
docker run -itd --name anylink --privileged \
|
||||||
|
-p 443:443 -p 8800:8800 \
|
||||||
|
--restart=always \
|
||||||
|
bjdgyc/anylink \
|
||||||
|
-c=/etc/server.toml --ip_lease=1209600 # IP地址租约时长
|
||||||
|
```
|
||||||
|
|
||||||
|
7. 构建镜像
|
||||||
|
|
||||||
|
```bash
|
||||||
|
#获取仓库源码
|
||||||
|
git clone https://github.com/bjdgyc/anylink.git
|
||||||
|
# 构建镜像
|
||||||
|
docker build -t anylink -f docker/Dockerfile .
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## 常见问题
|
||||||
|
|
||||||
|
请前往 [问题地址](doc/question.md) 查看具体信息
|
||||||
|
|
||||||
## Discussion
|
## Discussion
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
添加QQ群: 567510628
|
添加QQ群: 567510628
|
||||||
|
|
||||||
|
群共享文件有相关软件下载
|
||||||
|
|
||||||
|
<!--
|
||||||
|
添加微信群: 群共享文件有相关软件下载
|
||||||
|
|
||||||
|

|
||||||
|
-->
|
||||||
|
|
||||||
|
|
||||||
## Contribution
|
## Contribution
|
||||||
|
|
||||||
欢迎提交 PR、Issues,感谢为AnyLink做出贡献
|
欢迎提交 PR、Issues,感谢为 AnyLink 做出贡献。
|
||||||
|
|
||||||
|
注意新建 PR,需要提交到 dev 分支,其他分支暂不会合并。
|
||||||
|
|
||||||
## Other Screenshot
|
## Other Screenshot
|
||||||
|
|
||||||

|
<details>
|
||||||

|
<summary>展开查看</summary>
|
||||||

|
|
||||||

|

|
||||||

|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
本项目采用 MIT 开源授权许可证,完整的授权说明已放置在 LICENSE 文件中。
|
本项目采用 AGPL-3.0 开源授权许可证,完整的授权说明已放置在 LICENSE 文件中。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Thank
|
||||||
|
|
||||||
|
<a href="https://www.jetbrains.com">
|
||||||
|
<img src="doc/screenshot/jetbrains.png" width="200" alt="jetbrains.png" />
|
||||||
|
</a>
|
||||||
|
@@ -1,62 +0,0 @@
|
|||||||
package admin
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/bjdgyc/anylink/dbdata"
|
|
||||||
)
|
|
||||||
|
|
||||||
func setOtherGet(data interface{}, w http.ResponseWriter) {
|
|
||||||
err := dbdata.SettingGet(data)
|
|
||||||
if err != nil && !dbdata.CheckErrNotFound(err) {
|
|
||||||
RespError(w, RespInternalErr, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
RespSucess(w, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func setOtherEdit(data interface{}, w http.ResponseWriter, r *http.Request) {
|
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
RespError(w, RespInternalErr, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
err = json.Unmarshal(body, data)
|
|
||||||
if err != nil {
|
|
||||||
RespError(w, RespInternalErr, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// fmt.Println(data)
|
|
||||||
|
|
||||||
err = dbdata.SettingSet(data)
|
|
||||||
if err != nil {
|
|
||||||
RespError(w, RespInternalErr, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
RespSucess(w, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetOtherSmtp(w http.ResponseWriter, r *http.Request) {
|
|
||||||
data := &dbdata.SettingSmtp{}
|
|
||||||
setOtherGet(data, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetOtherSmtpEdit(w http.ResponseWriter, r *http.Request) {
|
|
||||||
data := &dbdata.SettingSmtp{}
|
|
||||||
setOtherEdit(data, w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetOther(w http.ResponseWriter, r *http.Request) {
|
|
||||||
data := &dbdata.SettingOther{}
|
|
||||||
setOtherGet(data, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetOtherEdit(w http.ResponseWriter, r *http.Request) {
|
|
||||||
data := &dbdata.SettingOther{}
|
|
||||||
setOtherEdit(data, w, r)
|
|
||||||
}
|
|
@@ -1,71 +0,0 @@
|
|||||||
// admin:后台管理接口
|
|
||||||
package admin
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"net/http/pprof"
|
|
||||||
|
|
||||||
"github.com/bjdgyc/anylink/base"
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 开启服务
|
|
||||||
func StartAdmin() {
|
|
||||||
|
|
||||||
r := mux.NewRouter()
|
|
||||||
r.Use(authMiddleware)
|
|
||||||
|
|
||||||
r.Handle("/", http.RedirectHandler("/ui/", http.StatusFound)).Name("index")
|
|
||||||
r.PathPrefix("/ui/").Handler(
|
|
||||||
http.StripPrefix("/ui/", http.FileServer(http.Dir(base.Cfg.UiPath))),
|
|
||||||
).Name("static")
|
|
||||||
r.HandleFunc("/base/login", Login).Name("login")
|
|
||||||
|
|
||||||
r.HandleFunc("/set/home", SetHome)
|
|
||||||
r.HandleFunc("/set/system", SetSystem)
|
|
||||||
r.HandleFunc("/set/soft", SetSoft)
|
|
||||||
r.HandleFunc("/set/other", SetOther)
|
|
||||||
r.HandleFunc("/set/other/edit", SetOtherEdit)
|
|
||||||
r.HandleFunc("/set/other/smtp", SetOtherSmtp)
|
|
||||||
r.HandleFunc("/set/other/smtp/edit", SetOtherSmtpEdit)
|
|
||||||
|
|
||||||
r.HandleFunc("/user/list", UserList)
|
|
||||||
r.HandleFunc("/user/detail", UserDetail)
|
|
||||||
r.HandleFunc("/user/set", UserSet)
|
|
||||||
r.HandleFunc("/user/del", UserDel)
|
|
||||||
r.HandleFunc("/user/online", UserOnline)
|
|
||||||
r.HandleFunc("/user/offline", UserOffline)
|
|
||||||
r.HandleFunc("/user/reline", UserReline)
|
|
||||||
r.HandleFunc("/user/otp_qr", UserOtpQr)
|
|
||||||
r.HandleFunc("/user/ip_map/list", UserIpMapList)
|
|
||||||
r.HandleFunc("/user/ip_map/detail", UserIpMapDetail)
|
|
||||||
r.HandleFunc("/user/ip_map/set", UserIpMapSet)
|
|
||||||
r.HandleFunc("/user/ip_map/del", UserIpMapDel)
|
|
||||||
|
|
||||||
r.HandleFunc("/group/list", GroupList)
|
|
||||||
r.HandleFunc("/group/names", GroupNames)
|
|
||||||
r.HandleFunc("/group/detail", GroupDetail)
|
|
||||||
r.HandleFunc("/group/set", GroupSet)
|
|
||||||
r.HandleFunc("/group/del", GroupDel)
|
|
||||||
|
|
||||||
// pprof
|
|
||||||
r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
|
||||||
r.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
|
||||||
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
|
||||||
r.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
|
||||||
r.HandleFunc("/debug/pprof", location("/debug/pprof/"))
|
|
||||||
r.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
|
|
||||||
|
|
||||||
base.Info("Listen admin", base.Cfg.AdminAddr)
|
|
||||||
err := http.ListenAndServe(base.Cfg.AdminAddr, r)
|
|
||||||
if err != nil {
|
|
||||||
base.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func location(url string) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Location", url)
|
|
||||||
w.WriteHeader(http.StatusFound)
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,129 +0,0 @@
|
|||||||
package base
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/pelletier/go-toml"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
LinkModeTUN = "tun"
|
|
||||||
LinkModeTAP = "tap"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
Cfg = &ServerConfig{}
|
|
||||||
)
|
|
||||||
|
|
||||||
// # ReKey time (in seconds)
|
|
||||||
// rekey-time = 172800
|
|
||||||
// # ReKey method
|
|
||||||
// # Valid options: ssl, new-tunnel
|
|
||||||
// # ssl: Will perform an efficient rehandshake on the channel allowing
|
|
||||||
// # a seamless connection during rekey.
|
|
||||||
// # new-tunnel: Will instruct the client to discard and re-establish the channel.
|
|
||||||
// # Use this option only if the connecting clients have issues with the ssl
|
|
||||||
// # option.
|
|
||||||
// rekey-method = ssl
|
|
||||||
|
|
||||||
type ServerConfig struct {
|
|
||||||
LinkAddr string `toml:"link_addr" info:"vpn服务对外地址"`
|
|
||||||
ServerAddr string `toml:"server_addr" info:"前台服务监听地址"`
|
|
||||||
AdminAddr string `toml:"admin_addr" info:"后台服务监听地址"`
|
|
||||||
ProxyProtocol bool `toml:"proxy_protocol" info:"TCP代理协议"`
|
|
||||||
DbFile string `toml:"db_file" info:"数据库地址"`
|
|
||||||
CertFile string `toml:"cert_file" info:"证书文件"`
|
|
||||||
CertKey string `toml:"cert_key" info:"证书密钥"`
|
|
||||||
UiPath string `toml:"ui_path" info:"ui文件路径"`
|
|
||||||
DownFilesPath string `toml:"down_files_path" info:"外部下载文件路径"`
|
|
||||||
LogLevel string `toml:"log_level" info:"日志等级"`
|
|
||||||
Issuer string `toml:"issuer" info:"系统名称"`
|
|
||||||
AdminUser string `toml:"admin_user" info:"管理用户名"`
|
|
||||||
AdminPass string `toml:"admin_pass" info:"管理用户密码"`
|
|
||||||
JwtSecret string `toml:"jwt_secret" info:"JWT密钥"`
|
|
||||||
|
|
||||||
LinkMode string `toml:"link_mode" info:"虚拟网络类型"` // tun tap
|
|
||||||
Ipv4Network string `toml:"ipv4_network" info:"ipv4_network"` // 192.168.1.0
|
|
||||||
Ipv4Netmask string `toml:"ipv4_netmask" info:"ipv4_netmask"` // 255.255.255.0
|
|
||||||
Ipv4Gateway string `toml:"ipv4_gateway" info:"ipv4_gateway"`
|
|
||||||
Ipv4Pool []string `toml:"ipv4_pool" info:"IPV4起止地址池"` // Pool[0]=192.168.1.100 Pool[1]=192.168.1.200
|
|
||||||
IpLease int `toml:"ip_lease" info:"IP租期(秒)"`
|
|
||||||
|
|
||||||
MaxClient int `toml:"max_client" info:"最大用户连接"`
|
|
||||||
MaxUserClient int `toml:"max_user_client" info:"最大单用户连接"`
|
|
||||||
DefaultGroup string `toml:"default_group" info:"默认用户组"`
|
|
||||||
CstpKeepalive int `toml:"cstp_keepalive" info:"keepalive时间(秒)"` // in seconds
|
|
||||||
CstpDpd int `toml:"cstp_dpd" info:"死链接检测时间(秒)"` // Dead peer detection in seconds
|
|
||||||
MobileKeepalive int `toml:"mobile_keepalive" info:"移动端keepalive接检测时间(秒)"`
|
|
||||||
MobileDpd int `toml:"mobile_dpd" info:"移动端死链接检测时间(秒)"`
|
|
||||||
|
|
||||||
SessionTimeout int `toml:"session_timeout" info:"session过期时间(秒)"` // in seconds
|
|
||||||
AuthTimeout int `toml:"auth_timeout" info:"auth_timeout"` // in seconds
|
|
||||||
}
|
|
||||||
|
|
||||||
func initServerCfg() {
|
|
||||||
b, err := ioutil.ReadFile(serverFile)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
err = toml.Unmarshal(b, Cfg)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
sf, _ := filepath.Abs(serverFile)
|
|
||||||
base := filepath.Dir(sf)
|
|
||||||
|
|
||||||
// 转换成绝对路径
|
|
||||||
Cfg.DbFile = getAbsPath(base, Cfg.DbFile)
|
|
||||||
Cfg.CertFile = getAbsPath(base, Cfg.CertFile)
|
|
||||||
Cfg.CertKey = getAbsPath(base, Cfg.CertKey)
|
|
||||||
Cfg.UiPath = getAbsPath(base, Cfg.UiPath)
|
|
||||||
Cfg.DownFilesPath = getAbsPath(base, Cfg.DownFilesPath)
|
|
||||||
|
|
||||||
if len(Cfg.JwtSecret) < 20 {
|
|
||||||
fmt.Println("请设置 jwt_secret 长度20位以上")
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("ServerCfg: %+v \n", Cfg)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getAbsPath(base, cfile string) string {
|
|
||||||
abs := filepath.IsAbs(cfile)
|
|
||||||
if abs {
|
|
||||||
return cfile
|
|
||||||
}
|
|
||||||
return filepath.Join(base, cfile)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ServerCfg2Slice() interface{} {
|
|
||||||
ref := reflect.ValueOf(Cfg)
|
|
||||||
s := ref.Elem()
|
|
||||||
|
|
||||||
type cfg struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Info string `json:"info"`
|
|
||||||
Data interface{} `json:"data"`
|
|
||||||
}
|
|
||||||
var datas []cfg
|
|
||||||
|
|
||||||
typ := s.Type()
|
|
||||||
numFields := s.NumField()
|
|
||||||
for i := 0; i < numFields; i++ {
|
|
||||||
field := typ.Field(i)
|
|
||||||
value := s.Field(i)
|
|
||||||
tag := field.Tag.Get("toml")
|
|
||||||
tags := strings.Split(tag, ",")
|
|
||||||
info := field.Tag.Get("info")
|
|
||||||
|
|
||||||
datas = append(datas, cfg{Name: tags[0], Info: info, Data: value.Interface()})
|
|
||||||
}
|
|
||||||
|
|
||||||
return datas
|
|
||||||
}
|
|
54
base/flag.go
@@ -1,54 +0,0 @@
|
|||||||
package base
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"math/rand"
|
|
||||||
"os"
|
|
||||||
"runtime"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/bjdgyc/anylink/pkg/utils"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// 提交id
|
|
||||||
CommitId string
|
|
||||||
// 配置文件
|
|
||||||
serverFile string
|
|
||||||
// pass明文
|
|
||||||
passwd string
|
|
||||||
// 生成密钥
|
|
||||||
secret bool
|
|
||||||
// 显示版本信息
|
|
||||||
rev bool
|
|
||||||
)
|
|
||||||
|
|
||||||
func initFlag() {
|
|
||||||
flag.StringVar(&serverFile, "conf", "./conf/server.toml", "server config file path")
|
|
||||||
flag.StringVar(&passwd, "passwd", "", "convert the password plaintext")
|
|
||||||
flag.BoolVar(&secret, "secret", false, "generate a random jwt secret")
|
|
||||||
flag.BoolVar(&rev, "rev", false, "display version info")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if passwd != "" {
|
|
||||||
pass, _ := utils.PasswordHash(passwd)
|
|
||||||
fmt.Printf("Passwd:%s\n", pass)
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
if secret {
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
|
||||||
s, _ := utils.RandSecret(40, 60)
|
|
||||||
s = strings.Trim(s, "=")
|
|
||||||
fmt.Printf("Secret:%s\n", s)
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
if rev {
|
|
||||||
fmt.Printf("%s v%s build on %s [%s, %s] commit_id(%s) \n",
|
|
||||||
APP_NAME, APP_VER, runtime.Version(), runtime.GOOS, runtime.GOARCH, CommitId)
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
}
|
|
90
base/log.go
@@ -1,90 +0,0 @@
|
|||||||
package base
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
_Debug = iota
|
|
||||||
_Info
|
|
||||||
_Warn
|
|
||||||
_Error
|
|
||||||
_Fatal
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
baseLog *log.Logger
|
|
||||||
baseLevel int
|
|
||||||
levels map[int]string
|
|
||||||
)
|
|
||||||
|
|
||||||
func initLog() {
|
|
||||||
baseLog = log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile)
|
|
||||||
baseLevel = logLevel2Int(Cfg.LogLevel)
|
|
||||||
}
|
|
||||||
|
|
||||||
func logLevel2Int(l string) int {
|
|
||||||
levels = map[int]string{
|
|
||||||
_Debug: "Debug",
|
|
||||||
_Info: "Info",
|
|
||||||
_Warn: "Warn",
|
|
||||||
_Error: "Error",
|
|
||||||
_Fatal: "Fatal",
|
|
||||||
}
|
|
||||||
lvl := _Info
|
|
||||||
for k, v := range levels {
|
|
||||||
if strings.ToLower(l) == strings.ToLower(v) {
|
|
||||||
lvl = k
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return lvl
|
|
||||||
}
|
|
||||||
|
|
||||||
func output(l int, s ...interface{}) {
|
|
||||||
lvl := fmt.Sprintf("[%s] ", levels[l])
|
|
||||||
baseLog.Output(3, lvl+fmt.Sprintln(s...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Debug(v ...interface{}) {
|
|
||||||
l := _Debug
|
|
||||||
if baseLevel > l {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
output(l, v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Info(v ...interface{}) {
|
|
||||||
l := _Info
|
|
||||||
if baseLevel > l {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
output(l, v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Warn(v ...interface{}) {
|
|
||||||
l := _Warn
|
|
||||||
if baseLevel > l {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
output(l, v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Error(v ...interface{}) {
|
|
||||||
l := _Error
|
|
||||||
if baseLevel > l {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
output(l, v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Fatal(v ...interface{}) {
|
|
||||||
l := _Fatal
|
|
||||||
if baseLevel > l {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
output(l, v...)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
@@ -1,7 +0,0 @@
|
|||||||
package base
|
|
||||||
|
|
||||||
func Start() {
|
|
||||||
initFlag()
|
|
||||||
initServerCfg()
|
|
||||||
initLog()
|
|
||||||
}
|
|
@@ -1,42 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
#################################
|
|
||||||
# Set up Ethernet bridge on Linux
|
|
||||||
# Requires: bridge-utils
|
|
||||||
#################################
|
|
||||||
|
|
||||||
# Define Bridge Interface
|
|
||||||
br="anylink0"
|
|
||||||
|
|
||||||
# Define list of TAP interfaces to be bridged,
|
|
||||||
# for example tap="tap0 tap1 tap2".
|
|
||||||
tap="tap0"
|
|
||||||
|
|
||||||
# Define physical ethernet interface to be bridged
|
|
||||||
# with TAP interface(s) above.
|
|
||||||
|
|
||||||
eth="eth0"
|
|
||||||
eth_ip="192.168.10.4"
|
|
||||||
eth_netmask="255.255.255.0"
|
|
||||||
eth_broadcast="192.168.10.255"
|
|
||||||
eth_gateway="192.168.10.1"
|
|
||||||
|
|
||||||
|
|
||||||
brctl addbr $br
|
|
||||||
brctl addif $br $eth
|
|
||||||
|
|
||||||
ifconfig $eth 0.0.0.0 up
|
|
||||||
|
|
||||||
mac=`cat /sys/class/net/$eth/address`
|
|
||||||
ifconfig $br hw ether $mac
|
|
||||||
ifconfig $br $eth_ip netmask $eth_netmask broadcast $eth_broadcast up
|
|
||||||
|
|
||||||
route add default gateway $eth_gateway
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
56
build.sh
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -x
|
||||||
|
function RETVAL() {
|
||||||
|
rt=$1
|
||||||
|
if [ $rt != 0 ]; then
|
||||||
|
echo $rt
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
#当前目录
|
||||||
|
cpath=$(pwd)
|
||||||
|
|
||||||
|
echo "编译前端项目"
|
||||||
|
cd $cpath/web
|
||||||
|
#国内可替换源加快速度
|
||||||
|
#npx browserslist@latest --update-db
|
||||||
|
#npm install --registry=https://registry.npm.taobao.org
|
||||||
|
#npm install
|
||||||
|
#npm run build
|
||||||
|
|
||||||
|
yarn install
|
||||||
|
yarn run build
|
||||||
|
|
||||||
|
RETVAL $?
|
||||||
|
|
||||||
|
echo "编译二进制文件"
|
||||||
|
cd $cpath/server
|
||||||
|
rm -rf ui
|
||||||
|
cp -rf $cpath/web/ui .
|
||||||
|
#国内可替换源加快速度
|
||||||
|
export GOPROXY=https://goproxy.io
|
||||||
|
go mod tidy
|
||||||
|
go build -v -o anylink -ldflags "-X main.CommitId=$(git rev-parse HEAD)"
|
||||||
|
RETVAL $?
|
||||||
|
|
||||||
|
cd $cpath
|
||||||
|
|
||||||
|
echo "整理部署文件"
|
||||||
|
deploy="anylink-deploy"
|
||||||
|
rm -rf $deploy ${deploy}.tar.gz
|
||||||
|
mkdir $deploy
|
||||||
|
|
||||||
|
cp -r server/anylink $deploy
|
||||||
|
#cp -r server/bridge-init.sh $deploy
|
||||||
|
cp -r server/conf $deploy
|
||||||
|
|
||||||
|
cp -r systemd $deploy
|
||||||
|
cp -r LICENSE $deploy
|
||||||
|
|
||||||
|
tar zcvf ${deploy}.tar.gz $deploy
|
||||||
|
|
||||||
|
#注意使用root权限运行
|
||||||
|
#cd anylink-deploy
|
||||||
|
#sudo ./anylink --conf="conf/server.toml"
|
15
build_docker.sh
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
ver=`cat server/base/app_ver.go | grep APP_VER | awk '{print $3}' | sed 's/"//g'`
|
||||||
|
echo $ver
|
||||||
|
|
||||||
|
#docker login -u bjdgyc
|
||||||
|
|
||||||
|
#docker build -t bjdgyc/anylink .
|
||||||
|
docker build -t bjdgyc/anylink -f docker/Dockerfile .
|
||||||
|
|
||||||
|
docker tag bjdgyc/anylink:latest bjdgyc/anylink:$ver
|
||||||
|
|
||||||
|
docker push bjdgyc/anylink:$ver
|
||||||
|
docker push bjdgyc/anylink:latest
|
||||||
|
|
@@ -1,65 +0,0 @@
|
|||||||
#服务配置信息
|
|
||||||
|
|
||||||
#其他配置文件,可以使用绝对路径
|
|
||||||
#或者相对于server.toml的路径
|
|
||||||
|
|
||||||
#数据文件
|
|
||||||
db_file = "./data.db"
|
|
||||||
#证书文件
|
|
||||||
cert_file = "./vpn_cert.pem"
|
|
||||||
cert_key = "./vpn_cert.key"
|
|
||||||
ui_path = "../ui"
|
|
||||||
down_files_path = "../down_files"
|
|
||||||
|
|
||||||
log_level = "info"
|
|
||||||
|
|
||||||
#系统名称
|
|
||||||
issuer = "XX公司VPN"
|
|
||||||
#后台管理用户
|
|
||||||
admin_user = "admin"
|
|
||||||
#pass 123456
|
|
||||||
admin_pass = "$2a$10$UQ7C.EoPifDeJh6d8.31TeSPQU7hM/NOM2nixmBucJpAuXDQNqNke"
|
|
||||||
jwt_secret = ""
|
|
||||||
|
|
||||||
|
|
||||||
#vpn服务对外地址,影响开通邮件二维码
|
|
||||||
link_addr = "vpn.xx.com"
|
|
||||||
|
|
||||||
#前台服务监听地址
|
|
||||||
server_addr = ":443"
|
|
||||||
#后台服务监听地址
|
|
||||||
admin_addr = ":8800"
|
|
||||||
#开启tcp proxy protocol协议
|
|
||||||
proxy_protocol = false
|
|
||||||
|
|
||||||
link_mode = "tun"
|
|
||||||
|
|
||||||
#客户端分配的ip地址池
|
|
||||||
ipv4_network = "192.168.10.0"
|
|
||||||
ipv4_netmask = "255.255.255.0"
|
|
||||||
ipv4_gateway = "192.168.10.1"
|
|
||||||
ipv4_pool = ["192.168.10.100", "192.168.10.200"]
|
|
||||||
|
|
||||||
#最大客户端数量
|
|
||||||
max_client = 100
|
|
||||||
#单个用户同时在线数量
|
|
||||||
max_user_client = 3
|
|
||||||
#IP租期(秒)
|
|
||||||
ip_lease = 1209600
|
|
||||||
|
|
||||||
#默认选择的组
|
|
||||||
default_group = "one"
|
|
||||||
|
|
||||||
#客户端失效检测时间(秒) dpd > keepalive
|
|
||||||
cstp_keepalive = 20
|
|
||||||
cstp_dpd = 30
|
|
||||||
mobile_keepalive = 50
|
|
||||||
mobile_dpd = 60
|
|
||||||
#session过期时间,用于断线重连,0永不过期
|
|
||||||
session_timeout = 3600
|
|
||||||
auth_timeout = 0
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
90
dbdata/db.go
@@ -1,90 +0,0 @@
|
|||||||
package dbdata
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/asdine/storm/v3"
|
|
||||||
"github.com/asdine/storm/v3/codec/json"
|
|
||||||
"github.com/bjdgyc/anylink/base"
|
|
||||||
bolt "go.etcd.io/bbolt"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
sdb *storm.DB
|
|
||||||
)
|
|
||||||
|
|
||||||
func initDb() {
|
|
||||||
var err error
|
|
||||||
sdb, err = storm.Open(base.Cfg.DbFile, storm.Codec(json.Codec),
|
|
||||||
storm.BoltOptions(0600, &bolt.Options{Timeout: 10 * time.Second}))
|
|
||||||
if err != nil {
|
|
||||||
base.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化数据库
|
|
||||||
err = sdb.Init(&User{})
|
|
||||||
if err != nil {
|
|
||||||
base.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// fmt.Println("s1")
|
|
||||||
}
|
|
||||||
|
|
||||||
func initData() {
|
|
||||||
var (
|
|
||||||
err error
|
|
||||||
install bool
|
|
||||||
)
|
|
||||||
|
|
||||||
// 判断是否初次使用
|
|
||||||
err = Get(SettingBucket, Installed, &install)
|
|
||||||
if err == nil && install {
|
|
||||||
// 已经安装过
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer Set(SettingBucket, Installed, true)
|
|
||||||
|
|
||||||
smtp := &SettingSmtp{
|
|
||||||
Host: "127.0.0.1",
|
|
||||||
Port: 25,
|
|
||||||
From: "vpn@xx.com",
|
|
||||||
}
|
|
||||||
SettingSet(smtp)
|
|
||||||
|
|
||||||
other := &SettingOther{
|
|
||||||
Banner: "您已接入公司网络,请按照公司规定使用。\n请勿进行非工作下载及视频行为!",
|
|
||||||
AccountMail: accountMail,
|
|
||||||
}
|
|
||||||
SettingSet(other)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func CheckErrNotFound(err error) bool {
|
|
||||||
if err == storm.ErrNotFound {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
const accountMail = `<p>您好:</p>
|
|
||||||
<p> 您的{{.Issuer}}账号已经审核开通。</p>
|
|
||||||
<p>
|
|
||||||
登陆地址: <b>{{.LinkAddr}}</b> <br/>
|
|
||||||
用户组: <b>{{.Group}}</b> <br/>
|
|
||||||
用户名: <b>{{.Username}}</b> <br/>
|
|
||||||
用户PIN码: <b>{{.PinCode}}</b> <br/>
|
|
||||||
用户动态码(3天后失效):<br/>
|
|
||||||
<img src="{{.OtpImg}}"/>
|
|
||||||
</p>
|
|
||||||
<div>
|
|
||||||
使用说明:
|
|
||||||
<ul>
|
|
||||||
<li>请使用OTP软件扫描动态码二维码</li>
|
|
||||||
<li>然后使用anyconnect客户端进行登陆</li>
|
|
||||||
<li>登陆密码为 【PIN码+动态码】</li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
<p>
|
|
||||||
软件下载地址: https://gitee.com/bjdgyc/anylink-soft/blob/master/README.md
|
|
||||||
</p>`
|
|
@@ -1,66 +0,0 @@
|
|||||||
package dbdata
|
|
||||||
|
|
||||||
import "github.com/asdine/storm/v3/index"
|
|
||||||
|
|
||||||
const PageSize = 10
|
|
||||||
|
|
||||||
func Save(data interface{}) error {
|
|
||||||
return sdb.Save(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Update(data interface{}) error {
|
|
||||||
return sdb.Update(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func UpdateField(data interface{}, fieldName string, value interface{}) error {
|
|
||||||
return sdb.UpdateField(data, fieldName, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Del(data interface{}) error {
|
|
||||||
return sdb.DeleteStruct(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Set(bucket, key string, data interface{}) error {
|
|
||||||
return sdb.Set(bucket, key, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Get(bucket, key string, data interface{}) error {
|
|
||||||
return sdb.Get(bucket, key, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func CountAll(data interface{}) int {
|
|
||||||
n, _ := sdb.Count(data)
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
func One(fieldName string, value interface{}, to interface{}) error {
|
|
||||||
return sdb.One(fieldName, value, to)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Find(fieldName string, value interface{}, to interface{}, options ...func(q *index.Options)) error {
|
|
||||||
return sdb.Find(fieldName, value, to, options...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func All(to interface{}, limit, page int) error {
|
|
||||||
opt := getOpt(limit, page)
|
|
||||||
return sdb.All(to, opt)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Prefix(fieldName string, prefix string, to interface{}, limit, page int) error {
|
|
||||||
opt := getOpt(limit, page)
|
|
||||||
return sdb.Prefix(fieldName, prefix, to, opt)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getOpt(limit, page int) func(*index.Options) {
|
|
||||||
skip := (page - 1) * limit
|
|
||||||
opt := func(opt *index.Options) {
|
|
||||||
opt.Reverse = true
|
|
||||||
if limit > 0 {
|
|
||||||
opt.Limit = limit
|
|
||||||
}
|
|
||||||
if skip > 0 {
|
|
||||||
opt.Skip = skip
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return opt
|
|
||||||
}
|
|
134
dbdata/group.go
@@ -1,134 +0,0 @@
|
|||||||
package dbdata
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/bjdgyc/anylink/base"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
Allow = "allow"
|
|
||||||
Deny = "deny"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GroupLinkAcl struct {
|
|
||||||
// 自上而下匹配 默认 allow * *
|
|
||||||
Action string `json:"action"` // allow、deny
|
|
||||||
Val string `json:"val"`
|
|
||||||
Port uint16 `json:"port"`
|
|
||||||
IpNet *net.IPNet `json:"ip_net"`
|
|
||||||
Note string `json:"note"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ValData struct {
|
|
||||||
Val string `json:"val"`
|
|
||||||
IpMask string `json:"ip_mask"`
|
|
||||||
Note string `json:"note"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Group struct {
|
|
||||||
Id int `json:"id" storm:"id,increment"`
|
|
||||||
Name string `json:"name" storm:"unique"`
|
|
||||||
Note string `json:"note"`
|
|
||||||
AllowLan bool `json:"allow_lan"`
|
|
||||||
ClientDns []ValData `json:"client_dns"`
|
|
||||||
RouteInclude []ValData `json:"route_include"`
|
|
||||||
RouteExclude []ValData `json:"route_exclude"`
|
|
||||||
LinkAcl []GroupLinkAcl `json:"link_acl"`
|
|
||||||
Bandwidth int `json:"bandwidth"` // 带宽限制
|
|
||||||
Status int8 `json:"status"` // 1正常
|
|
||||||
CreatedAt time.Time `json:"created_at"`
|
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetGroupNames() []string {
|
|
||||||
var datas []Group
|
|
||||||
err := All(&datas, 0, 0)
|
|
||||||
if err != nil {
|
|
||||||
base.Error(err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var names []string
|
|
||||||
for _, v := range datas {
|
|
||||||
names = append(names, v.Name)
|
|
||||||
}
|
|
||||||
return names
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetGroup(g *Group) error {
|
|
||||||
var err error
|
|
||||||
if g.Name == "" {
|
|
||||||
return errors.New("用户组名错误")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 判断数据
|
|
||||||
clientDns := []ValData{}
|
|
||||||
for _, v := range g.ClientDns {
|
|
||||||
if v.Val != "" {
|
|
||||||
clientDns = append(clientDns, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(clientDns) == 0 {
|
|
||||||
return errors.New("DNS 错误")
|
|
||||||
}
|
|
||||||
g.ClientDns = clientDns
|
|
||||||
|
|
||||||
routeInclude := []ValData{}
|
|
||||||
for _, v := range g.RouteInclude {
|
|
||||||
if v.Val != "" {
|
|
||||||
ipMask, _, err := parseIpNet(v.Val)
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("RouteInclude 错误" + err.Error())
|
|
||||||
}
|
|
||||||
vn := ValData{Val: v.Val, IpMask: ipMask}
|
|
||||||
routeInclude = append(routeInclude, vn)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
g.RouteInclude = routeInclude
|
|
||||||
routeExclude := []ValData{}
|
|
||||||
for _, v := range g.RouteExclude {
|
|
||||||
if v.Val != "" {
|
|
||||||
ipMask, _, err := parseIpNet(v.Val)
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("RouteExclude 错误" + err.Error())
|
|
||||||
}
|
|
||||||
vn := ValData{Val: v.Val, IpMask: ipMask}
|
|
||||||
routeExclude = append(routeExclude, vn)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
g.RouteExclude = routeExclude
|
|
||||||
// 转换数据
|
|
||||||
linkAcl := []GroupLinkAcl{}
|
|
||||||
for _, v := range g.LinkAcl {
|
|
||||||
if v.Val != "" {
|
|
||||||
_, ipNet, err := parseIpNet(v.Val)
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("GroupLinkAcl 错误" + err.Error())
|
|
||||||
}
|
|
||||||
vn := v
|
|
||||||
vn.IpNet = ipNet
|
|
||||||
linkAcl = append(linkAcl, vn)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
g.LinkAcl = linkAcl
|
|
||||||
|
|
||||||
g.UpdatedAt = time.Now()
|
|
||||||
err = Save(g)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseIpNet(s string) (string, *net.IPNet, error) {
|
|
||||||
ip, ipNet, err := net.ParseCIDR(s)
|
|
||||||
if err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
mask := net.IP(ipNet.Mask)
|
|
||||||
ipMask := fmt.Sprintf("%s/%s", ip, mask)
|
|
||||||
|
|
||||||
return ipMask, ipNet, nil
|
|
||||||
}
|
|
@@ -1,18 +0,0 @@
|
|||||||
package dbdata
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type IpMap struct {
|
|
||||||
Id int `json:"id" storm:"id,increment"`
|
|
||||||
IpAddr net.IP `json:"ip_addr" storm:"unique"`
|
|
||||||
MacAddr string `json:"mac_addr" storm:"unique"`
|
|
||||||
Username string `json:"username"`
|
|
||||||
Keep bool `json:"keep"` // 保留 ip-mac 绑定
|
|
||||||
KeepTime time.Time `json:"keep_time"`
|
|
||||||
Note string `json:"note"` // 备注
|
|
||||||
LastLogin time.Time `json:"last_login"`
|
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
|
||||||
}
|
|
@@ -1,47 +0,0 @@
|
|||||||
package dbdata
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
SettingBucket = "SettingBucket"
|
|
||||||
Installed = "Installed"
|
|
||||||
)
|
|
||||||
|
|
||||||
func StructName(data interface{}) string {
|
|
||||||
ref := reflect.ValueOf(data)
|
|
||||||
s := &ref
|
|
||||||
if s.Kind() == reflect.Ptr {
|
|
||||||
e := s.Elem()
|
|
||||||
s = &e
|
|
||||||
}
|
|
||||||
name := s.Type().Name()
|
|
||||||
return name
|
|
||||||
}
|
|
||||||
|
|
||||||
func SettingSet(data interface{}) error {
|
|
||||||
key := StructName(data)
|
|
||||||
err := Set(SettingBucket, key, data)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func SettingGet(data interface{}) error {
|
|
||||||
key := StructName(data)
|
|
||||||
err := Get(SettingBucket, key, data)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
type SettingSmtp struct {
|
|
||||||
Host string `json:"host"`
|
|
||||||
Port int `json:"port"`
|
|
||||||
Username string `json:"username"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
From string `json:"from"`
|
|
||||||
UseSSl bool `json:"use_ssl"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type SettingOther struct {
|
|
||||||
Banner string `json:"banner"`
|
|
||||||
AccountMail string `json:"account_mail"`
|
|
||||||
}
|
|
143
dbdata/user.go
@@ -1,143 +0,0 @@
|
|||||||
package dbdata
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/bjdgyc/anylink/pkg/utils"
|
|
||||||
"github.com/xlzd/gotp"
|
|
||||||
)
|
|
||||||
|
|
||||||
type User struct {
|
|
||||||
Id int `json:"id" storm:"id,increment"`
|
|
||||||
Username string `json:"username" storm:"unique"`
|
|
||||||
Nickname string `json:"nickname"`
|
|
||||||
Email string `json:"email"`
|
|
||||||
// Password string `json:"password"`
|
|
||||||
PinCode string `json:"pin_code"`
|
|
||||||
OtpSecret string `json:"otp_secret"`
|
|
||||||
Groups []string `json:"groups"`
|
|
||||||
Status int8 `json:"status"` // 1正常
|
|
||||||
SendEmail bool `json:"send_email"`
|
|
||||||
CreatedAt time.Time `json:"created_at"`
|
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetUser(v *User) error {
|
|
||||||
var err error
|
|
||||||
if v.Username == "" || len(v.Groups) == 0 {
|
|
||||||
return errors.New("用户名或组错误")
|
|
||||||
}
|
|
||||||
|
|
||||||
planPass := v.PinCode
|
|
||||||
// 自动生成密码
|
|
||||||
if len(planPass) < 6 {
|
|
||||||
planPass = utils.RandomNum(8)
|
|
||||||
}
|
|
||||||
v.PinCode = planPass
|
|
||||||
|
|
||||||
if v.OtpSecret == "" {
|
|
||||||
v.OtpSecret = gotp.RandomSecret(24)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 判断组是否有效
|
|
||||||
ng := []string{}
|
|
||||||
groups := GetGroupNames()
|
|
||||||
for _, g := range v.Groups {
|
|
||||||
if utils.InArrStr(groups, g) {
|
|
||||||
ng = append(ng, g)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(ng) == 0 {
|
|
||||||
return errors.New("用户名或组错误")
|
|
||||||
}
|
|
||||||
v.Groups = ng
|
|
||||||
|
|
||||||
v.UpdatedAt = time.Now()
|
|
||||||
err = Save(v)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 验证用户登陆信息
|
|
||||||
func CheckUser(name, pwd, group string) error {
|
|
||||||
// return nil
|
|
||||||
|
|
||||||
pl := len(pwd)
|
|
||||||
if name == "" || pl < 6 {
|
|
||||||
return fmt.Errorf("%s %s", name, "密码错误")
|
|
||||||
}
|
|
||||||
v := &User{}
|
|
||||||
err := One("Username", name, v)
|
|
||||||
if err != nil || v.Status != 1 {
|
|
||||||
return fmt.Errorf("%s %s", name, "用户名错误")
|
|
||||||
}
|
|
||||||
// 判断用户组信息
|
|
||||||
if !utils.InArrStr(v.Groups, group) {
|
|
||||||
return fmt.Errorf("%s %s", name, "用户组错误")
|
|
||||||
}
|
|
||||||
groupData := &Group{}
|
|
||||||
err = One("Name", group, groupData)
|
|
||||||
if err != nil || groupData.Status != 1 {
|
|
||||||
return fmt.Errorf("%s %s", name, "用户组错误")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 判断otp信息
|
|
||||||
otp := pwd[pl-6:]
|
|
||||||
if !checkOtp(name, otp) {
|
|
||||||
return fmt.Errorf("%s %s", name, "动态码错误")
|
|
||||||
}
|
|
||||||
totp := gotp.NewDefaultTOTP(v.OtpSecret)
|
|
||||||
unix := time.Now().Unix()
|
|
||||||
verify := totp.Verify(otp, int(unix))
|
|
||||||
if !verify {
|
|
||||||
return fmt.Errorf("%s %s", name, "动态码错误")
|
|
||||||
}
|
|
||||||
|
|
||||||
pinCode := pwd[:pl-6]
|
|
||||||
if pinCode != v.PinCode {
|
|
||||||
return fmt.Errorf("%s %s", name, "密码错误")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
userOtpMux = sync.Mutex{}
|
|
||||||
userOtp = map[string]time.Time{}
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
go func() {
|
|
||||||
expire := time.Second * 60
|
|
||||||
|
|
||||||
for range time.Tick(time.Second * 10) {
|
|
||||||
tnow := time.Now()
|
|
||||||
userOtpMux.Lock()
|
|
||||||
for k, v := range userOtp {
|
|
||||||
if tnow.After(v.Add(expire)) {
|
|
||||||
delete(userOtp, k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
userOtpMux.Unlock()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 令牌只能使用一次
|
|
||||||
func checkOtp(username, otp string) bool {
|
|
||||||
key := fmt.Sprintf("%s:%s", username, otp)
|
|
||||||
|
|
||||||
userOtpMux.Lock()
|
|
||||||
defer userOtpMux.Unlock()
|
|
||||||
|
|
||||||
if _, ok := userOtp[key]; ok {
|
|
||||||
// 已经存在
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
userOtp[key] = time.Now()
|
|
||||||
return true
|
|
||||||
}
|
|
28
deploy.sh
@@ -1,28 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
#编译二进制文件
|
|
||||||
go build -o anylink -ldflags "-X main.COMMIT_ID=`git rev-parse HEAD`"
|
|
||||||
|
|
||||||
#编译前端项目
|
|
||||||
git clone https://github.com/bjdgyc/anylink-web.git
|
|
||||||
cd anylink-web
|
|
||||||
#国内可替换源加快速度
|
|
||||||
#npm install --registry=https://registry.npm.taobao.org
|
|
||||||
#npm run build --registry=https://registry.npm.taobao.org
|
|
||||||
npm install
|
|
||||||
npm run build
|
|
||||||
|
|
||||||
cd ../
|
|
||||||
|
|
||||||
#整理部署文件
|
|
||||||
mkdir anylink-deploy
|
|
||||||
|
|
||||||
cp -r anylink anylink-deploy
|
|
||||||
cp -r anylink-web/ui anylink-deploy
|
|
||||||
cp -r conf anylink-deploy
|
|
||||||
cp -r down_files anylink-deploy
|
|
||||||
|
|
||||||
#注意使用root权限运行
|
|
||||||
#cd anylink-deploy
|
|
||||||
#sudo ./anylink -conf="conf/server.toml"
|
|
||||||
|
|
38
doc/README.md
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
## Donate
|
||||||
|
|
||||||
|
> 如果您觉得 AnyLink 对你有帮助,欢迎给我们打赏,也是帮助 AnyLink 更好的发展。
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<img src="screenshot/wxpay2.png" width="435" alt="anylink捐赠二维码" />
|
||||||
|
</p>
|
||||||
|
|
||||||
|
## Donator
|
||||||
|
|
||||||
|
> 感谢以下同学的打赏,AnyLink 有你更美好!
|
||||||
|
>
|
||||||
|
> 需要展示主页的同学,可以在QQ群(567510628) 直接联系我添加。
|
||||||
|
|
||||||
|
| 昵称 | 主页 |
|
||||||
|
|---------| ---------------------------- |
|
||||||
|
| 代码 oo8 | |
|
||||||
|
| 甘磊 | https://github.com/ganlei333 |
|
||||||
|
| Oo@ | https://github.com/chooop |
|
||||||
|
| 虚极静笃 | |
|
||||||
|
| 请喝可乐 | |
|
||||||
|
| 加油加油 | |
|
||||||
|
| 李建 | |
|
||||||
|
| lanbin | |
|
||||||
|
| 乐在东途 | |
|
||||||
|
| 孤鸿 | |
|
||||||
|
| 刘国华 | |
|
||||||
|
| 改名好无聊 | |
|
||||||
|
| 全能互联网专家 | |
|
||||||
|
| JCM | |
|
||||||
|
| Eh... | |
|
||||||
|
| 沉 | |
|
||||||
|
| 刘国华 | |
|
||||||
|
| 忧郁的豚骨拉面 | |
|
||||||
|
| 张小旋当爹地 | |
|
||||||
|
|
||||||
|
|
||||||
|
|
42
doc/question.md
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# 常见问题
|
||||||
|
|
||||||
|
### anyconnect 客户端问题
|
||||||
|
> 客户端请使用群共享文件的版本,其他版本没有测试过,不保证使用正常
|
||||||
|
>
|
||||||
|
> 添加QQ群: 567510628
|
||||||
|
|
||||||
|
### OTP 动态码
|
||||||
|
> 请使用手机安装 freeotp ,然后扫描otp二维码,生成的数字即是动态码
|
||||||
|
|
||||||
|
### 远程桌面连接
|
||||||
|
> 本软件已经支持远程桌面里面连接anyconnect。
|
||||||
|
|
||||||
|
### 私有证书问题
|
||||||
|
> anylink 默认不支持私有证书
|
||||||
|
>
|
||||||
|
> 其他使用私有证书的问题,请自行解决
|
||||||
|
|
||||||
|
### dpd timeout 设置问题
|
||||||
|
```
|
||||||
|
#客户端失效检测时间(秒) dpd > keepalive
|
||||||
|
cstp_keepalive = 20
|
||||||
|
cstp_dpd = 30
|
||||||
|
mobile_keepalive = 40
|
||||||
|
mobile_dpd = 50
|
||||||
|
```
|
||||||
|
> 以上dpd参数为客户端的超时检测时间, 如一段时间内,没有数据传输,防火墙会主动关闭连接
|
||||||
|
>
|
||||||
|
> 如经常出现 timeout 的错误信息,应根据当前防火墙的设置,适当减小dpd数值
|
||||||
|
|
||||||
|
### 性能问题
|
||||||
|
```
|
||||||
|
内网环境测试数据
|
||||||
|
虚拟服务器: centos7 4C8G
|
||||||
|
anylink: tun模式 tcp传输
|
||||||
|
客户端文件下载速度:240Mb/s
|
||||||
|
客户端网卡下载速度:270Mb/s
|
||||||
|
服务端网卡上传速度:280Mb/s
|
||||||
|
```
|
||||||
|
> 客户端tls加密协议、隧道header头都会占用一定带宽
|
||||||
|
|
||||||
|
|
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 44 KiB |
Before Width: | Height: | Size: 47 KiB After Width: | Height: | Size: 47 KiB |
BIN
doc/screenshot/jetbrains.png
Normal file
After Width: | Height: | Size: 164 KiB |
Before Width: | Height: | Size: 55 KiB After Width: | Height: | Size: 55 KiB |
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
Before Width: | Height: | Size: 49 KiB After Width: | Height: | Size: 49 KiB |
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 48 KiB |
BIN
doc/screenshot/wxpay.png
Normal file
After Width: | Height: | Size: 135 KiB |
BIN
doc/screenshot/wxpay2.png
Normal file
After Width: | Height: | Size: 71 KiB |
49
docker/Dockerfile
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
# web
|
||||||
|
FROM node:16.17.1-alpine3.15 as builder_node
|
||||||
|
WORKDIR /web
|
||||||
|
COPY ./web /web
|
||||||
|
RUN yarn install \
|
||||||
|
&& yarn run build \
|
||||||
|
&& ls /web/ui
|
||||||
|
|
||||||
|
# server
|
||||||
|
FROM golang:1.18-alpine as builder_golang
|
||||||
|
#TODO 本地打包时使用镜像
|
||||||
|
ENV GOPROXY=https://goproxy.io
|
||||||
|
ENV GOOS=linux
|
||||||
|
WORKDIR /anylink
|
||||||
|
COPY . /anylink
|
||||||
|
COPY --from=builder_node /web/ui /anylink/server/ui
|
||||||
|
|
||||||
|
#TODO 本地打包时使用镜像
|
||||||
|
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
|
||||||
|
RUN apk add --no-cache git gcc musl-dev
|
||||||
|
RUN cd /anylink/server;go mod tidy;go build -o anylink -ldflags "-X main.CommitId=$(git rev-parse HEAD)" \
|
||||||
|
&& /anylink/server/anylink tool -v
|
||||||
|
|
||||||
|
# anylink
|
||||||
|
FROM alpine
|
||||||
|
LABEL maintainer="github.com/bjdgyc"
|
||||||
|
|
||||||
|
ENV IPV4_CIDR="192.168.10.0/24"
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=builder_golang /anylink/server/anylink /app/
|
||||||
|
COPY docker/docker_entrypoint.sh /app/
|
||||||
|
|
||||||
|
#COPY ./server/bridge-init.sh /app/
|
||||||
|
COPY ./server/conf /app/conf
|
||||||
|
COPY ./LICENSE /app/LICENSE
|
||||||
|
|
||||||
|
|
||||||
|
#TODO 本地打包时使用镜像
|
||||||
|
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
|
||||||
|
RUN apk add --no-cache bash iptables \
|
||||||
|
&& chmod +x /app/docker_entrypoint.sh \
|
||||||
|
&& ls /app
|
||||||
|
|
||||||
|
EXPOSE 443 8800
|
||||||
|
|
||||||
|
#CMD ["/app/anylink"]
|
||||||
|
ENTRYPOINT ["/app/docker_entrypoint.sh"]
|
||||||
|
|
23
docker/docker_entrypoint.sh
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
var1=$1
|
||||||
|
|
||||||
|
#set -x
|
||||||
|
|
||||||
|
case $var1 in
|
||||||
|
"bash" | "sh")
|
||||||
|
echo $var1
|
||||||
|
exec "$@"
|
||||||
|
;;
|
||||||
|
|
||||||
|
"tool")
|
||||||
|
/app/anylink "$@"
|
||||||
|
;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
sysctl -w net.ipv4.ip_forward=1
|
||||||
|
#iptables -t nat -A POSTROUTING -s "${IPV4_CIDR}" -o eth0+ -j MASQUERADE
|
||||||
|
#iptables -nL -t nat
|
||||||
|
|
||||||
|
exec /app/anylink "$@"
|
||||||
|
;;
|
||||||
|
esac
|
41
docker/generate-certs.sh
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
mkdir -p /ssl
|
||||||
|
|
||||||
|
OUTPUT_FILENAME="vpn.xx.com"
|
||||||
|
|
||||||
|
printf "[req]
|
||||||
|
prompt = no
|
||||||
|
default_bits = 4096
|
||||||
|
default_md = sha256
|
||||||
|
encrypt_key = no
|
||||||
|
string_mask = utf8only
|
||||||
|
|
||||||
|
distinguished_name = cert_distinguished_name
|
||||||
|
req_extensions = req_x509v3_extensions
|
||||||
|
x509_extensions = req_x509v3_extensions
|
||||||
|
|
||||||
|
[ cert_distinguished_name ]
|
||||||
|
C = CN
|
||||||
|
ST = BJ
|
||||||
|
L = BJ
|
||||||
|
O = xx.com
|
||||||
|
OU = xx.com
|
||||||
|
CN = xx.com
|
||||||
|
|
||||||
|
[req_x509v3_extensions]
|
||||||
|
basicConstraints = critical,CA:true
|
||||||
|
subjectKeyIdentifier = hash
|
||||||
|
keyUsage = critical,digitalSignature,keyCertSign,cRLSign #,keyEncipherment
|
||||||
|
extendedKeyUsage = critical,serverAuth #, clientAuth
|
||||||
|
subjectAltName = @alt_names
|
||||||
|
|
||||||
|
[alt_names]
|
||||||
|
DNS.1 = xx.com
|
||||||
|
DNS.2 = *.xx.com
|
||||||
|
|
||||||
|
">/ssl/${OUTPUT_FILENAME}.conf
|
||||||
|
|
||||||
|
openssl req -x509 -newkey rsa:2048 -keyout /ssl/test_vpn_key.pem -out /ssl/test_vpn_cert.pem \
|
||||||
|
-days 3600 -nodes -config /ssl/${OUTPUT_FILENAME}.conf
|
||||||
|
|
5
down_files/.gitignore
vendored
@@ -1,5 +0,0 @@
|
|||||||
# Binaries for programs and plugins
|
|
||||||
|
|
||||||
*
|
|
||||||
!.gitignore
|
|
||||||
!index.html
|
|
@@ -1,10 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Title</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
26
go.mod
@@ -1,26 +0,0 @@
|
|||||||
module github.com/bjdgyc/anylink
|
|
||||||
|
|
||||||
go 1.15
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
|
|
||||||
github.com/asdine/storm/v3 v3.2.1
|
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
|
||||||
github.com/go-ole/go-ole v1.2.4 // indirect
|
|
||||||
github.com/google/gopacket v1.1.19
|
|
||||||
github.com/gorilla/mux v1.8.0
|
|
||||||
github.com/mojocn/base64Captcha v1.3.1
|
|
||||||
github.com/pelletier/go-toml v1.8.1
|
|
||||||
github.com/shirou/gopsutil v3.20.11+incompatible
|
|
||||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
|
||||||
github.com/songgao/packets v0.0.0-20160404182456-549a10cd4091
|
|
||||||
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
|
|
||||||
github.com/stretchr/testify v1.6.1
|
|
||||||
github.com/xhit/go-simple-mail/v2 v2.6.0
|
|
||||||
github.com/xlzd/gotp v0.0.0-20181030022105-c8557ba2c119
|
|
||||||
go.etcd.io/bbolt v1.3.5
|
|
||||||
golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9
|
|
||||||
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11
|
|
||||||
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
|
|
||||||
)
|
|
98
go.sum
@@ -1,98 +0,0 @@
|
|||||||
github.com/DataDog/zstd v1.4.1 h1:3oxKN3wbHibqx897utPC2LTQU4J+IHWWJO+glkAkpFM=
|
|
||||||
github.com/DataDog/zstd v1.4.1/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
|
|
||||||
github.com/Sereal/Sereal v0.0.0-20190618215532-0b8ac451a863 h1:BRrxwOZBolJN4gIwvZMJY1tzqBvQgpaZiQRuIDD40jM=
|
|
||||||
github.com/Sereal/Sereal v0.0.0-20190618215532-0b8ac451a863/go.mod h1:D0JMgToj/WdxCgd30Kc1UcA9E+WdZoJqeVOuYW7iTBM=
|
|
||||||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
|
|
||||||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
|
|
||||||
github.com/asdine/storm/v3 v3.2.1 h1:I5AqhkPK6nBZ/qJXySdI7ot5BlXSZ7qvDY1zAn5ZJac=
|
|
||||||
github.com/asdine/storm/v3 v3.2.1/go.mod h1:LEpXwGt4pIqrE/XcTvCnZHT5MgZCV6Ub9q7yQzOFWr0=
|
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
|
||||||
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
|
|
||||||
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
|
|
||||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
|
||||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
|
||||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
|
||||||
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
|
|
||||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
|
||||||
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
|
|
||||||
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
|
||||||
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
|
|
||||||
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
|
|
||||||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
|
||||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
|
||||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
|
||||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
|
||||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
|
||||||
github.com/mojocn/base64Captcha v1.3.1 h1:2Wbkt8Oc8qjmNJ5GyOfSo4tgVQPsbKMftqASnq8GlT0=
|
|
||||||
github.com/mojocn/base64Captcha v1.3.1/go.mod h1:wAQCKEc5bDujxKRmbT6/vTnTt5CjStQ8bRfPWUuz/iY=
|
|
||||||
github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM=
|
|
||||||
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
|
||||||
github.com/shirou/gopsutil v3.20.11+incompatible h1:LJr4ZQK4mPpIV5gOa4jCOKOGb4ty4DZO54I4FGqIpto=
|
|
||||||
github.com/shirou/gopsutil v3.20.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
|
|
||||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
|
|
||||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
|
|
||||||
github.com/songgao/packets v0.0.0-20160404182456-549a10cd4091 h1:1zN6ImoqhSJhN8hGXFaJlSC8msLmIbX8bFqOfWLKw0w=
|
|
||||||
github.com/songgao/packets v0.0.0-20160404182456-549a10cd4091/go.mod h1:N20Z5Y8oye9a7HmytmZ+tr8Q2vlP0tAHP13kTHzwvQY=
|
|
||||||
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8 h1:TG/diQgUe0pntT/2D9tmUCz4VNwm9MfrtPr0SU2qSX8=
|
|
||||||
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8/go.mod h1:P5HUIBuIWKbyjl083/loAegFkfbFNx5i2qEP4CNbm7E=
|
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
|
||||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
|
||||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
|
||||||
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
|
|
||||||
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
|
|
||||||
github.com/xhit/go-simple-mail/v2 v2.6.0 h1:pvPmpDUUWy07cnTgwxwEe5fjdyYtETnxcvdGPQxtv/k=
|
|
||||||
github.com/xhit/go-simple-mail/v2 v2.6.0/go.mod h1:kA1XbQfCI4JxQ9ccSN6VFyIEkkugOm7YiPkA5hKiQn4=
|
|
||||||
github.com/xlzd/gotp v0.0.0-20181030022105-c8557ba2c119 h1:YyPWX3jLOtYKulBR6AScGIs74lLrJcgeKRwcbAuQOG4=
|
|
||||||
github.com/xlzd/gotp v0.0.0-20181030022105-c8557ba2c119/go.mod h1:/nuTSlK+okRfR/vnIPqR89fFKonnWPiZymN5ydRJkX8=
|
|
||||||
go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
|
|
||||||
go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0=
|
|
||||||
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
|
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
|
||||||
golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9 h1:sYNJzB4J8toYPQTM6pAkcmBRgw9SnQKP9oXCHfgy604=
|
|
||||||
golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
|
||||||
golang.org/x/image v0.0.0-20190501045829-6d32002ffd75 h1:TbGuee8sSq15Iguxu4deQ7+Bqq/d2rsQejGcEtADAMQ=
|
|
||||||
golang.org/x/image v0.0.0-20190501045829-6d32002ffd75/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
|
|
||||||
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
|
|
||||||
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
|
||||||
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
|
||||||
golang.org/x/net v0.0.0-20191105084925-a882066a44e0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
|
||||||
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11 h1:lwlPPsmjDKK0J6eG6xDWd5XPehI0R024zxjDnw3esPA=
|
|
||||||
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
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/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
|
||||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 h1:Hir2P/De0WpUhtrKGGjvSb2YxUgyZ7EFOSLIcSSpiwE=
|
|
||||||
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM=
|
|
||||||
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
@@ -1,5 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
// 暂时没有实现
|
|
||||||
func startDtls() {
|
|
||||||
}
|
|
@@ -1,115 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/binary"
|
|
||||||
"net"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/bjdgyc/anylink/base"
|
|
||||||
"github.com/bjdgyc/anylink/sessdata"
|
|
||||||
)
|
|
||||||
|
|
||||||
func LinkCstp(conn net.Conn, cSess *sessdata.ConnSession) {
|
|
||||||
defer func() {
|
|
||||||
// log.Println("LinkCstp return")
|
|
||||||
conn.Close()
|
|
||||||
cSess.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
var (
|
|
||||||
err error
|
|
||||||
n int
|
|
||||||
dataLen uint16
|
|
||||||
dead = time.Duration(cSess.CstpDpd+5) * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
go cstpWrite(conn, cSess)
|
|
||||||
|
|
||||||
for {
|
|
||||||
|
|
||||||
// 设置超时限制
|
|
||||||
err = conn.SetReadDeadline(time.Now().Add(dead))
|
|
||||||
if err != nil {
|
|
||||||
base.Error("SetDeadline: ", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
hdata := make([]byte, BufferSize)
|
|
||||||
n, err = conn.Read(hdata)
|
|
||||||
if err != nil {
|
|
||||||
base.Error("read hdata: ", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 限流设置
|
|
||||||
err = cSess.RateLimit(n, true)
|
|
||||||
if err != nil {
|
|
||||||
base.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch hdata[6] {
|
|
||||||
case 0x07: // KEEPALIVE
|
|
||||||
// do nothing
|
|
||||||
// base.Debug("recv keepalive", cSess.IpAddr)
|
|
||||||
case 0x05: // DISCONNECT
|
|
||||||
base.Debug("DISCONNECT", cSess.IpAddr)
|
|
||||||
return
|
|
||||||
case 0x03: // DPD-REQ
|
|
||||||
// base.Debug("recv DPD-REQ", cSess.IpAddr)
|
|
||||||
if payloadOut(cSess, sessdata.LTypeIPData, 0x04, nil) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
case 0x04:
|
|
||||||
// log.Println("recv DPD-RESP")
|
|
||||||
case 0x00: // DATA
|
|
||||||
dataLen = binary.BigEndian.Uint16(hdata[4:6]) // 4,5
|
|
||||||
if payloadIn(cSess, sessdata.LTypeIPData, 0x00, hdata[8:8+dataLen]) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func cstpWrite(conn net.Conn, cSess *sessdata.ConnSession) {
|
|
||||||
defer func() {
|
|
||||||
// log.Println("cstpWrite return")
|
|
||||||
conn.Close()
|
|
||||||
cSess.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
var (
|
|
||||||
err error
|
|
||||||
n int
|
|
||||||
header []byte
|
|
||||||
payload *sessdata.Payload
|
|
||||||
)
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case payload = <-cSess.PayloadOut:
|
|
||||||
case <-cSess.CloseChan:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if payload.LType != sessdata.LTypeIPData {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
header = []byte{'S', 'T', 'F', 0x01, 0x00, 0x00, payload.PType, 0x00}
|
|
||||||
if payload.PType == 0x00 { // data
|
|
||||||
binary.BigEndian.PutUint16(header[4:6], uint16(len(payload.Data)))
|
|
||||||
header = append(header, payload.Data...)
|
|
||||||
}
|
|
||||||
n, err = conn.Write(header)
|
|
||||||
if err != nil {
|
|
||||||
base.Error("write err", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 限流设置
|
|
||||||
err = cSess.RateLimit(n, false)
|
|
||||||
if err != nil {
|
|
||||||
base.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,160 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/bjdgyc/anylink/base"
|
|
||||||
"github.com/bjdgyc/anylink/sessdata"
|
|
||||||
)
|
|
||||||
|
|
||||||
var hn string
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
// 获取主机名称
|
|
||||||
hn, _ = os.Hostname()
|
|
||||||
}
|
|
||||||
|
|
||||||
func LinkTunnel(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// TODO 调试信息输出
|
|
||||||
// hd, _ := httputil.DumpRequest(r, true)
|
|
||||||
// fmt.Println("DumpRequest: ", string(hd))
|
|
||||||
// fmt.Println("LinkTunnel", r.RemoteAddr)
|
|
||||||
|
|
||||||
// 判断session-token的值
|
|
||||||
cookie, err := r.Cookie("webvpn")
|
|
||||||
if err != nil || cookie.Value == "" {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
sess := sessdata.SToken2Sess(cookie.Value)
|
|
||||||
if sess == nil {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 开启link
|
|
||||||
cSess := sess.NewConn()
|
|
||||||
if cSess == nil {
|
|
||||||
log.Println(err)
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 客户端信息
|
|
||||||
cstpMtu := r.Header.Get("X-CSTP-MTU")
|
|
||||||
masterSecret := r.Header.Get("X-DTLS-Master-Secret")
|
|
||||||
localIp := r.Header.Get("X-Cstp-Local-Address-Ip4")
|
|
||||||
mobile := r.Header.Get("X-Cstp-License")
|
|
||||||
|
|
||||||
cSess.SetMtu(cstpMtu)
|
|
||||||
cSess.MasterSecret = masterSecret
|
|
||||||
cSess.RemoteAddr = r.RemoteAddr
|
|
||||||
cSess.LocalIp = net.ParseIP(localIp)
|
|
||||||
cstpKeepalive := base.Cfg.CstpKeepalive
|
|
||||||
cstpDpd := base.Cfg.CstpDpd
|
|
||||||
cSess.Client = "pc"
|
|
||||||
if mobile == "mobile" {
|
|
||||||
// 手机客户端
|
|
||||||
cstpKeepalive = base.Cfg.MobileKeepalive
|
|
||||||
cstpDpd = base.Cfg.MobileDpd
|
|
||||||
cSess.Client = "mobile"
|
|
||||||
}
|
|
||||||
cSess.CstpDpd = cstpDpd
|
|
||||||
|
|
||||||
base.Debug(cSess.IpAddr, cSess.MacHw, sess.Username, mobile)
|
|
||||||
|
|
||||||
// 返回客户端数据
|
|
||||||
w.Header().Set("Server", fmt.Sprintf("%s %s", base.APP_NAME, base.APP_VER))
|
|
||||||
w.Header().Set("X-CSTP-Version", "1")
|
|
||||||
w.Header().Set("X-CSTP-Protocol", "Copyright (c) 2004 Cisco Systems, Inc.")
|
|
||||||
w.Header().Set("X-CSTP-Address", cSess.IpAddr.String()) // 分配的ip地址
|
|
||||||
w.Header().Set("X-CSTP-Netmask", base.Cfg.Ipv4Netmask) // 子网掩码
|
|
||||||
w.Header().Set("X-CSTP-Hostname", hn) // 机器名称
|
|
||||||
|
|
||||||
// 允许本地LAN访问vpn网络,必须放在路由的第一个
|
|
||||||
if cSess.Group.AllowLan {
|
|
||||||
w.Header().Set("X-CSTP-Split-Exclude", "0.0.0.0/255.255.255.255")
|
|
||||||
}
|
|
||||||
// dns地址
|
|
||||||
for _, v := range cSess.Group.ClientDns {
|
|
||||||
w.Header().Add("X-CSTP-DNS", v.Val)
|
|
||||||
}
|
|
||||||
// 允许的路由
|
|
||||||
for _, v := range cSess.Group.RouteInclude {
|
|
||||||
w.Header().Add("X-CSTP-Split-Include", v.IpMask)
|
|
||||||
}
|
|
||||||
// 不允许的路由
|
|
||||||
for _, v := range cSess.Group.RouteExclude {
|
|
||||||
w.Header().Add("X-CSTP-Split-Exclude", v.IpMask)
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("X-CSTP-Lease-Duration", fmt.Sprintf("%d", base.Cfg.IpLease)) // ip地址租期
|
|
||||||
w.Header().Set("X-CSTP-Session-Timeout", "none")
|
|
||||||
w.Header().Set("X-CSTP-Session-Timeout-Alert-Interval", "60")
|
|
||||||
w.Header().Set("X-CSTP-Session-Timeout-Remaining", "none")
|
|
||||||
w.Header().Set("X-CSTP-Idle-Timeout", "18000")
|
|
||||||
w.Header().Set("X-CSTP-Disconnected-Timeout", "18000")
|
|
||||||
w.Header().Set("X-CSTP-Keep", "true")
|
|
||||||
w.Header().Set("X-CSTP-Tunnel-All-DNS", "false")
|
|
||||||
|
|
||||||
w.Header().Set("X-CSTP-Rekey-Time", "172800")
|
|
||||||
w.Header().Set("X-CSTP-Rekey-Method", "new-tunnel")
|
|
||||||
|
|
||||||
w.Header().Set("X-CSTP-DPD", fmt.Sprintf("%d", cstpDpd))
|
|
||||||
w.Header().Set("X-CSTP-Keepalive", fmt.Sprintf("%d", cstpKeepalive))
|
|
||||||
// w.Header().Set("X-CSTP-Banner", banner.Banner)
|
|
||||||
w.Header().Set("X-CSTP-MSIE-Proxy-Lockdown", "true")
|
|
||||||
w.Header().Set("X-CSTP-Smartcard-Removal-Disconnect", "true")
|
|
||||||
|
|
||||||
w.Header().Set("X-CSTP-MTU", fmt.Sprintf("%d", cSess.Mtu)) // 1399
|
|
||||||
w.Header().Set("X-DTLS-MTU", fmt.Sprintf("%d", cSess.Mtu))
|
|
||||||
|
|
||||||
w.Header().Set("X-DTLS-Session-ID", sess.DtlsSid)
|
|
||||||
w.Header().Set("X-DTLS-Port", "4433")
|
|
||||||
w.Header().Set("X-DTLS-Keepalive", fmt.Sprintf("%d", base.Cfg.CstpKeepalive))
|
|
||||||
w.Header().Set("X-DTLS-Rekey-Time", "5400")
|
|
||||||
w.Header().Set("X-DTLS12-CipherSuite", "ECDHE-ECDSA-AES128-GCM-SHA256")
|
|
||||||
// w.Header().Set("X-DTLS12-CipherSuite", "ECDHE-RSA-AES128-GCM-SHA256")
|
|
||||||
|
|
||||||
w.Header().Set("X-CSTP-License", "accept")
|
|
||||||
w.Header().Set("X-CSTP-Routing-Filtering-Ignore", "false")
|
|
||||||
w.Header().Set("X-CSTP-Quarantine", "false")
|
|
||||||
w.Header().Set("X-CSTP-Disable-Always-On-VPN", "false")
|
|
||||||
w.Header().Set("X-CSTP-Client-Bypass-Protocol", "false")
|
|
||||||
w.Header().Set("X-CSTP-TCP-Keepalive", "false")
|
|
||||||
// w.Header().Set("X-CSTP-Post-Auth-XML", ``)
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
|
|
||||||
h := w.Header().Clone()
|
|
||||||
headers := make([]byte, 0)
|
|
||||||
buf := bytes.NewBuffer(headers)
|
|
||||||
h.Write(buf)
|
|
||||||
base.Debug(string(buf.Bytes()))
|
|
||||||
|
|
||||||
hj := w.(http.Hijacker)
|
|
||||||
conn, _, err := hj.Hijack()
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 开始数据处理
|
|
||||||
switch base.Cfg.LinkMode {
|
|
||||||
case base.LinkModeTUN:
|
|
||||||
err = LinkTun(cSess)
|
|
||||||
case base.LinkModeTAP:
|
|
||||||
err = LinkTap(cSess)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
go LinkCstp(conn, cSess)
|
|
||||||
}
|
|
@@ -1,47 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import "github.com/bjdgyc/anylink/sessdata"
|
|
||||||
|
|
||||||
func payloadIn(cSess *sessdata.ConnSession, lType sessdata.LType, pType byte, data []byte) bool {
|
|
||||||
payload := &sessdata.Payload{
|
|
||||||
LType: lType,
|
|
||||||
PType: pType,
|
|
||||||
Data: data,
|
|
||||||
}
|
|
||||||
|
|
||||||
return payloadInData(cSess, payload)
|
|
||||||
}
|
|
||||||
|
|
||||||
func payloadInData(cSess *sessdata.ConnSession, payload *sessdata.Payload) bool {
|
|
||||||
closed := false
|
|
||||||
|
|
||||||
select {
|
|
||||||
case cSess.PayloadIn <- payload:
|
|
||||||
case <-cSess.CloseChan:
|
|
||||||
closed = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return closed
|
|
||||||
}
|
|
||||||
|
|
||||||
func payloadOut(cSess *sessdata.ConnSession, lType sessdata.LType, pType byte, data []byte) bool {
|
|
||||||
payload := &sessdata.Payload{
|
|
||||||
LType: lType,
|
|
||||||
PType: pType,
|
|
||||||
Data: data,
|
|
||||||
}
|
|
||||||
|
|
||||||
return payloadOutData(cSess, payload)
|
|
||||||
}
|
|
||||||
|
|
||||||
func payloadOutData(cSess *sessdata.ConnSession, payload *sessdata.Payload) bool {
|
|
||||||
closed := false
|
|
||||||
|
|
||||||
select {
|
|
||||||
case cSess.PayloadOut <- payload:
|
|
||||||
case <-cSess.CloseChan:
|
|
||||||
closed = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return closed
|
|
||||||
}
|
|
@@ -1,76 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/tls"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/bjdgyc/anylink/base"
|
|
||||||
"github.com/bjdgyc/anylink/pkg/proxyproto"
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
)
|
|
||||||
|
|
||||||
func startTls() {
|
|
||||||
addr := base.Cfg.ServerAddr
|
|
||||||
certFile := base.Cfg.CertFile
|
|
||||||
keyFile := base.Cfg.CertKey
|
|
||||||
|
|
||||||
logger := log.New(os.Stdout, "[SERVER]", log.Lshortfile|log.Ldate)
|
|
||||||
// 设置tls信息
|
|
||||||
tlsConfig := &tls.Config{
|
|
||||||
NextProtos: []string{"http/1.1"},
|
|
||||||
MinVersion: tls.VersionTLS12,
|
|
||||||
}
|
|
||||||
srv := &http.Server{
|
|
||||||
Addr: addr,
|
|
||||||
Handler: initRoute(),
|
|
||||||
TLSConfig: tlsConfig,
|
|
||||||
ErrorLog: logger,
|
|
||||||
}
|
|
||||||
|
|
||||||
var ln net.Listener
|
|
||||||
|
|
||||||
ln, err := net.Listen("tcp", addr)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
defer ln.Close()
|
|
||||||
|
|
||||||
if base.Cfg.ProxyProtocol {
|
|
||||||
ln = &proxyproto.Listener{Listener: ln, ProxyHeaderTimeout: time.Second * 5}
|
|
||||||
}
|
|
||||||
|
|
||||||
base.Info("listen server", addr)
|
|
||||||
err = srv.ServeTLS(ln, certFile, keyFile)
|
|
||||||
if err != nil {
|
|
||||||
base.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func initRoute() http.Handler {
|
|
||||||
r := mux.NewRouter()
|
|
||||||
r.HandleFunc("/", LinkHome).Methods(http.MethodGet)
|
|
||||||
r.HandleFunc("/", LinkAuth).Methods(http.MethodPost)
|
|
||||||
r.HandleFunc("/CSCOSSLC/tunnel", LinkTunnel).Methods(http.MethodConnect)
|
|
||||||
r.HandleFunc("/otp_qr", LinkOtpQr).Methods(http.MethodGet)
|
|
||||||
r.PathPrefix("/down_files/").Handler(
|
|
||||||
http.StripPrefix("/down_files/",
|
|
||||||
http.FileServer(http.Dir(base.Cfg.DownFilesPath)),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
r.NotFoundHandler = http.HandlerFunc(notFound)
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
func notFound(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// fmt.Println(r.RemoteAddr)
|
|
||||||
// hu, _ := httputil.DumpRequest(r, true)
|
|
||||||
// fmt.Println("NotFound: ", string(hu))
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
fmt.Fprintln(w, "404 page not found")
|
|
||||||
}
|
|
@@ -1,25 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/bjdgyc/anylink/admin"
|
|
||||||
"github.com/bjdgyc/anylink/base"
|
|
||||||
"github.com/bjdgyc/anylink/dbdata"
|
|
||||||
"github.com/bjdgyc/anylink/sessdata"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Start() {
|
|
||||||
dbdata.Start()
|
|
||||||
sessdata.Start()
|
|
||||||
|
|
||||||
checkTun()
|
|
||||||
if base.Cfg.LinkMode == base.LinkModeTAP {
|
|
||||||
checkTap()
|
|
||||||
}
|
|
||||||
go admin.StartAdmin()
|
|
||||||
go startTls()
|
|
||||||
go startDtls()
|
|
||||||
}
|
|
||||||
|
|
||||||
func Stop() {
|
|
||||||
dbdata.Stop()
|
|
||||||
}
|
|
@@ -1,288 +0,0 @@
|
|||||||
// copy from: https://github.com/armon/go-proxyproto/blob/master/protocol.go
|
|
||||||
// design: http://www.haproxy.org/download/2.2/doc/proxy-protocol.txt
|
|
||||||
|
|
||||||
// HAProxy proxy proto v1
|
|
||||||
package proxyproto
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"net"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// prefix is the string we look for at the start of a connection
|
|
||||||
// to check if this connection is using the proxy protocol
|
|
||||||
prefix = []byte("PROXY ")
|
|
||||||
prefixLen = len(prefix)
|
|
||||||
|
|
||||||
ErrInvalidUpstream = errors.New("upstream connection address not trusted for PROXY information")
|
|
||||||
)
|
|
||||||
|
|
||||||
// SourceChecker can be used to decide whether to trust the PROXY info or pass
|
|
||||||
// the original connection address through. If set, the connecting address is
|
|
||||||
// passed in as an argument. If the function returns an error due to the source
|
|
||||||
// being disallowed, it should return ErrInvalidUpstream.
|
|
||||||
//
|
|
||||||
// If error is not nil, the call to Accept() will fail. If the reason for
|
|
||||||
// triggering this failure is due to a disallowed source, it should return
|
|
||||||
// ErrInvalidUpstream.
|
|
||||||
//
|
|
||||||
// If bool is true, the PROXY-set address is used.
|
|
||||||
//
|
|
||||||
// If bool is false, the connection's remote address is used, rather than the
|
|
||||||
// address claimed in the PROXY info.
|
|
||||||
type SourceChecker func(net.Addr) (bool, error)
|
|
||||||
|
|
||||||
// Listener is used to wrap an underlying listener,
|
|
||||||
// whose connections may be using the HAProxy Proxy Protocol (version 1).
|
|
||||||
// If the connection is using the protocol, the RemoteAddr() will return
|
|
||||||
// the correct client address.
|
|
||||||
//
|
|
||||||
// Optionally define ProxyHeaderTimeout to set a maximum time to
|
|
||||||
// receive the Proxy Protocol Header. Zero means no timeout.
|
|
||||||
type Listener struct {
|
|
||||||
Listener net.Listener
|
|
||||||
ProxyHeaderTimeout time.Duration
|
|
||||||
SourceCheck SourceChecker
|
|
||||||
UnknownOK bool // allow PROXY UNKNOWN
|
|
||||||
}
|
|
||||||
|
|
||||||
// Conn is used to wrap and underlying connection which
|
|
||||||
// may be speaking the Proxy Protocol. If it is, the RemoteAddr() will
|
|
||||||
// return the address of the client instead of the proxy address.
|
|
||||||
type Conn struct {
|
|
||||||
bufReader *bufio.Reader
|
|
||||||
conn net.Conn
|
|
||||||
dstAddr *net.TCPAddr
|
|
||||||
srcAddr *net.TCPAddr
|
|
||||||
useConnAddr bool
|
|
||||||
once sync.Once
|
|
||||||
proxyHeaderTimeout time.Duration
|
|
||||||
unknownOK bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// Accept waits for and returns the next connection to the listener.
|
|
||||||
func (p *Listener) Accept() (net.Conn, error) {
|
|
||||||
// Get the underlying connection
|
|
||||||
conn, err := p.Listener.Accept()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var useConnAddr bool
|
|
||||||
if p.SourceCheck != nil {
|
|
||||||
allowed, err := p.SourceCheck(conn.RemoteAddr())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if !allowed {
|
|
||||||
useConnAddr = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
newConn := NewConn(conn, p.ProxyHeaderTimeout)
|
|
||||||
newConn.useConnAddr = useConnAddr
|
|
||||||
newConn.unknownOK = p.UnknownOK
|
|
||||||
return newConn, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close closes the underlying listener.
|
|
||||||
func (p *Listener) Close() error {
|
|
||||||
return p.Listener.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Addr returns the underlying listener's network address.
|
|
||||||
func (p *Listener) Addr() net.Addr {
|
|
||||||
return p.Listener.Addr()
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewConn is used to wrap a net.Conn that may be speaking
|
|
||||||
// the proxy protocol into a proxyproto.Conn
|
|
||||||
func NewConn(conn net.Conn, timeout time.Duration) *Conn {
|
|
||||||
pConn := &Conn{
|
|
||||||
bufReader: bufio.NewReader(conn),
|
|
||||||
conn: conn,
|
|
||||||
proxyHeaderTimeout: timeout,
|
|
||||||
}
|
|
||||||
return pConn
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read is check for the proxy protocol header when doing
|
|
||||||
// the initial scan. If there is an error parsing the header,
|
|
||||||
// it is returned and the socket is closed.
|
|
||||||
func (p *Conn) Read(b []byte) (int, error) {
|
|
||||||
var err error
|
|
||||||
p.once.Do(func() { err = p.checkPrefix() })
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return p.bufReader.Read(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Conn) ReadFrom(r io.Reader) (int64, error) {
|
|
||||||
if rf, ok := p.conn.(io.ReaderFrom); ok {
|
|
||||||
return rf.ReadFrom(r)
|
|
||||||
}
|
|
||||||
return io.Copy(p.conn, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Conn) WriteTo(w io.Writer) (int64, error) {
|
|
||||||
var err error
|
|
||||||
p.once.Do(func() { err = p.checkPrefix() })
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return p.bufReader.WriteTo(w)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Conn) Write(b []byte) (int, error) {
|
|
||||||
return p.conn.Write(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Conn) Close() error {
|
|
||||||
return p.conn.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Conn) LocalAddr() net.Addr {
|
|
||||||
p.checkPrefixOnce()
|
|
||||||
if p.dstAddr != nil && !p.useConnAddr {
|
|
||||||
return p.dstAddr
|
|
||||||
}
|
|
||||||
return p.conn.LocalAddr()
|
|
||||||
}
|
|
||||||
|
|
||||||
// RemoteAddr returns the address of the client if the proxy
|
|
||||||
// protocol is being used, otherwise just returns the address of
|
|
||||||
// the socket peer. If there is an error parsing the header, the
|
|
||||||
// address of the client is not returned, and the socket is closed.
|
|
||||||
// Once implication of this is that the call could block if the
|
|
||||||
// client is slow. Using a Deadline is recommended if this is called
|
|
||||||
// before Read()
|
|
||||||
func (p *Conn) RemoteAddr() net.Addr {
|
|
||||||
p.checkPrefixOnce()
|
|
||||||
if p.srcAddr != nil && !p.useConnAddr {
|
|
||||||
return p.srcAddr
|
|
||||||
}
|
|
||||||
return p.conn.RemoteAddr()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Conn) SetDeadline(t time.Time) error {
|
|
||||||
return p.conn.SetDeadline(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Conn) SetReadDeadline(t time.Time) error {
|
|
||||||
return p.conn.SetReadDeadline(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Conn) SetWriteDeadline(t time.Time) error {
|
|
||||||
return p.conn.SetWriteDeadline(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Conn) checkPrefixOnce() {
|
|
||||||
p.once.Do(func() {
|
|
||||||
if err := p.checkPrefix(); err != nil && err != io.EOF {
|
|
||||||
log.Printf("[ERR] Failed to read proxy prefix: %v", err)
|
|
||||||
p.Close()
|
|
||||||
p.bufReader = bufio.NewReader(p.conn)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Conn) checkPrefix() error {
|
|
||||||
if p.proxyHeaderTimeout != 0 {
|
|
||||||
readDeadLine := time.Now().Add(p.proxyHeaderTimeout)
|
|
||||||
p.conn.SetReadDeadline(readDeadLine)
|
|
||||||
defer p.conn.SetReadDeadline(time.Time{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Incrementally check each byte of the prefix
|
|
||||||
for i := 1; i <= prefixLen; i++ {
|
|
||||||
inp, err := p.bufReader.Peek(i)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
|
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for a prefix mis-match, quit early
|
|
||||||
if !bytes.Equal(inp, prefix[:i]) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read the header line
|
|
||||||
header, err := p.bufReader.ReadString('\n')
|
|
||||||
if err != nil {
|
|
||||||
p.conn.Close()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strip the carriage return and new line
|
|
||||||
header = header[:len(header)-2]
|
|
||||||
|
|
||||||
// Split on spaces, should be (PROXY <type> <src addr> <dst addr> <src port> <dst port>)
|
|
||||||
parts := strings.Split(header, " ")
|
|
||||||
if len(parts) < 2 {
|
|
||||||
p.conn.Close()
|
|
||||||
return fmt.Errorf("Invalid header line: %s", header)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify the type is known
|
|
||||||
switch parts[1] {
|
|
||||||
case "UNKNOWN":
|
|
||||||
if !p.unknownOK || len(parts) != 2 {
|
|
||||||
p.conn.Close()
|
|
||||||
return fmt.Errorf("Invalid UNKNOWN header line: %s", header)
|
|
||||||
}
|
|
||||||
p.useConnAddr = true
|
|
||||||
return nil
|
|
||||||
case "TCP4":
|
|
||||||
case "TCP6":
|
|
||||||
default:
|
|
||||||
p.conn.Close()
|
|
||||||
return fmt.Errorf("Unhandled address type: %s", parts[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(parts) != 6 {
|
|
||||||
p.conn.Close()
|
|
||||||
return fmt.Errorf("Invalid header line: %s", header)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse out the source address
|
|
||||||
ip := net.ParseIP(parts[2])
|
|
||||||
if ip == nil {
|
|
||||||
p.conn.Close()
|
|
||||||
return fmt.Errorf("Invalid source ip: %s", parts[2])
|
|
||||||
}
|
|
||||||
port, err := strconv.Atoi(parts[4])
|
|
||||||
if err != nil {
|
|
||||||
p.conn.Close()
|
|
||||||
return fmt.Errorf("Invalid source port: %s", parts[4])
|
|
||||||
}
|
|
||||||
p.srcAddr = &net.TCPAddr{IP: ip, Port: port}
|
|
||||||
|
|
||||||
// Parse out the destination address
|
|
||||||
ip = net.ParseIP(parts[3])
|
|
||||||
if ip == nil {
|
|
||||||
p.conn.Close()
|
|
||||||
return fmt.Errorf("Invalid destination ip: %s", parts[3])
|
|
||||||
}
|
|
||||||
port, err = strconv.Atoi(parts[5])
|
|
||||||
if err != nil {
|
|
||||||
p.conn.Close()
|
|
||||||
return fmt.Errorf("Invalid destination port: %s", parts[5])
|
|
||||||
}
|
|
||||||
p.dstAddr = &net.TCPAddr{IP: ip, Port: port}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
@@ -1,486 +0,0 @@
|
|||||||
// copy from: https://github.com/armon/go-proxyproto/blob/master/protocol_test.go
|
|
||||||
package proxyproto
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
goodAddr = "127.0.0.1"
|
|
||||||
badAddr = "127.0.0.2"
|
|
||||||
errAddr = "9999.0.0.2"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
checkAddr string
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPassthrough(t *testing.T) {
|
|
||||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pl := &Listener{Listener: l}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
conn, err := net.Dial("tcp", pl.Addr().String())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
conn.Write([]byte("ping"))
|
|
||||||
recv := make([]byte, 4)
|
|
||||||
_, err = conn.Read(recv)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(recv, []byte("pong")) {
|
|
||||||
t.Fatalf("bad: %v", recv)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
conn, err := pl.Accept()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
recv := make([]byte, 4)
|
|
||||||
_, err = conn.Read(recv)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(recv, []byte("ping")) {
|
|
||||||
t.Fatalf("bad: %v", recv)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := conn.Write([]byte("pong")); err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTimeout(t *testing.T) {
|
|
||||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
clientWriteDelay := 200 * time.Millisecond
|
|
||||||
proxyHeaderTimeout := 50 * time.Millisecond
|
|
||||||
pl := &Listener{Listener: l, ProxyHeaderTimeout: proxyHeaderTimeout}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
conn, err := net.Dial("tcp", pl.Addr().String())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// Do not send data for a while
|
|
||||||
time.Sleep(clientWriteDelay)
|
|
||||||
|
|
||||||
conn.Write([]byte("ping"))
|
|
||||||
recv := make([]byte, 4)
|
|
||||||
_, err = conn.Read(recv)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(recv, []byte("pong")) {
|
|
||||||
t.Fatalf("bad: %v", recv)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
conn, err := pl.Accept()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// Check the remote addr is the original 127.0.0.1
|
|
||||||
remoteAddrStartTime := time.Now()
|
|
||||||
addr := conn.RemoteAddr().(*net.TCPAddr)
|
|
||||||
if addr.IP.String() != "127.0.0.1" {
|
|
||||||
t.Fatalf("bad: %v", addr)
|
|
||||||
}
|
|
||||||
remoteAddrDuration := time.Since(remoteAddrStartTime)
|
|
||||||
|
|
||||||
// Check RemoteAddr() call did timeout
|
|
||||||
if remoteAddrDuration >= clientWriteDelay {
|
|
||||||
t.Fatalf("RemoteAddr() took longer than the specified timeout: %v < %v", proxyHeaderTimeout, remoteAddrDuration)
|
|
||||||
}
|
|
||||||
|
|
||||||
recv := make([]byte, 4)
|
|
||||||
_, err = conn.Read(recv)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(recv, []byte("ping")) {
|
|
||||||
t.Fatalf("bad: %v", recv)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := conn.Write([]byte("pong")); err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParse_ipv4(t *testing.T) {
|
|
||||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pl := &Listener{Listener: l}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
conn, err := net.Dial("tcp", pl.Addr().String())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// Write out the header!
|
|
||||||
header := "PROXY TCP4 10.1.1.1 20.2.2.2 1000 2000\r\n"
|
|
||||||
conn.Write([]byte(header))
|
|
||||||
|
|
||||||
conn.Write([]byte("ping"))
|
|
||||||
recv := make([]byte, 4)
|
|
||||||
_, err = conn.Read(recv)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(recv, []byte("pong")) {
|
|
||||||
t.Fatalf("bad: %v", recv)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
conn, err := pl.Accept()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
recv := make([]byte, 4)
|
|
||||||
_, err = conn.Read(recv)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(recv, []byte("ping")) {
|
|
||||||
t.Fatalf("bad: %v", recv)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := conn.Write([]byte("pong")); err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check the remote addr
|
|
||||||
addr := conn.RemoteAddr().(*net.TCPAddr)
|
|
||||||
if addr.IP.String() != "10.1.1.1" {
|
|
||||||
t.Fatalf("bad: %v", addr)
|
|
||||||
}
|
|
||||||
if addr.Port != 1000 {
|
|
||||||
t.Fatalf("bad: %v", addr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParse_ipv6(t *testing.T) {
|
|
||||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pl := &Listener{Listener: l}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
conn, err := net.Dial("tcp", pl.Addr().String())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// Write out the header!
|
|
||||||
header := "PROXY TCP6 ffff::ffff ffff::ffff 1000 2000\r\n"
|
|
||||||
conn.Write([]byte(header))
|
|
||||||
|
|
||||||
conn.Write([]byte("ping"))
|
|
||||||
recv := make([]byte, 4)
|
|
||||||
_, err = conn.Read(recv)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(recv, []byte("pong")) {
|
|
||||||
t.Fatalf("bad: %v", recv)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
conn, err := pl.Accept()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
recv := make([]byte, 4)
|
|
||||||
_, err = conn.Read(recv)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(recv, []byte("ping")) {
|
|
||||||
t.Fatalf("bad: %v", recv)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := conn.Write([]byte("pong")); err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check the remote addr
|
|
||||||
addr := conn.RemoteAddr().(*net.TCPAddr)
|
|
||||||
if addr.IP.String() != "ffff::ffff" {
|
|
||||||
t.Fatalf("bad: %v", addr)
|
|
||||||
}
|
|
||||||
if addr.Port != 1000 {
|
|
||||||
t.Fatalf("bad: %v", addr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParse_Unknown(t *testing.T) {
|
|
||||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pl := &Listener{Listener: l, UnknownOK: true}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
conn, err := net.Dial("tcp", pl.Addr().String())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// Write out the header!
|
|
||||||
header := "PROXY UNKNOWN\r\n"
|
|
||||||
conn.Write([]byte(header))
|
|
||||||
|
|
||||||
conn.Write([]byte("ping"))
|
|
||||||
recv := make([]byte, 4)
|
|
||||||
_, err = conn.Read(recv)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(recv, []byte("pong")) {
|
|
||||||
t.Fatalf("bad: %v", recv)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
conn, err := pl.Accept()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
recv := make([]byte, 4)
|
|
||||||
_, err = conn.Read(recv)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(recv, []byte("ping")) {
|
|
||||||
t.Fatalf("bad: %v", recv)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := conn.Write([]byte("pong")); err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParse_BadHeader(t *testing.T) {
|
|
||||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pl := &Listener{Listener: l}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
conn, err := net.Dial("tcp", pl.Addr().String())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// Write out the header!
|
|
||||||
header := "PROXY TCP4 what 127.0.0.1 1000 2000\r\n"
|
|
||||||
conn.Write([]byte(header))
|
|
||||||
|
|
||||||
conn.Write([]byte("ping"))
|
|
||||||
|
|
||||||
recv := make([]byte, 4)
|
|
||||||
_, err = conn.Read(recv)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
conn, err := pl.Accept()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// Check the remote addr, should be the local addr
|
|
||||||
addr := conn.RemoteAddr().(*net.TCPAddr)
|
|
||||||
if addr.IP.String() != "127.0.0.1" {
|
|
||||||
t.Fatalf("bad: %v", addr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read should fail
|
|
||||||
recv := make([]byte, 4)
|
|
||||||
_, err = conn.Read(recv)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParse_ipv4_checkfunc(t *testing.T) {
|
|
||||||
checkAddr = goodAddr
|
|
||||||
testParse_ipv4_checkfunc(t)
|
|
||||||
checkAddr = badAddr
|
|
||||||
testParse_ipv4_checkfunc(t)
|
|
||||||
checkAddr = errAddr
|
|
||||||
testParse_ipv4_checkfunc(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testParse_ipv4_checkfunc(t *testing.T) {
|
|
||||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
checkFunc := func(addr net.Addr) (bool, error) {
|
|
||||||
tcpAddr := addr.(*net.TCPAddr)
|
|
||||||
if tcpAddr.IP.String() == checkAddr {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
pl := &Listener{Listener: l, SourceCheck: checkFunc}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
conn, err := net.Dial("tcp", pl.Addr().String())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// Write out the header!
|
|
||||||
header := "PROXY TCP4 10.1.1.1 20.2.2.2 1000 2000\r\n"
|
|
||||||
conn.Write([]byte(header))
|
|
||||||
|
|
||||||
conn.Write([]byte("ping"))
|
|
||||||
recv := make([]byte, 4)
|
|
||||||
_, err = conn.Read(recv)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(recv, []byte("pong")) {
|
|
||||||
t.Fatalf("bad: %v", recv)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
conn, err := pl.Accept()
|
|
||||||
if err != nil {
|
|
||||||
if checkAddr == badAddr {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
recv := make([]byte, 4)
|
|
||||||
_, err = conn.Read(recv)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(recv, []byte("ping")) {
|
|
||||||
t.Fatalf("bad: %v", recv)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := conn.Write([]byte("pong")); err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check the remote addr
|
|
||||||
addr := conn.RemoteAddr().(*net.TCPAddr)
|
|
||||||
switch checkAddr {
|
|
||||||
case goodAddr:
|
|
||||||
if addr.IP.String() != "10.1.1.1" {
|
|
||||||
t.Fatalf("bad: %v", addr)
|
|
||||||
}
|
|
||||||
if addr.Port != 1000 {
|
|
||||||
t.Fatalf("bad: %v", addr)
|
|
||||||
}
|
|
||||||
case badAddr:
|
|
||||||
if addr.IP.String() != "127.0.0.1" {
|
|
||||||
t.Fatalf("bad: %v", addr)
|
|
||||||
}
|
|
||||||
if addr.Port == 1000 {
|
|
||||||
t.Fatalf("bad: %v", addr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type testConn struct {
|
|
||||||
readFromCalledWith io.Reader
|
|
||||||
net.Conn // nil; crash on any unexpected use
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *testConn) ReadFrom(r io.Reader) (int64, error) {
|
|
||||||
c.readFromCalledWith = r
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
func (c *testConn) Write(p []byte) (int, error) {
|
|
||||||
return len(p), nil
|
|
||||||
}
|
|
||||||
func (c *testConn) Read(p []byte) (int, error) {
|
|
||||||
return 1, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCopyToWrappedConnection(t *testing.T) {
|
|
||||||
innerConn := &testConn{}
|
|
||||||
wrappedConn := NewConn(innerConn, 0)
|
|
||||||
dummySrc := &testConn{}
|
|
||||||
|
|
||||||
io.Copy(wrappedConn, dummySrc)
|
|
||||||
if innerConn.readFromCalledWith != dummySrc {
|
|
||||||
t.Error("Expected io.Copy to delegate to ReadFrom function of inner destination connection")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCopyFromWrappedConnection(t *testing.T) {
|
|
||||||
wrappedConn := NewConn(&testConn{}, 0)
|
|
||||||
dummyDst := &testConn{}
|
|
||||||
|
|
||||||
io.Copy(dummyDst, wrappedConn)
|
|
||||||
if dummyDst.readFromCalledWith != wrappedConn.conn {
|
|
||||||
t.Errorf("Expected io.Copy to pass inner source connection to ReadFrom method of destination")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCopyFromWrappedConnectionToWrappedConnection(t *testing.T) {
|
|
||||||
innerConn1 := &testConn{}
|
|
||||||
wrappedConn1 := NewConn(innerConn1, 0)
|
|
||||||
innerConn2 := &testConn{}
|
|
||||||
wrappedConn2 := NewConn(innerConn2, 0)
|
|
||||||
|
|
||||||
io.Copy(wrappedConn1, wrappedConn2)
|
|
||||||
if innerConn1.readFromCalledWith != innerConn2 {
|
|
||||||
t.Errorf("Expected io.Copy to pass inner source connection to ReadFrom of inner destination connection")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
5
server/.codecov.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
ignore:
|
||||||
|
- "screenshot"
|
||||||
|
- "web"
|
||||||
|
- "server/conf"
|
||||||
|
- "server/files"
|
21
server/.gitignore
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
|
|
||||||
|
# Dependency directories (remove the comment below to include it)
|
||||||
|
vendor/
|
||||||
|
|
||||||
|
ui/
|
||||||
|
.idea/
|
||||||
|
anylink
|
||||||
|
data.db
|
||||||
|
conf/*.db
|
@@ -10,26 +10,26 @@ import (
|
|||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 登陆接口
|
// Login 登陆接口
|
||||||
func Login(w http.ResponseWriter, r *http.Request) {
|
func Login(w http.ResponseWriter, r *http.Request) {
|
||||||
// TODO 调试信息输出
|
// TODO 调试信息输出
|
||||||
// hd, _ := httputil.DumpRequest(r, true)
|
// hd, _ := httputil.DumpRequest(r, true)
|
||||||
// fmt.Println("DumpRequest: ", string(hd))
|
// fmt.Println("DumpRequest: ", string(hd))
|
||||||
|
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
admin_user := r.PostFormValue("admin_user")
|
adminUser := r.PostFormValue("admin_user")
|
||||||
admin_pass := r.PostFormValue("admin_pass")
|
adminPass := r.PostFormValue("admin_pass")
|
||||||
|
|
||||||
// 认证错误
|
// 认证错误
|
||||||
if !(admin_user == base.Cfg.AdminUser &&
|
if !(adminUser == base.Cfg.AdminUser &&
|
||||||
utils.PasswordVerify(admin_pass, base.Cfg.AdminPass)) {
|
utils.PasswordVerify(adminPass, base.Cfg.AdminPass)) {
|
||||||
RespError(w, RespUserOrPassErr)
|
RespError(w, RespUserOrPassErr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// token有效期
|
// token有效期
|
||||||
expiresAt := time.Now().Unix() + 3600*3
|
expiresAt := time.Now().Unix() + 3600*3
|
||||||
jwtData := map[string]interface{}{"admin_user": admin_user}
|
jwtData := map[string]interface{}{"admin_user": adminUser}
|
||||||
tokenString, err := SetJwtData(jwtData, expiresAt)
|
tokenString, err := SetJwtData(jwtData, expiresAt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
RespError(w, 1, err)
|
RespError(w, 1, err)
|
||||||
@@ -38,7 +38,7 @@ func Login(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
data := make(map[string]interface{})
|
data := make(map[string]interface{})
|
||||||
data["token"] = tokenString
|
data["token"] = tokenString
|
||||||
data["admin_user"] = admin_user
|
data["admin_user"] = adminUser
|
||||||
data["expires_at"] = expiresAt
|
data["expires_at"] = expiresAt
|
||||||
|
|
||||||
RespSucess(w, data)
|
RespSucess(w, data)
|
||||||
@@ -56,7 +56,7 @@ func authMiddleware(next http.Handler) http.Handler {
|
|||||||
route := mux.CurrentRoute(r)
|
route := mux.CurrentRoute(r)
|
||||||
name := route.GetName()
|
name := route.GetName()
|
||||||
// fmt.Println("bb", r.URL.Path, name)
|
// fmt.Println("bb", r.URL.Path, name)
|
||||||
if utils.InArrStr([]string{"login", "index", "static"}, name) {
|
if utils.InArrStr([]string{"login", "index", "static", "debug"}, name) {
|
||||||
// 不进行鉴权
|
// 不进行鉴权
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
return
|
return
|
@@ -2,7 +2,7 @@ package admin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func GroupList(w http.ResponseWriter, r *http.Request) {
|
func GroupList(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
pageS := r.FormValue("page")
|
pageS := r.FormValue("page")
|
||||||
page, _ := strconv.Atoi(pageS)
|
page, _ := strconv.Atoi(pageS)
|
||||||
if page < 1 {
|
if page < 1 {
|
||||||
@@ -22,7 +22,7 @@ func GroupList(w http.ResponseWriter, r *http.Request) {
|
|||||||
count := dbdata.CountAll(&dbdata.Group{})
|
count := dbdata.CountAll(&dbdata.Group{})
|
||||||
|
|
||||||
var datas []dbdata.Group
|
var datas []dbdata.Group
|
||||||
err := dbdata.All(&datas, pageSize, page)
|
err := dbdata.Find(&datas, pageSize, page)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
RespError(w, RespInternalErr, err)
|
RespError(w, RespInternalErr, err)
|
||||||
return
|
return
|
||||||
@@ -47,8 +47,18 @@ func GroupNames(w http.ResponseWriter, r *http.Request) {
|
|||||||
RespSucess(w, data)
|
RespSucess(w, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GroupNamesIds(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var names = dbdata.GetGroupNamesIds()
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"count": len(names),
|
||||||
|
"page_size": 0,
|
||||||
|
"datas": names,
|
||||||
|
}
|
||||||
|
RespSucess(w, data)
|
||||||
|
}
|
||||||
|
|
||||||
func GroupDetail(w http.ResponseWriter, r *http.Request) {
|
func GroupDetail(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
idS := r.FormValue("id")
|
idS := r.FormValue("id")
|
||||||
id, _ := strconv.Atoi(idS)
|
id, _ := strconv.Atoi(idS)
|
||||||
if id < 1 {
|
if id < 1 {
|
||||||
@@ -62,12 +72,14 @@ func GroupDetail(w http.ResponseWriter, r *http.Request) {
|
|||||||
RespError(w, RespInternalErr, err)
|
RespError(w, RespInternalErr, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if len(data.Auth) == 0 {
|
||||||
|
data.Auth["type"] = "local"
|
||||||
|
}
|
||||||
RespSucess(w, data)
|
RespSucess(w, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GroupSet(w http.ResponseWriter, r *http.Request) {
|
func GroupSet(w http.ResponseWriter, r *http.Request) {
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
RespError(w, RespInternalErr, err)
|
RespError(w, RespInternalErr, err)
|
||||||
return
|
return
|
||||||
@@ -90,7 +102,7 @@ func GroupSet(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GroupDel(w http.ResponseWriter, r *http.Request) {
|
func GroupDel(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
idS := r.FormValue("id")
|
idS := r.FormValue("id")
|
||||||
id, _ := strconv.Atoi(idS)
|
id, _ := strconv.Atoi(idS)
|
||||||
if id < 1 {
|
if id < 1 {
|
@@ -2,16 +2,15 @@ package admin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/bjdgyc/anylink/dbdata"
|
"github.com/bjdgyc/anylink/dbdata"
|
||||||
)
|
)
|
||||||
|
|
||||||
func UserIpMapList(w http.ResponseWriter, r *http.Request) {
|
func UserIpMapList(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
pageS := r.FormValue("page")
|
pageS := r.FormValue("page")
|
||||||
page, _ := strconv.Atoi(pageS)
|
page, _ := strconv.Atoi(pageS)
|
||||||
if page < 1 {
|
if page < 1 {
|
||||||
@@ -23,7 +22,7 @@ func UserIpMapList(w http.ResponseWriter, r *http.Request) {
|
|||||||
count := dbdata.CountAll(&dbdata.IpMap{})
|
count := dbdata.CountAll(&dbdata.IpMap{})
|
||||||
|
|
||||||
var datas []dbdata.IpMap
|
var datas []dbdata.IpMap
|
||||||
err := dbdata.All(&datas, pageSize, page)
|
err := dbdata.Find(&datas, pageSize, page)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
RespError(w, RespInternalErr, err)
|
RespError(w, RespInternalErr, err)
|
||||||
return
|
return
|
||||||
@@ -39,7 +38,7 @@ func UserIpMapList(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UserIpMapDetail(w http.ResponseWriter, r *http.Request) {
|
func UserIpMapDetail(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
idS := r.FormValue("id")
|
idS := r.FormValue("id")
|
||||||
id, _ := strconv.Atoi(idS)
|
id, _ := strconv.Atoi(idS)
|
||||||
if id < 1 {
|
if id < 1 {
|
||||||
@@ -58,9 +57,9 @@ func UserIpMapDetail(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UserIpMapSet(w http.ResponseWriter, r *http.Request) {
|
func UserIpMapSet(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
RespError(w, RespInternalErr, err)
|
RespError(w, RespInternalErr, err)
|
||||||
return
|
return
|
||||||
@@ -75,14 +74,7 @@ func UserIpMapSet(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// fmt.Println(v, len(v.Ip), len(v.MacAddr))
|
// fmt.Println(v, len(v.Ip), len(v.MacAddr))
|
||||||
|
|
||||||
if len(v.IpAddr) < 4 || len(v.MacAddr) < 6 {
|
err = dbdata.SetIpMap(v)
|
||||||
RespError(w, RespParamErr, "IP或MAC错误")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
v.UpdatedAt = time.Now()
|
|
||||||
err = dbdata.Save(v)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
RespError(w, RespInternalErr, err)
|
RespError(w, RespInternalErr, err)
|
||||||
return
|
return
|
||||||
@@ -92,7 +84,7 @@ func UserIpMapSet(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UserIpMapDel(w http.ResponseWriter, r *http.Request) {
|
func UserIpMapDel(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
idS := r.FormValue("id")
|
idS := r.FormValue("id")
|
||||||
id, _ := strconv.Atoi(idS)
|
id, _ := strconv.Atoi(idS)
|
||||||
|
|
118
server/admin/api_other.go
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/bjdgyc/anylink/base"
|
||||||
|
"github.com/bjdgyc/anylink/dbdata"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setOtherGet(data interface{}, w http.ResponseWriter) {
|
||||||
|
err := dbdata.SettingGet(data)
|
||||||
|
if err != nil && !dbdata.CheckErrNotFound(err) {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 不明文输出SMTP的密码
|
||||||
|
switch dbdata.StructName(data) {
|
||||||
|
case "SettingSmtp":
|
||||||
|
data.(*dbdata.SettingSmtp).Password = ""
|
||||||
|
}
|
||||||
|
RespSucess(w, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setOtherEdit(data interface{}, w http.ResponseWriter, r *http.Request) {
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
err = json.Unmarshal(body, data)
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// fmt.Println(data)
|
||||||
|
switch dbdata.StructName(data) {
|
||||||
|
case "SettingSmtp":
|
||||||
|
// 密码为空时则不修改
|
||||||
|
smtp := &dbdata.SettingSmtp{}
|
||||||
|
err := dbdata.SettingGet(smtp)
|
||||||
|
if err == nil && data.(*dbdata.SettingSmtp).Password == "" {
|
||||||
|
data.(*dbdata.SettingSmtp).Password = smtp.Password
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = dbdata.SettingSet(data)
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
RespSucess(w, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetOtherSmtp(w http.ResponseWriter, r *http.Request) {
|
||||||
|
data := &dbdata.SettingSmtp{}
|
||||||
|
setOtherGet(data, w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetOtherSmtpEdit(w http.ResponseWriter, r *http.Request) {
|
||||||
|
data := &dbdata.SettingSmtp{}
|
||||||
|
setOtherEdit(data, w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetOther(w http.ResponseWriter, r *http.Request) {
|
||||||
|
data := &dbdata.SettingOther{}
|
||||||
|
setOtherGet(data, w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetOtherEdit(w http.ResponseWriter, r *http.Request) {
|
||||||
|
data := &dbdata.SettingOther{}
|
||||||
|
setOtherEdit(data, w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetOtherAuditLog(w http.ResponseWriter, r *http.Request) {
|
||||||
|
data, err := dbdata.SettingGetAuditLog()
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data.AuditInterval = base.Cfg.AuditInterval
|
||||||
|
RespSucess(w, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetOtherAuditLogEdit(w http.ResponseWriter, r *http.Request) {
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer r.Body.Close()
|
||||||
|
data := &dbdata.SettingAuditLog{}
|
||||||
|
err = json.Unmarshal(body, data)
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if data.LifeDay < 0 || data.LifeDay > 365 {
|
||||||
|
RespError(w, RespParamErr, errors.New("日志存储时长范围在 0 ~ 365"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ok, _ := regexp.Match("^([0-9]|0[0-9]|1[0-9]|2[0-3]):([0][0])$", []byte(data.ClearTime))
|
||||||
|
if !ok {
|
||||||
|
RespError(w, RespParamErr, errors.New("每天清理时间填写有误"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = dbdata.SettingSet(data)
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
RespSucess(w, data)
|
||||||
|
}
|
98
server/admin/api_policy.go
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/bjdgyc/anylink/dbdata"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PolicyList(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_ = r.ParseForm()
|
||||||
|
pageS := r.FormValue("page")
|
||||||
|
page, _ := strconv.Atoi(pageS)
|
||||||
|
if page < 1 {
|
||||||
|
page = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
var pageSize = dbdata.PageSize
|
||||||
|
|
||||||
|
count := dbdata.CountAll(&dbdata.Policy{})
|
||||||
|
|
||||||
|
var datas []dbdata.Policy
|
||||||
|
err := dbdata.Find(&datas, pageSize, page)
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"count": count,
|
||||||
|
"page_size": pageSize,
|
||||||
|
"datas": datas,
|
||||||
|
}
|
||||||
|
|
||||||
|
RespSucess(w, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func PolicyDetail(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_ = r.ParseForm()
|
||||||
|
idS := r.FormValue("id")
|
||||||
|
id, _ := strconv.Atoi(idS)
|
||||||
|
if id < 1 {
|
||||||
|
RespError(w, RespParamErr, "Id错误")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var data dbdata.Policy
|
||||||
|
err := dbdata.One("Id", id, &data)
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
RespSucess(w, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func PolicySet(w http.ResponseWriter, r *http.Request) {
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer r.Body.Close()
|
||||||
|
v := &dbdata.Policy{}
|
||||||
|
err = json.Unmarshal(body, v)
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = dbdata.SetPolicy(v)
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
RespSucess(w, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func PolicyDel(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_ = r.ParseForm()
|
||||||
|
idS := r.FormValue("id")
|
||||||
|
id, _ := strconv.Atoi(idS)
|
||||||
|
if id < 1 {
|
||||||
|
RespError(w, RespParamErr, "Id错误")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := dbdata.Policy{Id: id}
|
||||||
|
err := dbdata.Del(&data)
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
RespSucess(w, nil)
|
||||||
|
}
|
@@ -1,16 +1,14 @@
|
|||||||
package admin
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/bjdgyc/anylink/dbdata"
|
|
||||||
"github.com/bjdgyc/anylink/sessdata"
|
|
||||||
|
|
||||||
"github.com/bjdgyc/anylink/base"
|
"github.com/bjdgyc/anylink/base"
|
||||||
|
"github.com/bjdgyc/anylink/dbdata"
|
||||||
"github.com/bjdgyc/anylink/pkg/utils"
|
"github.com/bjdgyc/anylink/pkg/utils"
|
||||||
|
"github.com/bjdgyc/anylink/sessdata"
|
||||||
"github.com/shirou/gopsutil/cpu"
|
"github.com/shirou/gopsutil/cpu"
|
||||||
"github.com/shirou/gopsutil/disk"
|
"github.com/shirou/gopsutil/disk"
|
||||||
"github.com/shirou/gopsutil/host"
|
"github.com/shirou/gopsutil/host"
|
||||||
@@ -72,6 +70,8 @@ func SetSystem(w http.ResponseWriter, r *http.Request) {
|
|||||||
"goArch": runtime.GOARCH,
|
"goArch": runtime.GOARCH,
|
||||||
"goVersion": runtime.Version(),
|
"goVersion": runtime.Version(),
|
||||||
"goroutine": runtime.NumGoroutine(),
|
"goroutine": runtime.NumGoroutine(),
|
||||||
|
"appVersion": "v" + base.APP_VER,
|
||||||
|
"appCommitId": base.CommitId,
|
||||||
|
|
||||||
"hostname": hi.Hostname,
|
"hostname": hi.Hostname,
|
||||||
"platform": fmt.Sprintf("%v %v %v", hi.Platform, hi.PlatformFamily, hi.PlatformVersion),
|
"platform": fmt.Sprintf("%v %v %v", hi.Platform, hi.PlatformFamily, hi.PlatformVersion),
|
||||||
@@ -84,9 +84,8 @@ func SetSystem(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SetSoft(w http.ResponseWriter, r *http.Request) {
|
func SetSoft(w http.ResponseWriter, r *http.Request) {
|
||||||
datas := base.ServerCfg2Slice()
|
data := base.ServerCfg2Slice()
|
||||||
b, _ := json.Marshal(datas)
|
RespSucess(w, data)
|
||||||
w.Write(b)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func decimal(f float64) float64 {
|
func decimal(f float64) float64 {
|
79
server/admin/api_set_audit.go
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/bjdgyc/anylink/dbdata"
|
||||||
|
"github.com/gocarina/gocsv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SetAuditList(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_ = r.ParseForm()
|
||||||
|
pageS := r.FormValue("page")
|
||||||
|
page, _ := strconv.Atoi(pageS)
|
||||||
|
if page < 1 {
|
||||||
|
page = 1
|
||||||
|
}
|
||||||
|
var datas []dbdata.AccessAudit
|
||||||
|
session := dbdata.GetAuditSession(r.FormValue("search"))
|
||||||
|
count, err := dbdata.FindAndCount(session, &datas, dbdata.PageSize, page)
|
||||||
|
if err != nil && !dbdata.CheckErrNotFound(err) {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"count": count,
|
||||||
|
"page_size": dbdata.PageSize,
|
||||||
|
"datas": datas,
|
||||||
|
}
|
||||||
|
|
||||||
|
RespSucess(w, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetAuditExport(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var datas []dbdata.AccessAudit
|
||||||
|
maxNum := 1000000
|
||||||
|
session := dbdata.GetAuditSession(r.FormValue("search"))
|
||||||
|
count, err := dbdata.FindAndCount(session, &datas, maxNum, 0)
|
||||||
|
if err != nil && !dbdata.CheckErrNotFound(err) {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if count == 0 {
|
||||||
|
RespError(w, RespParamErr, "你导出的总数量为0条,请调整搜索条件,再导出")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if count > int64(maxNum) {
|
||||||
|
RespError(w, RespParamErr, "你导出的数据量超过100万条,请调整搜索条件,再导出")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
gocsv.Marshal(datas, w)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func UserActLogList(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_ = r.ParseForm()
|
||||||
|
pageS := r.FormValue("page")
|
||||||
|
page, _ := strconv.Atoi(pageS)
|
||||||
|
if page < 1 {
|
||||||
|
page = 1
|
||||||
|
}
|
||||||
|
var datas []dbdata.UserActLog
|
||||||
|
session := dbdata.UserActLogIns.GetSession(r.Form)
|
||||||
|
count, err := dbdata.FindAndCount(session, &datas, dbdata.PageSize, page)
|
||||||
|
if err != nil && !dbdata.CheckErrNotFound(err) {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"count": count,
|
||||||
|
"page_size": dbdata.PageSize,
|
||||||
|
"datas": datas,
|
||||||
|
"statusOps": dbdata.UserActLogIns.GetStatusOpsWithTag(),
|
||||||
|
"osOps": dbdata.UserActLogIns.OsOps,
|
||||||
|
"clientOps": dbdata.UserActLogIns.ClientOps,
|
||||||
|
}
|
||||||
|
|
||||||
|
RespSucess(w, data)
|
||||||
|
}
|
33
server/admin/api_statsinfo.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/bjdgyc/anylink/dbdata"
|
||||||
|
)
|
||||||
|
|
||||||
|
func StatsInfoList(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var ok bool
|
||||||
|
_ = r.ParseForm()
|
||||||
|
action := r.FormValue("action")
|
||||||
|
scope := r.FormValue("scope")
|
||||||
|
ok = dbdata.StatsInfoIns.ValidAction(action)
|
||||||
|
if !ok {
|
||||||
|
RespError(w, RespParamErr, errors.New("不存在的图表类别"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ok = dbdata.StatsInfoIns.ValidScope(scope)
|
||||||
|
if !ok {
|
||||||
|
RespError(w, RespParamErr, errors.New("不存在的日期范围"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
datas, err := dbdata.StatsInfoIns.GetData(action, scope)
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := make(map[string]interface{})
|
||||||
|
data["datas"] = datas
|
||||||
|
RespSucess(w, data)
|
||||||
|
}
|
114
server/admin/api_uploaduser.go
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/bjdgyc/anylink/base"
|
||||||
|
"github.com/bjdgyc/anylink/dbdata"
|
||||||
|
"github.com/bjdgyc/anylink/pkg/utils"
|
||||||
|
mapset "github.com/deckarep/golang-set"
|
||||||
|
"github.com/spf13/cast"
|
||||||
|
"github.com/xuri/excelize/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func UserUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
|
r.ParseMultipartForm(8 << 20)
|
||||||
|
file, header, err := r.FormFile("file")
|
||||||
|
if err != nil || !strings.Contains(header.Filename, ".xlsx") || !strings.Contains(header.Filename, ".xls") {
|
||||||
|
RespError(w, RespInternalErr, "文件解析失败:仅支持xlsx或xls文件")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
newFile, err := os.Create(base.Cfg.FilesPath + header.Filename)
|
||||||
|
if err != nil {
|
||||||
|
RespError(w, RespInternalErr, "创建文件失败:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer newFile.Close()
|
||||||
|
io.Copy(newFile, file)
|
||||||
|
if err = UploadUser(newFile.Name()); err != nil {
|
||||||
|
RespError(w, RespInternalErr, err)
|
||||||
|
os.Remove(base.Cfg.FilesPath + header.Filename)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
os.Remove(base.Cfg.FilesPath + header.Filename)
|
||||||
|
RespSucess(w, "批量添加成功")
|
||||||
|
}
|
||||||
|
func UploadUser(file string) error {
|
||||||
|
f, err := excelize.OpenFile(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := f.Close(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
rows, err := f.GetRows("Sheet1")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if rows[0][0] != "id" || rows[0][1] != "username" || rows[0][2] != "nickname" || rows[0][3] != "email" || rows[0][4] != "pin_code" || rows[0][5] != "limittime" || rows[0][6] != "otp_secret" || rows[0][7] != "disable_otp" || rows[0][8] != "groups" || rows[0][9] != "status" || rows[0][10] != "send_email" {
|
||||||
|
return fmt.Errorf("批量添加失败,表格格式不正确")
|
||||||
|
}
|
||||||
|
var k []interface{}
|
||||||
|
for _, v := range dbdata.GetGroupNames() {
|
||||||
|
k = append(k, v)
|
||||||
|
}
|
||||||
|
for index, row := range rows {
|
||||||
|
if index == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
id, _ := strconv.Atoi(row[0])
|
||||||
|
if len(row[4]) < 6 {
|
||||||
|
row[4] = utils.RandomRunes(8)
|
||||||
|
}
|
||||||
|
limittime, _ := time.ParseInLocation("2006-01-02 15:04:05", row[5], time.Local)
|
||||||
|
disableOtp, _ := strconv.ParseBool(row[7])
|
||||||
|
var group []string
|
||||||
|
if row[8] == "" {
|
||||||
|
return fmt.Errorf("第%d行数据错误,用户组不允许为空", index)
|
||||||
|
}
|
||||||
|
for _, v := range strings.Split(row[8], ",") {
|
||||||
|
if s := mapset.NewSetFromSlice(k); s.Contains(v) {
|
||||||
|
group = append(group, v)
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("用户组【%s】不存在,请检查第%d行数据", v, index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
status := cast.ToInt8(row[9])
|
||||||
|
sendmail, _ := strconv.ParseBool(row[10])
|
||||||
|
// createdAt, _ := time.ParseInLocation("2006-01-02 15:04:05", row[11], time.Local)
|
||||||
|
// updatedAt, _ := time.ParseInLocation("2006-01-02 15:04:05", row[12], time.Local)
|
||||||
|
user := &dbdata.User{
|
||||||
|
Id: id,
|
||||||
|
Username: row[1],
|
||||||
|
Nickname: row[2],
|
||||||
|
Email: row[3],
|
||||||
|
PinCode: row[4],
|
||||||
|
LimitTime: &limittime,
|
||||||
|
OtpSecret: row[6],
|
||||||
|
DisableOtp: disableOtp,
|
||||||
|
Groups: group,
|
||||||
|
Status: status,
|
||||||
|
SendEmail: sendmail,
|
||||||
|
// CreatedAt: createdAt,
|
||||||
|
// UpdatedAt: updatedAt,
|
||||||
|
}
|
||||||
|
if err := dbdata.AddBatch(user); err != nil {
|
||||||
|
return fmt.Errorf("请检查第%d行数据是否导入有重复用户", index)
|
||||||
|
}
|
||||||
|
if user.SendEmail {
|
||||||
|
if err := userAccountMail(user); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
@@ -5,8 +5,9 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
@@ -19,7 +20,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func UserList(w http.ResponseWriter, r *http.Request) {
|
func UserList(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
prefix := r.FormValue("prefix")
|
prefix := r.FormValue("prefix")
|
||||||
pageS := r.FormValue("page")
|
pageS := r.FormValue("page")
|
||||||
page, _ := strconv.Atoi(pageS)
|
page, _ := strconv.Atoi(pageS)
|
||||||
@@ -36,11 +37,11 @@ func UserList(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// 查询前缀匹配
|
// 查询前缀匹配
|
||||||
if len(prefix) > 0 {
|
if len(prefix) > 0 {
|
||||||
count = pageSize
|
count = dbdata.CountPrefix("username", prefix, &dbdata.User{})
|
||||||
err = dbdata.Prefix("Username", prefix, &datas, pageSize, 1)
|
err = dbdata.Prefix("username", prefix, &datas, pageSize, 1)
|
||||||
} else {
|
} else {
|
||||||
count = dbdata.CountAll(&dbdata.User{})
|
count = dbdata.CountAll(&dbdata.User{})
|
||||||
err = dbdata.All(&datas, pageSize, page)
|
err = dbdata.Find(&datas, pageSize, page)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil && !dbdata.CheckErrNotFound(err) {
|
if err != nil && !dbdata.CheckErrNotFound(err) {
|
||||||
@@ -58,7 +59,7 @@ func UserList(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UserDetail(w http.ResponseWriter, r *http.Request) {
|
func UserDetail(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
idS := r.FormValue("id")
|
idS := r.FormValue("id")
|
||||||
id, _ := strconv.Atoi(idS)
|
id, _ := strconv.Atoi(idS)
|
||||||
if id < 1 {
|
if id < 1 {
|
||||||
@@ -77,9 +78,9 @@ func UserDetail(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UserSet(w http.ResponseWriter, r *http.Request) {
|
func UserSet(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
RespError(w, RespInternalErr, err)
|
RespError(w, RespInternalErr, err)
|
||||||
return
|
return
|
||||||
@@ -106,12 +107,13 @@ func UserSet(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//修改用户资料后执行过期用户检测
|
||||||
|
sessdata.CloseUserLimittimeSession()
|
||||||
RespSucess(w, nil)
|
RespSucess(w, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func UserDel(w http.ResponseWriter, r *http.Request) {
|
func UserDel(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
idS := r.FormValue("id")
|
idS := r.FormValue("id")
|
||||||
id, _ := strconv.Atoi(idS)
|
id, _ := strconv.Atoi(idS)
|
||||||
|
|
||||||
@@ -130,29 +132,45 @@ func UserDel(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UserOtpQr(w http.ResponseWriter, r *http.Request) {
|
func UserOtpQr(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
b64 := r.FormValue("b64")
|
b64S := r.FormValue("b64")
|
||||||
idS := r.FormValue("id")
|
idS := r.FormValue("id")
|
||||||
id, _ := strconv.Atoi(idS)
|
id, _ := strconv.Atoi(idS)
|
||||||
var user dbdata.User
|
|
||||||
err := dbdata.One("Id", id, &user)
|
var b64 bool
|
||||||
|
if b64S == "1" {
|
||||||
|
b64 = true
|
||||||
|
}
|
||||||
|
data, err := userOtpQr(id, b64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
RespError(w, RespInternalErr, err)
|
base.Error(err)
|
||||||
return
|
}
|
||||||
|
io.WriteString(w, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func userOtpQr(uid int, b64 bool) (string, error) {
|
||||||
|
var user dbdata.User
|
||||||
|
err := dbdata.One("Id", uid, &user)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
issuer := base.Cfg.Issuer
|
issuer := url.QueryEscape(base.Cfg.Issuer)
|
||||||
qrstr := fmt.Sprintf("otpauth://totp/%s:%s?issuer=%s&secret=%s", issuer, user.Email, issuer, user.OtpSecret)
|
qrstr := fmt.Sprintf("otpauth://totp/%s:%s?issuer=%s&secret=%s", issuer, user.Email, issuer, user.OtpSecret)
|
||||||
qr, _ := qrcode.New(qrstr, qrcode.High)
|
qr, _ := qrcode.New(qrstr, qrcode.High)
|
||||||
|
|
||||||
if b64 == "1" {
|
if b64 {
|
||||||
data, _ := qr.PNG(300)
|
data, err := qr.PNG(300)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
s := base64.StdEncoding.EncodeToString(data)
|
s := base64.StdEncoding.EncodeToString(data)
|
||||||
fmt.Fprint(w, s)
|
return s, nil
|
||||||
} else {
|
|
||||||
qr.Write(300, w)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
err = qr.Write(300, buf)
|
||||||
|
return buf.String(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 在线用户
|
// 在线用户
|
||||||
@@ -169,14 +187,14 @@ func UserOnline(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UserOffline(w http.ResponseWriter, r *http.Request) {
|
func UserOffline(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
token := r.FormValue("token")
|
token := r.FormValue("token")
|
||||||
sessdata.CloseSess(token)
|
sessdata.CloseSess(token, dbdata.UserLogoutAdmin)
|
||||||
RespSucess(w, nil)
|
RespSucess(w, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func UserReline(w http.ResponseWriter, r *http.Request) {
|
func UserReline(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
token := r.FormValue("token")
|
token := r.FormValue("token")
|
||||||
sessdata.CloseCSess(token)
|
sessdata.CloseCSess(token)
|
||||||
RespSucess(w, nil)
|
RespSucess(w, nil)
|
||||||
@@ -189,6 +207,7 @@ type userAccountMailData struct {
|
|||||||
Username string
|
Username string
|
||||||
PinCode string
|
PinCode string
|
||||||
OtpImg string
|
OtpImg string
|
||||||
|
OtpImgBase64 string
|
||||||
}
|
}
|
||||||
|
|
||||||
func userAccountMail(user *dbdata.User) error {
|
func userAccountMail(user *dbdata.User) error {
|
||||||
@@ -222,16 +241,29 @@ func userAccountMail(user *dbdata.User) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setting := &dbdata.SettingOther{}
|
||||||
|
err = dbdata.SettingGet(setting)
|
||||||
|
if err != nil {
|
||||||
|
base.Error(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
otpData, _ := userOtpQr(user.Id, true)
|
||||||
|
|
||||||
data := userAccountMailData{
|
data := userAccountMailData{
|
||||||
LinkAddr: base.Cfg.LinkAddr,
|
LinkAddr: setting.LinkAddr,
|
||||||
Group: strings.Join(user.Groups, ","),
|
Group: strings.Join(user.Groups, ","),
|
||||||
Username: user.Username,
|
Username: user.Username,
|
||||||
PinCode: user.PinCode,
|
PinCode: user.PinCode,
|
||||||
OtpImg: fmt.Sprintf("https://%s/otp_qr?id=%d&jwt=%s", base.Cfg.LinkAddr, user.Id, tokenString),
|
OtpImg: fmt.Sprintf("https://%s/otp_qr?id=%d&jwt=%s", setting.LinkAddr, user.Id, tokenString),
|
||||||
|
OtpImgBase64: "data:image/png;base64," + otpData,
|
||||||
}
|
}
|
||||||
w := bytes.NewBufferString("")
|
w := bytes.NewBufferString("")
|
||||||
t, _ := template.New("auth_complete").Parse(htmlBody)
|
t, _ := template.New("auth_complete").Parse(htmlBody)
|
||||||
t.Execute(w, data)
|
err = t.Execute(w, data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
// fmt.Println(w.String())
|
// fmt.Println(w.String())
|
||||||
return SendMail(base.Cfg.Issuer+"平台通知", user.Email, w.String())
|
return SendMail(base.Cfg.Issuer+"平台通知", user.Email, w.String())
|
||||||
}
|
}
|
@@ -7,9 +7,9 @@ import (
|
|||||||
|
|
||||||
"github.com/bjdgyc/anylink/base"
|
"github.com/bjdgyc/anylink/base"
|
||||||
"github.com/bjdgyc/anylink/dbdata"
|
"github.com/bjdgyc/anylink/dbdata"
|
||||||
"github.com/dgrijalva/jwt-go"
|
"github.com/golang-jwt/jwt/v4"
|
||||||
"github.com/mojocn/base64Captcha"
|
|
||||||
mail "github.com/xhit/go-simple-mail/v2"
|
mail "github.com/xhit/go-simple-mail/v2"
|
||||||
|
// "github.com/mojocn/base64Captcha"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetJwtData(data map[string]interface{}, expiresAt int64) (string, error) {
|
func SetJwtData(data map[string]interface{}, expiresAt int64) (string, error) {
|
||||||
@@ -43,15 +43,6 @@ func GetJwtData(jwtToken string) (map[string]interface{}, error) {
|
|||||||
return claims, nil
|
return claims, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createCaptcha() {
|
|
||||||
var store = base64Captcha.DefaultMemStore
|
|
||||||
var driver base64Captcha.Driver
|
|
||||||
driverString := &base64Captcha.DriverString{}
|
|
||||||
driver = driverString.ConvertFonts()
|
|
||||||
c := base64Captcha.NewCaptcha(driver, store)
|
|
||||||
_ = c
|
|
||||||
}
|
|
||||||
|
|
||||||
func SendMail(subject, to, htmlBody string) error {
|
func SendMail(subject, to, htmlBody string) error {
|
||||||
|
|
||||||
dataSmtp := &dbdata.SettingSmtp{}
|
dataSmtp := &dbdata.SettingSmtp{}
|
||||||
@@ -68,8 +59,14 @@ func SendMail(subject, to, htmlBody string) error {
|
|||||||
server.Port = dataSmtp.Port
|
server.Port = dataSmtp.Port
|
||||||
server.Username = dataSmtp.Username
|
server.Username = dataSmtp.Username
|
||||||
server.Password = dataSmtp.Password
|
server.Password = dataSmtp.Password
|
||||||
if dataSmtp.UseSSl {
|
|
||||||
server.Encryption = mail.EncryptionSSL
|
switch dataSmtp.Encryption {
|
||||||
|
case "SSLTLS":
|
||||||
|
server.Encryption = mail.EncryptionSSLTLS
|
||||||
|
case "STARTTLS":
|
||||||
|
server.Encryption = mail.EncryptionSTARTTLS
|
||||||
|
default:
|
||||||
|
server.Encryption = mail.EncryptionNone
|
||||||
}
|
}
|
||||||
|
|
||||||
// Since v2.3.0 you can specified authentication type:
|
// Since v2.3.0 you can specified authentication type:
|
23
server/admin/common_test.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/bjdgyc/anylink/base"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestJwtData(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
base.Cfg.JwtSecret = "dsfasfdfsadfasdfasd3sdaf"
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"key": "value",
|
||||||
|
}
|
||||||
|
expiresAt := time.Now().Add(time.Minute).Unix()
|
||||||
|
token, err := SetJwtData(data, expiresAt)
|
||||||
|
assert.Nil(err)
|
||||||
|
dataN, err := GetJwtData(token)
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal(dataN["key"], "value")
|
||||||
|
}
|
@@ -43,8 +43,10 @@ func respHttp(w http.ResponseWriter, respCode int, data interface{}, errS ...int
|
|||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write(b)
|
_, err = w.Write(b)
|
||||||
|
if err != nil {
|
||||||
|
base.Error(err)
|
||||||
|
}
|
||||||
// 记录返回数据
|
// 记录返回数据
|
||||||
// logger.Category("response").Debug(string(b))
|
// logger.Category("response").Debug(string(b))
|
||||||
}
|
}
|
39
server/admin/resp_test.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRespSucess(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
RespSucess(w, "data")
|
||||||
|
// fmt.Println(w)
|
||||||
|
assert.Equal(w.Code, 200)
|
||||||
|
body, _ := io.ReadAll(w.Body)
|
||||||
|
res := Resp{}
|
||||||
|
err := json.Unmarshal(body, &res)
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal(res.Code, 0)
|
||||||
|
assert.Equal(res.Data, "data")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRespError(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
RespError(w, 10, "err-msg")
|
||||||
|
// fmt.Println(w)
|
||||||
|
assert.Equal(w.Code, 200)
|
||||||
|
body, _ := io.ReadAll(w.Body)
|
||||||
|
res := Resp{}
|
||||||
|
err := json.Unmarshal(body, &res)
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal(res.Code, 10)
|
||||||
|
assert.Equal(res.Msg, "err-msg")
|
||||||
|
}
|
120
server/admin/server.go
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
// admin:后台管理接口
|
||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"embed"
|
||||||
|
"net/http"
|
||||||
|
"net/http/pprof"
|
||||||
|
|
||||||
|
"github.com/arl/statsviz"
|
||||||
|
"github.com/bjdgyc/anylink/base"
|
||||||
|
"github.com/gorilla/handlers"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
var UiData embed.FS
|
||||||
|
|
||||||
|
// StartAdmin 开启服务
|
||||||
|
func StartAdmin() {
|
||||||
|
|
||||||
|
r := mux.NewRouter()
|
||||||
|
r.Use(authMiddleware)
|
||||||
|
r.Use(handlers.CompressHandler)
|
||||||
|
|
||||||
|
// 监控检测
|
||||||
|
r.HandleFunc("/status.html", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Write([]byte("ok"))
|
||||||
|
}).Name("index")
|
||||||
|
|
||||||
|
r.Handle("/", http.RedirectHandler("/ui/", http.StatusFound)).Name("index")
|
||||||
|
r.PathPrefix("/ui/").Handler(
|
||||||
|
// http.StripPrefix("/ui/", http.FileServer(http.Dir(base.Cfg.UiPath))),
|
||||||
|
http.FileServer(http.FS(UiData)),
|
||||||
|
).Name("static")
|
||||||
|
r.HandleFunc("/base/login", Login).Name("login")
|
||||||
|
|
||||||
|
r.HandleFunc("/set/home", SetHome)
|
||||||
|
r.HandleFunc("/set/system", SetSystem)
|
||||||
|
r.HandleFunc("/set/soft", SetSoft)
|
||||||
|
r.HandleFunc("/set/other", SetOther)
|
||||||
|
r.HandleFunc("/set/other/edit", SetOtherEdit)
|
||||||
|
r.HandleFunc("/set/other/smtp", SetOtherSmtp)
|
||||||
|
r.HandleFunc("/set/other/smtp/edit", SetOtherSmtpEdit)
|
||||||
|
r.HandleFunc("/set/other/audit_log", SetOtherAuditLog)
|
||||||
|
r.HandleFunc("/set/other/audit_log/edit", SetOtherAuditLogEdit)
|
||||||
|
r.HandleFunc("/set/audit/list", SetAuditList)
|
||||||
|
r.HandleFunc("/set/audit/export", SetAuditExport)
|
||||||
|
r.HandleFunc("/set/audit/act_log_list", UserActLogList)
|
||||||
|
|
||||||
|
r.HandleFunc("/user/list", UserList)
|
||||||
|
r.HandleFunc("/user/detail", UserDetail)
|
||||||
|
r.HandleFunc("/user/set", UserSet)
|
||||||
|
r.HandleFunc("/user/uploaduser", UserUpload).Methods(http.MethodPost)
|
||||||
|
r.HandleFunc("/user/del", UserDel)
|
||||||
|
r.HandleFunc("/user/online", UserOnline)
|
||||||
|
r.HandleFunc("/user/offline", UserOffline)
|
||||||
|
r.HandleFunc("/user/reline", UserReline)
|
||||||
|
r.HandleFunc("/user/otp_qr", UserOtpQr)
|
||||||
|
r.HandleFunc("/user/ip_map/list", UserIpMapList)
|
||||||
|
r.HandleFunc("/user/ip_map/detail", UserIpMapDetail)
|
||||||
|
r.HandleFunc("/user/ip_map/set", UserIpMapSet)
|
||||||
|
r.HandleFunc("/user/ip_map/del", UserIpMapDel)
|
||||||
|
r.HandleFunc("/user/policy/list", PolicyList)
|
||||||
|
r.HandleFunc("/user/policy/detail", PolicyDetail)
|
||||||
|
r.HandleFunc("/user/policy/set", PolicySet)
|
||||||
|
r.HandleFunc("/user/policy/del", PolicyDel)
|
||||||
|
|
||||||
|
r.HandleFunc("/group/list", GroupList)
|
||||||
|
r.HandleFunc("/group/names", GroupNames)
|
||||||
|
r.HandleFunc("/group/names_ids", GroupNamesIds)
|
||||||
|
r.HandleFunc("/group/detail", GroupDetail)
|
||||||
|
r.HandleFunc("/group/set", GroupSet)
|
||||||
|
r.HandleFunc("/group/del", GroupDel)
|
||||||
|
|
||||||
|
r.HandleFunc("/statsinfo/list", StatsInfoList)
|
||||||
|
|
||||||
|
// pprof
|
||||||
|
if base.Cfg.Pprof {
|
||||||
|
r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline).Name("debug")
|
||||||
|
r.HandleFunc("/debug/pprof/profile", pprof.Profile).Name("debug")
|
||||||
|
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol).Name("debug")
|
||||||
|
r.HandleFunc("/debug/pprof/trace", pprof.Trace).Name("debug")
|
||||||
|
r.HandleFunc("/debug/pprof", location("/debug/pprof/")).Name("debug")
|
||||||
|
r.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index).Name("debug")
|
||||||
|
// statsviz
|
||||||
|
r.Path("/debug/statsviz/ws").Name("debug").HandlerFunc(statsviz.Ws)
|
||||||
|
r.PathPrefix("/debug/statsviz/").Name("debug").Handler(statsviz.Index)
|
||||||
|
}
|
||||||
|
|
||||||
|
base.Info("Listen admin", base.Cfg.AdminAddr)
|
||||||
|
|
||||||
|
// 修复 CVE-2016-2183
|
||||||
|
cipherSuites := tls.CipherSuites()
|
||||||
|
selectedCipherSuites := make([]uint16, 0, len(cipherSuites))
|
||||||
|
for _, s := range cipherSuites {
|
||||||
|
selectedCipherSuites = append(selectedCipherSuites, s.ID)
|
||||||
|
}
|
||||||
|
// 设置tls信息
|
||||||
|
tlsConfig := &tls.Config{
|
||||||
|
NextProtos: []string{"http/1.1"},
|
||||||
|
MinVersion: tls.VersionTLS12,
|
||||||
|
CipherSuites: selectedCipherSuites,
|
||||||
|
}
|
||||||
|
srv := &http.Server{
|
||||||
|
Addr: base.Cfg.AdminAddr,
|
||||||
|
Handler: r,
|
||||||
|
TLSConfig: tlsConfig,
|
||||||
|
}
|
||||||
|
err := srv.ListenAndServeTLS(base.Cfg.CertFile, base.Cfg.CertKey)
|
||||||
|
if err != nil {
|
||||||
|
base.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func location(url string) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Location", url)
|
||||||
|
w.WriteHeader(http.StatusFound)
|
||||||
|
}
|
||||||
|
}
|
@@ -2,5 +2,6 @@ package base
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
APP_NAME = "AnyLink"
|
APP_NAME = "AnyLink"
|
||||||
APP_VER = "0.1.0"
|
// app版本号
|
||||||
|
APP_VER = "0.9.2-beta1"
|
||||||
)
|
)
|
185
server/base/cfg.go
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
package base
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
LinkModeTUN = "tun"
|
||||||
|
LinkModeTAP = "tap"
|
||||||
|
LinkModeMacvtap = "macvtap"
|
||||||
|
LinkModeIpvtap = "ipvtap"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Cfg = &ServerConfig{}
|
||||||
|
)
|
||||||
|
|
||||||
|
// # ReKey time (in seconds)
|
||||||
|
// rekey-time = 172800
|
||||||
|
// # ReKey method
|
||||||
|
// # Valid options: ssl, new-tunnel
|
||||||
|
// # ssl: Will perform an efficient rehandshake on the channel allowing
|
||||||
|
// # a seamless connection during rekey.
|
||||||
|
// # new-tunnel: Will instruct the client to discard and re-establish the channel.
|
||||||
|
// # Use this option only if the connecting clients have issues with the ssl
|
||||||
|
// # option.
|
||||||
|
// rekey-method = ssl
|
||||||
|
|
||||||
|
type ServerConfig struct {
|
||||||
|
// LinkAddr string `json:"link_addr"`
|
||||||
|
Conf string `json:"conf"`
|
||||||
|
Profile string `json:"profile"`
|
||||||
|
ServerAddr string `json:"server_addr"`
|
||||||
|
ServerDTLSAddr string `json:"server_dtls_addr"`
|
||||||
|
ServerDTLS bool `json:"server_dtls"`
|
||||||
|
AdminAddr string `json:"admin_addr"`
|
||||||
|
ProxyProtocol bool `json:"proxy_protocol"`
|
||||||
|
DbType string `json:"db_type"`
|
||||||
|
DbSource string `json:"db_source"`
|
||||||
|
CertFile string `json:"cert_file"`
|
||||||
|
CertKey string `json:"cert_key"`
|
||||||
|
FilesPath string `json:"files_path"`
|
||||||
|
LogPath string `json:"log_path"`
|
||||||
|
LogLevel string `json:"log_level"`
|
||||||
|
Pprof bool `json:"pprof"`
|
||||||
|
Issuer string `json:"issuer"`
|
||||||
|
AdminUser string `json:"admin_user"`
|
||||||
|
AdminPass string `json:"admin_pass"`
|
||||||
|
JwtSecret string `json:"jwt_secret"`
|
||||||
|
|
||||||
|
LinkMode string `json:"link_mode"` // tun tap macvtap ipvtap
|
||||||
|
Ipv4Master string `json:"ipv4_master"` // eth0
|
||||||
|
Ipv4CIDR string `json:"ipv4_cidr"` // 192.168.10.0/24
|
||||||
|
Ipv4Gateway string `json:"ipv4_gateway"` // 192.168.10.1
|
||||||
|
Ipv4Start string `json:"ipv4_start"` // 192.168.10.100
|
||||||
|
Ipv4End string `json:"ipv4_end"` // 192.168.10.200
|
||||||
|
IpLease int `json:"ip_lease"`
|
||||||
|
|
||||||
|
MaxClient int `json:"max_client"`
|
||||||
|
MaxUserClient int `json:"max_user_client"`
|
||||||
|
DefaultGroup string `json:"default_group"`
|
||||||
|
CstpKeepalive int `json:"cstp_keepalive"` // in seconds
|
||||||
|
CstpDpd int `json:"cstp_dpd"` // Dead peer detection in seconds
|
||||||
|
MobileKeepalive int `json:"mobile_keepalive"`
|
||||||
|
MobileDpd int `json:"mobile_dpd"`
|
||||||
|
Mtu int `json:"mtu"`
|
||||||
|
DefaultDomain string `json:"default_domain"`
|
||||||
|
|
||||||
|
SessionTimeout int `json:"session_timeout"` // in seconds
|
||||||
|
// AuthTimeout int `json:"auth_timeout"` // in seconds
|
||||||
|
AuditInterval int `json:"audit_interval"` // in seconds
|
||||||
|
|
||||||
|
ShowSQL bool `json:"show_sql"` // bool
|
||||||
|
IptablesNat bool `json:"iptables_nat"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func initServerCfg() {
|
||||||
|
|
||||||
|
// TODO 取消绝对地址转换
|
||||||
|
// sf, _ := filepath.Abs(cfgFile)
|
||||||
|
// base := filepath.Dir(sf)
|
||||||
|
|
||||||
|
// 转换成绝对路径
|
||||||
|
// Cfg.DbFile = getAbsPath(base, Cfg.DbFile)
|
||||||
|
// Cfg.CertFile = getAbsPath(base, Cfg.CertFile)
|
||||||
|
// Cfg.CertKey = getAbsPath(base, Cfg.CertKey)
|
||||||
|
// Cfg.UiPath = getAbsPath(base, Cfg.UiPath)
|
||||||
|
// Cfg.FilesPath = getAbsPath(base, Cfg.FilesPath)
|
||||||
|
// Cfg.LogPath = getAbsPath(base, Cfg.LogPath)
|
||||||
|
|
||||||
|
if Cfg.AdminPass == defaultPwd {
|
||||||
|
fmt.Fprintln(os.Stderr, "=== 使用默认的admin_pass有安全风险,请设置新的admin_pass ===")
|
||||||
|
}
|
||||||
|
|
||||||
|
if Cfg.JwtSecret == defaultJwt {
|
||||||
|
fmt.Fprintln(os.Stderr, "=== 使用默认的jwt_secret有安全风险,请设置新的jwt_secret ===")
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("ServerCfg: %+v \n", Cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAbsPath(base, cfile string) string {
|
||||||
|
if cfile == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
abs := filepath.IsAbs(cfile)
|
||||||
|
if abs {
|
||||||
|
return cfile
|
||||||
|
}
|
||||||
|
return filepath.Join(base, cfile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func initCfg() {
|
||||||
|
ref := reflect.ValueOf(Cfg)
|
||||||
|
s := ref.Elem()
|
||||||
|
|
||||||
|
typ := s.Type()
|
||||||
|
numFields := s.NumField()
|
||||||
|
for i := 0; i < numFields; i++ {
|
||||||
|
field := typ.Field(i)
|
||||||
|
value := s.Field(i)
|
||||||
|
tag := field.Tag.Get("json")
|
||||||
|
|
||||||
|
for _, v := range configs {
|
||||||
|
if v.Name == tag {
|
||||||
|
if v.Typ == cfgStr {
|
||||||
|
value.SetString(linkViper.GetString(v.Name))
|
||||||
|
}
|
||||||
|
if v.Typ == cfgInt {
|
||||||
|
value.SetInt(int64(linkViper.GetInt(v.Name)))
|
||||||
|
}
|
||||||
|
if v.Typ == cfgBool {
|
||||||
|
value.SetBool(linkViper.GetBool(v.Name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
initServerCfg()
|
||||||
|
}
|
||||||
|
|
||||||
|
type SCfg struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Env string `json:"env"`
|
||||||
|
Info string `json:"info"`
|
||||||
|
Data interface{} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ServerCfg2Slice() []SCfg {
|
||||||
|
ref := reflect.ValueOf(Cfg)
|
||||||
|
s := ref.Elem()
|
||||||
|
|
||||||
|
var datas []SCfg
|
||||||
|
|
||||||
|
typ := s.Type()
|
||||||
|
numFields := s.NumField()
|
||||||
|
for i := 0; i < numFields; i++ {
|
||||||
|
field := typ.Field(i)
|
||||||
|
value := s.Field(i)
|
||||||
|
tag := field.Tag.Get("json")
|
||||||
|
usage, env := getUsageEnv(tag)
|
||||||
|
|
||||||
|
datas = append(datas, SCfg{Name: tag, Env: env, Info: usage, Data: value.Interface()})
|
||||||
|
}
|
||||||
|
|
||||||
|
return datas
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUsageEnv(name string) (usage, env string) {
|
||||||
|
for _, v := range configs {
|
||||||
|
if v.Name == name {
|
||||||
|
usage = v.Usage
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if e, ok := envs[name]; ok {
|
||||||
|
env = e
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
147
server/base/cmd.go
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
package base
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"reflect"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bjdgyc/anylink/pkg/utils"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// 提交id
|
||||||
|
CommitId string
|
||||||
|
// pass明文
|
||||||
|
passwd string
|
||||||
|
// 生成密钥
|
||||||
|
secret bool
|
||||||
|
// 显示版本信息
|
||||||
|
rev bool
|
||||||
|
// 输出debug信息
|
||||||
|
debug bool
|
||||||
|
|
||||||
|
// Used for flags.
|
||||||
|
runSrv bool
|
||||||
|
|
||||||
|
linkViper *viper.Viper
|
||||||
|
rootCmd *cobra.Command
|
||||||
|
)
|
||||||
|
|
||||||
|
// Execute executes the root command.
|
||||||
|
func execute() {
|
||||||
|
initCmd()
|
||||||
|
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// viper.Debug()
|
||||||
|
ref := reflect.ValueOf(linkViper)
|
||||||
|
s := ref.Elem()
|
||||||
|
ee := s.FieldByName("env")
|
||||||
|
if ee.Kind() != reflect.Map {
|
||||||
|
panic("Viper env is err")
|
||||||
|
}
|
||||||
|
rr := ee.MapRange()
|
||||||
|
for rr.Next() {
|
||||||
|
// fmt.Println(rr.Key(), rr.Value().Index(0))
|
||||||
|
envs[rr.Key().String()] = rr.Value().Index(0).String()
|
||||||
|
}
|
||||||
|
|
||||||
|
if !runSrv {
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func initCmd() {
|
||||||
|
linkViper = viper.New()
|
||||||
|
rootCmd = &cobra.Command{
|
||||||
|
Use: "anylink",
|
||||||
|
Short: "AnyLink VPN Server",
|
||||||
|
Long: `AnyLink is a VPN Server application`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
// fmt.Println("cmd:", cmd.Use, args)
|
||||||
|
runSrv = true
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
linkViper.SetEnvPrefix("link")
|
||||||
|
|
||||||
|
// 基础配置
|
||||||
|
|
||||||
|
for _, v := range configs {
|
||||||
|
if v.Typ == cfgStr {
|
||||||
|
rootCmd.Flags().StringP(v.Name, v.Short, v.ValStr, v.Usage)
|
||||||
|
}
|
||||||
|
if v.Typ == cfgInt {
|
||||||
|
rootCmd.Flags().IntP(v.Name, v.Short, v.ValInt, v.Usage)
|
||||||
|
}
|
||||||
|
if v.Typ == cfgBool {
|
||||||
|
rootCmd.Flags().BoolP(v.Name, v.Short, v.ValBool, v.Usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = linkViper.BindPFlag(v.Name, rootCmd.Flags().Lookup(v.Name))
|
||||||
|
_ = linkViper.BindEnv(v.Name)
|
||||||
|
// viper.SetDefault(v.Name, v.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
rootCmd.AddCommand(initToolCmd())
|
||||||
|
|
||||||
|
cobra.OnInitialize(func() {
|
||||||
|
linkViper.AutomaticEnv()
|
||||||
|
conf := linkViper.GetString("conf")
|
||||||
|
|
||||||
|
_, err := os.Stat(conf)
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
// 没有配置文件,不做处理
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
linkViper.SetConfigFile(conf)
|
||||||
|
err = linkViper.ReadInConfig()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Using config file:", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func initToolCmd() *cobra.Command {
|
||||||
|
toolCmd := &cobra.Command{
|
||||||
|
Use: "tool",
|
||||||
|
Short: "AnyLink tool",
|
||||||
|
Long: `AnyLink tool is a application`,
|
||||||
|
}
|
||||||
|
|
||||||
|
toolCmd.Flags().BoolVarP(&rev, "version", "v", false, "display version info")
|
||||||
|
toolCmd.Flags().BoolVarP(&secret, "secret", "s", false, "generate a random jwt secret")
|
||||||
|
toolCmd.Flags().StringVarP(&passwd, "passwd", "p", "", "convert the password plaintext")
|
||||||
|
toolCmd.Flags().BoolVarP(&debug, "debug", "d", false, "list the config viper.Debug() info")
|
||||||
|
|
||||||
|
toolCmd.Run = func(cmd *cobra.Command, args []string) {
|
||||||
|
switch {
|
||||||
|
case rev:
|
||||||
|
fmt.Printf("%s v%s build on %s [%s, %s] commit_id(%s) \n",
|
||||||
|
APP_NAME, APP_VER, runtime.Version(), runtime.GOOS, runtime.GOARCH, CommitId)
|
||||||
|
case secret:
|
||||||
|
s, _ := utils.RandSecret(40, 60)
|
||||||
|
s = strings.Trim(s, "=")
|
||||||
|
fmt.Printf("Secret:%s\n", s)
|
||||||
|
case passwd != "":
|
||||||
|
pass, _ := utils.PasswordHash(passwd)
|
||||||
|
fmt.Printf("Passwd:%s\n", pass)
|
||||||
|
case debug:
|
||||||
|
linkViper.Debug()
|
||||||
|
default:
|
||||||
|
fmt.Println("Using [anylink tool -h] for help")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return toolCmd
|
||||||
|
}
|
67
server/base/config.go
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package base
|
||||||
|
|
||||||
|
const (
|
||||||
|
cfgStr = iota
|
||||||
|
cfgInt
|
||||||
|
cfgBool
|
||||||
|
|
||||||
|
defaultJwt = "abcdef.0123456789.abcdef"
|
||||||
|
defaultPwd = "$2a$10$UQ7C.EoPifDeJh6d8.31TeSPQU7hM/NOM2nixmBucJpAuXDQNqNke"
|
||||||
|
)
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
Typ int
|
||||||
|
Name string
|
||||||
|
Short string
|
||||||
|
Usage string
|
||||||
|
ValStr string
|
||||||
|
ValInt int
|
||||||
|
ValBool bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var configs = []config{
|
||||||
|
{Typ: cfgStr, Name: "conf", Usage: "config file", ValStr: "./conf/server.toml", Short: "c"},
|
||||||
|
{Typ: cfgStr, Name: "profile", Usage: "profile.xml file", ValStr: "./conf/profile.xml"},
|
||||||
|
{Typ: cfgStr, Name: "server_addr", Usage: "服务监听地址", ValStr: ":443"},
|
||||||
|
{Typ: cfgBool, Name: "server_dtls", Usage: "开启DTLS", ValBool: false},
|
||||||
|
{Typ: cfgStr, Name: "server_dtls_addr", Usage: "DTLS监听地址", ValStr: ":443"},
|
||||||
|
{Typ: cfgStr, Name: "admin_addr", Usage: "后台服务监听地址", ValStr: ":8800"},
|
||||||
|
{Typ: cfgBool, Name: "proxy_protocol", Usage: "TCP代理协议", ValBool: false},
|
||||||
|
{Typ: cfgStr, Name: "db_type", Usage: "数据库类型 [sqlite3 mysql postgres]", ValStr: "sqlite3"},
|
||||||
|
{Typ: cfgStr, Name: "db_source", Usage: "数据库source", ValStr: "./conf/anylink.db"},
|
||||||
|
{Typ: cfgStr, Name: "cert_file", Usage: "证书文件", ValStr: "./conf/vpn_cert.pem"},
|
||||||
|
{Typ: cfgStr, Name: "cert_key", Usage: "证书密钥", ValStr: "./conf/vpn_cert.key"},
|
||||||
|
{Typ: cfgStr, Name: "files_path", Usage: "外部下载文件路径", ValStr: "./conf/files"},
|
||||||
|
{Typ: cfgStr, Name: "log_path", Usage: "日志文件路径,默认标准输出", ValStr: ""},
|
||||||
|
{Typ: cfgStr, Name: "log_level", Usage: "日志等级 [debug info warn error]", ValStr: "debug"},
|
||||||
|
{Typ: cfgBool, Name: "pprof", Usage: "开启pprof", ValBool: false},
|
||||||
|
{Typ: cfgStr, Name: "issuer", Usage: "系统名称", ValStr: "XX公司VPN"},
|
||||||
|
{Typ: cfgStr, Name: "admin_user", Usage: "管理用户名", ValStr: "admin"},
|
||||||
|
{Typ: cfgStr, Name: "admin_pass", Usage: "管理用户密码", ValStr: defaultPwd},
|
||||||
|
{Typ: cfgStr, Name: "jwt_secret", Usage: "JWT密钥", ValStr: defaultJwt},
|
||||||
|
{Typ: cfgStr, Name: "link_mode", Usage: "虚拟网络类型[tun tap macvtap ipvtap]", ValStr: "tun"},
|
||||||
|
{Typ: cfgStr, Name: "ipv4_master", Usage: "ipv4主网卡名称", ValStr: "eth0"},
|
||||||
|
{Typ: cfgStr, Name: "ipv4_cidr", Usage: "ip地址网段", ValStr: "192.168.90.0/24"},
|
||||||
|
{Typ: cfgStr, Name: "ipv4_gateway", Usage: "ipv4_gateway", ValStr: "192.168.90.1"},
|
||||||
|
{Typ: cfgStr, Name: "ipv4_start", Usage: "IPV4开始地址", ValStr: "192.168.90.100"},
|
||||||
|
{Typ: cfgStr, Name: "ipv4_end", Usage: "IPV4结束", ValStr: "192.168.90.200"},
|
||||||
|
{Typ: cfgStr, Name: "default_group", Usage: "默认用户组", ValStr: "one"},
|
||||||
|
{Typ: cfgStr, Name: "default_domain", Usage: "要发布的默认域", ValStr: ""},
|
||||||
|
|
||||||
|
{Typ: cfgInt, Name: "ip_lease", Usage: "IP租期(秒)", ValInt: 1209600},
|
||||||
|
{Typ: cfgInt, Name: "max_client", Usage: "最大用户连接", ValInt: 200},
|
||||||
|
{Typ: cfgInt, Name: "max_user_client", Usage: "最大单用户连接", ValInt: 3},
|
||||||
|
{Typ: cfgInt, Name: "cstp_keepalive", Usage: "keepalive时间(秒)", ValInt: 4},
|
||||||
|
{Typ: cfgInt, Name: "cstp_dpd", Usage: "死链接检测时间(秒)", ValInt: 10},
|
||||||
|
{Typ: cfgInt, Name: "mobile_keepalive", Usage: "移动端keepalive接检测时间(秒)", ValInt: 7},
|
||||||
|
{Typ: cfgInt, Name: "mobile_dpd", Usage: "移动端死链接检测时间(秒)", ValInt: 15},
|
||||||
|
{Typ: cfgInt, Name: "mtu", Usage: "最大传输单元MTU", ValInt: 1460},
|
||||||
|
{Typ: cfgInt, Name: "session_timeout", Usage: "session过期时间(秒)", ValInt: 3600},
|
||||||
|
// {Typ: cfgInt, Name: "auth_timeout", Usage: "auth_timeout", ValInt: 0},
|
||||||
|
{Typ: cfgInt, Name: "audit_interval", Usage: "审计去重间隔(秒),-1关闭", ValInt: -1},
|
||||||
|
|
||||||
|
{Typ: cfgBool, Name: "show_sql", Usage: "显示sql语句,用于调试", ValBool: false},
|
||||||
|
{Typ: cfgBool, Name: "iptables_nat", Usage: "是否自动添加NAT", ValBool: true},
|
||||||
|
}
|
||||||
|
|
||||||
|
var envs = map[string]string{}
|
151
server/base/log.go
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
package base
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
_Debug = iota
|
||||||
|
_Info
|
||||||
|
_Warn
|
||||||
|
_Error
|
||||||
|
_Fatal
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
baseLw *logWriter
|
||||||
|
baseLog *log.Logger
|
||||||
|
baseLevel int
|
||||||
|
levels map[int]string
|
||||||
|
|
||||||
|
dateFormat = "2006-01-02"
|
||||||
|
logName = "anylink.log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 实现 os.Writer 接口
|
||||||
|
type logWriter struct {
|
||||||
|
UseStdout bool
|
||||||
|
FileName string
|
||||||
|
File *os.File
|
||||||
|
NowDate string
|
||||||
|
}
|
||||||
|
|
||||||
|
// 实现日志文件的切割
|
||||||
|
func (lw *logWriter) Write(p []byte) (n int, err error) {
|
||||||
|
if !lw.UseStdout {
|
||||||
|
return lw.File.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
date := time.Now().Format(dateFormat)
|
||||||
|
if lw.NowDate != date {
|
||||||
|
_ = lw.File.Close()
|
||||||
|
_ = os.Rename(lw.FileName, lw.FileName+"."+lw.NowDate)
|
||||||
|
lw.NowDate = date
|
||||||
|
lw.newFile()
|
||||||
|
}
|
||||||
|
return lw.File.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建新文件
|
||||||
|
func (lw *logWriter) newFile() {
|
||||||
|
if lw.UseStdout {
|
||||||
|
lw.File = os.Stdout
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.OpenFile(lw.FileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
lw.File = f
|
||||||
|
}
|
||||||
|
|
||||||
|
func initLog() {
|
||||||
|
// 初始化 baseLog
|
||||||
|
baseLw = &logWriter{
|
||||||
|
UseStdout: Cfg.LogPath == "",
|
||||||
|
FileName: path.Join(Cfg.LogPath, logName),
|
||||||
|
NowDate: time.Now().Format(dateFormat),
|
||||||
|
}
|
||||||
|
|
||||||
|
baseLw.newFile()
|
||||||
|
baseLevel = logLevel2Int(Cfg.LogLevel)
|
||||||
|
baseLog = log.New(baseLw, "", log.LstdFlags|log.Lshortfile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetBaseLw() *logWriter {
|
||||||
|
return baseLw
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取 log.Logger
|
||||||
|
func GetBaseLog() *log.Logger {
|
||||||
|
return baseLog
|
||||||
|
}
|
||||||
|
|
||||||
|
func logLevel2Int(l string) int {
|
||||||
|
levels = map[int]string{
|
||||||
|
_Debug: "Debug",
|
||||||
|
_Info: "Info",
|
||||||
|
_Warn: "Warn",
|
||||||
|
_Error: "Error",
|
||||||
|
_Fatal: "Fatal",
|
||||||
|
}
|
||||||
|
lvl := _Info
|
||||||
|
for k, v := range levels {
|
||||||
|
if strings.EqualFold(strings.ToLower(l), strings.ToLower(v)) {
|
||||||
|
lvl = k
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lvl
|
||||||
|
}
|
||||||
|
|
||||||
|
func output(l int, s ...interface{}) {
|
||||||
|
lvl := fmt.Sprintf("[%s] ", levels[l])
|
||||||
|
_ = baseLog.Output(3, lvl+fmt.Sprintln(s...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Debug(v ...interface{}) {
|
||||||
|
l := _Debug
|
||||||
|
if baseLevel > l {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
output(l, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Info(v ...interface{}) {
|
||||||
|
l := _Info
|
||||||
|
if baseLevel > l {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
output(l, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Warn(v ...interface{}) {
|
||||||
|
l := _Warn
|
||||||
|
if baseLevel > l {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
output(l, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Error(v ...interface{}) {
|
||||||
|
l := _Error
|
||||||
|
if baseLevel > l {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
output(l, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fatal(v ...interface{}) {
|
||||||
|
l := _Fatal
|
||||||
|
if baseLevel > l {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
output(l, v...)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
11
server/base/start.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package base
|
||||||
|
|
||||||
|
func Start() {
|
||||||
|
execute()
|
||||||
|
initCfg()
|
||||||
|
initLog()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test() {
|
||||||
|
initLog()
|
||||||
|
}
|
35
server/bridge-init.sh
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
#yum install bridge-utils
|
||||||
|
|
||||||
|
# Define Bridge Interface
|
||||||
|
br="anylink0"
|
||||||
|
|
||||||
|
# 请根据sever服务器信息,更新下面的信息
|
||||||
|
eth="eth0"
|
||||||
|
eth_ip="192.168.10.4/24"
|
||||||
|
eth_broadcast="192.168.10.255"
|
||||||
|
eth_gateway="192.168.10.1"
|
||||||
|
|
||||||
|
|
||||||
|
brctl addbr $br
|
||||||
|
brctl addif $br $eth
|
||||||
|
|
||||||
|
ip addr del $eth_ip dev $eth
|
||||||
|
ip addr add 0.0.0.0 dev $eth
|
||||||
|
ip link set dev $eth up promisc on
|
||||||
|
|
||||||
|
mac=`cat /sys/class/net/$eth/address`
|
||||||
|
ip link set dev $br up address $mac promisc on
|
||||||
|
ip addr add $eth_ip broadcast $eth_broadcast dev $br
|
||||||
|
|
||||||
|
|
||||||
|
route add default gateway $eth_gateway
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
0
server/conf/files/index.html
Normal file
2
server/conf/files/info.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
客户端软件需放置在files目录内,
|
||||||
|
如需要帮助请加QQ群:567510628
|
34
server/conf/profile.xml
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<AnyConnectProfile xmlns="http://schemas.xmlsoap.org/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://schemas.xmlsoap.org/encoding/ AnyConnectProfile.xsd">
|
||||||
|
|
||||||
|
<ClientInitialization>
|
||||||
|
<UseStartBeforeLogon UserControllable="false">false</UseStartBeforeLogon>
|
||||||
|
<StrictCertificateTrust>false</StrictCertificateTrust>
|
||||||
|
<RestrictPreferenceCaching>false</RestrictPreferenceCaching>
|
||||||
|
<RestrictTunnelProtocols>IPSec</RestrictTunnelProtocols>
|
||||||
|
<BypassDownloader>true</BypassDownloader>
|
||||||
|
<WindowsVPNEstablishment>AllowRemoteUsers</WindowsVPNEstablishment>
|
||||||
|
<LinuxVPNEstablishment>AllowRemoteUsers</LinuxVPNEstablishment>
|
||||||
|
<CertEnrollmentPin>pinAllowed</CertEnrollmentPin>
|
||||||
|
<CertificateMatch>
|
||||||
|
<KeyUsage>
|
||||||
|
<MatchKey>Digital_Signature</MatchKey>
|
||||||
|
</KeyUsage>
|
||||||
|
<ExtendedKeyUsage>
|
||||||
|
<ExtendedMatchKey>ClientAuth</ExtendedMatchKey>
|
||||||
|
</ExtendedKeyUsage>
|
||||||
|
</CertificateMatch>
|
||||||
|
|
||||||
|
<BackupServerList>
|
||||||
|
<HostAddress>localhost</HostAddress>
|
||||||
|
</BackupServerList>
|
||||||
|
</ClientInitialization>
|
||||||
|
|
||||||
|
<ServerList>
|
||||||
|
<HostEntry>
|
||||||
|
<HostName>VPN Server</HostName>
|
||||||
|
<HostAddress>localhost</HostAddress>
|
||||||
|
</HostEntry>
|
||||||
|
</ServerList>
|
||||||
|
</AnyConnectProfile>
|
81
server/conf/server-sample.toml
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
#示例配置信息
|
||||||
|
|
||||||
|
#其他配置文件,可以使用绝对路径
|
||||||
|
#或者相对于 anylink 二进制文件的路径
|
||||||
|
|
||||||
|
#数据文件
|
||||||
|
db_type = "sqlite3"
|
||||||
|
db_source = "./conf/anylink.db"
|
||||||
|
#证书文件 使用跟nginx一样的证书即可
|
||||||
|
cert_file = "./conf/vpn_cert.crt"
|
||||||
|
cert_key = "./conf/vpn_cert.key"
|
||||||
|
files_path = "./conf/files"
|
||||||
|
profile = "./conf/profile.xml"
|
||||||
|
#日志目录,为空写入标准输出
|
||||||
|
#log_path = "./log"
|
||||||
|
log_path = ""
|
||||||
|
log_level = "debug"
|
||||||
|
pprof = false
|
||||||
|
|
||||||
|
#系统名称
|
||||||
|
issuer = "XX公司VPN"
|
||||||
|
#后台管理用户
|
||||||
|
admin_user = "admin"
|
||||||
|
#pass 123456
|
||||||
|
admin_pass = "$2a$10$UQ7C.EoPifDeJh6d8.31TeSPQU7hM/NOM2nixmBucJpAuXDQNqNke"
|
||||||
|
jwt_secret = "abcdef.0123456789.abcdef"
|
||||||
|
|
||||||
|
|
||||||
|
#服务监听地址
|
||||||
|
server_addr = ":443"
|
||||||
|
#开启 DTLS, 默认关闭
|
||||||
|
server_dtls = false
|
||||||
|
server_dtls_addr = ":443"
|
||||||
|
#后台服务监听地址
|
||||||
|
admin_addr = ":8800"
|
||||||
|
#开启tcp proxy protocol协议
|
||||||
|
proxy_protocol = false
|
||||||
|
|
||||||
|
link_mode = "tun"
|
||||||
|
|
||||||
|
#客户端分配的ip地址池
|
||||||
|
ipv4_master = "eth0"
|
||||||
|
ipv4_cidr = "192.168.90.0/24"
|
||||||
|
ipv4_gateway = "192.168.90.1"
|
||||||
|
ipv4_start = "192.168.90.100"
|
||||||
|
ipv4_end = "192.168.90.200"
|
||||||
|
|
||||||
|
#最大客户端数量
|
||||||
|
max_client = 100
|
||||||
|
#单个用户同时在线数量
|
||||||
|
max_user_client = 3
|
||||||
|
#IP租期(秒)
|
||||||
|
ip_lease = 1209600
|
||||||
|
|
||||||
|
#默认选择的组
|
||||||
|
default_group = "one"
|
||||||
|
|
||||||
|
#客户端失效检测时间(秒) dpd > keepalive
|
||||||
|
cstp_keepalive = 6
|
||||||
|
cstp_dpd = 10
|
||||||
|
mobile_keepalive = 15
|
||||||
|
mobile_dpd = 20
|
||||||
|
|
||||||
|
#设置最大传输单元
|
||||||
|
mtu = 1460
|
||||||
|
|
||||||
|
# 要发布的默认域
|
||||||
|
default_domain = "example.com"
|
||||||
|
#default_domain = "example.com abc.example.com"
|
||||||
|
|
||||||
|
#session过期时间,用于断线重连,0永不过期
|
||||||
|
session_timeout = 3600
|
||||||
|
auth_timeout = 0
|
||||||
|
audit_interval = -1
|
||||||
|
|
||||||
|
show_sql = false
|
||||||
|
|
||||||
|
#是否自动添加nat
|
||||||
|
iptables_nat = true
|
||||||
|
|
||||||
|
|
37
server/conf/server.toml
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#示例配置信息
|
||||||
|
|
||||||
|
#其他配置文件,可以使用绝对路径
|
||||||
|
#或者相对于 anylink 二进制文件的路径
|
||||||
|
|
||||||
|
#数据文件
|
||||||
|
db_type = "sqlite3"
|
||||||
|
db_source = "./conf/anylink.db"
|
||||||
|
#证书文件
|
||||||
|
cert_file = "./conf/vpn_cert.crt"
|
||||||
|
cert_key = "./conf/vpn_cert.key"
|
||||||
|
files_path = "./conf/files"
|
||||||
|
log_level = "debug"
|
||||||
|
|
||||||
|
#系统名称
|
||||||
|
issuer = "XX公司VPN"
|
||||||
|
#后台管理用户
|
||||||
|
admin_user = "admin"
|
||||||
|
#pass 123456
|
||||||
|
admin_pass = "$2a$10$UQ7C.EoPifDeJh6d8.31TeSPQU7hM/NOM2nixmBucJpAuXDQNqNke"
|
||||||
|
jwt_secret = "abcdef.0123456789.abcdef"
|
||||||
|
|
||||||
|
#服务监听地址
|
||||||
|
server_addr = ":443"
|
||||||
|
#后台服务监听地址
|
||||||
|
admin_addr = ":8800"
|
||||||
|
|
||||||
|
|
||||||
|
#客户端分配的ip地址池
|
||||||
|
ipv4_master = "eth0"
|
||||||
|
ipv4_cidr = "192.168.90.0/24"
|
||||||
|
ipv4_gateway = "192.168.90.1"
|
||||||
|
ipv4_start = "192.168.90.100"
|
||||||
|
ipv4_end = "192.168.90.200"
|
||||||
|
|
||||||
|
#是否自动添加nat
|
||||||
|
iptables_nat = true
|
61
server/conf/vpn_cert.crt
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
MIIF9jCCBN6gAwIBAgIQAuUy6Rv6Bo3nDXn5FackbDANBgkqhkiG9w0BAQsFADBu
|
||||||
|
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
|
||||||
|
d3cuZGlnaWNlcnQuY29tMS0wKwYDVQQDEyRFbmNyeXB0aW9uIEV2ZXJ5d2hlcmUg
|
||||||
|
RFYgVExTIENBIC0gRzEwHhcNMjMwMTAzMDAwMDAwWhcNMjQwMTAzMjM1OTU5WjAc
|
||||||
|
MRowGAYDVQQDExF2cG4udGVzdC52cWlsdS5jbjCCASIwDQYJKoZIhvcNAQEBBQAD
|
||||||
|
ggEPADCCAQoCggEBANJAJPYBvOP/7v8SgMIkVLIulN/ziPALvFcEwVnQDImUIky8
|
||||||
|
4udy0fmvJ2E3E3NL6Qv14ZHDGtH7CafukimNWTT2BVmQBYiO1ZlUkHcHUX4IoYEh
|
||||||
|
egdy2xw0WwknJWTOyvkRkeDhtT9QUpA/zeemS4q1TG95zRDf5htUR4OMZXsZpkQ2
|
||||||
|
bkSgnLtdyUmw2nhfSWgsD9fbwr6WnOx/swsUe52N3sIDZ6JTgn3N7xeT3/lVJKVN
|
||||||
|
wyYkZldialmRzrs6btr3mmnqpWObcc4FvKr/CLmoOSXl0I1wWsr+HnQ4X9hHsJUk
|
||||||
|
jk3EZKfhH3mM37HF8apqztb6WjC3R96Zam6Z8bMCAwEAAaOCAuAwggLcMB8GA1Ud
|
||||||
|
IwQYMBaAFFV0T7JyT/VgulDR1+ZRXJoBhxrXMB0GA1UdDgQWBBSUpPcW3emC2l0o
|
||||||
|
q6qRBOBDMjQ2rDAcBgNVHREEFTATghF2cG4udGVzdC52cWlsdS5jbjAOBgNVHQ8B
|
||||||
|
Af8EBAMCBaAwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMD4GA1UdIAQ3
|
||||||
|
MDUwMwYGZ4EMAQIBMCkwJwYIKwYBBQUHAgEWG2h0dHA6Ly93d3cuZGlnaWNlcnQu
|
||||||
|
Y29tL0NQUzCBgAYIKwYBBQUHAQEEdDByMCQGCCsGAQUFBzABhhhodHRwOi8vb2Nz
|
||||||
|
cC5kaWdpY2VydC5jb20wSgYIKwYBBQUHMAKGPmh0dHA6Ly9jYWNlcnRzLmRpZ2lj
|
||||||
|
ZXJ0LmNvbS9FbmNyeXB0aW9uRXZlcnl3aGVyZURWVExTQ0EtRzEuY3J0MAkGA1Ud
|
||||||
|
EwQCMAAwggF9BgorBgEEAdZ5AgQCBIIBbQSCAWkBZwB2AO7N0GTV2xrOxVy3nbTN
|
||||||
|
E6Iyh0Z8vOzew1FIWUZxH7WbAAABhXXkyXMAAAQDAEcwRQIgVhLvLOPcW0V1xhBv
|
||||||
|
5KSeqGHbAnRVhew3kutV3Bu1x+ICIQCbYjRtmkDo1hx6p0YNdNfkZ3N5u+syVjwH
|
||||||
|
Al3a9NpVxgB1AEiw42vapkc0D+VqAvqdMOscUgHLVt0sgdm7v6s52IRzAAABhXXk
|
||||||
|
yY4AAAQDAEYwRAIgcuscG2kkSGNvAsVH9CAtXjNUwk9UJriY0+3OtQ4WVrMCIAsC
|
||||||
|
CkqEI1Ek5M26yrWt0Q7+u+UZ8rXhfYu3kcMMq7PVAHYAO1N3dT4tuYBOizBbBv5A
|
||||||
|
O2fYT8P0x70ADS1yb+H61BcAAAGFdeTJkAAABAMARzBFAiAEJbJTN8hrRUZ6UaaD
|
||||||
|
2TlyDQfzUvTkex0XGT6PGKHkagIhAJ+Kg6tdt/csKde2vdweu+dT01fzg/fq4q3o
|
||||||
|
mjfPhFm1MA0GCSqGSIb3DQEBCwUAA4IBAQAKFTUHbpgKsXARCBIIfEZGqkOvaafm
|
||||||
|
QaoNodc6cj0+LJCbuMzrTlkzmII0X/U52MBG8JCEIO8BPe5R4NIFqqaE066zQANq
|
||||||
|
HOsROOJi2A+WTTZcSEHbH3uhdVwcEQHvDzaOEEJc9Ilz6pdYsrv+trOmeR5PeIxv
|
||||||
|
t1jQacSwN1z6z0N4CRjBpePV/9nwETkEaKjQuXSoYlN+pczK/4nX2W9+E/OnwtZs
|
||||||
|
ScyFffPtTLHf1u4eSYuBT/AdwaKHXetxWzh98GP9LRfQhm63Gs+/WcloYl489dG/
|
||||||
|
FOFjch2TdmrPcUwxxGEbbPt3zXRxSVlzvIaf4gTUl2+PsKwbKy/w4OLS
|
||||||
|
-----END CERTIFICATE-----
|
||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
MIIEqjCCA5KgAwIBAgIQAnmsRYvBskWr+YBTzSybsTANBgkqhkiG9w0BAQsFADBh
|
||||||
|
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
|
||||||
|
d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD
|
||||||
|
QTAeFw0xNzExMjcxMjQ2MTBaFw0yNzExMjcxMjQ2MTBaMG4xCzAJBgNVBAYTAlVT
|
||||||
|
MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j
|
||||||
|
b20xLTArBgNVBAMTJEVuY3J5cHRpb24gRXZlcnl3aGVyZSBEViBUTFMgQ0EgLSBH
|
||||||
|
MTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALPeP6wkab41dyQh6mKc
|
||||||
|
oHqt3jRIxW5MDvf9QyiOR7VfFwK656es0UFiIb74N9pRntzF1UgYzDGu3ppZVMdo
|
||||||
|
lbxhm6dWS9OK/lFehKNT0OYI9aqk6F+U7cA6jxSC+iDBPXwdF4rs3KRyp3aQn6pj
|
||||||
|
pp1yr7IB6Y4zv72Ee/PlZ/6rK6InC6WpK0nPVOYR7n9iDuPe1E4IxUMBH/T33+3h
|
||||||
|
yuH3dvfgiWUOUkjdpMbyxX+XNle5uEIiyBsi4IvbcTCh8ruifCIi5mDXkZrnMT8n
|
||||||
|
wfYCV6v6kDdXkbgGRLKsR4pucbJtbKqIkUGxuZI2t7pfewKRc5nWecvDBZf3+p1M
|
||||||
|
pA8CAwEAAaOCAU8wggFLMB0GA1UdDgQWBBRVdE+yck/1YLpQ0dfmUVyaAYca1zAf
|
||||||
|
BgNVHSMEGDAWgBQD3lA1VtFMu2bwo+IbG8OXsj3RVTAOBgNVHQ8BAf8EBAMCAYYw
|
||||||
|
HQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMBIGA1UdEwEB/wQIMAYBAf8C
|
||||||
|
AQAwNAYIKwYBBQUHAQEEKDAmMCQGCCsGAQUFBzABhhhodHRwOi8vb2NzcC5kaWdp
|
||||||
|
Y2VydC5jb20wQgYDVR0fBDswOTA3oDWgM4YxaHR0cDovL2NybDMuZGlnaWNlcnQu
|
||||||
|
Y29tL0RpZ2lDZXJ0R2xvYmFsUm9vdENBLmNybDBMBgNVHSAERTBDMDcGCWCGSAGG
|
||||||
|
/WwBAjAqMCgGCCsGAQUFBwIBFhxodHRwczovL3d3dy5kaWdpY2VydC5jb20vQ1BT
|
||||||
|
MAgGBmeBDAECATANBgkqhkiG9w0BAQsFAAOCAQEAK3Gp6/aGq7aBZsxf/oQ+TD/B
|
||||||
|
SwW3AU4ETK+GQf2kFzYZkby5SFrHdPomunx2HBzViUchGoofGgg7gHW0W3MlQAXW
|
||||||
|
M0r5LUvStcr82QDWYNPaUy4taCQmyaJ+VB+6wxHstSigOlSNF2a6vg4rgexixeiV
|
||||||
|
4YSB03Yqp2t3TeZHM9ESfkus74nQyW7pRGezj+TC44xCagCQQOzzNmzEAP2SnCrJ
|
||||||
|
sNE2DpRVMnL8J6xBRdjmOsC3N6cQuKuRXbzByVBjCqAA8t1L0I+9wXJerLPyErjy
|
||||||
|
rMKWaBFLmfK/AHNF4ZihwPGOc7w6UHczBZXH5RFzJNnww+WnKuTPI0HfnVH8lg==
|
||||||
|
-----END CERTIFICATE-----
|
27
server/conf/vpn_cert.key
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
-----BEGIN RSA PRIVATE KEY-----
|
||||||
|
MIIEpQIBAAKCAQEA0kAk9gG84//u/xKAwiRUsi6U3/OI8Au8VwTBWdAMiZQiTLzi
|
||||||
|
53LR+a8nYTcTc0vpC/XhkcMa0fsJp+6SKY1ZNPYFWZAFiI7VmVSQdwdRfgihgSF6
|
||||||
|
B3LbHDRbCSclZM7K+RGR4OG1P1BSkD/N56ZLirVMb3nNEN/mG1RHg4xlexmmRDZu
|
||||||
|
RKCcu13JSbDaeF9JaCwP19vCvpac7H+zCxR7nY3ewgNnolOCfc3vF5Pf+VUkpU3D
|
||||||
|
JiRmV2JqWZHOuzpu2veaaeqlY5txzgW8qv8Iuag5JeXQjXBayv4edDhf2EewlSSO
|
||||||
|
TcRkp+EfeYzfscXxqmrO1vpaMLdH3plqbpnxswIDAQABAoIBAGKKzugAi4Q/Vch2
|
||||||
|
ZyPXPF0hCQToE3QSxAzy/R53rRCkfekClMTO44xHpEjjs/mTiCBjd3xGeiEVrIJp
|
||||||
|
hlb0WW3Bq2M9ZeKJs6JAaM9o/jB4oh2wT44DLqALB+oDz3puk+Jl8j34++a3YmMa
|
||||||
|
jIq4veo+rBsJduwkTKjdeQE2ge/ODZEQ6bUmSjYo1P9LNGEyO2wmcVk+jHx0zBi7
|
||||||
|
8fR3oY03Io+byuN0494Di1m3IpIdj3ma0MV5zJf31urLXqqYtOApouWL/yhZOIuo
|
||||||
|
YW+mcuS7ZgK5FsqrUm0vGBcf24GcKhhlBlUu0mfLrCRrWLDsqJDQ/8alvuNP0IVm
|
||||||
|
gqz0H5UCgYEA8yaKzMfkeRXSTERt6NZHo/8ShIn26Yf+pMDpVdYKfVBL254vGTeq
|
||||||
|
B+LQhDpxZV1iMr1FNvkhHtNGQ74ZbWOj4+5Xjsllaw1ao2iGp1w/chuJ6FG/Go4q
|
||||||
|
9FaY3eGiCqRJOQNivBxU/D7sN0y4b48HAEQdmp516pItlGtG+C8TY38CgYEA3VyD
|
||||||
|
6EczHdAmPO7bdbYn/irfe78so1lHT04P0FiVg2W77ZKuQINTNDK9w/alYyZ2tH1b
|
||||||
|
N2JznulJ6UDcl4xw43xJixxhme2jWPaYzmQUuQHviZ0D0tCgmOkN8bUnc9LSvEGA
|
||||||
|
SnaiKbOtUfP8Z3c/mF997wuFNfdmhww8LBpbO80CgYEAmdRKf9/+5bQuhd3NAz99
|
||||||
|
t31KQ9vdAEXvjmAVvx5ZKIrCU0EyXuvegHq4nM80qoJ3+83Omkbm80+K5pTAFXqy
|
||||||
|
VyOU9Vro9N9P9o3MktlDsndFulrtYmmLN2YJ9GYpVD43rQA9WPE7uxI784hwLvP3
|
||||||
|
4+00JXwW8b5lY76y+ZUe2RUCgYEAt6BQN/YgPCH4JmHKIWp64If2HbQntlWQJwRN
|
||||||
|
b/qcBIT3EQu1iwSll85jxtSqu4YjwHOgoGAGI5PIYTsSApFY8AyhAUoI2OTdtSXS
|
||||||
|
+prg6dvmNhTPICk6n73seE5bLOR9NfdsEdk5ijhnlW09OyMb2S2VzR+UYIEbRvnq
|
||||||
|
THeMqR0CgYEAwQ9aAIGD4t4DXjHHtz6Wqpbq6jj6mmYBsL9jqRgu8ASj1/THgWWn
|
||||||
|
iUrlCbFIXWu2vnP1h0SBV56GA7MSTqAt0ZdTzpI1PRkMOIO9z1dN4Q2R1SEya1eF
|
||||||
|
7/LPLqJIoLbILGvy6U3DLYdMckPZaoTPf5BNKD52paZcNbjkXTlVLSY=
|
||||||
|
-----END RSA PRIVATE KEY-----
|
20
server/cron/clear_audit.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package cron
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bjdgyc/anylink/base"
|
||||||
|
"github.com/bjdgyc/anylink/dbdata"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 清除访问审计日志
|
||||||
|
func ClearAudit() {
|
||||||
|
lifeDay, timesUp := isClearTime()
|
||||||
|
if !timesUp {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 当审计日志永久保存,则退出
|
||||||
|
if lifeDay <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
affected, err := dbdata.ClearAccessAudit(getTimeAgo(lifeDay))
|
||||||
|
base.Info("Cron ClearAudit: ", affected, err)
|
||||||
|
}
|
47
server/cron/clear_statsinfo.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package cron
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/bjdgyc/anylink/base"
|
||||||
|
"github.com/bjdgyc/anylink/dbdata"
|
||||||
|
)
|
||||||
|
|
||||||
|
const siLifeDay = 30
|
||||||
|
|
||||||
|
// 清除图表数据
|
||||||
|
func ClearStatsInfo() {
|
||||||
|
_, timesUp := isClearTime()
|
||||||
|
if !timesUp {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ts := getTimeAgo(siLifeDay)
|
||||||
|
for _, item := range dbdata.StatsInfoIns.Actions {
|
||||||
|
affected, err := dbdata.StatsInfoIns.ClearStatsInfo(item, ts)
|
||||||
|
base.Info("Cron ClearStatsInfo "+item+": ", affected, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 是否到了"清理时间"
|
||||||
|
func isClearTime() (int, bool) {
|
||||||
|
dataLog, err := dbdata.SettingGetAuditLog()
|
||||||
|
if err != nil {
|
||||||
|
base.Error("Cron SettingGetLog: ", err)
|
||||||
|
return -1, false
|
||||||
|
}
|
||||||
|
currentTime := time.Now().Format("15:04")
|
||||||
|
// 未到"清理时间"时, 则返回
|
||||||
|
if dataLog.ClearTime != currentTime {
|
||||||
|
return -1, false
|
||||||
|
}
|
||||||
|
return dataLog.LifeDay, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据存储时长,获取清理日期
|
||||||
|
func getTimeAgo(days int) string {
|
||||||
|
var timeS string
|
||||||
|
ts := time.Now().AddDate(0, 0, -days)
|
||||||
|
tsZero := time.Date(ts.Year(), ts.Month(), ts.Day(), 0, 0, 0, 0, time.Local)
|
||||||
|
timeS = tsZero.Format(dbdata.LayoutTimeFormat)
|
||||||
|
return timeS
|
||||||
|
}
|
20
server/cron/clear_user_act_log.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package cron
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bjdgyc/anylink/base"
|
||||||
|
"github.com/bjdgyc/anylink/dbdata"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 清除用户活动日志
|
||||||
|
func ClearUserActLog() {
|
||||||
|
lifeDay, timesUp := isClearTime()
|
||||||
|
if !timesUp {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 当审计日志永久保存时,则退出
|
||||||
|
if lifeDay <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
affected, err := dbdata.UserActLogIns.ClearUserActLog(getTimeAgo(lifeDay))
|
||||||
|
base.Info("Cron ClearUserActLog: ", affected, err)
|
||||||
|
}
|
17
server/cron/start.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package cron
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/bjdgyc/anylink/sessdata"
|
||||||
|
"github.com/go-co-op/gocron"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Start() {
|
||||||
|
s := gocron.NewScheduler(time.Local)
|
||||||
|
s.Cron("0 * * * *").Do(ClearAudit)
|
||||||
|
s.Cron("0 * * * *").Do(ClearStatsInfo)
|
||||||
|
s.Cron("0 * * * *").Do(ClearUserActLog)
|
||||||
|
s.Every(1).Day().At("00:00").Do(sessdata.CloseUserLimittimeSession)
|
||||||
|
s.StartAsync()
|
||||||
|
}
|
62
server/dbdata/audit.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package dbdata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"xorm.io/xorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SearchCon struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Src string `json:"src"`
|
||||||
|
Dst string `json:"dst"`
|
||||||
|
DstPort string `json:"dst_port"`
|
||||||
|
AccessProto string `json:"access_proto"`
|
||||||
|
Date []string `json:"date"`
|
||||||
|
Info string `json:"info"`
|
||||||
|
Sort int `json:"sort"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAuditSession(search string) *xorm.Session {
|
||||||
|
session := xdb.Where("1=1")
|
||||||
|
if search == "" {
|
||||||
|
return session
|
||||||
|
}
|
||||||
|
var searchData SearchCon
|
||||||
|
err := json.Unmarshal([]byte(search), &searchData)
|
||||||
|
if err != nil {
|
||||||
|
return session
|
||||||
|
}
|
||||||
|
if searchData.Username != "" {
|
||||||
|
session.And("username = ?", searchData.Username)
|
||||||
|
}
|
||||||
|
if searchData.Src != "" {
|
||||||
|
session.And("src = ?", searchData.Src)
|
||||||
|
}
|
||||||
|
if searchData.Dst != "" {
|
||||||
|
session.And("dst = ?", searchData.Dst)
|
||||||
|
}
|
||||||
|
if searchData.DstPort != "" {
|
||||||
|
session.And("dst_port = ?", searchData.DstPort)
|
||||||
|
}
|
||||||
|
if searchData.AccessProto != "" {
|
||||||
|
session.And("access_proto = ?", searchData.AccessProto)
|
||||||
|
}
|
||||||
|
if len(searchData.Date) > 0 && searchData.Date[0] != "" {
|
||||||
|
session.And("created_at BETWEEN ? AND ?", searchData.Date[0], searchData.Date[1])
|
||||||
|
}
|
||||||
|
if searchData.Info != "" {
|
||||||
|
session.And("info LIKE ?", "%"+searchData.Info+"%")
|
||||||
|
}
|
||||||
|
if searchData.Sort == 1 {
|
||||||
|
session.OrderBy("id desc")
|
||||||
|
} else {
|
||||||
|
session.OrderBy("id asc")
|
||||||
|
}
|
||||||
|
return session
|
||||||
|
}
|
||||||
|
|
||||||
|
func ClearAccessAudit(ts string) (int64, error) {
|
||||||
|
affected, err := xdb.Where("created_at < '" + ts + "'").Delete(&AccessAudit{})
|
||||||
|
return affected, err
|
||||||
|
}
|
43
server/dbdata/audit_test.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package dbdata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSearchAudit(t *testing.T) {
|
||||||
|
ast := assert.New(t)
|
||||||
|
|
||||||
|
preIpData()
|
||||||
|
defer closeIpdata()
|
||||||
|
|
||||||
|
currDateVal := "2022-07-24 00:00:00"
|
||||||
|
CreatedAt, _ := time.ParseInLocation("2006-01-02 15:04:05", currDateVal, time.Local)
|
||||||
|
|
||||||
|
dataTest := AccessAudit{
|
||||||
|
Username: "Test",
|
||||||
|
Protocol: 6,
|
||||||
|
Src: "10.10.1.5",
|
||||||
|
SrcPort: 0,
|
||||||
|
Dst: "172.217.160.68",
|
||||||
|
DstPort: 80,
|
||||||
|
AccessProto: 4,
|
||||||
|
Info: "www.google.com",
|
||||||
|
CreatedAt: CreatedAt,
|
||||||
|
}
|
||||||
|
err := Add(dataTest)
|
||||||
|
ast.Nil(err)
|
||||||
|
|
||||||
|
var datas []AccessAudit
|
||||||
|
searchFormat := `{"username": "%s", "src":"%s", "dst": "%s", "dst_port":"%d","access_proto":"%d","info":"%s","date":["%s","%s"]}`
|
||||||
|
search := fmt.Sprintf(searchFormat, dataTest.Username, dataTest.Src, dataTest.Dst, dataTest.DstPort, dataTest.AccessProto, dataTest.Info, currDateVal, currDateVal)
|
||||||
|
|
||||||
|
session := GetAuditSession(search)
|
||||||
|
count, _ := FindAndCount(session, &datas, PageSize, 0)
|
||||||
|
ast.Equal(count, int64(1))
|
||||||
|
ast.Equal(datas[0].Username, dataTest.Username)
|
||||||
|
ast.Equal(datas[0].Dst, dataTest.Dst)
|
||||||
|
}
|
169
server/dbdata/db.go
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
package dbdata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/bjdgyc/anylink/base"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/lib/pq"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
"xorm.io/xorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
xdb *xorm.Engine
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetXdb() *xorm.Engine {
|
||||||
|
return xdb
|
||||||
|
}
|
||||||
|
|
||||||
|
func initDb() {
|
||||||
|
var err error
|
||||||
|
xdb, err = xorm.NewEngine(base.Cfg.DbType, base.Cfg.DbSource)
|
||||||
|
// 初始化xorm时区
|
||||||
|
xdb.DatabaseTZ = time.Local
|
||||||
|
xdb.TZLocation = time.Local
|
||||||
|
if err != nil {
|
||||||
|
base.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if base.Cfg.ShowSQL {
|
||||||
|
xdb.ShowSQL(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化数据库
|
||||||
|
err = xdb.Sync2(&User{}, &Setting{}, &Group{}, &IpMap{}, &AccessAudit{}, &Policy{}, &StatsNetwork{}, &StatsCpu{}, &StatsMem{}, &StatsOnline{}, &UserActLog{})
|
||||||
|
if err != nil {
|
||||||
|
base.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// fmt.Println("s1=============", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func initData() {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
// 判断是否初次使用
|
||||||
|
install := &SettingInstall{}
|
||||||
|
err = SettingGet(install)
|
||||||
|
|
||||||
|
if err == nil && install.Installed {
|
||||||
|
// 已经安装过
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发生错误
|
||||||
|
if err != ErrNotFound {
|
||||||
|
base.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = addInitData()
|
||||||
|
if err != nil {
|
||||||
|
base.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func addInitData() error {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
sess := xdb.NewSession()
|
||||||
|
defer sess.Close()
|
||||||
|
|
||||||
|
err = sess.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SettingSmtp
|
||||||
|
smtp := &SettingSmtp{
|
||||||
|
Host: "127.0.0.1",
|
||||||
|
Port: 25,
|
||||||
|
From: "vpn@xx.com",
|
||||||
|
Encryption: "None",
|
||||||
|
}
|
||||||
|
err = SettingSessAdd(sess, smtp)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SettingAuditLog
|
||||||
|
auditLog := SettingGetAuditLogDefault()
|
||||||
|
err = SettingSessAdd(sess, auditLog)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SettingOther
|
||||||
|
other := &SettingOther{
|
||||||
|
LinkAddr: "vpn.xx.com",
|
||||||
|
Banner: "您已接入公司网络,请按照公司规定使用。\n请勿进行非工作下载及视频行为!",
|
||||||
|
Homeindex: "AnyLink 是一个企业级远程办公 sslvpn 的软件,可以支持多人同时在线使用。",
|
||||||
|
AccountMail: accountMail,
|
||||||
|
}
|
||||||
|
err = SettingSessAdd(sess, other)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Install
|
||||||
|
install := &SettingInstall{Installed: true}
|
||||||
|
err = SettingSessAdd(sess, install)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = sess.Commit()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
g1 := Group{
|
||||||
|
Name: "ops",
|
||||||
|
AllowLan: true,
|
||||||
|
ClientDns: []ValData{{Val: "114.114.114.114"}},
|
||||||
|
RouteInclude: []ValData{{Val: All}},
|
||||||
|
Status: 1,
|
||||||
|
}
|
||||||
|
err = SetGroup(&g1)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckErrNotFound(err error) bool {
|
||||||
|
return err == ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
const accountMail = `<p>您好:</p>
|
||||||
|
<p> 您的{{.Issuer}}账号已经审核开通。</p>
|
||||||
|
<p>
|
||||||
|
登陆地址: <b>{{.LinkAddr}}</b> <br/>
|
||||||
|
用户组: <b>{{.Group}}</b> <br/>
|
||||||
|
用户名: <b>{{.Username}}</b> <br/>
|
||||||
|
用户PIN码: <b>{{.PinCode}}</b> <br/>
|
||||||
|
<!--
|
||||||
|
用户动态码(3天后失效):<br/>
|
||||||
|
<img src="{{.OtpImg}}"/>
|
||||||
|
-->
|
||||||
|
用户动态码(请妥善保存):<br/>
|
||||||
|
<img src="{{.OtpImgBase64}}"/>
|
||||||
|
</p>
|
||||||
|
<div>
|
||||||
|
使用说明:
|
||||||
|
<ul>
|
||||||
|
<li>请使用OTP软件扫描动态码二维码</li>
|
||||||
|
<li>然后使用anyconnect客户端进行登陆</li>
|
||||||
|
<li>登陆密码为 【PIN码+动态码】</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<p>
|
||||||
|
软件下载地址: https://{{.LinkAddr}}/files/info.txt
|
||||||
|
</p>`
|
109
server/dbdata/db_orm.go
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
package dbdata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"xorm.io/xorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
const PageSize = 10
|
||||||
|
|
||||||
|
var ErrNotFound = errors.New("ErrNotFound")
|
||||||
|
|
||||||
|
func Add(data interface{}) error {
|
||||||
|
_, err := xdb.InsertOne(data)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddBatch(data interface{}) error {
|
||||||
|
_, err := xdb.Insert(data)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Update(fieldName string, value interface{}, data interface{}) error {
|
||||||
|
_, err := xdb.Where(fieldName+"=?", value).Update(data)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Del(data interface{}) error {
|
||||||
|
_, err := xdb.Delete(data)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func extract(data interface{}, fieldName string) interface{} {
|
||||||
|
ref := reflect.ValueOf(data)
|
||||||
|
r := &ref
|
||||||
|
if r.Kind() == reflect.Ptr {
|
||||||
|
e := r.Elem()
|
||||||
|
r = &e
|
||||||
|
}
|
||||||
|
field := r.FieldByName(fieldName).Interface()
|
||||||
|
return field
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新全部字段
|
||||||
|
func Set(data interface{}) error {
|
||||||
|
id := extract(data, "Id")
|
||||||
|
_, err := xdb.ID(id).AllCols().Update(data)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func One(fieldName string, value interface{}, data interface{}) error {
|
||||||
|
has, err := xdb.Where(fieldName+"=?", value).Get(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !has {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CountAll(data interface{}) int {
|
||||||
|
n, _ := xdb.Count(data)
|
||||||
|
return int(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Find(data interface{}, limit, page int) error {
|
||||||
|
if limit == 0 {
|
||||||
|
return xdb.Find(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
start := (page - 1) * limit
|
||||||
|
return xdb.Limit(limit, start).Find(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindWhere(data interface{}, limit int, page int, where string, args ...interface{}) error {
|
||||||
|
if limit == 0 {
|
||||||
|
return xdb.Where(where, args...).Find(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
start := (page - 1) * limit
|
||||||
|
return xdb.Where(where, args...).Limit(limit, start).Find(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CountPrefix(fieldName string, prefix string, data interface{}) int {
|
||||||
|
n, _ := xdb.Where(fieldName+" like ?", prefix+"%").Count(data)
|
||||||
|
return int(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Prefix(fieldName string, prefix string, data interface{}, limit, page int) error {
|
||||||
|
where := xdb.Where(fieldName+" like ?", prefix+"%")
|
||||||
|
if limit == 0 {
|
||||||
|
return where.Find(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
start := (page - 1) * limit
|
||||||
|
return where.Limit(limit, start).Find(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindAndCount(session *xorm.Session, data interface{}, limit, page int) (int64, error) {
|
||||||
|
if limit == 0 {
|
||||||
|
return session.FindAndCount(data)
|
||||||
|
}
|
||||||
|
start := (page - 1) * limit
|
||||||
|
totalCount, err := session.Limit(limit, start).FindAndCount(data)
|
||||||
|
return totalCount, err
|
||||||
|
}
|
@@ -11,23 +11,25 @@ import (
|
|||||||
|
|
||||||
func preIpData() {
|
func preIpData() {
|
||||||
tmpDb := path.Join(os.TempDir(), "anylink_test.db")
|
tmpDb := path.Join(os.TempDir(), "anylink_test.db")
|
||||||
base.Cfg.DbFile = tmpDb
|
base.Cfg.DbType = "sqlite3"
|
||||||
|
base.Cfg.DbSource = tmpDb
|
||||||
initDb()
|
initDb()
|
||||||
}
|
}
|
||||||
|
|
||||||
func closeIpdata() {
|
func closeIpdata() {
|
||||||
sdb.Close()
|
xdb.Close()
|
||||||
tmpDb := path.Join(os.TempDir(), "anylink_test.db")
|
tmpDb := path.Join(os.TempDir(), "anylink_test.db")
|
||||||
os.Remove(tmpDb)
|
os.Remove(tmpDb)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDb(t *testing.T) {
|
func TestDb(t *testing.T) {
|
||||||
assert := assert.New(t)
|
ast := assert.New(t)
|
||||||
preIpData()
|
preIpData()
|
||||||
defer closeIpdata()
|
defer closeIpdata()
|
||||||
|
|
||||||
u := User{Username: "a"}
|
u := User{Username: "a"}
|
||||||
Save(&u)
|
err := Add(&u)
|
||||||
|
ast.Nil(err)
|
||||||
|
|
||||||
assert.Equal(u.Id, 1)
|
ast.Equal(u.Id, 1)
|
||||||
}
|
}
|
265
server/dbdata/group.go
Normal file
@@ -0,0 +1,265 @@
|
|||||||
|
package dbdata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/bjdgyc/anylink/base"
|
||||||
|
"golang.org/x/text/language"
|
||||||
|
"golang.org/x/text/message"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Allow = "allow"
|
||||||
|
Deny = "deny"
|
||||||
|
All = "all"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 域名分流最大字符2万
|
||||||
|
const DsMaxLen = 20000
|
||||||
|
|
||||||
|
type GroupLinkAcl struct {
|
||||||
|
// 自上而下匹配 默认 allow * *
|
||||||
|
Action string `json:"action"` // allow、deny
|
||||||
|
Val string `json:"val"`
|
||||||
|
Port uint16 `json:"port"`
|
||||||
|
IpNet *net.IPNet `json:"ip_net"`
|
||||||
|
Note string `json:"note"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ValData struct {
|
||||||
|
Val string `json:"val"`
|
||||||
|
IpMask string `json:"ip_mask"`
|
||||||
|
Note string `json:"note"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GroupNameId struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// type Group struct {
|
||||||
|
// Id int `json:"id" xorm:"pk autoincr not null"`
|
||||||
|
// Name string `json:"name" xorm:"varchar(60) not null unique"`
|
||||||
|
// Note string `json:"note" xorm:"varchar(255)"`
|
||||||
|
// AllowLan bool `json:"allow_lan" xorm:"Bool"`
|
||||||
|
// ClientDns []ValData `json:"client_dns" xorm:"Text"`
|
||||||
|
// RouteInclude []ValData `json:"route_include" xorm:"Text"`
|
||||||
|
// RouteExclude []ValData `json:"route_exclude" xorm:"Text"`
|
||||||
|
// DsExcludeDomains string `json:"ds_exclude_domains" xorm:"Text"`
|
||||||
|
// DsIncludeDomains string `json:"ds_include_domains" xorm:"Text"`
|
||||||
|
// LinkAcl []GroupLinkAcl `json:"link_acl" xorm:"Text"`
|
||||||
|
// Bandwidth int `json:"bandwidth" xorm:"Int"` // 带宽限制
|
||||||
|
// Auth map[string]interface{} `json:"auth" xorm:"not null default '{}' varchar(255)"` // 认证方式
|
||||||
|
// Status int8 `json:"status" xorm:"Int"` // 1正常
|
||||||
|
// CreatedAt time.Time `json:"created_at" xorm:"DateTime created"`
|
||||||
|
// UpdatedAt time.Time `json:"updated_at" xorm:"DateTime updated"`
|
||||||
|
// }
|
||||||
|
|
||||||
|
func GetGroupNames() []string {
|
||||||
|
var datas []Group
|
||||||
|
err := Find(&datas, 0, 0)
|
||||||
|
if err != nil {
|
||||||
|
base.Error(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var names []string
|
||||||
|
for _, v := range datas {
|
||||||
|
names = append(names, v.Name)
|
||||||
|
}
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetGroupNamesNormal() []string {
|
||||||
|
var datas []Group
|
||||||
|
err := FindWhere(&datas, 0, 0, "status=1")
|
||||||
|
if err != nil {
|
||||||
|
base.Error(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var names []string
|
||||||
|
for _, v := range datas {
|
||||||
|
names = append(names, v.Name)
|
||||||
|
}
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetGroupNamesIds() []GroupNameId {
|
||||||
|
var datas []Group
|
||||||
|
err := Find(&datas, 0, 0)
|
||||||
|
if err != nil {
|
||||||
|
base.Error(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var names []GroupNameId
|
||||||
|
for _, v := range datas {
|
||||||
|
names = append(names, GroupNameId{Id: v.Id, Name: v.Name})
|
||||||
|
}
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetGroup(g *Group) error {
|
||||||
|
var err error
|
||||||
|
if g.Name == "" {
|
||||||
|
return errors.New("用户组名错误")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断数据
|
||||||
|
routeInclude := []ValData{}
|
||||||
|
for _, v := range g.RouteInclude {
|
||||||
|
if v.Val != "" {
|
||||||
|
if v.Val == All {
|
||||||
|
routeInclude = append(routeInclude, v)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
ipMask, _, err := parseIpNet(v.Val)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("RouteInclude 错误" + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
v.IpMask = ipMask
|
||||||
|
routeInclude = append(routeInclude, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g.RouteInclude = routeInclude
|
||||||
|
routeExclude := []ValData{}
|
||||||
|
for _, v := range g.RouteExclude {
|
||||||
|
if v.Val != "" {
|
||||||
|
ipMask, _, err := parseIpNet(v.Val)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("RouteExclude 错误" + err.Error())
|
||||||
|
}
|
||||||
|
v.IpMask = ipMask
|
||||||
|
routeExclude = append(routeExclude, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g.RouteExclude = routeExclude
|
||||||
|
// 转换数据
|
||||||
|
linkAcl := []GroupLinkAcl{}
|
||||||
|
for _, v := range g.LinkAcl {
|
||||||
|
if v.Val != "" {
|
||||||
|
_, ipNet, err := parseIpNet(v.Val)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("GroupLinkAcl 错误" + err.Error())
|
||||||
|
}
|
||||||
|
v.IpNet = ipNet
|
||||||
|
linkAcl = append(linkAcl, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g.LinkAcl = linkAcl
|
||||||
|
|
||||||
|
// DNS 判断
|
||||||
|
clientDns := []ValData{}
|
||||||
|
for _, v := range g.ClientDns {
|
||||||
|
if v.Val != "" {
|
||||||
|
ip := net.ParseIP(v.Val)
|
||||||
|
if ip.String() != v.Val {
|
||||||
|
return errors.New("DNS IP 错误")
|
||||||
|
}
|
||||||
|
clientDns = append(clientDns, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 是否默认路由
|
||||||
|
isDefRoute := len(routeInclude) == 0 || (len(routeInclude) == 1 && routeInclude[0].Val == "all")
|
||||||
|
if isDefRoute && len(clientDns) == 0 {
|
||||||
|
return errors.New("默认路由,必须设置一个DNS")
|
||||||
|
}
|
||||||
|
g.ClientDns = clientDns
|
||||||
|
// 域名拆分隧道,不能同时填写
|
||||||
|
g.DsIncludeDomains = strings.TrimSpace(g.DsIncludeDomains)
|
||||||
|
g.DsExcludeDomains = strings.TrimSpace(g.DsExcludeDomains)
|
||||||
|
if g.DsIncludeDomains != "" && g.DsExcludeDomains != "" {
|
||||||
|
return errors.New("包含/排除域名不能同时填写")
|
||||||
|
}
|
||||||
|
// 校验包含域名的格式
|
||||||
|
err = CheckDomainNames(g.DsIncludeDomains)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("包含域名有误:" + err.Error())
|
||||||
|
}
|
||||||
|
// 校验排除域名的格式
|
||||||
|
err = CheckDomainNames(g.DsExcludeDomains)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("排除域名有误:" + err.Error())
|
||||||
|
}
|
||||||
|
if isDefRoute && g.DsIncludeDomains != "" {
|
||||||
|
return errors.New("默认路由, 不允许设置\"包含域名\", 请重新配置")
|
||||||
|
}
|
||||||
|
// 处理登入方式的逻辑
|
||||||
|
defAuth := map[string]interface{}{
|
||||||
|
"type": "local",
|
||||||
|
}
|
||||||
|
if len(g.Auth) == 0 {
|
||||||
|
g.Auth = defAuth
|
||||||
|
}
|
||||||
|
authType := g.Auth["type"].(string)
|
||||||
|
if authType == "local" {
|
||||||
|
g.Auth = defAuth
|
||||||
|
} else {
|
||||||
|
if _, ok := authRegistry[authType]; !ok {
|
||||||
|
return errors.New("未知的认证方式: " + authType)
|
||||||
|
}
|
||||||
|
auth := makeInstance(authType).(IUserAuth)
|
||||||
|
err = auth.checkData(g.Auth)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// 重置Auth, 删除多余的key
|
||||||
|
g.Auth = map[string]interface{}{
|
||||||
|
"type": authType,
|
||||||
|
authType: g.Auth[authType],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
g.UpdatedAt = time.Now()
|
||||||
|
if g.Id > 0 {
|
||||||
|
err = Set(g)
|
||||||
|
} else {
|
||||||
|
err = Add(g)
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseIpNet(s string) (string, *net.IPNet, error) {
|
||||||
|
ip, ipNet, err := net.ParseCIDR(s)
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
mask := net.IP(ipNet.Mask)
|
||||||
|
ipMask := fmt.Sprintf("%s/%s", ip, mask)
|
||||||
|
|
||||||
|
return ipMask, ipNet, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckDomainNames(domains string) error {
|
||||||
|
if domains == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
strLen := 0
|
||||||
|
str_slice := strings.Split(domains, ",")
|
||||||
|
for _, val := range str_slice {
|
||||||
|
if val == "" {
|
||||||
|
return errors.New(val + " 请以逗号分隔域名")
|
||||||
|
}
|
||||||
|
if !ValidateDomainName(val) {
|
||||||
|
return errors.New(val + " 域名有误")
|
||||||
|
}
|
||||||
|
strLen += len(val)
|
||||||
|
}
|
||||||
|
if strLen > DsMaxLen {
|
||||||
|
p := message.NewPrinter(language.English)
|
||||||
|
return fmt.Errorf("字符长度超出限制,最大%s个(不包含逗号), 请删减一些域名", p.Sprintf("%d", DsMaxLen))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ValidateDomainName(domain string) bool {
|
||||||
|
RegExp := regexp.MustCompile(`^([a-zA-Z0-9][-a-zA-Z0-9]{0,62}\.)+[A-Za-z]{2,18}$`)
|
||||||
|
return RegExp.MatchString(domain)
|
||||||
|
}
|
74
server/dbdata/group_test.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package dbdata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/bjdgyc/anylink/pkg/utils"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetGroupNames(t *testing.T) {
|
||||||
|
ast := assert.New(t)
|
||||||
|
|
||||||
|
preIpData()
|
||||||
|
defer closeIpdata()
|
||||||
|
|
||||||
|
// 添加 group
|
||||||
|
g1 := Group{Name: "g1", ClientDns: []ValData{{Val: "114.114.114.114"}}}
|
||||||
|
err := SetGroup(&g1)
|
||||||
|
ast.Nil(err)
|
||||||
|
g2 := Group{Name: "g2", ClientDns: []ValData{{Val: "114.114.114.114"}}}
|
||||||
|
err = SetGroup(&g2)
|
||||||
|
ast.Nil(err)
|
||||||
|
g3 := Group{Name: "g3", ClientDns: []ValData{{Val: "114.114.114.114"}}}
|
||||||
|
err = SetGroup(&g3)
|
||||||
|
ast.Nil(err)
|
||||||
|
|
||||||
|
authData := map[string]interface{}{
|
||||||
|
"type": "radius",
|
||||||
|
"radius": map[string]string{
|
||||||
|
"addr": "192.168.8.12:1044",
|
||||||
|
"secret": "43214132",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
g4 := Group{Name: "g4", ClientDns: []ValData{{Val: "114.114.114.114"}}, Auth: authData}
|
||||||
|
err = SetGroup(&g4)
|
||||||
|
ast.Nil(err)
|
||||||
|
g5 := Group{Name: "g5", ClientDns: []ValData{{Val: "114.114.114.114"}}, DsIncludeDomains: "baidu.com,163.com"}
|
||||||
|
err = SetGroup(&g5)
|
||||||
|
if ast.NotNil(err) {
|
||||||
|
ast.Equal("默认路由, 不允许设置\"包含域名\", 请重新配置", err.Error())
|
||||||
|
}
|
||||||
|
g6 := Group{Name: "g6", ClientDns: []ValData{{Val: "114.114.114.114"}}, DsExcludeDomains: "com.cn,qq.com"}
|
||||||
|
err = SetGroup(&g6)
|
||||||
|
ast.Nil(err)
|
||||||
|
|
||||||
|
authData = map[string]interface{}{
|
||||||
|
"type": "ldap",
|
||||||
|
"ldap": map[string]interface{}{
|
||||||
|
"addr": "192.168.8.12:389",
|
||||||
|
"tls": true,
|
||||||
|
"bind_name": "userfind@abc.com",
|
||||||
|
"bind_pwd": "afdbfdsafds",
|
||||||
|
"base_dn": "dc=abc,dc=com",
|
||||||
|
"search_attr": "sAMAccountName",
|
||||||
|
"member_of": "cn=vpn,cn=user,dc=abc,dc=com",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
g7 := Group{Name: "g7", ClientDns: []ValData{{Val: "114.114.114.114"}}, Auth: authData}
|
||||||
|
err = SetGroup(&g7)
|
||||||
|
ast.Nil(err)
|
||||||
|
|
||||||
|
// 判断所有数据
|
||||||
|
gAll := []string{"g1", "g2", "g3", "g4", "g5", "g6", "g7"}
|
||||||
|
gs := GetGroupNames()
|
||||||
|
for _, v := range gs {
|
||||||
|
ast.Equal(true, utils.InArrStr(gAll, v))
|
||||||
|
}
|
||||||
|
|
||||||
|
gni := GetGroupNamesIds()
|
||||||
|
for _, v := range gni {
|
||||||
|
ast.NotEqual(0, v.Id)
|
||||||
|
ast.Equal(true, utils.InArrStr(gAll, v.Name))
|
||||||
|
}
|
||||||
|
}
|
34
server/dbdata/ip_map.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package dbdata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// type IpMap struct {
|
||||||
|
// Id int `json:"id" xorm:"pk autoincr not null"`
|
||||||
|
// IpAddr string `json:"ip_addr" xorm:"not null unique"`
|
||||||
|
// MacAddr string `json:"mac_addr" xorm:"not null unique"`
|
||||||
|
// Username string `json:"username"`
|
||||||
|
// Keep bool `json:"keep"` // 保留 ip-mac 绑定
|
||||||
|
// KeepTime time.Time `json:"keep_time"`
|
||||||
|
// Note string `json:"note"` // 备注
|
||||||
|
// LastLogin time.Time `json:"last_login"`
|
||||||
|
// UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
// }
|
||||||
|
|
||||||
|
func SetIpMap(v *IpMap) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if len(v.IpAddr) < 4 || len(v.MacAddr) < 6 {
|
||||||
|
return errors.New("IP或MAC错误")
|
||||||
|
}
|
||||||
|
|
||||||
|
v.UpdatedAt = time.Now()
|
||||||
|
if v.Id > 0 {
|
||||||
|
err = Set(v)
|
||||||
|
} else {
|
||||||
|
err = Add(v)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
101
server/dbdata/policy.go
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
package dbdata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetPolicy(Username string) *Policy {
|
||||||
|
policyData := &Policy{}
|
||||||
|
err := One("Username", Username, policyData)
|
||||||
|
if err != nil {
|
||||||
|
return policyData
|
||||||
|
}
|
||||||
|
return policyData
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetPolicy(p *Policy) error {
|
||||||
|
var err error
|
||||||
|
if p.Username == "" {
|
||||||
|
return errors.New("用户名错误")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 包含路由
|
||||||
|
routeInclude := []ValData{}
|
||||||
|
for _, v := range p.RouteInclude {
|
||||||
|
if v.Val != "" {
|
||||||
|
if v.Val == All {
|
||||||
|
routeInclude = append(routeInclude, v)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
ipMask, _, err := parseIpNet(v.Val)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("RouteInclude 错误" + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
v.IpMask = ipMask
|
||||||
|
routeInclude = append(routeInclude, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p.RouteInclude = routeInclude
|
||||||
|
// 排除路由
|
||||||
|
routeExclude := []ValData{}
|
||||||
|
for _, v := range p.RouteExclude {
|
||||||
|
if v.Val != "" {
|
||||||
|
ipMask, _, err := parseIpNet(v.Val)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("RouteExclude 错误" + err.Error())
|
||||||
|
}
|
||||||
|
v.IpMask = ipMask
|
||||||
|
routeExclude = append(routeExclude, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p.RouteExclude = routeExclude
|
||||||
|
|
||||||
|
// DNS 判断
|
||||||
|
clientDns := []ValData{}
|
||||||
|
for _, v := range p.ClientDns {
|
||||||
|
if v.Val != "" {
|
||||||
|
ip := net.ParseIP(v.Val)
|
||||||
|
if ip.String() != v.Val {
|
||||||
|
return errors.New("DNS IP 错误")
|
||||||
|
}
|
||||||
|
clientDns = append(clientDns, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(routeInclude) == 0 || (len(routeInclude) == 1 && routeInclude[0].Val == "all") {
|
||||||
|
if len(clientDns) == 0 {
|
||||||
|
return errors.New("默认路由,必须设置一个DNS")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p.ClientDns = clientDns
|
||||||
|
|
||||||
|
// 域名拆分隧道,不能同时填写
|
||||||
|
p.DsIncludeDomains = strings.TrimSpace(p.DsIncludeDomains)
|
||||||
|
p.DsExcludeDomains = strings.TrimSpace(p.DsExcludeDomains)
|
||||||
|
if p.DsIncludeDomains != "" && p.DsExcludeDomains != "" {
|
||||||
|
return errors.New("包含/排除域名不能同时填写")
|
||||||
|
}
|
||||||
|
// 校验包含域名的格式
|
||||||
|
err = CheckDomainNames(p.DsIncludeDomains)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("包含域名有误:" + err.Error())
|
||||||
|
}
|
||||||
|
// 校验排除域名的格式
|
||||||
|
err = CheckDomainNames(p.DsExcludeDomains)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("排除域名有误:" + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
p.UpdatedAt = time.Now()
|
||||||
|
if p.Id > 0 {
|
||||||
|
err = Set(p)
|
||||||
|
} else {
|
||||||
|
err = Add(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
45
server/dbdata/policy_test.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package dbdata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetPolicy(t *testing.T) {
|
||||||
|
ast := assert.New(t)
|
||||||
|
|
||||||
|
preIpData()
|
||||||
|
defer closeIpdata()
|
||||||
|
|
||||||
|
// 添加 Policy
|
||||||
|
p1 := Policy{Username: "a1", ClientDns: []ValData{{Val: "114.114.114.114"}}, DsExcludeDomains: "baidu.com,163.com"}
|
||||||
|
err := SetPolicy(&p1)
|
||||||
|
ast.Nil(err)
|
||||||
|
|
||||||
|
p2 := Policy{Username: "a2", ClientDns: []ValData{{Val: "114.114.114.114"}}, DsExcludeDomains: "com.cn,qq.com"}
|
||||||
|
err = SetPolicy(&p2)
|
||||||
|
ast.Nil(err)
|
||||||
|
|
||||||
|
route := []ValData{{Val: "192.168.1.1/24"}}
|
||||||
|
p3 := Policy{Username: "a3", ClientDns: []ValData{{Val: "114.114.114.114"}}, RouteInclude: route, DsExcludeDomains: "com.cn,qq.com"}
|
||||||
|
err = SetPolicy(&p3)
|
||||||
|
ast.Nil(err)
|
||||||
|
// 判断 IpMask
|
||||||
|
ast.Equal(p3.RouteInclude[0].IpMask, "192.168.1.1/255.255.255.0")
|
||||||
|
|
||||||
|
route2 := []ValData{{Val: "192.168.2.1/24"}}
|
||||||
|
p4 := Policy{Username: "a4", ClientDns: []ValData{{Val: "114.114.114.114"}}, RouteExclude: route2, DsIncludeDomains: "com.cn,qq.com"}
|
||||||
|
err = SetPolicy(&p4)
|
||||||
|
ast.Nil(err)
|
||||||
|
// 判断 IpMask
|
||||||
|
ast.Equal(p4.RouteExclude[0].IpMask, "192.168.2.1/255.255.255.0")
|
||||||
|
|
||||||
|
// 判断所有数据
|
||||||
|
var userPolicy *Policy
|
||||||
|
pAll := []string{"a1", "a2", "a3", "a4"}
|
||||||
|
for _, v := range pAll {
|
||||||
|
userPolicy = GetPolicy(v)
|
||||||
|
ast.NotEqual(userPolicy.Id, 0, "user policy id is zero")
|
||||||
|
}
|
||||||
|
}
|
100
server/dbdata/setting.go
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
package dbdata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"xorm.io/xorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SettingInstall struct {
|
||||||
|
Installed bool `json:"installed"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SettingSmtp struct {
|
||||||
|
Host string `json:"host"`
|
||||||
|
Port int `json:"port"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
From string `json:"from"`
|
||||||
|
Encryption string `json:"encryption"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SettingAuditLog struct {
|
||||||
|
AuditInterval int `json:"audit_interval"`
|
||||||
|
LifeDay int `json:"life_day"`
|
||||||
|
ClearTime string `json:"clear_time"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SettingOther struct {
|
||||||
|
LinkAddr string `json:"link_addr"`
|
||||||
|
Banner string `json:"banner"`
|
||||||
|
Homeindex string `json:"homeindex"`
|
||||||
|
AccountMail string `json:"account_mail"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func StructName(data interface{}) string {
|
||||||
|
ref := reflect.ValueOf(data)
|
||||||
|
s := &ref
|
||||||
|
if s.Kind() == reflect.Ptr {
|
||||||
|
e := s.Elem()
|
||||||
|
s = &e
|
||||||
|
}
|
||||||
|
name := s.Type().Name()
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
|
func SettingSessAdd(sess *xorm.Session, data interface{}) error {
|
||||||
|
name := StructName(data)
|
||||||
|
v, _ := json.Marshal(data)
|
||||||
|
s := &Setting{Name: name, Data: v}
|
||||||
|
_, err := sess.InsertOne(s)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func SettingSet(data interface{}) error {
|
||||||
|
name := StructName(data)
|
||||||
|
v, _ := json.Marshal(data)
|
||||||
|
s := &Setting{Data: v}
|
||||||
|
err := Update("name", name, s)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func SettingGet(data interface{}) error {
|
||||||
|
name := StructName(data)
|
||||||
|
s := &Setting{Name: name}
|
||||||
|
err := One("name", name, s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(s.Data, data)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func SettingGetAuditLog() (SettingAuditLog, error) {
|
||||||
|
data := SettingAuditLog{}
|
||||||
|
err := SettingGet(&data)
|
||||||
|
if err == nil {
|
||||||
|
return data, err
|
||||||
|
}
|
||||||
|
if !CheckErrNotFound(err) {
|
||||||
|
return data, err
|
||||||
|
}
|
||||||
|
sess := xdb.NewSession()
|
||||||
|
defer sess.Close()
|
||||||
|
auditLog := SettingGetAuditLogDefault()
|
||||||
|
err = SettingSessAdd(sess, auditLog)
|
||||||
|
if err != nil {
|
||||||
|
return data, err
|
||||||
|
}
|
||||||
|
return auditLog, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SettingGetAuditLogDefault() SettingAuditLog {
|
||||||
|
auditLog := SettingAuditLog{
|
||||||
|
LifeDay: 0,
|
||||||
|
ClearTime: "05:00",
|
||||||
|
}
|
||||||
|
return auditLog
|
||||||
|
}
|
@@ -6,5 +6,5 @@ func Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Stop() error {
|
func Stop() error {
|
||||||
return sdb.Close()
|
return xdb.Close()
|
||||||
}
|
}
|
247
server/dbdata/statsinfo.go
Normal file
@@ -0,0 +1,247 @@
|
|||||||
|
package dbdata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"container/list"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/bjdgyc/anylink/base"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
LayoutTimeFormat = "2006-01-02 15:04:05"
|
||||||
|
LayoutTimeFormatMin = "2006-01-02 15:04"
|
||||||
|
RealTimeMaxSize = 120 // 实时数据最大保存条数
|
||||||
|
)
|
||||||
|
|
||||||
|
type StatsInfo struct {
|
||||||
|
RealtimeData map[string]*list.List
|
||||||
|
Actions []string
|
||||||
|
Scopes []string
|
||||||
|
mux sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
type ScopeDetail struct {
|
||||||
|
sTime time.Time
|
||||||
|
eTime time.Time
|
||||||
|
minutes int
|
||||||
|
fsTime string
|
||||||
|
feTime string
|
||||||
|
}
|
||||||
|
|
||||||
|
var StatsInfoIns *StatsInfo
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
StatsInfoIns = &StatsInfo{
|
||||||
|
Actions: []string{"online", "network", "cpu", "mem"},
|
||||||
|
Scopes: []string{"rt", "1h", "24h", "3d", "7d", "30d"},
|
||||||
|
RealtimeData: make(map[string]*list.List),
|
||||||
|
}
|
||||||
|
for _, v := range StatsInfoIns.Actions {
|
||||||
|
StatsInfoIns.RealtimeData[v] = list.New()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验统计类型值
|
||||||
|
func (s *StatsInfo) ValidAction(action string) bool {
|
||||||
|
for _, item := range s.Actions {
|
||||||
|
if item == action {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验日期范围值
|
||||||
|
func (s *StatsInfo) ValidScope(scope string) bool {
|
||||||
|
for _, item := range s.Scopes {
|
||||||
|
if item == scope {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置实时统计数据
|
||||||
|
func (s *StatsInfo) SetRealTime(action string, val interface{}) {
|
||||||
|
s.mux.Lock()
|
||||||
|
defer s.mux.Unlock()
|
||||||
|
|
||||||
|
if s.RealtimeData[action].Len() >= RealTimeMaxSize {
|
||||||
|
ele := s.RealtimeData[action].Front()
|
||||||
|
s.RealtimeData[action].Remove(ele)
|
||||||
|
}
|
||||||
|
s.RealtimeData[action].PushBack(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取实时统计数据
|
||||||
|
func (s *StatsInfo) GetRealTime(action string) (res []interface{}) {
|
||||||
|
s.mux.Lock()
|
||||||
|
defer s.mux.Unlock()
|
||||||
|
|
||||||
|
for e := s.RealtimeData[action].Front(); e != nil; e = e.Next() {
|
||||||
|
res = append(res, e.Value)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存数据至数据库
|
||||||
|
func (s *StatsInfo) SaveStatsInfo(so StatsOnline, sn StatsNetwork, sc StatsCpu, sm StatsMem) {
|
||||||
|
if so.Num != 0 {
|
||||||
|
_ = Add(so)
|
||||||
|
}
|
||||||
|
if sn.Up != 0 || sn.Down != 0 {
|
||||||
|
_ = Add(sn)
|
||||||
|
}
|
||||||
|
if sc.Percent != 0 {
|
||||||
|
_ = Add(sc)
|
||||||
|
}
|
||||||
|
if sm.Percent != 0 {
|
||||||
|
_ = Add(sm)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取统计数据
|
||||||
|
func (s *StatsInfo) GetData(action string, scope string) (res []interface{}, err error) {
|
||||||
|
if scope == "rt" {
|
||||||
|
return s.GetRealTime(action), nil
|
||||||
|
}
|
||||||
|
statsMaps := make(map[string]interface{})
|
||||||
|
currSec := fmt.Sprintf("%02d", time.Now().Second())
|
||||||
|
|
||||||
|
// 获取时间段数据
|
||||||
|
sd := s.getScopeDetail(scope)
|
||||||
|
timeList := s.getTimeList(sd)
|
||||||
|
res = make([]interface{}, len(timeList))
|
||||||
|
|
||||||
|
// 获取数据库查询条件
|
||||||
|
where := s.getStatsWhere(sd)
|
||||||
|
if where == "" {
|
||||||
|
return nil, errors.New("不支持的数据库类型: " + base.Cfg.DbType)
|
||||||
|
}
|
||||||
|
// 查询数据表
|
||||||
|
switch action {
|
||||||
|
case "online":
|
||||||
|
statsRes := []StatsOnline{}
|
||||||
|
FindWhere(&statsRes, 0, 0, where, sd.fsTime, sd.feTime)
|
||||||
|
for _, v := range statsRes {
|
||||||
|
t := v.CreatedAt.Format(LayoutTimeFormatMin)
|
||||||
|
statsMaps[t] = v
|
||||||
|
}
|
||||||
|
case "network":
|
||||||
|
statsRes := []StatsNetwork{}
|
||||||
|
FindWhere(&statsRes, 0, 0, where, sd.fsTime, sd.feTime)
|
||||||
|
for _, v := range statsRes {
|
||||||
|
t := v.CreatedAt.Format(LayoutTimeFormatMin)
|
||||||
|
statsMaps[t] = v
|
||||||
|
}
|
||||||
|
case "cpu":
|
||||||
|
statsRes := []StatsCpu{}
|
||||||
|
FindWhere(&statsRes, 0, 0, where, sd.fsTime, sd.feTime)
|
||||||
|
for _, v := range statsRes {
|
||||||
|
t := v.CreatedAt.Format(LayoutTimeFormatMin)
|
||||||
|
statsMaps[t] = v
|
||||||
|
}
|
||||||
|
case "mem":
|
||||||
|
statsRes := []StatsMem{}
|
||||||
|
FindWhere(&statsRes, 0, 0, where, sd.fsTime, sd.feTime)
|
||||||
|
for _, v := range statsRes {
|
||||||
|
t := v.CreatedAt.Format(LayoutTimeFormatMin)
|
||||||
|
statsMaps[t] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 整合数据
|
||||||
|
for i, v := range timeList {
|
||||||
|
if mv, ok := statsMaps[v]; ok {
|
||||||
|
res[i] = mv
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
t, _ := time.ParseInLocation(LayoutTimeFormat, v+":"+currSec, time.Local)
|
||||||
|
switch action {
|
||||||
|
case "online":
|
||||||
|
res[i] = StatsOnline{CreatedAt: t}
|
||||||
|
case "network":
|
||||||
|
res[i] = StatsNetwork{CreatedAt: t}
|
||||||
|
case "cpu":
|
||||||
|
res[i] = StatsCpu{CreatedAt: t}
|
||||||
|
case "mem":
|
||||||
|
res[i] = StatsMem{CreatedAt: t}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取日期范围的明细值
|
||||||
|
func (s *StatsInfo) getScopeDetail(scope string) (sd *ScopeDetail) {
|
||||||
|
sd = &ScopeDetail{}
|
||||||
|
t := time.Now()
|
||||||
|
sd.eTime = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), 59, t.Nanosecond(), time.Local)
|
||||||
|
sd.minutes = 0
|
||||||
|
switch scope {
|
||||||
|
case "1h":
|
||||||
|
sd.sTime = sd.eTime.Add(-time.Minute * 60)
|
||||||
|
sd.minutes = 1
|
||||||
|
case "24h":
|
||||||
|
sd.sTime = sd.eTime.AddDate(0, 0, -1)
|
||||||
|
sd.minutes = 5
|
||||||
|
case "7d":
|
||||||
|
sd.sTime = sd.eTime.AddDate(0, 0, -7)
|
||||||
|
sd.minutes = 30
|
||||||
|
case "30d":
|
||||||
|
sd.sTime = sd.eTime.AddDate(0, 0, -30)
|
||||||
|
sd.minutes = 150
|
||||||
|
}
|
||||||
|
if sd.minutes != 0 {
|
||||||
|
sd.sTime = sd.sTime.Add(-time.Minute * time.Duration(sd.minutes))
|
||||||
|
}
|
||||||
|
sd.fsTime = sd.sTime.Format(LayoutTimeFormat)
|
||||||
|
sd.feTime = sd.eTime.Format(LayoutTimeFormat)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 针对日期范围进行拆解
|
||||||
|
func (s *StatsInfo) getTimeList(sd *ScopeDetail) []string {
|
||||||
|
subSec := int64(60 * sd.minutes)
|
||||||
|
count := (sd.eTime.Unix()-sd.sTime.Unix())/subSec - 1
|
||||||
|
eTime := sd.eTime.Unix() - subSec
|
||||||
|
timeLists := make([]string, count)
|
||||||
|
for i := count - 1; i >= 0; i-- {
|
||||||
|
timeLists[i] = time.Unix(eTime, 0).Format(LayoutTimeFormatMin)
|
||||||
|
eTime = eTime - subSec
|
||||||
|
}
|
||||||
|
return timeLists
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取where条件
|
||||||
|
func (s *StatsInfo) getStatsWhere(sd *ScopeDetail) (where string) {
|
||||||
|
where = "created_at BETWEEN ? AND ?"
|
||||||
|
min := strconv.Itoa(sd.minutes)
|
||||||
|
switch base.Cfg.DbType {
|
||||||
|
case "mysql":
|
||||||
|
where += " AND floor(TIMESTAMPDIFF(SECOND, created_at, '" + sd.feTime + "') / 60) % " + min + " = 0"
|
||||||
|
case "sqlite3":
|
||||||
|
where += " AND CAST(ROUND((JULIANDAY('" + sd.feTime + "') - JULIANDAY(created_at)) * 86400) / 60 as integer) % " + min + " = 0"
|
||||||
|
case "postgres":
|
||||||
|
where += " AND floor((EXTRACT(EPOCH FROM " + sd.feTime + ") - EXTRACT(EPOCH FROM created_at)) / 60) % " + min + " = 0"
|
||||||
|
default:
|
||||||
|
where = ""
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StatsInfo) ClearStatsInfo(action string, ts string) (affected int64, err error) {
|
||||||
|
switch action {
|
||||||
|
case "online":
|
||||||
|
affected, err = xdb.Where("created_at < '" + ts + "'").Delete(&StatsOnline{})
|
||||||
|
case "network":
|
||||||
|
affected, err = xdb.Where("created_at < '" + ts + "'").Delete(&StatsNetwork{})
|
||||||
|
case "cpu":
|
||||||
|
affected, err = xdb.Where("created_at < '" + ts + "'").Delete(&StatsCpu{})
|
||||||
|
case "mem":
|
||||||
|
affected, err = xdb.Where("created_at < '" + ts + "'").Delete(&StatsMem{})
|
||||||
|
}
|
||||||
|
return affected, err
|
||||||
|
}
|