Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 55 additions & 44 deletions ws-tcp-relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,61 @@ import (
"golang.org/x/net/websocket"
)

var tcpAddress string
var binaryMode bool
var (
tcpAddress string
certFile string
keyFile string
port uint
binaryMode bool
fs flag.FlagSet
)

func init() {
fs = *flag.NewFlagSet("Commands", flag.ExitOnError)
fs.UintVar(&port, "p", 4223, "The port to listen on")
fs.UintVar(&port, "port", 4223, "The port to listen on")
fs.StringVar(&certFile, "tlscert", "", "TLS cert file path")
fs.StringVar(&keyFile, "tlskey", "", "TLS key file path")
fs.BoolVar(&binaryMode, "b", false, "Use binary frames instead of text frames")
fs.BoolVar(&binaryMode, "binary", false, "Use binary frames instead of text frames")

fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s <tcpTargetAddress>\n", os.Args[0])
fs.PrintDefaults()
}

if len(os.Args) <= 1 {
fmt.Fprintln(os.Stderr, "No address specified")
os.Exit(1)
}

if os.Args[1] == "--help" || os.Args[1] == "-h" {
fmt.Fprintf(os.Stderr, "Usage: %s <tcpTargetAddress>\n", os.Args[0])
fs.PrintDefaults()
os.Exit(0)
}
fs.Parse(os.Args[2:])
}

func main() {
portString := fmt.Sprintf(":%d", port)

tcpAddress = os.Args[1]

log.Printf("[INFO] Listening on %s\n", portString)

http.Handle("/", websocket.Handler(relayHandler))

var err error
if certFile != "" && keyFile != "" {
err = http.ListenAndServeTLS(portString, certFile, keyFile, nil)
} else {
err = http.ListenAndServe(portString, nil)
}
if err != nil {
log.Fatal(err)
}
}

func copyWorker(dst io.Writer, src io.Reader, doneCh chan<- bool) {
io.Copy(dst, src)
Expand Down Expand Up @@ -41,45 +94,3 @@ func relayHandler(ws *websocket.Conn) {
ws.Close()
<-doneCh
}

func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s <tcpTargetAddress>\n", os.Args[0])
flag.PrintDefaults()
}

func main() {
var port uint
var certFile string
var keyFile string

flag.UintVar(&port, "p", 4223, "The port to listen on")
flag.UintVar(&port, "port", 4223, "The port to listen on")
flag.StringVar(&certFile, "tlscert", "", "TLS cert file path")
flag.StringVar(&keyFile, "tlskey", "", "TLS key file path")
flag.BoolVar(&binaryMode, "b", false, "Use binary frames instead of text frames")
flag.BoolVar(&binaryMode, "binary", false, "Use binary frames instead of text frames")
flag.Usage = usage
flag.Parse()

tcpAddress = flag.Arg(0)
if tcpAddress == "" {
fmt.Fprintln(os.Stderr, "No address specified")
os.Exit(1)
}

portString := fmt.Sprintf(":%d", port)

log.Printf("[INFO] Listening on %s\n", portString)

http.Handle("/", websocket.Handler(relayHandler))

var err error
if certFile != "" && keyFile != "" {
err = http.ListenAndServeTLS(portString, certFile, keyFile, nil)
} else {
err = http.ListenAndServe(portString, nil)
}
if err != nil {
log.Fatal(err)
}
}