From 57e7a687e4db05d23b031be8c05d02e6ff7d9e37 Mon Sep 17 00:00:00 2001 From: Vinicius Fortuna Date: Mon, 16 Oct 2023 10:26:56 -0400 Subject: [PATCH] chore: cleanup tools (#103) - More outline-fetch to fetch. `outline-` is redundant and makes commands longer - Add response headers to verbose output - Fix and improve usage strings --- x/examples/{outline-fetch => fetch}/README.md | 8 +++--- x/examples/{outline-fetch => fetch}/main.go | 26 ++++++++++++++++++- x/examples/outline-connectivity/main.go | 11 +++++++- 3 files changed, 39 insertions(+), 6 deletions(-) rename x/examples/{outline-fetch => fetch}/README.md (63%) rename x/examples/{outline-fetch => fetch}/main.go (72%) diff --git a/x/examples/outline-fetch/README.md b/x/examples/fetch/README.md similarity index 63% rename from x/examples/outline-fetch/README.md rename to x/examples/fetch/README.md index b6a6a53a..d5acb526 100644 --- a/x/examples/outline-fetch/README.md +++ b/x/examples/fetch/README.md @@ -5,7 +5,7 @@ This app illustrates how to use different transports to fetch a URL in Go. Direct fetch: ```sh -$ go run github.com/Jigsaw-Code/outline-sdk/x/examples/outline-fetch@latest https://ipinfo.io +$ go run github.com/Jigsaw-Code/outline-sdk/x/examples/fetch@latest https://ipinfo.io { ... "city": "Amsterdam", @@ -18,7 +18,7 @@ $ go run github.com/Jigsaw-Code/outline-sdk/x/examples/outline-fetch@latest http Using a Shadowsocks server: ```sh -$ go run github.com/Jigsaw-Code/outline-sdk/x/examples/outline-fetch@latest -transport ss://[redacted]@[redacted]:80 https://ipinfo.io +$ go run github.com/Jigsaw-Code/outline-sdk/x/examples/fetch@latest -transport ss://[redacted]@[redacted]:80 https://ipinfo.io { ... "region": "New Jersey", @@ -31,7 +31,7 @@ $ go run github.com/Jigsaw-Code/outline-sdk/x/examples/outline-fetch@latest -tra Using a SOCKS5 server: ```sh -$ go run github.com/Jigsaw-Code/outline-sdk/x/examples/outline-fetch@latest -transport socks5://[redacted]:5703 https://ipinfo.io +$ go run github.com/Jigsaw-Code/outline-sdk/x/examples/fetch@latest -transport socks5://[redacted]:5703 https://ipinfo.io { ... "city": "Berlin", @@ -44,7 +44,7 @@ $ go run github.com/Jigsaw-Code/outline-sdk/x/examples/outline-fetch@latest -tra Using packet splitting: ```sh -$ go run github.com/Jigsaw-Code/outline-sdk/x/examples/outline-fetch@latest -transport split:3 https://ipinfo.io +$ go run github.com/Jigsaw-Code/outline-sdk/x/examples/fetch@latest -transport split:3 https://ipinfo.io { ... "city": "Amsterdam", diff --git a/x/examples/outline-fetch/main.go b/x/examples/fetch/main.go similarity index 72% rename from x/examples/outline-fetch/main.go rename to x/examples/fetch/main.go index 99054a53..80888366 100644 --- a/x/examples/outline-fetch/main.go +++ b/x/examples/fetch/main.go @@ -23,18 +23,36 @@ import ( "net" "net/http" "os" + "path" "strings" "github.com/Jigsaw-Code/outline-sdk/x/config" ) +var debugLog log.Logger = *log.New(io.Discard, "", 0) + +func init() { + flag.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [flags...] \n", path.Base(os.Args[0])) + flag.PrintDefaults() + } +} + func main() { + verboseFlag := flag.Bool("v", false, "Enable debug output") transportFlag := flag.String("transport", "", "Transport config") + flag.Parse() + if *verboseFlag { + debugLog = *log.New(os.Stderr, "[DEBUG] ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile) + } + url := flag.Arg(0) if url == "" { - log.Fatal("Need to pass the URL to fetch in the command-line") + log.Print("Need to pass the URL to fetch in the command-line") + flag.Usage() + os.Exit(1) } dialer, err := config.NewStreamDialer(*transportFlag) @@ -55,6 +73,12 @@ func main() { } defer resp.Body.Close() + if *verboseFlag { + for k, v := range resp.Header { + debugLog.Printf("%v: %v", k, v) + } + } + _, err = io.Copy(os.Stdout, resp.Body) if err != nil { log.Fatalf("Read of page body failed: %v", err) diff --git a/x/examples/outline-connectivity/main.go b/x/examples/outline-connectivity/main.go index 3dbb1f56..7bca19c7 100644 --- a/x/examples/outline-connectivity/main.go +++ b/x/examples/outline-connectivity/main.go @@ -19,10 +19,12 @@ import ( "encoding/json" "errors" "flag" + "fmt" "io" "log" "net" "os" + "path" "strings" "time" @@ -83,12 +85,19 @@ func unwrapAll(err error) error { } } +func init() { + flag.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [flags...]\n", path.Base(os.Args[0])) + flag.PrintDefaults() + } +} + func main() { verboseFlag := flag.Bool("v", false, "Enable debug output") transportFlag := flag.String("transport", "", "Transport config") domainFlag := flag.String("domain", "example.com.", "Domain name to resolve in the test") resolverFlag := flag.String("resolver", "8.8.8.8,2001:4860:4860::8888", "Comma-separated list of addresses of DNS resolver to use for the test") - protoFlag := flag.String("proto", "tcp,udp", "Comma-separated list of the protocols to test. Muse be \"tcp\", \"udp\", or a combination of them") + protoFlag := flag.String("proto", "tcp,udp", "Comma-separated list of the protocols to test. Must be \"tcp\", \"udp\", or a combination of them") flag.Parse() if *verboseFlag {