Skip to content

Commit 743edaa

Browse files
authored
Version 1.2 (#14)
* Add pointer receiver for initializing ProxyServer and add Teardown function * Rename receiver variable to p from proxyServer * Make config available as a struct * Define Proxy interface and implement it on ProxyServer * Fix metric exports * Add MIT license badge to README * Return if ctx has error * Add build pipeline with GitHub Actions (#15)
1 parent 3ed31c2 commit 743edaa

File tree

7 files changed

+151
-82
lines changed

7 files changed

+151
-82
lines changed

.github/workflows/build-binary.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Build Binary
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
build:
8+
strategy:
9+
fail-fast: false
10+
matrix:
11+
platforms: [ 'linux-amd64', 'darwin-amd64', 'linux-arm64' ]
12+
go: [ '1.15.5' ]
13+
os: [ 'ubuntu-20.04' ]
14+
15+
name: Build ${{ matrix.platforms }}
16+
runs-on: ${{ matrix.os }}
17+
18+
steps:
19+
- name: Setup Go
20+
uses: actions/setup-go@v2
21+
with:
22+
go-version: ${{ matrix.go }}
23+
24+
- name: Cache dependencies
25+
uses: actions/cache@v2
26+
with:
27+
path: |
28+
~/go/pkg/mod
29+
~/.cache/go-build
30+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
31+
restore-keys: |
32+
${{ runner.os }}-go-
33+
34+
- name: Checkout code
35+
uses: actions/checkout@v2
36+
37+
- name: Build Go WebSockify
38+
run: |
39+
make PLATFORM=${{ matrix.platforms }}
40+
41+
- name: Upload binary
42+
uses: actions/upload-artifact@v2
43+
with:
44+
name: go-websockify-${{ matrix.platforms }}
45+
path: |
46+
./bin/go-websockify-${{ matrix.platforms }}

Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
# make
44
# make clean
55

6+
7+
PLATFORM=linux-amd64
8+
9+
parseOS=$(firstword $(subst -, ,$1))
10+
parseArch=$(or $(word 2,$(subst -, ,$1)),$(value 2))
11+
12+
OS=$(call parseOS, ${PLATFORM})
13+
ARCH=$(call parseArch, ${PLATFORM})
14+
615
.PHONY: all
716

817
all: build
918

1019
build:
11-
GOOS=linux GOARCH=amd64 go build -ldflags="-w -s -X main.versionString=$$(git describe --tags --first-parent --abbrev=6 --long --dirty --always) -X main.buildTime=$(date)" -o ./bin/go-websockify
20+
GOOS=${OS} GOARCH=${ARCH} go build -a -ldflags="-w -s -X main.versionString=$$(git describe --tags --first-parent --abbrev=6 --long --dirty --always) -X main.buildTime=$(date)" -o ./bin/go-websockify-${OS}-${ARCH}
1221

1322
clean:
1423
rm -rf bin/

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Go WebSockify
2+
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
23
> RFC 6455 compliant TCP to WebSocket proxy.
34
45
Go WebSockify is a pure Go implementation of [novnc/websockify](https://github.com/novnc/websockify) TCP to WebSocket proxy with improved connection handling. Runs on Linux, Windows and MacOS.

docker-compose.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
version: '3.8'
1+
version: '3.3'
22

33
networks:
44
lan:
55

66
volumes:
7-
data: {}
7+
data:
88

99
services:
1010
# Prometheus
@@ -53,8 +53,8 @@ services:
5353
build: ./client
5454
command: yarn run dev
5555
volumes:
56-
- ./client:/usr/client/
57-
- /usr/client/node_modules
56+
- ./client:/opt/frontend/
57+
- /opt/frontend/node_modules
5858
ports:
5959
- "1234:1234" # Parcel
6060
- "4321:4321" # HMR

http.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ import (
1616

1717
var (
1818
upgrader = websocket.Upgrader{
19-
ReadBufferSize: bufferSize,
20-
WriteBufferSize: bufferSize,
19+
ReadBufferSize: config.bufferSize,
20+
WriteBufferSize: config.bufferSize,
2121
CheckOrigin: authenticateOrigin,
2222
Subprotocols: []string{"binary"},
2323
}
24-
proxyServer *ProxyServer
2524
ctx, stopHTTP = context.WithCancel(context.Background())
2625
server = &http.Server{}
2726
)
@@ -35,24 +34,24 @@ func StartHTTP() {
3534
prometheus.DefaultGatherer,
3635
promhttp.HandlerOpts{},
3736
))
38-
39-
router.HandleFunc(httpPath, webSocketHandler)
37+
router.HandleFunc(config.httpPath, webSocketHandler)
4038

4139
server = &http.Server{
4240
ReadHeaderTimeout: 5 * time.Second,
4341
ReadTimeout: 5 * time.Second,
4442
WriteTimeout: 5 * time.Second,
4543
IdleTimeout: 60 * time.Second,
46-
Addr: bindAddr,
44+
Addr: config.bindAddr,
4745
Handler: router,
4846
}
4947

50-
listening := fmt.Sprintf("Listening at address %s", bindAddr)
48+
listening := fmt.Sprintf("Listening at address %s", config.bindAddr)
5149
log.Println(listening)
5250
log.Fatal(server.ListenAndServe())
5351

5452
if ctx.Err() != nil {
5553
log.Fatalln(ctx.Err())
54+
return
5655
}
5756
}
5857

@@ -65,33 +64,33 @@ func webSocketHandler(w http.ResponseWriter, r *http.Request) {
6564
wsConn, err := upgrader.Upgrade(w, r, nil)
6665

6766
if err != nil {
68-
log.Println("Failed to upgrade websocket request: ", err)
67+
log.Println("failed to upgrade websocket request: ", err)
6968
return
7069
}
71-
7270
wsConnCounter.Inc()
7371

74-
host, port, err := net.SplitHostPort(remoteAddr)
72+
host, port, err := net.SplitHostPort(config.remoteAddr)
7573
if err != nil {
76-
log.Println("Failed to parse remote address")
74+
log.Println("failed to parse remote address")
7775
return
7876
}
7977
addr := fmt.Sprintf("%s:%s", host, port)
8078

8179
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
8280
if err != nil {
83-
message := "Failed to resolve destination: " + err.Error()
81+
message := "failed to resolve destination: " + err.Error()
8482
log.Println(message)
8583
_ = wsConn.WriteMessage(websocket.CloseMessage, []byte(message))
8684
return
8785
}
8886

89-
proxyServer = NewWebSocketProxy(wsConn, tcpAddr)
87+
var p Proxy = new(ProxyServer)
88+
p.Initialize(wsConn, tcpAddr)
9089

91-
if err := proxyServer.Dial(); err != nil {
90+
if err := p.Dial(); err != nil {
9291
log.Println(err)
9392
return
9493
}
9594

96-
go proxyServer.Start()
95+
go p.Start()
9796
}

main.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@ import (
1111
"github.com/msquee/go-websockify/util"
1212
)
1313

14-
var (
14+
var config struct {
1515
bindAddr string
1616
remoteAddr string
1717
bufferSize int
1818
httpPath string
1919

20-
runAsDaemon bool
21-
showVersion bool
22-
echoServer bool
20+
runAsDaemon bool
21+
showVersion bool
22+
echoServer bool
23+
2324
versionString string
2425
buildTime string
25-
26-
daemonContext daemon.Context
27-
)
26+
}
27+
var daemonContext daemon.Context
2828

2929
func init() {
3030
SetupInterruptHandler()
3131

32-
rootCmd.PersistentFlags().StringVar(&bindAddr, "bind-addr", "0.0.0.0:8080", "bind address")
33-
rootCmd.PersistentFlags().StringVar(&remoteAddr, "remote-addr", "127.0.0.1:1984", "remote address")
34-
rootCmd.PersistentFlags().IntVar(&bufferSize, "buffer", 65536, "buffer size")
35-
rootCmd.PersistentFlags().BoolVar(&echoServer, "echo", false, "sidecar echo server")
36-
rootCmd.PersistentFlags().StringVar(&httpPath, "path", "/websockify", "url path clients connect to")
32+
rootCmd.PersistentFlags().StringVar(&config.bindAddr, "bind-addr", "0.0.0.0:8080", "bind address")
33+
rootCmd.PersistentFlags().StringVar(&config.remoteAddr, "remote-addr", "127.0.0.1:1984", "remote address")
34+
rootCmd.PersistentFlags().IntVar(&config.bufferSize, "buffer", 65536, "buffer size")
35+
rootCmd.PersistentFlags().BoolVar(&config.echoServer, "echo", false, "sidecar echo server")
36+
rootCmd.PersistentFlags().StringVar(&config.httpPath, "path", "/websockify", "url path clients connect to")
3737

38-
rootCmd.Flags().BoolVarP(&runAsDaemon, "daemon", "D", false, "run as daemon")
39-
rootCmd.Flags().BoolVarP(&showVersion, "version", "v", false, "print version")
38+
rootCmd.Flags().BoolVarP(&config.runAsDaemon, "daemon", "D", false, "run as daemon")
39+
rootCmd.Flags().BoolVarP(&config.showVersion, "version", "v", false, "print version")
4040
}
4141

4242
func main() {
@@ -50,18 +50,18 @@ var rootCmd = &cobra.Command{
5050
Use: "go-websockify",
5151
Long: `Starts a TCP to WebSocket proxy.`,
5252
Run: func(cmd *cobra.Command, args []string) {
53-
if showVersion {
54-
fmt.Println(fmt.Sprintf("Go WebSockify version %s built on %s", versionString, buildTime))
53+
if config.showVersion {
54+
fmt.Println(fmt.Sprintf("Go WebSockify version %s built on %s", config.versionString, config.buildTime))
5555
os.Exit(0)
5656
}
5757

5858
log.Println("Starting Go WebSockify")
5959

60-
if echoServer {
60+
if config.echoServer {
6161
go util.StartEchoTCPServer()
6262
}
6363

64-
if runAsDaemon {
64+
if config.runAsDaemon {
6565
log.Println("Running Go WebSockify as daemon")
6666

6767
daemonContext := &daemon.Context{
@@ -84,6 +84,7 @@ var rootCmd = &cobra.Command{
8484
daemonMessage := fmt.Sprintf("Daemon running under PID %d", os.Getpid())
8585
log.Println(daemonMessage)
8686
}
87+
8788
StartHTTP()
8889
},
8990
}

0 commit comments

Comments
 (0)