diff --git a/cmd/agent/agent.go b/cmd/agent/agent.go index fa2de8a..3c582e4 100755 --- a/cmd/agent/agent.go +++ b/cmd/agent/agent.go @@ -6,6 +6,7 @@ import ( "converge/pkg/iowrappers" "converge/pkg/terminal" "converge/pkg/websocketutil" + "crypto/tls" "flag" "fmt" "github.com/gliderlabs/ssh" @@ -15,6 +16,7 @@ import ( "io" "log" "net" + "net/http" "net/url" "os" "os/exec" @@ -148,6 +150,7 @@ func main() { advanceWarningTime := flag.Duration("warning-time", 5*time.Minute, "advance warning time before sessio ends") agentExpriryTime := flag.Duration("expiry-time", 10*time.Minute, "expiry time of the session") tickerInterval := flag.Duration("check-interval", 60*time.Second, "interval at which expiry is checked") + insecure := flag.Bool("insecure", false, "allow invalid certificates") flag.Parse() if flag.NArg() != 1 { @@ -158,7 +161,14 @@ func main() { agent.ConfigureAgent(*advanceWarningTime, *agentExpriryTime, *tickerInterval) - conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil) + dialer := websocket.Dialer{ + Proxy: http.ProxyFromEnvironment, + HandshakeTimeout: 45 * time.Second, + } + if *insecure { + dialer.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} + } + conn, _, err := dialer.Dial(wsURL, nil) if err != nil { log.Println("WebSocket connection error:", err) return diff --git a/cmd/tcptows/tcptows.go b/cmd/tcptows/tcptows.go index 0ef0d68..e413ad6 100644 --- a/cmd/tcptows/tcptows.go +++ b/cmd/tcptows/tcptows.go @@ -3,10 +3,13 @@ package main import ( "converge/pkg/iowrappers" "converge/pkg/websocketutil" + "crypto/tls" + "flag" "fmt" "github.com/gorilla/websocket" "log" "net" + "net/http" "os" "time" ) @@ -18,11 +21,18 @@ func closeConnection(conn net.Conn) { _ = conn.Close() } -func handleConnection(conn net.Conn, wsURL string) { +func handleConnection(conn net.Conn, wsURL string, insecure bool) { defer closeConnection(conn) log.Printf("Connecting to '%s'\n", wsURL) - _wsConn, _, err := websocket.DefaultDialer.Dial(wsURL, nil) + dialer := websocket.Dialer{ + Proxy: http.ProxyFromEnvironment, + HandshakeTimeout: 45 * time.Second, + } + if insecure { + dialer.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} + } + _wsConn, _, err := dialer.Dial(wsURL, nil) if err != nil { log.Println("WebSocket connection error:", err) return @@ -36,14 +46,23 @@ func handleConnection(conn net.Conn, wsURL string) { } func main() { - if len(os.Args) != 3 { - fmt.Fprintln(os.Stderr, "Usage: tcptows ws[s]://[:port]/client/") - fmt.Fprintln(os.Stderr) - fmt.Fprintln(os.Stderr, "Here is the rendez-vous id of a continuous integratio job") + usage := "Usage: tcptows [options] ws[s]://[:port]/client/\n" + + "\n" + + "Here is the rendez-vous id of a continuous integratio job\n" + insecure := flag.Bool("insecure", false, "allow invalid certificates") + + flag.Usage = func() { + fmt.Fprintln(os.Stderr, usage) + flag.PrintDefaults() + } + flag.Parse() + fmt.Println("Narg ", flag.NArg()) + if flag.NArg() != 2 { + flag.Usage() os.Exit(1) } - tcpPort := os.Args[1] - wsURL := os.Args[2] + tcpPort := flag.Arg(0) + wsURL := flag.Arg(1) listener, err := net.Listen("tcp", ":"+tcpPort) if err != nil { @@ -60,6 +79,6 @@ func main() { log.Println(err) continue } - go handleConnection(conn, wsURL) + go handleConnection(conn, wsURL, *insecure) } } diff --git a/cmd/wsproxy/proxy.go b/cmd/wsproxy/proxy.go deleted file mode 100644 index 6c754e0..0000000 --- a/cmd/wsproxy/proxy.go +++ /dev/null @@ -1,61 +0,0 @@ -package main - -import ( - "converge/pkg/iowrappers" - "converge/pkg/websocketutil" - "fmt" - "github.com/gorilla/websocket" - "log" - "net" - "os" - "time" -) - -func closeConnection(conn net.Conn) { - if tcpConn, ok := conn.(*net.TCPConn); ok { - tcpConn.SetLinger(0) - } - _ = conn.Close() -} - -type Stdio struct{} - -func (stdio Stdio) Read(b []byte) (n int, err error) { - return os.Stdin.Read(b) -} -func (stdio Stdio) Write(b []byte) (n int, err error) { - return os.Stdout.Write(b) -} - -func main() { - if len(os.Args) != 2 { - fmt.Fprintln(os.Stderr, "Usage: tcptows ws[s]://[:port]/client/") - fmt.Fprintln(os.Stderr) - fmt.Fprintln(os.Stderr, "Here is the rendez-vous id of a continuous integratio job") - fmt.Fprintln(os.Stderr, "Use this in an ssh command like this; ") - fmt.Fprintln(os.Stderr) - fmt.Fprintln(os.Stderr, " ssh -oProxyCommand='wsproxy ws[s]://[:port]/client/' abc@localhost") - fmt.Fprintln(os.Stderr) - fmt.Fprintln(os.Stderr, "This lets ssh connect through wsproxy to the remote websocket on") - fmt.Fprintln(os.Stderr, "Converge server.") - fmt.Fprintln(os.Stderr) - os.Exit(1) - } - - wsURL := os.Args[1] - - _wsConn, _, err := websocket.DefaultDialer.Dial(wsURL, nil) - if err != nil { - panic(err) - } - _wsConn.SetReadDeadline(time.Time{}) - _wsConn.SetWriteDeadline(time.Time{}) - if err != nil { - log.Println("WebSocket connection error:", err) - panic(err) - } - wsConn := websocketutil.NewWebSocketConn(_wsConn) - defer wsConn.Close() - - iowrappers.SynchronizeStreams(wsConn, Stdio{}) -} diff --git a/cmd/wsproxy/wsproxy.go b/cmd/wsproxy/wsproxy.go new file mode 100644 index 0000000..e5e6673 --- /dev/null +++ b/cmd/wsproxy/wsproxy.go @@ -0,0 +1,79 @@ +package main + +import ( + "converge/pkg/iowrappers" + "converge/pkg/websocketutil" + "crypto/tls" + "flag" + "fmt" + "github.com/gorilla/websocket" + "log" + "net" + "net/http" + "os" + "time" +) + +func closeConnection(conn net.Conn) { + if tcpConn, ok := conn.(*net.TCPConn); ok { + tcpConn.SetLinger(0) + } + _ = conn.Close() +} + +type Stdio struct{} + +func (stdio Stdio) Read(b []byte) (n int, err error) { + return os.Stdin.Read(b) +} +func (stdio Stdio) Write(b []byte) (n int, err error) { + return os.Stdout.Write(b) +} + +func main() { + usage := "Usage: tcptows ws[s]://[:port]/client/\n\n" + + "\n" + + "Here is the rendez-vous id of a continuous integration job\n" + + "\n" + + "Use this in an ssh command like this: \n" + + "\n" + + " ssh -oProxyCommand='wsproxy ws[s]://[:port]/client/' abc@localhost\n" + + "\n" + + "This latssh connect through wsproxy tocalhost\n" + + insecure := flag.Bool("insecure", false, "allow invalid certificates") + + flag.Usage = func() { + fmt.Fprintln(os.Stderr, usage) + flag.PrintDefaults() + } + flag.Parse() + if flag.NArg() != 1 { + flag.Usage() + os.Exit(1) + } + + wsURL := flag.Arg(0) + + dialer := websocket.Dialer{ + Proxy: http.ProxyFromEnvironment, + HandshakeTimeout: 45 * time.Second, + } + if *insecure { + dialer.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} + } + _wsConn, _, err := dialer.Dial(wsURL, nil) + if err != nil { + panic(err) + } + _wsConn.SetReadDeadline(time.Time{}) + _wsConn.SetWriteDeadline(time.Time{}) + if err != nil { + log.Println("WebSocket connection error:", err) + panic(err) + } + wsConn := websocketutil.NewWebSocketConn(_wsConn) + defer wsConn.Close() + + iowrappers.SynchronizeStreams(wsConn, Stdio{}) +}