Skip to content

Commit

Permalink
add sigterm/sigint handler to handle iptables clear under linux for tcp
Browse files Browse the repository at this point in the history
emulation
  • Loading branch information
xtaci committed Aug 20, 2024
1 parent 9066f71 commit 48f9ce7
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 18 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/xtaci/kcp-go/v5 v5.6.14
github.com/xtaci/qpp v1.1.17
github.com/xtaci/smux v1.5.26
github.com/xtaci/tcpraw v1.2.29
github.com/xtaci/tcpraw v1.2.30
golang.org/x/crypto v0.26.0
)

Expand All @@ -28,6 +28,8 @@ require (
golang.org/x/sys v0.24.0 // indirect
)

//replace github.com/xtaci/tcpraw => /home/xtaci/tcpraw

go 1.22.3

toolchain go1.23.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ github.com/xtaci/tcpraw v1.2.28 h1:fRZp0wE027xw/dRjq4wb/vDUxTPeBfenRv5Lmz/3zHg=
github.com/xtaci/tcpraw v1.2.28/go.mod h1:T1blYD2EDkLneb+HtxddnzX38SoC9BG537EhkXeaT2k=
github.com/xtaci/tcpraw v1.2.29 h1:iVk1b2XiXNvIrMgtB3CkHCB/CigYXnG0tvFmOLUwCI8=
github.com/xtaci/tcpraw v1.2.29/go.mod h1:T1blYD2EDkLneb+HtxddnzX38SoC9BG537EhkXeaT2k=
github.com/xtaci/tcpraw v1.2.30 h1:JZlQaxcnSK0z827SPvV8bqqrv9SJdnQnnwX8Q7POFoM=
github.com/xtaci/tcpraw v1.2.30/go.mod h1:T1blYD2EDkLneb+HtxddnzX38SoC9BG537EhkXeaT2k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
28 changes: 28 additions & 0 deletions std/atexit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// The MIT License (MIT)
//
// # Copyright (c) 2024 xtaci
//
// 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:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// 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.

//go:build !linux

package std

func postProcess() {
}
31 changes: 31 additions & 0 deletions std/atexit_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// The MIT License (MIT)
//
// # Copyright (c) 2024 xtaci
//
// 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:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// 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.

//go:build linux

package std

import "github.com/xtaci/tcpraw"

func postProcess() {
tcpraw.IPTablesReset()
}
9 changes: 7 additions & 2 deletions std/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ func init() {

func sigHandler() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGUSR1)
signal.Notify(ch, syscall.SIGUSR1, syscall.SIGTERM, syscall.SIGINT)
signal.Ignore(syscall.SIGPIPE)

for {
switch <-ch {
sig := <-ch
switch sig {
case syscall.SIGUSR1:
log.Printf("KCP SNMP:%+v", kcp.DefaultSnmp.Copy())
case syscall.SIGTERM, syscall.SIGINT:
postProcess()
signal.Stop(ch)
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ github.com/xtaci/qpp
# github.com/xtaci/smux v1.5.26
## explicit; go 1.13
github.com/xtaci/smux
# github.com/xtaci/tcpraw v1.2.29
# github.com/xtaci/tcpraw v1.2.30
## explicit; go 1.13
github.com/xtaci/tcpraw
# golang.org/x/crypto v0.26.0
Expand Down

0 comments on commit 48f9ce7

Please sign in to comment.