From 76aedde4138d8f90a4fd35fd81c1288d21460ee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 16:44:07 +0800 Subject: [PATCH 01/25] draft for nslookup --- lib/net/network.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 lib/net/network.go diff --git a/lib/net/network.go b/lib/net/network.go new file mode 100644 index 0000000..5df300a --- /dev/null +++ b/lib/net/network.go @@ -0,0 +1,62 @@ +// Package net provides network-related functions for Starlark, inspired by Go's net package and Python's socket module. +package net + +import ( + "context" + "net" + "strings" + "sync" + "time" + + "go.starlark.net/starlark" + "go.starlark.net/starlarkstruct" +) + +// ModuleName defines the expected name for this Module when used in starlark's load() function, eg: load('net', 'tcping') +const ModuleName = "net" + +var ( + once sync.Once + modFunc starlark.StringDict +) + +// LoadModule loads the net module. It is concurrency-safe and idempotent. +func LoadModule() (starlark.StringDict, error) { + once.Do(func() { + modFunc = starlark.StringDict{ + ModuleName: &starlarkstruct.Module{ + Name: ModuleName, + Members: starlark.StringDict{ + //"tcping": starlark.NewBuiltin("net.tcping", tcping), + //"nslookup": starlark.NewBuiltin("net.nslookup", nslookup), + }, + }, + } + }) + return modFunc, nil +} + +func nsLookup(ctx context.Context, domain, dnsServer string, timeout time.Duration) ([]string, error) { + // create a custom resolver if a DNS server is specified + var r *net.Resolver + if dnsServer != "" { + if !strings.Contains(dnsServer, ":") { + // append default DNS port if not specified + dnsServer = net.JoinHostPort(dnsServer, "53") + } + r = &net.Resolver{ + PreferGo: true, + Dial: func(ctx context.Context, network, address string) (net.Conn, error) { + d := net.Dialer{ + Timeout: timeout, + } + return d.DialContext(ctx, "udp", dnsServer) + }, + } + } else { + r = net.DefaultResolver + } + + // perform the DNS lookup + return r.LookupHost(ctx, domain) +} From 30f1bb773a7a2e814b402d4fbc73a615fa34ce0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 17:21:55 +0800 Subject: [PATCH 02/25] for lookup --- lib/net/network.go | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/net/network.go b/lib/net/network.go index 5df300a..c42a480 100644 --- a/lib/net/network.go +++ b/lib/net/network.go @@ -8,6 +8,8 @@ import ( "sync" "time" + "github.com/1set/starlet/dataconv" + tps "github.com/1set/starlet/dataconv/types" "go.starlark.net/starlark" "go.starlark.net/starlarkstruct" ) @@ -16,6 +18,7 @@ import ( const ModuleName = "net" var ( + none = starlark.None once sync.Once modFunc starlark.StringDict ) @@ -25,10 +28,9 @@ func LoadModule() (starlark.StringDict, error) { once.Do(func() { modFunc = starlark.StringDict{ ModuleName: &starlarkstruct.Module{ - Name: ModuleName, + Name: ModuleName, Members: starlark.StringDict{ - //"tcping": starlark.NewBuiltin("net.tcping", tcping), - //"nslookup": starlark.NewBuiltin("net.nslookup", nslookup), + "nslookup": starlark.NewBuiltin(ModuleName+".nslookup", starLookup), }, }, } @@ -36,7 +38,7 @@ func LoadModule() (starlark.StringDict, error) { return modFunc, nil } -func nsLookup(ctx context.Context, domain, dnsServer string, timeout time.Duration) ([]string, error) { +func goLookup(ctx context.Context, domain, dnsServer string, timeout time.Duration) ([]string, error) { // create a custom resolver if a DNS server is specified var r *net.Resolver if dnsServer != "" { @@ -60,3 +62,35 @@ func nsLookup(ctx context.Context, domain, dnsServer string, timeout time.Durati // perform the DNS lookup return r.LookupHost(ctx, domain) } + +func starLookup(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + var ( + domain tps.StringOrBytes + dnsServer tps.NullableStringOrBytes + timeout tps.FloatOrInt = 10 + ) + if err := starlark.UnpackArgs(b.Name(), args, kwargs, "domain", &domain, "dns_server?", &dnsServer, "timeout?", &timeout); err != nil { + return nil, err + } + + // correct timeout value + if timeout <= 0 { + timeout = 10 + } + + // get the context + ctx := dataconv.GetThreadContext(thread) + + // perform the DNS lookup + ips, err := goLookup(ctx, domain.GoString(), dnsServer.GoString(), time.Duration(timeout)*time.Second) + + // return the result + if err != nil { + return none, err + } + var list []starlark.Value + for _, ip := range ips { + list = append(list, starlark.String(ip)) + } + return starlark.NewList(list), nil +} From cc613c815346e70a948dd5b9cac61663b805a9a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 17:45:30 +0800 Subject: [PATCH 03/25] for invalid --- lib/net/network.go | 3 +- lib/net/network_test.go | 61 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 lib/net/network_test.go diff --git a/lib/net/network.go b/lib/net/network.go index c42a480..d4c9dc3 100644 --- a/lib/net/network.go +++ b/lib/net/network.go @@ -3,6 +3,7 @@ package net import ( "context" + "fmt" "net" "strings" "sync" @@ -86,7 +87,7 @@ func starLookup(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tupl // return the result if err != nil { - return none, err + return none, fmt.Errorf("%s: %w", b.Name(), err) } var list []starlark.Value for _, ip := range ips { diff --git a/lib/net/network_test.go b/lib/net/network_test.go new file mode 100644 index 0000000..fa22a3f --- /dev/null +++ b/lib/net/network_test.go @@ -0,0 +1,61 @@ +package net_test + +import ( + "testing" + + itn "github.com/1set/starlet/internal" + "github.com/1set/starlet/lib/net" +) + +func TestLoadModule_Network(t *testing.T) { + tests := []struct { + name string + script string + wantErr string + }{ + { + name: `nslookup: normal`, + script: itn.HereDoc(` + load('net', 'nslookup') + ips = nslookup('bing.com') + print(ips) + assert.true(len(ips) > 0) + `), + }, + { + name: `nslookup: ip`, + script: itn.HereDoc(` + load('net', 'nslookup') + ips = nslookup('8.8.8.8') + print(ips) + assert.true(len(ips) > 0) + `), + }, + { + name: `nslookup: localhost`, + script: itn.HereDoc(` + load('net', 'nslookup') + ips = nslookup('localhost') + print(ips) + assert.true(len(ips) > 0) + `), + }, + { + name: `nslookup: not exists`, + script: itn.HereDoc(` + load('net', 'nslookup') + ips = nslookup('missing.invalid') + `), + wantErr: `no such host`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + res, err := itn.ExecModuleWithErrorTest(t, net.ModuleName, net.LoadModule, tt.script, tt.wantErr, nil) + if (err != nil) != (tt.wantErr != "") { + t.Errorf("net(%q) expects error = '%v', actual error = '%v', result = %v", tt.name, tt.wantErr, err, res) + return + } + }) + } +} From 65250b98fef824cc1dcacf64c66af5206288fe5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 18:18:05 +0800 Subject: [PATCH 04/25] for issues --- lib/net/network_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/net/network_test.go b/lib/net/network_test.go index fa22a3f..ce8a5e7 100644 --- a/lib/net/network_test.go +++ b/lib/net/network_test.go @@ -22,6 +22,24 @@ func TestLoadModule_Network(t *testing.T) { assert.true(len(ips) > 0) `), }, + { + name: `nslookup: normal with dns`, + script: itn.HereDoc(` + load('net', 'nslookup') + ips = nslookup('bing.com', '8.8.8.8') + print(ips) + assert.true(len(ips) > 0) + `), + }, + { + name: `nslookup: normal with dns:port`, + script: itn.HereDoc(` + load('net', 'nslookup') + ips = nslookup('bing.com', '1.1.1.1:53') + print(ips) + assert.true(len(ips) > 0) + `), + }, { name: `nslookup: ip`, script: itn.HereDoc(` @@ -48,6 +66,14 @@ func TestLoadModule_Network(t *testing.T) { `), wantErr: `no such host`, }, + { + name: `nslookup: wrong dns`, + script: itn.HereDoc(` + load('net', 'nslookup') + ips = nslookup('bing.com', '127.0.0.1', timeout=1) + `), + //wantErr: `i/o timeout`, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From fa551dd50a7ab581fd925fe746068c568585717d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 18:29:08 +0800 Subject: [PATCH 05/25] for timeout --- lib/net/network.go | 6 +++++- lib/net/network_test.go | 13 +++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/net/network.go b/lib/net/network.go index d4c9dc3..5b94f48 100644 --- a/lib/net/network.go +++ b/lib/net/network.go @@ -60,8 +60,12 @@ func goLookup(ctx context.Context, domain, dnsServer string, timeout time.Durati r = net.DefaultResolver } + // Create a new context with timeout + ctxWithTimeout, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + // perform the DNS lookup - return r.LookupHost(ctx, domain) + return r.LookupHost(ctxWithTimeout, domain) } func starLookup(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { diff --git a/lib/net/network_test.go b/lib/net/network_test.go index ce8a5e7..eac98a8 100644 --- a/lib/net/network_test.go +++ b/lib/net/network_test.go @@ -22,6 +22,15 @@ func TestLoadModule_Network(t *testing.T) { assert.true(len(ips) > 0) `), }, + { + name: `nslookup: normal with timeout`, + script: itn.HereDoc(` + load('net', 'nslookup') + ips = nslookup('bing.com', timeout=5) + print(ips) + assert.true(len(ips) > 0) + `), + }, { name: `nslookup: normal with dns`, script: itn.HereDoc(` @@ -70,9 +79,9 @@ func TestLoadModule_Network(t *testing.T) { name: `nslookup: wrong dns`, script: itn.HereDoc(` load('net', 'nslookup') - ips = nslookup('bing.com', '127.0.0.1', timeout=1) + ips = nslookup('bing.com', 'microsoft.com', timeout=1) `), - //wantErr: `i/o timeout`, + wantErr: `i/o timeout`, }, } for _, tt := range tests { From 7eef1fa6fe129a16f46be3416dc1aab48d4f71df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 18:31:32 +0800 Subject: [PATCH 06/25] for test cases --- lib/net/network_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/net/network_test.go b/lib/net/network_test.go index eac98a8..0941abb 100644 --- a/lib/net/network_test.go +++ b/lib/net/network_test.go @@ -83,6 +83,22 @@ func TestLoadModule_Network(t *testing.T) { `), wantErr: `i/o timeout`, }, + { + name: `nslookup: no args`, + script: itn.HereDoc(` + load('net', 'nslookup') + nslookup() + `), + wantErr: `net.nslookup: missing argument for domain`, + }, + { + name: `nslookup: invalid args`, + script: itn.HereDoc(` + load('net', 'nslookup') + nslookup(1, 2, 3) + `), + wantErr: `net.nslookup: for parameter domain: got int, want string or bytes`, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 4851da0f7841b159a76ef43450d3e17694cd257b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 20:23:51 +0800 Subject: [PATCH 07/25] draft for tcp ping --- lib/net/network.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/lib/net/network.go b/lib/net/network.go index 5b94f48..1e64d9b 100644 --- a/lib/net/network.go +++ b/lib/net/network.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "net" + "strconv" "strings" "sync" "time" @@ -99,3 +100,64 @@ func starLookup(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tupl } return starlark.NewList(list), nil } + +// goTCPPing performs a TCP ping to the given address and port. It returns the average round-trip time (RTT) and the packet loss percentage. +func goTCPPing(ctx context.Context, hostname string, port int, count int, timeout, interval time.Duration) (avgRTT time.Duration, lossPercentage float64, err error) { + // set default + avgRTT = 0 + lossPercentage = 100 + + // get the target count + if count <= 0 { + err = fmt.Errorf("count must be greater than 0") + return + } + + // resolve the hostname to an IP address + ips, e := goLookup(ctx, hostname, "", timeout) + if e != nil { + err = e + return + } + if len(ips) == 0 { + err = fmt.Errorf("unable to resolve hostname") + return + } + addr := net.JoinHostPort(ips[0], strconv.Itoa(port)) + + // perform the TCP ping + var ( + totalRTT time.Duration + successCount int + ) + for i := 1; i <= count; i++ { + // dial with timeout + start := time.Now() + conn, e := net.DialTimeout("tcp", addr, timeout) + if e != nil { + continue + } + rtt := time.Since(start) + _ = conn.Close() + + // measure the RTT + totalRTT += rtt + successCount++ + + // apply interval + if i < count { + time.Sleep(interval) + } + } + + // calculate the result + if successCount == 0 { + err = fmt.Errorf("no successful connections") + return + } + + // calculate the average RTT and loss percentage + avgRTT = totalRTT / time.Duration(successCount) + lossPercentage = 100.0 * float64(count-successCount) / float64(count) + return +} From d966c79dc5ebe5ddf2711a82011cb074d1c6e44b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 20:56:53 +0800 Subject: [PATCH 08/25] for checks --- go.sum | 107 --------------------------------------------- lib/net/network.go | 105 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 104 insertions(+), 108 deletions(-) diff --git a/go.sum b/go.sum index ade5c82..e75969e 100644 --- a/go.sum +++ b/go.sum @@ -1,138 +1,31 @@ -cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/1set/starlight v0.1.2 h1:Lf+ktJPLeck5QJLnKGj+brFkBBtitQBWLvXVA0cTcq8= github.com/1set/starlight v0.1.2/go.mod h1:UBovtihT3K/JtaX+Nv/xBmdDk3LW6kr5yzqaYFo4KDQ= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= -github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= -github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE= -github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8= -github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= -github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= -github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= -github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= -github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= -github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= -github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= -github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= -github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= -github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k= -github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/h2so5/here v0.0.0-20200815043652-5e14eb691fae h1:ghqI9EdSyyIL2iuOM9UIGVO7kEYQFVLKAUIFoOea5MY= github.com/h2so5/here v0.0.0-20200815043652-5e14eb691fae/go.mod h1:Q+Ziz4FsuRTHql1UqcQ3iZwl9LcKpi7mVVgn20Rj+IU= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE= github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.starlark.net v0.0.0-20240123142251-f86470692795 h1:LmbG8Pq7KDGkglKVn8VpZOZj6vb9b8nKEGcg9l03epM= go.starlark.net v0.0.0-20240123142251-f86470692795/go.mod h1:LcLNIzVOMp4oV+uusnpk+VU+SzXaJakUuBjoCSWH5dM= -go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= -go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= -go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg= -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/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= -golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= -golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= -golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= -google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= -google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= -google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= -google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= -google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= -google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= -google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= -google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= -google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= -google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/lib/net/network.go b/lib/net/network.go index 1e64d9b..4b121a6 100644 --- a/lib/net/network.go +++ b/lib/net/network.go @@ -10,6 +10,8 @@ import ( "sync" "time" + "github.com/montanaflynn/stats" + "github.com/1set/starlet/dataconv" tps "github.com/1set/starlet/dataconv/types" "go.starlark.net/starlark" @@ -102,7 +104,7 @@ func starLookup(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tupl } // goTCPPing performs a TCP ping to the given address and port. It returns the average round-trip time (RTT) and the packet loss percentage. -func goTCPPing(ctx context.Context, hostname string, port int, count int, timeout, interval time.Duration) (avgRTT time.Duration, lossPercentage float64, err error) { +func goTCPPing2(ctx context.Context, hostname string, port int, count int, timeout, interval time.Duration) (avgRTT time.Duration, lossPercentage float64, err error) { // set default avgRTT = 0 lossPercentage = 100 @@ -161,3 +163,104 @@ func goTCPPing(ctx context.Context, hostname string, port int, count int, timeou lossPercentage = 100.0 * float64(count-successCount) / float64(count) return } + +// goTCPPing performs a TCP ping to the given address and port. It returns a slice of round-trip times (RTTs) for each successful connection attempt and an error if any. +func goTCPPing(ctx context.Context, hostname string, port int, count int, timeout, interval time.Duration) ([]time.Duration, string, error) { + if count <= 0 { + return nil, "", fmt.Errorf("count must be greater than 0") + } + + // resolve the hostname to an IP address + ips, err := goLookup(ctx, hostname, "", timeout) + if err != nil { + return nil, "", err + } + if len(ips) == 0 { + return nil, "", fmt.Errorf("unable to resolve hostname") + } + addr := net.JoinHostPort(ips[0], strconv.Itoa(port)) + + // slice to hold the RTTs of successful pings + var rttDurations []time.Duration + for i := 1; i <= count; i++ { + start := time.Now() + conn, err := net.DialTimeout("tcp", addr, timeout) + if err != nil { + // if the connection fails, continue to the next attempt without adding RTT + continue + } + rtt := time.Since(start) + _ = conn.Close() + + // store the successful RTT, and wait for the interval before the next attempt + rttDurations = append(rttDurations, rtt) + if i < count { + time.Sleep(interval) + } + } + + // if there were no successful connections, return an error. + if len(rttDurations) == 0 { + return nil, addr, fmt.Errorf("no successful connections") + } + + // return the slice of RTTs. + return rttDurations, addr, nil +} + +// starTCPPing performs a TCP ping to the given address and port. It returns the average round-trip time (RTT) and the packet loss percentage. +// for statistics result like: 4 packets transmitted, 4 packets received, 0.0% packet loss, round-trip min/avg/max/stddev = 0.409/0.666/0.773/0.149 ms +func starTCPPing(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + var ( + hostname tps.StringOrBytes + port int = 80 + count int = 4 + timeout tps.FloatOrInt = 10 + interval tps.FloatOrInt = 1 + ) + if err := starlark.UnpackArgs(b.Name(), args, kwargs, "hostname", &hostname, "port?", &port, "count?", &count, "timeout?", &timeout, "interval?", &interval); err != nil { + return nil, err + } + + // correct timeout value + if timeout <= 0 { + timeout = 10 + } + if interval <= 0 { + interval = 1 + } + + // get the context + ctx := dataconv.GetThreadContext(thread) + + // perform the TCP ping + rtts, addr, err := goTCPPing(ctx, hostname.GoString(), port, count, time.Duration(timeout)*time.Second, time.Duration(interval)*time.Second) + + // return the result + if err != nil { + return none, fmt.Errorf("%s: %w", b.Name(), err) + } + + // statistics + vals := make([]float64, len(rtts)) + for i, rtt := range rtts { + vals[i] = float64(rtt) / float64(time.Millisecond) + } + succ := len(rtts) + loss := float64(count-succ) / float64(count) * 100 + avg, _ := stats.Mean(vals) + min, _ := stats.Min(vals) + max, _ := stats.Max(vals) + stddev, _ := stats.StandardDeviation(vals) + sd := starlark.StringDict{ + "hostname": starlark.String(addr), + "total": starlark.MakeInt(count), + "success": starlark.MakeInt(succ), + "loss": starlark.Float(loss), + "min": starlark.Float(min), + "avg": starlark.Float(avg), + "max": starlark.Float(max), + "stddev": starlark.Float(stddev), + } + return starlarkstruct.FromStringDict(starlark.String(`statistics`), sd), nil +} From 1d08c2003cfc007b7ef902597c7bbb8cf67988a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 20:58:35 +0800 Subject: [PATCH 09/25] fix go sum --- go.sum | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/go.sum b/go.sum index e75969e..ade5c82 100644 --- a/go.sum +++ b/go.sum @@ -1,31 +1,138 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/1set/starlight v0.1.2 h1:Lf+ktJPLeck5QJLnKGj+brFkBBtitQBWLvXVA0cTcq8= github.com/1set/starlight v0.1.2/go.mod h1:UBovtihT3K/JtaX+Nv/xBmdDk3LW6kr5yzqaYFo4KDQ= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= +github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k= +github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/h2so5/here v0.0.0-20200815043652-5e14eb691fae h1:ghqI9EdSyyIL2iuOM9UIGVO7kEYQFVLKAUIFoOea5MY= github.com/h2so5/here v0.0.0-20200815043652-5e14eb691fae/go.mod h1:Q+Ziz4FsuRTHql1UqcQ3iZwl9LcKpi7mVVgn20Rj+IU= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE= github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.starlark.net v0.0.0-20240123142251-f86470692795 h1:LmbG8Pq7KDGkglKVn8VpZOZj6vb9b8nKEGcg9l03epM= go.starlark.net v0.0.0-20240123142251-f86470692795/go.mod h1:LcLNIzVOMp4oV+uusnpk+VU+SzXaJakUuBjoCSWH5dM= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= +go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= +go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg= +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/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= From bab66de8d06cb9efcafc356bd991df7ecd3119a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 21:04:10 +0800 Subject: [PATCH 10/25] for ping and test --- lib/net/network.go | 62 +---------------------------------------- lib/net/network_test.go | 10 +++++++ 2 files changed, 11 insertions(+), 61 deletions(-) diff --git a/lib/net/network.go b/lib/net/network.go index 4b121a6..266f742 100644 --- a/lib/net/network.go +++ b/lib/net/network.go @@ -35,6 +35,7 @@ func LoadModule() (starlark.StringDict, error) { Name: ModuleName, Members: starlark.StringDict{ "nslookup": starlark.NewBuiltin(ModuleName+".nslookup", starLookup), + "tcping": starlark.NewBuiltin(ModuleName+".tcping", starTCPPing), }, }, } @@ -103,67 +104,6 @@ func starLookup(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tupl return starlark.NewList(list), nil } -// goTCPPing performs a TCP ping to the given address and port. It returns the average round-trip time (RTT) and the packet loss percentage. -func goTCPPing2(ctx context.Context, hostname string, port int, count int, timeout, interval time.Duration) (avgRTT time.Duration, lossPercentage float64, err error) { - // set default - avgRTT = 0 - lossPercentage = 100 - - // get the target count - if count <= 0 { - err = fmt.Errorf("count must be greater than 0") - return - } - - // resolve the hostname to an IP address - ips, e := goLookup(ctx, hostname, "", timeout) - if e != nil { - err = e - return - } - if len(ips) == 0 { - err = fmt.Errorf("unable to resolve hostname") - return - } - addr := net.JoinHostPort(ips[0], strconv.Itoa(port)) - - // perform the TCP ping - var ( - totalRTT time.Duration - successCount int - ) - for i := 1; i <= count; i++ { - // dial with timeout - start := time.Now() - conn, e := net.DialTimeout("tcp", addr, timeout) - if e != nil { - continue - } - rtt := time.Since(start) - _ = conn.Close() - - // measure the RTT - totalRTT += rtt - successCount++ - - // apply interval - if i < count { - time.Sleep(interval) - } - } - - // calculate the result - if successCount == 0 { - err = fmt.Errorf("no successful connections") - return - } - - // calculate the average RTT and loss percentage - avgRTT = totalRTT / time.Duration(successCount) - lossPercentage = 100.0 * float64(count-successCount) / float64(count) - return -} - // goTCPPing performs a TCP ping to the given address and port. It returns a slice of round-trip times (RTTs) for each successful connection attempt and an error if any. func goTCPPing(ctx context.Context, hostname string, port int, count int, timeout, interval time.Duration) ([]time.Duration, string, error) { if count <= 0 { diff --git a/lib/net/network_test.go b/lib/net/network_test.go index 0941abb..8b551a0 100644 --- a/lib/net/network_test.go +++ b/lib/net/network_test.go @@ -99,6 +99,16 @@ func TestLoadModule_Network(t *testing.T) { `), wantErr: `net.nslookup: for parameter domain: got int, want string or bytes`, }, + { + name: `tcping: normal`, + script: itn.HereDoc(` + load('net', 'tcping') + s = tcping('bing.com') + print(s) + assert.eq(s.total, 4) + assert.true(s.success > 0) + `), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From b649253860f435bb73cc74fbbfa697f47f150002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 21:05:17 +0800 Subject: [PATCH 11/25] ping it now --- lib/net/network_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/net/network_test.go b/lib/net/network_test.go index 8b551a0..bd24def 100644 --- a/lib/net/network_test.go +++ b/lib/net/network_test.go @@ -109,6 +109,16 @@ func TestLoadModule_Network(t *testing.T) { assert.true(s.success > 0) `), }, + { + name: `tcping: faster`, + script: itn.HereDoc(` + load('net', 'tcping') + s = tcping('bing.com', count=10, timeout=5, interval=0.01) + print(s) + assert.eq(s.total, 10) + assert.true(s.success > 0) + `), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 9b280ed2d6a373ec856f5d98abbc52e1ea96b05d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 21:07:13 +0800 Subject: [PATCH 12/25] include --- config.go | 2 ++ lib/net/network.go | 3 +-- module_test.go | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index 7637912..f830ec7 100644 --- a/config.go +++ b/config.go @@ -10,6 +10,7 @@ import ( libhttp "github.com/1set/starlet/lib/http" libjson "github.com/1set/starlet/lib/json" liblog "github.com/1set/starlet/lib/log" + libnet "github.com/1set/starlet/lib/net" libpath "github.com/1set/starlet/lib/path" librand "github.com/1set/starlet/lib/random" libre "github.com/1set/starlet/lib/re" @@ -49,6 +50,7 @@ var allBuiltinModules = ModuleLoaderMap{ libfile.ModuleName: libfile.LoadModule, libhash.ModuleName: libhash.LoadModule, libhttp.ModuleName: libhttp.LoadModule, + libnet.ModuleName: libnet.LoadModule, libjson.ModuleName: libjson.LoadModule, liblog.ModuleName: liblog.LoadModule, libpath.ModuleName: libpath.LoadModule, diff --git a/lib/net/network.go b/lib/net/network.go index 266f742..ca81961 100644 --- a/lib/net/network.go +++ b/lib/net/network.go @@ -10,10 +10,9 @@ import ( "sync" "time" - "github.com/montanaflynn/stats" - "github.com/1set/starlet/dataconv" tps "github.com/1set/starlet/dataconv/types" + "github.com/montanaflynn/stats" "go.starlark.net/starlark" "go.starlark.net/starlarkstruct" ) diff --git a/module_test.go b/module_test.go index cab68ae..5baf44e 100644 --- a/module_test.go +++ b/module_test.go @@ -16,7 +16,7 @@ import ( ) var ( - builtinModules = []string{"atom", "base64", "csv", "file", "go_idiomatic", "hashlib", "http", "json", "log", "math", "path", "random", "re", "runtime", "stats", "string", "struct", "time"} + builtinModules = []string{"atom", "base64", "csv", "file", "go_idiomatic", "hashlib", "http", "json", "log", "math", "net", "path", "random", "re", "runtime", "stats", "string", "struct", "time"} ) func TestListBuiltinModules(t *testing.T) { From 52e8ddcd2a8689046982164f63c95e0c8ffdfd94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 21:18:28 +0800 Subject: [PATCH 13/25] fix test case --- lib/net/network_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/net/network_test.go b/lib/net/network_test.go index bd24def..ba62d58 100644 --- a/lib/net/network_test.go +++ b/lib/net/network_test.go @@ -73,13 +73,13 @@ func TestLoadModule_Network(t *testing.T) { load('net', 'nslookup') ips = nslookup('missing.invalid') `), - wantErr: `no such host`, + wantErr: `missing.invalid`, // mac/win: no such host, linux: server misbehaving }, { name: `nslookup: wrong dns`, script: itn.HereDoc(` load('net', 'nslookup') - ips = nslookup('bing.com', 'microsoft.com', timeout=1) + ips = nslookup('bing.com', 'apple.com', timeout=1) `), wantErr: `i/o timeout`, }, From e057c6849b0b383484998296e042b4d4deccc111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 21:29:16 +0800 Subject: [PATCH 14/25] test coverage --- lib/net/network_test.go | 45 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/net/network_test.go b/lib/net/network_test.go index ba62d58..29d0282 100644 --- a/lib/net/network_test.go +++ b/lib/net/network_test.go @@ -53,7 +53,7 @@ func TestLoadModule_Network(t *testing.T) { name: `nslookup: ip`, script: itn.HereDoc(` load('net', 'nslookup') - ips = nslookup('8.8.8.8') + ips = nslookup('8.8.8.8', timeout=-1) print(ips) assert.true(len(ips) > 0) `), @@ -109,6 +109,17 @@ func TestLoadModule_Network(t *testing.T) { assert.true(s.success > 0) `), }, + { + name: `tcping: abnormal`, + script: itn.HereDoc(` + load('net', 'tcping') + s = tcping('apple.com', count=1, timeout=-5, interval=-2) + print(s) + assert.eq(s.total, 1) + assert.true(s.success > 0) + assert.eq(s.stddev, 0) + `), + }, { name: `tcping: faster`, script: itn.HereDoc(` @@ -119,6 +130,38 @@ func TestLoadModule_Network(t *testing.T) { assert.true(s.success > 0) `), }, + { + name: `tcping: not exists`, + script: itn.HereDoc(` + load('net', 'tcping') + s = tcping('missing.invalid') + `), + wantErr: `missing.invalid`, // mac/win: no such host, linux: server misbehaving + }, + { + name: `tcping: wrong count`, + script: itn.HereDoc(` + load('net', 'tcping') + s = tcping('bing.com', count=0) + `), + wantErr: `net.tcping: count must be greater than 0`, + }, + { + name: `tcping: no args`, + script: itn.HereDoc(` + load('net', 'tcping') + tcping() + `), + wantErr: `net.tcping: missing argument for hostname`, + }, + { + name: `tcping: invalid args`, + script: itn.HereDoc(` + load('net', 'tcping') + tcping(123) + `), + wantErr: `net.tcping: for parameter hostname: got int, want string or bytes`, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 4d1563aa0f7a7c6321a5c9825036ab6c65312ef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 21:57:50 +0800 Subject: [PATCH 15/25] fix lint --- lib/net/network.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/net/network.go b/lib/net/network.go index ca81961..bbd6bae 100644 --- a/lib/net/network.go +++ b/lib/net/network.go @@ -120,11 +120,11 @@ func goTCPPing(ctx context.Context, hostname string, port int, count int, timeou addr := net.JoinHostPort(ips[0], strconv.Itoa(port)) // slice to hold the RTTs of successful pings - var rttDurations []time.Duration + rttDurations := make([]time.Duration, 0, count) for i := 1; i <= count; i++ { start := time.Now() - conn, err := net.DialTimeout("tcp", addr, timeout) - if err != nil { + conn, e := net.DialTimeout("tcp", addr, timeout) + if e != nil { // if the connection fails, continue to the next attempt without adding RTT continue } @@ -152,8 +152,8 @@ func goTCPPing(ctx context.Context, hostname string, port int, count int, timeou func starTCPPing(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { var ( hostname tps.StringOrBytes - port int = 80 - count int = 4 + port = 80 + count = 4 timeout tps.FloatOrInt = 10 interval tps.FloatOrInt = 1 ) From 5dd05ebf754bcfe7033df1265096f79e0917dcac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 23:01:43 +0800 Subject: [PATCH 16/25] skip win --- lib/net/network_test.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/net/network_test.go b/lib/net/network_test.go index 29d0282..9a7b121 100644 --- a/lib/net/network_test.go +++ b/lib/net/network_test.go @@ -1,6 +1,7 @@ package net_test import ( + "runtime" "testing" itn "github.com/1set/starlet/internal" @@ -8,10 +9,12 @@ import ( ) func TestLoadModule_Network(t *testing.T) { + isOnWindows := runtime.GOOS == "windows" tests := []struct { - name string - script string - wantErr string + name string + script string + wantErr string + skipWindows bool }{ { name: `nslookup: normal`, @@ -79,9 +82,10 @@ func TestLoadModule_Network(t *testing.T) { name: `nslookup: wrong dns`, script: itn.HereDoc(` load('net', 'nslookup') - ips = nslookup('bing.com', 'apple.com', timeout=1) + ips = nslookup('bing.com', 'microsoft.com', timeout=1) `), - wantErr: `i/o timeout`, + wantErr: `i/o timeout`, + skipWindows: true, }, { name: `nslookup: no args`, @@ -113,7 +117,7 @@ func TestLoadModule_Network(t *testing.T) { name: `tcping: abnormal`, script: itn.HereDoc(` load('net', 'tcping') - s = tcping('apple.com', count=1, timeout=-5, interval=-2) + s = tcping('bing.com', count=1, timeout=-5, interval=-2) print(s) assert.eq(s.total, 1) assert.true(s.success > 0) @@ -165,6 +169,10 @@ func TestLoadModule_Network(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + if isOnWindows && tt.skipWindows { + t.Skipf("Skip test on Windows") + return + } res, err := itn.ExecModuleWithErrorTest(t, net.ModuleName, net.LoadModule, tt.script, tt.wantErr, nil) if (err != nil) != (tt.wantErr != "") { t.Errorf("net(%q) expects error = '%v', actual error = '%v', result = %v", tt.name, tt.wantErr, err, res) From 44755e4b72940c15f39979f2f3ccde658d3855a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 22 Jul 2024 23:10:02 +0800 Subject: [PATCH 17/25] add notes for test case --- lib/net/network_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/net/network_test.go b/lib/net/network_test.go index 9a7b121..e9dac04 100644 --- a/lib/net/network_test.go +++ b/lib/net/network_test.go @@ -85,7 +85,7 @@ func TestLoadModule_Network(t *testing.T) { ips = nslookup('bing.com', 'microsoft.com', timeout=1) `), wantErr: `i/o timeout`, - skipWindows: true, + skipWindows: true, // on Windows 2022 with Go 1.18.10, it returns results from the default DNS server }, { name: `nslookup: no args`, From 21cce1e8a0f6b158fc88dd21f20fb66080dd4672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Tue, 23 Jul 2024 00:13:48 +0800 Subject: [PATCH 18/25] refactor for httping --- lib/net/network.go | 103 --------------------------- lib/net/ping.go | 171 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+), 103 deletions(-) create mode 100644 lib/net/ping.go diff --git a/lib/net/network.go b/lib/net/network.go index bbd6bae..0ef4994 100644 --- a/lib/net/network.go +++ b/lib/net/network.go @@ -5,14 +5,12 @@ import ( "context" "fmt" "net" - "strconv" "strings" "sync" "time" "github.com/1set/starlet/dataconv" tps "github.com/1set/starlet/dataconv/types" - "github.com/montanaflynn/stats" "go.starlark.net/starlark" "go.starlark.net/starlarkstruct" ) @@ -102,104 +100,3 @@ func starLookup(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tupl } return starlark.NewList(list), nil } - -// goTCPPing performs a TCP ping to the given address and port. It returns a slice of round-trip times (RTTs) for each successful connection attempt and an error if any. -func goTCPPing(ctx context.Context, hostname string, port int, count int, timeout, interval time.Duration) ([]time.Duration, string, error) { - if count <= 0 { - return nil, "", fmt.Errorf("count must be greater than 0") - } - - // resolve the hostname to an IP address - ips, err := goLookup(ctx, hostname, "", timeout) - if err != nil { - return nil, "", err - } - if len(ips) == 0 { - return nil, "", fmt.Errorf("unable to resolve hostname") - } - addr := net.JoinHostPort(ips[0], strconv.Itoa(port)) - - // slice to hold the RTTs of successful pings - rttDurations := make([]time.Duration, 0, count) - for i := 1; i <= count; i++ { - start := time.Now() - conn, e := net.DialTimeout("tcp", addr, timeout) - if e != nil { - // if the connection fails, continue to the next attempt without adding RTT - continue - } - rtt := time.Since(start) - _ = conn.Close() - - // store the successful RTT, and wait for the interval before the next attempt - rttDurations = append(rttDurations, rtt) - if i < count { - time.Sleep(interval) - } - } - - // if there were no successful connections, return an error. - if len(rttDurations) == 0 { - return nil, addr, fmt.Errorf("no successful connections") - } - - // return the slice of RTTs. - return rttDurations, addr, nil -} - -// starTCPPing performs a TCP ping to the given address and port. It returns the average round-trip time (RTT) and the packet loss percentage. -// for statistics result like: 4 packets transmitted, 4 packets received, 0.0% packet loss, round-trip min/avg/max/stddev = 0.409/0.666/0.773/0.149 ms -func starTCPPing(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { - var ( - hostname tps.StringOrBytes - port = 80 - count = 4 - timeout tps.FloatOrInt = 10 - interval tps.FloatOrInt = 1 - ) - if err := starlark.UnpackArgs(b.Name(), args, kwargs, "hostname", &hostname, "port?", &port, "count?", &count, "timeout?", &timeout, "interval?", &interval); err != nil { - return nil, err - } - - // correct timeout value - if timeout <= 0 { - timeout = 10 - } - if interval <= 0 { - interval = 1 - } - - // get the context - ctx := dataconv.GetThreadContext(thread) - - // perform the TCP ping - rtts, addr, err := goTCPPing(ctx, hostname.GoString(), port, count, time.Duration(timeout)*time.Second, time.Duration(interval)*time.Second) - - // return the result - if err != nil { - return none, fmt.Errorf("%s: %w", b.Name(), err) - } - - // statistics - vals := make([]float64, len(rtts)) - for i, rtt := range rtts { - vals[i] = float64(rtt) / float64(time.Millisecond) - } - succ := len(rtts) - loss := float64(count-succ) / float64(count) * 100 - avg, _ := stats.Mean(vals) - min, _ := stats.Min(vals) - max, _ := stats.Max(vals) - stddev, _ := stats.StandardDeviation(vals) - sd := starlark.StringDict{ - "hostname": starlark.String(addr), - "total": starlark.MakeInt(count), - "success": starlark.MakeInt(succ), - "loss": starlark.Float(loss), - "min": starlark.Float(min), - "avg": starlark.Float(avg), - "max": starlark.Float(max), - "stddev": starlark.Float(stddev), - } - return starlarkstruct.FromStringDict(starlark.String(`statistics`), sd), nil -} diff --git a/lib/net/ping.go b/lib/net/ping.go new file mode 100644 index 0000000..8e6a944 --- /dev/null +++ b/lib/net/ping.go @@ -0,0 +1,171 @@ +package net + +import ( + "context" + "fmt" + "net" + "net/http" + "strconv" + "time" + + "github.com/1set/starlet/dataconv" + tps "github.com/1set/starlet/dataconv/types" + "github.com/montanaflynn/stats" + "go.starlark.net/starlark" + "go.starlark.net/starlarkstruct" +) + +func goPingWrap(ctx context.Context, address string, count int, timeout, interval time.Duration, pingFunc func(ctx context.Context, address string, timeout time.Duration) (time.Duration, error)) ([]time.Duration, error) { + if count <= 0 { + return nil, fmt.Errorf("count must be greater than 0") + } + + rttDurations := make([]time.Duration, 0, count) + for i := 1; i <= count; i++ { + rtt, err := pingFunc(ctx, address, timeout) + if err != nil { + continue + } + rttDurations = append(rttDurations, rtt) + if i < count { + time.Sleep(interval) + } + } + + if len(rttDurations) == 0 { + return nil, fmt.Errorf("no successful connections") + } + + return rttDurations, nil +} + +func tcpPingFunc(ctx context.Context, address string, timeout time.Duration) (time.Duration, error) { + start := time.Now() + conn, err := net.DialTimeout("tcp", address, timeout) + if err != nil { + return 0, err + } + rtt := time.Since(start) + conn.Close() + return rtt, nil +} + +func httpPingFunc(ctx context.Context, url string, timeout time.Duration) (time.Duration, error) { + client := &http.Client{ + Timeout: timeout, + } + start := time.Now() + resp, err := client.Get(url) + if err != nil { + return 0, err + } + defer resp.Body.Close() + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return 0, fmt.Errorf("unacceptable status code: %d", resp.StatusCode) + } + rtt := time.Since(start) + return rtt, nil +} + +func createPingStats(address string, count int, rtts []time.Duration) starlark.Value { + vals := make([]float64, len(rtts)) + for i, rtt := range rtts { + vals[i] = float64(rtt) / float64(time.Millisecond) + } + succ := len(rtts) + loss := float64(count-succ) / float64(count) * 100 + avg, _ := stats.Mean(vals) + min, _ := stats.Min(vals) + max, _ := stats.Max(vals) + stddev, _ := stats.StandardDeviation(vals) + sd := starlark.StringDict{ + "address": starlark.String(address), + "total": starlark.MakeInt(count), + "success": starlark.MakeInt(succ), + "loss": starlark.Float(loss), + "min": starlark.Float(min), + "avg": starlark.Float(avg), + "max": starlark.Float(max), + "stddev": starlark.Float(stddev), + } + return starlarkstruct.FromStringDict(starlark.String(`statistics`), sd) +} + +func starTCPPing(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + var ( + hostname tps.StringOrBytes + port = 80 + count = 4 + timeout tps.FloatOrInt = 10 + interval tps.FloatOrInt = 1 + ) + if err := starlark.UnpackArgs(b.Name(), args, kwargs, "hostname", &hostname, "port?", &port, "count?", &count, "timeout?", &timeout, "interval?", &interval); err != nil { + return nil, err + } + + // correct timeout value + if timeout <= 0 { + timeout = 10 + } + if interval <= 0 { + interval = 1 + } + + // get the context + ctx := dataconv.GetThreadContext(thread) + + // resolve the hostname to an IP address + ips, err := goLookup(ctx, hostname.GoString(), "", time.Duration(timeout)*time.Second) + if err != nil { + return none, fmt.Errorf("%s: %w", b.Name(), err) + } + if len(ips) == 0 { + return none, fmt.Errorf("unable to resolve hostname") + } + address := net.JoinHostPort(ips[0], strconv.Itoa(port)) + + // perform the TCP ping + rtts, err := goPingWrap(ctx, address, count, time.Duration(timeout)*time.Second, time.Duration(interval)*time.Second, tcpPingFunc) + + // return the result + if err != nil { + return none, fmt.Errorf("%s: %w", b.Name(), err) + } + + // statistics + return createPingStats(address, count, rtts), nil +} + +func starHTTPing(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + var ( + url tps.StringOrBytes + count = 4 + timeout tps.FloatOrInt = 10 + interval tps.FloatOrInt = 1 + ) + if err := starlark.UnpackArgs(b.Name(), args, kwargs, "url", &url, "count?", &count, "timeout?", &timeout, "interval?", &interval); err != nil { + return nil, err + } + + // correct timeout value + if timeout <= 0 { + timeout = 10 + } + if interval <= 0 { + interval = 1 + } + + // get the context + ctx := dataconv.GetThreadContext(thread) + + // perform the HTTP ping + rtts, err := goPingWrap(ctx, url.GoString(), count, time.Duration(timeout)*time.Second, time.Duration(interval)*time.Second, httpPingFunc) + + // return the result + if err != nil { + return none, fmt.Errorf("%s: %w", b.Name(), err) + } + + // statistics + return createPingStats(url.GoString(), count, rtts), nil +} From 246b1d7d8a8221dfd0b5ecccee08375a7c55e611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Tue, 23 Jul 2024 00:17:18 +0800 Subject: [PATCH 19/25] clean comments --- lib/net/ping.go | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/lib/net/ping.go b/lib/net/ping.go index 8e6a944..98cf54d 100644 --- a/lib/net/ping.go +++ b/lib/net/ping.go @@ -111,7 +111,7 @@ func starTCPPing(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tup interval = 1 } - // get the context + // get the context for the DNS lookup and TCP ping ctx := dataconv.GetThreadContext(thread) // resolve the hostname to an IP address @@ -124,15 +124,11 @@ func starTCPPing(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tup } address := net.JoinHostPort(ips[0], strconv.Itoa(port)) - // perform the TCP ping + // perform the TCP ping, and get the statistics rtts, err := goPingWrap(ctx, address, count, time.Duration(timeout)*time.Second, time.Duration(interval)*time.Second, tcpPingFunc) - - // return the result if err != nil { return none, fmt.Errorf("%s: %w", b.Name(), err) } - - // statistics return createPingStats(address, count, rtts), nil } @@ -155,17 +151,11 @@ func starHTTPing(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tup interval = 1 } - // get the context + // perform the HTTP ping, and get the statistics ctx := dataconv.GetThreadContext(thread) - - // perform the HTTP ping rtts, err := goPingWrap(ctx, url.GoString(), count, time.Duration(timeout)*time.Second, time.Duration(interval)*time.Second, httpPingFunc) - - // return the result if err != nil { return none, fmt.Errorf("%s: %w", b.Name(), err) } - - // statistics return createPingStats(url.GoString(), count, rtts), nil } From 293598813635ccf938ee7b71f04492221eca863b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Tue, 23 Jul 2024 00:49:44 +0800 Subject: [PATCH 20/25] use net/http/httptrace --- lib/net/ping.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/net/ping.go b/lib/net/ping.go index 98cf54d..056c4f0 100644 --- a/lib/net/ping.go +++ b/lib/net/ping.go @@ -5,7 +5,9 @@ import ( "fmt" "net" "net/http" + "net/http/httptrace" "strconv" + "sync" "time" "github.com/1set/starlet/dataconv" @@ -50,7 +52,7 @@ func tcpPingFunc(ctx context.Context, address string, timeout time.Duration) (ti return rtt, nil } -func httpPingFunc(ctx context.Context, url string, timeout time.Duration) (time.Duration, error) { +func httpPingFunc2(ctx context.Context, url string, timeout time.Duration) (time.Duration, error) { client := &http.Client{ Timeout: timeout, } @@ -67,6 +69,47 @@ func httpPingFunc(ctx context.Context, url string, timeout time.Duration) (time. return rtt, nil } +func httpPingFunc(ctx context.Context, url string, timeout time.Duration) (time.Duration, error) { + // create a custom http client tracing + var ( + onceStart, onceDone sync.Once + connStart time.Time + connDur time.Duration + ) + trace := &httptrace.ClientTrace{ + ConnectStart: func(network, addr string) { + onceStart.Do(func() { + connStart = time.Now() + }) + }, + ConnectDone: func(network, addr string, err error) { + onceDone.Do(func() { + connDur = time.Since(connStart) + }) + }, + } + + // create a http client with timeout and tracing + client := &http.Client{ + Timeout: timeout, + } + req, err := http.NewRequestWithContext(httptrace.WithClientTrace(ctx, trace), "GET", url, nil) + if err != nil { + return 0, err + } + + // perform the HTTP request + resp, err := client.Do(req) + if err != nil { + return 0, err + } + defer resp.Body.Close() + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return 0, fmt.Errorf("unacceptable status: %d", resp.StatusCode) + } + return connDur, nil +} + func createPingStats(address string, count int, rtts []time.Duration) starlark.Value { vals := make([]float64, len(rtts)) for i, rtt := range rtts { From a64baf83d099a72df5ffe06958a333c47545cb04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Tue, 23 Jul 2024 00:52:11 +0800 Subject: [PATCH 21/25] split test files --- lib/net/network.go | 1 + lib/net/network_test.go | 65 +--------------------------- lib/net/ping.go | 17 -------- lib/net/ping_test.go | 96 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 81 deletions(-) create mode 100644 lib/net/ping_test.go diff --git a/lib/net/network.go b/lib/net/network.go index 0ef4994..35ba49f 100644 --- a/lib/net/network.go +++ b/lib/net/network.go @@ -33,6 +33,7 @@ func LoadModule() (starlark.StringDict, error) { Members: starlark.StringDict{ "nslookup": starlark.NewBuiltin(ModuleName+".nslookup", starLookup), "tcping": starlark.NewBuiltin(ModuleName+".tcping", starTCPPing), + "httping": starlark.NewBuiltin(ModuleName+".httping", starHTTPing), }, }, } diff --git a/lib/net/network_test.go b/lib/net/network_test.go index e9dac04..cd09b55 100644 --- a/lib/net/network_test.go +++ b/lib/net/network_test.go @@ -8,7 +8,7 @@ import ( "github.com/1set/starlet/lib/net" ) -func TestLoadModule_Network(t *testing.T) { +func TestLoadModule_NSLookUp(t *testing.T) { isOnWindows := runtime.GOOS == "windows" tests := []struct { name string @@ -103,69 +103,6 @@ func TestLoadModule_Network(t *testing.T) { `), wantErr: `net.nslookup: for parameter domain: got int, want string or bytes`, }, - { - name: `tcping: normal`, - script: itn.HereDoc(` - load('net', 'tcping') - s = tcping('bing.com') - print(s) - assert.eq(s.total, 4) - assert.true(s.success > 0) - `), - }, - { - name: `tcping: abnormal`, - script: itn.HereDoc(` - load('net', 'tcping') - s = tcping('bing.com', count=1, timeout=-5, interval=-2) - print(s) - assert.eq(s.total, 1) - assert.true(s.success > 0) - assert.eq(s.stddev, 0) - `), - }, - { - name: `tcping: faster`, - script: itn.HereDoc(` - load('net', 'tcping') - s = tcping('bing.com', count=10, timeout=5, interval=0.01) - print(s) - assert.eq(s.total, 10) - assert.true(s.success > 0) - `), - }, - { - name: `tcping: not exists`, - script: itn.HereDoc(` - load('net', 'tcping') - s = tcping('missing.invalid') - `), - wantErr: `missing.invalid`, // mac/win: no such host, linux: server misbehaving - }, - { - name: `tcping: wrong count`, - script: itn.HereDoc(` - load('net', 'tcping') - s = tcping('bing.com', count=0) - `), - wantErr: `net.tcping: count must be greater than 0`, - }, - { - name: `tcping: no args`, - script: itn.HereDoc(` - load('net', 'tcping') - tcping() - `), - wantErr: `net.tcping: missing argument for hostname`, - }, - { - name: `tcping: invalid args`, - script: itn.HereDoc(` - load('net', 'tcping') - tcping(123) - `), - wantErr: `net.tcping: for parameter hostname: got int, want string or bytes`, - }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/lib/net/ping.go b/lib/net/ping.go index 056c4f0..7dc0f5e 100644 --- a/lib/net/ping.go +++ b/lib/net/ping.go @@ -52,23 +52,6 @@ func tcpPingFunc(ctx context.Context, address string, timeout time.Duration) (ti return rtt, nil } -func httpPingFunc2(ctx context.Context, url string, timeout time.Duration) (time.Duration, error) { - client := &http.Client{ - Timeout: timeout, - } - start := time.Now() - resp, err := client.Get(url) - if err != nil { - return 0, err - } - defer resp.Body.Close() - if resp.StatusCode < 200 || resp.StatusCode >= 300 { - return 0, fmt.Errorf("unacceptable status code: %d", resp.StatusCode) - } - rtt := time.Since(start) - return rtt, nil -} - func httpPingFunc(ctx context.Context, url string, timeout time.Duration) (time.Duration, error) { // create a custom http client tracing var ( diff --git a/lib/net/ping_test.go b/lib/net/ping_test.go new file mode 100644 index 0000000..b6314b5 --- /dev/null +++ b/lib/net/ping_test.go @@ -0,0 +1,96 @@ +package net_test + +import ( + "runtime" + "testing" + + itn "github.com/1set/starlet/internal" + "github.com/1set/starlet/lib/net" +) + +func TestLoadModule_Ping(t *testing.T) { + isOnWindows := runtime.GOOS == "windows" + tests := []struct { + name string + script string + wantErr string + skipWindows bool + }{ + { + name: `tcping: normal`, + script: itn.HereDoc(` + load('net', 'tcping') + s = tcping('bing.com') + print(s) + assert.eq(s.total, 4) + assert.true(s.success > 0) + `), + }, + { + name: `tcping: abnormal`, + script: itn.HereDoc(` + load('net', 'tcping') + s = tcping('bing.com', count=1, timeout=-5, interval=-2) + print(s) + assert.eq(s.total, 1) + assert.true(s.success > 0) + assert.eq(s.stddev, 0) + `), + }, + { + name: `tcping: faster`, + script: itn.HereDoc(` + load('net', 'tcping') + s = tcping('bing.com', count=10, timeout=5, interval=0.01) + print(s) + assert.eq(s.total, 10) + assert.true(s.success > 0) + `), + }, + { + name: `tcping: not exists`, + script: itn.HereDoc(` + load('net', 'tcping') + s = tcping('missing.invalid') + `), + wantErr: `missing.invalid`, // mac/win: no such host, linux: server misbehaving + }, + { + name: `tcping: wrong count`, + script: itn.HereDoc(` + load('net', 'tcping') + s = tcping('bing.com', count=0) + `), + wantErr: `net.tcping: count must be greater than 0`, + }, + { + name: `tcping: no args`, + script: itn.HereDoc(` + load('net', 'tcping') + tcping() + `), + wantErr: `net.tcping: missing argument for hostname`, + }, + { + name: `tcping: invalid args`, + script: itn.HereDoc(` + load('net', 'tcping') + tcping(123) + `), + wantErr: `net.tcping: for parameter hostname: got int, want string or bytes`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if isOnWindows && tt.skipWindows { + t.Skipf("Skip test on Windows") + return + } + res, err := itn.ExecModuleWithErrorTest(t, net.ModuleName, net.LoadModule, tt.script, tt.wantErr, nil) + if (err != nil) != (tt.wantErr != "") { + t.Errorf("net(%q) expects error = '%v', actual error = '%v', result = %v", tt.name, tt.wantErr, err, res) + return + } + }) + } +} From 2683c990cf78eaccb9ab7310cb42269487f746bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Tue, 23 Jul 2024 00:58:43 +0800 Subject: [PATCH 22/25] add tests --- lib/net/ping_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/lib/net/ping_test.go b/lib/net/ping_test.go index b6314b5..14f3d71 100644 --- a/lib/net/ping_test.go +++ b/lib/net/ping_test.go @@ -16,6 +16,7 @@ func TestLoadModule_Ping(t *testing.T) { wantErr string skipWindows bool }{ + // TCPing tests { name: `tcping: normal`, script: itn.HereDoc(` @@ -79,6 +80,71 @@ func TestLoadModule_Ping(t *testing.T) { `), wantErr: `net.tcping: for parameter hostname: got int, want string or bytes`, }, + + // HTTPing tests + { + name: `httping: normal`, + script: itn.HereDoc(` + load('net', 'httping') + s = httping('https://www.bing.com') + print(s) + assert.eq(s.total, 4) + assert.true(s.success > 0) + `), + }, + { + name: `httping: abnormal`, + script: itn.HereDoc(` + load('net', 'httping') + s = httping('https://www.bing.com', count=1, timeout=-5, interval=-2) + print(s) + assert.eq(s.total, 1) + assert.true(s.success > 0) + assert.eq(s.stddev, 0) + `), + }, + { + name: `httping: faster`, + script: itn.HereDoc(` + load('net', 'httping') + s = httping('https://www.bing.com', count=10, timeout=5, interval=0.01) + print(s) + assert.eq(s.total, 10) + assert.true(s.success > 0) + `), + }, + { + name: `httping: not exists`, + script: itn.HereDoc(` + load('net', 'httping') + s = httping('http://missing.invalid') + `), + wantErr: `net.httping: no successful connections`, // mac/win: no such host, linux: server misbehaving + }, + { + name: `httping: wrong count`, + script: itn.HereDoc(` + load('net', 'httping') + s = httping('https://www.bing.com', count=0) + `), + wantErr: `net.httping: count must be greater than 0`, + }, + { + name: `httping: no args`, + script: itn.HereDoc(` + load('net', 'httping') + httping() + `), + wantErr: `net.httping: missing argument for url`, + }, + { + name: `httping: invalid args`, + script: itn.HereDoc(` + load('net', 'httping') + httping(123) + `), + wantErr: `net.httping: for parameter url: got int, want string or bytes`, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From f6bca224155f46294e17b5a3113896b1c7762604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Tue, 23 Jul 2024 01:02:52 +0800 Subject: [PATCH 23/25] fix var naming --- lib/net/ping.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/net/ping.go b/lib/net/ping.go index 7dc0f5e..8b5bd91 100644 --- a/lib/net/ping.go +++ b/lib/net/ping.go @@ -37,7 +37,6 @@ func goPingWrap(ctx context.Context, address string, count int, timeout, interva if len(rttDurations) == 0 { return nil, fmt.Errorf("no successful connections") } - return rttDurations, nil } @@ -178,10 +177,11 @@ func starHTTPing(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tup } // perform the HTTP ping, and get the statistics + address := url.GoString() ctx := dataconv.GetThreadContext(thread) - rtts, err := goPingWrap(ctx, url.GoString(), count, time.Duration(timeout)*time.Second, time.Duration(interval)*time.Second, httpPingFunc) + rtts, err := goPingWrap(ctx, address, count, time.Duration(timeout)*time.Second, time.Duration(interval)*time.Second, httpPingFunc) if err != nil { return none, fmt.Errorf("%s: %w", b.Name(), err) } - return createPingStats(url.GoString(), count, rtts), nil + return createPingStats(address, count, rtts), nil } From 7e5e9cedf21dac4a8c9a9b166a091e7529c7be07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Tue, 23 Jul 2024 01:27:58 +0800 Subject: [PATCH 24/25] for 200-400, and fix reuse --- lib/net/ping.go | 8 +++++++- lib/net/ping_test.go | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/net/ping.go b/lib/net/ping.go index 8b5bd91..258c069 100644 --- a/lib/net/ping.go +++ b/lib/net/ping.go @@ -74,6 +74,12 @@ func httpPingFunc(ctx context.Context, url string, timeout time.Duration) (time. // create a http client with timeout and tracing client := &http.Client{ Timeout: timeout, + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse // do not follow redirects + }, + Transport: &http.Transport{ + DisableKeepAlives: true, + }, } req, err := http.NewRequestWithContext(httptrace.WithClientTrace(ctx, trace), "GET", url, nil) if err != nil { @@ -86,7 +92,7 @@ func httpPingFunc(ctx context.Context, url string, timeout time.Duration) (time. return 0, err } defer resp.Body.Close() - if resp.StatusCode < 200 || resp.StatusCode >= 300 { + if resp.StatusCode < 200 || resp.StatusCode >= 400 { return 0, fmt.Errorf("unacceptable status: %d", resp.StatusCode) } return connDur, nil diff --git a/lib/net/ping_test.go b/lib/net/ping_test.go index 14f3d71..6b9c484 100644 --- a/lib/net/ping_test.go +++ b/lib/net/ping_test.go @@ -90,6 +90,7 @@ func TestLoadModule_Ping(t *testing.T) { print(s) assert.eq(s.total, 4) assert.true(s.success > 0) + assert.true(s.min > 0) `), }, { @@ -111,6 +112,7 @@ func TestLoadModule_Ping(t *testing.T) { print(s) assert.eq(s.total, 10) assert.true(s.success > 0) + assert.true(s.min > 0) `), }, { From 305fd8b07e13ac10d93b0b3caa79b8383e14a460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Tue, 23 Jul 2024 07:32:44 +0800 Subject: [PATCH 25/25] for mock server --- lib/net/ping_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/lib/net/ping_test.go b/lib/net/ping_test.go index 6b9c484..8790e29 100644 --- a/lib/net/ping_test.go +++ b/lib/net/ping_test.go @@ -1,14 +1,42 @@ package net_test import ( + "net/http" + "net/http/httptest" "runtime" "testing" itn "github.com/1set/starlet/internal" "github.com/1set/starlet/lib/net" + "go.starlark.net/starlark" ) +// A helper function to create a mock server that returns the specified status code +func createMockServer(statusCode int) *httptest.Server { + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(statusCode) + }) + return httptest.NewServer(handler) +} + +// Create a mock server that returns a 301 status code with a Location header +func createRedirectMockServer(location string) *httptest.Server { + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Location", location) + w.WriteHeader(http.StatusMovedPermanently) + }) + return httptest.NewServer(handler) +} + func TestLoadModule_Ping(t *testing.T) { + // create mock servers for testing + server301 := createRedirectMockServer("https://notgoingthere.invalid") + defer server301.Close() + server404 := createMockServer(http.StatusNotFound) + defer server404.Close() + server500 := createMockServer(http.StatusInternalServerError) + defer server500.Close() + isOnWindows := runtime.GOOS == "windows" tests := []struct { name string @@ -115,6 +143,31 @@ func TestLoadModule_Ping(t *testing.T) { assert.true(s.min > 0) `), }, + { + name: `httping: ignore redirect`, + script: itn.HereDoc(` + load('net', 'httping') + s = httping(server_301, interval=0.1) + assert.eq(s.total, 4) + assert.eq(s.success, 4) + `), + }, + { + name: `httping: status 404`, + script: itn.HereDoc(` + load('net', 'httping') + s = httping(server_404, interval=0.1) + `), + wantErr: `net.httping: no successful connections`, + }, + { + name: `httping: status 500`, + script: itn.HereDoc(` + load('net', 'httping') + s = httping(server_500, interval=0.1) + `), + wantErr: `net.httping: no successful connections`, + }, { name: `httping: not exists`, script: itn.HereDoc(` @@ -154,7 +207,12 @@ func TestLoadModule_Ping(t *testing.T) { t.Skipf("Skip test on Windows") return } - res, err := itn.ExecModuleWithErrorTest(t, net.ModuleName, net.LoadModule, tt.script, tt.wantErr, nil) + extra := starlark.StringDict{ + "server_301": starlark.String(server301.URL), + "server_404": starlark.String(server404.URL), + "server_500": starlark.String(server500.URL), + } + res, err := itn.ExecModuleWithErrorTest(t, net.ModuleName, net.LoadModule, tt.script, tt.wantErr, extra) if (err != nil) != (tt.wantErr != "") { t.Errorf("net(%q) expects error = '%v', actual error = '%v', result = %v", tt.name, tt.wantErr, err, res) return