Skip to content

Commit

Permalink
feat!: change tcp standard format from tcp:example.com:80 to tcp://ex…
Browse files Browse the repository at this point in the history
…ample.com:80
  • Loading branch information
macrat committed May 1, 2021
1 parent d0f59d0 commit 398dc1c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ examples:
Connect to TCP and check the service listening or not.

examples:
- `tcp:example.com:3309`
- `tcp://example.com:3309`

#### dns

Expand Down
4 changes: 2 additions & 2 deletions probe/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func TestTargetURLNormalize(t *testing.T) {
{"http-head://example.com/foo/bar?hoge=fuga#piyo", url.URL{Scheme: "http-head", Host: "example.com", Path: "/foo/bar", RawQuery: "hoge=fuga", Fragment: "piyo"}},
{"https-options://example.com/foo/bar?hoge=fuga#piyo", url.URL{Scheme: "https-options", Host: "example.com", Path: "/foo/bar", RawQuery: "hoge=fuga", Fragment: "piyo"}},

{"tcp:example.com:80", url.URL{Scheme: "tcp", Opaque: "example.com:80"}},
{"tcp://example.com:80/foo/bar?hoge=fuga#piyo", url.URL{Scheme: "tcp", Opaque: "example.com:80"}},
{"tcp:example.com:80", url.URL{Scheme: "tcp", Host: "example.com:80"}},
{"tcp://example.com:80/foo/bar?hoge=fuga#piyo", url.URL{Scheme: "tcp", Host: "example.com:80"}},

{"dns:example.com", url.URL{Scheme: "dns", Opaque: "example.com"}},
{"dns://example.com:80/foo/bar?hoge=fuga#piyo", url.URL{Scheme: "dns", Opaque: "example.com"}},
Expand Down
16 changes: 10 additions & 6 deletions probe/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ import (
"github.com/macrat/ayd/store"
)

var (
ErrTCPPortMissing = errors.New("TCP target's port number is required")
)

type TCPProbe struct {
target *url.URL
}

func NewTCPProbe(u *url.URL) (TCPProbe, error) {
p := TCPProbe{&url.URL{Scheme: "tcp", Opaque: u.Opaque}}
if u.Opaque == "" {
p.target.Opaque = u.Host
p := TCPProbe{&url.URL{Scheme: "tcp", Host: u.Host}}
if u.Host == "" {
p.target.Host = u.Opaque
}
if _, _, err := net.SplitHostPort(p.target.Opaque); err != nil {
return TCPProbe{}, err
if port := p.target.Port(); port == "" {
return TCPProbe{}, ErrTCPPortMissing
}
return p, nil
}
Expand All @@ -36,7 +40,7 @@ func (p TCPProbe) Check(ctx context.Context, r Reporter) {
var dialer net.Dialer

st := time.Now()
conn, err := dialer.DialContext(ctx, "tcp", p.target.Opaque)
conn, err := dialer.DialContext(ctx, "tcp", p.target.Host)
d := time.Now().Sub(st)

rec := store.Record{
Expand Down
6 changes: 3 additions & 3 deletions probe/tcp_otheros_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func TestTCPProbe(t *testing.T) {
defer server.Close()

AssertProbe(t, []ProbeTest{
{strings.Replace(server.URL, "http://", "tcp:", 1), store.STATUS_HEALTHY, `(127\.0\.0\.1|\[::1\]):[0-9]+ -> (127\.0\.0\.1|\[::1\]):[0-9]+`},
{"tcp:localhost:56789", store.STATUS_FAILURE, `dial tcp (127\.0\.0\.1|\[::1\]):56789: connect: connection refused`},
{strings.Replace(server.URL, "http://", "tcp://", 1), store.STATUS_HEALTHY, `(127\.0\.0\.1|\[::1\]):[0-9]+ -> (127\.0\.0\.1|\[::1\]):[0-9]+`},
{"tcp://localhost:56789", store.STATUS_FAILURE, `dial tcp (127\.0\.0\.1|\[::1\]):56789: connect: connection refused`},
})

AssertTimeout(t, strings.Replace(server.URL, "http://", "tcp:", 1))
AssertTimeout(t, strings.Replace(server.URL, "http://", "tcp://", 1))
}
6 changes: 3 additions & 3 deletions probe/tcp_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func TestTCPProbe(t *testing.T) {
defer server.Close()

AssertProbe(t, []ProbeTest{
{strings.Replace(server.URL, "http://", "tcp:", 1), store.STATUS_HEALTHY, `(127\.0\.0\.1|\[::1\]):[0-9]+ -> (127\.0\.0\.1|\[::1\]):[0-9]+`},
{"tcp:localhost:56789", store.STATUS_FAILURE, `dial tcp (127\.0\.0\.1|\[::1\]):56789: connectex: No connection could be made because the target machine actively refused it.`},
{strings.Replace(server.URL, "http://", "tcp://", 1), store.STATUS_HEALTHY, `(127\.0\.0\.1|\[::1\]):[0-9]+ -> (127\.0\.0\.1|\[::1\]):[0-9]+`},
{"tcp://localhost:56789", store.STATUS_FAILURE, `dial tcp (127\.0\.0\.1|\[::1\]):56789: connectex: No connection could be made because the target machine actively refused it.`},
})

AssertTimeout(t, strings.Replace(server.URL, "http://", "tcp:", 1))
AssertTimeout(t, strings.Replace(server.URL, "http://", "tcp://", 1))
}

0 comments on commit 398dc1c

Please sign in to comment.