Skip to content

Commit

Permalink
feat(runner): add reachable dest addr check
Browse files Browse the repository at this point in the history
  • Loading branch information
dwisiswant0 committed Jul 11, 2023
1 parent 6e0fb27 commit 9955366
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/runner/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package runner

import "errors"

var errDestUnreachable = errors.New("destination address unreachable")
5 changes: 5 additions & 0 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import (
)

func New(opt *common.Options) error {
reachable := isReachable(opt.Destination, 5*time.Second)
if !reachable {
return errDestUnreachable
}

tun, err := tunnel.NewTunnel(opt.Port, opt.Destination, opt.Config.Path, opt.Config.Format)
if err != nil {
return err
Expand Down
17 changes: 17 additions & 0 deletions internal/runner/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package runner

import (
"net"
"time"
)

func isReachable(dest string, timeout time.Duration) bool {
conn, err := net.DialTimeout("tcp", dest, timeout)
if err != nil {
return false
}

defer conn.Close()

return true
}

0 comments on commit 9955366

Please sign in to comment.