81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
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]://<host>[:port]/client/<ID>\n\n" +
|
|
"\n" +
|
|
"Here <ID> 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]://<host>[:port]/client/<ID>' 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()
|
|
log.Println("Narg ", flag.NFlag())
|
|
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{})
|
|
}
|