Skip to content

Commit

Permalink
[update] 端口连通性检查
Browse files Browse the repository at this point in the history
  • Loading branch information
lazydog28 committed Aug 20, 2024
1 parent cf069cd commit 4d7217e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
dist
2 changes: 1 addition & 1 deletion natter/natter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Start(ctx context.Context, forwardAddr *net.TCPAddr) (err error) {
return
default:
forward.KeepAlive()
if !TestPort(rAddr.Port) {
if !TestPort(rAddr) {
logger.Error("公网端口不可达")
forward.StopForward()
err = Start(ctx, forwardAddr)
Expand Down
18 changes: 15 additions & 3 deletions natter/portTest.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,24 @@ func testTransmission(port int) bool {
return bodyStr == "1"
}

func TestPort(port int) bool {
func localTestAddr(rAddr *net.TCPAddr) bool {
conn, err := net.DialTimeout("tcp4", rAddr.String(), time.Second*3)
if err != nil {
return false
}
c(conn)
return true
}

func TestPort(rAddr *net.TCPAddr) bool {
for i := 0; i < 3; i++ {
if testIfConfigCo(port) {
if testIfConfigCo(rAddr.Port) {
return true
}
if testTransmission(rAddr.Port) {
return true
}
if testTransmission(port) {
if localTestAddr(rAddr) {
return true
}
time.Sleep(1 * time.Second)
Expand Down

0 comments on commit 4d7217e

Please sign in to comment.