Skip to content

Commit 48f9ce7

Browse files
committed
add sigterm/sigint handler to handle iptables clear under linux for tcp
emulation
1 parent 9066f71 commit 48f9ce7

File tree

7 files changed

+73
-18
lines changed

7 files changed

+73
-18
lines changed

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/xtaci/kcp-go/v5 v5.6.14
99
github.com/xtaci/qpp v1.1.17
1010
github.com/xtaci/smux v1.5.26
11-
github.com/xtaci/tcpraw v1.2.29
11+
github.com/xtaci/tcpraw v1.2.30
1212
golang.org/x/crypto v0.26.0
1313
)
1414

@@ -28,6 +28,8 @@ require (
2828
golang.org/x/sys v0.24.0 // indirect
2929
)
3030

31+
//replace github.com/xtaci/tcpraw => /home/xtaci/tcpraw
32+
3133
go 1.22.3
3234

3335
toolchain go1.23.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ github.com/xtaci/tcpraw v1.2.28 h1:fRZp0wE027xw/dRjq4wb/vDUxTPeBfenRv5Lmz/3zHg=
8888
github.com/xtaci/tcpraw v1.2.28/go.mod h1:T1blYD2EDkLneb+HtxddnzX38SoC9BG537EhkXeaT2k=
8989
github.com/xtaci/tcpraw v1.2.29 h1:iVk1b2XiXNvIrMgtB3CkHCB/CigYXnG0tvFmOLUwCI8=
9090
github.com/xtaci/tcpraw v1.2.29/go.mod h1:T1blYD2EDkLneb+HtxddnzX38SoC9BG537EhkXeaT2k=
91+
github.com/xtaci/tcpraw v1.2.30 h1:JZlQaxcnSK0z827SPvV8bqqrv9SJdnQnnwX8Q7POFoM=
92+
github.com/xtaci/tcpraw v1.2.30/go.mod h1:T1blYD2EDkLneb+HtxddnzX38SoC9BG537EhkXeaT2k=
9193
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
9294
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
9395
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=

std/atexit.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// The MIT License (MIT)
2+
//
3+
// # Copyright (c) 2024 xtaci
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
//go:build !linux
24+
25+
package std
26+
27+
func postProcess() {
28+
}

std/atexit_linux.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// The MIT License (MIT)
2+
//
3+
// # Copyright (c) 2024 xtaci
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
//go:build linux
24+
25+
package std
26+
27+
import "github.com/xtaci/tcpraw"
28+
29+
func postProcess() {
30+
tcpraw.IPTablesReset()
31+
}

std/signal.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ func init() {
3939

4040
func sigHandler() {
4141
ch := make(chan os.Signal, 1)
42-
signal.Notify(ch, syscall.SIGUSR1)
42+
signal.Notify(ch, syscall.SIGUSR1, syscall.SIGTERM, syscall.SIGINT)
4343
signal.Ignore(syscall.SIGPIPE)
4444

4545
for {
46-
switch <-ch {
46+
sig := <-ch
47+
switch sig {
4748
case syscall.SIGUSR1:
4849
log.Printf("KCP SNMP:%+v", kcp.DefaultSnmp.Copy())
50+
case syscall.SIGTERM, syscall.SIGINT:
51+
postProcess()
52+
signal.Stop(ch)
53+
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
4954
}
5055
}
5156
}

vendor/github.com/xtaci/tcpraw/signal.go renamed to vendor/github.com/xtaci/tcpraw/clear.go

Lines changed: 1 addition & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ github.com/xtaci/qpp
5353
# github.com/xtaci/smux v1.5.26
5454
## explicit; go 1.13
5555
github.com/xtaci/smux
56-
# github.com/xtaci/tcpraw v1.2.29
56+
# github.com/xtaci/tcpraw v1.2.30
5757
## explicit; go 1.13
5858
github.com/xtaci/tcpraw
5959
# golang.org/x/crypto v0.26.0

0 commit comments

Comments
 (0)