Browse Source

h2i: start of flags, usage, dialing

Brad Fitzpatrick 10 years ago
parent
commit
0e20025167
1 changed files with 41 additions and 2 deletions
  1. 41 2
      h2i/h2i.go

+ 41 - 2
h2i/h2i.go

@@ -1,15 +1,54 @@
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+// See https://code.google.com/p/go/source/browse/CONTRIBUTORS
+// Licensed under the same terms as Go itself:
+// https://code.google.com/p/go/source/browse/LICENSE
+
+// The h2i command is an interactive HTTP/2 console.
 package main
 package main
 
 
 import (
 import (
+	"flag"
+	"fmt"
 	"io"
 	"io"
 	"log"
 	"log"
+	"net"
 	"os"
 	"os"
 	"time"
 	"time"
 
 
-	"code.google.com/p/go.crypto/ssh/terminal"
+	"golang.org/x/crypto/ssh/terminal"
 )
 )
 
 
+func usage() {
+	fmt.Fprintf(os.Stderr, "Usage: h2i <hostname>\n\n")
+	flag.PrintDefaults()
+	os.Exit(1)
+}
+
+// withPort adds ":443" if another port isn't already present.
+func withPort(host string) string {
+	if _, _, err := net.SplitHostPort(host); err != nil {
+		return net.JoinHostPort(host, "443")
+	}
+	return host
+}
+
 func main() {
 func main() {
+	flag.Usage = usage
+	flag.Parse()
+	if flag.NArg() != 1 {
+		usage()
+	}
+
+	host := flag.Arg(0)
+	c, err := net.Dial("tcp", withPort(host))
+	if err != nil {
+		log.Fatalf("Error dialing %s: %v", withPort(host), err)
+	}
+	defer c.Close()
+	log.Printf("Connected to %v", c.RemoteAddr())
+
 	oldState, err := terminal.MakeRaw(0)
 	oldState, err := terminal.MakeRaw(0)
 	if err != nil {
 	if err != nil {
 		panic(err)
 		panic(err)
@@ -34,7 +73,7 @@ func main() {
 		if err != nil {
 		if err != nil {
 			log.Fatal("ReadLine:", err)
 			log.Fatal("ReadLine:", err)
 		}
 		}
-		if line == "quit" {
+		if line == "q" || line == "quit" {
 			return
 			return
 		}
 		}
 		_, err = term.Write([]byte("boom - " + line + "\n"))
 		_, err = term.Write([]byte("boom - " + line + "\n"))