Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
90a3409aac | ||
|
|
5a0fe14f23 | ||
|
|
2629078988 | ||
|
|
b46e35cf7e | ||
|
|
5e356c0422 | ||
|
|
8311c8bd77 | ||
|
|
4899bbd9d5 | ||
|
|
b558cc6d19 | ||
|
|
32ce0e3dd6 | ||
|
|
ab629b5730 | ||
|
|
899d1e9e2e | ||
|
|
ae28d41231 | ||
|
|
eab5708b33 | ||
|
|
31af2bb6db | ||
|
|
29ec73c80b | ||
|
|
a59a0922ca | ||
|
|
186f2e228d | ||
|
|
58257be867 | ||
|
|
90ff08ed98 | ||
|
|
d930d9f003 | ||
|
|
72b61c4c4f | ||
|
|
39921584df | ||
|
|
55a8466d83 | ||
|
|
0d94b7c39e | ||
|
|
4ef73d7fe9 | ||
|
|
8d93fbcca9 | ||
|
|
885f1b0d49 | ||
|
|
ba081c02c2 | ||
|
|
def85f883b | ||
|
|
ab67dd5b8a | ||
|
|
90e78bf413 | ||
|
|
f70a0dab5f | ||
|
|
fcb3864558 | ||
|
|
12c101a122 | ||
|
|
3b24752e06 | ||
|
|
a4e7461f21 | ||
|
|
f86b4c873e | ||
|
|
4c964af58a | ||
|
|
a1200ae0ec | ||
|
|
3354a48c35 | ||
|
|
fd3ae7fa9b | ||
|
|
6ebd5e35bd | ||
|
|
28f5bdea8e | ||
|
|
fa3beabfaf | ||
|
|
f1a7a8c9d7 | ||
|
|
acd647b4a2 | ||
|
|
c759ceff49 | ||
|
|
347d59ec8d | ||
|
|
a1482b15d4 | ||
|
|
40c6f014be | ||
|
|
a3f8e59c39 | ||
|
|
cf9644e902 | ||
|
|
73d6c1dc1c | ||
|
|
8a28d243df | ||
|
|
6703a98419 | ||
|
|
2f039ace9d | ||
|
|
999fac8ad7 | ||
|
|
ac38d342fa | ||
|
|
5b1774c7b7 | ||
|
|
a3435c4c42 | ||
|
|
daaf1f28e1 | ||
|
|
a602b11270 | ||
|
|
f938b2fdd1 | ||
|
|
9993ce8131 | ||
|
|
dfeaf523ee | ||
|
|
3989463beb | ||
|
|
4ad17ba869 | ||
|
|
3fa7590400 | ||
|
|
f85c5cd9ad | ||
|
|
528386cfd6 | ||
|
|
9268e67529 | ||
|
|
614d81f374 | ||
|
|
883dd07d86 | ||
|
|
4b3834e46d | ||
|
|
2dc902fba1 | ||
|
|
f97a9176e7 | ||
|
|
c7e7d2bac6 | ||
|
|
e33b0be3b8 | ||
|
|
f365973bb9 | ||
|
|
d66f8724e9 | ||
|
|
cb1cc67e6b | ||
|
|
34fcd15101 | ||
|
|
391fdbd920 | ||
|
|
036a17a3d9 | ||
|
|
bc23c53981 | ||
|
|
50180e5b7f | ||
|
|
5da5bf1628 | ||
|
|
fd71e0821f | ||
|
|
627ea612e6 | ||
|
|
eccf3c2be1 | ||
|
|
3e26af308c | ||
|
|
5d2b3a53a0 | ||
|
|
1dca14fc01 | ||
|
|
78c71c2fa3 | ||
|
|
6de4877f24 | ||
|
|
509c81a2b5 | ||
|
|
01a4bcf13d | ||
|
|
08f7c1844a | ||
|
|
5a85e47646 | ||
|
|
fe11bf9398 | ||
|
|
829e3a1e47 | ||
|
|
2fc39c52ae | ||
|
|
24005c490d | ||
|
|
b902b5d373 | ||
|
|
6ba3598317 | ||
|
|
179aba622f | ||
|
|
d415b8747b | ||
|
|
27df8a1b51 | ||
|
|
7838ee66fd | ||
|
|
c24a3765a9 | ||
|
|
6f55c8095e | ||
|
|
a6a011c2fa | ||
|
|
adfd5d8179 | ||
|
|
72c14d949b | ||
|
|
12b4545889 | ||
|
|
f1cb45146f | ||
|
|
8d27a17cdb | ||
|
|
32325f70ab | ||
|
|
e706c956e6 | ||
|
|
dce055255c | ||
|
|
20ffb44994 | ||
|
|
04c0d59454 | ||
|
|
8a35139742 | ||
|
|
ff57f95755 | ||
|
|
2f6059908d | ||
|
|
0c9bebd764 | ||
|
|
10529f54f1 | ||
|
|
04ed043146 | ||
|
|
2318944315 | ||
|
|
ddc867d3cc | ||
|
|
37db3c836b | ||
|
|
2e5b1bd1e3 | ||
|
|
91c1ffc6ce | ||
|
|
927038fd4c | ||
|
|
674a178506 | ||
|
|
88dcea59ec | ||
|
|
bdf12fe22a | ||
|
|
9bb847df87 | ||
|
|
63543b5675 | ||
|
|
9abe5874b7 | ||
|
|
d888511f60 | ||
|
|
605f4aface | ||
|
|
e429b17e63 | ||
|
|
4381eb44b4 | ||
|
|
f352ee8f39 | ||
|
|
fadfc24e52 | ||
|
|
78ebeba1bb | ||
|
|
32e3d411ad | ||
|
|
405f41f87f | ||
|
|
9bb8230fc1 | ||
|
|
bd87864e26 | ||
|
|
bfe08e5114 | ||
|
|
aaf79b21f0 | ||
|
|
592c39fb1d | ||
|
|
2ca84c912b | ||
|
|
bc1783cfb6 | ||
|
|
a4bdbb2b54 | ||
|
|
f362c96e1e | ||
|
|
5f58c34c8b | ||
|
|
5f35415849 | ||
|
|
442354db17 | ||
|
|
23b023c562 | ||
|
|
c2f4510a0f | ||
|
|
d23ed2126d | ||
|
|
18ca5d04cc | ||
|
|
f5d5f63366 | ||
|
|
1f8e441090 | ||
|
|
4c8d7b0738 | ||
|
|
d9f9dc6acb | ||
|
|
b3ed822c72 | ||
|
|
d5488f8aa3 | ||
|
|
8bcf5313f4 | ||
|
|
c062cc414a | ||
|
|
a61ff2d200 | ||
|
|
0de2c95934 | ||
|
|
847f0ce1d4 | ||
|
|
a28d7319d8 | ||
|
|
f0201c1039 | ||
|
|
9351c94f35 | ||
|
|
02be614149 | ||
|
|
ee50a67f03 | ||
|
|
445ed1dd60 | ||
|
|
47527d1512 | ||
|
|
af8d4a8c12 | ||
|
|
1bf4cf2347 | ||
|
|
d4a6560d9a | ||
|
|
6157b1a528 | ||
|
|
11d185ad59 | ||
|
|
a05995fba5 | ||
|
|
1c1be202b7 | ||
|
|
5e55b761cf | ||
|
|
fce53fa308 | ||
|
|
9d3df6be7e | ||
|
|
89df38422c | ||
|
|
3413ceb7c2 | ||
|
|
51a3787708 | ||
|
|
bb2cffe10a | ||
|
|
41c282b38b | ||
|
|
53c2e472ae | ||
|
|
4bb7e33b16 | ||
|
|
f35a73f734 | ||
|
|
00900c1315 | ||
|
|
18c11f108b | ||
|
|
3cc4234117 | ||
|
|
fe79fe9fc7 | ||
|
|
57c49089de | ||
|
|
d35e3510a9 | ||
|
|
7178b33807 | ||
|
|
b0d16d3b3d | ||
|
|
5fcbeb60aa | ||
|
|
0435dce9cd | ||
|
|
25e71790c1 | ||
|
|
6aea46e6d5 | ||
|
|
30fc6d1689 | ||
|
|
8078d712f9 | ||
|
|
144f102935 | ||
|
|
383dbd1b7b | ||
|
|
4b7b2f4c27 | ||
|
|
fe27d4c9c7 | ||
|
|
41317a4ef9 | ||
|
|
2912246572 | ||
|
|
f6c596f318 | ||
|
|
45521d5680 | ||
|
|
89f1e72c50 | ||
|
|
4a7a0a7b75 | ||
|
|
6a978515ca | ||
|
|
0cc0e82c5d | ||
|
|
c01c61fc6b | ||
|
|
4831e17b38 | ||
|
|
3cb104cd1b | ||
|
|
60c8b0c7bf | ||
|
|
16c97a3c36 | ||
|
|
1e8180014d | ||
|
|
2330935b90 | ||
|
|
7ea9001aa4 | ||
|
|
694ebc5477 | ||
|
|
824b12a2f8 | ||
|
|
0c87b4119a | ||
|
|
f0f50579fc | ||
|
|
a831ccd8fa | ||
|
|
dd65e32fb5 | ||
|
|
b1b91b0c53 | ||
|
|
c29dd2ad36 | ||
|
|
f56b9ea883 | ||
|
|
d69ce38604 | ||
|
|
92ea594ccf | ||
|
|
5bbf247863 | ||
|
|
2b841adb1b | ||
|
|
5fd335f330 | ||
|
|
cd7f99063c | ||
|
|
58cb05e302 | ||
|
|
cc6d053b6d | ||
|
|
4b0aebd6a5 | ||
|
|
07b896550e | ||
|
|
886886197d | ||
|
|
4aefb4ba8a | ||
|
|
3d30f7a4b0 | ||
|
|
b09d3ec22c | ||
|
|
b43abb4b5d | ||
|
|
d42710497c | ||
|
|
42a73fa392 | ||
|
|
00a4a33c5f | ||
|
|
006ddbc871 | ||
|
|
69e60a3337 | ||
|
|
5beee738df | ||
|
|
8fa3e8f785 | ||
|
|
65e0822b63 | ||
|
|
7edc65cfde | ||
|
|
ff030b9c1c | ||
|
|
7637cd448e | ||
|
|
2a5a45a700 | ||
|
|
ec9439564e | ||
|
|
a0e599d96a | ||
|
|
55fd6ec406 | ||
|
|
e9256ab9aa | ||
|
|
e4b08b1b82 | ||
|
|
62bc3c856f | ||
|
|
f0493543ff | ||
|
|
1b475b123c | ||
|
|
b189fb1b6e | ||
|
|
328a068956 | ||
|
|
efa341c7e8 | ||
|
|
f43942413e | ||
|
|
24bd9ce6ba | ||
|
|
a4d429bbc1 | ||
|
|
c0e2b6283c | ||
|
|
a66b465046 | ||
|
|
7ec3e82b0f | ||
|
|
7e93497f1a | ||
|
|
e24b2921ac | ||
|
|
572dcd2aab | ||
|
|
97330bfbdc | ||
|
|
f78e81b452 | ||
|
|
d45bc3c066 | ||
|
|
f81fb7760e | ||
|
|
149b2e467c | ||
|
|
a29a7d4923 | ||
|
|
0d257a95dd | ||
|
|
ad22ada1f1 | ||
|
|
948d931883 | ||
|
|
8afa6b2929 | ||
|
|
7a49e48b89 | ||
|
|
0819afc9da | ||
|
|
eecf1c6c69 | ||
|
|
9428457487 | ||
|
|
f3e90f146d | ||
|
|
7bf54b608d | ||
|
|
496d9f52f9 | ||
|
|
d97b18f51f | ||
|
|
037e7b28f1 | ||
|
|
b3660c3b8d | ||
|
|
cbc3bd2afe | ||
|
|
5fc51f40a7 | ||
|
|
1c1aa5ec5b | ||
|
|
f526c56784 | ||
|
|
fbbd4bace2 | ||
|
|
534d428c6d | ||
|
|
0492268190 | ||
|
|
01fca7d568 | ||
|
|
0711b9befb | ||
|
|
2c13a02bdc | ||
|
|
204c53ddd3 | ||
|
|
6788f24821 | ||
|
|
e65ab33749 | ||
|
|
7f94b3871a | ||
|
|
aa5b869da9 | ||
|
|
f7898e019e | ||
|
|
ca04f19275 | ||
|
|
db43405237 | ||
|
|
750ecb824a | ||
|
|
2c608ddb7f | ||
|
|
934402e97d | ||
|
|
6fe69bc6b1 | ||
|
|
dab51c32a2 | ||
|
|
48c7309973 | ||
|
|
f2329f72f6 | ||
|
|
9f03c2f6eb | ||
|
|
7a8cb3d5b6 | ||
|
|
3b18d66835 | ||
|
|
9f6b33a62b | ||
|
|
44d314515b | ||
|
|
59d789d253 | ||
|
|
2e8af6f120 | ||
|
|
7af09a2f4c | ||
|
|
74b262503e | ||
|
|
87f2c8b2ce | ||
|
|
cbdad1adb0 | ||
|
|
73c297df8e | ||
|
|
da899fd3db | ||
|
|
662a799f02 | ||
|
|
1a415475ce | ||
|
|
8115784059 | ||
|
|
ffc4961d0c | ||
|
|
4f4ce71654 | ||
|
|
bb7a12f5a2 | ||
|
|
bdc7718dc3 | ||
|
|
ddeae71bc7 | ||
|
|
21de0a0ada | ||
|
|
c336bf94d8 | ||
|
|
4e6754deb9 | ||
|
|
eccc221e67 | ||
|
|
717028e5f1 | ||
|
|
1e215d00a6 | ||
|
|
f0c6eff23a | ||
|
|
b3dd70062b | ||
|
|
34faa8788f | ||
|
|
eccf1dbfb8 | ||
|
|
2af7b3d737 | ||
|
|
40184141e8 | ||
|
|
0b90bf3a18 | ||
|
|
c34e5e1a7d | ||
|
|
c533436c78 | ||
|
|
cc90bcf4e9 | ||
|
|
53e57ea70d | ||
|
|
72290275cc | ||
|
|
901048d1e6 | ||
|
|
a94cef65a1 | ||
|
|
2ad50ebb46 | ||
|
|
851241a0c7 | ||
|
|
bb882f348a | ||
|
|
0ce6ab1e15 | ||
|
|
a67cdff02a | ||
|
|
26c7ba96af | ||
|
|
55b0c4b98b | ||
|
|
03aa58cd77 | ||
|
|
478226e479 | ||
|
|
69ad2d3761 | ||
|
|
f3435ebd40 | ||
|
|
61768ae0aa | ||
|
|
6c3c09428e | ||
|
|
12ec5d6b26 | ||
|
|
60b5ea2959 | ||
|
|
1f61b99387 | ||
|
|
dcd21f211d | ||
|
|
05e66af647 |
@@ -0,0 +1,38 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
title: ''
|
||||
labels: bug
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Describe the bug**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
**To Reproduce**
|
||||
Steps to reproduce the behavior:
|
||||
1. Opening '...'
|
||||
2. Click on '....'
|
||||
3. See error
|
||||
|
||||
**Expected behavior**
|
||||
A clear and concise description of what you expected to happen.
|
||||
|
||||
**Screenshots or logs**
|
||||
Add screenshots or logs to help explain your problem.
|
||||
|
||||
**Server (please complete the following information):**
|
||||
- OS: [e.g. Centos, Windows]
|
||||
- ARCH: [e.g. Amd64, Arm]
|
||||
- Tunnel [e.g. TCP, HTTP]
|
||||
- Version [e.g. 0.24.0]
|
||||
|
||||
**Client (please complete the following information):**
|
||||
- OS: [e.g. Centos, Windows]
|
||||
- ARCH: [e.g. Amd64, Arm]
|
||||
- Tunnel [e.g. TCP, HTTP]
|
||||
- Version [e.g. 0.24.0]
|
||||
|
||||
**Additional context**
|
||||
Add any other context about the problem here.
|
||||
@@ -0,0 +1,20 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea for this project
|
||||
title: ''
|
||||
labels: enhancement
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Is your feature request related to a problem? Please describe.**
|
||||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||
|
||||
**Describe the solution you'd like**
|
||||
A clear and concise description of what you want to happen.
|
||||
|
||||
**Describe alternatives you've considered**
|
||||
A clear and concise description of any alternative solutions or features you've considered.
|
||||
|
||||
**Additional context**
|
||||
Add any other context or screenshots about the feature request here.
|
||||
@@ -0,0 +1,3 @@
|
||||
.idea
|
||||
nps
|
||||
npc
|
||||
@@ -0,0 +1,58 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- "1.13"
|
||||
- master
|
||||
services:
|
||||
- docker
|
||||
script:
|
||||
- go test -v ./cmd/nps/
|
||||
os:
|
||||
- linux
|
||||
before_deploy:
|
||||
- chmod +x ./build.sh && chmod +x ./build.android.sh && ./build.sh
|
||||
|
||||
deploy:
|
||||
provider: releases
|
||||
api_key:
|
||||
secure: ${TOKEN}
|
||||
skip_cleanup: true
|
||||
file:
|
||||
- freebsd_386_client.tar.gz
|
||||
- freebsd_386_server.tar.gz
|
||||
- freebsd_amd64_client.tar.gz
|
||||
- freebsd_amd64_server.tar.gz
|
||||
- freebsd_arm_client.tar.gz
|
||||
- freebsd_arm_server.tar.gz
|
||||
- linux_386_client.tar.gz
|
||||
- linux_386_server.tar.gz
|
||||
- linux_amd64_client.tar.gz
|
||||
- linux_amd64_server.tar.gz
|
||||
- linux_arm64_client.tar.gz
|
||||
- linux_arm64_server.tar.gz
|
||||
- linux_arm_v5_client.tar.gz
|
||||
- linux_arm_v6_client.tar.gz
|
||||
- linux_arm_v7_client.tar.gz
|
||||
- linux_arm_v5_server.tar.gz
|
||||
- linux_arm_v6_server.tar.gz
|
||||
- linux_arm_v7_server.tar.gz
|
||||
- linux_mips64le_client.tar.gz
|
||||
- linux_mips64le_server.tar.gz
|
||||
- linux_mips64_client.tar.gz
|
||||
- linux_mips64_server.tar.gz
|
||||
- linux_mipsle_client.tar.gz
|
||||
- linux_mipsle_server.tar.gz
|
||||
- linux_mips_client.tar.gz
|
||||
- linux_mips_server.tar.gz
|
||||
- darwin_amd64_client.tar.gz
|
||||
- darwin_amd64_server.tar.gz
|
||||
- windows_386_client.tar.gz
|
||||
- windows_386_server.tar.gz
|
||||
- windows_amd64_client.tar.gz
|
||||
- windows_amd64_server.tar.gz
|
||||
- npc_syno.spk
|
||||
- npc_sdk.tar.gz
|
||||
- android_client.apk
|
||||
on:
|
||||
tags: true
|
||||
all_branches: true
|
||||
@@ -0,0 +1,10 @@
|
||||
FROM golang as builder
|
||||
WORKDIR /go/src/github.com/cnlh/nps
|
||||
COPY . .
|
||||
RUN go get -d -v ./...
|
||||
RUN CGO_ENABLED=0 go build -ldflags="-w -s -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
FROM scratch
|
||||
COPY --from=builder /go/src/github.com/cnlh/nps/npc /
|
||||
VOLUME /conf
|
||||
ENTRYPOINT ["/npc"]
|
||||
@@ -0,0 +1,11 @@
|
||||
FROM golang as builder
|
||||
WORKDIR /go/src/github.com/cnlh/nps
|
||||
COPY . .
|
||||
RUN go get -d -v ./...
|
||||
RUN CGO_ENABLED=0 go build -ldflags="-w -s -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
FROM scratch
|
||||
COPY --from=builder /go/src/github.com/cnlh/nps/nps /
|
||||
COPY --from=builder /go/src/github.com/cnlh/nps/web /web
|
||||
VOLUME /conf
|
||||
CMD ["/nps"]
|
||||
@@ -1,21 +1,674 @@
|
||||
MIT License
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (c) 2018
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://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
|
||||
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:
|
||||
Preamble
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
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.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
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 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. Use with the GNU Affero General Public License.
|
||||
|
||||
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 Affero 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 special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU 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 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 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 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 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 General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
{project} Copyright (C) {year} {fullname}
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
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 GPL, see
|
||||
<http://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
|
||||
@@ -0,0 +1,75 @@
|
||||
SOURCE_FILES?=./...
|
||||
TEST_PATTERN?=.
|
||||
TEST_OPTIONS?=
|
||||
|
||||
export PATH := ./bin:$(PATH)
|
||||
export GO111MODULE := on
|
||||
export GOPROXY := https://gocenter.io
|
||||
|
||||
# Build a beta version of goreleaser
|
||||
build:
|
||||
go build cmd/nps/nps.go
|
||||
go build cmd/npc/npc.go
|
||||
.PHONY: build
|
||||
|
||||
# Install all the build and lint dependencies
|
||||
setup:
|
||||
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh
|
||||
curl -L https://git.io/misspell | sh
|
||||
go mod download
|
||||
.PHONY: setup
|
||||
|
||||
# Run all the tests
|
||||
test:
|
||||
go test $(TEST_OPTIONS) -failfast -race -coverpkg=./... -covermode=atomic -coverprofile=coverage.txt $(SOURCE_FILES) -run $(TEST_PATTERN) -timeout=2m
|
||||
.PHONY: test
|
||||
|
||||
# Run all the tests and opens the coverage report
|
||||
cover: test
|
||||
go tool cover -html=coverage.txt
|
||||
.PHONY: cover
|
||||
|
||||
# gofmt and goimports all go files
|
||||
fmt:
|
||||
find . -name '*.go' -not -wholename './vendor/*' | while read -r file; do gofmt -w -s "$$file"; goimports -w "$$file"; done
|
||||
.PHONY: fmt
|
||||
|
||||
# Run all the linters
|
||||
lint:
|
||||
# TODO: fix tests and lll issues
|
||||
./bin/golangci-lint run --tests=false --enable-all --disable=lll ./...
|
||||
./bin/misspell -error **/*
|
||||
.PHONY: lint
|
||||
|
||||
# Clean go.mod
|
||||
go-mod-tidy:
|
||||
@go mod tidy -v
|
||||
@git diff HEAD
|
||||
@git diff-index --quiet HEAD
|
||||
.PHONY: go-mod-tidy
|
||||
|
||||
# Run all the tests and code checks
|
||||
ci: build test lint go-mod-tidy
|
||||
.PHONY: ci
|
||||
|
||||
# Generate the static documentation
|
||||
static:
|
||||
@hugo --enableGitInfo --source www
|
||||
.PHONY: static
|
||||
|
||||
# Show to-do items per file.
|
||||
todo:
|
||||
@grep \
|
||||
--exclude-dir=vendor \
|
||||
--exclude-dir=node_modules \
|
||||
--exclude=Makefile \
|
||||
--text \
|
||||
--color \
|
||||
-nRo -E ' TODO:.*|SkipNow' .
|
||||
.PHONY: todo
|
||||
|
||||
clean:
|
||||
rm npc nps
|
||||
.PHONY: clean
|
||||
|
||||
.DEFAULT_GOAL := build
|
||||
@@ -1,477 +1,66 @@
|
||||
# easyProxy
|
||||
|
||||
easyProxy是一款轻量级、高性能、功能最为强大的**内网穿透**代理服务器。目前支持**tcp、udp流量转发**,可支持任何tcp、udp上层协议(访问内网网站、本地支付接口调试、ssh访问、远程桌面,内网dns解析等等……),此外还**支持内网http代理、内网socks5代理**,可实现在非内网环境下如同使用vpn一样访问内网资源和设备的效果,同时**支持socks5验证,snnapy压缩(节省带宽和流量)、站点保护、加密传输**。
|
||||
# nps
|
||||
 
|
||||
[](https://gitter.im/cnlh-nps/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
||||
[](https://travis-ci.org/cnlh/nps)
|
||||
|
||||
目前市面上提供类似服务的有花生壳、TeamView、GoToMyCloud等等,但要使用第三方的公网服务器就必须为第三方付费,并且这些服务都有各种各样的限制,此外,由于数据包会流经第三方,因此对数据安全也是一大隐患。
|
||||
nps是一款轻量级、高性能、功能强大的**内网穿透**代理服务器。目前支持**tcp、udp流量转发**,可支持任何**tcp、udp**上层协议(访问内网网站、本地支付接口调试、ssh访问、远程桌面,内网dns解析等等……),此外还**支持内网http代理、内网socks5代理**、**p2p等**,并带有功能强大的web管理端。
|
||||
|
||||
|
||||
支持客户端与服务端连接中断自动重连,多路传输,大大的提高请求处理速度,go语言编写,无第三方依赖。
|
||||
## 背景
|
||||

|
||||
|
||||
## 背景
|
||||

|
||||
1. web管理模式,可配置多条tcp、udp隧道,多个域名代理等等----> [web管理模式](#web管理模式)
|
||||
1. 做微信公众号开发、小程序开发等----> 域名代理模式
|
||||
|
||||
2. 内网多站点配合代理。----> [http反向代理请求](#http代理请求)
|
||||
2. 想在外网通过ssh连接内网的机器,做云服务器到内网服务器端口的映射,----> tcp代理模式
|
||||
|
||||
3. 想在外网通过ssh连接内网的机器,做云服务器到内网服务器端口的映射,或者做微信公众号开发、小程序开发等---->[tcp隧道模式](#tcp隧道模式)
|
||||
3. 在非内网环境下使用内网dns,或者需要通过udp访问内网机器等----> udp代理模式
|
||||
|
||||
4. 在非内网环境下使用内网dns,或者需要通过udp访问内网机器等---->[udp隧道模式](#udp隧道模式)
|
||||
4. 在外网使用HTTP代理访问内网站点----> http代理模式
|
||||
|
||||
5. 在外网使用HTTP代理访问内网站点---->[http代理模式](#http代理模式)
|
||||
5. 搭建一个内网穿透ss,在外网如同使用内网vpn一样访问内网资源或者设备----> socks5代理模式
|
||||
|
||||
6. 搭建一个内网穿透ss,在外网如同使用内网vpn一样访问内网资源或者设备----> [socks5代理模式](#socks5代理模式)
|
||||
## 快速开始
|
||||
|
||||
## 特点
|
||||
- [x] 支持snappy压缩,减小传输过程流量消耗
|
||||
- [x] 支持多站点配置,兼容多个内网网站,可处理相互之间的跳转包含关系
|
||||
- [x] 断线自动重连
|
||||
- [x] 支持多路传输,提高并发
|
||||
- [x] 跨站自动匹配替换
|
||||
- [x] 支持tcp隧道,提升访问效率
|
||||
- [x] 支持udp隧道
|
||||
- [x] 支持http代理
|
||||
- [x] 支持内网穿透sock5代理,配合proxifer可达到vpn的效果,在外网访问内网资源或者设备,同时可以设置用户名和密码验证
|
||||
- [x] 强大的web管理界面,可方便的设置的和管理隧道
|
||||
- [x] 支持站点密码保护
|
||||
- [x] 支持加密传输
|
||||
- [x] 支持TCP多路复用
|
||||
- [x] 支持同时开多条tcp、udp隧道等等,且只需要开一个客户端和服务端
|
||||
- [x] 支持一个服务端,多个客户端模式
|
||||
### 安装
|
||||
> [releases](https://github.com/cnlh/nps/releases)
|
||||
|
||||
## 目录
|
||||
下载对应的系统版本即可,服务端和客户端是单独的
|
||||
|
||||
1. [安装](#安装)
|
||||
2. [web管理模式](#web管理模式)(多隧道时推荐)
|
||||
3. [tcp隧道模式](#tcp隧道模式)
|
||||
4. [udp隧道模式](#udp隧道模式)
|
||||
5. [http反向代理请求](#http代理请求)
|
||||
6. [socks5代理模式](#socks5代理模式)
|
||||
7. [http代理模式](#http代理模式)
|
||||
8. [数据压缩支持](#数据压缩支持)
|
||||
9. [站点密码保护](#站点保护)
|
||||
10. [加密传输](#加密传输)
|
||||
11. [TCP多路复用](#多路复用)
|
||||
11. [配置文件说明](#配置文件)
|
||||
### 服务端启动
|
||||
下载完服务器压缩包后,解压,然后进入解压后的文件夹
|
||||
|
||||
## 安装
|
||||
- 执行安装命令
|
||||
|
||||
1. release安装
|
||||
> https://github.com/cnlh/easyProxy/releases
|
||||
对于linux|darwin ```sudo ./nps install```
|
||||
|
||||
下载对应的系统版本即可(目前linux和windows只编译了64位的),服务端和客户端共用一个程序,go语言开发,无需任何第三方依赖
|
||||
对于windows,管理员身份运行cmd,进入安装目录 ```nps.exe install```
|
||||
|
||||
2. 源码安装
|
||||
- 安装源码
|
||||
> go get github.com/cnlh/easyProxy
|
||||
- 编译(无第三方模块)
|
||||
> go build
|
||||
- 启动
|
||||
|
||||
## web管理模式
|
||||
对于linux|darwin ```sudo nps start```
|
||||
|
||||

|
||||
### 介绍
|
||||
对于windows,管理员身份运行cmd,进入程序目录 ```nps.exe start```
|
||||
|
||||
可在网页上配置和管理各个tcp、udp隧道、内网站点代理等等,功能极为强大,操作也非常方便。
|
||||
### 使用
|
||||
停止和重启可用,stop和restart
|
||||
|
||||
**有两种模式:**
|
||||
**如果发现没有启动成功,可以查看日志(Windows日志文件位于当前运行目录下,linux和darwin位于/var/log/nps.log)**
|
||||
- 访问服务端ip:web服务端口(默认为8080)
|
||||
- 使用用户名和密码登陆(默认admin/123,正式使用一定要更改)
|
||||
- 创建客户端
|
||||
|
||||
1、单客户端模式,所有的隧道流量均从这个单客户端转发。
|
||||
### 客户端连接
|
||||
- 点击web管理中客户端前的+号,复制启动命令
|
||||
- 执行启动命令,linux直接执行即可,windows将./npc换成npc.exe用cmd执行
|
||||
|
||||
如果需要注册到系统服务可查看[注册到系统服务](https://cnlh.github.io/nps/#/use?id=注册到系统服务)
|
||||
|
||||
- 服务端
|
||||
### 配置
|
||||
- 客户端连接后,在web中配置对应穿透服务即可
|
||||
- 更多高级用法见[完整文档](https://cnlh.github.io/nps/)
|
||||
|
||||
```
|
||||
./easyProxy -mode=webServer -tcpport=8284 -vkey=DKibZF5TXvic1g3kY
|
||||
```
|
||||
名称 | 含义
|
||||
---|---
|
||||
mode | 运行模式
|
||||
vkey | 验证密钥
|
||||
tcpport | 服务端与客户端通信端口
|
||||
|
||||
|
||||
- 客户端
|
||||
|
||||
```
|
||||
./easyProxy -server=ip:port -vkey=DKibZF5TXvic1g3kY
|
||||
```
|
||||
- 配置
|
||||
|
||||
进入web界面,公网ip:web界面端口(默认8080),密码为123
|
||||
|
||||
2、多客户端模式,不同的隧道流量均从不同的客户端转发。
|
||||
|
||||
|
||||
- 服务端
|
||||
|
||||
```
|
||||
./easyProxy -mode=webServer -tcpport=8284
|
||||
```
|
||||
名称 | 含义
|
||||
---|---
|
||||
mode | 运行模式
|
||||
tcpport | 服务端与客户端通信端口
|
||||
- 客户端
|
||||
|
||||
进入web管理界面,有详细的命令
|
||||
|
||||
- 配置
|
||||
|
||||
进入web界面,公网ip:web界面端口(默认8080),密码默认为123
|
||||
|
||||
|
||||
|
||||
|
||||
## tcp隧道模式
|
||||
|
||||
### 场景及原理
|
||||
较为适用于处理tcp连接,例如ssh,同时也适用于http等,访问服务端的8024端口相当于访问内网10.1.50.202机器的4000端口,构成如下所示的隧道。
|
||||
|
||||

|
||||
|
||||
例如:
|
||||
|
||||
**背景:**
|
||||
|
||||
- 内网机器10.1.50.203提供了web服务80端口
|
||||
|
||||
- 有VPS一个,公网IP:123.206.77.88
|
||||
|
||||
**需求:**
|
||||
|
||||
在家里能够通过访问VPS的8024端口访问到内网机器A的80端口
|
||||
|
||||
### 使用
|
||||
- 服务端
|
||||
|
||||
```
|
||||
./easyProxy -mode=tunnelServer -vkey=DKibZF5TXvic1g3kY -tcpport=8284 -httpport=8024 -target=10.1.50.203:80
|
||||
```
|
||||
|
||||
名称 | 含义
|
||||
---|---
|
||||
mode | 运行模式
|
||||
vkey | 验证密钥
|
||||
tcpport | 服务端与客户端通信端口
|
||||
httpport | 外部访问端口
|
||||
target | 目标地址,格式如上
|
||||
|
||||
- 客户端
|
||||
|
||||
|
||||
```
|
||||
./easyProxy -server=ip:port -vkey=DKibZF5TXvic1g3kY
|
||||
```
|
||||
|
||||
- 与nginx配合实现访问a.ourcauc.com等同访问10.1.50.203:80效果,将该域名解析道云服务器,nginx配置
|
||||
```
|
||||
server {
|
||||
listen 80;
|
||||
server_name a.ourcauc.com;
|
||||
location / {
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_set_header X-Nginx-Proxy true;
|
||||
proxy_set_header Connection "";
|
||||
proxy_pass http://127.0.0.1:8024;
|
||||
}
|
||||
}
|
||||
```
|
||||
## udp隧道模式
|
||||
|
||||
### 场景及原理
|
||||
|
||||
**背景**
|
||||
- 内网机器A提供了DNS解析服务,10.1.50.210:53端口
|
||||
|
||||
- 有VPS一个,公网IP:123.206.77.88
|
||||
|
||||
**需求:**
|
||||
在家里能够通过设置本地dns为123.206.77.88,使用内网机器A进行域名解析服务.
|
||||
|
||||
访问vps的53端口相当于访问10.1.50.210的53端口,构成如下所示的隧道。
|
||||
|
||||

|
||||
|
||||
|
||||
### 使用
|
||||
- 服务端
|
||||
|
||||
```
|
||||
./easyProxy -mode=udpServer -vkey=DKibZF5TXvic1g3kY -tcpport=8284 -httpport=53 -target=10.1.50.210:53
|
||||
```
|
||||
|
||||
名称 | 含义
|
||||
---|---
|
||||
mode | 运行模式
|
||||
vkey | 验证密钥
|
||||
tcpport | 服务端与客户端通信端口
|
||||
httpport | 公网vps的访问端口
|
||||
target | 目标地址,格式如上
|
||||
|
||||
- 客户端
|
||||
|
||||
|
||||
```
|
||||
./easyProxy -server=ip:port -vkey=DKibZF5TXvic1g3kY
|
||||
```
|
||||
|
||||
## http代理请求
|
||||
|
||||
### 场景及原理
|
||||
|
||||
较为适用于http,也就是web站点的穿透,服务端与客户端之间建立连接,服务端收到http请求后,将请求发送到客户端,客户端再执行这个请求,并将结果返回给服务端,服务端收到后再返回。
|
||||
|
||||
<html>
|
||||
<span style="color:red">特点:支持同时代理多个站点,不同站点之间有联系还可以实现匹配替换</span>
|
||||
</html>
|
||||
|
||||

|
||||
|
||||
**最终效果**:
|
||||
- 访问a.server.com和访问10.1.50.203的80端口相同
|
||||
- 访问b.server.com和访问10.1.50.202的80端口相同
|
||||
- 访问c.server.com和访问10.1.50.201的80端口相同
|
||||
### 使用
|
||||
- 服务端
|
||||
|
||||
```
|
||||
./easyProxy -mode=httpServer -vkey=DKibZF5TXvic1g3kY -tcpport=8284 -httpport=8024
|
||||
```
|
||||
|
||||
名称 | 含义
|
||||
---|---
|
||||
mode | 运行模式
|
||||
vkey | 验证密钥
|
||||
tcpport | 服务端与客户端通信端口
|
||||
httpport | 代理的http端口(与nginx配合使用)
|
||||
|
||||
- 客户端
|
||||
|
||||
```
|
||||
建立配置文件 config.json
|
||||
```
|
||||
|
||||
|
||||
```
|
||||
./easyProxy -server=ip:port -config=config.json -vkey=DKibZF5TXvic1g3kY
|
||||
```
|
||||
|
||||
|
||||
名称 | 含义
|
||||
---|---
|
||||
config | 配置文件路径
|
||||
### 配置文件config.json
|
||||
|
||||
```
|
||||
{
|
||||
"SiteList": [
|
||||
{
|
||||
"host": "a.ourcauc.com",
|
||||
"url": "10.1.50.203",
|
||||
"port": 80
|
||||
},
|
||||
{
|
||||
"host": "b.ourcauc.com",
|
||||
"url": "10.1.50.202",
|
||||
"port": 80
|
||||
},
|
||||
{
|
||||
"host": "c.ourcauc.com",
|
||||
"url": "10.1.50.203",
|
||||
"port": 80
|
||||
}
|
||||
],
|
||||
"Replace": 0
|
||||
}
|
||||
```
|
||||
名称 | 含义
|
||||
---|---
|
||||
SiteList | 本地解析的域名列表
|
||||
host | 域名地址
|
||||
url | 内网代理的地址
|
||||
port | 内网代理的地址对应的端口
|
||||
Replace | 是否自动匹配替换[(查看场景)](https://github.com/cnlh/easyProxy/issues/1)
|
||||
|
||||
|
||||
### nginx代理配置示例
|
||||
```
|
||||
server {
|
||||
listen 80;
|
||||
server_name a.ourcauc.com b.ourcauc.com c.ourcauc.com ;
|
||||
location / {
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_set_header X-Nginx-Proxy true;
|
||||
proxy_set_header Connection "";
|
||||
proxy_pass http://127.0.0.1:8024;
|
||||
}
|
||||
}
|
||||
```
|
||||
## 域名配置示例
|
||||
> -a A 123.206.77.88
|
||||
|
||||
> -b A 123.206.77.88
|
||||
|
||||
> -c A 123.206.77.88
|
||||
|
||||
### 跨站自动匹配替换说明
|
||||
|
||||
例如,访问:a.ourcauc.com,该页面里面有一个超链接为10.1.50.202:80,将根据配置文件自动该将url替换为b.ourcauc.com,以达到跨站也可访问的效果,但需要提前在配置文件中配置这些站点。
|
||||
|
||||
如需开启,请加配置文件Replace值设置为1
|
||||
>注意:开启可能导致不应该被替换的内容被替换,请谨慎开启
|
||||
|
||||
### 二级域名示范
|
||||
|
||||
[二级域名](https://github.com/cnlh/easyProxy/wiki/%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B)
|
||||
|
||||
|
||||
## socks5代理模式
|
||||
|
||||
### 场景及原理
|
||||
|
||||
**原理**
|
||||
|
||||
主要用于socks5代理,也就是和ss类似,不过是代理内网。使用此模式时,可在非内网环境下配置本机的socks5代理(服务器ip、sock5代理端口),即可实现socks5代理,达到访问内网的网站的效果,配合proxifer等全局代理软件,即可如同使用内网vpn一样,访问内网网站,通过ssh连接内网机器等等……。
|
||||

|
||||
|
||||
### 使用
|
||||
- 服务端
|
||||
|
||||
```
|
||||
./easyProxy -mode=socks5Server -vkey=DKibZF5TXvic1g3kY -tcpport=8284 -httpport=8024
|
||||
```
|
||||
|
||||
名称 | 含义
|
||||
---|---
|
||||
mode | 运行模式
|
||||
vkey | 验证密钥
|
||||
tcpport | 服务端与客户端通信端口
|
||||
httpport | 代理的http端口(socks5连接端口)
|
||||
u | 验证的用户名
|
||||
p | 验证的密码
|
||||
|
||||
**说明**:用户名和密码验证模式,仅部分socks5客户端支持,例如proxifer。命令行执行加上,web管理模式中可单独配置
|
||||
|
||||
|
||||
```
|
||||
-u=user -p=password
|
||||
```
|
||||
|
||||
- 客户端
|
||||
|
||||
|
||||
```
|
||||
./easyProxy -server=ip:port -vkey=DKibZF5TXvic1g3kY
|
||||
```
|
||||
|
||||
- 需要使用内网代理的机器
|
||||
|
||||
```
|
||||
配置socks5代理即可,ip为外网服务器ip,端口为httpport,即可在外网环境使用内网啦!也可使用proxifer等全局代理软件。
|
||||
```
|
||||
如果设置了用户名和密码,记得填上用户名和密码(仅部分客户端支持密码验证)
|
||||
|
||||
|
||||
|
||||
## http代理模式
|
||||
|
||||
### 场景及原理
|
||||
主要用于HTTP代理,区别也就是HTTP代理和sock5代理的区别。使用此模式时,可在非内网环境下配置本机的HTTP代理(服务器ip、HTTP代理端口),即可实现HTTP代理,达到访问内网的网站的效果。
|
||||

|
||||
|
||||
|
||||
### 使用
|
||||
- 服务端
|
||||
|
||||
```
|
||||
./easyProxy -mode=httpProxyServer -vkey=DKibZF5TXvic1g3kY -tcpport=8284 -httpport=8024
|
||||
```
|
||||
|
||||
名称 | 含义
|
||||
---|---
|
||||
mode | 运行模式
|
||||
vkey | 验证密钥
|
||||
tcpport | 服务端与客户端通信端口
|
||||
httpport | http代理连接端口
|
||||
|
||||
- 客户端
|
||||
|
||||
|
||||
|
||||
```
|
||||
./easyProxy -server=ip:port -vkey=DKibZF5TXvic1g3kY
|
||||
```
|
||||
|
||||
- 需要使用内网代理的机器
|
||||
|
||||
|
||||
```
|
||||
配置HTTP代理即可,ip为外网服务器ip,端口为httpport,即可在外网环境访问内网啦!
|
||||
```
|
||||
|
||||
## 数据压缩支持
|
||||
|
||||
由于是内网穿透,内网客户端与服务端之间的隧道存在大量的数据交换,为节省流量,加快传输速度,由此本程序支持SNNAPY形式的压缩。
|
||||
|
||||
|
||||
- 所有模式均支持数据压缩,可以与加密同时使用
|
||||
|
||||
|
||||
- 在server端加上参数 -compress=snappy(或在web中设置),例如在TCP隧道模式
|
||||
```
|
||||
./easyProxy -mode=tunnelServer -vkey=DKibZF5TXvic1g3kY -tcpport=8284 -httpport=8024 -target=10.1.50.203:80 -compress=snappy
|
||||
```
|
||||
|
||||
## 加密传输
|
||||
|
||||
如果公司内网防火墙对外网访问进行了流量识别与屏蔽,例如禁止了ssh协议等,通过设置 配置文件,将服务端与客户端之间的通信内容加密传输,将会有效防止流量被拦截。
|
||||
|
||||
|
||||
- 在server端加上参数 -crypt=true(或在web管理中设置)
|
||||
```
|
||||
-crypt=true
|
||||
```
|
||||
|
||||
## 多路复用
|
||||
|
||||
客户端和服务器端之间的连接支持多路复用,不再需要为每一个用户请求创建一个连接,使连接建立的延迟降低,并且避免了大量文件描述符的占用。
|
||||
|
||||
|
||||
- 在server端加上参数 -mux=true(或在web管理中设置)
|
||||
|
||||
```
|
||||
-mux=true
|
||||
```
|
||||
|
||||
|
||||
## 站点保护
|
||||
由于所有客户端共用一个 http 服务端口,任何知道你的域名和 url 的人都能访问到你部署在内网的 web 服务,但是在某些场景下需要确保只有限定的用户才能访问。
|
||||
|
||||
easyProxy支持通过 HTTP Basic Auth 来保护你的 web 服务,使用户需要通过用户名和密码才能访问到你的服务。
|
||||
|
||||
目前支持使用tcp协议的web站点保护,命令行运行时可设置
|
||||
```
|
||||
-u=user -p=password
|
||||
```
|
||||
|
||||
web管理中也可配置
|
||||
|
||||
|
||||
|
||||
## 配置文件
|
||||
- /conf/app.conf
|
||||
|
||||
名称 | 含义
|
||||
---|---
|
||||
httpport | web管理端口
|
||||
password | web界面管理密码
|
||||
hostPort | 域名代理模式监听端口
|
||||
|
||||
|
||||
## 操作系统支持
|
||||
支持Windows、Linux、MacOSX等,无第三方依赖库。
|
||||
## 贡献
|
||||
- 如果遇到bug可以直接提交至dev分支
|
||||
- 使用遇到问题可以通过issues反馈
|
||||
- 项目处于开发阶段,还有很多待完善的地方,如果可以贡献代码,请提交 PR 至 dev 分支
|
||||
- 如果有新的功能特性反馈,可以通过issues或者qq群反馈
|
||||
|
||||
@@ -0,0 +1,527 @@
|
||||
package bridge
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"github.com/cnlh/nps/lib/mux"
|
||||
"github.com/cnlh/nps/lib/version"
|
||||
"github.com/cnlh/nps/server/connection"
|
||||
"github.com/cnlh/nps/server/tool"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
tunnel *mux.Mux
|
||||
signal *conn.Conn
|
||||
file *mux.Mux
|
||||
Version string
|
||||
retryTime int // it will be add 1 when ping not ok until to 3 will close the client
|
||||
}
|
||||
|
||||
func NewClient(t, f *mux.Mux, s *conn.Conn, vs string) *Client {
|
||||
return &Client{
|
||||
signal: s,
|
||||
tunnel: t,
|
||||
file: f,
|
||||
Version: vs,
|
||||
}
|
||||
}
|
||||
|
||||
type Bridge struct {
|
||||
TunnelPort int //通信隧道端口
|
||||
Client sync.Map
|
||||
Register sync.Map
|
||||
tunnelType string //bridge type kcp or tcp
|
||||
OpenTask chan *file.Tunnel
|
||||
CloseTask chan *file.Tunnel
|
||||
CloseClient chan int
|
||||
SecretChan chan *conn.Secret
|
||||
ipVerify bool
|
||||
runList map[int]interface{}
|
||||
}
|
||||
|
||||
func NewTunnel(tunnelPort int, tunnelType string, ipVerify bool, runList map[int]interface{}) *Bridge {
|
||||
return &Bridge{
|
||||
TunnelPort: tunnelPort,
|
||||
tunnelType: tunnelType,
|
||||
OpenTask: make(chan *file.Tunnel),
|
||||
CloseTask: make(chan *file.Tunnel),
|
||||
CloseClient: make(chan int),
|
||||
SecretChan: make(chan *conn.Secret),
|
||||
ipVerify: ipVerify,
|
||||
runList: runList,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Bridge) StartTunnel() error {
|
||||
go s.ping()
|
||||
if s.tunnelType == "kcp" {
|
||||
logs.Info("server start, the bridge type is %s, the bridge port is %d", s.tunnelType, s.TunnelPort)
|
||||
return conn.NewKcpListenerAndProcess(beego.AppConfig.String("bridge_ip")+":"+beego.AppConfig.String("bridge_port"), func(c net.Conn) {
|
||||
s.cliProcess(conn.NewConn(c))
|
||||
})
|
||||
} else {
|
||||
listener, err := connection.GetBridgeListener(s.tunnelType)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
os.Exit(0)
|
||||
return err
|
||||
}
|
||||
conn.Accept(listener, func(c net.Conn) {
|
||||
s.cliProcess(conn.NewConn(c))
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//get health information form client
|
||||
func (s *Bridge) GetHealthFromClient(id int, c *conn.Conn) {
|
||||
for {
|
||||
if info, status, err := c.GetHealthInfo(); err != nil {
|
||||
break
|
||||
} else if !status { //the status is true , return target to the targetArr
|
||||
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
|
||||
v := value.(*file.Tunnel)
|
||||
if v.Client.Id == id && v.Mode == "tcp" && strings.Contains(v.Target.TargetStr, info) {
|
||||
v.Lock()
|
||||
if v.Target.TargetArr == nil || (len(v.Target.TargetArr) == 0 && len(v.HealthRemoveArr) == 0) {
|
||||
v.Target.TargetArr = common.TrimArr(strings.Split(v.Target.TargetStr, "\n"))
|
||||
}
|
||||
v.Target.TargetArr = common.RemoveArrVal(v.Target.TargetArr, info)
|
||||
if v.HealthRemoveArr == nil {
|
||||
v.HealthRemoveArr = make([]string, 0)
|
||||
}
|
||||
v.HealthRemoveArr = append(v.HealthRemoveArr, info)
|
||||
v.Unlock()
|
||||
}
|
||||
return true
|
||||
})
|
||||
file.GetDb().JsonDb.Hosts.Range(func(key, value interface{}) bool {
|
||||
v := value.(*file.Host)
|
||||
if v.Client.Id == id && strings.Contains(v.Target.TargetStr, info) {
|
||||
v.Lock()
|
||||
if v.Target.TargetArr == nil || (len(v.Target.TargetArr) == 0 && len(v.HealthRemoveArr) == 0) {
|
||||
v.Target.TargetArr = common.TrimArr(strings.Split(v.Target.TargetStr, "\n"))
|
||||
}
|
||||
v.Target.TargetArr = common.RemoveArrVal(v.Target.TargetArr, info)
|
||||
if v.HealthRemoveArr == nil {
|
||||
v.HealthRemoveArr = make([]string, 0)
|
||||
}
|
||||
v.HealthRemoveArr = append(v.HealthRemoveArr, info)
|
||||
v.Unlock()
|
||||
}
|
||||
return true
|
||||
})
|
||||
} else { //the status is false,remove target from the targetArr
|
||||
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
|
||||
v := value.(*file.Tunnel)
|
||||
if v.Client.Id == id && v.Mode == "tcp" && common.IsArrContains(v.HealthRemoveArr, info) && !common.IsArrContains(v.Target.TargetArr, info) {
|
||||
v.Lock()
|
||||
v.Target.TargetArr = append(v.Target.TargetArr, info)
|
||||
v.HealthRemoveArr = common.RemoveArrVal(v.HealthRemoveArr, info)
|
||||
v.Unlock()
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
file.GetDb().JsonDb.Hosts.Range(func(key, value interface{}) bool {
|
||||
v := value.(*file.Host)
|
||||
if v.Client.Id == id && common.IsArrContains(v.HealthRemoveArr, info) && !common.IsArrContains(v.Target.TargetArr, info) {
|
||||
v.Lock()
|
||||
v.Target.TargetArr = append(v.Target.TargetArr, info)
|
||||
v.HealthRemoveArr = common.RemoveArrVal(v.HealthRemoveArr, info)
|
||||
v.Unlock()
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
s.DelClient(id)
|
||||
}
|
||||
|
||||
//验证失败,返回错误验证flag,并且关闭连接
|
||||
func (s *Bridge) verifyError(c *conn.Conn) {
|
||||
c.Write([]byte(common.VERIFY_EER))
|
||||
}
|
||||
|
||||
func (s *Bridge) verifySuccess(c *conn.Conn) {
|
||||
c.Write([]byte(common.VERIFY_SUCCESS))
|
||||
}
|
||||
|
||||
func (s *Bridge) cliProcess(c *conn.Conn) {
|
||||
//read test flag
|
||||
if _, err := c.GetShortContent(3); err != nil {
|
||||
logs.Info("The client %s connect error", c.Conn.RemoteAddr(), err.Error())
|
||||
return
|
||||
}
|
||||
//version check
|
||||
if b, err := c.GetShortLenContent(); err != nil || string(b) != version.GetVersion() {
|
||||
logs.Info("The client %s version does not match", c.Conn.RemoteAddr())
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
//version get
|
||||
var vs []byte
|
||||
var err error
|
||||
if vs, err = c.GetShortLenContent(); err != nil {
|
||||
logs.Info("get client %s version error", err.Error())
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
//write server version to client
|
||||
c.Write([]byte(crypt.Md5(version.GetVersion())))
|
||||
c.SetReadDeadlineBySecond(5)
|
||||
var buf []byte
|
||||
//get vKey from client
|
||||
if buf, err = c.GetShortContent(32); err != nil {
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
//verify
|
||||
id, err := file.GetDb().GetIdByVerifyKey(string(buf), c.Conn.RemoteAddr().String())
|
||||
if err != nil {
|
||||
logs.Info("Current client connection validation error, close this client:", c.Conn.RemoteAddr())
|
||||
s.verifyError(c)
|
||||
return
|
||||
} else {
|
||||
s.verifySuccess(c)
|
||||
}
|
||||
if flag, err := c.ReadFlag(); err == nil {
|
||||
s.typeDeal(flag, c, id, string(vs))
|
||||
} else {
|
||||
logs.Warn(err, flag)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Bridge) DelClient(id int) {
|
||||
if v, ok := s.Client.Load(id); ok {
|
||||
if v.(*Client).signal != nil {
|
||||
v.(*Client).signal.Close()
|
||||
}
|
||||
s.Client.Delete(id)
|
||||
if file.GetDb().IsPubClient(id) {
|
||||
return
|
||||
}
|
||||
if c, err := file.GetDb().GetClient(id); err == nil {
|
||||
s.CloseClient <- c.Id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//use different
|
||||
func (s *Bridge) typeDeal(typeVal string, c *conn.Conn, id int, vs string) {
|
||||
isPub := file.GetDb().IsPubClient(id)
|
||||
switch typeVal {
|
||||
case common.WORK_MAIN:
|
||||
if isPub {
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
//the vKey connect by another ,close the client of before
|
||||
if v, ok := s.Client.LoadOrStore(id, NewClient(nil, nil, c, vs)); ok {
|
||||
if v.(*Client).signal != nil {
|
||||
v.(*Client).signal.WriteClose()
|
||||
}
|
||||
v.(*Client).signal = c
|
||||
v.(*Client).Version = vs
|
||||
}
|
||||
go s.GetHealthFromClient(id, c)
|
||||
logs.Info("clientId %d connection succeeded, address:%s ", id, c.Conn.RemoteAddr())
|
||||
case common.WORK_CHAN:
|
||||
muxConn := mux.NewMux(c.Conn, s.tunnelType)
|
||||
if v, ok := s.Client.LoadOrStore(id, NewClient(muxConn, nil, nil, vs)); ok {
|
||||
v.(*Client).tunnel = muxConn
|
||||
}
|
||||
case common.WORK_CONFIG:
|
||||
client, err := file.GetDb().GetClient(id)
|
||||
if err != nil || (!isPub && !client.ConfigConnAllow) {
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
binary.Write(c, binary.LittleEndian, isPub)
|
||||
go s.getConfig(c, isPub, client)
|
||||
case common.WORK_REGISTER:
|
||||
go s.register(c)
|
||||
case common.WORK_SECRET:
|
||||
if b, err := c.GetShortContent(32); err == nil {
|
||||
s.SecretChan <- conn.NewSecret(string(b), c)
|
||||
} else {
|
||||
logs.Error("secret error, failed to match the key successfully")
|
||||
}
|
||||
case common.WORK_FILE:
|
||||
muxConn := mux.NewMux(c.Conn, s.tunnelType)
|
||||
if v, ok := s.Client.LoadOrStore(id, NewClient(nil, muxConn, nil, vs)); ok {
|
||||
v.(*Client).file = muxConn
|
||||
}
|
||||
case common.WORK_P2P:
|
||||
//read md5 secret
|
||||
if b, err := c.GetShortContent(32); err != nil {
|
||||
logs.Error("p2p error,", err.Error())
|
||||
} else if t := file.GetDb().GetTaskByMd5Password(string(b)); t == nil {
|
||||
logs.Error("p2p error, failed to match the key successfully")
|
||||
} else {
|
||||
if v, ok := s.Client.Load(t.Client.Id); !ok {
|
||||
return
|
||||
} else {
|
||||
//向密钥对应的客户端发送与服务端udp建立连接信息,地址,密钥
|
||||
v.(*Client).signal.Write([]byte(common.NEW_UDP_CONN))
|
||||
svrAddr := beego.AppConfig.String("p2p_ip") + ":" + beego.AppConfig.String("p2p_port")
|
||||
if err != nil {
|
||||
logs.Warn("get local udp addr error")
|
||||
return
|
||||
}
|
||||
v.(*Client).signal.WriteLenContent([]byte(svrAddr))
|
||||
v.(*Client).signal.WriteLenContent(b)
|
||||
//向该请求者发送建立连接请求,服务器地址
|
||||
c.WriteLenContent([]byte(svrAddr))
|
||||
}
|
||||
}
|
||||
}
|
||||
c.SetAlive(s.tunnelType)
|
||||
return
|
||||
}
|
||||
|
||||
//register ip
|
||||
func (s *Bridge) register(c *conn.Conn) {
|
||||
var hour int32
|
||||
if err := binary.Read(c, binary.LittleEndian, &hour); err == nil {
|
||||
s.Register.Store(common.GetIpByAddr(c.Conn.RemoteAddr().String()), time.Now().Add(time.Hour*time.Duration(hour)))
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Bridge) SendLinkInfo(clientId int, link *conn.Link, t *file.Tunnel) (target net.Conn, err error) {
|
||||
//if the proxy type is local
|
||||
if link.LocalProxy {
|
||||
target, err = net.Dial("tcp", link.Host)
|
||||
return
|
||||
}
|
||||
if v, ok := s.Client.Load(clientId); ok {
|
||||
//If ip is restricted to do ip verification
|
||||
if s.ipVerify {
|
||||
ip := common.GetIpByAddr(link.RemoteAddr)
|
||||
if v, ok := s.Register.Load(ip); !ok {
|
||||
return nil, errors.New(fmt.Sprintf("The ip %s is not in the validation list", ip))
|
||||
} else {
|
||||
if !v.(time.Time).After(time.Now()) {
|
||||
return nil, errors.New(fmt.Sprintf("The validity of the ip %s has expired", ip))
|
||||
}
|
||||
}
|
||||
}
|
||||
var tunnel *mux.Mux
|
||||
if t != nil && t.Mode == "file" {
|
||||
tunnel = v.(*Client).file
|
||||
} else {
|
||||
tunnel = v.(*Client).tunnel
|
||||
}
|
||||
if tunnel == nil {
|
||||
err = errors.New("the client connect error")
|
||||
return
|
||||
}
|
||||
if target, err = tunnel.NewConn(); err != nil {
|
||||
return
|
||||
}
|
||||
if t != nil && t.Mode == "file" {
|
||||
//TODO if t.mode is file ,not use crypt or compress
|
||||
link.Crypt = false
|
||||
link.Compress = false
|
||||
return
|
||||
}
|
||||
if _, err = conn.NewConn(target).SendInfo(link, ""); err != nil {
|
||||
logs.Info("new connect error ,the target %s refuse to connect", link.Host)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
err = errors.New(fmt.Sprintf("the client %d is not connect", clientId))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Bridge) ping() {
|
||||
ticker := time.NewTicker(time.Second * 5)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
arr := make([]int, 0)
|
||||
s.Client.Range(func(key, value interface{}) bool {
|
||||
v := value.(*Client)
|
||||
if v.tunnel == nil || v.signal == nil {
|
||||
v.retryTime += 1
|
||||
if v.retryTime >= 3 {
|
||||
arr = append(arr, key.(int))
|
||||
}
|
||||
return true
|
||||
}
|
||||
if v.tunnel.IsClose {
|
||||
arr = append(arr, key.(int))
|
||||
}
|
||||
return true
|
||||
})
|
||||
for _, v := range arr {
|
||||
logs.Info("the client %d closed", v)
|
||||
s.DelClient(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//get config and add task from client config
|
||||
func (s *Bridge) getConfig(c *conn.Conn, isPub bool, client *file.Client) {
|
||||
var fail bool
|
||||
loop:
|
||||
for {
|
||||
flag, err := c.ReadFlag()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
switch flag {
|
||||
case common.WORK_STATUS:
|
||||
if b, err := c.GetShortContent(32); err != nil {
|
||||
break loop
|
||||
} else {
|
||||
var str string
|
||||
id, err := file.GetDb().GetClientIdByVkey(string(b))
|
||||
if err != nil {
|
||||
break loop
|
||||
}
|
||||
file.GetDb().JsonDb.Hosts.Range(func(key, value interface{}) bool {
|
||||
v := value.(*file.Host)
|
||||
if v.Client.Id == id {
|
||||
str += v.Remark + common.CONN_DATA_SEQ
|
||||
}
|
||||
return true
|
||||
})
|
||||
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
|
||||
v := value.(*file.Tunnel)
|
||||
if _, ok := s.runList[v.Id]; ok && v.Client.Id == id {
|
||||
str += v.Remark + common.CONN_DATA_SEQ
|
||||
}
|
||||
return true
|
||||
})
|
||||
binary.Write(c, binary.LittleEndian, int32(len([]byte(str))))
|
||||
binary.Write(c, binary.LittleEndian, []byte(str))
|
||||
}
|
||||
case common.NEW_CONF:
|
||||
var err error
|
||||
if client, err = c.GetConfigInfo(); err != nil {
|
||||
fail = true
|
||||
c.WriteAddFail()
|
||||
break loop
|
||||
} else {
|
||||
if err = file.GetDb().NewClient(client); err != nil {
|
||||
fail = true
|
||||
c.WriteAddFail()
|
||||
break loop
|
||||
}
|
||||
c.WriteAddOk()
|
||||
c.Write([]byte(client.VerifyKey))
|
||||
s.Client.Store(client.Id, NewClient(nil, nil, nil, ""))
|
||||
}
|
||||
case common.NEW_HOST:
|
||||
h, err := c.GetHostInfo()
|
||||
if err != nil {
|
||||
fail = true
|
||||
c.WriteAddFail()
|
||||
break loop
|
||||
}
|
||||
h.Client = client
|
||||
if h.Location == "" {
|
||||
h.Location = "/"
|
||||
}
|
||||
if !client.HasHost(h) {
|
||||
if file.GetDb().IsHostExist(h) {
|
||||
fail = true
|
||||
c.WriteAddFail()
|
||||
break loop
|
||||
} else {
|
||||
file.GetDb().NewHost(h)
|
||||
c.WriteAddOk()
|
||||
}
|
||||
} else {
|
||||
c.WriteAddOk()
|
||||
}
|
||||
case common.NEW_TASK:
|
||||
if t, err := c.GetTaskInfo(); err != nil {
|
||||
fail = true
|
||||
c.WriteAddFail()
|
||||
break loop
|
||||
} else {
|
||||
ports := common.GetPorts(t.Ports)
|
||||
targets := common.GetPorts(t.Target.TargetStr)
|
||||
if len(ports) > 1 && (t.Mode == "tcp" || t.Mode == "udp") && (len(ports) != len(targets)) {
|
||||
fail = true
|
||||
c.WriteAddFail()
|
||||
break loop
|
||||
} else if t.Mode == "secret" || t.Mode == "p2p" {
|
||||
ports = append(ports, 0)
|
||||
}
|
||||
if len(ports) == 0 {
|
||||
fail = true
|
||||
c.WriteAddFail()
|
||||
break loop
|
||||
}
|
||||
for i := 0; i < len(ports); i++ {
|
||||
tl := new(file.Tunnel)
|
||||
tl.Mode = t.Mode
|
||||
tl.Port = ports[i]
|
||||
tl.ServerIp = t.ServerIp
|
||||
if len(ports) == 1 {
|
||||
tl.Target = t.Target
|
||||
tl.Remark = t.Remark
|
||||
} else {
|
||||
tl.Remark = t.Remark + "_" + strconv.Itoa(tl.Port)
|
||||
tl.Target = new(file.Target)
|
||||
if t.TargetAddr != "" {
|
||||
tl.Target.TargetStr = t.TargetAddr + ":" + strconv.Itoa(targets[i])
|
||||
} else {
|
||||
tl.Target.TargetStr = strconv.Itoa(targets[i])
|
||||
}
|
||||
}
|
||||
tl.Id = int(file.GetDb().JsonDb.GetTaskId())
|
||||
tl.Status = true
|
||||
tl.Flow = new(file.Flow)
|
||||
tl.NoStore = true
|
||||
tl.Client = client
|
||||
tl.Password = t.Password
|
||||
tl.LocalPath = t.LocalPath
|
||||
tl.StripPre = t.StripPre
|
||||
tl.MultiAccount = t.MultiAccount
|
||||
if !client.HasTunnel(tl) {
|
||||
if err := file.GetDb().NewTask(tl); err != nil {
|
||||
logs.Notice("Add task error ", err.Error())
|
||||
fail = true
|
||||
c.WriteAddFail()
|
||||
break loop
|
||||
}
|
||||
if b := tool.TestServerPort(tl.Port, tl.Mode); !b && t.Mode != "secret" && t.Mode != "p2p" {
|
||||
fail = true
|
||||
c.WriteAddFail()
|
||||
break loop
|
||||
} else {
|
||||
s.OpenTask <- tl
|
||||
}
|
||||
}
|
||||
c.WriteAddOk()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if fail && client != nil {
|
||||
s.DelClient(client.Id)
|
||||
}
|
||||
c.Close()
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#/bin/bash
|
||||
#sudo apt-get install libgl1-mesa-dev xorg-dev
|
||||
#go get github.com/ffdfgdfg/fyne-cross
|
||||
#fyne-cross --targets=linux/amd64,windows/amd64,darwin/amd64 gui/npc/npc.go
|
||||
|
||||
mkdir -p /go/src/github.com/cnlh/nps
|
||||
cp -R * /go/src/github.com/cnlh/nps
|
||||
cd /go/src/github.com/cnlh/nps
|
||||
go get -u fyne.io/fyne fyne.io/fyne/cmd/fyne
|
||||
go mod vendor
|
||||
cd vendor
|
||||
cp -R * /go/src
|
||||
cd ..
|
||||
rm -rf vendor
|
||||
cd gui/npc
|
||||
fyne package -os android -appID org.nps.client -icon ../../docs/logo.png
|
||||
mv npc.apk /app/android_client.apk
|
||||
@@ -0,0 +1,179 @@
|
||||
#/bash/sh
|
||||
export VERSION=0.25.2
|
||||
|
||||
sudo apt-get install gcc-mingw-w64-i686
|
||||
env GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc go build -ldflags "-s -w -extldflags -static -extldflags -static" -buildmode=c-shared -o npc_sdk.dll cmd/npc/sdk.go
|
||||
tar -czvf npc_sdk.tar.gz npc_sdk.dll npc_sdk.h
|
||||
|
||||
wget https://github.com/upx/upx/releases/download/v3.95/upx-3.95-amd64_linux.tar.xz
|
||||
tar -xvf upx-3.95-amd64_linux.tar.xz
|
||||
cp upx-3.95-amd64_linux/upx ./
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf linux_amd64_client.tar.gz npc conf/npc.conf conf/multi_account.conf
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf linux_386_client.tar.gz npc conf/npc.conf conf/multi_account.conf
|
||||
|
||||
CGO_ENABLED=0 GOOS=freebsd GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf freebsd_386_client.tar.gz npc conf/npc.conf conf/multi_account.conf
|
||||
|
||||
CGO_ENABLED=0 GOOS=freebsd GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf freebsd_amd64_client.tar.gz npc conf/npc.conf conf/multi_account.conf
|
||||
|
||||
CGO_ENABLED=0 GOOS=freebsd GOARCH=arm go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf freebsd_arm_client.tar.gz npc conf/npc.conf conf/multi_account.conf
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=7 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf linux_arm_v7_client.tar.gz npc conf/npc.conf conf/multi_account.conf
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=6 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf linux_arm_v6_client.tar.gz npc conf/npc.conf conf/multi_account.conf
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=5 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf linux_arm_v5_client.tar.gz npc conf/npc.conf conf/multi_account.conf
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf linux_arm64_client.tar.gz npc conf/npc.conf conf/multi_account.conf
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=mips64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf linux_mips64_client.tar.gz npc conf/npc.conf conf/multi_account.conf
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=mips64le go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf linux_mips64le_client.tar.gz npc conf/npc.conf conf/multi_account.conf
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=mipsle go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf linux_mipsle_client.tar.gz npc conf/npc.conf conf/multi_account.conf
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=mips go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf linux_mips_client.tar.gz npc conf/npc.conf conf/multi_account.conf
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf windows_386_client.tar.gz npc.exe conf/npc.conf conf/multi_account.conf
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf windows_amd64_client.tar.gz npc.exe conf/npc.conf conf/multi_account.conf
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/npc/npc.go
|
||||
|
||||
tar -czvf darwin_amd64_client.tar.gz npc conf/npc.conf conf/multi_account.conf
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf linux_amd64_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf linux_386_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=5 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf linux_arm_v5_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=6 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf linux_arm_v6_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=7 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf linux_arm_v7_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf linux_arm64_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=freebsd GOARCH=arm go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf freebsd_arm_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=freebsd GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf freebsd_386_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=freebsd GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf freebsd_amd64_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps
|
||||
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=mips go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf linux_mips_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=mips64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf linux_mips64_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=mips64le go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf linux_mips64le_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=mipsle go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf linux_mipsle_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps
|
||||
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf darwin_amd64_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf windows_amd64_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps.exe
|
||||
|
||||
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags "-s -w -extldflags -static -extldflags -static" ./cmd/nps/nps.go
|
||||
|
||||
tar -czvf windows_386_server.tar.gz conf/nps.conf conf/tasks.json conf/clients.json conf/hosts.json conf/server.key conf/server.pem web/views web/static nps.exe
|
||||
|
||||
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
|
||||
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
|
||||
sudo apt-get update
|
||||
sudo apt-get -y -o Dpkg::Options::="--force-confnew" install docker-ce
|
||||
docker --version
|
||||
docker run --rm -i -w /app -v $(pwd):/app -e ANDROID_HOME=/usr/local/android_sdk ffdfgdfg/fyne-cross:android /app/build.android.sh
|
||||
git clone https://github.com/cnlh/spksrc.git ~/spksrc
|
||||
mkdir ~/spksrc/nps && cp -rf ./* ~/spksrc/nps/
|
||||
docker run -itd --name spksrc --env VERSION=$VERSION -v ~/spksrc:/spksrc synocommunity/spksrc /bin/bash
|
||||
docker exec -it spksrc /bin/bash -c 'cd /spksrc && make setup && cd /spksrc/spk/npc && make'
|
||||
cp ~/spksrc/packages/npc_noarch-all_$VERSION-1.spk ./npc_syno.spk
|
||||
|
||||
|
||||
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
|
||||
export DOCKER_CLI_EXPERIMENTAL=enabled
|
||||
docker run --rm --privileged docker/binfmt:66f9012c56a8316f9244ffd7622d7c21c1f6f28d
|
||||
docker buildx create --use --name mybuilder
|
||||
docker buildx build --tag ffdfgdfg/nps:$VERSION --output type=image,push=true --file Dockerfile.nps --platform=linux/amd64,linux/arm64,linux/386,linux/arm .
|
||||
docker buildx build --tag ffdfgdfg/npc:$VERSION --output type=image,push=true --file Dockerfile.npc --platform=linux/amd64,linux/arm64,linux/386,linux/arm .
|
||||
@@ -0,0 +1,301 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/xtaci/kcp-go"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/config"
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"github.com/cnlh/nps/lib/mux"
|
||||
)
|
||||
|
||||
type TRPClient struct {
|
||||
svrAddr string
|
||||
bridgeConnType string
|
||||
proxyUrl string
|
||||
vKey string
|
||||
p2pAddr map[string]string
|
||||
tunnel *mux.Mux
|
||||
signal *conn.Conn
|
||||
ticker *time.Ticker
|
||||
cnf *config.Config
|
||||
}
|
||||
|
||||
//new client
|
||||
func NewRPClient(svraddr string, vKey string, bridgeConnType string, proxyUrl string, cnf *config.Config) *TRPClient {
|
||||
return &TRPClient{
|
||||
svrAddr: svraddr,
|
||||
p2pAddr: make(map[string]string, 0),
|
||||
vKey: vKey,
|
||||
bridgeConnType: bridgeConnType,
|
||||
proxyUrl: proxyUrl,
|
||||
cnf: cnf,
|
||||
}
|
||||
}
|
||||
|
||||
var NowStatus int
|
||||
var CloseClient bool
|
||||
//start
|
||||
func (s *TRPClient) Start() {
|
||||
CloseClient = false
|
||||
retry:
|
||||
if CloseClient {
|
||||
return
|
||||
}
|
||||
NowStatus = 0
|
||||
c, err := NewConn(s.bridgeConnType, s.vKey, s.svrAddr, common.WORK_MAIN, s.proxyUrl)
|
||||
if err != nil {
|
||||
logs.Error("The connection server failed and will be reconnected in five seconds, error", err.Error())
|
||||
time.Sleep(time.Second * 5)
|
||||
goto retry
|
||||
}
|
||||
if c == nil {
|
||||
logs.Error("Error data from server, and will be reconnected in five seconds")
|
||||
time.Sleep(time.Second * 5)
|
||||
goto retry
|
||||
}
|
||||
logs.Info("Successful connection with server %s", s.svrAddr)
|
||||
//monitor the connection
|
||||
go s.ping()
|
||||
s.signal = c
|
||||
//start a channel connection
|
||||
go s.newChan()
|
||||
//start health check if the it's open
|
||||
if s.cnf != nil && len(s.cnf.Healths) > 0 {
|
||||
go heathCheck(s.cnf.Healths, s.signal)
|
||||
}
|
||||
NowStatus = 1
|
||||
//msg connection, eg udp
|
||||
s.handleMain()
|
||||
}
|
||||
|
||||
//handle main connection
|
||||
func (s *TRPClient) handleMain() {
|
||||
for {
|
||||
flags, err := s.signal.ReadFlag()
|
||||
if err != nil {
|
||||
logs.Error("Accept server data error %s, end this service", err.Error())
|
||||
break
|
||||
}
|
||||
switch flags {
|
||||
case common.NEW_UDP_CONN:
|
||||
//read server udp addr and password
|
||||
if lAddr, err := s.signal.GetShortLenContent(); err != nil {
|
||||
logs.Warn(err)
|
||||
return
|
||||
} else if pwd, err := s.signal.GetShortLenContent(); err == nil {
|
||||
var localAddr string
|
||||
//The local port remains unchanged for a certain period of time
|
||||
if v, ok := s.p2pAddr[crypt.Md5(string(pwd)+strconv.Itoa(int(time.Now().Unix()/100)))]; !ok {
|
||||
tmpConn, err := common.GetLocalUdpAddr()
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
localAddr = tmpConn.LocalAddr().String()
|
||||
} else {
|
||||
localAddr = v
|
||||
}
|
||||
go s.newUdpConn(localAddr, string(lAddr), string(pwd))
|
||||
}
|
||||
}
|
||||
}
|
||||
s.Close()
|
||||
}
|
||||
|
||||
func (s *TRPClient) newUdpConn(localAddr, rAddr string, md5Password string) {
|
||||
var localConn net.PacketConn
|
||||
var err error
|
||||
var remoteAddress string
|
||||
if remoteAddress, localConn, err = handleP2PUdp(localAddr, rAddr, md5Password, common.WORK_P2P_PROVIDER); err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
l, err := kcp.ServeConn(nil, 150, 3, localConn)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
logs.Trace("start local p2p udp listen, local address", localConn.LocalAddr().String())
|
||||
for {
|
||||
udpTunnel, err := l.AcceptKCP()
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
l.Close()
|
||||
return
|
||||
}
|
||||
if udpTunnel.RemoteAddr().String() == string(remoteAddress) {
|
||||
conn.SetUdpSession(udpTunnel)
|
||||
logs.Trace("successful connection with client ,address %s", udpTunnel.RemoteAddr().String())
|
||||
//read link info from remote
|
||||
conn.Accept(mux.NewMux(udpTunnel, s.bridgeConnType), func(c net.Conn) {
|
||||
go s.handleChan(c)
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//mux tunnel
|
||||
func (s *TRPClient) newChan() {
|
||||
tunnel, err := NewConn(s.bridgeConnType, s.vKey, s.svrAddr, common.WORK_CHAN, s.proxyUrl)
|
||||
if err != nil {
|
||||
logs.Error("connect to ", s.svrAddr, "error:", err)
|
||||
return
|
||||
}
|
||||
s.tunnel = mux.NewMux(tunnel.Conn, s.bridgeConnType)
|
||||
for {
|
||||
src, err := s.tunnel.Accept()
|
||||
if err != nil {
|
||||
logs.Warn(err)
|
||||
s.Close()
|
||||
break
|
||||
}
|
||||
go s.handleChan(src)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TRPClient) handleChan(src net.Conn) {
|
||||
lk, err := conn.NewConn(src).GetLinkInfo()
|
||||
if err != nil || lk == nil {
|
||||
src.Close()
|
||||
logs.Error("get connection info from server error ", err)
|
||||
return
|
||||
}
|
||||
//host for target processing
|
||||
lk.Host = common.FormatAddress(lk.Host)
|
||||
//if Conn type is http, read the request and log
|
||||
if lk.ConnType == "http" {
|
||||
if targetConn, err := net.DialTimeout(common.CONN_TCP, lk.Host, lk.Option.Timeout); err != nil {
|
||||
logs.Warn("connect to %s error %s", lk.Host, err.Error())
|
||||
src.Close()
|
||||
} else {
|
||||
srcConn := conn.GetConn(src, lk.Crypt, lk.Compress, nil, false)
|
||||
go func() {
|
||||
common.CopyBuffer(srcConn, targetConn)
|
||||
srcConn.Close()
|
||||
targetConn.Close()
|
||||
}()
|
||||
for {
|
||||
if r, err := http.ReadRequest(bufio.NewReader(srcConn)); err != nil {
|
||||
srcConn.Close()
|
||||
targetConn.Close()
|
||||
break
|
||||
} else {
|
||||
logs.Trace("http request, method %s, host %s, url %s, remote address %s", r.Method, r.Host, r.URL.Path, r.RemoteAddr)
|
||||
r.Write(targetConn)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
if lk.ConnType == "udp5" {
|
||||
logs.Trace("new %s connection with the goal of %s, remote address:%s", lk.ConnType, lk.Host, lk.RemoteAddr)
|
||||
s.handleUdp(src)
|
||||
}
|
||||
//connect to target if conn type is tcp or udp
|
||||
if targetConn, err := net.DialTimeout(lk.ConnType, lk.Host, lk.Option.Timeout); err != nil {
|
||||
logs.Warn("connect to %s error %s", lk.Host, err.Error())
|
||||
src.Close()
|
||||
} else {
|
||||
logs.Trace("new %s connection with the goal of %s, remote address:%s", lk.ConnType, lk.Host, lk.RemoteAddr)
|
||||
conn.CopyWaitGroup(src, targetConn, lk.Crypt, lk.Compress, nil, nil, false, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TRPClient) handleUdp(serverConn net.Conn) {
|
||||
// bind a local udp port
|
||||
local, err := net.ListenUDP("udp", nil)
|
||||
defer local.Close()
|
||||
defer serverConn.Close()
|
||||
if err != nil {
|
||||
logs.Error("bind local udp port error ", err.Error())
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
defer serverConn.Close()
|
||||
b := common.BufPoolUdp.Get().([]byte)
|
||||
defer common.BufPoolUdp.Put(b)
|
||||
for {
|
||||
n, raddr, err := local.ReadFrom(b)
|
||||
if err != nil {
|
||||
logs.Error("read data from remote server error", err.Error())
|
||||
}
|
||||
buf := bytes.Buffer{}
|
||||
dgram := common.NewUDPDatagram(common.NewUDPHeader(0, 0, common.ToSocksAddr(raddr)), b[:n])
|
||||
dgram.Write(&buf)
|
||||
b, err := conn.GetLenBytes(buf.Bytes())
|
||||
if err != nil {
|
||||
logs.Warn("get len bytes error", err.Error())
|
||||
continue
|
||||
}
|
||||
if _, err := serverConn.Write(b); err != nil {
|
||||
logs.Error("write data to remote error", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
b := common.BufPoolUdp.Get().([]byte)
|
||||
defer common.BufPoolUdp.Put(b)
|
||||
for {
|
||||
n, err := serverConn.Read(b)
|
||||
if err != nil {
|
||||
logs.Error("read udp data from server error ", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
udpData, err := common.ReadUDPDatagram(bytes.NewReader(b[:n]))
|
||||
if err != nil {
|
||||
logs.Error("unpack data error", err.Error())
|
||||
return
|
||||
}
|
||||
raddr, err := net.ResolveUDPAddr("udp", udpData.Header.Addr.String())
|
||||
if err != nil {
|
||||
logs.Error("build remote addr err", err.Error())
|
||||
continue // drop silently
|
||||
}
|
||||
_, err = local.WriteTo(udpData.Data, raddr)
|
||||
if err != nil {
|
||||
logs.Error("write data to remote ", raddr.String(), "error", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Whether the monitor channel is closed
|
||||
func (s *TRPClient) ping() {
|
||||
s.ticker = time.NewTicker(time.Second * 5)
|
||||
loop:
|
||||
for {
|
||||
select {
|
||||
case <-s.ticker.C:
|
||||
if s.tunnel != nil && s.tunnel.IsClose {
|
||||
s.Close()
|
||||
break loop
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TRPClient) Close() {
|
||||
CloseClient = true
|
||||
NowStatus = 0
|
||||
if s.tunnel != nil {
|
||||
s.tunnel.Close()
|
||||
}
|
||||
if s.signal != nil {
|
||||
s.signal.Close()
|
||||
}
|
||||
if s.ticker != nil {
|
||||
s.ticker.Stop()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,528 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/config"
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"github.com/cnlh/nps/lib/version"
|
||||
"github.com/xtaci/kcp-go"
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
func GetTaskStatus(path string) {
|
||||
cnf, err := config.NewConfig(path)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
c, err := NewConn(cnf.CommonConfig.Tp, cnf.CommonConfig.VKey, cnf.CommonConfig.Server, common.WORK_CONFIG, cnf.CommonConfig.ProxyUrl)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
if _, err := c.Write([]byte(common.WORK_STATUS)); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
//read now vKey and write to server
|
||||
if f, err := common.ReadAllFromFile(filepath.Join(common.GetTmpPath(), "npc_vkey.txt")); err != nil {
|
||||
log.Fatalln(err)
|
||||
} else if _, err := c.Write([]byte(crypt.Md5(string(f)))); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
var isPub bool
|
||||
binary.Read(c, binary.LittleEndian, &isPub)
|
||||
if l, err := c.GetLen(); err != nil {
|
||||
log.Fatalln(err)
|
||||
} else if b, err := c.GetShortContent(l); err != nil {
|
||||
log.Fatalln(err)
|
||||
} else {
|
||||
arr := strings.Split(string(b), common.CONN_DATA_SEQ)
|
||||
for _, v := range cnf.Hosts {
|
||||
if common.InStrArr(arr, v.Remark) {
|
||||
log.Println(v.Remark, "ok")
|
||||
} else {
|
||||
log.Println(v.Remark, "not running")
|
||||
}
|
||||
}
|
||||
for _, v := range cnf.Tasks {
|
||||
ports := common.GetPorts(v.Ports)
|
||||
if v.Mode == "secret" {
|
||||
ports = append(ports, 0)
|
||||
}
|
||||
for _, vv := range ports {
|
||||
var remark string
|
||||
if len(ports) > 1 {
|
||||
remark = v.Remark + "_" + strconv.Itoa(vv)
|
||||
} else {
|
||||
remark = v.Remark
|
||||
}
|
||||
if common.InStrArr(arr, remark) {
|
||||
log.Println(remark, "ok")
|
||||
} else {
|
||||
log.Println(remark, "not running")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
var errAdd = errors.New("The server returned an error, which port or host may have been occupied or not allowed to open.")
|
||||
|
||||
func StartFromFile(path string) {
|
||||
first := true
|
||||
cnf, err := config.NewConfig(path)
|
||||
if err != nil || cnf.CommonConfig == nil {
|
||||
logs.Error("Config file %s loading error %s", path, err.Error())
|
||||
os.Exit(0)
|
||||
}
|
||||
logs.Info("Loading configuration file %s successfully", path)
|
||||
|
||||
re:
|
||||
if first || cnf.CommonConfig.AutoReconnection {
|
||||
if !first {
|
||||
logs.Info("Reconnecting...")
|
||||
time.Sleep(time.Second * 5)
|
||||
}
|
||||
} else {
|
||||
return
|
||||
}
|
||||
first = false
|
||||
c, err := NewConn(cnf.CommonConfig.Tp, cnf.CommonConfig.VKey, cnf.CommonConfig.Server, common.WORK_CONFIG, cnf.CommonConfig.ProxyUrl)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
goto re
|
||||
}
|
||||
var isPub bool
|
||||
binary.Read(c, binary.LittleEndian, &isPub)
|
||||
|
||||
// get tmp password
|
||||
var b []byte
|
||||
vkey := cnf.CommonConfig.VKey
|
||||
if isPub {
|
||||
// send global configuration to server and get status of config setting
|
||||
if _, err := c.SendInfo(cnf.CommonConfig.Client, common.NEW_CONF); err != nil {
|
||||
logs.Error(err)
|
||||
goto re
|
||||
}
|
||||
if !c.GetAddStatus() {
|
||||
logs.Error("the web_user may have been occupied!")
|
||||
goto re
|
||||
}
|
||||
|
||||
if b, err = c.GetShortContent(16); err != nil {
|
||||
logs.Error(err)
|
||||
goto re
|
||||
}
|
||||
vkey = string(b)
|
||||
}
|
||||
ioutil.WriteFile(filepath.Join(common.GetTmpPath(), "npc_vkey.txt"), []byte(vkey), 0600)
|
||||
|
||||
//send hosts to server
|
||||
for _, v := range cnf.Hosts {
|
||||
if _, err := c.SendInfo(v, common.NEW_HOST); err != nil {
|
||||
logs.Error(err)
|
||||
goto re
|
||||
}
|
||||
if !c.GetAddStatus() {
|
||||
logs.Error(errAdd, v.Host)
|
||||
goto re
|
||||
}
|
||||
}
|
||||
|
||||
//send task to server
|
||||
for _, v := range cnf.Tasks {
|
||||
if _, err := c.SendInfo(v, common.NEW_TASK); err != nil {
|
||||
logs.Error(err)
|
||||
goto re
|
||||
}
|
||||
if !c.GetAddStatus() {
|
||||
logs.Error(errAdd, v.Ports, v.Remark)
|
||||
goto re
|
||||
}
|
||||
if v.Mode == "file" {
|
||||
//start local file server
|
||||
go startLocalFileServer(cnf.CommonConfig, v, vkey)
|
||||
}
|
||||
}
|
||||
|
||||
//create local server secret or p2p
|
||||
for _, v := range cnf.LocalServer {
|
||||
go StartLocalServer(v, cnf.CommonConfig)
|
||||
}
|
||||
|
||||
c.Close()
|
||||
if cnf.CommonConfig.Client.WebUserName == "" || cnf.CommonConfig.Client.WebPassword == "" {
|
||||
logs.Notice("web access login username:user password:%s", vkey)
|
||||
} else {
|
||||
logs.Notice("web access login username:%s password:%s", cnf.CommonConfig.Client.WebUserName, cnf.CommonConfig.Client.WebPassword)
|
||||
}
|
||||
NewRPClient(cnf.CommonConfig.Server, vkey, cnf.CommonConfig.Tp, cnf.CommonConfig.ProxyUrl, cnf).Start()
|
||||
CloseLocalServer()
|
||||
goto re
|
||||
}
|
||||
|
||||
// Create a new connection with the server and verify it
|
||||
func NewConn(tp string, vkey string, server string, connType string, proxyUrl string) (*conn.Conn, error) {
|
||||
var err error
|
||||
var connection net.Conn
|
||||
var sess *kcp.UDPSession
|
||||
if tp == "tcp" {
|
||||
if proxyUrl != "" {
|
||||
u, er := url.Parse(proxyUrl)
|
||||
if er != nil {
|
||||
return nil, er
|
||||
}
|
||||
switch u.Scheme {
|
||||
case "socks5":
|
||||
n, er := proxy.FromURL(u, nil)
|
||||
if er != nil {
|
||||
return nil, er
|
||||
}
|
||||
connection, err = n.Dial("tcp", server)
|
||||
default:
|
||||
connection, err = NewHttpProxyConn(u, server)
|
||||
}
|
||||
} else {
|
||||
connection, err = net.Dial("tcp", server)
|
||||
}
|
||||
} else {
|
||||
sess, err = kcp.DialWithOptions(server, nil, 10, 3)
|
||||
if err == nil {
|
||||
conn.SetUdpSession(sess)
|
||||
connection = sess
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
connection.SetDeadline(time.Now().Add(time.Second * 10))
|
||||
defer connection.SetDeadline(time.Time{})
|
||||
c := conn.NewConn(connection)
|
||||
if _, err := c.Write([]byte(common.CONN_TEST)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := c.WriteLenContent([]byte(version.GetVersion())); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := c.WriteLenContent([]byte(version.VERSION)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b, err := c.GetShortContent(32)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
if crypt.Md5(version.GetVersion()) != string(b) {
|
||||
logs.Error("The client does not match the server version. The current core version of the client is", version.GetVersion())
|
||||
return nil, err
|
||||
}
|
||||
if _, err := c.Write([]byte(common.Getverifyval(vkey))); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s, err := c.ReadFlag(); err != nil {
|
||||
return nil, err
|
||||
} else if s == common.VERIFY_EER {
|
||||
return nil, errors.New(fmt.Sprintf("Validation key %s incorrect", vkey))
|
||||
}
|
||||
if _, err := c.Write([]byte(connType)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.SetAlive(tp)
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
//http proxy connection
|
||||
func NewHttpProxyConn(url *url.URL, remoteAddr string) (net.Conn, error) {
|
||||
req := &http.Request{
|
||||
Method: "CONNECT",
|
||||
URL: url,
|
||||
Host: remoteAddr,
|
||||
Header: http.Header{},
|
||||
Proto: "HTTP/1.1",
|
||||
}
|
||||
password, _ := url.User.Password()
|
||||
req.Header.Set("Authorization", "Basic "+basicAuth(strings.Trim(url.User.Username(), " "), password))
|
||||
b, err := httputil.DumpRequest(req, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
proxyConn, err := net.Dial("tcp", url.Host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := proxyConn.Write(b); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf := make([]byte, 1024)
|
||||
if _, err := proxyConn.Read(buf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return proxyConn, nil
|
||||
}
|
||||
|
||||
//get a basic auth string
|
||||
func basicAuth(username, password string) string {
|
||||
auth := username + ":" + password
|
||||
return base64.StdEncoding.EncodeToString([]byte(auth))
|
||||
}
|
||||
|
||||
func getRemoteAddressFromServer(rAddr string, localConn *net.UDPConn, md5Password, role string, add int) error {
|
||||
rAddr, err := getNextAddr(rAddr, add)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return err
|
||||
}
|
||||
addr, err := net.ResolveUDPAddr("udp", rAddr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := localConn.WriteTo(common.GetWriteStr(md5Password, role), addr); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleP2PUdp(localAddr, rAddr, md5Password, role string) (remoteAddress string, c net.PacketConn, err error) {
|
||||
localConn, err := newUdpConnByAddr(localAddr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = getRemoteAddressFromServer(rAddr, localConn, md5Password, role, 0)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
err = getRemoteAddressFromServer(rAddr, localConn, md5Password, role, 1)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
err = getRemoteAddressFromServer(rAddr, localConn, md5Password, role, 2)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
var remoteAddr1, remoteAddr2, remoteAddr3 string
|
||||
for {
|
||||
buf := make([]byte, 1024)
|
||||
if n, addr, er := localConn.ReadFromUDP(buf); er != nil {
|
||||
err = er
|
||||
return
|
||||
} else {
|
||||
rAddr2, _ := getNextAddr(rAddr, 1)
|
||||
rAddr3, _ := getNextAddr(rAddr, 2)
|
||||
switch addr.String() {
|
||||
case rAddr:
|
||||
remoteAddr1 = string(buf[:n])
|
||||
case rAddr2:
|
||||
remoteAddr2 = string(buf[:n])
|
||||
case rAddr3:
|
||||
remoteAddr3 = string(buf[:n])
|
||||
}
|
||||
}
|
||||
if remoteAddr1 != "" && remoteAddr2 != "" && remoteAddr3 != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
if remoteAddress, err = sendP2PTestMsg(localConn, remoteAddr1, remoteAddr2, remoteAddr3); err != nil {
|
||||
return
|
||||
}
|
||||
c, err = newUdpConnByAddr(localAddr)
|
||||
return
|
||||
}
|
||||
|
||||
func sendP2PTestMsg(localConn *net.UDPConn, remoteAddr1, remoteAddr2, remoteAddr3 string) (string, error) {
|
||||
logs.Trace(remoteAddr3, remoteAddr2, remoteAddr1)
|
||||
defer localConn.Close()
|
||||
isClose := false
|
||||
defer func() { isClose = true }()
|
||||
interval, err := getAddrInterval(remoteAddr1, remoteAddr2, remoteAddr3)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
go func() {
|
||||
addr, err := getNextAddr(remoteAddr3, interval)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
remoteUdpAddr, err := net.ResolveUDPAddr("udp", addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
logs.Trace("try send test packet to target %s", addr)
|
||||
ticker := time.NewTicker(time.Millisecond * 500)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
if isClose {
|
||||
return
|
||||
}
|
||||
if _, err := localConn.WriteTo([]byte(common.WORK_P2P_CONNECT), remoteUdpAddr); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
if interval != 0 {
|
||||
ip := common.GetIpByAddr(remoteAddr2)
|
||||
go func() {
|
||||
ports := getRandomPortArr(common.GetPortByAddr(remoteAddr3), common.GetPortByAddr(remoteAddr3)+interval*50)
|
||||
for i := 0; i <= 50; i++ {
|
||||
go func(port int) {
|
||||
trueAddress := ip + ":" + strconv.Itoa(port)
|
||||
logs.Trace("try send test packet to target %s", trueAddress)
|
||||
remoteUdpAddr, err := net.ResolveUDPAddr("udp", trueAddress)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ticker := time.NewTicker(time.Second * 2)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
if isClose {
|
||||
return
|
||||
}
|
||||
if _, err := localConn.WriteTo([]byte(common.WORK_P2P_CONNECT), remoteUdpAddr); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}(ports[i])
|
||||
time.Sleep(time.Millisecond * 10)
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
buf := make([]byte, 10)
|
||||
for {
|
||||
localConn.SetReadDeadline(time.Now().Add(time.Second * 10))
|
||||
n, addr, err := localConn.ReadFromUDP(buf)
|
||||
localConn.SetReadDeadline(time.Time{})
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
switch string(buf[:n]) {
|
||||
case common.WORK_P2P_SUCCESS:
|
||||
for i := 20; i > 0; i-- {
|
||||
if _, err = localConn.WriteTo([]byte(common.WORK_P2P_END), addr); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return addr.String(), nil
|
||||
case common.WORK_P2P_END:
|
||||
logs.Trace("Remotely Address %s Reply Packet Successfully Received", addr.String())
|
||||
return addr.String(), nil
|
||||
case common.WORK_P2P_CONNECT:
|
||||
go func() {
|
||||
for i := 20; i > 0; i-- {
|
||||
logs.Trace("try send receive success packet to target %s", addr.String())
|
||||
if _, err = localConn.WriteTo([]byte(common.WORK_P2P_SUCCESS), addr); err != nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}()
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
return "", errors.New("connect to the target failed, maybe the nat type is not support p2p")
|
||||
}
|
||||
|
||||
func newUdpConnByAddr(addr string) (*net.UDPConn, error) {
|
||||
udpAddr, err := net.ResolveUDPAddr("udp", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
udpConn, err := net.ListenUDP("udp", udpAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return udpConn, nil
|
||||
}
|
||||
|
||||
func getNextAddr(addr string, n int) (string, error) {
|
||||
arr := strings.Split(addr, ":")
|
||||
if len(arr) != 2 {
|
||||
return "", errors.New(fmt.Sprintf("the format of %s incorrect", addr))
|
||||
}
|
||||
if p, err := strconv.Atoi(arr[1]); err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
return arr[0] + ":" + strconv.Itoa(p+n), nil
|
||||
}
|
||||
}
|
||||
|
||||
func getAddrInterval(addr1, addr2, addr3 string) (int, error) {
|
||||
arr1 := strings.Split(addr1, ":")
|
||||
if len(arr1) != 2 {
|
||||
return 0, errors.New(fmt.Sprintf("the format of %s incorrect", addr1))
|
||||
}
|
||||
arr2 := strings.Split(addr2, ":")
|
||||
if len(arr2) != 2 {
|
||||
return 0, errors.New(fmt.Sprintf("the format of %s incorrect", addr2))
|
||||
}
|
||||
arr3 := strings.Split(addr3, ":")
|
||||
if len(arr3) != 2 {
|
||||
return 0, errors.New(fmt.Sprintf("the format of %s incorrect", addr3))
|
||||
}
|
||||
p1, err := strconv.Atoi(arr1[1])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
p2, err := strconv.Atoi(arr2[1])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
p3, err := strconv.Atoi(arr3[1])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
interVal := int(math.Floor(math.Min(math.Abs(float64(p3-p2)), math.Abs(float64(p2-p1)))))
|
||||
if p3-p1 < 0 {
|
||||
return -interVal, nil
|
||||
}
|
||||
return interVal, nil
|
||||
}
|
||||
|
||||
func getRandomPortArr(min, max int) []int {
|
||||
if min > max {
|
||||
min, max = max, min
|
||||
}
|
||||
addrAddr := make([]int, max-min+1)
|
||||
for i := min; i <= max; i++ {
|
||||
addrAddr[max-i] = i
|
||||
}
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
var r, temp int
|
||||
for i := max - min; i > 0; i-- {
|
||||
r = rand.Int() % i
|
||||
temp = addrAddr[i]
|
||||
addrAddr[i] = addrAddr[r]
|
||||
addrAddr[r] = temp
|
||||
}
|
||||
return addrAddr
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"github.com/cnlh/nps/lib/sheap"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var isStart bool
|
||||
var serverConn *conn.Conn
|
||||
|
||||
func heathCheck(healths []*file.Health, c *conn.Conn) bool {
|
||||
serverConn = c
|
||||
if isStart {
|
||||
for _, v := range healths {
|
||||
v.HealthMap = make(map[string]int)
|
||||
}
|
||||
return true
|
||||
}
|
||||
isStart = true
|
||||
h := &sheap.IntHeap{}
|
||||
for _, v := range healths {
|
||||
if v.HealthMaxFail > 0 && v.HealthCheckTimeout > 0 && v.HealthCheckInterval > 0 {
|
||||
v.HealthNextTime = time.Now().Add(time.Duration(v.HealthCheckInterval) * time.Second)
|
||||
heap.Push(h, v.HealthNextTime.Unix())
|
||||
v.HealthMap = make(map[string]int)
|
||||
}
|
||||
}
|
||||
go session(healths, h)
|
||||
return true
|
||||
}
|
||||
|
||||
func session(healths []*file.Health, h *sheap.IntHeap) {
|
||||
for {
|
||||
if h.Len() == 0 {
|
||||
logs.Error("health check error")
|
||||
break
|
||||
}
|
||||
rs := heap.Pop(h).(int64) - time.Now().Unix()
|
||||
if rs <= 0 {
|
||||
continue
|
||||
}
|
||||
timer := time.NewTimer(time.Duration(rs) * time.Second)
|
||||
select {
|
||||
case <-timer.C:
|
||||
for _, v := range healths {
|
||||
if v.HealthNextTime.Before(time.Now()) {
|
||||
v.HealthNextTime = time.Now().Add(time.Duration(v.HealthCheckInterval) * time.Second)
|
||||
//check
|
||||
go check(v)
|
||||
//reset time
|
||||
heap.Push(h, v.HealthNextTime.Unix())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// work when just one port and many target
|
||||
func check(t *file.Health) {
|
||||
arr := strings.Split(t.HealthCheckTarget, ",")
|
||||
var err error
|
||||
var rs *http.Response
|
||||
for _, v := range arr {
|
||||
if t.HealthCheckType == "tcp" {
|
||||
var c net.Conn
|
||||
c, err = net.DialTimeout("tcp", v, time.Duration(t.HealthCheckTimeout)*time.Second)
|
||||
if err == nil {
|
||||
c.Close()
|
||||
}
|
||||
} else {
|
||||
client := &http.Client{}
|
||||
client.Timeout = time.Duration(t.HealthCheckTimeout) * time.Second
|
||||
rs, err = client.Get("http://" + v + t.HttpHealthUrl)
|
||||
if err == nil && rs.StatusCode != 200 {
|
||||
err = errors.New("status code is not match")
|
||||
}
|
||||
}
|
||||
t.Lock()
|
||||
if err != nil {
|
||||
t.HealthMap[v] += 1
|
||||
} else if t.HealthMap[v] >= t.HealthMaxFail {
|
||||
//send recovery add
|
||||
serverConn.SendHealthInfo(v, "1")
|
||||
t.HealthMap[v] = 0
|
||||
}
|
||||
|
||||
if t.HealthMap[v] > 0 && t.HealthMap[v]%t.HealthMaxFail == 0 {
|
||||
//send fail remove
|
||||
serverConn.SendHealthInfo(v, "0")
|
||||
}
|
||||
t.Unlock()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/config"
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"github.com/cnlh/nps/lib/mux"
|
||||
"github.com/cnlh/nps/server/proxy"
|
||||
"github.com/xtaci/kcp-go"
|
||||
)
|
||||
|
||||
var (
|
||||
LocalServer []*net.TCPListener
|
||||
udpConn net.Conn
|
||||
muxSession *mux.Mux
|
||||
fileServer []*http.Server
|
||||
p2pNetBridge *p2pBridge
|
||||
lock sync.RWMutex
|
||||
udpConnStatus bool
|
||||
)
|
||||
|
||||
type p2pBridge struct {
|
||||
}
|
||||
|
||||
func (p2pBridge *p2pBridge) SendLinkInfo(clientId int, link *conn.Link, t *file.Tunnel) (target net.Conn, err error) {
|
||||
for i := 0; muxSession == nil; i++ {
|
||||
if i >= 20 {
|
||||
err = errors.New("p2pBridge:too many times to get muxSession")
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
runtime.Gosched() // waiting for another goroutine establish the mux connection
|
||||
}
|
||||
nowConn, err := muxSession.NewConn()
|
||||
if err != nil {
|
||||
udpConn = nil
|
||||
return nil, err
|
||||
}
|
||||
if _, err := conn.NewConn(nowConn).SendInfo(link, ""); err != nil {
|
||||
udpConnStatus = false
|
||||
return nil, err
|
||||
}
|
||||
return nowConn, nil
|
||||
}
|
||||
|
||||
func CloseLocalServer() {
|
||||
for _, v := range LocalServer {
|
||||
v.Close()
|
||||
}
|
||||
for _, v := range fileServer {
|
||||
v.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func startLocalFileServer(config *config.CommonConfig, t *file.Tunnel, vkey string) {
|
||||
remoteConn, err := NewConn(config.Tp, vkey, config.Server, common.WORK_FILE, config.ProxyUrl)
|
||||
if err != nil {
|
||||
logs.Error("Local connection server failed ", err.Error())
|
||||
return
|
||||
}
|
||||
srv := &http.Server{
|
||||
Handler: http.StripPrefix(t.StripPre, http.FileServer(http.Dir(t.LocalPath))),
|
||||
}
|
||||
logs.Info("start local file system, local path %s, strip prefix %s ,remote port %s ", t.LocalPath, t.StripPre, t.Ports)
|
||||
fileServer = append(fileServer, srv)
|
||||
listener := mux.NewMux(remoteConn.Conn, common.CONN_TCP)
|
||||
logs.Error(srv.Serve(listener))
|
||||
}
|
||||
|
||||
func StartLocalServer(l *config.LocalServer, config *config.CommonConfig) error {
|
||||
if l.Type != "secret" {
|
||||
go handleUdpMonitor(config, l)
|
||||
}
|
||||
task := &file.Tunnel{
|
||||
Port: l.Port,
|
||||
ServerIp: "0.0.0.0",
|
||||
Status: true,
|
||||
Client: &file.Client{
|
||||
Cnf: &file.Config{
|
||||
U: "",
|
||||
P: "",
|
||||
Compress: config.Client.Cnf.Compress,
|
||||
},
|
||||
Status: true,
|
||||
RateLimit: 0,
|
||||
Flow: &file.Flow{},
|
||||
},
|
||||
Flow: &file.Flow{},
|
||||
Target: &file.Target{},
|
||||
}
|
||||
switch l.Type {
|
||||
case "p2ps":
|
||||
logs.Info("successful start-up of local socks5 monitoring, port", l.Port)
|
||||
return proxy.NewSock5ModeServer(p2pNetBridge, task).Start()
|
||||
case "p2pt":
|
||||
logs.Info("successful start-up of local tcp trans monitoring, port", l.Port)
|
||||
return proxy.NewTunnelModeServer(proxy.HandleTrans, p2pNetBridge, task).Start()
|
||||
case "p2p", "secret":
|
||||
listener, err := net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP("0.0.0.0"), l.Port, ""})
|
||||
if err != nil {
|
||||
logs.Error("local listener startup failed port %d, error %s", l.Port, err.Error())
|
||||
return err
|
||||
}
|
||||
LocalServer = append(LocalServer, listener)
|
||||
logs.Info("successful start-up of local tcp monitoring, port", l.Port)
|
||||
conn.Accept(listener, func(c net.Conn) {
|
||||
logs.Trace("new %s connection", l.Type)
|
||||
if l.Type == "secret" {
|
||||
handleSecret(c, config, l)
|
||||
} else if l.Type == "p2p" {
|
||||
handleP2PVisitor(c, config, l)
|
||||
}
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleUdpMonitor(config *config.CommonConfig, l *config.LocalServer) {
|
||||
ticker := time.NewTicker(time.Second * 1)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
if !udpConnStatus {
|
||||
udpConn = nil
|
||||
tmpConn, err := common.GetLocalUdpAddr()
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
for i := 0; i < 10; i++ {
|
||||
logs.Notice("try to connect to the server", i+1)
|
||||
newUdpConn(tmpConn.LocalAddr().String(), config, l)
|
||||
if udpConn != nil {
|
||||
udpConnStatus = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleSecret(localTcpConn net.Conn, config *config.CommonConfig, l *config.LocalServer) {
|
||||
remoteConn, err := NewConn(config.Tp, config.VKey, config.Server, common.WORK_SECRET, config.ProxyUrl)
|
||||
if err != nil {
|
||||
logs.Error("Local connection server failed ", err.Error())
|
||||
return
|
||||
}
|
||||
if _, err := remoteConn.Write([]byte(crypt.Md5(l.Password))); err != nil {
|
||||
logs.Error("Local connection server failed ", err.Error())
|
||||
return
|
||||
}
|
||||
conn.CopyWaitGroup(remoteConn.Conn, localTcpConn, false, false, nil, nil, false, nil)
|
||||
}
|
||||
|
||||
func handleP2PVisitor(localTcpConn net.Conn, config *config.CommonConfig, l *config.LocalServer) {
|
||||
if udpConn == nil {
|
||||
logs.Notice("new conn, P2P can not penetrate successfully, traffic will be transferred through the server")
|
||||
handleSecret(localTcpConn, config, l)
|
||||
return
|
||||
}
|
||||
logs.Trace("start trying to connect with the server")
|
||||
//TODO just support compress now because there is not tls file in client packages
|
||||
link := conn.NewLink(common.CONN_TCP, l.Target, false, config.Client.Cnf.Compress, localTcpConn.LocalAddr().String(), false)
|
||||
if target, err := p2pNetBridge.SendLinkInfo(0, link, nil); err != nil {
|
||||
logs.Error(err)
|
||||
udpConnStatus = false
|
||||
return
|
||||
} else {
|
||||
conn.CopyWaitGroup(target, localTcpConn, false, config.Client.Cnf.Compress, nil, nil, false, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func newUdpConn(localAddr string, config *config.CommonConfig, l *config.LocalServer) {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
remoteConn, err := NewConn(config.Tp, config.VKey, config.Server, common.WORK_P2P, config.ProxyUrl)
|
||||
if err != nil {
|
||||
logs.Error("Local connection server failed ", err.Error())
|
||||
return
|
||||
}
|
||||
if _, err := remoteConn.Write([]byte(crypt.Md5(l.Password))); err != nil {
|
||||
logs.Error("Local connection server failed ", err.Error())
|
||||
return
|
||||
}
|
||||
var rAddr []byte
|
||||
//读取服务端地址、密钥 继续做处理
|
||||
if rAddr, err = remoteConn.GetShortLenContent(); err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
var localConn net.PacketConn
|
||||
var remoteAddress string
|
||||
if remoteAddress, localConn, err = handleP2PUdp(localAddr, string(rAddr), crypt.Md5(l.Password), common.WORK_P2P_VISITOR); err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
udpTunnel, err := kcp.NewConn(remoteAddress, nil, 150, 3, localConn)
|
||||
if err != nil || udpTunnel == nil {
|
||||
logs.Warn(err)
|
||||
return
|
||||
}
|
||||
logs.Trace("successful create a connection with server", remoteAddress)
|
||||
conn.SetUdpSession(udpTunnel)
|
||||
udpConn = udpTunnel
|
||||
muxSession = mux.NewMux(udpConn, "kcp")
|
||||
p2pNetBridge = &p2pBridge{}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
)
|
||||
|
||||
func RegisterLocalIp(server string, vKey string, tp string, proxyUrl string, hour int) {
|
||||
c, err := NewConn(tp, vKey, server, common.WORK_REGISTER, proxyUrl)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
if err := binary.Write(c, binary.LittleEndian, int32(hour)); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
log.Printf("Successful ip registration for local public network, the validity period is %d hours.", hour)
|
||||
os.Exit(0)
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/ccding/go-stun/stun"
|
||||
"github.com/cnlh/nps/client"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/config"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"github.com/cnlh/nps/lib/install"
|
||||
"github.com/cnlh/nps/lib/version"
|
||||
"github.com/kardianos/service"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
serverAddr = flag.String("server", "", "Server addr (ip:port)")
|
||||
configPath = flag.String("config", "", "Configuration file path")
|
||||
verifyKey = flag.String("vkey", "", "Authentication key")
|
||||
logType = flag.String("log", "stdout", "Log output mode(stdout|file)")
|
||||
connType = flag.String("type", "tcp", "Connection type with the server(kcp|tcp)")
|
||||
proxyUrl = flag.String("proxy", "", "proxy socks5 url(eg:socks5://111:222@127.0.0.1:9007)")
|
||||
logLevel = flag.String("log_level", "7", "log level 0~7")
|
||||
registerTime = flag.Int("time", 2, "register time long /h")
|
||||
localPort = flag.Int("local_port", 2000, "p2p local port")
|
||||
password = flag.String("password", "", "p2p password flag")
|
||||
target = flag.String("target", "", "p2p target")
|
||||
localType = flag.String("local_type", "p2p", "p2p target")
|
||||
logPath = flag.String("log_path", "", "npc log path")
|
||||
debug = flag.Bool("debug", true, "npc debug")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
logs.Reset()
|
||||
logs.EnableFuncCallDepth(true)
|
||||
logs.SetLogFuncCallDepth(3)
|
||||
if *logPath == "" {
|
||||
*logPath = common.GetNpcLogPath()
|
||||
}
|
||||
if common.IsWindows() {
|
||||
*logPath = strings.Replace(*logPath, "\\", "\\\\", -1)
|
||||
}
|
||||
if *debug {
|
||||
logs.SetLogger(logs.AdapterConsole, `{"level":`+*logLevel+`,"color":true}`)
|
||||
} else {
|
||||
logs.SetLogger(logs.AdapterFile, `{"level":`+*logLevel+`,"filename":"`+*logPath+`","daily":false,"maxlines":100000,"color":true}`)
|
||||
}
|
||||
|
||||
// init service
|
||||
options := make(service.KeyValue)
|
||||
options["Restart"] = "on-success"
|
||||
options["SuccessExitStatus"] = "1 2 8 SIGKILL"
|
||||
svcConfig := &service.Config{
|
||||
Name: "Npc",
|
||||
DisplayName: "nps内网穿透客户端",
|
||||
Description: "一款轻量级、功能强大的内网穿透代理服务器。支持tcp、udp流量转发,支持内网http代理、内网socks5代理,同时支持snappy压缩、站点保护、加密传输、多路复用、header修改等。支持web图形化管理,集成多用户模式。",
|
||||
Option: options,
|
||||
}
|
||||
if !common.IsWindows() {
|
||||
svcConfig.Dependencies = []string{
|
||||
"Requires=network.target",
|
||||
"After=network-online.target syslog.target"}
|
||||
}
|
||||
for _, v := range os.Args[1:] {
|
||||
switch v {
|
||||
case "install", "start", "stop", "uninstall", "restart":
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(v, "-service=") && !strings.Contains(v, "-debug=") {
|
||||
svcConfig.Arguments = append(svcConfig.Arguments, v)
|
||||
}
|
||||
}
|
||||
svcConfig.Arguments = append(svcConfig.Arguments, "-debug=false")
|
||||
prg := &npc{
|
||||
exit: make(chan struct{}),
|
||||
}
|
||||
s, err := service.New(prg, svcConfig)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
if len(os.Args) >= 2 {
|
||||
switch os.Args[1] {
|
||||
case "status":
|
||||
if len(os.Args) > 2 {
|
||||
path := strings.Replace(os.Args[2], "-config=", "", -1)
|
||||
client.GetTaskStatus(path)
|
||||
}
|
||||
case "register":
|
||||
flag.CommandLine.Parse(os.Args[2:])
|
||||
client.RegisterLocalIp(*serverAddr, *verifyKey, *connType, *proxyUrl, *registerTime)
|
||||
case "update":
|
||||
install.UpdateNpc()
|
||||
return
|
||||
case "nat":
|
||||
nat, host, err := stun.NewClient().Discover()
|
||||
if err != nil || host == nil {
|
||||
logs.Error("get nat type error", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("nat type: %s \npublic address: %s\n", nat.String(), host.String())
|
||||
os.Exit(0)
|
||||
case "install", "start", "stop", "uninstall", "restart":
|
||||
if os.Args[1] == "install" {
|
||||
install.InstallNpc()
|
||||
}
|
||||
err := service.Control(s, os.Args[1])
|
||||
if err != nil {
|
||||
logs.Error("Valid actions: %q\n", service.ControlAction, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
s.Run()
|
||||
}
|
||||
|
||||
type npc struct {
|
||||
exit chan struct{}
|
||||
}
|
||||
|
||||
func (p *npc) Start(s service.Service) error {
|
||||
go p.run()
|
||||
return nil
|
||||
}
|
||||
func (p *npc) Stop(s service.Service) error {
|
||||
close(p.exit)
|
||||
if service.Interactive() {
|
||||
os.Exit(0)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *npc) run() error {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
const size = 64 << 10
|
||||
buf := make([]byte, size)
|
||||
buf = buf[:runtime.Stack(buf, false)]
|
||||
logs.Warning("npc: panic serving %v: %v\n%s", err, string(buf))
|
||||
}
|
||||
}()
|
||||
//p2p or secret command
|
||||
if *password != "" {
|
||||
commonConfig := new(config.CommonConfig)
|
||||
commonConfig.Server = *serverAddr
|
||||
commonConfig.VKey = *verifyKey
|
||||
commonConfig.Tp = *connType
|
||||
localServer := new(config.LocalServer)
|
||||
localServer.Type = *localType
|
||||
localServer.Password = *password
|
||||
localServer.Target = *target
|
||||
localServer.Port = *localPort
|
||||
commonConfig.Client = new(file.Client)
|
||||
commonConfig.Client.Cnf = new(file.Config)
|
||||
go client.StartLocalServer(localServer, commonConfig)
|
||||
return nil
|
||||
}
|
||||
env := common.GetEnvMap()
|
||||
if *serverAddr == "" {
|
||||
*serverAddr, _ = env["NPC_SERVER_ADDR"]
|
||||
}
|
||||
if *verifyKey == "" {
|
||||
*verifyKey, _ = env["NPC_SERVER_VKEY"]
|
||||
}
|
||||
logs.Info("the version of client is %s, the core version of client is %s", version.VERSION, version.GetVersion())
|
||||
if *verifyKey != "" && *serverAddr != "" && *configPath == "" {
|
||||
go func() {
|
||||
for {
|
||||
client.NewRPClient(*serverAddr, *verifyKey, *connType, *proxyUrl, nil).Start()
|
||||
logs.Info("It will be reconnected in five seconds")
|
||||
time.Sleep(time.Second * 5)
|
||||
}
|
||||
}()
|
||||
} else {
|
||||
if *configPath == "" {
|
||||
*configPath = "conf/npc.conf"
|
||||
}
|
||||
go client.StartFromFile(*configPath)
|
||||
}
|
||||
select {
|
||||
case <-p.exit:
|
||||
logs.Warning("stop...")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"C"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/client"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/version"
|
||||
)
|
||||
|
||||
var cl *client.TRPClient
|
||||
|
||||
//export StartClientByVerifyKey
|
||||
func StartClientByVerifyKey(serverAddr, verifyKey, connType, proxyUrl *C.char) int {
|
||||
logs.SetLogger("store")
|
||||
if cl != nil {
|
||||
cl.Close()
|
||||
}
|
||||
cl = client.NewRPClient(C.GoString(serverAddr), C.GoString(verifyKey), C.GoString(connType), C.GoString(proxyUrl), nil)
|
||||
go func() {
|
||||
cl.Start()
|
||||
return
|
||||
}()
|
||||
return 1
|
||||
}
|
||||
|
||||
//export GetClientStatus
|
||||
func GetClientStatus() int {
|
||||
return client.NowStatus
|
||||
}
|
||||
|
||||
//export CloseClient
|
||||
func CloseClient() {
|
||||
if cl != nil {
|
||||
cl.Close()
|
||||
}
|
||||
}
|
||||
|
||||
//export Version
|
||||
func Version() *C.char {
|
||||
return C.CString(version.VERSION)
|
||||
}
|
||||
|
||||
//export Logs
|
||||
func Logs() *C.char {
|
||||
return C.CString(common.GetLogMsg())
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Need a main function to make CGO compile package as C shared library
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"github.com/cnlh/nps/lib/install"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"github.com/cnlh/nps/lib/daemon"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"github.com/cnlh/nps/lib/version"
|
||||
"github.com/cnlh/nps/server"
|
||||
"github.com/cnlh/nps/server/connection"
|
||||
"github.com/cnlh/nps/server/tool"
|
||||
|
||||
"github.com/cnlh/nps/web/routers"
|
||||
"github.com/kardianos/service"
|
||||
)
|
||||
|
||||
var (
|
||||
level string
|
||||
logType = flag.String("log", "stdout", "Log output mode(stdout|file)")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
// init log
|
||||
if err := beego.LoadAppConfig("ini", filepath.Join(common.GetRunPath(), "conf", "nps.conf")); err != nil {
|
||||
log.Fatalln("load config file error", err.Error())
|
||||
}
|
||||
if level = beego.AppConfig.String("log_level"); level == "" {
|
||||
level = "7"
|
||||
}
|
||||
logs.Reset()
|
||||
logs.EnableFuncCallDepth(true)
|
||||
logs.SetLogFuncCallDepth(3)
|
||||
logPath := beego.AppConfig.String("log_path")
|
||||
if logPath == "" {
|
||||
logPath = common.GetLogPath()
|
||||
}
|
||||
if common.IsWindows() {
|
||||
logPath = strings.Replace(logPath, "\\", "\\\\", -1)
|
||||
}
|
||||
logs.SetLogger(logs.AdapterFile, `{"level":`+level+`,"filename":"`+logPath+`","daily":false,"maxlines":100000,"color":true}`)
|
||||
// init service
|
||||
options := make(service.KeyValue)
|
||||
options["Restart"] = "on-success"
|
||||
options["SuccessExitStatus"] = "1 2 8 SIGKILL"
|
||||
svcConfig := &service.Config{
|
||||
Name: "Nps",
|
||||
DisplayName: "nps内网穿透代理服务器",
|
||||
Description: "一款轻量级、功能强大的内网穿透代理服务器。支持tcp、udp流量转发,支持内网http代理、内网socks5代理,同时支持snappy压缩、站点保护、加密传输、多路复用、header修改等。支持web图形化管理,集成多用户模式。",
|
||||
Option: options,
|
||||
}
|
||||
if !common.IsWindows() {
|
||||
svcConfig.Dependencies = []string{
|
||||
"Requires=network.target",
|
||||
"After=network-online.target syslog.target"}
|
||||
}
|
||||
prg := &nps{}
|
||||
prg.exit = make(chan struct{})
|
||||
s, err := service.New(prg, svcConfig)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
if len(os.Args) > 1 {
|
||||
switch os.Args[1] {
|
||||
case "debug":
|
||||
logs.SetLogger(logs.AdapterConsole, `{"level":`+level+`,"color":true}`)
|
||||
case "reload":
|
||||
daemon.InitDaemon("nps", common.GetRunPath(), common.GetTmpPath())
|
||||
return
|
||||
case "install":
|
||||
// uninstall before
|
||||
service.Control(s, "uninstall")
|
||||
|
||||
binPath := install.InstallNps()
|
||||
svcConfig.Executable = binPath
|
||||
s, err := service.New(prg, svcConfig)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
err = service.Control(s, os.Args[1])
|
||||
if err != nil {
|
||||
logs.Error("Valid actions: %q\n", service.ControlAction, err.Error())
|
||||
}
|
||||
return
|
||||
case "start", "restart", "stop", "uninstall":
|
||||
err := service.Control(s, os.Args[1])
|
||||
if err != nil {
|
||||
logs.Error("Valid actions: %q\n", service.ControlAction, err.Error())
|
||||
}
|
||||
return
|
||||
case "update":
|
||||
install.UpdateNps()
|
||||
return
|
||||
default:
|
||||
logs.Error("command is not support")
|
||||
return
|
||||
}
|
||||
}
|
||||
s.Run()
|
||||
}
|
||||
|
||||
type nps struct {
|
||||
exit chan struct{}
|
||||
}
|
||||
|
||||
func (p *nps) Start(s service.Service) error {
|
||||
p.run()
|
||||
return nil
|
||||
}
|
||||
func (p *nps) Stop(s service.Service) error {
|
||||
close(p.exit)
|
||||
if service.Interactive() {
|
||||
os.Exit(0)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *nps) run() error {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
const size = 64 << 10
|
||||
buf := make([]byte, size)
|
||||
buf = buf[:runtime.Stack(buf, false)]
|
||||
logs.Warning("nps: panic serving %v: %v\n%s", err, string(buf))
|
||||
}
|
||||
}()
|
||||
routers.Init()
|
||||
task := &file.Tunnel{
|
||||
Mode: "webServer",
|
||||
}
|
||||
bridgePort, err := beego.AppConfig.Int("bridge_port")
|
||||
if err != nil {
|
||||
logs.Error("Getting bridge_port error", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
logs.Info("the version of server is %s ,allow client core version to be %s", version.VERSION, version.GetVersion())
|
||||
connection.InitConnectionService()
|
||||
crypt.InitTls(filepath.Join(common.GetRunPath(), "conf", "server.pem"), filepath.Join(common.GetRunPath(), "conf", "server.key"))
|
||||
tool.InitAllowPort()
|
||||
tool.StartSystemInfo()
|
||||
go server.StartNewServer(bridgePort, task, beego.AppConfig.String("bridge_type"))
|
||||
select {
|
||||
case <-p.exit:
|
||||
logs.Warning("stop...")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
appname = easyProxy
|
||||
|
||||
#web管理端口
|
||||
httpport = 8080
|
||||
|
||||
#启动模式dev|pro
|
||||
runmode = pro
|
||||
|
||||
#web管理密码
|
||||
password=123
|
||||
|
||||
#http监听端口
|
||||
hostPort=80
|
||||
@@ -1,3 +0,0 @@
|
||||
b.proxy.com,127.0.0.1:82,o2430bnq22jgnmcl
|
||||
b.o.com,127.0.0.1:88,ts08z6vk5nc72fs8
|
||||
a.o.com,127.0.0.1:88,ts08z6vk5nc72fs8
|
||||
|
@@ -0,0 +1,2 @@
|
||||
# key -> user | value -> pwd
|
||||
npc=npc.pwd
|
||||
@@ -0,0 +1,76 @@
|
||||
[common]
|
||||
server_addr=127.0.0.1:8024
|
||||
conn_type=tcp
|
||||
vkey=123
|
||||
auto_reconnection=true
|
||||
max_conn=1000
|
||||
flow_limit=1000
|
||||
rate_limit=1000
|
||||
basic_username=11
|
||||
basic_password=3
|
||||
web_username=user
|
||||
web_password=1234
|
||||
crypt=true
|
||||
compress=true
|
||||
|
||||
[health_check_test1]
|
||||
health_check_timeout=1
|
||||
health_check_max_failed=3
|
||||
health_check_interval=1
|
||||
health_http_url=/
|
||||
health_check_type=http
|
||||
health_check_target=127.0.0.1:8083,127.0.0.1:8082
|
||||
|
||||
[health_check_test2]
|
||||
health_check_timeout=1
|
||||
health_check_max_failed=3
|
||||
health_check_interval=1
|
||||
health_check_type=tcp
|
||||
health_check_target=127.0.0.1:8083,127.0.0.1:8082
|
||||
|
||||
[web]
|
||||
host=c.o.com
|
||||
target_addr=127.0.0.1:8083,127.0.0.1:8082
|
||||
|
||||
[tcp]
|
||||
mode=tcp
|
||||
target_addr=127.0.0.1:8080
|
||||
server_port=10000
|
||||
|
||||
[socks5]
|
||||
mode=socks5
|
||||
server_port=19009
|
||||
multi_account=multi_account.conf
|
||||
|
||||
[file]
|
||||
mode=file
|
||||
server_port=19008
|
||||
local_path=/Users/liuhe/Downloads
|
||||
strip_pre=/web/
|
||||
|
||||
[http]
|
||||
mode=httpProxy
|
||||
server_port=19004
|
||||
|
||||
[udp]
|
||||
mode=udp
|
||||
server_port=12253
|
||||
target_addr=114.114.114.114:53
|
||||
|
||||
[ssh_secret]
|
||||
mode=secret
|
||||
password=ssh2
|
||||
target_addr=123.206.77.88:22
|
||||
|
||||
[ssh_p2p]
|
||||
mode=p2p
|
||||
password=ssh3
|
||||
|
||||
[secret_ssh]
|
||||
local_port=2001
|
||||
password=ssh2
|
||||
|
||||
[p2p_ssh]
|
||||
local_port=2002
|
||||
password=ssh3
|
||||
target_addr=123.206.77.88:22
|
||||
@@ -0,0 +1,76 @@
|
||||
appname = nps
|
||||
#Boot mode(dev|pro)
|
||||
runmode = dev
|
||||
|
||||
#HTTP(S) proxy port, no startup if empty
|
||||
http_proxy_ip=0.0.0.0
|
||||
http_proxy_port=80
|
||||
https_proxy_port=443
|
||||
https_just_proxy=true
|
||||
#default https certificate setting
|
||||
https_default_cert_file=conf/server.pem
|
||||
https_default_key_file=conf/server.key
|
||||
|
||||
##bridge
|
||||
bridge_type=tcp
|
||||
bridge_port=8024
|
||||
bridge_ip=0.0.0.0
|
||||
|
||||
# Public password, which clients can use to connect to the server
|
||||
# After the connection, the server will be able to open relevant ports and parse related domain names according to its own configuration file.
|
||||
public_vkey=123
|
||||
|
||||
#Traffic data persistence interval(minute)
|
||||
#Ignorance means no persistence
|
||||
#flow_store_interval=1
|
||||
|
||||
# log level LevelEmergency->0 LevelAlert->1 LevelCritical->2 LevelError->3 LevelWarning->4 LevelNotice->5 LevelInformational->6 LevelDebug->7
|
||||
log_level=7
|
||||
#log_path=nps.log
|
||||
|
||||
#Whether to restrict IP access, true or false or ignore
|
||||
#ip_limit=true
|
||||
|
||||
#p2p
|
||||
#p2p_ip=127.0.0.1
|
||||
#p2p_port=6000
|
||||
|
||||
#web
|
||||
web_host=a.o.com
|
||||
web_username=admin
|
||||
web_password=123
|
||||
web_port = 8080
|
||||
web_ip=0.0.0.0
|
||||
web_base_url=
|
||||
web_open_ssl=false
|
||||
web_cert_file=conf/server.pem
|
||||
web_key_file=conf/server.key
|
||||
# if web under proxy use sub path. like http://host/nps need this.
|
||||
#web_base_url=/nps
|
||||
|
||||
#Web API unauthenticated IP address(the len of auth_crypt_key must be 16)
|
||||
auth_key=test
|
||||
auth_crypt_key =1234567812345678
|
||||
|
||||
#allow_ports=9001-9009,10001,11000-12000
|
||||
|
||||
#Web management multi-user login
|
||||
allow_user_login=false
|
||||
allow_user_register=false
|
||||
allow_user_change_username=false
|
||||
|
||||
|
||||
#extension
|
||||
allow_flow_limit=false
|
||||
allow_rate_limit=false
|
||||
allow_tunnel_num_limit=false
|
||||
allow_local_proxy=false
|
||||
allow_connection_num_limit=false
|
||||
allow_multi_ip=false
|
||||
system_info_display=false
|
||||
|
||||
#cache
|
||||
http_cache=false
|
||||
http_cache_length=100
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEpAIBAAKCAQEA2MVLOHvgU8FCp6LgQrPfaWcGygrsRk7TL9hbT8MxbCRUSLV7
|
||||
Lbt3q5Knz8eTN4NWmwE6L5glOcH2x3Hnn+hPjbvgq35XBBIccAm0cYYKqoKkikeK
|
||||
FZM0Gp/WhSrhJ4laTyQqyleIFKpwD9kHDiC/sxjGDhSFmHKhhAnsQIRm2tppFXX0
|
||||
aAMqJEm88jzk1BN2QtKjEAn1u8v1+QW1KP3WuzdXH4L7hhMll66/KIm6Hfs2FRHQ
|
||||
pRUWqZeJY4q79NW5p5f+siGwOsGpxb/p11pM+0xnCH3UIFbm3zCTzP4sLvkfFGAe
|
||||
yAHsAwmaP8dJxh40ej3NN8uNiNvt8nw2Vb/1LwIDAQABAoIBAD40x/RKoEKIyE8B
|
||||
D6g0pB1EQo+CePFoN3SYewO1uR4WgtVmtxWVoa7r5BpdZGLe3uCWhpMX7z7W6bGs
|
||||
f1LFQOckjkHIfMIfTGfecRjO5Yqu+Pbxtq+gUah+S/plJr3IzdC+SUVNvzBnBMeX
|
||||
eU3Vmg2UQ2nQ+9GWu8D/c/vDwxx0X8oQ2G8QaxX0tUurlSMNA3M7xySwEvhx54fO
|
||||
UrDF3Q4yF48eA4butxVLFWf3cnlY+nR8uYd2vKfmp689/8C6kkfoM9igB78e93sm
|
||||
uDM2eRLm4kU5WLl301T42n6AF7w8J0MhLLVOIeLs4l5gZPa3uKvYFmuHQao7e/5R
|
||||
U/jHKrECgYEA8alPXuxFSVOvdhIsSN//Frj9CdExVdYmaLkt/2LO4FMnOaWh1xh7
|
||||
5iCY1bJT8D9dhfbqRg3qW2oguZD8gu04R8fTRegQ89qmAIwsEYqVf9salR41lZU4
|
||||
Rc+5yc7O11WIe9Lzu+ONFBFkAh3UFMR4zVZ/JhKIG/P5Srm7SUdKW2cCgYEA5aHo
|
||||
x2LR+yKhjkrBzHG3Qrfy1PtlYHjOpYYAKHQcBFuiG08W3CK/vkYl+mhv0uyhT7mn
|
||||
q6NDqrpZPRnDlOoEqgRS1X/QWKN6Pgd4HNLIawvp0vK9jYXDPcAXFzVthXCIwFcn
|
||||
3a3m4cHiuLdRNOHkydiHQyTOF6eEneN07TDvwvkCgYEApzOd1u9igPmFzQuF2GYi
|
||||
+HXFnaU/nUQuDwcQ7EJRIKRn31raPxiRoQesty5LJU6yRp4wOYgnPliPi9Tk4TGA
|
||||
XynC4/tMv2vorzhMxVY9Wdke602bhYNZC/RNd3O/aP2lEQdD3Bv04I2nxE8fDb9i
|
||||
VbAjCRSJV83WDf2zt1+78sECgYEAzezjRiKdcZu9y0/I+WEk2cUCE/MaF2he0FsZ
|
||||
uy1cjp/qAJltQ5452xUnK6cKWNlxU4CHF0mC/hC8xCldliZCZoEYE3PaUBLSJdwm
|
||||
35o6tpxpZI3gZJCG5NJlIp/8BkVDrVC7ZHV17hAkFEf4n/bPaB8wNYtE8jt8luaK
|
||||
TcarzGkCgYBn2alN0RLN2PHDurraFZB6GuCvh/arEjSCY3SDFQPF10CVjTDV7sx3
|
||||
eqJkwJ81syTmfJwZIceWbOFGgsuSx37UrQAVlHZSvzeqEg9dA5HqSoOACyidJI7j
|
||||
RG2+HB+KpsIZjGgLrEM4i7VOpYUDRdaouIXngFq/t9HNT+MDck5/Lw==
|
||||
-----END RSA PRIVATE KEY-----
|
||||
@@ -0,0 +1,22 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIDtTCCAp2gAwIBAgIJAPXRSiP0Fs7sMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
|
||||
BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX
|
||||
aWRnaXRzIFB0eSBMdGQwHhcNMTcxMTA3MDg1MzQ2WhcNMjcxMTA1MDg1MzQ2WjBF
|
||||
MQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50
|
||||
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
|
||||
CgKCAQEA2MVLOHvgU8FCp6LgQrPfaWcGygrsRk7TL9hbT8MxbCRUSLV7Lbt3q5Kn
|
||||
z8eTN4NWmwE6L5glOcH2x3Hnn+hPjbvgq35XBBIccAm0cYYKqoKkikeKFZM0Gp/W
|
||||
hSrhJ4laTyQqyleIFKpwD9kHDiC/sxjGDhSFmHKhhAnsQIRm2tppFXX0aAMqJEm8
|
||||
8jzk1BN2QtKjEAn1u8v1+QW1KP3WuzdXH4L7hhMll66/KIm6Hfs2FRHQpRUWqZeJ
|
||||
Y4q79NW5p5f+siGwOsGpxb/p11pM+0xnCH3UIFbm3zCTzP4sLvkfFGAeyAHsAwma
|
||||
P8dJxh40ej3NN8uNiNvt8nw2Vb/1LwIDAQABo4GnMIGkMB0GA1UdDgQWBBQdPc0R
|
||||
a8alY6Ab7voidkTGaH4PxzB1BgNVHSMEbjBsgBQdPc0Ra8alY6Ab7voidkTGaH4P
|
||||
x6FJpEcwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNV
|
||||
BAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZIIJAPXRSiP0Fs7sMAwGA1UdEwQF
|
||||
MAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAH1IZNkjuvt2nZPzXsuiVNyCE1vm346z
|
||||
naE0Uzt3aseAN9m/iiB8mLz+ryvWc2aFMX5lTdsHdm2rqmqBCBXeRwTLf4OeHIju
|
||||
ZQW6makWt6PxANEo6gbdPbQXbS420ssUhnR2irIH1SdI31iikVFPdiS0baRRE/gS
|
||||
+440M1jOOOnKm0Qin92ejsshmji/0qaD2+6D5TNw4HmIZaFTBw+kfjxCL6trfeBn
|
||||
4fT0RJ121V3G3+AtG5sWQ93B3pCg+jtD+fGKkNSLhphq84bD1Zv7l73QGOoylkEn
|
||||
Sc0ajTLOXFBb83yRdlgV3Da95jH9rDZ4jSod48m+KemoZTDQw0vSwAU=
|
||||
-----END CERTIFICATE-----
|
||||
@@ -1,3 +0,0 @@
|
||||
8001,tunnelServer,127.0.0.1:88,jq5i7n0sjs1h0jje,aaa,bbb,,1,0,0,0,1
|
||||
0,hostServer,,ts08z6vk5nc72fs8,aaac,bbb,snappy,1,0,1,2,3
|
||||
0,hostServer,,7n7bxc2bm1fyjfab,ab,b,,1,1,1,0,1
|
||||
|
@@ -1,20 +0,0 @@
|
||||
{
|
||||
"Server": {
|
||||
"ip": "123.206.77.88",
|
||||
"tcp": 8224,
|
||||
"vkey": "DKibZF5TXvic1g3kY",
|
||||
"num": 10
|
||||
},
|
||||
"SiteList": [
|
||||
{
|
||||
"host": "a.server.ourcauc.com",
|
||||
"url": "10.1.50.203",
|
||||
"port": 80
|
||||
},
|
||||
{
|
||||
"host": "b.server.ourcauc.com",
|
||||
"url": "10.1.50.196",
|
||||
"port": 4000
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/cnlh/easyProxy/lib"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type BaseController struct {
|
||||
beego.Controller
|
||||
controllerName string
|
||||
actionName string
|
||||
}
|
||||
|
||||
//初始化参数
|
||||
func (s *BaseController) Prepare() {
|
||||
controllerName, actionName := s.GetControllerAndAction()
|
||||
s.controllerName = strings.ToLower(controllerName[0 : len(controllerName)-10])
|
||||
s.actionName = strings.ToLower(actionName)
|
||||
if s.GetSession("auth") != true {
|
||||
s.Redirect("/login/index", 302)
|
||||
}
|
||||
}
|
||||
|
||||
//加载模板
|
||||
func (s *BaseController) display(tpl ...string) {
|
||||
var tplname string
|
||||
if len(tpl) > 0 {
|
||||
tplname = strings.Join([]string{tpl[0], "html"}, ".")
|
||||
} else {
|
||||
tplname = s.controllerName + "/" + s.actionName + ".html"
|
||||
}
|
||||
s.Data["menu"] = s.actionName
|
||||
ip := s.Ctx.Request.Host
|
||||
s.Data["ip"] = lib.Gethostbyname(ip[0:strings.LastIndex(ip, ":")])
|
||||
s.Data["p"] = *lib.TcpPort
|
||||
s.Data["proxyPort"] = beego.AppConfig.String("hostPort")
|
||||
s.Layout = "public/layout.html"
|
||||
s.TplName = tplname
|
||||
}
|
||||
|
||||
//错误
|
||||
func (s *BaseController) error() {
|
||||
s.Layout = "public/layout.html"
|
||||
s.TplName = "public/error.html"
|
||||
}
|
||||
|
||||
//去掉没有err返回值的int
|
||||
func (s *BaseController) GetIntNoErr(key string, def ...int) int {
|
||||
strv := s.Ctx.Input.Query(key)
|
||||
if len(strv) == 0 && len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
val, _ := strconv.Atoi(strv)
|
||||
return val
|
||||
}
|
||||
|
||||
//获取去掉错误的bool值
|
||||
func (s *BaseController) GetBoolNoErr(key string, def ...bool) bool {
|
||||
strv := s.Ctx.Input.Query(key)
|
||||
if len(strv) == 0 && len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
val, _ := strconv.ParseBool(strv)
|
||||
return val
|
||||
}
|
||||
|
||||
//ajax正确返回
|
||||
func (s *BaseController) AjaxOk(str string) {
|
||||
s.Data["json"] = ajax(str, 1)
|
||||
s.ServeJSON()
|
||||
s.StopRun()
|
||||
}
|
||||
|
||||
//ajax错误返回
|
||||
func (s *BaseController) AjaxErr(str string) {
|
||||
s.Data["json"] = ajax(str, 0)
|
||||
s.ServeJSON()
|
||||
s.StopRun()
|
||||
}
|
||||
|
||||
//组装ajax
|
||||
func ajax(str string, status int) (map[string]interface{}) {
|
||||
json := make(map[string]interface{})
|
||||
json["status"] = status
|
||||
json["msg"] = str
|
||||
return json
|
||||
}
|
||||
|
||||
//ajax table返回
|
||||
func (s *BaseController) AjaxTable(list interface{}, cnt int, recordsTotal int) {
|
||||
json := make(map[string]interface{})
|
||||
json["data"] = list
|
||||
json["draw"] = s.GetIntNoErr("draw")
|
||||
json["err"] = ""
|
||||
json["recordsTotal"] = recordsTotal
|
||||
json["recordsFiltered"] = cnt
|
||||
s.Data["json"] = json
|
||||
s.ServeJSON()
|
||||
s.StopRun()
|
||||
}
|
||||
|
||||
//ajax table参数
|
||||
func (s *BaseController) GetAjaxParams() (start, limit int) {
|
||||
s.Ctx.Input.Bind(&start, "start")
|
||||
s.Ctx.Input.Bind(&limit, "length")
|
||||
return
|
||||
}
|
||||
|
||||
func (s *BaseController) SetInfo(name string) {
|
||||
s.Data["name"] = name
|
||||
}
|
||||
|
||||
func (s *BaseController) SetType(name string) {
|
||||
s.Data["type"] = name
|
||||
}
|
||||
@@ -1,169 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/cnlh/easyProxy/lib"
|
||||
)
|
||||
|
||||
type IndexController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
func (s *IndexController) Index() {
|
||||
s.SetInfo("使用说明")
|
||||
s.display("index/index")
|
||||
}
|
||||
|
||||
func (s *IndexController) Tcp() {
|
||||
s.SetInfo("tcp隧道管理")
|
||||
s.SetType("tunnelServer")
|
||||
s.display("index/list")
|
||||
}
|
||||
|
||||
func (s *IndexController) Udp() {
|
||||
s.SetInfo("udp隧道管理")
|
||||
s.SetType("udpServer")
|
||||
s.display("index/list")
|
||||
}
|
||||
|
||||
func (s *IndexController) Socks5() {
|
||||
s.SetInfo("socks5管理")
|
||||
s.SetType("sock5Server")
|
||||
s.display("index/list")
|
||||
}
|
||||
|
||||
func (s *IndexController) Http() {
|
||||
s.SetInfo("http代理管理")
|
||||
s.SetType("httpProxyServer")
|
||||
s.display("index/list")
|
||||
}
|
||||
|
||||
func (s *IndexController) Host() {
|
||||
s.SetInfo("host模式管理")
|
||||
s.SetType("hostServer")
|
||||
s.display("index/list")
|
||||
}
|
||||
|
||||
func (s *IndexController) GetServerConfig() {
|
||||
start, length := s.GetAjaxParams()
|
||||
taskType := s.GetString("type")
|
||||
list, cnt := lib.CsvDb.GetServerConfig(start, length, taskType)
|
||||
s.AjaxTable(list, cnt, cnt)
|
||||
}
|
||||
|
||||
func (s *IndexController) Add() {
|
||||
if s.Ctx.Request.Method == "GET" {
|
||||
s.Data["type"] = s.GetString("type")
|
||||
s.SetInfo("新增")
|
||||
s.display()
|
||||
} else {
|
||||
t := &lib.ServerConfig{
|
||||
TcpPort: s.GetIntNoErr("port"),
|
||||
Mode: s.GetString("type"),
|
||||
Target: s.GetString("target"),
|
||||
VerifyKey: lib.GetRandomString(16),
|
||||
U: s.GetString("u"),
|
||||
P: s.GetString("p"),
|
||||
Compress: s.GetString("compress"),
|
||||
Crypt: lib.GetBoolByStr(s.GetString("crypt")),
|
||||
Mux: lib.GetBoolByStr(s.GetString("mux")),
|
||||
IsRun: 0,
|
||||
}
|
||||
lib.CsvDb.NewTask(t)
|
||||
if err := lib.AddTask(t); err != nil {
|
||||
s.AjaxErr(err.Error())
|
||||
} else {
|
||||
s.AjaxOk("添加成功")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *IndexController) Edit() {
|
||||
if s.Ctx.Request.Method == "GET" {
|
||||
vKey := s.GetString("vKey")
|
||||
if t, err := lib.CsvDb.GetTask(vKey); err != nil {
|
||||
s.error()
|
||||
} else {
|
||||
s.Data["t"] = t
|
||||
}
|
||||
s.SetInfo("修改")
|
||||
s.display()
|
||||
} else {
|
||||
vKey := s.GetString("vKey")
|
||||
if t, err := lib.CsvDb.GetTask(vKey); err != nil {
|
||||
s.error()
|
||||
} else {
|
||||
t.TcpPort = s.GetIntNoErr("port")
|
||||
t.Mode = s.GetString("type")
|
||||
t.Target = s.GetString("target")
|
||||
t.U = s.GetString("u")
|
||||
t.P = s.GetString("p")
|
||||
t.Compress = s.GetString("compress")
|
||||
t.Crypt = lib.GetBoolByStr(s.GetString("crypt"))
|
||||
t.Mux = lib.GetBoolByStr(s.GetString("mux"))
|
||||
lib.CsvDb.UpdateTask(t)
|
||||
lib.StopServer(t.VerifyKey)
|
||||
lib.StartTask(t.VerifyKey)
|
||||
}
|
||||
s.AjaxOk("修改成功")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *IndexController) Stop() {
|
||||
vKey := s.GetString("vKey")
|
||||
if err := lib.StopServer(vKey); err != nil {
|
||||
s.AjaxErr("停止失败")
|
||||
}
|
||||
s.AjaxOk("停止成功")
|
||||
}
|
||||
func (s *IndexController) Del() {
|
||||
vKey := s.GetString("vKey")
|
||||
if err := lib.DelTask(vKey); err != nil {
|
||||
s.AjaxErr("删除失败")
|
||||
}
|
||||
s.AjaxOk("删除成功")
|
||||
}
|
||||
|
||||
func (s *IndexController) Start() {
|
||||
vKey := s.GetString("vKey")
|
||||
if err := lib.StartTask(vKey); err != nil {
|
||||
s.AjaxErr("开启失败")
|
||||
}
|
||||
s.AjaxOk("开启成功")
|
||||
}
|
||||
|
||||
func (s *IndexController) HostList() {
|
||||
if s.Ctx.Request.Method == "GET" {
|
||||
s.Data["vkey"] = s.GetString("vkey")
|
||||
s.SetInfo("域名列表")
|
||||
s.display("index/hlist")
|
||||
} else {
|
||||
start, length := s.GetAjaxParams()
|
||||
vkey := s.GetString("vkey")
|
||||
list, cnt := lib.CsvDb.GetHostList(start, length, vkey)
|
||||
s.AjaxTable(list, cnt, cnt)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *IndexController) DelHost() {
|
||||
host := s.GetString("host")
|
||||
if err := lib.CsvDb.DelHost(host); err != nil {
|
||||
s.AjaxErr("删除失败")
|
||||
}
|
||||
s.AjaxOk("删除成功")
|
||||
}
|
||||
|
||||
func (s *IndexController) AddHost() {
|
||||
if s.Ctx.Request.Method == "GET" {
|
||||
s.Data["vkey"] = s.GetString("vkey")
|
||||
s.SetInfo("新增")
|
||||
s.display("index/hadd")
|
||||
} else {
|
||||
h := &lib.HostList{
|
||||
Vkey: s.GetString("vkey"),
|
||||
Host: s.GetString("host"),
|
||||
Target: s.GetString("target"),
|
||||
}
|
||||
lib.CsvDb.NewHost(h)
|
||||
s.AjaxOk("添加成功")
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
type LoginController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (self *LoginController) Index() {
|
||||
self.TplName = "login/index.html"
|
||||
}
|
||||
func (self *LoginController) Verify() {
|
||||
if self.GetString("psd") == beego.AppConfig.String("password") {
|
||||
self.SetSession("auth", true)
|
||||
self.Data["json"] = map[string]interface{}{"status": 1, "msg": "验证成功"}
|
||||
self.ServeJSON()
|
||||
} else {
|
||||
self.Data["json"] = map[string]interface{}{"status": 0, "msg": "验证失败"}
|
||||
self.ServeJSON()
|
||||
}
|
||||
}
|
||||
func (self *LoginController) Out() {
|
||||
self.SetSession("auth", false)
|
||||
self.Redirect("/login/index", 302)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
# nps
|
||||
 
|
||||
[](https://gitter.im/cnlh-nps/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
||||
[](https://travis-ci.org/cnlh/nps)
|
||||
|
||||
nps是一款轻量级、高性能、功能强大的**内网穿透**代理服务器。目前支持**tcp、udp流量转发**,可支持任何**tcp、udp**上层协议(访问内网网站、本地支付接口调试、ssh访问、远程桌面,内网dns解析等等……),此外还**支持内网http代理、内网socks5代理**、**p2p等**,并带有功能强大的web管理端。
|
||||
|
||||
|
||||
## 背景
|
||||

|
||||
|
||||
1. 做微信公众号开发、小程序开发等----> 域名代理模式
|
||||
|
||||
|
||||
2. 想在外网通过ssh连接内网的机器,做云服务器到内网服务器端口的映射,----> tcp代理模式
|
||||
|
||||
3. 在非内网环境下使用内网dns,或者需要通过udp访问内网机器等----> udp代理模式
|
||||
|
||||
4. 在外网使用HTTP代理访问内网站点----> http代理模式
|
||||
|
||||
5. 搭建一个内网穿透ss,在外网如同使用内网vpn一样访问内网资源或者设备----> socks5代理模式
|
||||
@@ -0,0 +1,14 @@
|
||||

|
||||
|
||||
# NPS <small>0.25.1</small>
|
||||
|
||||
> 一款轻量级、高性能、功能强大的内网穿透代理服务器
|
||||
|
||||
- 支持tcp、udp流量转发
|
||||
- 支持内网http代理、内网socks5代理、p2p
|
||||
- 简洁方便的WEB管理界面
|
||||
- 服务端控制
|
||||
|
||||
|
||||
[GitHub](https://github.com/cnlh/nps/)
|
||||
[开始使用](#nps)
|
||||
@@ -0,0 +1,28 @@
|
||||
* 入门
|
||||
* [安装](install.md)
|
||||
* [启动](run.md)
|
||||
* [使用示例](example.md)
|
||||
* 服务端
|
||||
* [介绍](introduction.md)
|
||||
* [使用](nps_use.md)
|
||||
* [配置文件](server_config.md)
|
||||
* [增强功能](nps_extend.md)
|
||||
|
||||
* 客户端
|
||||
|
||||
* [基本使用](use.md)
|
||||
* [增强功能](npc_extend.md)
|
||||
|
||||
* 扩展
|
||||
|
||||
* [功能](feature.md)
|
||||
* [说明](description.md)
|
||||
* [web api](api.md)
|
||||
* [sdk](npc_sdk.md)
|
||||
git
|
||||
* 其他
|
||||
|
||||
* [贡献](contribute.md)
|
||||
* [捐助](donate.md)
|
||||
* [致谢](thanks.md)
|
||||
* [交流](discuss.md)
|
||||
@@ -0,0 +1,46 @@
|
||||
# web api
|
||||
## webAPI验证说明
|
||||
- 采用auth_key的验证方式
|
||||
- 在提交的每个请求后面附带两个参数,`auth_key` 和`timestamp`
|
||||
|
||||
```
|
||||
auth_key的生成方式为:md5(配置文件中的auth_key+当前时间戳)
|
||||
```
|
||||
|
||||
```
|
||||
timestamp为当前时间戳
|
||||
```
|
||||
```
|
||||
curl --request POST \
|
||||
--url http://127.0.0.1:8080/client/list \
|
||||
--data 'auth_key=2a0000d9229e7dbcf79dd0f5e04bb084×tamp=1553045344&start=0&limit=10'
|
||||
```
|
||||
**注意:** 为保证安全,时间戳的有效范围为20秒内,所以每次提交请求必须重新生成。
|
||||
|
||||
## 获取服务端时间
|
||||
由于服务端与api请求的客户端时间差异不能太大,所以提供了一个可以获取服务端时间的接口
|
||||
|
||||
```
|
||||
POST /auth/gettime
|
||||
```
|
||||
|
||||
## 获取服务端authKey
|
||||
|
||||
如果想获取authKey,服务端提供获取authKey的接口
|
||||
|
||||
```
|
||||
POST /auth/getauthkey
|
||||
```
|
||||
将返回加密后的authKey,采用aes cbc加密,请使用与服务端配置文件中cryptKey相同的密钥进行解密
|
||||
|
||||
**注意:** nps配置文件中`auth_crypt_key`需为16位
|
||||
- 解密密钥长度128
|
||||
- 偏移量与密钥相同
|
||||
- 补码方式pkcs5padding
|
||||
- 解密串编码方式 十六进制
|
||||
|
||||
## 详细文档
|
||||
- **此文档近期可能更新较慢,建议自行抓包**
|
||||
|
||||
为方便第三方扩展,在web模式下可利用webAPI进行相关操作,详情见
|
||||
[webAPI文档](https://github.com/cnlh/nps/wiki/webAPI%E6%96%87%E6%A1%A3)
|
||||
@@ -0,0 +1,6 @@
|
||||
# 贡献
|
||||
|
||||
- 如果遇到bug可以直接提交至dev分支
|
||||
- 使用遇到问题可以通过issues反馈
|
||||
- 项目处于开发阶段,还有很多待完善的地方,如果可以贡献代码,请提交 PR 至 dev 分支
|
||||
- 如果有新的功能特性反馈,可以通过issues或者qq群反馈
|
||||
@@ -0,0 +1,29 @@
|
||||
# 说明
|
||||
## 获取用户真实ip
|
||||
|
||||
在域名代理模式中,可以通过request请求 header 中的 X-Forwarded-For 和 X-Real-IP 来获取用户真实 IP。
|
||||
|
||||
**本代理前会在每一个http(s)请求中添加了这两个 header。**
|
||||
|
||||
## 热更新支持
|
||||
对于绝大多数配置,在web管理中的修改将实时使用,无需重启客户端或者服务端
|
||||
|
||||
## 客户端地址显示
|
||||
在web管理中将显示客户端的连接地址
|
||||
|
||||
## 流量统计
|
||||
可统计显示每个代理使用的流量,由于压缩和加密等原因,会和实际环境中的略有差异
|
||||
|
||||
## 当前客户端带宽
|
||||
可统计每个客户端当前的带宽,可能和实际有一定差异,仅供参考。
|
||||
|
||||
## 客户端与服务端版本对比
|
||||
为了程序正常运行,客户端与服务端的核心版本必须一致,否则将导致客户端无法成功连接致服务端。
|
||||
|
||||
## Linux系统限制
|
||||
默认情况下linux对连接数量有限制,对于性能好的机器完全可以调整内核参数以处理更多的连接。
|
||||
`tcp_max_syn_backlog` `somaxconn`
|
||||
酌情调整参数,增强网络性能
|
||||
|
||||
## web管理保护
|
||||
当一个ip连续登陆失败次数超过10次,将在一分钟内禁止该ip再次尝试。
|
||||
@@ -0,0 +1,3 @@
|
||||
# 交流群
|
||||
|
||||

|
||||
@@ -0,0 +1,7 @@
|
||||
# 捐助
|
||||
如果您觉得nps对你有帮助,欢迎给予我们一定捐助,也是帮助nps更好的发展。
|
||||
|
||||
## 支付宝
|
||||

|
||||
## 微信
|
||||

|
||||
@@ -0,0 +1,121 @@
|
||||
# 使用示例
|
||||
## 统一准备工作(必做)
|
||||
- 开启服务端,假设公网服务器ip为1.1.1.1,配置文件中`bridge_port`为8284,配置文件中`web_port`为8080
|
||||
- 访问1.1.1.1:8080
|
||||
- 在客户端管理中创建一个客户端,记录下验证密钥
|
||||
- 内网客户端运行(windows使用cmd运行加.exe)
|
||||
|
||||
```shell
|
||||
./npc -server=1.1.1.1:8284 -vkey=客户端的密钥
|
||||
```
|
||||
**注意:运行服务端后,请确保能从客户端设备上正常访问配置文件中所配置的`bridge_port`端口,telnet,netcat这类的来检查**
|
||||
|
||||
## 域名解析
|
||||
|
||||
**适用范围:** 小程序开发、微信公众号开发、产品演示
|
||||
|
||||
**假设场景:**
|
||||
- 有一个域名proxy.com,有一台公网机器ip为1.1.1.1
|
||||
- 两个内网开发站点127.0.0.1:81,127.0.0.1:82
|
||||
- 想通过(http|https://)a.proxy.com访问127.0.0.1:81,通过(http|https://)b.proxy.com访问127.0.0.1:82
|
||||
|
||||
**使用步骤**
|
||||
- 将*.proxy.com解析到公网服务器1.1.1.1
|
||||
- 点击刚才创建的客户端的域名管理,添加两条规则规则:1、域名:`a.proxy.com`,内网目标:`127.0.0.1:81`,2、域名:`b.proxy.com`,内网目标:`127.0.0.1:82`
|
||||
|
||||
现在访问(http|https://)`a.proxy.com`,`b.proxy.com`即可成功
|
||||
|
||||
**https:** 如需使用https请进行相关配置,详见 [使用https](/nps_extend?id=使用https)
|
||||
|
||||
## tcp隧道
|
||||
|
||||
|
||||
**适用范围:** ssh、远程桌面等tcp连接场景
|
||||
|
||||
**假设场景:**
|
||||
想通过访问公网服务器1.1.1.1的8001端口,连接内网机器10.1.50.101的22端口,实现ssh连接
|
||||
|
||||
**使用步骤**
|
||||
- 在刚才创建的客户端隧道管理中添加一条tcp隧道,填写监听的端口(8001)、内网目标ip和目标端口(10.1.50.101:22),保存。
|
||||
- 访问公网服务器ip(1.1.1.1),填写的监听端口(8001),相当于访问内网ip(10.1.50.101):目标端口(22),例如:`ssh -p 8001 root@1.1.1.1`
|
||||
|
||||
## udp隧道
|
||||
|
||||
**适用范围:** 内网dns解析等udp连接场景
|
||||
|
||||
**假设场景:**
|
||||
内网有一台dns(10.1.50.102:53),在非内网环境下想使用该dns,公网服务器为1.1.1.1
|
||||
|
||||
**使用步骤**
|
||||
- 在刚才创建的客户端的隧道管理中添加一条udp隧道,填写监听的端口(53)、内网目标ip和目标端口(10.1.50.102:53),保存。
|
||||
- 修改需要使用的dns地址为1.1.1.1,则相当于使用10.1.50.102作为dns服务器
|
||||
|
||||
## socks5代理
|
||||
|
||||
|
||||
**适用范围:** 在外网环境下如同使用vpn一样访问内网设备或者资源
|
||||
|
||||
**假设场景:**
|
||||
想将公网服务器1.1.1.1的8003端口作为socks5代理,达到访问内网任意设备或者资源的效果
|
||||
|
||||
**使用步骤**
|
||||
- 在刚才创建的客户端隧道管理中添加一条socks5代理,填写监听的端口(8003),保存。
|
||||
- 在外网环境的本机配置socks5代理(例如使用proxifier进行全局代理),ip为公网服务器ip(1.1.1.1),端口为填写的监听端口(8003),即可畅享内网了
|
||||
|
||||
**注意**
|
||||
经过socks5代理,当收到socks5数据包时socket已经是accept状态。表现是扫描端口全open,建立连接后短时间关闭。若想同内网表现一致,建议远程连接一台设备。
|
||||
|
||||
## http正向代理
|
||||
|
||||
**适用范围:** 在外网环境下使用http正向代理访问内网站点
|
||||
|
||||
**假设场景:**
|
||||
想将公网服务器1.1.1.1的8004端口作为http代理,访问内网网站
|
||||
|
||||
**使用步骤**
|
||||
|
||||
- 在刚才创建的客户端隧道管理中添加一条http代理,填写监听的端口(8004),保存。
|
||||
- 在外网环境的本机配置http代理,ip为公网服务器ip(1.1.1.1),端口为填写的监听端口(8004),即可访问了
|
||||
|
||||
## 私密代理
|
||||
|
||||
**适用范围:** 无需占用多余的端口、安全性要求较高可以防止其他人连接的tcp服务,例如ssh。
|
||||
|
||||
**假设场景:**
|
||||
无需新增多的端口实现访问内网服务器10.1.50.2的22端口
|
||||
|
||||
**使用步骤**
|
||||
- 在刚才创建的客户端中添加一条私密代理,并设置唯一密钥secrettest和内网目标10.1.50.2:22
|
||||
- 在需要连接ssh的机器上以执行命令
|
||||
|
||||
```
|
||||
./npc -server=1.1.1.1:8284 -vkey=vkey -type=tcp -password=secrettest -local_type=secret
|
||||
```
|
||||
如需指定本地端口可加参数`-local_port=xx`,默认为2000
|
||||
|
||||
**注意:** password为web管理上添加的唯一密钥,具体命令可查看web管理上的命令提示
|
||||
|
||||
假设10.1.50.2用户名为root,现在执行`ssh -p 2000 root@1.1.1.1`即可访问ssh
|
||||
|
||||
|
||||
## p2p服务
|
||||
|
||||
**适用范围:** 大流量传输场景,流量不经过公网服务器,但是由于p2p穿透和nat类型关系较大,不保证100%成功,支持大部分nat类型。[nat类型检测](/npc_extend?id=nat类型检测)
|
||||
|
||||
**假设场景:**
|
||||
|
||||
想通过访问使用端机器(访问端,也就是本机)的2000端口---->访问到内网机器 10.2.50.2的22端口
|
||||
|
||||
**使用步骤**
|
||||
- 在`nps.conf`中设置`p2p_ip`(nps服务器ip)和`p2p_port`(nps服务器udp端口)
|
||||
- 在刚才刚才创建的客户端中添加一条p2p代理,并设置唯一密钥p2pssh
|
||||
- 在使用端机器(本机)执行命令
|
||||
|
||||
```
|
||||
./npc -server=1.1.1.1:8284 -vkey=123 -password=p2pssh -target=10.2.50.2:22
|
||||
```
|
||||
如需指定本地端口可加参数`-local_port=xx`,默认为2000
|
||||
|
||||
**注意:** password为web管理上添加的唯一密钥,具体命令可查看web管理上的命令提示
|
||||
|
||||
假设内网机器为10.2.50.2的ssh用户名为root,现在在本机上执行`ssh -p 2000 root@127.0.0.1`即可访问机器2的ssh,如果是网站在浏览器访问127.0.0.1:2000端口即可。
|
||||
@@ -0,0 +1,247 @@
|
||||
# 扩展功能
|
||||
## 缓存支持
|
||||
对于web站点来说,一些静态文件往往消耗更大的流量,且在内网穿透中,静态文件还需到客户端获取一次,这将导致更大的流量消耗。nps在域名解析代理中支持对静态文件进行缓存。
|
||||
|
||||
即假设一个站点有a.css,nps将只需从npc客户端读取一次该文件,然后把该文件的内容放在内存中,下一次将不再对npc客户端进行请求而直接返回内存中的对应内容。该功能默认是关闭的,如需开启请在`nps.conf`中设置`http_cache=true`,并设置`http_cache_length`(缓存文件的个数,消耗内存,不宜过大,0表示不限制个数)
|
||||
|
||||
## 数据压缩支持
|
||||
|
||||
由于是内网穿透,内网客户端与服务端之间的隧道存在大量的数据交换,为节省流量,加快传输速度,由此本程序支持SNNAPY形式的压缩。
|
||||
|
||||
|
||||
- 所有模式均支持数据压缩
|
||||
- 在web管理或客户端配置文件中设置
|
||||
|
||||
|
||||
## 加密传输
|
||||
|
||||
如果公司内网防火墙对外网访问进行了流量识别与屏蔽,例如禁止了ssh协议等,通过设置 配置文件,将服务端与客户端之间的通信内容加密传输,将会有效防止流量被拦截。
|
||||
- nps使用tls加密,所以一定要保留conf目录下的密钥文件,同时也可以自行生成
|
||||
- 在web管理或客户端配置文件中设置
|
||||
|
||||
|
||||
|
||||
## 站点保护
|
||||
域名代理模式所有客户端共用一个http服务端口,在知道域名后任何人都可访问,一些开发或者测试环境需要保密,所以可以设置用户名和密码,nps将通过 Http Basic Auth 来保护,访问时需要输入正确的用户名和密码。
|
||||
|
||||
|
||||
- 在web管理或客户端配置文件中设置
|
||||
|
||||
## host修改
|
||||
|
||||
由于内网站点需要的host可能与公网域名不一致,域名代理支持host修改功能,即修改request的header中的host字段。
|
||||
|
||||
**使用方法:在web管理中设置**
|
||||
|
||||
## 自定义header
|
||||
|
||||
支持对header进行新增或者修改,以配合服务的需要
|
||||
|
||||
## 404页面配置
|
||||
支持域名解析模式的自定义404页面,修改/web/static/page/error.html中内容即可,暂不支持静态文件等内容
|
||||
|
||||
## 流量限制
|
||||
|
||||
支持客户端级流量限制,当该客户端入口流量与出口流量达到设定的总量后会拒绝服务
|
||||
,域名代理会返回404页面,其他代理会拒绝连接,使用该功能需要在`nps.conf`中设置`allow_flow_limit`,默认是关闭的。
|
||||
|
||||
## 带宽限制
|
||||
|
||||
支持客户端级带宽限制,带宽计算方式为入口和出口总和,权重均衡,使用该功能需要在`nps.conf`中设置`allow_rate_limit`,默认是关闭的。
|
||||
|
||||
## 负载均衡
|
||||
本代理支持域名解析模式和tcp代理的负载均衡,在web域名添加或者编辑中内网目标分行填写多个目标即可实现轮训级别的负载均衡
|
||||
|
||||
## 端口白名单
|
||||
为了防止服务端上的端口被滥用,可在nps.conf中配置allow_ports限制可开启的端口,忽略或者不填表示端口不受限制,格式:
|
||||
|
||||
```ini
|
||||
allow_ports=9001-9009,10001,11000-12000
|
||||
```
|
||||
|
||||
## 端口范围映射
|
||||
当客户端以配置文件的方式启动时,可以将本地的端口进行范围映射,仅支持tcp和udp模式,例如:
|
||||
|
||||
```ini
|
||||
[tcp]
|
||||
mode=tcp
|
||||
server_port=9001-9009,10001,11000-12000
|
||||
target_port=8001-8009,10002,13000-14000
|
||||
```
|
||||
|
||||
逗号分隔,可单个或者范围,注意上下端口的对应关系,无法一一对应将不能成功
|
||||
## 端口范围映射到其他机器
|
||||
```ini
|
||||
[tcp]
|
||||
mode=tcp
|
||||
server_port=9001-9009,10001,11000-12000
|
||||
target_port=8001-8009,10002,13000-14000
|
||||
target_ip=10.1.50.2
|
||||
```
|
||||
填写target_ip后则表示映射的该地址机器的端口,忽略则便是映射本地127.0.0.1,仅范围映射时有效
|
||||
## 守护进程
|
||||
本代理支持守护进程,使用示例如下,服务端客户端所有模式通用,支持linux,darwin,windows。
|
||||
```
|
||||
./(nps|npc) start|stop|restart|status 若有其他参数可加其他参数
|
||||
```
|
||||
```
|
||||
(nps|npc).exe start|stop|restart|status 若有其他参数可加其他参数
|
||||
```
|
||||
## KCP协议支持
|
||||
|
||||
KCP 是一个快速可靠协议,能以比 TCP浪费10%-20%的带宽的代价,换取平均延迟降低 30%-40%,在弱网环境下对性能能有一定的提升。可在nps.conf中修改`bridge_type`为kcp
|
||||
,设置后本代理将开启udp端口(`bridge_port`)
|
||||
|
||||
注意:当服务端为kcp时,客户端连接时也需要使用相同配置,无配置文件模式加上参数type=kcp,配置文件模式在配置文件中设置tp=kcp
|
||||
|
||||
## 域名泛解析
|
||||
支持域名泛解析,例如将host设置为*.proxy.com,a.proxy.com、b.proxy.com等都将解析到同一目标,在web管理中或客户端配置文件中将host设置为此格式即可。
|
||||
|
||||
## URL路由
|
||||
本代理支持根据URL将同一域名转发到不同的内网服务器,可在web中或客户端配置文件中设置,此参数也可忽略,例如在客户端配置文件中
|
||||
|
||||
```ini
|
||||
[web1]
|
||||
host=a.proxy.com
|
||||
target_addr=127.0.0.1:7001
|
||||
location=/test
|
||||
[web2]
|
||||
host=a.proxy.com
|
||||
target_addr=127.0.0.1:7002
|
||||
location=/static
|
||||
```
|
||||
对于`a.proxy.com/test`将转发到`web1`,对于`a.proxy.com/static`将转发到`web2`
|
||||
|
||||
## 限制ip访问
|
||||
如果将一些危险性高的端口例如ssh端口暴露在公网上,可能会带来一些风险,本代理支持限制ip访问。
|
||||
|
||||
**使用方法:** 在配置文件nps.conf中设置`ip_limit`=true,设置后仅通过注册的ip方可访问。
|
||||
|
||||
**ip注册**:
|
||||
|
||||
**方式一:**
|
||||
在需要访问的机器上,运行客户端
|
||||
|
||||
```
|
||||
./npc register -server=ip:port -vkey=公钥或客户端密钥 time=2
|
||||
```
|
||||
|
||||
time为有效小时数,例如time=2,在当前时间后的两小时内,本机公网ip都可以访问nps代理.
|
||||
|
||||
**方式二:**
|
||||
此外nps的web登陆也可提供验证的功能,成功登陆nps web admin后将自动为登陆的ip注册两小时的允许访问权限。
|
||||
|
||||
|
||||
**注意:** 本机公网ip并不是一成不变的,请自行注意有效期的设置,同时同一网络下,多人也可能是在公用同一个公网ip。
|
||||
## 客户端最大连接数
|
||||
为防止恶意大量长连接,影响服务端程序的稳定性,可以在web或客户端配置文件中为每个客户端设置最大连接数。该功能针对`socks5`、`http正向代理`、`域名代理`、`tcp代理`、`udp代理`、`私密代理`生效,使用该功能需要在`nps.conf`中设置`allow_connection_num_limit=true`,默认是关闭的。
|
||||
|
||||
## 客户端最大隧道数限制
|
||||
nps支持对客户端的隧道数量进行限制,该功能默认是关闭的,如需开启,请在`nps.conf`中设置`allow_tunnel_num_limit=true`。
|
||||
## 端口复用
|
||||
在一些严格的网络环境中,对端口的个数等限制较大,nps支持强大端口复用功能。将`bridge_port`、 `http_proxy_port`、 `https_proxy_port` 、`web_port`都设置为同一端口,也能正常使用。
|
||||
|
||||
- 使用时将需要复用的端口设置为与`bridge_port`一致即可,将自动识别。
|
||||
- 如需将web管理的端口也复用,需要配置`web_host`也就是一个二级域名以便区分
|
||||
|
||||
## 多路复用
|
||||
|
||||
nps主要通信默认基于多路复用,无需开启。
|
||||
|
||||
多路复用基于TCP滑动窗口原理设计,动态计算延迟以及带宽来算出应该往网络管道中打入的流量。
|
||||
由于主要通信大多采用TCP协议,并无法探测其实时丢包情况,对于产生丢包重传的情况,采用较大的宽容度,
|
||||
5分钟的等待时间,超时将会关闭当前隧道连接并重新建立,这将会抛弃当前所有的连接。
|
||||
在Linux上,可以通过调节内核参数来适应不同应用场景。
|
||||
|
||||
对于需求大带宽又有一定的丢包的场景,可以保持默认参数不变,尽可能少抛弃连接
|
||||
高并发下可根据[Linux系统限制](## Linux系统限制) 调整
|
||||
|
||||
对于延迟敏感而又有一定丢包的场景,可以适当调整TCP重传次数
|
||||
`tcp_syn_retries`, `tcp_retries1`, `tcp_retries2`
|
||||
高并发同上
|
||||
nps会在系统主动关闭连接的时候拿到报错,进而重新建立隧道连接
|
||||
|
||||
## 环境变量渲染
|
||||
npc支持环境变量渲染以适应在某些特殊场景下的要求。
|
||||
|
||||
**在无配置文件启动模式下:**
|
||||
设置环境变量
|
||||
```
|
||||
export NPC_SERVER_ADDR=1.1.1.1:8284
|
||||
export NPC_SERVER_VKEY=xxxxx
|
||||
```
|
||||
直接执行./npc即可运行
|
||||
|
||||
**在配置文件启动模式下:**
|
||||
```ini
|
||||
[common]
|
||||
server_addr={{.NPC_SERVER_ADDR}}
|
||||
conn_type=tcp
|
||||
vkey={{.NPC_SERVER_VKEY}}
|
||||
auto_reconnection=true
|
||||
[web]
|
||||
host={{.NPC_WEB_HOST}}
|
||||
target_addr={{.NPC_WEB_TARGET}}
|
||||
```
|
||||
在配置文件中填入相应的环境变量名称,npc将自动进行渲染配置文件替换环境变量
|
||||
|
||||
## 健康检查
|
||||
|
||||
当客户端以配置文件模式启动时,支持多节点的健康检查。配置示例如下
|
||||
|
||||
```ini
|
||||
[health_check_test1]
|
||||
health_check_timeout=1
|
||||
health_check_max_failed=3
|
||||
health_check_interval=1
|
||||
health_http_url=/
|
||||
health_check_type=http
|
||||
health_check_target=127.0.0.1:8083,127.0.0.1:8082
|
||||
|
||||
[health_check_test2]
|
||||
health_check_timeout=1
|
||||
health_check_max_failed=3
|
||||
health_check_interval=1
|
||||
health_check_type=tcp
|
||||
health_check_target=127.0.0.1:8083,127.0.0.1:8082
|
||||
```
|
||||
**health关键词必须在开头存在**
|
||||
|
||||
第一种是http模式,也就是以get的方式请求目标+url,返回状态码为200表示成功
|
||||
|
||||
第一种是tcp模式,也就是以tcp的方式与目标建立连接,能成功建立连接表示成功
|
||||
|
||||
如果失败次数超过`health_check_max_failed`,nps则会移除该npc下的所有该目标,如果失败后目标重新上线,nps将自动将目标重新加入。
|
||||
|
||||
项 | 含义
|
||||
---|---
|
||||
health_check_timeout | 健康检查超时时间
|
||||
health_check_max_failed | 健康检查允许失败次数
|
||||
health_check_interval | 健康检查间隔
|
||||
health_check_type | 健康检查类型
|
||||
health_check_target | 健康检查目标,多个以逗号(,)分隔
|
||||
health_check_type | 健康检查类型
|
||||
health_http_url | 健康检查url,仅http模式适用
|
||||
|
||||
## 日志输出
|
||||
|
||||
日志输出级别
|
||||
|
||||
**对于npc:**
|
||||
```
|
||||
-log_level=0~7 -log_path=npc.log
|
||||
```
|
||||
```
|
||||
LevelEmergency->0 LevelAlert->1
|
||||
|
||||
LevelCritical->2 LevelError->3
|
||||
|
||||
LevelWarning->4 LevelNotice->5
|
||||
|
||||
LevelInformational->6 LevelDebug->7
|
||||
```
|
||||
默认为全输出,级别为0到7
|
||||
|
||||
**对于nps:**
|
||||
|
||||
在`nps.conf`中设置相关配置即可
|
||||
@@ -0,0 +1,42 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Document</title>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
|
||||
<meta name="description" content="Description">
|
||||
<meta name="viewport"
|
||||
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
||||
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script src="//unpkg.com/docsify-edit-on-github/index.js"></script>
|
||||
|
||||
<script>
|
||||
|
||||
window.$docsify = {
|
||||
name: '',
|
||||
repo: '',
|
||||
loadSidebar: true,
|
||||
coverpage: true,
|
||||
subMaxLevel: 2,
|
||||
maxLevel: 4,
|
||||
search: {
|
||||
noData: "没有结果",
|
||||
paths: 'auto',
|
||||
placeholder: "搜索",
|
||||
hideOtherSidebarContent: true, // whether or not to hide other sidebar content
|
||||
},
|
||||
plugins: [
|
||||
EditOnGithubPlugin.create("https://github.com/cnlh/nps/tree/master/docs/", "", "在github上编辑"),
|
||||
]
|
||||
|
||||
}
|
||||
</script>
|
||||
<script src="//unpkg.com/docsify/lib/docsify.min.js"></script>
|
||||
<script src="//unpkg.com/docsify/lib/plugins/search.min.js"></script>
|
||||
<script src="//unpkg.com/docsify-copy-code"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,19 @@
|
||||
# 安装
|
||||
## 安装包安装
|
||||
[releases](https://github.com/cnlh/nps/releases)
|
||||
|
||||
下载对应的系统版本即可,服务端和客户端是单独的
|
||||
|
||||
## 源码安装
|
||||
- 安装源码
|
||||
```go get -u github.com/cnlh/nps...
|
||||
```
|
||||
- 编译
|
||||
|
||||
服务端```go build cmd/nps/nps.go```
|
||||
|
||||
客户端```go build cmd/npc/npc.go```
|
||||
|
||||
## docker安装
|
||||
> [server](https://hub.docker.com/r/ffdfgdfg/nps)
|
||||
> [client](https://hub.docker.com/r/ffdfgdfg/npc)
|
||||
@@ -0,0 +1,4 @@
|
||||

|
||||
# 介绍
|
||||
|
||||
可在网页上配置和管理各个tcp、udp隧道、内网站点代理,http、https解析等,功能强大,操作方便。
|
||||
|
After Width: | Height: | Size: 1.4 KiB |
@@ -0,0 +1 @@
|
||||
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1576165662444" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="50156" xmlns:xlink="http://www.w3.org/1999/xlink" width="48" height="48"><defs><style type="text/css"></style></defs><path d="M414.764 498.138c-4.094-8.188-13-12.844-22.094-11.562L181.58 516.73a21.496 21.496 0 0 0-12.062 6.032L48.926 643.4c-8.344 8.344-8.344 21.842 0 30.154l90.436 90.466c8.344 8.312 21.844 8.312 30.156 0l241.246-241.26a21.344 21.344 0 0 0 4-24.622zM507.262 842.428l30.156-211.09c1.312-9.094-3.376-17.968-11.562-22.094a21.34 21.34 0 0 0-24.624 4L259.984 854.49c-8.312 8.312-8.312 21.812 0 30.156l90.468 90.464c8.344 8.312 21.842 8.312 30.156 0l120.624-120.622a21.214 21.214 0 0 0 6.03-12.06z" fill="#DA4453" p-id="50157"></path><path d="M732.444 186.238a21.254 21.254 0 0 0-14.75-15.156l-93.778-26.876a21.288 21.288 0 0 0-20.938 5.438l-87.188 87.154a21.266 21.266 0 0 0-5.656 19.968 21.088 21.088 0 0 0 5.656 10.188 21.33 21.33 0 0 0 8.344 5.156l90.468 30.156a21.27 21.27 0 0 0 21.812-5.156l90.464-90.468a21.252 21.252 0 0 0 5.566-20.404zM879.786 400.078l-26.842-93.75a21.348 21.348 0 0 0-15.156-14.78 21.388 21.388 0 0 0-20.438 5.562l-90.468 90.466a21.348 21.348 0 0 0-5.156 21.812l30.156 90.466a21.33 21.33 0 0 0 5.156 8.344 21.4 21.4 0 0 0 10.188 5.688 21.356 21.356 0 0 0 19.968-5.688l87.186-87.156a21.39 21.39 0 0 0 5.406-20.964z" fill="#434A54" p-id="50158"></path><path d="M997.284 138.206c30.312-61.56 34.876-103.592 13.5-124.966-21.376-21.374-63.438-16.842-124.998 13.5-47.06 23.188-97.592 58.28-128.748 89.436L6.27 866.99c-4 4-6.25 9.406-6.25 15.062s2.25 11.094 6.25 15.094l120.624 120.59c8.312 8.344 21.812 8.344 30.156 0l750.768-750.782c31.154-31.156 66.278-81.686 89.466-128.748z" fill="#E6E9ED" p-id="50159"></path><path d="M907.816 266.954c31.154-31.156 66.28-81.686 89.466-128.748 30.312-61.56 34.876-103.592 13.5-124.966-21.376-21.374-63.438-16.842-124.998 13.5-47.06 23.188-97.592 58.28-128.748 89.436l150.78 150.778z" fill="#ED5564" p-id="50160"></path><path d="M157.048 1017.736l15.062-15.062-150.778-150.778-15.062 15.094c-4 4-6.25 9.406-6.25 15.062s2.25 11.094 6.25 15.094l120.624 120.59c8.312 8.344 21.81 8.344 30.154 0z" fill="#FFCE54" p-id="50161"></path><path d="M81.662 972.546l-30.156-30.156 30.156-30.154L111.814 942.39z" fill="#F6BB42" p-id="50162"></path><path d="M199.672 824.334c-8.312-8.344-8.312-21.844 0-30.156l241.246-241.246c8.344-8.312 21.844-8.312 30.156 0 8.344 8.344 8.344 21.844 0 30.156L229.828 824.334c-8.312 8.312-21.812 8.312-30.156 0z" fill="#ED5564" p-id="50163"></path><path d="M787.194 236.798c-8.312-8.312-21.812-8.312-30.156 0l-120.622 120.624c-8.312 8.344-8.312 21.844 0 30.156 8.342 8.344 21.842 8.344 30.154 0l120.624-120.624c8.344-8.312 8.344-21.812 0-30.156z" fill="#656D78" p-id="50164"></path><path d="M997.284 138.206c30.312-61.56 34.876-103.592 13.5-124.966a40.728 40.728 0 0 0-8.562-6.562c13.376 23.156 6.688 62.25-20.032 116.466-23.188 47.062-58.312 97.592-89.436 128.748L141.954 1002.674c-8.312 8.312-21.812 8.312-30.156 0l-60.312-60.31-45.216-45.248v0.032l120.624 120.59c8.312 8.344 21.812 8.344 30.156 0l750.768-750.782c31.154-31.158 66.278-81.688 89.466-128.75z" fill="#FFFFFF" opacity=".2" p-id="50165"></path></svg>
|
||||
|
After Width: | Height: | Size: 3.3 KiB |
@@ -0,0 +1,36 @@
|
||||
# 增强功能
|
||||
## nat类型检测
|
||||
```
|
||||
./npc nat
|
||||
```
|
||||
如果p2p双方都是Symmetric Nat,肯定不能成功,其他组合都有较大成功率。
|
||||
## 状态检查
|
||||
```
|
||||
./npc status -config=npc配置文件路径
|
||||
```
|
||||
## 重载配置文件
|
||||
```
|
||||
./npc restart -config=npc配置文件路径
|
||||
```
|
||||
|
||||
## 通过代理连接nps
|
||||
有时候运行npc的内网机器无法直接访问外网,此时可以可以通过socks5代理连接nps
|
||||
|
||||
对于配置文件方式启动,设置
|
||||
```ini
|
||||
[common]
|
||||
proxy_url=socks5://111:222@127.0.0.1:8024
|
||||
```
|
||||
对于无配置文件模式,加上参数
|
||||
|
||||
```
|
||||
-proxy=socks5://111:222@127.0.0.1:8024
|
||||
```
|
||||
支持socks5和http两种模式
|
||||
|
||||
即socks5://username:password@ip:port
|
||||
|
||||
或http://username:password@ip:port
|
||||
|
||||
## 群晖支持
|
||||
可在releases中下载spk群晖套件,例如`npc_x64-6.1_0.19.0-1.spk`
|
||||
@@ -0,0 +1,23 @@
|
||||
# npc sdk文档
|
||||
|
||||
```
|
||||
命令行模式启动客户端
|
||||
p0->连接地址
|
||||
p1->vkey
|
||||
p2->连接类型(tcp or udp)
|
||||
p3->连接代理
|
||||
|
||||
extern GoInt StartClientByVerifyKey(char* p0, char* p1, char* p2, char* p3);
|
||||
|
||||
查看当前启动的客户端状态,在线为1,离线为0
|
||||
extern GoInt GetClientStatus();
|
||||
|
||||
关闭客户端
|
||||
extern void CloseClient();
|
||||
|
||||
获取当前客户端版本
|
||||
extern char* Version();
|
||||
|
||||
获取日志,实时更新
|
||||
extern char* Logs();
|
||||
```
|
||||
@@ -0,0 +1,107 @@
|
||||
# 增强功能
|
||||
## 使用https
|
||||
|
||||
**方式一:** 类似于nginx实现https的处理
|
||||
|
||||
在配置文件中将https_proxy_port设置为443或者其他你想配置的端口,和在web中对应域名编辑中设置对应的证书路径,将`https_just_proxy`设置为false,然后就和http代理一样了
|
||||
|
||||
**此外:** 可以在`nps.conf`中设置一个默认的https配置,当遇到未在web中设置https证书的域名解析时,将自动使用默认证书,另还有一种情况就是对于某些请求的clienthello不携带sni扩展信息,nps也将自动使用默认证书
|
||||
|
||||
|
||||
**方式二:** 在内网对应服务器上设置https
|
||||
|
||||
在`nps.conf`中将`https_just_proxy`设置为true,并且打开`https_proxy_port`端口,然后nps将直接转发https请求到内网服务器上,由内网服务器进行https处理
|
||||
|
||||
## 与nginx配合
|
||||
|
||||
有时候我们还需要在云服务器上运行nginx来保证静态文件缓存等,本代理可和nginx配合使用,在配置文件中将httpProxyPort设置为非80端口,并在nginx中配置代理,例如httpProxyPort为8024时
|
||||
```
|
||||
server {
|
||||
listen 80;
|
||||
server_name *.proxy.com;
|
||||
location / {
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_pass http://127.0.0.1:8024;
|
||||
}
|
||||
}
|
||||
```
|
||||
如需使用https也可在nginx监听443端口并配置ssl,并将本代理的httpsProxyPort设置为空关闭https即可,例如httpProxyPort为8024时
|
||||
|
||||
```
|
||||
server {
|
||||
listen 443;
|
||||
server_name *.proxy.com;
|
||||
ssl on;
|
||||
ssl_certificate certificate.crt;
|
||||
ssl_certificate_key private.key;
|
||||
ssl_session_timeout 5m;
|
||||
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
|
||||
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
|
||||
ssl_prefer_server_ciphers on;
|
||||
location / {
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_pass http://127.0.0.1:8024;
|
||||
}
|
||||
}
|
||||
```
|
||||
## web管理使用https
|
||||
如果web管理需要使用https,可以在配置文件`nps.conf`中设置`web_open_ssl=true`,并配置`web_cert_file`和`web_key_file`
|
||||
## web使用Caddy代理
|
||||
|
||||
如果将web配置到Caddy代理,实现子路径访问nps,可以这样配置.
|
||||
|
||||
假设我们想通过 `http://caddy_ip:caddy_port/nps` 来访问后台, Caddyfile 这样配置:
|
||||
|
||||
```Caddyfile
|
||||
caddy_ip:caddy_port/nps {
|
||||
##server_ip 为 nps 服务器IP
|
||||
##web_port 为 nps 后台端口
|
||||
proxy / http://server_ip:web_port/nps {
|
||||
transparent
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
nps.conf 修改 `web_base_url` 为 `/nps` 即可
|
||||
```
|
||||
web_base_url=/nps
|
||||
```
|
||||
|
||||
|
||||
## 关闭代理
|
||||
|
||||
如需关闭http代理可在配置文件中将http_proxy_port设置为空,如需关闭https代理可在配置文件中将https_proxy_port设置为空。
|
||||
|
||||
## 流量数据持久化
|
||||
服务端支持将流量数据持久化,默认情况下是关闭的,如果有需求可以设置`nps.conf`中的`flow_store_interval`参数,单位为分钟
|
||||
|
||||
**注意:** nps不会持久化通过公钥连接的客户端
|
||||
## 系统信息显示
|
||||
nps服务端支持在web上显示和统计服务器的相关信息,但默认一些统计图表是关闭的,如需开启请在`nps.conf`中设置`system_info_display=true`
|
||||
|
||||
## 自定义客户端连接密钥
|
||||
web上可以自定义客户端连接的密钥,但是必须具有唯一性
|
||||
## 关闭公钥访问
|
||||
可以将`nps.conf`中的`public_vkey`设置为空或者删除
|
||||
|
||||
## 关闭web管理
|
||||
可以将`nps.conf`中的`web_port`设置为空或者删除
|
||||
|
||||
## 服务端多用户登陆
|
||||
如果将`nps.conf`中的`allow_user_login`设置为true,服务端web将支持多用户登陆,登陆用户名为user,默认密码为每个客户端的验证密钥,登陆后可以进入客户端编辑修改web登陆的用户名和密码,默认该功能是关闭的。
|
||||
|
||||
## 用户注册功能
|
||||
nps服务端支持用户注册功能,可将`nps.conf`中的`allow_user_register`设置为true,开启后登陆页将会有有注册功能,
|
||||
|
||||
## 监听指定ip
|
||||
|
||||
nps支持每个隧道监听不同的服务端端口,在`nps.conf`中设置`allow_multi_ip=true`后,可在web中控制,或者npc配置文件中(可忽略,默认为0.0.0.0)
|
||||
```ini
|
||||
server_ip=xxx
|
||||
```
|
||||
## 代理到服务端本地
|
||||
在使用nps监听80或者443端口时,默认是将所有的请求都会转发到内网上,但有时候我们的nps服务器的上一些服务也需要使用这两个端口,nps提供类似于`nginx` `proxy_pass` 的功能,支持将代理到服务器本地,该功能支持域名解析,tcp、udp隧道,默认关闭。
|
||||
|
||||
**即:** 假设在nps的vps服务器上有一个服务使用5000端口,这时候nps占用了80端口和443,我们想能使用一个域名通过http(s)访问到5000的服务。
|
||||
|
||||
**使用方式:** 在`nps.conf`中设置`allow_local_proxy=true`,然后在web上设置想转发的隧道或者域名然后选择转发到本地选项即可成功。
|
||||
@@ -0,0 +1,43 @@
|
||||
# 使用
|
||||
**提示:使用web模式时,服务端执行文件必须在项目根目录,否则无法正确加载配置文件**
|
||||
|
||||
## web管理
|
||||
|
||||
进入web界面,公网ip:web界面端口(默认8080),密码默认为123
|
||||
|
||||
进入web管理界面,有详细的说明
|
||||
|
||||
## 服务端配置文件重载
|
||||
对于linux、darwin
|
||||
```shell
|
||||
sudo nps reload
|
||||
```
|
||||
对于windows
|
||||
```shell
|
||||
nps.exe reload
|
||||
```
|
||||
**说明:** 仅支持部分配置重载,例如`allow_user_login` `auth_crypt_key` `auth_key` `web_username` `web_password` 等,未来将支持更多
|
||||
|
||||
|
||||
## 服务端停止或重启
|
||||
对于linux、darwin
|
||||
```shell
|
||||
sudo nps stop|restart
|
||||
```
|
||||
对于windows
|
||||
```shell
|
||||
nps.exe stop|restart
|
||||
```
|
||||
## 服务端更新
|
||||
请首先执行`sudo nps stop`或者`nps.exe stop`停止运行,然后
|
||||
|
||||
对于linux
|
||||
```shell
|
||||
sudo nps-update update
|
||||
```
|
||||
对于windows
|
||||
```shell
|
||||
nps-update.exe update
|
||||
```
|
||||
|
||||
更新完成后,执行执行`sudo nps start`或者`nps.exe start`重新运行即可完成升级
|
||||
@@ -0,0 +1,33 @@
|
||||
# 启动
|
||||
## 服务端
|
||||
下载完服务器压缩包后,解压,然后进入解压后的文件夹
|
||||
|
||||
- 执行安装命令
|
||||
|
||||
对于linux|darwin ```sudo ./nps install```
|
||||
|
||||
对于windows,管理员身份运行cmd,进入安装目录 ```nps.exe install```
|
||||
|
||||
- 启动
|
||||
|
||||
对于linux|darwin ```sudo nps start```
|
||||
|
||||
对于windows,管理员身份运行cmd,进入程序目录 ```nps.exe start```
|
||||
|
||||
停止和重启可用,stop和restart
|
||||
|
||||
**如果发现没有启动成功,可以使用`nps(.exe) debug`运行调试,或查看日志**(Windows日志文件位于当前运行目录下,linux和darwin位于/var/log/nps.log)
|
||||
- 访问服务端ip:web服务端口(默认为8080)
|
||||
- 使用用户名和密码登陆(默认admin/123,正式使用一定要更改)
|
||||
- 创建客户端
|
||||
|
||||
## 客户端
|
||||
- 下载客户端安装包并解压,进入到解压目录
|
||||
- 点击web管理中客户端前的+号,复制启动命令
|
||||
- 执行启动命令,linux直接执行即可,windows将./npc换成npc.exe用cmd执行
|
||||
|
||||
如果需要注册到系统服务可查看[注册到系统服务](/use?id=注册到系统服务)
|
||||
|
||||
## 配置
|
||||
- 客户端连接后,在web中配置对应穿透服务即可
|
||||
- 可以查看[使用示例](/example)
|
||||
@@ -0,0 +1,21 @@
|
||||
# 服务端配置文件
|
||||
- /conf/nps.conf
|
||||
|
||||
名称 | 含义
|
||||
---|---
|
||||
web_port | web管理端口
|
||||
web_password | web界面管理密码
|
||||
web_username | web界面管理账号
|
||||
web_base_url | web管理主路径,用于将web管理置于代理子路径后面
|
||||
bridge_port | 服务端客户端通信端口
|
||||
https_proxy_port | 域名代理https代理监听端口
|
||||
http_proxy_port | 域名代理http代理监听端口
|
||||
auth_key|web api密钥
|
||||
bridge_type|客户端与服务端连接方式kcp或tcp
|
||||
public_vkey|客户端以配置文件模式启动时的密钥,设置为空表示关闭客户端配置文件连接模式
|
||||
ip_limit|是否限制ip访问,true或false或忽略
|
||||
flow_store_interval|服务端流量数据持久化间隔,单位分钟,忽略表示不持久化
|
||||
log_level|日志输出级别
|
||||
auth_crypt_key | 获取服务端authKey时的aes加密密钥,16位
|
||||
p2p_ip| 服务端Ip,使用p2p模式必填
|
||||
p2p_port|p2p模式开启的udp端口
|
||||
@@ -0,0 +1,5 @@
|
||||
Thanks [jetbrains](https://www.jetbrains.com/?from=nps) for providing development tools for nps
|
||||
|
||||
<html>
|
||||
<img src="https://ftp.bmp.ovh/imgs/2019/12/6435398b0c7402b1.png" width="300" align=center />
|
||||
</html>
|
||||
@@ -0,0 +1,219 @@
|
||||
# 基本使用
|
||||
## 无配置文件模式
|
||||
此模式的各种配置在服务端web管理中完成,客户端除运行一条命令外无需任何其他设置
|
||||
```
|
||||
./npc -debug=true -server=ip:port -vkey=web界面中显示的密钥
|
||||
```
|
||||
## 注册到系统服务
|
||||
对于linux、darwin
|
||||
- 注册:`sudo ./npc install 其他参数(例如-server=xx -vkey=xx或者-config=xxx)`
|
||||
- 启动:`sudo ./npc start`
|
||||
- 停止:`sudo ./npc stop`
|
||||
- 如果需要更换命令内容需要先卸载`./npc -service=uninstall`,再重新注册
|
||||
|
||||
对于windows,使用管理员身份运行cmd
|
||||
|
||||
- 注册:`npc.exe install 其他参数(例如-server=xx -vkey=xx或者-config=xxx)`
|
||||
- 启动:`npc.exe start`
|
||||
- 停止:`npc.exe stop`
|
||||
- 如果需要更换命令内容需要先卸载`npc.exe -service=uninstall`,再重新注册
|
||||
|
||||
注册到服务后,日志文件windows位于当前目录下,linux和darwin位于/var/log/npc.log
|
||||
|
||||
## 客户端更新
|
||||
首先进入到对于的客户端二进制文件目录
|
||||
|
||||
请首先执行`sudo ./npc stop`或者`nps.exe stop`停止运行,然后
|
||||
|
||||
对于linux
|
||||
```shell
|
||||
sudo ./npc-update update
|
||||
```
|
||||
对于windows
|
||||
```shell
|
||||
npc-update.exe update
|
||||
```
|
||||
|
||||
更新完成后,执行执行`sudo nps start`或者`nps.exe start`重新运行即可完成升级
|
||||
|
||||
## 配置文件模式
|
||||
此模式使用nps的公钥或者客户端私钥验证,各种配置在客户端完成,同时服务端web也可以进行管理
|
||||
```
|
||||
./npc -config=npc配置文件路径
|
||||
```
|
||||
## 配置文件说明
|
||||
[示例配置文件](https://github.com/cnlh/nps/tree/master/conf/npc.conf)
|
||||
#### 全局配置
|
||||
```ini
|
||||
[common]
|
||||
server_addr=1.1.1.1:8284
|
||||
conn_type=tcp
|
||||
vkey=123
|
||||
username=111
|
||||
password=222
|
||||
compress=true
|
||||
crypt=true
|
||||
rate_limit=10000
|
||||
flow_limit=100
|
||||
remark=test
|
||||
max_conn=10
|
||||
```
|
||||
项 | 含义
|
||||
---|---
|
||||
server_addr | 服务端ip:port
|
||||
conn_type | 与服务端通信模式(tcp或kcp)
|
||||
vkey|服务端配置文件中的密钥(非web)
|
||||
username|socks5或http(s)密码保护用户名(可忽略)
|
||||
password|socks5或http(s)密码保护密码(可忽略)
|
||||
compress|是否压缩传输(true或false或忽略)
|
||||
crypt|是否加密传输(true或false或忽略)
|
||||
rate_limit|速度限制,可忽略
|
||||
flow_limit|流量限制,可忽略
|
||||
remark|客户端备注,可忽略
|
||||
max_conn|最大连接数,可忽略
|
||||
#### 域名代理
|
||||
|
||||
```ini
|
||||
[common]
|
||||
server_addr=1.1.1.1:8284
|
||||
vkey=123
|
||||
[web1]
|
||||
host=a.proxy.com
|
||||
target_addr=127.0.0.1:8080,127.0.0.1:8082
|
||||
host_change=www.proxy.com
|
||||
header_set_proxy=nps
|
||||
```
|
||||
项 | 含义
|
||||
---|---
|
||||
web1 | 备注
|
||||
host | 域名(http|https都可解析)
|
||||
target_addr|内网目标,负载均衡时多个目标,逗号隔开
|
||||
host_change|请求host修改
|
||||
header_xxx|请求header修改或添加,header_proxy表示添加header proxy:nps
|
||||
|
||||
#### tcp隧道模式
|
||||
|
||||
```ini
|
||||
[common]
|
||||
server_addr=1.1.1.1:8284
|
||||
vkey=123
|
||||
[tcp]
|
||||
mode=tcp
|
||||
target_addr=127.0.0.1:8080
|
||||
server_port=9001
|
||||
```
|
||||
项 | 含义
|
||||
---|---
|
||||
mode | tcp
|
||||
server_port | 在服务端的代理端口
|
||||
tartget_addr|内网目标
|
||||
|
||||
#### udp隧道模式
|
||||
|
||||
```ini
|
||||
[common]
|
||||
server_addr=1.1.1.1:8284
|
||||
vkey=123
|
||||
[udp]
|
||||
mode=udp
|
||||
target_addr=127.0.0.1:8080
|
||||
server_port=9002
|
||||
```
|
||||
项 | 含义
|
||||
---|---
|
||||
mode | udp
|
||||
server_port | 在服务端的代理端口
|
||||
target_addr|内网目标
|
||||
#### http代理模式
|
||||
|
||||
```ini
|
||||
[common]
|
||||
server_addr=1.1.1.1:8284
|
||||
vkey=123
|
||||
[http]
|
||||
mode=httpProxy
|
||||
server_port=9003
|
||||
```
|
||||
项 | 含义
|
||||
---|---
|
||||
mode | httpProxy
|
||||
server_port | 在服务端的代理端口
|
||||
#### socks5代理模式
|
||||
|
||||
```ini
|
||||
[common]
|
||||
server_addr=1.1.1.1:8284
|
||||
vkey=123
|
||||
[socks5]
|
||||
mode=socks5
|
||||
server_port=9004
|
||||
multi_account=multi_account.conf
|
||||
```
|
||||
项 | 含义
|
||||
---|---
|
||||
mode | socks5
|
||||
server_port | 在服务端的代理端口
|
||||
multi_account | socks5多账号配置文件(可选),配置后使用basic_username和basic_password无法通过认证
|
||||
#### 私密代理模式
|
||||
|
||||
```ini
|
||||
[common]
|
||||
server_addr=1.1.1.1:8284
|
||||
vkey=123
|
||||
[secret_ssh]
|
||||
mode=secret
|
||||
password=ssh2
|
||||
target_addr=10.1.50.2:22
|
||||
```
|
||||
项 | 含义
|
||||
---|---
|
||||
mode | secret
|
||||
password | 唯一密钥
|
||||
target_addr|内网目标
|
||||
|
||||
#### p2p代理模式
|
||||
|
||||
```ini
|
||||
[common]
|
||||
server_addr=1.1.1.1:8284
|
||||
vkey=123
|
||||
[p2p_ssh]
|
||||
mode=p2p
|
||||
password=ssh2
|
||||
target_addr=10.1.50.2:22
|
||||
```
|
||||
项 | 含义
|
||||
---|---
|
||||
mode | p2p
|
||||
password | 唯一密钥
|
||||
target_addr|内网目标
|
||||
|
||||
|
||||
#### 文件访问模式
|
||||
利用nps提供一个公网可访问的本地文件服务,此模式仅客户端使用配置文件模式方可启动
|
||||
|
||||
```ini
|
||||
[common]
|
||||
server_addr=1.1.1.1:8284
|
||||
vkey=123
|
||||
[file]
|
||||
mode=file
|
||||
server_port=9100
|
||||
local_path=/tmp/
|
||||
strip_pre=/web/
|
||||
````
|
||||
|
||||
项 | 含义
|
||||
---|---
|
||||
mode | file
|
||||
server_port | 服务端开启的端口
|
||||
local_path|本地文件目录
|
||||
strip_pre|前缀
|
||||
|
||||
对于`strip_pre`,访问公网`ip:9100/web/`相当于访问`/tmp/`目录
|
||||
|
||||
#### 断线重连
|
||||
```ini
|
||||
[common]
|
||||
auto_reconnection=true
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
module github.com/cnlh/nps
|
||||
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
fyne.io/fyne v1.2.0
|
||||
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
|
||||
github.com/astaxie/beego v1.12.0
|
||||
github.com/bradfitz/iter v0.0.0-20190303215204-33e6a9893b0c // indirect
|
||||
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c
|
||||
github.com/ccding/go-stun v0.0.0-20180726100737-be486d185f3d
|
||||
github.com/dsnet/compress v0.0.1 // indirect
|
||||
github.com/go-ole/go-ole v1.2.4 // indirect
|
||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db
|
||||
github.com/hooklift/assert v0.0.0-20170704181755-9d1defd6d214 // indirect
|
||||
github.com/kardianos/service v1.0.0
|
||||
github.com/klauspost/cpuid v1.2.1 // indirect
|
||||
github.com/klauspost/pgzip v1.2.1 // indirect
|
||||
github.com/klauspost/reedsolomon v1.9.2 // indirect
|
||||
github.com/panjf2000/ants/v2 v2.2.2
|
||||
github.com/pkg/errors v0.8.1
|
||||
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect
|
||||
github.com/shirou/gopsutil v2.19.11+incompatible
|
||||
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 // indirect
|
||||
github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b // indirect
|
||||
github.com/tjfoc/gmsm v1.0.1 // indirect
|
||||
github.com/xtaci/kcp-go v5.4.4+incompatible
|
||||
github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae // indirect
|
||||
golang.org/x/net v0.0.0-20181220203305-927f97764cc3
|
||||
golang.org/x/sys v0.0.0-20190804053845-51ab0e2deafa // indirect
|
||||
)
|
||||
|
||||
replace github.com/astaxie/beego => github.com/exfly/beego v1.12.0-export-init
|
||||
@@ -0,0 +1,132 @@
|
||||
fyne.io/fyne v1.2.0 h1:mdp7Cs7QmSJTeazYxEDa9wWeJNig7paBcjm0dooFtLE=
|
||||
fyne.io/fyne v1.2.0/go.mod h1:Ab+3DIB/FVteW0y4DXfmZv4N3JdnCBh2lHkINI02BOU=
|
||||
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
|
||||
github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9/go.mod h1:7uhhqiBaR4CpN0k9rMjOtjpcfGd6DG2m04zQxKnWQ0I=
|
||||
github.com/OwnLocal/goes v1.0.0/go.mod h1:8rIFjBGTue3lCU0wplczcUgt9Gxgrkkrw7etMIcn8TM=
|
||||
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/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
|
||||
github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd/go.mod h1:1b+Y/CofkYwXMUU0OhQqGvsY2Bvgr4j6jfT699wyZKQ=
|
||||
github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542/go.mod h1:kSeGC/p1AbBiEp5kat81+DSQrZenVBZXklMLaELspWU=
|
||||
github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737/go.mod h1:PmM6Mmwb0LSuEubjR8N7PtNe1KxZLtOUHtbeikc5h60=
|
||||
github.com/bradfitz/iter v0.0.0-20190303215204-33e6a9893b0c h1:FUUopH4brHNO2kJoNN3pV+OBEYmgraLT/KHZrMM69r0=
|
||||
github.com/bradfitz/iter v0.0.0-20190303215204-33e6a9893b0c/go.mod h1:PyRFw1Lt2wKX4ZVSQ2mk+PeDa1rxyObEDlApuIsUKuo=
|
||||
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c h1:aprLqMn7gSPT+vdDSl+/E6NLEuArwD/J7IWd8bJt5lQ=
|
||||
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c/go.mod h1:Ie6SubJv/NTO9Q0UBH0QCl3Ve50lu9hjbi5YJUw03TE=
|
||||
github.com/casbin/casbin v1.7.0/go.mod h1:c67qKN6Oum3UF5Q1+BByfFxkwKvhwW57ITjqwtzR1KE=
|
||||
github.com/ccding/go-stun v0.0.0-20180726100737-be486d185f3d h1:As4937T5NVbJ/DmZT9z33pyLEprMd6CUSfhbmMY57Io=
|
||||
github.com/ccding/go-stun v0.0.0-20180726100737-be486d185f3d/go.mod h1:3FK1bMar37f7jqVY7q/63k3OMX1c47pGCufzt3X0sYE=
|
||||
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58/go.mod h1:EOBUe0h4xcZ5GoxqC5SDxFQ8gwyZPKQoEzownBlhI80=
|
||||
github.com/couchbase/go-couchbase v0.0.0-20181122212707-3e9b6e1258bb/go.mod h1:TWI8EKQMs5u5jLKW/tsb9VwauIrMIxQG1r5fMsswK5U=
|
||||
github.com/couchbase/gomemcached v0.0.0-20181122193126-5125a94a666c/go.mod h1:srVSlQLB8iXBVXHgnqemxUXqN6FCvClgCMPCsjBDR7c=
|
||||
github.com/couchbase/goutils v0.0.0-20180530154633-e865a1461c8a/go.mod h1:BQwMFlJzDjFDG3DJUdU0KORxn88UlsOULuxLExMh3Hs=
|
||||
github.com/cupcake/rdb v0.0.0-20161107195141-43ba34106c76/go.mod h1:vYwsqCOLxGiisLwp9rITslkFNpZD5rz43tf41QFkTWY=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q=
|
||||
github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo=
|
||||
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
|
||||
github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
|
||||
github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk=
|
||||
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
|
||||
github.com/exfly/beego v1.12.0-export-init h1:VQNYKdXhAwZGUaFmQv8Aj921O3rQJZRIF8xeGrhsjrI=
|
||||
github.com/exfly/beego v1.12.0-export-init/go.mod h1:fysx+LZNZKnvh4GED/xND7jWtjCR6HzydR2Hh2Im57o=
|
||||
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw=
|
||||
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk=
|
||||
github.com/go-gl/glfw v0.0.0-20181213070059-819e8ce5125f h1:7MsFMbSn8Lcw0blK4+NEOf8DuHoOBDhJsHz04yh13pM=
|
||||
github.com/go-gl/glfw v0.0.0-20181213070059-819e8ce5125f/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
|
||||
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/go-redis/redis v6.14.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
|
||||
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
|
||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff h1:W71vTCKoxtdXgnm1ECDFkfQnpdqAO00zzGXLA5yaEX8=
|
||||
github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff/go.mod h1:wfqRWLHRBsRgkp5dmbG56SA0DmVtwrF5N3oPdI8t+Aw=
|
||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w=
|
||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
|
||||
github.com/hooklift/assert v0.0.0-20170704181755-9d1defd6d214 h1:WgfvpuKg42WVLkxNwzfFraXkTXPK36bMqXvMFN67clI=
|
||||
github.com/hooklift/assert v0.0.0-20170704181755-9d1defd6d214/go.mod h1:kj6hFWqfwSjFjLnYW5PK1DoxZ4O0uapwHRmd9jhln4E=
|
||||
github.com/jackmordaunt/icns v0.0.0-20181231085925-4f16af745526/go.mod h1:UQkeMHVoNcyXYq9otUupF7/h/2tmHlhrS2zw7ZVvUqc=
|
||||
github.com/josephspurrier/goversioninfo v0.0.0-20190124120936-8611f5a5ff3f/go.mod h1:eJTEwMjXb7kZ633hO3Ln9mBUCOjX2+FlTljvpl9SYdE=
|
||||
github.com/kardianos/service v1.0.0 h1:HgQS3mFfOlyntWX8Oke98JcJLqt1DBcHR4kxShpYef0=
|
||||
github.com/kardianos/service v1.0.0/go.mod h1:8CzDhVuCuugtsHyZoTvsOBuvonN/UDBvl0kH+BUxvbo=
|
||||
github.com/klauspost/compress v1.4.1 h1:8VMb5+0wMgdBykOV96DwNwKFQ+WTI4pzYURP99CcB9E=
|
||||
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
|
||||
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
|
||||
github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w=
|
||||
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
|
||||
github.com/klauspost/pgzip v1.2.1 h1:oIPZROsWuPHpOdMVWLuJZXwgjhrW8r1yEX8UqMyeNHM=
|
||||
github.com/klauspost/pgzip v1.2.1/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
|
||||
github.com/klauspost/reedsolomon v1.9.2 h1:E9CMS2Pqbv+C7tsrYad4YC9MfhnMVWhMRsTi7U0UB18=
|
||||
github.com/klauspost/reedsolomon v1.9.2/go.mod h1:CwCi+NUr9pqSVktrkN+Ondf06rkhYZ/pcNv7fu+8Un4=
|
||||
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
|
||||
github.com/panjf2000/ants/v2 v2.2.2 h1:TWzusBjq/IflXhy+/S6u5wmMLCBdJnB9tPIx9Zmhvok=
|
||||
github.com/panjf2000/ants/v2 v2.2.2/go.mod h1:1GFm8bV8nyCQvU5K4WvBCTG1/YBFOD2VzjffD8fV55A=
|
||||
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
|
||||
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
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/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 h1:X+yvsM2yrEktyI+b2qND5gpH8YhURn0k8OCaeRnkINo=
|
||||
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644/go.mod h1:nkxAfR/5quYxwPZhyDxgasBMnRtBZd0FCEpawpjMUFg=
|
||||
github.com/shirou/gopsutil v2.19.11+incompatible h1:lJHR0foqAjI4exXqWsU3DbH7bX1xvdhGdnXTIARA9W4=
|
||||
github.com/shirou/gopsutil v2.19.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
|
||||
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726/go.mod h1:3yhqj7WBBfRhbBlzyOC3gUxftwsU0u8gqevxwIHQpMw=
|
||||
github.com/siddontang/ledisdb v0.0.0-20181029004158-becf5f38d373/go.mod h1:mF1DpOSOUiJRMR+FDqaqu3EBqrybQtrDDszLUZ6oxPg=
|
||||
github.com/siddontang/rdb v0.0.0-20150307021120-fc89ed2e418d/go.mod h1:AMEsy7v5z92TR1JKMkLLoaOQk++LVnOKL3ScbJ8GNGA=
|
||||
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
|
||||
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
||||
github.com/srwiley/oksvg v0.0.0-20190829233741-58e08c8fe40e h1:LJUrNHytcMXWKxnULIHPe5SCb1jDpO9o672VB1x2EuQ=
|
||||
github.com/srwiley/oksvg v0.0.0-20190829233741-58e08c8fe40e/go.mod h1:afMbS0qvv1m5tfENCwnOdZGOF8RGR/FsZ7bvBxQGZG4=
|
||||
github.com/srwiley/rasterx v0.0.0-20181219215540-696f7edb7a7e h1:FFotfUvew9Eg02LYRl8YybAnm0HCwjjfY5JlOI1oB00=
|
||||
github.com/srwiley/rasterx v0.0.0-20181219215540-696f7edb7a7e/go.mod h1:mvWM0+15UqyrFKqdRjY6LuAVJR0HOVhJlEgZ5JWtSWU=
|
||||
github.com/ssdb/gossdb v0.0.0-20180723034631-88f6b59b84ec/go.mod h1:QBvMkMya+gXctz3kmljlUCu/yB3GZ6oee+dUozsezQE=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709 h1:Ko2LQMrRU+Oy/+EDBwX7eZ2jp3C47eDBB8EIhKTun+I=
|
||||
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/syndtr/goleveldb v0.0.0-20181127023241-353a9fca669c/go.mod h1:Z4AUp2Km+PwemOoO/VB5AOx9XSsIItzFjoJlOSiYmn0=
|
||||
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 h1:89CEmDvlq/F7SJEOqkIdNDGJXrQIhuIx9D2DBXjavSU=
|
||||
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161/go.mod h1:wM7WEvslTq+iOEAMDLSzhVuOt5BRZ05WirO+b09GHQU=
|
||||
github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b h1:mnG1fcsIB1d/3vbkBak2MM0u+vhGhlQwpeimUi7QncM=
|
||||
github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b/go.mod h1:5XA7W9S6mni3h5uvOC75dA3m9CCCaS83lltmc0ukdi4=
|
||||
github.com/tjfoc/gmsm v1.0.1 h1:R11HlqhXkDospckjZEihx9SW/2VW0RgdwrykyWMFOQU=
|
||||
github.com/tjfoc/gmsm v1.0.1/go.mod h1:XxO4hdhhrzAd+G4CjDqaOkd0hUzmtPR/d3EiBBMn/wc=
|
||||
github.com/ulikunitz/xz v0.5.6 h1:jGHAfXawEGZQ3blwU5wnWKQJvAraT7Ftq9EXjnXYgt8=
|
||||
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
|
||||
github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b/go.mod h1:Q12BUT7DqIlHRmgv3RskH+UCM/4eqVMgI0EMmlSpAXc=
|
||||
github.com/xtaci/kcp-go v5.4.4+incompatible h1:QIJ0a0Q0N1G20yLHL2+fpdzyy2v/Cb3PI+xiwx/KK9c=
|
||||
github.com/xtaci/kcp-go v5.4.4+incompatible/go.mod h1:bN6vIwHQbfHaHtFpEssmWsN45a+AZwO7eyRCmEIbtvE=
|
||||
github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae h1:J0GxkO96kL4WF+AIT3M4mfUVinOCPgf2uUWYFUzN0sM=
|
||||
github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae/go.mod h1:gXtu8J62kEgmN++bm9BVICuT/e8yiLI2KFobd/TRFsE=
|
||||
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85 h1:et7+NAX3lLIk5qUCTA9QelBjGE/NkhzYw/mhnr0s7nI=
|
||||
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8 h1:idBdZTd9UioThJp8KpM/rTSinK/ChZFBE43/WtIy8zg=
|
||||
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
|
||||
golang.org/x/image v0.0.0-20190802002840-cff245a6509b h1:+qEpEAPhDZ1o0x3tHzZTQDArnOixOzGD9HUJfcg0mb4=
|
||||
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028 h1:4+4C/Iv2U4fMZBiMCc98MG1In4gJY5YRhtpDNeDeHWs=
|
||||
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
|
||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a h1:gOpx8G595UYyvj8UK4+OFyY4rx037g3fmfhe5SasG3U=
|
||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis=
|
||||
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190804053845-51ab0e2deafa h1:KIDDMLT1O0Nr7TSxp8xM5tJcdn8tgyAONntO829og1M=
|
||||
golang.org/x/sys v0.0.0-20190804053845-51ab0e2deafa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
@@ -0,0 +1,173 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"fyne.io/fyne"
|
||||
"fyne.io/fyne/app"
|
||||
"fyne.io/fyne/layout"
|
||||
"fyne.io/fyne/widget"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/client"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/daemon"
|
||||
"github.com/cnlh/nps/lib/version"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
daemon.InitDaemon("npc", common.GetRunPath(), common.GetTmpPath())
|
||||
logs.SetLogger("store")
|
||||
application := app.New()
|
||||
window := application.NewWindow("Npc " + version.VERSION)
|
||||
window.SetContent(WidgetScreen())
|
||||
window.Resize(fyne.NewSize(910, 350))
|
||||
|
||||
window.ShowAndRun()
|
||||
|
||||
}
|
||||
|
||||
var (
|
||||
start bool
|
||||
status = "Start!"
|
||||
connType = "tcp"
|
||||
cl = new(client.TRPClient)
|
||||
)
|
||||
|
||||
func WidgetScreen() fyne.CanvasObject {
|
||||
return fyne.NewContainerWithLayout(layout.NewBorderLayout(nil, nil, nil, nil),
|
||||
makeMainTab(),
|
||||
)
|
||||
}
|
||||
|
||||
func makeMainTab() fyne.Widget {
|
||||
serverPort := widget.NewEntry()
|
||||
serverPort.SetPlaceHolder("Server:Port")
|
||||
|
||||
vKey := widget.NewEntry()
|
||||
vKey.SetPlaceHolder("Vkey")
|
||||
|
||||
radio := widget.NewRadio([]string{"tcp", "kcp"}, func(s string) { connType = s })
|
||||
radio.Horizontal = true
|
||||
|
||||
refreshCh := make(chan struct{})
|
||||
button := widget.NewButton(status, func() {
|
||||
start = !start
|
||||
if start {
|
||||
status = "Stop!"
|
||||
// init the npc
|
||||
fmt.Println("submit", serverPort.Text, vKey.Text, connType)
|
||||
sp, vk, ct := loadConfig()
|
||||
if sp != serverPort.Text || vk != vKey.Text || ct != connType {
|
||||
saveConfig(serverPort.Text, vKey.Text, connType)
|
||||
}
|
||||
cl = client.NewRPClient(serverPort.Text, vKey.Text, connType, "", nil)
|
||||
go cl.Start()
|
||||
} else {
|
||||
// close the npc
|
||||
status = "Start!"
|
||||
if cl != nil {
|
||||
go cl.Close()
|
||||
cl = nil
|
||||
}
|
||||
}
|
||||
refreshCh <- struct{}{}
|
||||
})
|
||||
go func() {
|
||||
for {
|
||||
<-refreshCh
|
||||
button.SetText(status)
|
||||
}
|
||||
}()
|
||||
|
||||
lo := widget.NewMultiLineEntry()
|
||||
lo.SetReadOnly(true)
|
||||
lo.Resize(fyne.NewSize(910, 250))
|
||||
slo := widget.NewScrollContainer(lo)
|
||||
slo.Resize(fyne.NewSize(910, 250))
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
lo.SetText(common.GetLogMsg())
|
||||
slo.Resize(fyne.NewSize(910, 250))
|
||||
}
|
||||
}()
|
||||
|
||||
sp, vk, ct := loadConfig()
|
||||
if sp != "" && vk != "" && ct != "" {
|
||||
serverPort.SetText(sp)
|
||||
vKey.SetText(vk)
|
||||
connType = ct
|
||||
radio.SetSelected(ct)
|
||||
}
|
||||
|
||||
return widget.NewVBox(
|
||||
widget.NewLabel("Npc "+version.VERSION),
|
||||
serverPort,
|
||||
vKey,
|
||||
radio,
|
||||
button,
|
||||
slo,
|
||||
)
|
||||
}
|
||||
|
||||
func getDir() (dir string, err error) {
|
||||
if runtime.GOOS != "android" {
|
||||
dir, err = os.UserConfigDir()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
dir = "/data/data/org.nps.client/files"
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func saveConfig(host, vkey, connType string) {
|
||||
data := strings.Join([]string{host, vkey, connType}, "\n")
|
||||
ph, err := getDir()
|
||||
if err != nil {
|
||||
logs.Warn("not found config dir")
|
||||
return
|
||||
}
|
||||
_ = os.Remove(path.Join(ph, "npc.conf"))
|
||||
f, err := os.OpenFile(path.Join(ph, "npc.conf"), os.O_CREATE|os.O_WRONLY, 0644)
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
if _, err := f.Write([]byte(data)); err != nil {
|
||||
f.Close() // ignore error; Write error takes precedence
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func loadConfig() (host, vkey, connType string) {
|
||||
ph, err := getDir()
|
||||
if err != nil {
|
||||
logs.Warn("not found config dir")
|
||||
return
|
||||
}
|
||||
f, err := os.OpenFile(path.Join(ph, "npc.conf"), os.O_RDONLY, 0644)
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
data, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return
|
||||
}
|
||||
li := strings.Split(string(data), "\n")
|
||||
host = li[0]
|
||||
vkey = li[1]
|
||||
connType = li[2]
|
||||
return
|
||||
}
|
||||
|
After Width: | Height: | Size: 276 KiB |
|
After Width: | Height: | Size: 276 KiB |
|
After Width: | Height: | Size: 26 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 712 KiB |
|
After Width: | Height: | Size: 690 KiB |
|
Before Width: | Height: | Size: 233 KiB After Width: | Height: | Size: 290 KiB |
|
Before Width: | Height: | Size: 280 KiB After Width: | Height: | Size: 232 KiB |
@@ -1,227 +0,0 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type list struct {
|
||||
connList chan *Conn
|
||||
}
|
||||
|
||||
func (l *list) Add(c *Conn) {
|
||||
l.connList <- c
|
||||
}
|
||||
|
||||
func (l *list) Pop() *Conn {
|
||||
return <-l.connList
|
||||
}
|
||||
func (l *list) Len() int {
|
||||
return len(l.connList)
|
||||
}
|
||||
|
||||
func newList() *list {
|
||||
l := new(list)
|
||||
l.connList = make(chan *Conn, 1000)
|
||||
return l
|
||||
}
|
||||
|
||||
type Tunnel struct {
|
||||
tunnelPort int //通信隧道端口
|
||||
listener *net.TCPListener //server端监听
|
||||
signalList map[string]*list //通信
|
||||
tunnelList map[string]*list //隧道
|
||||
lock sync.Mutex
|
||||
tunnelLock sync.Mutex
|
||||
}
|
||||
|
||||
func newTunnel(tunnelPort int) *Tunnel {
|
||||
t := new(Tunnel)
|
||||
t.tunnelPort = tunnelPort
|
||||
t.signalList = make(map[string]*list)
|
||||
t.tunnelList = make(map[string]*list)
|
||||
return t
|
||||
}
|
||||
|
||||
func (s *Tunnel) StartTunnel() error {
|
||||
var err error
|
||||
s.listener, err = net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP("0.0.0.0"), s.tunnelPort, ""})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go s.tunnelProcess()
|
||||
return nil
|
||||
}
|
||||
|
||||
//tcp server
|
||||
func (s *Tunnel) tunnelProcess() error {
|
||||
var err error
|
||||
for {
|
||||
conn, err := s.listener.Accept()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
go s.cliProcess(NewConn(conn))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
//验证失败,返回错误验证flag,并且关闭连接
|
||||
func (s *Tunnel) verifyError(c *Conn) {
|
||||
c.conn.Write([]byte(VERIFY_EER))
|
||||
c.conn.Close()
|
||||
}
|
||||
|
||||
func (s *Tunnel) cliProcess(c *Conn) error {
|
||||
c.conn.(*net.TCPConn).SetReadDeadline(time.Now().Add(time.Duration(5) * time.Second))
|
||||
vval := make([]byte, 32)
|
||||
if _, err := c.conn.Read(vval); err != nil {
|
||||
log.Println("客户端读超时。客户端地址为::", c.conn.RemoteAddr())
|
||||
c.conn.Close()
|
||||
return err
|
||||
}
|
||||
if !verify(string(vval)) {
|
||||
log.Println("当前客户端连接校验错误,关闭此客户端:", c.conn.RemoteAddr())
|
||||
s.verifyError(c)
|
||||
return err
|
||||
}
|
||||
c.conn.(*net.TCPConn).SetReadDeadline(time.Time{})
|
||||
//做一个判断 添加到对应的channel里面以供使用
|
||||
if flag, err := c.ReadFlag(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return s.typeDeal(flag, c, string(vval))
|
||||
}
|
||||
}
|
||||
|
||||
//tcp连接类型区分
|
||||
func (s *Tunnel) typeDeal(typeVal string, c *Conn, cFlag string) error {
|
||||
switch typeVal {
|
||||
case WORK_MAIN:
|
||||
s.addList(s.signalList, c, cFlag)
|
||||
case WORK_CHAN:
|
||||
s.addList(s.tunnelList, c, cFlag)
|
||||
default:
|
||||
return errors.New("无法识别")
|
||||
}
|
||||
c.SetAlive()
|
||||
return nil
|
||||
}
|
||||
|
||||
//加到对应的list中
|
||||
func (s *Tunnel) addList(m map[string]*list, c *Conn, cFlag string) {
|
||||
s.lock.Lock()
|
||||
if v, ok := m[cFlag]; ok {
|
||||
v.Add(c)
|
||||
} else {
|
||||
l := newList()
|
||||
l.Add(c)
|
||||
m[cFlag] = l
|
||||
}
|
||||
s.lock.Unlock()
|
||||
}
|
||||
|
||||
//新建隧道
|
||||
func (s *Tunnel) newChan(cFlag string) error {
|
||||
if err := s.wait(s.signalList, cFlag); err != nil {
|
||||
return err
|
||||
}
|
||||
retry:
|
||||
connPass := s.signalList[cFlag].Pop()
|
||||
_, err := connPass.conn.Write([]byte("chan"))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
goto retry
|
||||
}
|
||||
s.signalList[cFlag].Add(connPass)
|
||||
return nil
|
||||
}
|
||||
|
||||
//得到一个tcp隧道
|
||||
func (s *Tunnel) GetTunnel(cFlag string, en, de int, crypt, mux bool) (c *Conn, err error) {
|
||||
s.tunnelLock.Lock()
|
||||
if v, ok := s.tunnelList[cFlag]; !ok || v.Len() < 3 { //新建通道
|
||||
go s.newChan(cFlag)
|
||||
}
|
||||
retry:
|
||||
if err = s.wait(s.tunnelList, cFlag); err != nil {
|
||||
return
|
||||
}
|
||||
c = s.tunnelList[cFlag].Pop()
|
||||
if _, err = c.wTest(); err != nil {
|
||||
c.Close()
|
||||
goto retry
|
||||
}
|
||||
c.WriteConnInfo(en, de, crypt, mux)
|
||||
s.tunnelLock.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
//得到一个通信通道
|
||||
func (s *Tunnel) GetSignal(cFlag string) (err error, conn *Conn) {
|
||||
if v, ok := s.signalList[cFlag]; !ok || v.Len() == 0 {
|
||||
err = errors.New("客户端未连接")
|
||||
return
|
||||
}
|
||||
conn = s.signalList[cFlag].Pop()
|
||||
return
|
||||
}
|
||||
|
||||
//重回slice 复用
|
||||
func (s *Tunnel) ReturnSignal(conn *Conn, cFlag string) {
|
||||
if v, ok := s.signalList[cFlag]; ok {
|
||||
v.Add(conn)
|
||||
}
|
||||
}
|
||||
|
||||
//重回slice 复用
|
||||
func (s *Tunnel) ReturnTunnel(conn *Conn, cFlag string) {
|
||||
if v, ok := s.tunnelList[cFlag]; ok {
|
||||
v.Add(conn)
|
||||
}
|
||||
}
|
||||
|
||||
//删除通信通道
|
||||
func (s *Tunnel) DelClientSignal(cFlag string) {
|
||||
s.delClient(cFlag, s.signalList)
|
||||
}
|
||||
|
||||
//删除隧道
|
||||
func (s *Tunnel) DelClientTunnel(cFlag string) {
|
||||
s.delClient(cFlag, s.tunnelList)
|
||||
}
|
||||
|
||||
func (s *Tunnel) delClient(cFlag string, l map[string]*list) {
|
||||
if t := l[getverifyval(cFlag)]; t != nil {
|
||||
for {
|
||||
if t.Len() <= 0 {
|
||||
break
|
||||
}
|
||||
t.Pop().Close()
|
||||
}
|
||||
delete(l, getverifyval(cFlag))
|
||||
}
|
||||
}
|
||||
|
||||
//等待
|
||||
func (s *Tunnel) wait(m map[string]*list, cFlag string) error {
|
||||
ticker := time.NewTicker(time.Millisecond * 100)
|
||||
stop := time.After(time.Second * 10)
|
||||
loop:
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
if _, ok := m[cFlag]; ok {
|
||||
ticker.Stop()
|
||||
break loop
|
||||
}
|
||||
case <-stop:
|
||||
return errors.New("client key: " + cFlag + ",err: get client conn timeout")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Cache is an LRU cache. It is safe for concurrent access.
|
||||
type Cache struct {
|
||||
// MaxEntries is the maximum number of cache entries before
|
||||
// an item is evicted. Zero means no limit.
|
||||
MaxEntries int
|
||||
|
||||
//Execute this callback function when an element is culled
|
||||
OnEvicted func(key Key, value interface{})
|
||||
|
||||
ll *list.List //list
|
||||
cache sync.Map
|
||||
}
|
||||
|
||||
// A Key may be any value that is comparable. See http://golang.org/ref/spec#Comparison_operators
|
||||
type Key interface{}
|
||||
|
||||
type entry struct {
|
||||
key Key
|
||||
value interface{}
|
||||
}
|
||||
|
||||
// New creates a new Cache.
|
||||
// If maxEntries is 0, the cache has no length limit.
|
||||
// that eviction is done by the caller.
|
||||
func New(maxEntries int) *Cache {
|
||||
return &Cache{
|
||||
MaxEntries: maxEntries,
|
||||
ll: list.New(),
|
||||
//cache: make(map[interface{}]*list.Element),
|
||||
}
|
||||
}
|
||||
|
||||
// If the key value already exists, move the key to the front
|
||||
func (c *Cache) Add(key Key, value interface{}) {
|
||||
if ee, ok := c.cache.Load(key); ok {
|
||||
c.ll.MoveToFront(ee.(*list.Element)) // move to the front
|
||||
ee.(*list.Element).Value.(*entry).value = value
|
||||
return
|
||||
}
|
||||
ele := c.ll.PushFront(&entry{key, value})
|
||||
c.cache.Store(key, ele)
|
||||
if c.MaxEntries != 0 && c.ll.Len() > c.MaxEntries { // Remove the oldest element if the limit is exceeded
|
||||
c.RemoveOldest()
|
||||
}
|
||||
}
|
||||
|
||||
// Get looks up a key's value from the cache.
|
||||
func (c *Cache) Get(key Key) (value interface{}, ok bool) {
|
||||
if ele, hit := c.cache.Load(key); hit {
|
||||
c.ll.MoveToFront(ele.(*list.Element))
|
||||
return ele.(*list.Element).Value.(*entry).value, true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Remove removes the provided key from the cache.
|
||||
func (c *Cache) Remove(key Key) {
|
||||
if ele, hit := c.cache.Load(key); hit {
|
||||
c.removeElement(ele.(*list.Element))
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveOldest removes the oldest item from the cache.
|
||||
func (c *Cache) RemoveOldest() {
|
||||
ele := c.ll.Back()
|
||||
if ele != nil {
|
||||
c.removeElement(ele)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) removeElement(e *list.Element) {
|
||||
c.ll.Remove(e)
|
||||
kv := e.Value.(*entry)
|
||||
c.cache.Delete(kv.key)
|
||||
if c.OnEvicted != nil {
|
||||
c.OnEvicted(kv.key, kv.value)
|
||||
}
|
||||
}
|
||||
|
||||
// Len returns the number of items in the cache.
|
||||
func (c *Cache) Len() int {
|
||||
return c.ll.Len()
|
||||
}
|
||||
|
||||
// Clear purges all stored items from the cache.
|
||||
func (c *Cache) Clear() {
|
||||
if c.OnEvicted != nil {
|
||||
c.cache.Range(func(key, value interface{}) bool {
|
||||
kv := value.(*list.Element).Value.(*entry)
|
||||
c.OnEvicted(kv.key, kv.value)
|
||||
return true
|
||||
})
|
||||
}
|
||||
c.ll = nil
|
||||
}
|
||||
@@ -1,160 +0,0 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TRPClient struct {
|
||||
svrAddr string
|
||||
tcpNum int
|
||||
sync.Mutex
|
||||
vKey string
|
||||
}
|
||||
|
||||
//new client
|
||||
func NewRPClient(svraddr string, tcpNum int, vKey string) *TRPClient {
|
||||
c := new(TRPClient)
|
||||
c.svrAddr = svraddr
|
||||
c.tcpNum = tcpNum
|
||||
c.vKey = vKey
|
||||
return c
|
||||
}
|
||||
|
||||
//start
|
||||
func (s *TRPClient) Start() error {
|
||||
for i := 0; i < s.tcpNum; i++ {
|
||||
go s.newConn()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//新建
|
||||
func (s *TRPClient) newConn() error {
|
||||
s.Lock()
|
||||
conn, err := net.Dial("tcp", s.svrAddr)
|
||||
if err != nil {
|
||||
log.Println("连接服务端失败,五秒后将重连")
|
||||
time.Sleep(time.Second * 5)
|
||||
s.Unlock()
|
||||
go s.newConn()
|
||||
return err
|
||||
}
|
||||
s.Unlock()
|
||||
return s.process(NewConn(conn))
|
||||
}
|
||||
|
||||
//处理
|
||||
func (s *TRPClient) process(c *Conn) error {
|
||||
c.SetAlive()
|
||||
if _, err := c.Write([]byte(getverifyval(s.vKey))); err != nil {
|
||||
return err
|
||||
}
|
||||
c.wMain()
|
||||
for {
|
||||
flags, err := c.ReadFlag()
|
||||
if err != nil {
|
||||
log.Println("服务端断开,五秒后将重连", err)
|
||||
time.Sleep(5 * time.Second)
|
||||
go s.newConn()
|
||||
break
|
||||
}
|
||||
switch flags {
|
||||
case VERIFY_EER:
|
||||
log.Fatalln("vkey:", s.vKey, "不正确,服务端拒绝连接,请检查")
|
||||
case RES_SIGN: //代理请求模式
|
||||
if err := s.dealHttp(c); err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
case WORK_CHAN: //隧道模式,每次开启10个,加快连接速度
|
||||
for i := 0; i < 5; i++ {
|
||||
go s.dealChan()
|
||||
}
|
||||
case RES_MSG:
|
||||
log.Println("服务端返回错误。")
|
||||
default:
|
||||
log.Println("无法解析该错误。", flags)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//隧道模式处理
|
||||
func (s *TRPClient) dealChan() {
|
||||
var err error
|
||||
//创建一个tcp连接
|
||||
conn, err := net.Dial("tcp", s.svrAddr)
|
||||
if err != nil {
|
||||
log.Println("connect to ", s.svrAddr, "error:", err)
|
||||
return
|
||||
}
|
||||
//验证
|
||||
if _, err := conn.Write([]byte(getverifyval(s.vKey))); err != nil {
|
||||
log.Println("connect to ", s.svrAddr, "error:", err)
|
||||
return
|
||||
}
|
||||
//默认长连接保持
|
||||
c := NewConn(conn)
|
||||
c.SetAlive()
|
||||
//写标志
|
||||
c.wChan()
|
||||
re:
|
||||
//获取连接的host type(tcp or udp)
|
||||
typeStr, host, en, de, crypt, mux, err := c.GetHostFromConn()
|
||||
if err != nil {
|
||||
log.Println("get host info error:", err)
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
//与目标建立连接,超时时间为3
|
||||
server, err := net.DialTimeout(typeStr, host, time.Second*3)
|
||||
if err != nil {
|
||||
log.Println("connect to ", host, "error:", err, mux)
|
||||
c.wFail()
|
||||
goto end
|
||||
}
|
||||
c.wSuccess()
|
||||
go relay(server, c.conn, de, crypt, mux)
|
||||
relay(c.conn, server, en, crypt, mux)
|
||||
end:
|
||||
if mux {
|
||||
goto re
|
||||
} else {
|
||||
c.Close()
|
||||
}
|
||||
}
|
||||
|
||||
//http模式处理
|
||||
func (s *TRPClient) dealHttp(c *Conn) error {
|
||||
buf := make([]byte, 1024*32)
|
||||
en, de, crypt, _ := c.GetConnInfoFromConn()
|
||||
n, err := c.ReadFrom(buf, de, crypt)
|
||||
if err != nil {
|
||||
c.wError()
|
||||
return err
|
||||
}
|
||||
req, err := DecodeRequest(buf[:n])
|
||||
if err != nil {
|
||||
c.wError()
|
||||
return err
|
||||
}
|
||||
respBytes, err := GetEncodeResponse(req)
|
||||
if err != nil {
|
||||
c.wError()
|
||||
return err
|
||||
}
|
||||
c.wSign()
|
||||
n, err = c.WriteTo(respBytes, en, crypt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n != len(respBytes) {
|
||||
return errors.New(fmt.Sprintf("发送数据长度错误,已经发送:%dbyte,总字节长:%dbyte\n", n, len(respBytes)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package common
|
||||
|
||||
const (
|
||||
CONN_DATA_SEQ = "*#*" //Separator
|
||||
VERIFY_EER = "vkey"
|
||||
VERIFY_SUCCESS = "sucs"
|
||||
WORK_MAIN = "main"
|
||||
WORK_CHAN = "chan"
|
||||
WORK_CONFIG = "conf"
|
||||
WORK_REGISTER = "rgst"
|
||||
WORK_SECRET = "sert"
|
||||
WORK_FILE = "file"
|
||||
WORK_P2P = "p2pm"
|
||||
WORK_P2P_VISITOR = "p2pv"
|
||||
WORK_P2P_PROVIDER = "p2pp"
|
||||
WORK_P2P_CONNECT = "p2pc"
|
||||
WORK_P2P_SUCCESS = "p2ps"
|
||||
WORK_P2P_END = "p2pe"
|
||||
WORK_P2P_LAST = "p2pl"
|
||||
WORK_STATUS = "stus"
|
||||
RES_MSG = "msg0"
|
||||
RES_CLOSE = "clse"
|
||||
NEW_UDP_CONN = "udpc" //p2p udp conn
|
||||
NEW_TASK = "task"
|
||||
NEW_CONF = "conf"
|
||||
NEW_HOST = "host"
|
||||
CONN_TCP = "tcp"
|
||||
CONN_UDP = "udp"
|
||||
CONN_TEST = "TST"
|
||||
UnauthorizedBytes = `HTTP/1.1 401 Unauthorized
|
||||
Content-Type: text/plain; charset=utf-8
|
||||
WWW-Authenticate: Basic realm="easyProxy"
|
||||
|
||||
401 Unauthorized`
|
||||
ConnectionFailBytes = `HTTP/1.1 404 Not Found
|
||||
|
||||
`
|
||||
)
|
||||
|
||||
const (
|
||||
MUX_PING_FLAG uint8 = iota
|
||||
MUX_NEW_CONN_OK
|
||||
MUX_NEW_CONN_Fail
|
||||
MUX_NEW_MSG
|
||||
MUX_NEW_MSG_PART
|
||||
MUX_MSG_SEND_OK
|
||||
MUX_NEW_CONN
|
||||
MUX_CONN_CLOSE
|
||||
MUX_PING_RETURN
|
||||
MUX_PING int32 = -1
|
||||
MAXIMUM_SEGMENT_SIZE = PoolSizeWindow
|
||||
MAXIMUM_WINDOW_SIZE = 1 << 25 // 1<<31-1 TCP slide window size is very large,
|
||||
// we use 32M, reduce memory usage
|
||||
)
|
||||
@@ -0,0 +1,48 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego/logs"
|
||||
"time"
|
||||
)
|
||||
|
||||
const MaxMsgLen = 5000
|
||||
|
||||
var logMsgs string
|
||||
|
||||
func init() {
|
||||
logs.Register("store", func() logs.Logger {
|
||||
return new(StoreMsg)
|
||||
})
|
||||
}
|
||||
|
||||
func GetLogMsg() string {
|
||||
return logMsgs
|
||||
}
|
||||
|
||||
type StoreMsg struct {
|
||||
}
|
||||
|
||||
func (lg *StoreMsg) Init(config string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (lg *StoreMsg) WriteMsg(when time.Time, msg string, level int) error {
|
||||
m := when.Format("2006-01-02 15:04:05") + " " + msg + "\r\n"
|
||||
if len(logMsgs) > MaxMsgLen {
|
||||
start := MaxMsgLen - len(m)
|
||||
if start <= 0 {
|
||||
start = MaxMsgLen
|
||||
}
|
||||
logMsgs = logMsgs[start:]
|
||||
}
|
||||
logMsgs += m
|
||||
return nil
|
||||
}
|
||||
|
||||
func (lg *StoreMsg) Destroy() {
|
||||
return
|
||||
}
|
||||
|
||||
func (lg *StoreMsg) Flush() {
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,450 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type NetPackager interface {
|
||||
Pack(writer io.Writer) (err error)
|
||||
UnPack(reader io.Reader) (err error)
|
||||
}
|
||||
|
||||
type BasePackager struct {
|
||||
Length uint16
|
||||
Content []byte
|
||||
}
|
||||
|
||||
func (Self *BasePackager) NewPac(contents ...interface{}) (err error) {
|
||||
Self.clean()
|
||||
for _, content := range contents {
|
||||
switch content.(type) {
|
||||
case nil:
|
||||
Self.Content = Self.Content[:0]
|
||||
case []byte:
|
||||
err = Self.appendByte(content.([]byte))
|
||||
case string:
|
||||
err = Self.appendByte([]byte(content.(string)))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = Self.appendByte([]byte(CONN_DATA_SEQ))
|
||||
default:
|
||||
err = Self.marshal(content)
|
||||
}
|
||||
}
|
||||
Self.setLength()
|
||||
if Self.Length > MAXIMUM_SEGMENT_SIZE {
|
||||
err = errors.New("mux:packer: newpack content segment too large")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (Self *BasePackager) appendByte(data []byte) (err error) {
|
||||
m := len(Self.Content)
|
||||
n := m + len(data)
|
||||
if n <= cap(Self.Content) {
|
||||
Self.Content = Self.Content[0:n] // grow the length for copy
|
||||
copy(Self.Content[m:n], data)
|
||||
return nil
|
||||
} else {
|
||||
return errors.New("pack content too large")
|
||||
}
|
||||
}
|
||||
|
||||
//似乎这里涉及到父类作用域问题,当子类调用父类的方法时,其struct仅仅为父类的
|
||||
func (Self *BasePackager) Pack(writer io.Writer) (err error) {
|
||||
err = binary.Write(writer, binary.LittleEndian, Self.Length)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = binary.Write(writer, binary.LittleEndian, Self.Content)
|
||||
return
|
||||
}
|
||||
|
||||
//Unpack 会导致传入的数字类型转化成float64!!
|
||||
//主要原因是json unmarshal并未传入正确的数据类型
|
||||
func (Self *BasePackager) UnPack(reader io.Reader) (n uint16, err error) {
|
||||
Self.clean()
|
||||
n += 2 // uint16
|
||||
err = binary.Read(reader, binary.LittleEndian, &Self.Length)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if int(Self.Length) > cap(Self.Content) {
|
||||
err = errors.New("unpack err, content length too large")
|
||||
return
|
||||
}
|
||||
if Self.Length > MAXIMUM_SEGMENT_SIZE {
|
||||
err = errors.New("mux:packer: unpack content segment too large")
|
||||
return
|
||||
}
|
||||
Self.Content = Self.Content[:int(Self.Length)]
|
||||
//n, err := io.ReadFull(reader, Self.Content)
|
||||
//if n != int(Self.Length) {
|
||||
// err = io.ErrUnexpectedEOF
|
||||
//}
|
||||
err = binary.Read(reader, binary.LittleEndian, Self.Content)
|
||||
n += Self.Length
|
||||
return
|
||||
}
|
||||
|
||||
func (Self *BasePackager) marshal(content interface{}) (err error) {
|
||||
tmp, err := json.Marshal(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = Self.appendByte(tmp)
|
||||
return
|
||||
}
|
||||
|
||||
func (Self *BasePackager) Unmarshal(content interface{}) (err error) {
|
||||
err = json.Unmarshal(Self.Content, content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (Self *BasePackager) setLength() {
|
||||
Self.Length = uint16(len(Self.Content))
|
||||
return
|
||||
}
|
||||
|
||||
func (Self *BasePackager) clean() {
|
||||
Self.Length = 0
|
||||
Self.Content = Self.Content[:0] // reset length
|
||||
}
|
||||
|
||||
func (Self *BasePackager) Split() (strList []string) {
|
||||
n := bytes.IndexByte(Self.Content, 0)
|
||||
strList = strings.Split(string(Self.Content[:n]), CONN_DATA_SEQ)
|
||||
strList = strList[0 : len(strList)-1]
|
||||
return
|
||||
}
|
||||
|
||||
type ConnPackager struct {
|
||||
// Todo
|
||||
ConnType uint8
|
||||
BasePackager
|
||||
}
|
||||
|
||||
func (Self *ConnPackager) NewPac(connType uint8, content ...interface{}) (err error) {
|
||||
Self.ConnType = connType
|
||||
err = Self.BasePackager.NewPac(content...)
|
||||
return
|
||||
}
|
||||
|
||||
func (Self *ConnPackager) Pack(writer io.Writer) (err error) {
|
||||
err = binary.Write(writer, binary.LittleEndian, Self.ConnType)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = Self.BasePackager.Pack(writer)
|
||||
return
|
||||
}
|
||||
|
||||
func (Self *ConnPackager) UnPack(reader io.Reader) (n uint16, err error) {
|
||||
err = binary.Read(reader, binary.LittleEndian, &Self.ConnType)
|
||||
if err != nil && err != io.EOF {
|
||||
return
|
||||
}
|
||||
n, err = Self.BasePackager.UnPack(reader)
|
||||
n += 2
|
||||
return
|
||||
}
|
||||
|
||||
type MuxPackager struct {
|
||||
Flag uint8
|
||||
Id int32
|
||||
Window uint32
|
||||
ReadLength uint32
|
||||
BasePackager
|
||||
}
|
||||
|
||||
func (Self *MuxPackager) NewPac(flag uint8, id int32, content ...interface{}) (err error) {
|
||||
Self.Flag = flag
|
||||
Self.Id = id
|
||||
switch flag {
|
||||
case MUX_PING_FLAG, MUX_PING_RETURN, MUX_NEW_MSG, MUX_NEW_MSG_PART:
|
||||
Self.Content = WindowBuff.Get()
|
||||
err = Self.BasePackager.NewPac(content...)
|
||||
//logs.Warn(Self.Length, string(Self.Content))
|
||||
case MUX_MSG_SEND_OK:
|
||||
// MUX_MSG_SEND_OK contains two data
|
||||
switch content[0].(type) {
|
||||
case int:
|
||||
Self.Window = uint32(content[0].(int))
|
||||
case uint32:
|
||||
Self.Window = content[0].(uint32)
|
||||
}
|
||||
switch content[1].(type) {
|
||||
case int:
|
||||
Self.ReadLength = uint32(content[1].(int))
|
||||
case uint32:
|
||||
Self.ReadLength = content[1].(uint32)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (Self *MuxPackager) Pack(writer io.Writer) (err error) {
|
||||
err = binary.Write(writer, binary.LittleEndian, Self.Flag)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = binary.Write(writer, binary.LittleEndian, Self.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
switch Self.Flag {
|
||||
case MUX_NEW_MSG, MUX_NEW_MSG_PART, MUX_PING_FLAG, MUX_PING_RETURN:
|
||||
err = Self.BasePackager.Pack(writer)
|
||||
WindowBuff.Put(Self.Content)
|
||||
case MUX_MSG_SEND_OK:
|
||||
err = binary.Write(writer, binary.LittleEndian, Self.Window)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = binary.Write(writer, binary.LittleEndian, Self.ReadLength)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (Self *MuxPackager) UnPack(reader io.Reader) (n uint16, err error) {
|
||||
err = binary.Read(reader, binary.LittleEndian, &Self.Flag)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = binary.Read(reader, binary.LittleEndian, &Self.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
switch Self.Flag {
|
||||
case MUX_NEW_MSG, MUX_NEW_MSG_PART, MUX_PING_FLAG, MUX_PING_RETURN:
|
||||
Self.Content = WindowBuff.Get() // need get a window buf from pool
|
||||
Self.BasePackager.clean() // also clean the content
|
||||
n, err = Self.BasePackager.UnPack(reader)
|
||||
//logs.Warn("unpack", Self.Length, string(Self.Content))
|
||||
case MUX_MSG_SEND_OK:
|
||||
err = binary.Read(reader, binary.LittleEndian, &Self.Window)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n += 4 // uint32
|
||||
err = binary.Read(reader, binary.LittleEndian, &Self.ReadLength)
|
||||
n += 4 // uint32
|
||||
}
|
||||
n += 5 //uint8 int32
|
||||
return
|
||||
}
|
||||
|
||||
const (
|
||||
ipV4 = 1
|
||||
domainName = 3
|
||||
ipV6 = 4
|
||||
)
|
||||
|
||||
type UDPHeader struct {
|
||||
Rsv uint16
|
||||
Frag uint8
|
||||
Addr *Addr
|
||||
}
|
||||
|
||||
func NewUDPHeader(rsv uint16, frag uint8, addr *Addr) *UDPHeader {
|
||||
return &UDPHeader{
|
||||
Rsv: rsv,
|
||||
Frag: frag,
|
||||
Addr: addr,
|
||||
}
|
||||
}
|
||||
|
||||
type Addr struct {
|
||||
Type uint8
|
||||
Host string
|
||||
Port uint16
|
||||
}
|
||||
|
||||
func (addr *Addr) String() string {
|
||||
return net.JoinHostPort(addr.Host, strconv.Itoa(int(addr.Port)))
|
||||
}
|
||||
|
||||
func (addr *Addr) Decode(b []byte) error {
|
||||
addr.Type = b[0]
|
||||
pos := 1
|
||||
switch addr.Type {
|
||||
case ipV4:
|
||||
addr.Host = net.IP(b[pos : pos+net.IPv4len]).String()
|
||||
pos += net.IPv4len
|
||||
case ipV6:
|
||||
addr.Host = net.IP(b[pos : pos+net.IPv6len]).String()
|
||||
pos += net.IPv6len
|
||||
case domainName:
|
||||
addrlen := int(b[pos])
|
||||
pos++
|
||||
addr.Host = string(b[pos : pos+addrlen])
|
||||
pos += addrlen
|
||||
default:
|
||||
return errors.New("decode error")
|
||||
}
|
||||
|
||||
addr.Port = binary.BigEndian.Uint16(b[pos:])
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (addr *Addr) Encode(b []byte) (int, error) {
|
||||
b[0] = addr.Type
|
||||
pos := 1
|
||||
switch addr.Type {
|
||||
case ipV4:
|
||||
ip4 := net.ParseIP(addr.Host).To4()
|
||||
if ip4 == nil {
|
||||
ip4 = net.IPv4zero.To4()
|
||||
}
|
||||
pos += copy(b[pos:], ip4)
|
||||
case domainName:
|
||||
b[pos] = byte(len(addr.Host))
|
||||
pos++
|
||||
pos += copy(b[pos:], []byte(addr.Host))
|
||||
case ipV6:
|
||||
ip16 := net.ParseIP(addr.Host).To16()
|
||||
if ip16 == nil {
|
||||
ip16 = net.IPv6zero.To16()
|
||||
}
|
||||
pos += copy(b[pos:], ip16)
|
||||
default:
|
||||
b[0] = ipV4
|
||||
copy(b[pos:pos+4], net.IPv4zero.To4())
|
||||
pos += 4
|
||||
}
|
||||
binary.BigEndian.PutUint16(b[pos:], addr.Port)
|
||||
pos += 2
|
||||
|
||||
return pos, nil
|
||||
}
|
||||
|
||||
func (h *UDPHeader) Write(w io.Writer) error {
|
||||
b := BufPoolUdp.Get().([]byte)
|
||||
defer BufPoolUdp.Put(b)
|
||||
|
||||
binary.BigEndian.PutUint16(b[:2], h.Rsv)
|
||||
b[2] = h.Frag
|
||||
|
||||
addr := h.Addr
|
||||
if addr == nil {
|
||||
addr = &Addr{}
|
||||
}
|
||||
length, _ := addr.Encode(b[3:])
|
||||
|
||||
_, err := w.Write(b[:3+length])
|
||||
return err
|
||||
}
|
||||
|
||||
type UDPDatagram struct {
|
||||
Header *UDPHeader
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func ReadUDPDatagram(r io.Reader) (*UDPDatagram, error) {
|
||||
b := BufPoolUdp.Get().([]byte)
|
||||
defer BufPoolUdp.Put(b)
|
||||
|
||||
// when r is a streaming (such as TCP connection), we may read more than the required data,
|
||||
// but we don't know how to handle it. So we use io.ReadFull to instead of io.ReadAtLeast
|
||||
// to make sure that no redundant data will be discarded.
|
||||
n, err := io.ReadFull(r, b[:5])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
header := &UDPHeader{
|
||||
Rsv: binary.BigEndian.Uint16(b[:2]),
|
||||
Frag: b[2],
|
||||
}
|
||||
|
||||
atype := b[3]
|
||||
hlen := 0
|
||||
switch atype {
|
||||
case ipV4:
|
||||
hlen = 10
|
||||
case ipV6:
|
||||
hlen = 22
|
||||
case domainName:
|
||||
hlen = 7 + int(b[4])
|
||||
default:
|
||||
return nil, errors.New("addr not support")
|
||||
}
|
||||
dlen := int(header.Rsv)
|
||||
if dlen == 0 { // standard SOCKS5 UDP datagram
|
||||
extra, err := ioutil.ReadAll(r) // we assume no redundant data
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
copy(b[n:], extra)
|
||||
n += len(extra) // total length
|
||||
dlen = n - hlen // data length
|
||||
} else { // extended feature, for UDP over TCP, using reserved field as data length
|
||||
if _, err := io.ReadFull(r, b[n:hlen+dlen]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
n = hlen + dlen
|
||||
}
|
||||
header.Addr = new(Addr)
|
||||
if err := header.Addr.Decode(b[3:hlen]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data := make([]byte, dlen)
|
||||
copy(data, b[hlen:n])
|
||||
d := &UDPDatagram{
|
||||
Header: header,
|
||||
Data: data,
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func NewUDPDatagram(header *UDPHeader, data []byte) *UDPDatagram {
|
||||
return &UDPDatagram{
|
||||
Header: header,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *UDPDatagram) Write(w io.Writer) error {
|
||||
h := d.Header
|
||||
if h == nil {
|
||||
h = &UDPHeader{}
|
||||
}
|
||||
buf := bytes.Buffer{}
|
||||
if err := h.Write(&buf); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := buf.Write(d.Data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := buf.WriteTo(w)
|
||||
return err
|
||||
}
|
||||
|
||||
func ToSocksAddr(addr net.Addr) *Addr {
|
||||
host := "0.0.0.0"
|
||||
port := 0
|
||||
if addr != nil {
|
||||
h, p, _ := net.SplitHostPort(addr.String())
|
||||
host = h
|
||||
port, _ = strconv.Atoi(p)
|
||||
}
|
||||
return &Addr{
|
||||
Type: ipV4,
|
||||
Host: host,
|
||||
Port: uint16(port),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const PoolSize = 64 * 1024
|
||||
const PoolSizeSmall = 100
|
||||
const PoolSizeUdp = 1472 + 200
|
||||
const PoolSizeCopy = 32 << 10
|
||||
const PoolSizeBuffer = 4096
|
||||
const PoolSizeWindow = PoolSizeBuffer - 2 - 4 - 4 - 1
|
||||
|
||||
var BufPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, PoolSize)
|
||||
},
|
||||
}
|
||||
|
||||
var BufPoolUdp = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, PoolSizeUdp)
|
||||
},
|
||||
}
|
||||
var BufPoolMax = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, PoolSize)
|
||||
},
|
||||
}
|
||||
var BufPoolSmall = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, PoolSizeSmall)
|
||||
},
|
||||
}
|
||||
var BufPoolCopy = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, PoolSizeCopy)
|
||||
},
|
||||
}
|
||||
|
||||
func PutBufPoolUdp(buf []byte) {
|
||||
if cap(buf) == PoolSizeUdp {
|
||||
BufPoolUdp.Put(buf[:PoolSizeUdp])
|
||||
}
|
||||
}
|
||||
|
||||
func PutBufPoolCopy(buf []byte) {
|
||||
if cap(buf) == PoolSizeCopy {
|
||||
BufPoolCopy.Put(buf[:PoolSizeCopy])
|
||||
}
|
||||
}
|
||||
|
||||
func GetBufPoolCopy() []byte {
|
||||
return (BufPoolCopy.Get().([]byte))[:PoolSizeCopy]
|
||||
}
|
||||
|
||||
func PutBufPoolMax(buf []byte) {
|
||||
if cap(buf) == PoolSize {
|
||||
BufPoolMax.Put(buf[:PoolSize])
|
||||
}
|
||||
}
|
||||
|
||||
type copyBufferPool struct {
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
func (Self *copyBufferPool) New() {
|
||||
Self.pool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, PoolSizeCopy, PoolSizeCopy)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (Self *copyBufferPool) Get() []byte {
|
||||
buf := Self.pool.Get().([]byte)
|
||||
return buf[:PoolSizeCopy] // just like make a new slice, but data may not be 0
|
||||
}
|
||||
|
||||
func (Self *copyBufferPool) Put(x []byte) {
|
||||
if len(x) == PoolSizeCopy {
|
||||
Self.pool.Put(x)
|
||||
} else {
|
||||
x = nil // buf is not full, not allowed, New method returns a full buf
|
||||
}
|
||||
}
|
||||
|
||||
type windowBufferPool struct {
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
func (Self *windowBufferPool) New() {
|
||||
Self.pool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, PoolSizeWindow, PoolSizeWindow)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (Self *windowBufferPool) Get() (buf []byte) {
|
||||
buf = Self.pool.Get().([]byte)
|
||||
return buf[:PoolSizeWindow]
|
||||
}
|
||||
|
||||
func (Self *windowBufferPool) Put(x []byte) {
|
||||
Self.pool.Put(x[:PoolSizeWindow]) // make buf to full
|
||||
}
|
||||
|
||||
type bufferPool struct {
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
func (Self *bufferPool) New() {
|
||||
Self.pool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return bytes.NewBuffer(make([]byte, 0, PoolSizeBuffer))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (Self *bufferPool) Get() *bytes.Buffer {
|
||||
return Self.pool.Get().(*bytes.Buffer)
|
||||
}
|
||||
|
||||
func (Self *bufferPool) Put(x *bytes.Buffer) {
|
||||
x.Reset()
|
||||
Self.pool.Put(x)
|
||||
}
|
||||
|
||||
type muxPackagerPool struct {
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
func (Self *muxPackagerPool) New() {
|
||||
Self.pool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
pack := MuxPackager{}
|
||||
return &pack
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (Self *muxPackagerPool) Get() *MuxPackager {
|
||||
return Self.pool.Get().(*MuxPackager)
|
||||
}
|
||||
|
||||
func (Self *muxPackagerPool) Put(pack *MuxPackager) {
|
||||
Self.pool.Put(pack)
|
||||
}
|
||||
|
||||
type ListElement struct {
|
||||
Buf []byte
|
||||
L uint16
|
||||
Part bool
|
||||
}
|
||||
|
||||
type listElementPool struct {
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
func (Self *listElementPool) New() {
|
||||
Self.pool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
element := ListElement{}
|
||||
return &element
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (Self *listElementPool) Get() *ListElement {
|
||||
return Self.pool.Get().(*ListElement)
|
||||
}
|
||||
|
||||
func (Self *listElementPool) Put(element *ListElement) {
|
||||
element.L = 0
|
||||
element.Buf = nil
|
||||
element.Part = false
|
||||
Self.pool.Put(element)
|
||||
}
|
||||
|
||||
var once = sync.Once{}
|
||||
var BuffPool = bufferPool{}
|
||||
var CopyBuff = copyBufferPool{}
|
||||
var MuxPack = muxPackagerPool{}
|
||||
var WindowBuff = windowBufferPool{}
|
||||
var ListElementPool = listElementPool{}
|
||||
|
||||
func newPool() {
|
||||
BuffPool.New()
|
||||
CopyBuff.New()
|
||||
MuxPack.New()
|
||||
WindowBuff.New()
|
||||
ListElementPool.New()
|
||||
}
|
||||
|
||||
func init() {
|
||||
once.Do(newPool)
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
//Get the currently selected configuration file directory
|
||||
//For non-Windows systems, select the /etc/nps as config directory if exist, or select ./
|
||||
//windows system, select the C:\Program Files\nps as config directory if exist, or select ./
|
||||
func GetRunPath() string {
|
||||
var path string
|
||||
if path = GetInstallPath(); !FileExists(path) {
|
||||
return GetAppPath()
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
//Different systems get different installation paths
|
||||
func GetInstallPath() string {
|
||||
var path string
|
||||
if IsWindows() {
|
||||
path = `C:\Program Files\nps`
|
||||
} else {
|
||||
path = "/etc/nps"
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
//Get the absolute path to the running directory
|
||||
func GetAppPath() string {
|
||||
if path, err := filepath.Abs(filepath.Dir(os.Args[0])); err == nil {
|
||||
return path
|
||||
}
|
||||
return os.Args[0]
|
||||
}
|
||||
|
||||
//Determine whether the current system is a Windows system?
|
||||
func IsWindows() bool {
|
||||
if runtime.GOOS == "windows" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//interface log file path
|
||||
func GetLogPath() string {
|
||||
var path string
|
||||
if IsWindows() {
|
||||
path = filepath.Join(GetAppPath(), "nps.log")
|
||||
} else {
|
||||
path = "/var/log/nps.log"
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
//interface npc log file path
|
||||
func GetNpcLogPath() string {
|
||||
var path string
|
||||
if IsWindows() {
|
||||
path = filepath.Join(GetAppPath(), "npc.log")
|
||||
} else {
|
||||
path = "/var/log/npc.log"
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
//interface pid file path
|
||||
func GetTmpPath() string {
|
||||
var path string
|
||||
if IsWindows() {
|
||||
path = GetAppPath()
|
||||
} else {
|
||||
path = "/tmp"
|
||||
}
|
||||
return path
|
||||
}
|
||||
@@ -0,0 +1,461 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"html/template"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
)
|
||||
|
||||
//Get the corresponding IP address through domain name
|
||||
func GetHostByName(hostname string) string {
|
||||
if !DomainCheck(hostname) {
|
||||
return hostname
|
||||
}
|
||||
ips, _ := net.LookupIP(hostname)
|
||||
if ips != nil {
|
||||
for _, v := range ips {
|
||||
if v.To4() != nil {
|
||||
return v.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
//Check the legality of domain
|
||||
func DomainCheck(domain string) bool {
|
||||
var match bool
|
||||
IsLine := "^((http://)|(https://))?([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}(/)"
|
||||
NotLine := "^((http://)|(https://))?([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}"
|
||||
match, _ = regexp.MatchString(IsLine, domain)
|
||||
if !match {
|
||||
match, _ = regexp.MatchString(NotLine, domain)
|
||||
}
|
||||
return match
|
||||
}
|
||||
|
||||
//Check if the Request request is validated
|
||||
func CheckAuth(r *http.Request, user, passwd string) bool {
|
||||
s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
|
||||
if len(s) != 2 {
|
||||
s = strings.SplitN(r.Header.Get("Proxy-Authorization"), " ", 2)
|
||||
if len(s) != 2 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
b, err := base64.StdEncoding.DecodeString(s[1])
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
pair := strings.SplitN(string(b), ":", 2)
|
||||
if len(pair) != 2 {
|
||||
return false
|
||||
}
|
||||
return pair[0] == user && pair[1] == passwd
|
||||
}
|
||||
|
||||
//get bool by str
|
||||
func GetBoolByStr(s string) bool {
|
||||
switch s {
|
||||
case "1", "true":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//get str by bool
|
||||
func GetStrByBool(b bool) string {
|
||||
if b {
|
||||
return "1"
|
||||
}
|
||||
return "0"
|
||||
}
|
||||
|
||||
//int
|
||||
func GetIntNoErrByStr(str string) int {
|
||||
i, _ := strconv.Atoi(strings.TrimSpace(str))
|
||||
return i
|
||||
}
|
||||
|
||||
//Get verify value
|
||||
func Getverifyval(vkey string) string {
|
||||
return crypt.Md5(vkey)
|
||||
}
|
||||
|
||||
//Change headers and host of request
|
||||
func ChangeHostAndHeader(r *http.Request, host string, header string, addr string) {
|
||||
if host != "" {
|
||||
r.Host = host
|
||||
}
|
||||
if header != "" {
|
||||
h := strings.Split(header, "\n")
|
||||
for _, v := range h {
|
||||
hd := strings.Split(v, ":")
|
||||
if len(hd) == 2 {
|
||||
r.Header.Set(hd[0], hd[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
addr = strings.Split(addr, ":")[0]
|
||||
if prior, ok := r.Header["X-Forwarded-For"]; ok {
|
||||
addr = strings.Join(prior, ", ") + ", " + addr
|
||||
}
|
||||
r.Header.Set("X-Forwarded-For", addr)
|
||||
r.Header.Set("X-Real-IP", addr)
|
||||
}
|
||||
|
||||
//Read file content by file path
|
||||
func ReadAllFromFile(filePath string) ([]byte, error) {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
return ioutil.ReadAll(f)
|
||||
}
|
||||
|
||||
// FileExists reports whether the named file or directory exists.
|
||||
func FileExists(name string) bool {
|
||||
if _, err := os.Stat(name); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
//Judge whether the TCP port can open normally
|
||||
func TestTcpPort(port int) bool {
|
||||
l, err := net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP("0.0.0.0"), port, ""})
|
||||
defer func() {
|
||||
if l != nil {
|
||||
l.Close()
|
||||
}
|
||||
}()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
//Judge whether the UDP port can open normally
|
||||
func TestUdpPort(port int) bool {
|
||||
l, err := net.ListenUDP("udp", &net.UDPAddr{net.ParseIP("0.0.0.0"), port, ""})
|
||||
defer func() {
|
||||
if l != nil {
|
||||
l.Close()
|
||||
}
|
||||
}()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
//Write length and individual byte data
|
||||
//Length prevents sticking
|
||||
//# Characters are used to separate data
|
||||
func BinaryWrite(raw *bytes.Buffer, v ...string) {
|
||||
b := GetWriteStr(v...)
|
||||
binary.Write(raw, binary.LittleEndian, int32(len(b)))
|
||||
binary.Write(raw, binary.LittleEndian, b)
|
||||
}
|
||||
|
||||
// get seq str
|
||||
func GetWriteStr(v ...string) []byte {
|
||||
buffer := new(bytes.Buffer)
|
||||
var l int32
|
||||
for _, v := range v {
|
||||
l += int32(len([]byte(v))) + int32(len([]byte(CONN_DATA_SEQ)))
|
||||
binary.Write(buffer, binary.LittleEndian, []byte(v))
|
||||
binary.Write(buffer, binary.LittleEndian, []byte(CONN_DATA_SEQ))
|
||||
}
|
||||
return buffer.Bytes()
|
||||
}
|
||||
|
||||
//inArray str interface
|
||||
func InStrArr(arr []string, val string) bool {
|
||||
for _, v := range arr {
|
||||
if v == val {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//inArray int interface
|
||||
func InIntArr(arr []int, val int) bool {
|
||||
for _, v := range arr {
|
||||
if v == val {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//format ports str to a int array
|
||||
func GetPorts(p string) []int {
|
||||
var ps []int
|
||||
arr := strings.Split(p, ",")
|
||||
for _, v := range arr {
|
||||
fw := strings.Split(v, "-")
|
||||
if len(fw) == 2 {
|
||||
if IsPort(fw[0]) && IsPort(fw[1]) {
|
||||
start, _ := strconv.Atoi(fw[0])
|
||||
end, _ := strconv.Atoi(fw[1])
|
||||
for i := start; i <= end; i++ {
|
||||
ps = append(ps, i)
|
||||
}
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
} else if IsPort(v) {
|
||||
p, _ := strconv.Atoi(v)
|
||||
ps = append(ps, p)
|
||||
}
|
||||
}
|
||||
return ps
|
||||
}
|
||||
|
||||
//is the string a port
|
||||
func IsPort(p string) bool {
|
||||
pi, err := strconv.Atoi(p)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if pi > 65536 || pi < 1 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
//if the s is just a port,return 127.0.0.1:s
|
||||
func FormatAddress(s string) string {
|
||||
if strings.Contains(s, ":") {
|
||||
return s
|
||||
}
|
||||
return "127.0.0.1:" + s
|
||||
}
|
||||
|
||||
//get address from the complete address
|
||||
func GetIpByAddr(addr string) string {
|
||||
arr := strings.Split(addr, ":")
|
||||
return arr[0]
|
||||
}
|
||||
|
||||
//get port from the complete address
|
||||
func GetPortByAddr(addr string) int {
|
||||
arr := strings.Split(addr, ":")
|
||||
if len(arr) < 2 {
|
||||
return 0
|
||||
}
|
||||
p, err := strconv.Atoi(arr[1])
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func CopyBuffer(dst io.Writer, src io.Reader, label ...string) (written int64, err error) {
|
||||
buf := CopyBuff.Get()
|
||||
defer CopyBuff.Put(buf)
|
||||
for {
|
||||
nr, er := src.Read(buf)
|
||||
//if len(pr)>0 && pr[0] && nr > 50 {
|
||||
// logs.Warn(string(buf[:50]))
|
||||
//}
|
||||
if nr > 0 {
|
||||
nw, ew := dst.Write(buf[0:nr])
|
||||
if nw > 0 {
|
||||
written += int64(nw)
|
||||
}
|
||||
if ew != nil {
|
||||
err = ew
|
||||
break
|
||||
}
|
||||
if nr != nw {
|
||||
err = io.ErrShortWrite
|
||||
break
|
||||
}
|
||||
}
|
||||
if er != nil {
|
||||
err = er
|
||||
break
|
||||
}
|
||||
}
|
||||
return written, err
|
||||
}
|
||||
|
||||
//send this ip forget to get a local udp port
|
||||
func GetLocalUdpAddr() (net.Conn, error) {
|
||||
tmpConn, err := net.Dial("udp", "114.114.114.114:53")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tmpConn, tmpConn.Close()
|
||||
}
|
||||
|
||||
//parse template
|
||||
func ParseStr(str string) (string, error) {
|
||||
tmp := template.New("npc")
|
||||
var err error
|
||||
w := new(bytes.Buffer)
|
||||
if tmp, err = tmp.Parse(str); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err = tmp.Execute(w, GetEnvMap()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return w.String(), nil
|
||||
}
|
||||
|
||||
//get env
|
||||
func GetEnvMap() map[string]string {
|
||||
m := make(map[string]string)
|
||||
environ := os.Environ()
|
||||
for i := range environ {
|
||||
tmp := strings.Split(environ[i], "=")
|
||||
if len(tmp) == 2 {
|
||||
m[tmp[0]] = tmp[1]
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
//throw the empty element of the string array
|
||||
func TrimArr(arr []string) []string {
|
||||
newArr := make([]string, 0)
|
||||
for _, v := range arr {
|
||||
if v != "" {
|
||||
newArr = append(newArr, v)
|
||||
}
|
||||
}
|
||||
return newArr
|
||||
}
|
||||
|
||||
//
|
||||
func IsArrContains(arr []string, val string) bool {
|
||||
if arr == nil {
|
||||
return false
|
||||
}
|
||||
for _, v := range arr {
|
||||
if v == val {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//remove value from string array
|
||||
func RemoveArrVal(arr []string, val string) []string {
|
||||
for k, v := range arr {
|
||||
if v == val {
|
||||
arr = append(arr[:k], arr[k+1:]...)
|
||||
return arr
|
||||
}
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
//convert bytes to num
|
||||
func BytesToNum(b []byte) int {
|
||||
var str string
|
||||
for i := 0; i < len(b); i++ {
|
||||
str += strconv.Itoa(int(b[i]))
|
||||
}
|
||||
x, _ := strconv.Atoi(str)
|
||||
return int(x)
|
||||
}
|
||||
|
||||
//get the length of the sync map
|
||||
func GeSynctMapLen(m sync.Map) int {
|
||||
var c int
|
||||
m.Range(func(key, value interface{}) bool {
|
||||
c++
|
||||
return true
|
||||
})
|
||||
return c
|
||||
}
|
||||
|
||||
func GetExtFromPath(path string) string {
|
||||
s := strings.Split(path, ".")
|
||||
re, err := regexp.Compile(`(\w+)`)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(re.Find([]byte(s[0])))
|
||||
}
|
||||
|
||||
var externalIp string
|
||||
|
||||
func GetExternalIp() string {
|
||||
if externalIp != "" {
|
||||
return externalIp
|
||||
}
|
||||
resp, err := http.Get("http://myexternalip.com/raw")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
content, _ := ioutil.ReadAll(resp.Body)
|
||||
externalIp = string(content)
|
||||
return externalIp
|
||||
}
|
||||
|
||||
func GetIntranetIp() (error, string) {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return nil, ""
|
||||
}
|
||||
for _, address := range addrs {
|
||||
// 检查ip地址判断是否回环地址
|
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
return nil, ipnet.IP.To4().String()
|
||||
}
|
||||
}
|
||||
}
|
||||
return errors.New("get intranet ip error"), ""
|
||||
}
|
||||
|
||||
func IsPublicIP(IP net.IP) bool {
|
||||
if IP.IsLoopback() || IP.IsLinkLocalMulticast() || IP.IsLinkLocalUnicast() {
|
||||
return false
|
||||
}
|
||||
if ip4 := IP.To4(); ip4 != nil {
|
||||
switch true {
|
||||
case ip4[0] == 10:
|
||||
return false
|
||||
case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31:
|
||||
return false
|
||||
case ip4[0] == 192 && ip4[1] == 168:
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func GetServerIpByClientIp(clientIp net.IP) string {
|
||||
if IsPublicIP(clientIp) {
|
||||
return GetExternalIp()
|
||||
}
|
||||
_, ip := GetIntranetIp()
|
||||
return ip
|
||||
}
|
||||
@@ -0,0 +1,324 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
)
|
||||
|
||||
type CommonConfig struct {
|
||||
Server string
|
||||
VKey string
|
||||
Tp string //bridgeType kcp or tcp
|
||||
AutoReconnection bool
|
||||
ProxyUrl string
|
||||
Client *file.Client
|
||||
}
|
||||
|
||||
type LocalServer struct {
|
||||
Type string
|
||||
Port int
|
||||
Ip string
|
||||
Password string
|
||||
Target string
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
content string
|
||||
title []string
|
||||
CommonConfig *CommonConfig
|
||||
Hosts []*file.Host
|
||||
Tasks []*file.Tunnel
|
||||
Healths []*file.Health
|
||||
LocalServer []*LocalServer
|
||||
}
|
||||
|
||||
func NewConfig(path string) (c *Config, err error) {
|
||||
c = new(Config)
|
||||
var b []byte
|
||||
if b, err = common.ReadAllFromFile(path); err != nil {
|
||||
return
|
||||
} else {
|
||||
if c.content, err = common.ParseStr(string(b)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.title, err = getAllTitle(c.content); err != nil {
|
||||
return
|
||||
}
|
||||
var nowIndex int
|
||||
var nextIndex int
|
||||
var nowContent string
|
||||
for i := 0; i < len(c.title); i++ {
|
||||
nowIndex = strings.Index(c.content, c.title[i]) + len(c.title[i])
|
||||
if i < len(c.title)-1 {
|
||||
nextIndex = strings.Index(c.content, c.title[i+1])
|
||||
} else {
|
||||
nextIndex = len(c.content)
|
||||
}
|
||||
nowContent = c.content[nowIndex:nextIndex]
|
||||
|
||||
if strings.Index(getTitleContent(c.title[i]), "secret") == 0 && !strings.Contains(nowContent, "mode") {
|
||||
local := delLocalService(nowContent)
|
||||
local.Type = "secret"
|
||||
c.LocalServer = append(c.LocalServer, local)
|
||||
continue
|
||||
}
|
||||
//except mode
|
||||
if strings.Index(getTitleContent(c.title[i]), "p2p") == 0 && !strings.Contains(nowContent, "mode") {
|
||||
local := delLocalService(nowContent)
|
||||
local.Type = "p2p"
|
||||
c.LocalServer = append(c.LocalServer, local)
|
||||
continue
|
||||
}
|
||||
//health set
|
||||
if strings.Index(getTitleContent(c.title[i]), "health") == 0 {
|
||||
c.Healths = append(c.Healths, dealHealth(nowContent))
|
||||
continue
|
||||
}
|
||||
switch c.title[i] {
|
||||
case "[common]":
|
||||
c.CommonConfig = dealCommon(nowContent)
|
||||
default:
|
||||
if strings.Index(nowContent, "host") > -1 {
|
||||
h := dealHost(nowContent)
|
||||
h.Remark = getTitleContent(c.title[i])
|
||||
c.Hosts = append(c.Hosts, h)
|
||||
} else {
|
||||
t := dealTunnel(nowContent)
|
||||
t.Remark = getTitleContent(c.title[i])
|
||||
c.Tasks = append(c.Tasks, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func getTitleContent(s string) string {
|
||||
re, _ := regexp.Compile(`[\[\]]`)
|
||||
return re.ReplaceAllString(s, "")
|
||||
}
|
||||
|
||||
func dealCommon(s string) *CommonConfig {
|
||||
c := &CommonConfig{}
|
||||
c.Client = file.NewClient("", true, true)
|
||||
c.Client.Cnf = new(file.Config)
|
||||
for _, v := range splitStr(s) {
|
||||
item := strings.Split(v, "=")
|
||||
if len(item) == 0 {
|
||||
continue
|
||||
} else if len(item) == 1 {
|
||||
item = append(item, "")
|
||||
}
|
||||
switch item[0] {
|
||||
case "server_addr":
|
||||
c.Server = item[1]
|
||||
case "vkey":
|
||||
c.VKey = item[1]
|
||||
case "conn_type":
|
||||
c.Tp = item[1]
|
||||
case "auto_reconnection":
|
||||
c.AutoReconnection = common.GetBoolByStr(item[1])
|
||||
case "basic_username":
|
||||
c.Client.Cnf.U = item[1]
|
||||
case "basic_password":
|
||||
c.Client.Cnf.P = item[1]
|
||||
case "web_password":
|
||||
c.Client.WebPassword = item[1]
|
||||
case "web_username":
|
||||
c.Client.WebUserName = item[1]
|
||||
case "compress":
|
||||
c.Client.Cnf.Compress = common.GetBoolByStr(item[1])
|
||||
case "crypt":
|
||||
c.Client.Cnf.Crypt = common.GetBoolByStr(item[1])
|
||||
case "proxy_url":
|
||||
c.ProxyUrl = item[1]
|
||||
case "rate_limit":
|
||||
c.Client.RateLimit = common.GetIntNoErrByStr(item[1])
|
||||
case "flow_limit":
|
||||
c.Client.Flow.FlowLimit = int64(common.GetIntNoErrByStr(item[1]))
|
||||
case "max_conn":
|
||||
c.Client.MaxConn = common.GetIntNoErrByStr(item[1])
|
||||
case "remark":
|
||||
c.Client.Remark = item[1]
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func dealHost(s string) *file.Host {
|
||||
h := &file.Host{}
|
||||
h.Target = new(file.Target)
|
||||
h.Scheme = "all"
|
||||
var headerChange string
|
||||
for _, v := range splitStr(s) {
|
||||
item := strings.Split(v, "=")
|
||||
if len(item) == 0 {
|
||||
continue
|
||||
} else if len(item) == 1 {
|
||||
item = append(item, "")
|
||||
}
|
||||
switch strings.TrimSpace(item[0]) {
|
||||
case "host":
|
||||
h.Host = item[1]
|
||||
case "target_addr":
|
||||
h.Target.TargetStr = strings.Replace(item[1], ",", "\n", -1)
|
||||
case "host_change":
|
||||
h.HostChange = item[1]
|
||||
case "scheme":
|
||||
h.Scheme = item[1]
|
||||
case "location":
|
||||
h.Location = item[1]
|
||||
default:
|
||||
if strings.Contains(item[0], "header") {
|
||||
headerChange += strings.Replace(item[0], "header_", "", -1) + ":" + item[1] + "\n"
|
||||
}
|
||||
h.HeaderChange = headerChange
|
||||
}
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func dealHealth(s string) *file.Health {
|
||||
h := &file.Health{}
|
||||
for _, v := range splitStr(s) {
|
||||
item := strings.Split(v, "=")
|
||||
if len(item) == 0 {
|
||||
continue
|
||||
} else if len(item) == 1 {
|
||||
item = append(item, "")
|
||||
}
|
||||
switch strings.TrimSpace(item[0]) {
|
||||
case "health_check_timeout":
|
||||
h.HealthCheckTimeout = common.GetIntNoErrByStr(item[1])
|
||||
case "health_check_max_failed":
|
||||
h.HealthMaxFail = common.GetIntNoErrByStr(item[1])
|
||||
case "health_check_interval":
|
||||
h.HealthCheckInterval = common.GetIntNoErrByStr(item[1])
|
||||
case "health_http_url":
|
||||
h.HttpHealthUrl = item[1]
|
||||
case "health_check_type":
|
||||
h.HealthCheckType = item[1]
|
||||
case "health_check_target":
|
||||
h.HealthCheckTarget = item[1]
|
||||
}
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func dealTunnel(s string) *file.Tunnel {
|
||||
t := &file.Tunnel{}
|
||||
t.Target = new(file.Target)
|
||||
for _, v := range splitStr(s) {
|
||||
item := strings.Split(v, "=")
|
||||
if len(item) == 0 {
|
||||
continue
|
||||
} else if len(item) == 1 {
|
||||
item = append(item, "")
|
||||
}
|
||||
switch strings.TrimSpace(item[0]) {
|
||||
case "server_port":
|
||||
t.Ports = item[1]
|
||||
case "server_ip":
|
||||
t.ServerIp = item[1]
|
||||
case "mode":
|
||||
t.Mode = item[1]
|
||||
case "target_addr":
|
||||
t.Target.TargetStr = strings.Replace(item[1], ",", "\n", -1)
|
||||
case "target_port":
|
||||
t.Target.TargetStr = item[1]
|
||||
case "target_ip":
|
||||
t.TargetAddr = item[1]
|
||||
case "password":
|
||||
t.Password = item[1]
|
||||
case "local_path":
|
||||
t.LocalPath = item[1]
|
||||
case "strip_pre":
|
||||
t.StripPre = item[1]
|
||||
case "multi_account":
|
||||
t.MultiAccount = &file.MultiAccount{}
|
||||
if common.FileExists(item[1]){
|
||||
if b, err := common.ReadAllFromFile(item[1]); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
if content, err := common.ParseStr(string(b)); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
t.MultiAccount.AccountMap = dealMultiUser(content)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return t
|
||||
|
||||
}
|
||||
|
||||
func dealMultiUser(s string) map[string]string {
|
||||
multiUserMap := make(map[string]string)
|
||||
for _, v := range splitStr(s) {
|
||||
item := strings.Split(v, "=")
|
||||
if len(item) == 0 {
|
||||
continue
|
||||
} else if len(item) == 1 {
|
||||
item = append(item, "")
|
||||
}
|
||||
multiUserMap[strings.TrimSpace(item[0])] = item[1]
|
||||
}
|
||||
return multiUserMap
|
||||
}
|
||||
|
||||
func delLocalService(s string) *LocalServer {
|
||||
l := new(LocalServer)
|
||||
for _, v := range splitStr(s) {
|
||||
item := strings.Split(v, "=")
|
||||
if len(item) == 0 {
|
||||
continue
|
||||
} else if len(item) == 1 {
|
||||
item = append(item, "")
|
||||
}
|
||||
switch item[0] {
|
||||
case "local_port":
|
||||
l.Port = common.GetIntNoErrByStr(item[1])
|
||||
case "local_ip":
|
||||
l.Ip = item[1]
|
||||
case "password":
|
||||
l.Password = item[1]
|
||||
case "target_addr":
|
||||
l.Target = item[1]
|
||||
}
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func getAllTitle(content string) (arr []string, err error) {
|
||||
var re *regexp.Regexp
|
||||
re, err = regexp.Compile(`\[.+?\]`)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
arr = re.FindAllString(content, -1)
|
||||
m := make(map[string]bool)
|
||||
for _, v := range arr {
|
||||
if _, ok := m[v]; ok {
|
||||
err = errors.New(fmt.Sprintf("Item names %s are not allowed to be duplicated", v))
|
||||
return
|
||||
}
|
||||
m[v] = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func splitStr(s string) (configDataArr []string) {
|
||||
if common.IsWindows() {
|
||||
configDataArr = strings.Split(s, "\r\n")
|
||||
}
|
||||
if len(configDataArr) < 3 {
|
||||
configDataArr = strings.Split(s, "\n")
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReg(t *testing.T) {
|
||||
content := `
|
||||
[common]
|
||||
server=127.0.0.1:8284
|
||||
tp=tcp
|
||||
vkey=123
|
||||
[web2]
|
||||
host=www.baidu.com
|
||||
host_change=www.sina.com
|
||||
target=127.0.0.1:8080,127.0.0.1:8082
|
||||
header_cookkile=122123
|
||||
header_user-Agent=122123
|
||||
[web2]
|
||||
host=www.baidu.com
|
||||
host_change=www.sina.com
|
||||
target=127.0.0.1:8080,127.0.0.1:8082
|
||||
header_cookkile="122123"
|
||||
header_user-Agent=122123
|
||||
[tunnel1]
|
||||
type=udp
|
||||
target=127.0.0.1:8080
|
||||
port=9001
|
||||
compress=snappy
|
||||
crypt=true
|
||||
u=1
|
||||
p=2
|
||||
[tunnel2]
|
||||
type=tcp
|
||||
target=127.0.0.1:8080
|
||||
port=9001
|
||||
compress=snappy
|
||||
crypt=true
|
||||
u=1
|
||||
p=2
|
||||
`
|
||||
re, err := regexp.Compile(`\[.+?\]`)
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
log.Println(re.FindAllString(content, -1))
|
||||
}
|
||||
|
||||
func TestDealCommon(t *testing.T) {
|
||||
s := `server=127.0.0.1:8284
|
||||
tp=tcp
|
||||
vkey=123`
|
||||
f := new(CommonConfig)
|
||||
f.Server = "127.0.0.1:8284"
|
||||
f.Tp = "tcp"
|
||||
f.VKey = "123"
|
||||
if c := dealCommon(s); *c != *f {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTitleContent(t *testing.T) {
|
||||
s := "[common]"
|
||||
if getTitleContent(s) != "common" {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
@@ -1,352 +0,0 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"github.com/golang/snappy"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CryptConn struct {
|
||||
conn net.Conn
|
||||
crypt bool
|
||||
}
|
||||
|
||||
func NewCryptConn(conn net.Conn, crypt bool) *CryptConn {
|
||||
c := new(CryptConn)
|
||||
c.conn = conn
|
||||
c.crypt = crypt
|
||||
return c
|
||||
}
|
||||
|
||||
//加密写
|
||||
func (s *CryptConn) Write(b []byte) (n int, err error) {
|
||||
n = len(b)
|
||||
if s.crypt {
|
||||
if b, err = AesEncrypt(b, []byte(cryptKey)); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if b, err = GetLenBytes(b); err != nil {
|
||||
return
|
||||
}
|
||||
_, err = s.conn.Write(b)
|
||||
return
|
||||
}
|
||||
|
||||
//解密读
|
||||
func (s *CryptConn) Read(b []byte) (n int, err error) {
|
||||
defer func() {
|
||||
if err == nil && n == len(IO_EOF) && string(b[:n]) == IO_EOF {
|
||||
err = io.EOF
|
||||
n = 0
|
||||
}
|
||||
}()
|
||||
var lens int
|
||||
var buf, bs []byte
|
||||
c := NewConn(s.conn)
|
||||
if lens, err = c.GetLen(); err != nil {
|
||||
return
|
||||
}
|
||||
if buf, err = c.ReadLen(lens); err != nil {
|
||||
return
|
||||
}
|
||||
if s.crypt {
|
||||
if bs, err = AesDecrypt(buf, []byte(cryptKey)); err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
bs = buf
|
||||
}
|
||||
n = len(bs)
|
||||
copy(b, bs)
|
||||
return
|
||||
}
|
||||
|
||||
type SnappyConn struct {
|
||||
w *snappy.Writer
|
||||
r *snappy.Reader
|
||||
crypt bool
|
||||
}
|
||||
|
||||
func NewSnappyConn(conn net.Conn, crypt bool) *SnappyConn {
|
||||
c := new(SnappyConn)
|
||||
c.w = snappy.NewBufferedWriter(conn)
|
||||
c.r = snappy.NewReader(conn)
|
||||
c.crypt = crypt
|
||||
return c
|
||||
}
|
||||
|
||||
//snappy压缩写 包含加密
|
||||
func (s *SnappyConn) Write(b []byte) (n int, err error) {
|
||||
n = len(b)
|
||||
if s.crypt {
|
||||
if b, err = AesEncrypt(b, []byte(cryptKey)); err != nil {
|
||||
log.Println("encode crypt error:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if _, err = s.w.Write(b); err != nil {
|
||||
return
|
||||
}
|
||||
err = s.w.Flush()
|
||||
return
|
||||
}
|
||||
|
||||
//snappy压缩读 包含解密
|
||||
func (s *SnappyConn) Read(b []byte) (n int, err error) {
|
||||
defer func() {
|
||||
if err == nil && n == len(IO_EOF) && string(b[:n]) == IO_EOF {
|
||||
err = io.EOF
|
||||
n = 0
|
||||
}
|
||||
}()
|
||||
if n, err = s.r.Read(b); err != nil {
|
||||
return
|
||||
}
|
||||
if s.crypt {
|
||||
var bs []byte
|
||||
if bs, err = AesDecrypt(b[:n], []byte(cryptKey)); err != nil {
|
||||
log.Println("decode crypt error:", err)
|
||||
return
|
||||
}
|
||||
n = len(bs)
|
||||
copy(b, bs)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type Conn struct {
|
||||
conn net.Conn
|
||||
}
|
||||
|
||||
//new conn
|
||||
func NewConn(conn net.Conn) *Conn {
|
||||
c := new(Conn)
|
||||
c.conn = conn
|
||||
return c
|
||||
}
|
||||
|
||||
//读取指定长度内容
|
||||
func (s *Conn) ReadLen(cLen int) ([]byte, error) {
|
||||
if cLen > 65535 {
|
||||
return nil, errors.New("长度错误")
|
||||
}
|
||||
buf := bufPool.Get().([]byte)[:cLen]
|
||||
if n, err := io.ReadFull(s, buf); err != nil || n != cLen {
|
||||
return buf, errors.New("读取指定长度错误" + err.Error())
|
||||
}
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
//获取长度
|
||||
func (s *Conn) GetLen() (int, error) {
|
||||
val, err := s.ReadLen(4)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return GetLenByBytes(val)
|
||||
}
|
||||
|
||||
//写入长度+内容 粘包
|
||||
func (s *Conn) WriteLen(buf []byte) (int, error) {
|
||||
var b []byte
|
||||
if b, err = GetLenBytes(buf); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return s.Write(b)
|
||||
}
|
||||
|
||||
//读取flag
|
||||
func (s *Conn) ReadFlag() (string, error) {
|
||||
val, err := s.ReadLen(4)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(val), err
|
||||
}
|
||||
|
||||
//读取host 连接地址 压缩类型
|
||||
func (s *Conn) GetHostFromConn() (typeStr string, host string, en, de int, crypt, mux bool, err error) {
|
||||
retry:
|
||||
lType, err := s.ReadLen(3)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if typeStr = string(lType); typeStr == TEST_FLAG {
|
||||
en, de, crypt, mux = s.GetConnInfoFromConn()
|
||||
goto retry
|
||||
}
|
||||
cLen, err := s.GetLen()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
hostByte, err := s.ReadLen(cLen)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
host = string(hostByte)
|
||||
return
|
||||
}
|
||||
|
||||
//写连接类型 和 host地址
|
||||
func (s *Conn) WriteHost(ltype string, host string) (int, error) {
|
||||
raw := bytes.NewBuffer([]byte{})
|
||||
binary.Write(raw, binary.LittleEndian, []byte(ltype))
|
||||
binary.Write(raw, binary.LittleEndian, int32(len([]byte(host))))
|
||||
binary.Write(raw, binary.LittleEndian, []byte(host))
|
||||
return s.Write(raw.Bytes())
|
||||
}
|
||||
|
||||
//设置连接为长连接
|
||||
func (s *Conn) SetAlive() {
|
||||
conn := s.conn.(*net.TCPConn)
|
||||
conn.SetReadDeadline(time.Time{})
|
||||
conn.SetKeepAlive(true)
|
||||
conn.SetKeepAlivePeriod(time.Duration(2 * time.Second))
|
||||
}
|
||||
|
||||
//从tcp报文中解析出host,连接类型等
|
||||
func (s *Conn) GetHost() (method, address string, rb []byte, err error, r *http.Request) {
|
||||
var b [32 * 1024]byte
|
||||
var n int
|
||||
if n, err = s.Read(b[:]); err != nil {
|
||||
return
|
||||
}
|
||||
rb = b[:n]
|
||||
r, err = http.ReadRequest(bufio.NewReader(bytes.NewReader(rb)))
|
||||
if err != nil {
|
||||
log.Println("解析host出错:", err)
|
||||
return
|
||||
}
|
||||
hostPortURL, err := url.Parse(r.Host)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if hostPortURL.Opaque == "443" { //https访问
|
||||
address = r.Host + ":443"
|
||||
} else { //http访问
|
||||
if strings.Index(hostPortURL.Host, ":") == -1 { //host不带端口, 默认80
|
||||
address = r.Host + ":80"
|
||||
} else {
|
||||
address = r.Host
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//单独读(加密|压缩)
|
||||
func (s *Conn) ReadFrom(b []byte, compress int, crypt bool) (int, error) {
|
||||
if COMPRESS_SNAPY_DECODE == compress {
|
||||
return NewSnappyConn(s.conn, crypt).Read(b)
|
||||
}
|
||||
return NewCryptConn(s.conn, crypt).Read(b)
|
||||
}
|
||||
|
||||
//单独写(加密|压缩)
|
||||
func (s *Conn) WriteTo(b []byte, compress int, crypt bool) (n int, err error) {
|
||||
if COMPRESS_SNAPY_ENCODE == compress {
|
||||
return NewSnappyConn(s.conn, crypt).Write(b)
|
||||
}
|
||||
return NewCryptConn(s.conn, crypt).Write(b)
|
||||
}
|
||||
|
||||
//写压缩方式,加密
|
||||
func (s *Conn) WriteConnInfo(en, de int, crypt, mux bool) {
|
||||
s.Write([]byte(strconv.Itoa(en) + strconv.Itoa(de) + GetStrByBool(crypt) + GetStrByBool(mux)))
|
||||
}
|
||||
|
||||
//获取压缩方式,是否加密
|
||||
func (s *Conn) GetConnInfoFromConn() (en, de int, crypt, mux bool) {
|
||||
buf, err := s.ReadLen(4)
|
||||
//TODO:错误处理
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
en, _ = strconv.Atoi(string(buf[0]))
|
||||
de, _ = strconv.Atoi(string(buf[1]))
|
||||
crypt = GetBoolByStr(string(buf[2]))
|
||||
mux = GetBoolByStr(string(buf[3]))
|
||||
return
|
||||
}
|
||||
|
||||
//close
|
||||
func (s *Conn) Close() error {
|
||||
return s.conn.Close()
|
||||
}
|
||||
|
||||
//write
|
||||
func (s *Conn) Write(b []byte) (int, error) {
|
||||
return s.conn.Write(b)
|
||||
}
|
||||
|
||||
//read
|
||||
func (s *Conn) Read(b []byte) (int, error) {
|
||||
return s.conn.Read(b)
|
||||
}
|
||||
|
||||
//write error
|
||||
func (s *Conn) wError() (int, error) {
|
||||
return s.Write([]byte(RES_MSG))
|
||||
}
|
||||
|
||||
//write sign flag
|
||||
func (s *Conn) wSign() (int, error) {
|
||||
return s.Write([]byte(RES_SIGN))
|
||||
}
|
||||
|
||||
//write main
|
||||
func (s *Conn) wMain() (int, error) {
|
||||
return s.Write([]byte(WORK_MAIN))
|
||||
}
|
||||
|
||||
//write chan
|
||||
func (s *Conn) wChan() (int, error) {
|
||||
return s.Write([]byte(WORK_CHAN))
|
||||
}
|
||||
|
||||
//write test
|
||||
func (s *Conn) wTest() (int, error) {
|
||||
return s.Write([]byte(TEST_FLAG))
|
||||
}
|
||||
|
||||
//write test
|
||||
func (s *Conn) wSuccess() (int, error) {
|
||||
return s.Write([]byte(CONN_SUCCESS))
|
||||
}
|
||||
|
||||
//write test
|
||||
func (s *Conn) wFail() (int, error) {
|
||||
return s.Write([]byte(CONN_ERROR))
|
||||
}
|
||||
|
||||
//获取长度+内容
|
||||
func GetLenBytes(buf []byte) (b []byte, err error) {
|
||||
raw := bytes.NewBuffer([]byte{})
|
||||
if err = binary.Write(raw, binary.LittleEndian, int32(len(buf))); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(raw, binary.LittleEndian, buf); err != nil {
|
||||
return
|
||||
}
|
||||
b = raw.Bytes()
|
||||
return
|
||||
}
|
||||
|
||||
//解析出长度
|
||||
func GetLenByBytes(buf []byte) (int, error) {
|
||||
nlen := binary.LittleEndian.Uint32(buf)
|
||||
if nlen <= 0 {
|
||||
return 0, errors.New("数据长度错误")
|
||||
}
|
||||
return int(nlen), nil
|
||||
}
|
||||
@@ -0,0 +1,405 @@
|
||||
package conn
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/lib/goroutine"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"github.com/cnlh/nps/lib/mux"
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
"github.com/xtaci/kcp-go"
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
Conn net.Conn
|
||||
Rb []byte
|
||||
}
|
||||
|
||||
//new conn
|
||||
func NewConn(conn net.Conn) *Conn {
|
||||
return &Conn{Conn: conn}
|
||||
}
|
||||
|
||||
//get host 、connection type、method...from connection
|
||||
func (s *Conn) GetHost() (method, address string, rb []byte, err error, r *http.Request) {
|
||||
var b [32 * 1024]byte
|
||||
var n int
|
||||
if n, err = s.Read(b[:]); err != nil {
|
||||
return
|
||||
}
|
||||
rb = b[:n]
|
||||
r, err = http.ReadRequest(bufio.NewReader(bytes.NewReader(rb)))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
hostPortURL, err := url.Parse(r.Host)
|
||||
if err != nil {
|
||||
address = r.Host
|
||||
err = nil
|
||||
return
|
||||
}
|
||||
if hostPortURL.Opaque == "443" {
|
||||
if strings.Index(r.Host, ":") == -1 {
|
||||
address = r.Host + ":443"
|
||||
} else {
|
||||
address = r.Host
|
||||
}
|
||||
} else {
|
||||
if strings.Index(r.Host, ":") == -1 {
|
||||
address = r.Host + ":80"
|
||||
} else {
|
||||
address = r.Host
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Conn) GetShortLenContent() (b []byte, err error) {
|
||||
var l int
|
||||
if l, err = s.GetLen(); err != nil {
|
||||
return
|
||||
}
|
||||
if l < 0 || l > 32<<10 {
|
||||
err = errors.New("read length error")
|
||||
return
|
||||
}
|
||||
return s.GetShortContent(l)
|
||||
}
|
||||
|
||||
func (s *Conn) GetShortContent(l int) (b []byte, err error) {
|
||||
buf := make([]byte, l)
|
||||
return buf, binary.Read(s, binary.LittleEndian, &buf)
|
||||
}
|
||||
|
||||
//读取指定长度内容
|
||||
func (s *Conn) ReadLen(cLen int, buf []byte) (int, error) {
|
||||
if cLen > len(buf) || cLen <= 0 {
|
||||
return 0, errors.New("长度错误" + strconv.Itoa(cLen))
|
||||
}
|
||||
if n, err := io.ReadFull(s, buf[:cLen]); err != nil || n != cLen {
|
||||
return n, errors.New("Error reading specified length " + err.Error())
|
||||
}
|
||||
return cLen, nil
|
||||
}
|
||||
|
||||
func (s *Conn) GetLen() (int, error) {
|
||||
var l int32
|
||||
err := binary.Read(s, binary.LittleEndian, &l)
|
||||
return int(l), err
|
||||
}
|
||||
|
||||
func (s *Conn) WriteLenContent(buf []byte) (err error) {
|
||||
var b []byte
|
||||
if b, err = GetLenBytes(buf); err != nil {
|
||||
return
|
||||
}
|
||||
return binary.Write(s.Conn, binary.LittleEndian, b)
|
||||
}
|
||||
|
||||
//read flag
|
||||
func (s *Conn) ReadFlag() (string, error) {
|
||||
buf := make([]byte, 4)
|
||||
return string(buf), binary.Read(s, binary.LittleEndian, &buf)
|
||||
}
|
||||
|
||||
//set alive
|
||||
func (s *Conn) SetAlive(tp string) {
|
||||
switch s.Conn.(type) {
|
||||
case *kcp.UDPSession:
|
||||
s.Conn.(*kcp.UDPSession).SetReadDeadline(time.Time{})
|
||||
case *net.TCPConn:
|
||||
conn := s.Conn.(*net.TCPConn)
|
||||
conn.SetReadDeadline(time.Time{})
|
||||
//conn.SetKeepAlive(false)
|
||||
//conn.SetKeepAlivePeriod(time.Duration(2 * time.Second))
|
||||
case *mux.PortConn:
|
||||
s.Conn.(*mux.PortConn).SetReadDeadline(time.Time{})
|
||||
}
|
||||
}
|
||||
|
||||
//set read deadline
|
||||
func (s *Conn) SetReadDeadlineBySecond(t time.Duration) {
|
||||
switch s.Conn.(type) {
|
||||
case *kcp.UDPSession:
|
||||
s.Conn.(*kcp.UDPSession).SetReadDeadline(time.Now().Add(time.Duration(t) * time.Second))
|
||||
case *net.TCPConn:
|
||||
s.Conn.(*net.TCPConn).SetReadDeadline(time.Now().Add(time.Duration(t) * time.Second))
|
||||
case *mux.PortConn:
|
||||
s.Conn.(*mux.PortConn).SetReadDeadline(time.Now().Add(time.Duration(t) * time.Second))
|
||||
}
|
||||
}
|
||||
|
||||
//get link info from conn
|
||||
func (s *Conn) GetLinkInfo() (lk *Link, err error) {
|
||||
err = s.getInfo(&lk)
|
||||
return
|
||||
}
|
||||
|
||||
//send info for link
|
||||
func (s *Conn) SendHealthInfo(info, status string) (int, error) {
|
||||
raw := bytes.NewBuffer([]byte{})
|
||||
common.BinaryWrite(raw, info, status)
|
||||
return s.Write(raw.Bytes())
|
||||
}
|
||||
|
||||
//get health info from conn
|
||||
func (s *Conn) GetHealthInfo() (info string, status bool, err error) {
|
||||
var l int
|
||||
buf := common.BufPoolMax.Get().([]byte)
|
||||
defer common.PutBufPoolMax(buf)
|
||||
if l, err = s.GetLen(); err != nil {
|
||||
return
|
||||
} else if _, err = s.ReadLen(l, buf); err != nil {
|
||||
return
|
||||
} else {
|
||||
arr := strings.Split(string(buf[:l]), common.CONN_DATA_SEQ)
|
||||
if len(arr) >= 2 {
|
||||
return arr[0], common.GetBoolByStr(arr[1]), nil
|
||||
}
|
||||
}
|
||||
return "", false, errors.New("receive health info error")
|
||||
}
|
||||
|
||||
//get task info
|
||||
func (s *Conn) GetHostInfo() (h *file.Host, err error) {
|
||||
err = s.getInfo(&h)
|
||||
h.Id = int(file.GetDb().JsonDb.GetHostId())
|
||||
h.Flow = new(file.Flow)
|
||||
h.NoStore = true
|
||||
return
|
||||
}
|
||||
|
||||
//get task info
|
||||
func (s *Conn) GetConfigInfo() (c *file.Client, err error) {
|
||||
err = s.getInfo(&c)
|
||||
c.NoStore = true
|
||||
c.Status = true
|
||||
if c.Flow == nil {
|
||||
c.Flow = new(file.Flow)
|
||||
}
|
||||
c.NoDisplay = false
|
||||
return
|
||||
}
|
||||
|
||||
//get task info
|
||||
func (s *Conn) GetTaskInfo() (t *file.Tunnel, err error) {
|
||||
err = s.getInfo(&t)
|
||||
t.Id = int(file.GetDb().JsonDb.GetTaskId())
|
||||
t.NoStore = true
|
||||
t.Flow = new(file.Flow)
|
||||
return
|
||||
}
|
||||
|
||||
//send info
|
||||
func (s *Conn) SendInfo(t interface{}, flag string) (int, error) {
|
||||
/*
|
||||
The task info is formed as follows:
|
||||
+----+-----+---------+
|
||||
|type| len | content |
|
||||
+----+---------------+
|
||||
| 4 | 4 | ... |
|
||||
+----+---------------+
|
||||
*/
|
||||
raw := bytes.NewBuffer([]byte{})
|
||||
if flag != "" {
|
||||
binary.Write(raw, binary.LittleEndian, []byte(flag))
|
||||
}
|
||||
b, err := json.Marshal(t)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
lenBytes, err := GetLenBytes(b)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
binary.Write(raw, binary.LittleEndian, lenBytes)
|
||||
return s.Write(raw.Bytes())
|
||||
}
|
||||
|
||||
//get task info
|
||||
func (s *Conn) getInfo(t interface{}) (err error) {
|
||||
var l int
|
||||
buf := common.BufPoolMax.Get().([]byte)
|
||||
defer common.PutBufPoolMax(buf)
|
||||
if l, err = s.GetLen(); err != nil {
|
||||
return
|
||||
} else if _, err = s.ReadLen(l, buf); err != nil {
|
||||
return
|
||||
} else {
|
||||
json.Unmarshal(buf[:l], &t)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//close
|
||||
func (s *Conn) Close() error {
|
||||
return s.Conn.Close()
|
||||
}
|
||||
|
||||
//write
|
||||
func (s *Conn) Write(b []byte) (int, error) {
|
||||
return s.Conn.Write(b)
|
||||
}
|
||||
|
||||
//read
|
||||
func (s *Conn) Read(b []byte) (n int, err error) {
|
||||
if s.Rb != nil {
|
||||
//if the rb is not nil ,read rb first
|
||||
if len(s.Rb) > 0 {
|
||||
n = copy(b, s.Rb)
|
||||
s.Rb = s.Rb[n:]
|
||||
return
|
||||
}
|
||||
s.Rb = nil
|
||||
}
|
||||
return s.Conn.Read(b)
|
||||
}
|
||||
|
||||
//write sign flag
|
||||
func (s *Conn) WriteClose() (int, error) {
|
||||
return s.Write([]byte(common.RES_CLOSE))
|
||||
}
|
||||
|
||||
//write main
|
||||
func (s *Conn) WriteMain() (int, error) {
|
||||
return s.Write([]byte(common.WORK_MAIN))
|
||||
}
|
||||
|
||||
//write main
|
||||
func (s *Conn) WriteConfig() (int, error) {
|
||||
return s.Write([]byte(common.WORK_CONFIG))
|
||||
}
|
||||
|
||||
//write chan
|
||||
func (s *Conn) WriteChan() (int, error) {
|
||||
return s.Write([]byte(common.WORK_CHAN))
|
||||
}
|
||||
|
||||
//get task or host result of add
|
||||
func (s *Conn) GetAddStatus() (b bool) {
|
||||
binary.Read(s.Conn, binary.LittleEndian, &b)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Conn) WriteAddOk() error {
|
||||
return binary.Write(s.Conn, binary.LittleEndian, true)
|
||||
}
|
||||
|
||||
func (s *Conn) WriteAddFail() error {
|
||||
defer s.Close()
|
||||
return binary.Write(s.Conn, binary.LittleEndian, false)
|
||||
}
|
||||
|
||||
func (s *Conn) LocalAddr() net.Addr {
|
||||
return s.Conn.LocalAddr()
|
||||
}
|
||||
|
||||
func (s *Conn) RemoteAddr() net.Addr {
|
||||
return s.Conn.RemoteAddr()
|
||||
}
|
||||
|
||||
func (s *Conn) SetDeadline(t time.Time) error {
|
||||
return s.Conn.SetDeadline(t)
|
||||
}
|
||||
|
||||
func (s *Conn) SetWriteDeadline(t time.Time) error {
|
||||
return s.Conn.SetWriteDeadline(t)
|
||||
}
|
||||
|
||||
func (s *Conn) SetReadDeadline(t time.Time) error {
|
||||
return s.Conn.SetReadDeadline(t)
|
||||
}
|
||||
|
||||
//get the assembled amount data(len 4 and content)
|
||||
func GetLenBytes(buf []byte) (b []byte, err error) {
|
||||
raw := bytes.NewBuffer([]byte{})
|
||||
if err = binary.Write(raw, binary.LittleEndian, int32(len(buf))); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(raw, binary.LittleEndian, buf); err != nil {
|
||||
return
|
||||
}
|
||||
b = raw.Bytes()
|
||||
return
|
||||
}
|
||||
|
||||
//udp connection setting
|
||||
func SetUdpSession(sess *kcp.UDPSession) {
|
||||
sess.SetStreamMode(true)
|
||||
sess.SetWindowSize(1024, 1024)
|
||||
sess.SetReadBuffer(64 * 1024)
|
||||
sess.SetWriteBuffer(64 * 1024)
|
||||
sess.SetNoDelay(1, 10, 2, 1)
|
||||
sess.SetMtu(1600)
|
||||
sess.SetACKNoDelay(true)
|
||||
sess.SetWriteDelay(false)
|
||||
}
|
||||
|
||||
//conn1 mux conn
|
||||
func CopyWaitGroup(conn1, conn2 net.Conn, crypt bool, snappy bool, rate *rate.Rate, flow *file.Flow, isServer bool, rb []byte) {
|
||||
//var in, out int64
|
||||
//var wg sync.WaitGroup
|
||||
connHandle := GetConn(conn1, crypt, snappy, rate, isServer)
|
||||
if rb != nil {
|
||||
connHandle.Write(rb)
|
||||
}
|
||||
//go func(in *int64) {
|
||||
// wg.Add(1)
|
||||
// *in, _ = common.CopyBuffer(connHandle, conn2)
|
||||
// connHandle.Close()
|
||||
// conn2.Close()
|
||||
// wg.Done()
|
||||
//}(&in)
|
||||
//out, _ = common.CopyBuffer(conn2, connHandle)
|
||||
//connHandle.Close()
|
||||
//conn2.Close()
|
||||
//wg.Wait()
|
||||
//if flow != nil {
|
||||
// flow.Add(in, out)
|
||||
//}
|
||||
err := goroutine.CopyConnsPool.Invoke(goroutine.NewConns(connHandle, conn2, flow))
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
//get crypt or snappy conn
|
||||
func GetConn(conn net.Conn, cpt, snappy bool, rt *rate.Rate, isServer bool) io.ReadWriteCloser {
|
||||
if cpt {
|
||||
if isServer {
|
||||
return rate.NewRateConn(crypt.NewTlsServerConn(conn), rt)
|
||||
}
|
||||
return rate.NewRateConn(crypt.NewTlsClientConn(conn), rt)
|
||||
} else if snappy {
|
||||
return rate.NewRateConn(NewSnappyConn(conn), rt)
|
||||
}
|
||||
return rate.NewRateConn(conn, rt)
|
||||
}
|
||||
|
||||
type LenConn struct {
|
||||
conn io.Writer
|
||||
Len int
|
||||
}
|
||||
|
||||
func NewLenConn(conn io.Writer) *LenConn {
|
||||
return &LenConn{conn: conn}
|
||||
}
|
||||
func (c *LenConn) Write(p []byte) (n int, err error) {
|
||||
n, err = c.conn.Write(p)
|
||||
c.Len += n
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package conn
|
||||
|
||||
import "time"
|
||||
|
||||
type Secret struct {
|
||||
Password string
|
||||
Conn *Conn
|
||||
}
|
||||
|
||||
func NewSecret(p string, conn *Conn) *Secret {
|
||||
return &Secret{
|
||||
Password: p,
|
||||
Conn: conn,
|
||||
}
|
||||
}
|
||||
|
||||
type Link struct {
|
||||
ConnType string //连接类型
|
||||
Host string //目标
|
||||
Crypt bool //加密
|
||||
Compress bool
|
||||
LocalProxy bool
|
||||
RemoteAddr string
|
||||
Option Options
|
||||
}
|
||||
|
||||
type Option func(*Options)
|
||||
|
||||
type Options struct {
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
var defaultTimeOut = time.Second * 5
|
||||
|
||||
func NewLink(connType string, host string, crypt bool, compress bool, remoteAddr string, localProxy bool, opts ...Option) *Link {
|
||||
options := newOptions(opts...)
|
||||
|
||||
return &Link{
|
||||
RemoteAddr: remoteAddr,
|
||||
ConnType: connType,
|
||||
Host: host,
|
||||
Crypt: crypt,
|
||||
Compress: compress,
|
||||
LocalProxy: localProxy,
|
||||
Option: options,
|
||||
}
|
||||
}
|
||||
|
||||
func newOptions(opts ...Option) Options {
|
||||
opt := Options{
|
||||
Timeout: defaultTimeOut,
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(&opt)
|
||||
}
|
||||
return opt
|
||||
}
|
||||
|
||||
func LinkTimeout(t time.Duration) Option {
|
||||
return func(opt *Options) {
|
||||
opt.Timeout = t
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package conn
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/xtaci/kcp-go"
|
||||
)
|
||||
|
||||
func NewTcpListenerAndProcess(addr string, f func(c net.Conn), listener *net.Listener) error {
|
||||
var err error
|
||||
*listener, err = net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
Accept(*listener, f)
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewKcpListenerAndProcess(addr string, f func(c net.Conn)) error {
|
||||
kcpListener, err := kcp.ListenWithOptions(addr, nil, 150, 3)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return err
|
||||
}
|
||||
for {
|
||||
c, err := kcpListener.AcceptKCP()
|
||||
SetUdpSession(c)
|
||||
if err != nil {
|
||||
logs.Warn(err)
|
||||
continue
|
||||
}
|
||||
go f(c)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Accept(l net.Listener, f func(c net.Conn)) {
|
||||
for {
|
||||
c, err := l.Accept()
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "use of closed network connection") {
|
||||
break
|
||||
}
|
||||
if strings.Contains(err.Error(), "the mux has closed") {
|
||||
break
|
||||
}
|
||||
logs.Warn(err)
|
||||
continue
|
||||
}
|
||||
if c == nil {
|
||||
logs.Warn("nil connection")
|
||||
break
|
||||
}
|
||||
go f(c)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package conn
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/golang/snappy"
|
||||
)
|
||||
|
||||
type SnappyConn struct {
|
||||
w *snappy.Writer
|
||||
r *snappy.Reader
|
||||
}
|
||||
|
||||
func NewSnappyConn(conn io.ReadWriteCloser) *SnappyConn {
|
||||
c := new(SnappyConn)
|
||||
c.w = snappy.NewBufferedWriter(conn)
|
||||
c.r = snappy.NewReader(conn)
|
||||
return c
|
||||
}
|
||||
|
||||
//snappy压缩写
|
||||
func (s *SnappyConn) Write(b []byte) (n int, err error) {
|
||||
if n, err = s.w.Write(b); err != nil {
|
||||
return
|
||||
}
|
||||
if err = s.w.Flush(); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//snappy压缩读
|
||||
func (s *SnappyConn) Read(b []byte) (n int, err error) {
|
||||
return s.r.Read(b)
|
||||
}
|
||||
|
||||
func (s *SnappyConn) Close() error {
|
||||
s.w.Close()
|
||||
return s.w.Close()
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
package crypt
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type CurveID uint16
|
||||
type SignatureScheme uint16
|
||||
|
||||
const (
|
||||
statusTypeOCSP uint8 = 1
|
||||
extensionServerName uint16 = 0
|
||||
extensionStatusRequest uint16 = 5
|
||||
extensionSupportedCurves uint16 = 10
|
||||
extensionSupportedPoints uint16 = 11
|
||||
extensionSignatureAlgorithms uint16 = 13
|
||||
extensionALPN uint16 = 16
|
||||
extensionSCT uint16 = 18 // https://tools.ietf.org/html/rfc6962#section-6
|
||||
extensionSessionTicket uint16 = 35
|
||||
extensionNextProtoNeg uint16 = 13172 // not IANA assigned
|
||||
extensionRenegotiationInfo uint16 = 0xff01
|
||||
scsvRenegotiation uint16 = 0x00ff
|
||||
)
|
||||
|
||||
type ClientHelloMsg struct {
|
||||
raw []byte
|
||||
vers uint16
|
||||
random []byte
|
||||
sessionId []byte
|
||||
cipherSuites []uint16
|
||||
compressionMethods []uint8
|
||||
nextProtoNeg bool
|
||||
serverName string
|
||||
ocspStapling bool
|
||||
scts bool
|
||||
supportedCurves []CurveID
|
||||
supportedPoints []uint8
|
||||
ticketSupported bool
|
||||
sessionTicket []uint8
|
||||
supportedSignatureAlgorithms []SignatureScheme
|
||||
secureRenegotiation []byte
|
||||
secureRenegotiationSupported bool
|
||||
alpnProtocols []string
|
||||
}
|
||||
|
||||
func (m *ClientHelloMsg) GetServerName() string {
|
||||
return m.serverName
|
||||
}
|
||||
|
||||
func (m *ClientHelloMsg) Unmarshal(data []byte) bool {
|
||||
if len(data) < 42 {
|
||||
return false
|
||||
}
|
||||
m.raw = data
|
||||
m.vers = uint16(data[4])<<8 | uint16(data[5])
|
||||
m.random = data[6:38]
|
||||
sessionIdLen := int(data[38])
|
||||
if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
|
||||
return false
|
||||
}
|
||||
m.sessionId = data[39 : 39+sessionIdLen]
|
||||
data = data[39+sessionIdLen:]
|
||||
if len(data) < 2 {
|
||||
return false
|
||||
}
|
||||
// cipherSuiteLen is the number of bytes of cipher suite numbers. Since
|
||||
// they are uint16s, the number must be even.
|
||||
cipherSuiteLen := int(data[0])<<8 | int(data[1])
|
||||
if cipherSuiteLen%2 == 1 || len(data) < 2+cipherSuiteLen {
|
||||
return false
|
||||
}
|
||||
numCipherSuites := cipherSuiteLen / 2
|
||||
m.cipherSuites = make([]uint16, numCipherSuites)
|
||||
for i := 0; i < numCipherSuites; i++ {
|
||||
m.cipherSuites[i] = uint16(data[2+2*i])<<8 | uint16(data[3+2*i])
|
||||
if m.cipherSuites[i] == scsvRenegotiation {
|
||||
m.secureRenegotiationSupported = true
|
||||
}
|
||||
}
|
||||
data = data[2+cipherSuiteLen:]
|
||||
if len(data) < 1 {
|
||||
return false
|
||||
}
|
||||
compressionMethodsLen := int(data[0])
|
||||
if len(data) < 1+compressionMethodsLen {
|
||||
return false
|
||||
}
|
||||
m.compressionMethods = data[1 : 1+compressionMethodsLen]
|
||||
data = data[1+compressionMethodsLen:]
|
||||
|
||||
m.nextProtoNeg = false
|
||||
m.serverName = ""
|
||||
m.ocspStapling = false
|
||||
m.ticketSupported = false
|
||||
m.sessionTicket = nil
|
||||
m.supportedSignatureAlgorithms = nil
|
||||
m.alpnProtocols = nil
|
||||
m.scts = false
|
||||
|
||||
if len(data) == 0 {
|
||||
// ClientHello is optionally followed by extension data
|
||||
return true
|
||||
}
|
||||
if len(data) < 2 {
|
||||
return false
|
||||
}
|
||||
|
||||
extensionsLength := int(data[0])<<8 | int(data[1])
|
||||
data = data[2:]
|
||||
if extensionsLength != len(data) {
|
||||
return false
|
||||
}
|
||||
|
||||
for len(data) != 0 {
|
||||
if len(data) < 4 {
|
||||
return false
|
||||
}
|
||||
extension := uint16(data[0])<<8 | uint16(data[1])
|
||||
length := int(data[2])<<8 | int(data[3])
|
||||
data = data[4:]
|
||||
if len(data) < length {
|
||||
return false
|
||||
}
|
||||
|
||||
switch extension {
|
||||
case extensionServerName:
|
||||
d := data[:length]
|
||||
if len(d) < 2 {
|
||||
return false
|
||||
}
|
||||
namesLen := int(d[0])<<8 | int(d[1])
|
||||
d = d[2:]
|
||||
if len(d) != namesLen {
|
||||
return false
|
||||
}
|
||||
for len(d) > 0 {
|
||||
if len(d) < 3 {
|
||||
return false
|
||||
}
|
||||
nameType := d[0]
|
||||
nameLen := int(d[1])<<8 | int(d[2])
|
||||
d = d[3:]
|
||||
if len(d) < nameLen {
|
||||
return false
|
||||
}
|
||||
if nameType == 0 {
|
||||
m.serverName = string(d[:nameLen])
|
||||
// An SNI value may not include a
|
||||
// trailing dot. See
|
||||
// https://tools.ietf.org/html/rfc6066#section-3.
|
||||
if strings.HasSuffix(m.serverName, ".") {
|
||||
return false
|
||||
}
|
||||
break
|
||||
}
|
||||
d = d[nameLen:]
|
||||
}
|
||||
case extensionNextProtoNeg:
|
||||
if length > 0 {
|
||||
return false
|
||||
}
|
||||
m.nextProtoNeg = true
|
||||
case extensionStatusRequest:
|
||||
m.ocspStapling = length > 0 && data[0] == statusTypeOCSP
|
||||
case extensionSupportedCurves:
|
||||
// https://tools.ietf.org/html/rfc4492#section-5.5.1
|
||||
if length < 2 {
|
||||
return false
|
||||
}
|
||||
l := int(data[0])<<8 | int(data[1])
|
||||
if l%2 == 1 || length != l+2 {
|
||||
return false
|
||||
}
|
||||
numCurves := l / 2
|
||||
m.supportedCurves = make([]CurveID, numCurves)
|
||||
d := data[2:]
|
||||
for i := 0; i < numCurves; i++ {
|
||||
m.supportedCurves[i] = CurveID(d[0])<<8 | CurveID(d[1])
|
||||
d = d[2:]
|
||||
}
|
||||
case extensionSupportedPoints:
|
||||
// https://tools.ietf.org/html/rfc4492#section-5.5.2
|
||||
if length < 1 {
|
||||
return false
|
||||
}
|
||||
l := int(data[0])
|
||||
if length != l+1 {
|
||||
return false
|
||||
}
|
||||
m.supportedPoints = make([]uint8, l)
|
||||
copy(m.supportedPoints, data[1:])
|
||||
case extensionSessionTicket:
|
||||
// https://tools.ietf.org/html/rfc5077#section-3.2
|
||||
m.ticketSupported = true
|
||||
m.sessionTicket = data[:length]
|
||||
case extensionSignatureAlgorithms:
|
||||
// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
|
||||
if length < 2 || length&1 != 0 {
|
||||
return false
|
||||
}
|
||||
l := int(data[0])<<8 | int(data[1])
|
||||
if l != length-2 {
|
||||
return false
|
||||
}
|
||||
n := l / 2
|
||||
d := data[2:]
|
||||
m.supportedSignatureAlgorithms = make([]SignatureScheme, n)
|
||||
for i := range m.supportedSignatureAlgorithms {
|
||||
m.supportedSignatureAlgorithms[i] = SignatureScheme(d[0])<<8 | SignatureScheme(d[1])
|
||||
d = d[2:]
|
||||
}
|
||||
case extensionRenegotiationInfo:
|
||||
if length == 0 {
|
||||
return false
|
||||
}
|
||||
d := data[:length]
|
||||
l := int(d[0])
|
||||
d = d[1:]
|
||||
if l != len(d) {
|
||||
return false
|
||||
}
|
||||
|
||||
m.secureRenegotiation = d
|
||||
m.secureRenegotiationSupported = true
|
||||
case extensionALPN:
|
||||
if length < 2 {
|
||||
return false
|
||||
}
|
||||
l := int(data[0])<<8 | int(data[1])
|
||||
if l != length-2 {
|
||||
return false
|
||||
}
|
||||
d := data[2:length]
|
||||
for len(d) != 0 {
|
||||
stringLen := int(d[0])
|
||||
d = d[1:]
|
||||
if stringLen == 0 || stringLen > len(d) {
|
||||
return false
|
||||
}
|
||||
m.alpnProtocols = append(m.alpnProtocols, string(d[:stringLen]))
|
||||
d = d[stringLen:]
|
||||
}
|
||||
case extensionSCT:
|
||||
m.scts = true
|
||||
if length != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
data = data[length:]
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package lib
|
||||
package crypt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"crypto/cipher"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
@@ -18,11 +19,8 @@ func AesEncrypt(origData, key []byte) ([]byte, error) {
|
||||
}
|
||||
blockSize := block.BlockSize()
|
||||
origData = PKCS5Padding(origData, blockSize)
|
||||
// origData = ZeroPadding(origData, block.BlockSize())
|
||||
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
|
||||
crypted := make([]byte, len(origData))
|
||||
// 根据CryptBlocks方法的说明,如下方式初始化crypted也可以
|
||||
// crypted := origData
|
||||
blockMode.CryptBlocks(crypted, origData)
|
||||
return crypted, nil
|
||||
}
|
||||
@@ -36,36 +34,36 @@ func AesDecrypt(crypted, key []byte) ([]byte, error) {
|
||||
blockSize := block.BlockSize()
|
||||
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
|
||||
origData := make([]byte, len(crypted))
|
||||
// origData := crypted
|
||||
blockMode.CryptBlocks(origData, crypted)
|
||||
origData = PKCS5UnPadding(origData)
|
||||
// origData = ZeroUnPadding(origData)
|
||||
return origData, nil
|
||||
err, origData = PKCS5UnPadding(origData)
|
||||
return origData, err
|
||||
}
|
||||
|
||||
//补全
|
||||
//Completion when the length is insufficient
|
||||
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
|
||||
padding := blockSize - len(ciphertext)%blockSize
|
||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(ciphertext, padtext...)
|
||||
}
|
||||
|
||||
//去补
|
||||
func PKCS5UnPadding(origData []byte) []byte {
|
||||
//Remove excess
|
||||
func PKCS5UnPadding(origData []byte) (error, []byte) {
|
||||
length := len(origData)
|
||||
// 去掉最后一个字节 unpadding 次
|
||||
unpadding := int(origData[length-1])
|
||||
return origData[:(length - unpadding)]
|
||||
if (length - unpadding) < 0 {
|
||||
return errors.New("len error"), nil
|
||||
}
|
||||
return nil, origData[:(length - unpadding)]
|
||||
}
|
||||
|
||||
//生成32位md5字串
|
||||
//Generate 32-bit MD5 strings
|
||||
func Md5(s string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(s))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
//生成随机验证密钥
|
||||
//Generating Random Verification Key
|
||||
func GetRandomString(l int) string {
|
||||
str := "0123456789abcdefghijklmnopqrstuvwxyz"
|
||||
bytes := []byte(str)
|
||||
@@ -0,0 +1,34 @@
|
||||
package crypt
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/astaxie/beego/logs"
|
||||
)
|
||||
|
||||
var pemPath, keyPath string
|
||||
|
||||
func InitTls(pem, key string) {
|
||||
pemPath = pem
|
||||
keyPath = key
|
||||
}
|
||||
|
||||
func NewTlsServerConn(conn net.Conn) net.Conn {
|
||||
cert, err := tls.LoadX509KeyPair(pemPath, keyPath)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
os.Exit(0)
|
||||
return nil
|
||||
}
|
||||
config := &tls.Config{Certificates: []tls.Certificate{cert}}
|
||||
return tls.Server(conn, config)
|
||||
}
|
||||
|
||||
func NewTlsClientConn(conn net.Conn) net.Conn {
|
||||
conf := &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
return tls.Client(conn, conf)
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
)
|
||||
|
||||
func InitDaemon(f string, runPath string, pidPath string) {
|
||||
if len(os.Args) < 2 {
|
||||
return
|
||||
}
|
||||
var args []string
|
||||
args = append(args, os.Args[0])
|
||||
if len(os.Args) >= 2 {
|
||||
args = append(args, os.Args[2:]...)
|
||||
}
|
||||
args = append(args, "-log=file")
|
||||
switch os.Args[1] {
|
||||
case "start":
|
||||
start(args, f, pidPath, runPath)
|
||||
os.Exit(0)
|
||||
case "stop":
|
||||
stop(f, args[0], pidPath)
|
||||
os.Exit(0)
|
||||
case "restart":
|
||||
stop(f, args[0], pidPath)
|
||||
start(args, f, pidPath, runPath)
|
||||
os.Exit(0)
|
||||
case "status":
|
||||
if status(f, pidPath) {
|
||||
log.Printf("%s is running", f)
|
||||
} else {
|
||||
log.Printf("%s is not running", f)
|
||||
}
|
||||
os.Exit(0)
|
||||
case "reload":
|
||||
reload(f, pidPath)
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
func reload(f string, pidPath string) {
|
||||
if f == "nps" && !common.IsWindows() && !status(f, pidPath) {
|
||||
log.Println("reload fail")
|
||||
return
|
||||
}
|
||||
var c *exec.Cmd
|
||||
var err error
|
||||
b, err := ioutil.ReadFile(filepath.Join(pidPath, f+".pid"))
|
||||
if err == nil {
|
||||
c = exec.Command("/bin/bash", "-c", `kill -30 `+string(b))
|
||||
} else {
|
||||
log.Fatalln("reload error,pid file does not exist")
|
||||
}
|
||||
if c.Run() == nil {
|
||||
log.Println("reload success")
|
||||
} else {
|
||||
log.Println("reload fail")
|
||||
}
|
||||
}
|
||||
|
||||
func status(f string, pidPath string) bool {
|
||||
var cmd *exec.Cmd
|
||||
b, err := ioutil.ReadFile(filepath.Join(pidPath, f+".pid"))
|
||||
if err == nil {
|
||||
if !common.IsWindows() {
|
||||
cmd = exec.Command("/bin/sh", "-c", "ps -ax | awk '{ print $1 }' | grep "+string(b))
|
||||
} else {
|
||||
cmd = exec.Command("tasklist")
|
||||
}
|
||||
out, _ := cmd.Output()
|
||||
if strings.Index(string(out), string(b)) > -1 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func start(osArgs []string, f string, pidPath, runPath string) {
|
||||
if status(f, pidPath) {
|
||||
log.Printf(" %s is running", f)
|
||||
return
|
||||
}
|
||||
cmd := exec.Command(osArgs[0], osArgs[1:]...)
|
||||
cmd.Start()
|
||||
if cmd.Process.Pid > 0 {
|
||||
log.Println("start ok , pid:", cmd.Process.Pid, "config path:", runPath)
|
||||
d1 := []byte(strconv.Itoa(cmd.Process.Pid))
|
||||
ioutil.WriteFile(filepath.Join(pidPath, f+".pid"), d1, 0600)
|
||||
} else {
|
||||
log.Println("start error")
|
||||
}
|
||||
}
|
||||
|
||||
func stop(f string, p string, pidPath string) {
|
||||
if !status(f, pidPath) {
|
||||
log.Printf(" %s is not running", f)
|
||||
return
|
||||
}
|
||||
var c *exec.Cmd
|
||||
var err error
|
||||
if common.IsWindows() {
|
||||
p := strings.Split(p, `\`)
|
||||
c = exec.Command("taskkill", "/F", "/IM", p[len(p)-1])
|
||||
} else {
|
||||
b, err := ioutil.ReadFile(filepath.Join(pidPath, f+".pid"))
|
||||
if err == nil {
|
||||
c = exec.Command("/bin/bash", "-c", `kill -9 `+string(b))
|
||||
} else {
|
||||
log.Fatalln("stop error,pid file does not exist")
|
||||
}
|
||||
}
|
||||
err = c.Run()
|
||||
if err != nil {
|
||||
log.Println("stop error,", err)
|
||||
} else {
|
||||
log.Println("stop ok")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// +build !windows
|
||||
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
)
|
||||
|
||||
func init() {
|
||||
s := make(chan os.Signal, 1)
|
||||
signal.Notify(s, syscall.SIGUSR1)
|
||||
go func() {
|
||||
for {
|
||||
<-s
|
||||
beego.LoadAppConfig("ini", filepath.Join(common.GetRunPath(), "conf", "nps.conf"))
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -1,327 +0,0 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/astaxie/beego"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ServerConfig struct {
|
||||
TcpPort int //服务端与客户端通信端口
|
||||
Mode string //启动方式
|
||||
Target string //目标
|
||||
VerifyKey string //flag
|
||||
U string //socks5验证用户名
|
||||
P string //socks5验证密码
|
||||
Compress string //压缩方式
|
||||
Start int //是否开启
|
||||
IsRun int //是否在运行
|
||||
ClientStatus int //客s户端状态
|
||||
Crypt bool //是否加密
|
||||
Mux bool //是否加密
|
||||
CompressEncode int
|
||||
CompressDecode int
|
||||
}
|
||||
|
||||
type HostList struct {
|
||||
Vkey string //服务端与客户端通信端口
|
||||
Host string //启动方式
|
||||
Target string //目标
|
||||
}
|
||||
|
||||
func NewCsv(bridge *Tunnel, runList map[string]interface{}) *Csv {
|
||||
c := new(Csv)
|
||||
c.Bridge = bridge
|
||||
c.RunList = runList
|
||||
return c
|
||||
}
|
||||
|
||||
type Csv struct {
|
||||
Tasks []*ServerConfig
|
||||
Path string
|
||||
Bridge *Tunnel
|
||||
RunList map[string]interface{}
|
||||
Hosts []*HostList //域名列表
|
||||
}
|
||||
|
||||
func (s *Csv) Init() {
|
||||
s.LoadTaskFromCsv()
|
||||
s.LoadHostFromCsv()
|
||||
}
|
||||
|
||||
func (s *Csv) StoreTasksToCsv() {
|
||||
// 创建文件
|
||||
csvFile, err := os.Create(beego.AppPath + "/conf/tasks.csv")
|
||||
if err != nil {
|
||||
log.Fatalf(err.Error())
|
||||
}
|
||||
defer csvFile.Close()
|
||||
writer := csv.NewWriter(csvFile)
|
||||
for _, task := range s.Tasks {
|
||||
record := []string{
|
||||
strconv.Itoa(task.TcpPort),
|
||||
task.Mode,
|
||||
task.Target,
|
||||
task.VerifyKey,
|
||||
task.U,
|
||||
task.P,
|
||||
task.Compress,
|
||||
strconv.Itoa(task.Start),
|
||||
GetStrByBool(task.Crypt),
|
||||
GetStrByBool(task.Mux),
|
||||
strconv.Itoa(task.CompressEncode),
|
||||
strconv.Itoa(task.CompressDecode),
|
||||
}
|
||||
err := writer.Write(record)
|
||||
if err != nil {
|
||||
log.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
writer.Flush()
|
||||
}
|
||||
|
||||
func (s *Csv) LoadTaskFromCsv() {
|
||||
// 打开文件
|
||||
file, err := os.Open(beego.AppPath + "/conf/tasks.csv")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 获取csv的reader
|
||||
reader := csv.NewReader(file)
|
||||
|
||||
// 设置FieldsPerRecord为-1
|
||||
reader.FieldsPerRecord = -1
|
||||
|
||||
// 读取文件中所有行保存到slice中
|
||||
records, err := reader.ReadAll()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var tasks []*ServerConfig
|
||||
// 将每一行数据保存到内存slice中
|
||||
for _, item := range records {
|
||||
tcpPort, _ := strconv.Atoi(item[0])
|
||||
Start, _ := strconv.Atoi(item[7])
|
||||
post := &ServerConfig{
|
||||
TcpPort: tcpPort,
|
||||
Mode: item[1],
|
||||
Target: item[2],
|
||||
VerifyKey: item[3],
|
||||
U: item[4],
|
||||
P: item[5],
|
||||
Compress: item[6],
|
||||
Start: Start,
|
||||
Crypt: GetBoolByStr(item[8]),
|
||||
Mux: GetBoolByStr(item[9]),
|
||||
CompressEncode: GetIntNoerrByStr(item[10]),
|
||||
CompressDecode: GetIntNoerrByStr(item[11]),
|
||||
}
|
||||
tasks = append(tasks, post)
|
||||
}
|
||||
s.Tasks = tasks
|
||||
}
|
||||
|
||||
func (s *Csv) StoreHostToCsv() {
|
||||
// 创建文件
|
||||
csvFile, err := os.Create(s.Path + "hosts.csv")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer csvFile.Close()
|
||||
// 获取csv的Writer
|
||||
writer := csv.NewWriter(csvFile)
|
||||
// 将map中的Post转换成slice,因为csv的Write需要slice参数
|
||||
// 并写入csv文件
|
||||
for _, host := range s.Hosts {
|
||||
record := []string{
|
||||
host.Host,
|
||||
host.Target,
|
||||
host.Vkey,
|
||||
}
|
||||
err1 := writer.Write(record)
|
||||
if err1 != nil {
|
||||
panic(err1)
|
||||
}
|
||||
}
|
||||
// 确保所有内存数据刷到csv文件
|
||||
writer.Flush()
|
||||
}
|
||||
|
||||
func (s *Csv) LoadHostFromCsv() {
|
||||
// 打开文件
|
||||
file, err := os.Open(beego.AppPath + "/conf/hosts.csv")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 获取csv的reader
|
||||
reader := csv.NewReader(file)
|
||||
|
||||
// 设置FieldsPerRecord为-1
|
||||
reader.FieldsPerRecord = -1
|
||||
|
||||
// 读取文件中所有行保存到slice中
|
||||
records, err := reader.ReadAll()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var hosts []*HostList
|
||||
// 将每一行数据保存到内存slice中
|
||||
for _, item := range records {
|
||||
post := &HostList{
|
||||
Vkey: item[2],
|
||||
Host: item[0],
|
||||
Target: item[1],
|
||||
}
|
||||
hosts = append(hosts, post)
|
||||
}
|
||||
s.Hosts = hosts
|
||||
}
|
||||
|
||||
func (s *Csv) GetServerConfig(start, length int, typeVal string) ([]*ServerConfig, int) {
|
||||
list := make([]*ServerConfig, 0)
|
||||
var cnt int
|
||||
for _, v := range s.Tasks {
|
||||
if v.Mode != typeVal {
|
||||
continue
|
||||
}
|
||||
cnt++
|
||||
if start--; start < 0 {
|
||||
if length--; length > 0 {
|
||||
if _, ok := s.RunList[v.VerifyKey]; ok {
|
||||
v.IsRun = 1
|
||||
} else {
|
||||
v.IsRun = 0
|
||||
}
|
||||
if s, ok := s.Bridge.signalList[getverifyval(v.VerifyKey)]; ok {
|
||||
if s.Len() > 0 {
|
||||
v.ClientStatus = 1
|
||||
} else {
|
||||
v.ClientStatus = 0
|
||||
}
|
||||
} else {
|
||||
v.ClientStatus = 0
|
||||
}
|
||||
list = append(list, v)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return list, cnt
|
||||
}
|
||||
|
||||
func (s *Csv) NewTask(t *ServerConfig) {
|
||||
s.Tasks = append(s.Tasks, t)
|
||||
s.StoreTasksToCsv()
|
||||
}
|
||||
|
||||
func (s *Csv) UpdateTask(t *ServerConfig) error {
|
||||
for k, v := range s.Tasks {
|
||||
if v.VerifyKey == t.VerifyKey {
|
||||
s.Tasks = append(s.Tasks[:k], s.Tasks[k+1:]...)
|
||||
s.Tasks = append(s.Tasks, t)
|
||||
s.StoreTasksToCsv()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
//TODO:待测试
|
||||
return errors.New("不存在")
|
||||
}
|
||||
|
||||
func (s *Csv) AddRunList(vKey string, svr interface{}) {
|
||||
s.RunList[vKey] = svr
|
||||
}
|
||||
|
||||
func (s *Csv) DelRunList(vKey string) {
|
||||
delete(s.RunList, vKey)
|
||||
}
|
||||
|
||||
func (s *Csv) DelTask(vKey string) error {
|
||||
for k, v := range s.Tasks {
|
||||
if v.VerifyKey == vKey {
|
||||
s.Tasks = append(s.Tasks[:k], s.Tasks[k+1:]...)
|
||||
s.StoreTasksToCsv()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errors.New("不存在")
|
||||
}
|
||||
|
||||
func (s *Csv) GetTask(vKey string) (v *ServerConfig, err error) {
|
||||
for _, v = range s.Tasks {
|
||||
if v.VerifyKey == vKey {
|
||||
return
|
||||
}
|
||||
}
|
||||
err = errors.New("未找到")
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Csv) DelHost(host string) error {
|
||||
for k, v := range s.Hosts {
|
||||
if v.Host == host {
|
||||
s.Hosts = append(s.Hosts[:k], s.Hosts[k+1:]...)
|
||||
s.StoreHostToCsv()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errors.New("不存在")
|
||||
}
|
||||
|
||||
func (s *Csv) NewHost(t *HostList) {
|
||||
s.Hosts = append(s.Hosts, t)
|
||||
s.StoreHostToCsv()
|
||||
|
||||
}
|
||||
|
||||
func (s *Csv) GetHostList(start, length int, vKey string) ([]*HostList, int) {
|
||||
list := make([]*HostList, 0)
|
||||
var cnt int
|
||||
for _, v := range s.Hosts {
|
||||
if v.Vkey == vKey {
|
||||
cnt++
|
||||
if start--; start < 0 {
|
||||
if length--; length > 0 {
|
||||
list = append(list, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return list, cnt
|
||||
}
|
||||
|
||||
type Site struct {
|
||||
Host string
|
||||
Url string
|
||||
Port int
|
||||
}
|
||||
type Config struct {
|
||||
SiteList []Site
|
||||
Replace int
|
||||
}
|
||||
type JsonStruct struct {
|
||||
}
|
||||
|
||||
func NewJsonStruct() *JsonStruct {
|
||||
return &JsonStruct{}
|
||||
}
|
||||
func (jst *JsonStruct) Load(filename string) (Config, error) {
|
||||
data, err := ioutil.ReadFile(filename)
|
||||
config := Config{}
|
||||
if err != nil {
|
||||
return config, errors.New("配置文件打开错误")
|
||||
}
|
||||
err = json.Unmarshal(data, &config)
|
||||
if err != nil {
|
||||
return config, errors.New("配置文件解析错误")
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
@@ -0,0 +1,359 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
)
|
||||
|
||||
type DbUtils struct {
|
||||
JsonDb *JsonDb
|
||||
}
|
||||
|
||||
var (
|
||||
Db *DbUtils
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
//init csv from file
|
||||
func GetDb() *DbUtils {
|
||||
once.Do(func() {
|
||||
jsonDb := NewJsonDb(common.GetRunPath())
|
||||
jsonDb.LoadClientFromJsonFile()
|
||||
jsonDb.LoadTaskFromJsonFile()
|
||||
jsonDb.LoadHostFromJsonFile()
|
||||
Db = &DbUtils{JsonDb: jsonDb}
|
||||
})
|
||||
return Db
|
||||
}
|
||||
|
||||
func GetMapKeys(m sync.Map, isSort bool, sortKey, order string) (keys []int) {
|
||||
if sortKey != "" && isSort {
|
||||
return sortClientByKey(m, sortKey, order)
|
||||
}
|
||||
m.Range(func(key, value interface{}) bool {
|
||||
keys = append(keys, key.(int))
|
||||
return true
|
||||
})
|
||||
sort.Ints(keys)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *DbUtils) GetClientList(start, length int, search, sort, order string, clientId int) ([]*Client, int) {
|
||||
list := make([]*Client, 0)
|
||||
var cnt int
|
||||
keys := GetMapKeys(s.JsonDb.Clients, true, sort, order)
|
||||
for _, key := range keys {
|
||||
if value, ok := s.JsonDb.Clients.Load(key); ok {
|
||||
v := value.(*Client)
|
||||
if v.NoDisplay {
|
||||
continue
|
||||
}
|
||||
if clientId != 0 && clientId != v.Id {
|
||||
continue
|
||||
}
|
||||
if search != "" && !(v.Id == common.GetIntNoErrByStr(search) || strings.Contains(v.VerifyKey, search) || strings.Contains(v.Remark, search)) {
|
||||
continue
|
||||
}
|
||||
cnt++
|
||||
if start--; start < 0 {
|
||||
if length--; length >= 0 {
|
||||
list = append(list, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return list, cnt
|
||||
}
|
||||
|
||||
func (s *DbUtils) GetIdByVerifyKey(vKey string, addr string) (id int, err error) {
|
||||
var exist bool
|
||||
s.JsonDb.Clients.Range(func(key, value interface{}) bool {
|
||||
v := value.(*Client)
|
||||
if common.Getverifyval(v.VerifyKey) == vKey && v.Status {
|
||||
v.Addr = common.GetIpByAddr(addr)
|
||||
id = v.Id
|
||||
exist = true
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
if exist {
|
||||
return
|
||||
}
|
||||
return 0, errors.New("not found")
|
||||
}
|
||||
|
||||
func (s *DbUtils) NewTask(t *Tunnel) (err error) {
|
||||
s.JsonDb.Tasks.Range(func(key, value interface{}) bool {
|
||||
v := value.(*Tunnel)
|
||||
if (v.Mode == "secret" || v.Mode == "p2p") && v.Password == t.Password {
|
||||
err = errors.New(fmt.Sprintf("secret mode keys %s must be unique", t.Password))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
t.Flow = new(Flow)
|
||||
s.JsonDb.Tasks.Store(t.Id, t)
|
||||
s.JsonDb.StoreTasksToJsonFile()
|
||||
return
|
||||
}
|
||||
|
||||
func (s *DbUtils) UpdateTask(t *Tunnel) error {
|
||||
s.JsonDb.Tasks.Store(t.Id, t)
|
||||
s.JsonDb.StoreTasksToJsonFile()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *DbUtils) DelTask(id int) error {
|
||||
s.JsonDb.Tasks.Delete(id)
|
||||
s.JsonDb.StoreTasksToJsonFile()
|
||||
return nil
|
||||
}
|
||||
|
||||
//md5 password
|
||||
func (s *DbUtils) GetTaskByMd5Password(p string) (t *Tunnel) {
|
||||
s.JsonDb.Tasks.Range(func(key, value interface{}) bool {
|
||||
if crypt.Md5(value.(*Tunnel).Password) == p {
|
||||
t = value.(*Tunnel)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *DbUtils) GetTask(id int) (t *Tunnel, err error) {
|
||||
if v, ok := s.JsonDb.Tasks.Load(id); ok {
|
||||
t = v.(*Tunnel)
|
||||
return
|
||||
}
|
||||
err = errors.New("not found")
|
||||
return
|
||||
}
|
||||
|
||||
func (s *DbUtils) DelHost(id int) error {
|
||||
s.JsonDb.Hosts.Delete(id)
|
||||
s.JsonDb.StoreHostToJsonFile()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *DbUtils) IsHostExist(h *Host) bool {
|
||||
var exist bool
|
||||
s.JsonDb.Hosts.Range(func(key, value interface{}) bool {
|
||||
v := value.(*Host)
|
||||
if v.Id != h.Id && v.Host == h.Host && h.Location == v.Location && (v.Scheme == "all" || v.Scheme == h.Scheme) {
|
||||
exist = true
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
return exist
|
||||
}
|
||||
|
||||
func (s *DbUtils) NewHost(t *Host) error {
|
||||
if t.Location == "" {
|
||||
t.Location = "/"
|
||||
}
|
||||
if s.IsHostExist(t) {
|
||||
return errors.New("host has exist")
|
||||
}
|
||||
t.Flow = new(Flow)
|
||||
s.JsonDb.Hosts.Store(t.Id, t)
|
||||
s.JsonDb.StoreHostToJsonFile()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *DbUtils) GetHost(start, length int, id int, search string) ([]*Host, int) {
|
||||
list := make([]*Host, 0)
|
||||
var cnt int
|
||||
keys := GetMapKeys(s.JsonDb.Hosts, false, "", "")
|
||||
for _, key := range keys {
|
||||
if value, ok := s.JsonDb.Hosts.Load(key); ok {
|
||||
v := value.(*Host)
|
||||
if search != "" && !(v.Id == common.GetIntNoErrByStr(search) || strings.Contains(v.Host, search) || strings.Contains(v.Remark, search)) {
|
||||
continue
|
||||
}
|
||||
if id == 0 || v.Client.Id == id {
|
||||
cnt++
|
||||
if start--; start < 0 {
|
||||
if length--; length >= 0 {
|
||||
list = append(list, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return list, cnt
|
||||
}
|
||||
|
||||
func (s *DbUtils) DelClient(id int) error {
|
||||
s.JsonDb.Clients.Delete(id)
|
||||
s.JsonDb.StoreClientsToJsonFile()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *DbUtils) NewClient(c *Client) error {
|
||||
var isNotSet bool
|
||||
if c.WebUserName != "" && !s.VerifyUserName(c.WebUserName, c.Id) {
|
||||
return errors.New("web login username duplicate, please reset")
|
||||
}
|
||||
reset:
|
||||
if c.VerifyKey == "" || isNotSet {
|
||||
isNotSet = true
|
||||
c.VerifyKey = crypt.GetRandomString(16)
|
||||
}
|
||||
if c.RateLimit == 0 {
|
||||
c.Rate = rate.NewRate(int64(2 << 23))
|
||||
} else if c.Rate == nil {
|
||||
c.Rate = rate.NewRate(int64(c.RateLimit * 1024))
|
||||
}
|
||||
c.Rate.Start()
|
||||
if !s.VerifyVkey(c.VerifyKey, c.Id) {
|
||||
if isNotSet {
|
||||
goto reset
|
||||
}
|
||||
return errors.New("Vkey duplicate, please reset")
|
||||
}
|
||||
if c.Id == 0 {
|
||||
c.Id = int(s.JsonDb.GetClientId())
|
||||
}
|
||||
if c.Flow == nil {
|
||||
c.Flow = new(Flow)
|
||||
}
|
||||
s.JsonDb.Clients.Store(c.Id, c)
|
||||
s.JsonDb.StoreClientsToJsonFile()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *DbUtils) VerifyVkey(vkey string, id int) (res bool) {
|
||||
res = true
|
||||
s.JsonDb.Clients.Range(func(key, value interface{}) bool {
|
||||
v := value.(*Client)
|
||||
if v.VerifyKey == vkey && v.Id != id {
|
||||
res = false
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
return res
|
||||
}
|
||||
|
||||
func (s *DbUtils) VerifyUserName(username string, id int) (res bool) {
|
||||
res = true
|
||||
s.JsonDb.Clients.Range(func(key, value interface{}) bool {
|
||||
v := value.(*Client)
|
||||
if v.WebUserName == username && v.Id != id {
|
||||
res = false
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
return res
|
||||
}
|
||||
|
||||
func (s *DbUtils) UpdateClient(t *Client) error {
|
||||
s.JsonDb.Clients.Store(t.Id, t)
|
||||
if t.RateLimit == 0 {
|
||||
t.Rate = rate.NewRate(int64(2 << 23))
|
||||
t.Rate.Start()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *DbUtils) IsPubClient(id int) bool {
|
||||
client, err := s.GetClient(id)
|
||||
if err == nil {
|
||||
return client.NoDisplay
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *DbUtils) GetClient(id int) (c *Client, err error) {
|
||||
if v, ok := s.JsonDb.Clients.Load(id); ok {
|
||||
c = v.(*Client)
|
||||
return
|
||||
}
|
||||
err = errors.New("未找到客户端")
|
||||
return
|
||||
}
|
||||
|
||||
func (s *DbUtils) GetClientIdByVkey(vkey string) (id int, err error) {
|
||||
var exist bool
|
||||
s.JsonDb.Clients.Range(func(key, value interface{}) bool {
|
||||
v := value.(*Client)
|
||||
if crypt.Md5(v.VerifyKey) == vkey {
|
||||
exist = true
|
||||
id = v.Id
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
if exist {
|
||||
return
|
||||
}
|
||||
err = errors.New("未找到客户端")
|
||||
return
|
||||
}
|
||||
|
||||
func (s *DbUtils) GetHostById(id int) (h *Host, err error) {
|
||||
if v, ok := s.JsonDb.Hosts.Load(id); ok {
|
||||
h = v.(*Host)
|
||||
return
|
||||
}
|
||||
err = errors.New("The host could not be parsed")
|
||||
return
|
||||
}
|
||||
|
||||
//get key by host from x
|
||||
func (s *DbUtils) GetInfoByHost(host string, r *http.Request) (h *Host, err error) {
|
||||
var hosts []*Host
|
||||
//Handling Ported Access
|
||||
host = common.GetIpByAddr(host)
|
||||
s.JsonDb.Hosts.Range(func(key, value interface{}) bool {
|
||||
v := value.(*Host)
|
||||
if v.IsClose {
|
||||
return true
|
||||
}
|
||||
//Remove http(s) http(s)://a.proxy.com
|
||||
//*.proxy.com *.a.proxy.com Do some pan-parsing
|
||||
tmp := strings.Replace(v.Host, "*", `\w+?`, -1)
|
||||
var re *regexp.Regexp
|
||||
if re, err = regexp.Compile(tmp); err != nil {
|
||||
return true
|
||||
}
|
||||
if len(re.FindAllString(host, -1)) > 0 && (v.Scheme == "all" || v.Scheme == r.URL.Scheme) {
|
||||
//URL routing
|
||||
hosts = append(hosts, v)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
for _, v := range hosts {
|
||||
//If not set, default matches all
|
||||
if v.Location == "" {
|
||||
v.Location = "/"
|
||||
}
|
||||
if strings.Index(r.RequestURI, v.Location) == 0 {
|
||||
if h == nil || (len(v.Location) > len(h.Location)) {
|
||||
h = v
|
||||
}
|
||||
}
|
||||
}
|
||||
if h != nil {
|
||||
return
|
||||
}
|
||||
err = errors.New("The host could not be parsed")
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
)
|
||||
|
||||
func NewJsonDb(runPath string) *JsonDb {
|
||||
return &JsonDb{
|
||||
RunPath: runPath,
|
||||
TaskFilePath: filepath.Join(runPath, "conf", "tasks.json"),
|
||||
HostFilePath: filepath.Join(runPath, "conf", "hosts.json"),
|
||||
ClientFilePath: filepath.Join(runPath, "conf", "clients.json"),
|
||||
}
|
||||
}
|
||||
|
||||
type JsonDb struct {
|
||||
Tasks sync.Map
|
||||
Hosts sync.Map
|
||||
HostsTmp sync.Map
|
||||
Clients sync.Map
|
||||
RunPath string
|
||||
ClientIncreaseId int32 //client increased id
|
||||
TaskIncreaseId int32 //task increased id
|
||||
HostIncreaseId int32 //host increased id
|
||||
TaskFilePath string //task file path
|
||||
HostFilePath string //host file path
|
||||
ClientFilePath string //client file path
|
||||
}
|
||||
|
||||
func (s *JsonDb) LoadTaskFromJsonFile() {
|
||||
loadSyncMapFromFile(s.TaskFilePath, func(v string) {
|
||||
var err error
|
||||
post := new(Tunnel)
|
||||
if json.Unmarshal([]byte(v), &post) != nil {
|
||||
return
|
||||
}
|
||||
if post.Client, err = s.GetClient(post.Client.Id); err != nil {
|
||||
return
|
||||
}
|
||||
s.Tasks.Store(post.Id, post)
|
||||
if post.Id > int(s.TaskIncreaseId) {
|
||||
s.TaskIncreaseId = int32(post.Id)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (s *JsonDb) LoadClientFromJsonFile() {
|
||||
loadSyncMapFromFile(s.ClientFilePath, func(v string) {
|
||||
post := new(Client)
|
||||
if json.Unmarshal([]byte(v), &post) != nil {
|
||||
return
|
||||
}
|
||||
if post.RateLimit > 0 {
|
||||
post.Rate = rate.NewRate(int64(post.RateLimit * 1024))
|
||||
} else {
|
||||
post.Rate = rate.NewRate(int64(2 << 23))
|
||||
}
|
||||
post.Rate.Start()
|
||||
post.NowConn = 0
|
||||
s.Clients.Store(post.Id, post)
|
||||
if post.Id > int(s.ClientIncreaseId) {
|
||||
s.ClientIncreaseId = int32(post.Id)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (s *JsonDb) LoadHostFromJsonFile() {
|
||||
loadSyncMapFromFile(s.HostFilePath, func(v string) {
|
||||
var err error
|
||||
post := new(Host)
|
||||
if json.Unmarshal([]byte(v), &post) != nil {
|
||||
return
|
||||
}
|
||||
if post.Client, err = s.GetClient(post.Client.Id); err != nil {
|
||||
return
|
||||
}
|
||||
s.Hosts.Store(post.Id, post)
|
||||
if post.Id > int(s.HostIncreaseId) {
|
||||
s.HostIncreaseId = int32(post.Id)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (s *JsonDb) GetClient(id int) (c *Client, err error) {
|
||||
if v, ok := s.Clients.Load(id); ok {
|
||||
c = v.(*Client)
|
||||
return
|
||||
}
|
||||
err = errors.New("未找到客户端")
|
||||
return
|
||||
}
|
||||
|
||||
func (s *JsonDb) StoreHostToJsonFile() {
|
||||
storeSyncMapToFile(s.Hosts, s.HostFilePath)
|
||||
}
|
||||
|
||||
func (s *JsonDb) StoreTasksToJsonFile() {
|
||||
storeSyncMapToFile(s.Tasks, s.TaskFilePath)
|
||||
}
|
||||
|
||||
func (s *JsonDb) StoreClientsToJsonFile() {
|
||||
storeSyncMapToFile(s.Clients, s.ClientFilePath)
|
||||
}
|
||||
|
||||
func (s *JsonDb) GetClientId() int32 {
|
||||
return atomic.AddInt32(&s.ClientIncreaseId, 1)
|
||||
}
|
||||
|
||||
func (s *JsonDb) GetTaskId() int32 {
|
||||
return atomic.AddInt32(&s.TaskIncreaseId, 1)
|
||||
}
|
||||
|
||||
func (s *JsonDb) GetHostId() int32 {
|
||||
return atomic.AddInt32(&s.HostIncreaseId, 1)
|
||||
}
|
||||
|
||||
func loadSyncMapFromFile(filePath string, f func(value string)) {
|
||||
b, err := common.ReadAllFromFile(filePath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, v := range strings.Split(string(b), "\n"+common.CONN_DATA_SEQ) {
|
||||
f(v)
|
||||
}
|
||||
}
|
||||
|
||||
func storeSyncMapToFile(m sync.Map, filePath string) {
|
||||
file, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
m.Range(func(key, value interface{}) bool {
|
||||
var b []byte
|
||||
var err error
|
||||
switch value.(type) {
|
||||
case *Tunnel:
|
||||
obj := value.(*Tunnel)
|
||||
if obj.NoStore {
|
||||
return true
|
||||
}
|
||||
b, err = json.Marshal(obj)
|
||||
case *Host:
|
||||
obj := value.(*Host)
|
||||
if obj.NoStore {
|
||||
return true
|
||||
}
|
||||
b, err = json.Marshal(obj)
|
||||
case *Client:
|
||||
obj := value.(*Client)
|
||||
if obj.NoStore {
|
||||
return true
|
||||
}
|
||||
b, err = json.Marshal(obj)
|
||||
default:
|
||||
return true
|
||||
}
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
_, err = file.Write(b)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_, err = file.Write([]byte("\n" + common.CONN_DATA_SEQ))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Flow struct {
|
||||
ExportFlow int64
|
||||
InletFlow int64
|
||||
FlowLimit int64
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func (s *Flow) Add(in, out int64) {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
s.InletFlow += int64(in)
|
||||
s.ExportFlow += int64(out)
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
U string
|
||||
P string
|
||||
Compress bool
|
||||
Crypt bool
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
Cnf *Config
|
||||
Id int //id
|
||||
VerifyKey string //verify key
|
||||
Addr string //the ip of client
|
||||
Remark string //remark
|
||||
Status bool //is allow connect
|
||||
IsConnect bool //is the client connect
|
||||
RateLimit int //rate /kb
|
||||
Flow *Flow //flow setting
|
||||
Rate *rate.Rate //rate limit
|
||||
NoStore bool //no store to file
|
||||
NoDisplay bool //no display on web
|
||||
MaxConn int //the max connection num of client allow
|
||||
NowConn int32 //the connection num of now
|
||||
WebUserName string //the username of web login
|
||||
WebPassword string //the password of web login
|
||||
ConfigConnAllow bool //is allow connected by config file
|
||||
MaxTunnelNum int
|
||||
Version string
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func NewClient(vKey string, noStore bool, noDisplay bool) *Client {
|
||||
return &Client{
|
||||
Cnf: new(Config),
|
||||
Id: 0,
|
||||
VerifyKey: vKey,
|
||||
Addr: "",
|
||||
Remark: "",
|
||||
Status: true,
|
||||
IsConnect: false,
|
||||
RateLimit: 0,
|
||||
Flow: new(Flow),
|
||||
Rate: nil,
|
||||
NoStore: noStore,
|
||||
RWMutex: sync.RWMutex{},
|
||||
NoDisplay: noDisplay,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Client) CutConn() {
|
||||
atomic.AddInt32(&s.NowConn, 1)
|
||||
}
|
||||
|
||||
func (s *Client) AddConn() {
|
||||
atomic.AddInt32(&s.NowConn, -1)
|
||||
}
|
||||
|
||||
func (s *Client) GetConn() bool {
|
||||
if s.MaxConn == 0 || int(s.NowConn) < s.MaxConn {
|
||||
s.CutConn()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Client) HasTunnel(t *Tunnel) (exist bool) {
|
||||
GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
|
||||
v := value.(*Tunnel)
|
||||
if v.Client.Id == s.Id && v.Port == t.Port && t.Port != 0 {
|
||||
exist = true
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Client) GetTunnelNum() (num int) {
|
||||
GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
|
||||
v := value.(*Tunnel)
|
||||
if v.Client.Id == s.Id {
|
||||
num++
|
||||
}
|
||||
return true
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Client) HasHost(h *Host) bool {
|
||||
var has bool
|
||||
GetDb().JsonDb.Hosts.Range(func(key, value interface{}) bool {
|
||||
v := value.(*Host)
|
||||
if v.Client.Id == s.Id && v.Host == h.Host && h.Location == v.Location {
|
||||
has = true
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
return has
|
||||
}
|
||||
|
||||
type Tunnel struct {
|
||||
Id int
|
||||
Port int
|
||||
ServerIp string
|
||||
Mode string
|
||||
Status bool
|
||||
RunStatus bool
|
||||
Client *Client
|
||||
Ports string
|
||||
Flow *Flow
|
||||
Password string
|
||||
Remark string
|
||||
TargetAddr string
|
||||
NoStore bool
|
||||
LocalPath string
|
||||
StripPre string
|
||||
Target *Target
|
||||
MultiAccount *MultiAccount
|
||||
Health
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type Health struct {
|
||||
HealthCheckTimeout int
|
||||
HealthMaxFail int
|
||||
HealthCheckInterval int
|
||||
HealthNextTime time.Time
|
||||
HealthMap map[string]int
|
||||
HttpHealthUrl string
|
||||
HealthRemoveArr []string
|
||||
HealthCheckType string
|
||||
HealthCheckTarget string
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type Host struct {
|
||||
Id int
|
||||
Host string //host
|
||||
HeaderChange string //header change
|
||||
HostChange string //host change
|
||||
Location string //url router
|
||||
Remark string //remark
|
||||
Scheme string //http https all
|
||||
CertFilePath string
|
||||
KeyFilePath string
|
||||
NoStore bool
|
||||
IsClose bool
|
||||
Flow *Flow
|
||||
Client *Client
|
||||
Target *Target //目标
|
||||
Health `json:"-"`
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type Target struct {
|
||||
nowIndex int
|
||||
TargetStr string
|
||||
TargetArr []string
|
||||
LocalProxy bool
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type MultiAccount struct {
|
||||
AccountMap map[string]string // multi account and pwd
|
||||
}
|
||||
|
||||
func (s *Target) GetRandomTarget() (string, error) {
|
||||
if s.TargetArr == nil {
|
||||
s.TargetArr = strings.Split(s.TargetStr, "\n")
|
||||
}
|
||||
if len(s.TargetArr) == 1 {
|
||||
return s.TargetArr[0], nil
|
||||
}
|
||||
if len(s.TargetArr) == 0 {
|
||||
return "", errors.New("all inward-bending targets are offline")
|
||||
}
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
if s.nowIndex >= len(s.TargetArr)-1 {
|
||||
s.nowIndex = -1
|
||||
}
|
||||
s.nowIndex++
|
||||
return s.TargetArr[s.nowIndex], nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// A data structure to hold a key/value pair.
|
||||
type Pair struct {
|
||||
key string //sort key
|
||||
cId int
|
||||
order string
|
||||
clientFlow *Flow
|
||||
}
|
||||
|
||||
// A slice of Pairs that implements sort.Interface to sort by Value.
|
||||
type PairList []*Pair
|
||||
|
||||
func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
func (p PairList) Len() int { return len(p) }
|
||||
func (p PairList) Less(i, j int) bool {
|
||||
if p[i].order == "desc" {
|
||||
return reflect.ValueOf(*p[i].clientFlow).FieldByName(p[i].key).Int() < reflect.ValueOf(*p[j].clientFlow).FieldByName(p[j].key).Int()
|
||||
}
|
||||
return reflect.ValueOf(*p[i].clientFlow).FieldByName(p[i].key).Int() > reflect.ValueOf(*p[j].clientFlow).FieldByName(p[j].key).Int()
|
||||
}
|
||||
|
||||
// A function to turn a map into a PairList, then sort and return it.
|
||||
func sortClientByKey(m sync.Map, sortKey, order string) (res []int) {
|
||||
p := make(PairList, 0)
|
||||
m.Range(func(key, value interface{}) bool {
|
||||
p = append(p, &Pair{sortKey, value.(*Client).Id, order, value.(*Client).Flow})
|
||||
return true
|
||||
})
|
||||
sort.Sort(p)
|
||||
for _, v := range p {
|
||||
res = append(res, v.cId)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package goroutine
|
||||
|
||||
import (
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type connGroup struct {
|
||||
src io.ReadWriteCloser
|
||||
dst io.ReadWriteCloser
|
||||
wg *sync.WaitGroup
|
||||
n *int64
|
||||
}
|
||||
|
||||
func newConnGroup(dst, src io.ReadWriteCloser, wg *sync.WaitGroup, n *int64) connGroup {
|
||||
return connGroup{
|
||||
src: src,
|
||||
dst: dst,
|
||||
wg: wg,
|
||||
n: n,
|
||||
}
|
||||
}
|
||||
|
||||
func copyConnGroup(group interface{}) {
|
||||
cg, ok := group.(connGroup)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var err error
|
||||
*cg.n, err = common.CopyBuffer(cg.dst, cg.src)
|
||||
if err != nil {
|
||||
cg.src.Close()
|
||||
cg.dst.Close()
|
||||
//logs.Warn("close npc by copy from nps", err, c.connId)
|
||||
}
|
||||
cg.wg.Done()
|
||||
}
|
||||
|
||||
type Conns struct {
|
||||
conn1 io.ReadWriteCloser // mux connection
|
||||
conn2 net.Conn // outside connection
|
||||
flow *file.Flow
|
||||
}
|
||||
|
||||
func NewConns(c1 io.ReadWriteCloser, c2 net.Conn, flow *file.Flow) Conns {
|
||||
return Conns{
|
||||
conn1: c1,
|
||||
conn2: c2,
|
||||
flow: flow,
|
||||
}
|
||||
}
|
||||
|
||||
func copyConns(group interface{}) {
|
||||
conns := group.(Conns)
|
||||
wg := new(sync.WaitGroup)
|
||||
wg.Add(2)
|
||||
var in, out int64
|
||||
_ = connCopyPool.Invoke(newConnGroup(conns.conn1, conns.conn2, wg, &in))
|
||||
// outside to mux : incoming
|
||||
_ = connCopyPool.Invoke(newConnGroup(conns.conn2, conns.conn1, wg, &out))
|
||||
// mux to outside : outgoing
|
||||
wg.Wait()
|
||||
if conns.flow != nil {
|
||||
conns.flow.Add(in, out)
|
||||
}
|
||||
}
|
||||
|
||||
var connCopyPool, _ = ants.NewPoolWithFunc(200000, copyConnGroup, ants.WithNonblocking(false))
|
||||
var CopyConnsPool, _ = ants.NewPoolWithFunc(100000, copyConns, ants.WithNonblocking(false))
|
||||
@@ -1,201 +0,0 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
configPath = flag.String("config", "config.json", "配置文件路径")
|
||||
TcpPort = flag.Int("tcpport", 8284, "客户端与服务端通信端口")
|
||||
httpPort = flag.Int("httpport", 8024, "对外监听的端口")
|
||||
rpMode = flag.String("mode", "client", "启动模式")
|
||||
tunnelTarget = flag.String("target", "10.1.50.203:80", "远程目标")
|
||||
verifyKey = flag.String("vkey", "", "验证密钥")
|
||||
u = flag.String("u", "", "验证用户名(socks5和web)")
|
||||
p = flag.String("p", "", "验证密码(socks5和web)")
|
||||
compress = flag.String("compress", "", "数据压缩方式(snappy)")
|
||||
serverAddr = flag.String("server", "", "服务器地址ip:端口")
|
||||
crypt = flag.String("crypt", "false", "是否加密(true|false)")
|
||||
mux = flag.String("mux", "false", "是否TCP多路复用(true|false)")
|
||||
config Config
|
||||
err error
|
||||
RunList map[string]interface{} //运行中的任务
|
||||
bridge *Tunnel
|
||||
CsvDb *Csv
|
||||
)
|
||||
|
||||
const cryptKey = "1234567812345678"
|
||||
|
||||
func init() {
|
||||
RunList = make(map[string]interface{})
|
||||
}
|
||||
|
||||
func InitClient() {
|
||||
flag.Parse()
|
||||
if *rpMode == "client" {
|
||||
go func() {
|
||||
http.ListenAndServe("0.0.0.0:8899", nil)
|
||||
}()
|
||||
JsonParse := NewJsonStruct()
|
||||
if config, err = JsonParse.Load(*configPath); err != nil {
|
||||
log.Println("配置文件加载失败")
|
||||
}
|
||||
stop := make(chan int)
|
||||
for _, v := range strings.Split(*verifyKey, ",") {
|
||||
log.Println("客户端启动,连接:", *serverAddr, " 验证令牌:", v)
|
||||
go NewRPClient(*serverAddr, 1, v).Start()
|
||||
}
|
||||
<-stop
|
||||
}
|
||||
}
|
||||
func InitMode() {
|
||||
flag.Parse()
|
||||
if *rpMode == "client" {
|
||||
go func() {
|
||||
http.ListenAndServe("0.0.0.0:8899", nil)
|
||||
}()
|
||||
JsonParse := NewJsonStruct()
|
||||
if config, err = JsonParse.Load(*configPath); err != nil {
|
||||
log.Println("配置文件加载失败")
|
||||
}
|
||||
stop := make(chan int)
|
||||
for _, v := range strings.Split(*verifyKey, ",") {
|
||||
log.Println("客户端启动,连接:", *serverAddr, " 验证令牌:", v)
|
||||
go NewRPClient(*serverAddr, 1, v).Start()
|
||||
}
|
||||
<-stop
|
||||
} else {
|
||||
bridge = newTunnel(*TcpPort)
|
||||
if err := bridge.StartTunnel(); err != nil {
|
||||
log.Fatalln("服务端开启失败", err)
|
||||
}
|
||||
log.Println("服务端启动,监听tcp服务端端口:", *TcpPort)
|
||||
cnf := ServerConfig{
|
||||
TcpPort: *httpPort,
|
||||
Mode: *rpMode,
|
||||
Target: *tunnelTarget,
|
||||
VerifyKey: *verifyKey,
|
||||
U: *u,
|
||||
P: *p,
|
||||
Compress: *compress,
|
||||
Start: 0,
|
||||
IsRun: 0,
|
||||
ClientStatus: 0,
|
||||
Crypt: GetBoolByStr(*crypt),
|
||||
Mux: GetBoolByStr(*mux),
|
||||
CompressEncode: 0,
|
||||
CompressDecode: 0,
|
||||
}
|
||||
cnf.CompressDecode, cnf.CompressEncode = getCompressType(cnf.Compress)
|
||||
if svr := newMode(bridge, &cnf);
|
||||
svr != nil {
|
||||
reflect.ValueOf(svr).MethodByName("Start").Call(nil)
|
||||
} else {
|
||||
log.Fatalln("启动模式不正确")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//从csv文件中恢复任务
|
||||
func InitFromCsv() {
|
||||
for _, v := range CsvDb.Tasks {
|
||||
if v.Start == 1 {
|
||||
log.Println(""+
|
||||
"启动模式:", v.Mode, "监听端口:", v.TcpPort, "客户端令牌:", v.VerifyKey)
|
||||
AddTask(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newMode(bridge *Tunnel, config *ServerConfig) interface{} {
|
||||
switch config.Mode {
|
||||
case "httpServer":
|
||||
return NewHttpModeServer(bridge, config)
|
||||
case "tunnelServer":
|
||||
return NewTunnelModeServer(ProcessTunnel, bridge, config)
|
||||
case "socks5Server":
|
||||
return NewSock5ModeServer(bridge, config)
|
||||
case "httpProxyServer":
|
||||
return NewTunnelModeServer(ProcessHttp, bridge, config)
|
||||
case "udpServer":
|
||||
return NewUdpModeServer(bridge, config)
|
||||
case "webServer":
|
||||
InitCsvDb()
|
||||
return NewWebServer(bridge)
|
||||
case "hostServer":
|
||||
return NewHostServer(config)
|
||||
case "httpHostServer":
|
||||
return NewTunnelModeServer(ProcessHost, bridge, config)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func StopServer(cFlag string) error {
|
||||
if v, ok := RunList[cFlag]; ok {
|
||||
reflect.ValueOf(v).MethodByName("Close").Call(nil)
|
||||
delete(RunList, cFlag)
|
||||
if *verifyKey == "" { //多客户端模式关闭相关隧道
|
||||
bridge.DelClientSignal(cFlag)
|
||||
bridge.DelClientTunnel(cFlag)
|
||||
}
|
||||
if t, err := CsvDb.GetTask(cFlag); err != nil {
|
||||
return err
|
||||
} else {
|
||||
t.Start = 0
|
||||
CsvDb.UpdateTask(t)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return errors.New("未在运行中")
|
||||
}
|
||||
|
||||
func AddTask(t *ServerConfig) error {
|
||||
t.CompressDecode, t.CompressEncode = getCompressType(t.Compress)
|
||||
if svr := newMode(bridge, t); svr != nil {
|
||||
RunList[t.VerifyKey] = svr
|
||||
go func() {
|
||||
err := reflect.ValueOf(svr).MethodByName("Start").Call(nil)[0]
|
||||
if err.Interface() != nil {
|
||||
log.Println("客户端", t.VerifyKey, "启动失败,错误:", err)
|
||||
delete(RunList, t.VerifyKey)
|
||||
}
|
||||
}()
|
||||
} else {
|
||||
return errors.New("启动模式不正确")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func StartTask(vKey string) error {
|
||||
if t, err := CsvDb.GetTask(vKey); err != nil {
|
||||
return err
|
||||
} else {
|
||||
AddTask(t)
|
||||
t.Start = 1
|
||||
CsvDb.UpdateTask(t)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DelTask(vKey string) error {
|
||||
if err := StopServer(vKey); err != nil {
|
||||
return err
|
||||
}
|
||||
return CsvDb.DelTask(vKey)
|
||||
}
|
||||
|
||||
func InitCsvDb() *Csv {
|
||||
var once sync.Once
|
||||
once.Do(func() {
|
||||
CsvDb = NewCsv( bridge, RunList)
|
||||
CsvDb.Init()
|
||||
})
|
||||
return CsvDb
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
package install
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/c4milo/unpackit"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func UpdateNps() {
|
||||
destPath := downloadLatest("server")
|
||||
//复制文件到对应目录
|
||||
copyStaticFile(destPath, "nps")
|
||||
fmt.Println("Update completed, please restart")
|
||||
}
|
||||
|
||||
func UpdateNpc() {
|
||||
destPath := downloadLatest("client")
|
||||
//复制文件到对应目录
|
||||
copyStaticFile(destPath, "npc")
|
||||
fmt.Println("Update completed, please restart")
|
||||
}
|
||||
|
||||
type release struct {
|
||||
TagName string `json:"tag_name"`
|
||||
}
|
||||
|
||||
func downloadLatest(bin string) string {
|
||||
// get version
|
||||
data, err := http.Get("https://api.github.com/repos/cnlh/nps/releases/latest")
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
b, err := ioutil.ReadAll(data.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
rl := new(release)
|
||||
json.Unmarshal(b, &rl)
|
||||
version := rl.TagName
|
||||
fmt.Println("the latest version is", version)
|
||||
filename := runtime.GOOS + "_" + runtime.GOARCH + "_" + bin + ".tar.gz"
|
||||
// download latest package
|
||||
downloadUrl := fmt.Sprintf("https://github.com/cnlh/nps/releases/download/%s/%s", version, filename)
|
||||
fmt.Println("download package from ", downloadUrl)
|
||||
resp, err := http.Get(downloadUrl)
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
destPath, err := unpackit.Unpack(resp.Body, "")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if bin == "server" {
|
||||
destPath = strings.Replace(destPath, "/web", "", -1)
|
||||
destPath = strings.Replace(destPath, `\web`, "", -1)
|
||||
destPath = strings.Replace(destPath, "/views", "", -1)
|
||||
destPath = strings.Replace(destPath, `\views`, "", -1)
|
||||
} else {
|
||||
destPath = strings.Replace(destPath, `\conf`, "", -1)
|
||||
destPath = strings.Replace(destPath, "/conf", "", -1)
|
||||
}
|
||||
return destPath
|
||||
}
|
||||
|
||||
func copyStaticFile(srcPath, bin string) string {
|
||||
path := common.GetInstallPath()
|
||||
if bin == "nps" {
|
||||
//复制文件到对应目录
|
||||
if err := CopyDir(filepath.Join(srcPath, "web", "views"), filepath.Join(path, "web", "views")); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
chMod(filepath.Join(path, "web", "views"), 0766)
|
||||
if err := CopyDir(filepath.Join(srcPath, "web", "static"), filepath.Join(path, "web", "static")); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
chMod(filepath.Join(path, "web", "static"), 0766)
|
||||
}
|
||||
binPath, _ := filepath.Abs(os.Args[0])
|
||||
if !common.IsWindows() {
|
||||
if _, err := copyFile(filepath.Join(srcPath, bin), "/usr/bin/"+bin); err != nil {
|
||||
if _, err := copyFile(filepath.Join(srcPath, bin), "/usr/local/bin/"+bin); err != nil {
|
||||
log.Fatalln(err)
|
||||
} else {
|
||||
copyFile(filepath.Join(srcPath, "nps"), "/usr/local/bin/"+bin+"-update")
|
||||
binPath = "/usr/local/bin/" + bin
|
||||
}
|
||||
} else {
|
||||
copyFile(filepath.Join(srcPath, "nps"), "/usr/bin/"+bin+"-update")
|
||||
binPath = "/usr/bin/" + bin
|
||||
}
|
||||
} else {
|
||||
copyFile(filepath.Join(srcPath, bin+".exe"), filepath.Join(common.GetAppPath(), bin+"-update.exe"))
|
||||
copyFile(filepath.Join(srcPath, bin+".exe"), filepath.Join(common.GetAppPath(), bin+".exe"))
|
||||
}
|
||||
chMod(binPath, 0755)
|
||||
return binPath
|
||||
}
|
||||
|
||||
func InstallNpc() {
|
||||
path := common.GetInstallPath()
|
||||
if !common.FileExists(path) {
|
||||
err := os.Mkdir(path, 0755)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
copyStaticFile(common.GetAppPath(), "npc")
|
||||
}
|
||||
|
||||
func InstallNps() string {
|
||||
path := common.GetInstallPath()
|
||||
if common.FileExists(path) {
|
||||
MkidrDirAll(path, "web/static", "web/views")
|
||||
} else {
|
||||
MkidrDirAll(path, "conf", "web/static", "web/views")
|
||||
// not copy config if the config file is exist
|
||||
if err := CopyDir(filepath.Join(common.GetAppPath(), "conf"), filepath.Join(path, "conf")); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
chMod(filepath.Join(path, "conf"), 0766)
|
||||
}
|
||||
binPath := copyStaticFile(common.GetAppPath(), "nps")
|
||||
log.Println("install ok!")
|
||||
log.Println("Static files and configuration files in the current directory will be useless")
|
||||
log.Println("The new configuration file is located in", path, "you can edit them")
|
||||
if !common.IsWindows() {
|
||||
log.Println(`You can start with:
|
||||
nps start|stop|restart|uninstall|update or nps-update update
|
||||
anywhere!`)
|
||||
} else {
|
||||
log.Println(`You can copy executable files to any directory and start working with:
|
||||
nps.exe start|stop|restart|uninstall|update or nps-update.exe update
|
||||
now!`)
|
||||
}
|
||||
chMod(common.GetLogPath(), 0777)
|
||||
return binPath
|
||||
}
|
||||
func MkidrDirAll(path string, v ...string) {
|
||||
for _, item := range v {
|
||||
if err := os.MkdirAll(filepath.Join(path, item), 0755); err != nil {
|
||||
log.Fatalf("Failed to create directory %s error:%s", path, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CopyDir(srcPath string, destPath string) error {
|
||||
//检测目录正确性
|
||||
if srcInfo, err := os.Stat(srcPath); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return err
|
||||
} else {
|
||||
if !srcInfo.IsDir() {
|
||||
e := errors.New("SrcPath is not the right directory!")
|
||||
return e
|
||||
}
|
||||
}
|
||||
if destInfo, err := os.Stat(destPath); err != nil {
|
||||
return err
|
||||
} else {
|
||||
if !destInfo.IsDir() {
|
||||
e := errors.New("DestInfo is not the right directory!")
|
||||
return e
|
||||
}
|
||||
}
|
||||
err := filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error {
|
||||
if f == nil {
|
||||
return err
|
||||
}
|
||||
if !f.IsDir() {
|
||||
destNewPath := strings.Replace(path, srcPath, destPath, -1)
|
||||
log.Println("copy file ::" + path + " to " + destNewPath)
|
||||
copyFile(path, destNewPath)
|
||||
if !common.IsWindows() {
|
||||
chMod(destNewPath, 0766)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
//生成目录并拷贝文件
|
||||
func copyFile(src, dest string) (w int64, err error) {
|
||||
srcFile, err := os.Open(src)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer srcFile.Close()
|
||||
//分割path目录
|
||||
destSplitPathDirs := strings.Split(dest, string(filepath.Separator))
|
||||
|
||||
//检测时候存在目录
|
||||
destSplitPath := ""
|
||||
for index, dir := range destSplitPathDirs {
|
||||
if index < len(destSplitPathDirs)-1 {
|
||||
destSplitPath = destSplitPath + dir + string(filepath.Separator)
|
||||
b, _ := pathExists(destSplitPath)
|
||||
if b == false {
|
||||
log.Println("mkdir:" + destSplitPath)
|
||||
//创建目录
|
||||
err := os.Mkdir(destSplitPath, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
dstFile, err := os.Create(dest)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer dstFile.Close()
|
||||
|
||||
return io.Copy(dstFile, srcFile)
|
||||
}
|
||||
|
||||
//检测文件夹路径时候存在
|
||||
func pathExists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
func chMod(name string, mode os.FileMode) {
|
||||
if !common.IsWindows() {
|
||||
os.Chmod(name, mode)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package mux
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
)
|
||||
|
||||
//write bytes with int32 length
|
||||
func WriteLenBytes(buf []byte, w io.Writer) (int, error) {
|
||||
raw := bytes.NewBuffer([]byte{})
|
||||
if err := binary.Write(raw, binary.LittleEndian, int32(len(buf))); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err := binary.Write(raw, binary.LittleEndian, buf); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return w.Write(raw.Bytes())
|
||||
}
|
||||
|
||||
//read bytes by length
|
||||
func ReadLenBytes(buf []byte, r io.Reader) (int, error) {
|
||||
var l uint32
|
||||
var err error
|
||||
if binary.Read(r, binary.LittleEndian, &l) != nil {
|
||||
return 0, err
|
||||
}
|
||||
if _, err = io.ReadFull(r, buf[:l]); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(l), nil
|
||||
}
|
||||