Skip to content

Instantly share code, notes, and snippets.

@bit4bit
Created June 30, 2015 04:57
Show Gist options
  • Save bit4bit/ddec1a575f680b0d665e to your computer and use it in GitHub Desktop.
Save bit4bit/ddec1a575f680b0d665e to your computer and use it in GitHub Desktop.
Example: Go Websocket binary proxy noVnc
package main
import (
"flag"
"golang.org/x/net/websocket"
"io"
"log"
"net"
"net/http"
"os"
)
var (
sourceAddr = flag.String("source", "", "source address")
targeAddr = flag.String("target", "", "target address")
)
func main() {
flag.Parse()
if *sourceAddr == "" || *targeAddr == "" {
flag.PrintDefaults()
os.Exit(1)
}
mux := websocket.Server{
Handshake: bootHandshake,
Handler: handleWss}
http.Handle("/websockify", mux)
err := http.ListenAndServe(*sourceAddr, nil)
if err != nil {
log.Fatal(err)
}
}
func handleWss(wsconn *websocket.Conn) {
log.Println("handlewss")
conn, err := net.Dial("tcp", *targeAddr)
if err != nil {
log.Println(err)
wsconn.Close()
} else {
wsconn.PayloadType = websocket.BinaryFrame
go io.Copy(conn, wsconn)
go io.Copy(wsconn, conn)
select {}
}
}
func bootHandshake(config *websocket.Config, r *http.Request) error {
config.Protocol = []string{"binary"}
r.Header.Set("Access-Control-Allow-Origin", "*")
r.Header.Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE")
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment