Heartbeat mechanism from client to server over the custom connection for sending events to guarantee that the connectoin stays up.
83 lines
1.7 KiB
Go
83 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"converge/pkg/iowrappers"
|
|
"converge/pkg/websocketutil"
|
|
"crypto/tls"
|
|
"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()
|
|
}
|
|
|
|
func handleConnection(conn net.Conn, wsURL string, insecure bool) {
|
|
defer closeConnection(conn)
|
|
|
|
log.Printf("Connecting to '%s'\n", wsURL)
|
|
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
|
|
}
|
|
wsConn := websocketutil.NewWebSocketConn(_wsConn)
|
|
defer wsConn.Close()
|
|
|
|
iowrappers.SynchronizeStreams(wsConn, conn)
|
|
}
|
|
|
|
func main() {
|
|
usage := "Usage: tcptows [--insecure] <localport> ws[s]://<host>[:port]/client/<ID>\n" +
|
|
"\n" +
|
|
"Here <ID> is the rendez-vous id of a continuous integratio job\n"
|
|
insecure := false
|
|
|
|
args := os.Args[1:]
|
|
|
|
if len(args) == 3 && args[0] == "--insecure" {
|
|
insecure = true
|
|
args = args[1:]
|
|
}
|
|
|
|
if len(args) != 2 {
|
|
fmt.Fprintf(os.Stderr, usage)
|
|
os.Exit(1)
|
|
}
|
|
tcpPort := args[0]
|
|
wsURL := args[1]
|
|
|
|
listener, err := net.Listen("tcp", ":"+tcpPort)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
|
|
log.Printf("TCP server listening on port %s\n", tcpPort)
|
|
log.Printf("Forwarding connections to WebSocket server at %s\n", wsURL)
|
|
|
|
for {
|
|
conn, err := listener.Accept()
|
|
if err != nil {
|
|
log.Println(err)
|
|
continue
|
|
}
|
|
go handleConnection(conn, wsURL, insecure)
|
|
}
|
|
}
|