Skip to content

Commit 71965a0

Browse files
committed
feat: Add WASM build for toxcore and websocket bootstrap node.
The websocket bootstrap node will be running on Heroku.
1 parent 727982d commit 71965a0

File tree

11 files changed

+296
-8
lines changed

11 files changed

+296
-8
lines changed

.github/settings.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ branches:
1818
- "bazel-opt"
1919
- "bazel-tsan"
2020
- "bazel-valgrind"
21-
- "build-bootstrapd-docker"
2221
- "build-compcert"
2322
- "build-macos"
23+
- "build-msan"
2424
- "build-nacl"
2525
- "build-tcc"
2626
- "build-win32"
@@ -38,6 +38,9 @@ branches:
3838
- "cimple"
3939
- "code-review/reviewable"
4040
- "continuous-integration/appveyor/pr"
41+
- "docker-bootstrap-node"
42+
- "docker-bootstrap-node-websocket"
43+
- "docker-toxcore-js"
4144
- "mypy"
4245
- "sonar-scan"
4346

.github/workflows/docker.yml

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ on:
77
branches: [master]
88

99
jobs:
10-
build-bootstrapd-docker:
10+
docker-bootstrap-node:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- name: Set up Docker Buildx
14-
uses: docker/setup-buildx-action@v1
1513
- name: Login to DockerHub
16-
if: github.event_name == 'push'
14+
if: ${{ github.event_name == 'push' }}
1715
uses: docker/login-action@v1
1816
with:
1917
username: ${{ secrets.DOCKERHUB_USERNAME }}
@@ -22,8 +20,49 @@ jobs:
2220
- name: Docker Build
2321
run: .github/scripts/tox-bootstrapd-docker local
2422
- name: Push latest image to DockerHub
25-
if: github.event_name == 'push'
23+
if: ${{ github.event_name == 'push' }}
2624
run: docker push toxchat/bootstrap-node:latest
2725
- name: Push versioned image to DockerHub
28-
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
26+
if: ${{ github.event_name == 'push' && contains(github.ref, 'refs/tags/') }}
2927
run: docker push toxchat/bootstrap-node:"$(other/print-version)"
28+
29+
docker-toxcore-js:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- name: Set up Docker Buildx
33+
uses: docker/setup-buildx-action@v1
34+
- name: Login to DockerHub
35+
if: ${{ github.event_name == 'push' }}
36+
uses: docker/login-action@v1
37+
with:
38+
username: ${{ secrets.DOCKERHUB_USERNAME }}
39+
password: ${{ secrets.DOCKERHUB_TOKEN }}
40+
- name: Build and push
41+
uses: docker/build-push-action@v2
42+
with:
43+
file: other/emscripten/Dockerfile
44+
push: ${{ github.event_name == 'push' }}
45+
tags: toxchat/toxcore-js:latest
46+
cache-from: type=registry,ref=toxchat/toxcore-js:latest
47+
cache-to: type=inline
48+
49+
docker-bootstrap-node-websocket:
50+
runs-on: ubuntu-latest
51+
needs: [docker-bootstrap-node]
52+
steps:
53+
- name: Set up Docker Buildx
54+
uses: docker/setup-buildx-action@v1
55+
- name: Login to DockerHub
56+
if: ${{ github.event_name == 'push' }}
57+
uses: docker/login-action@v1
58+
with:
59+
username: ${{ secrets.DOCKERHUB_USERNAME }}
60+
password: ${{ secrets.DOCKERHUB_TOKEN }}
61+
- name: Build and push
62+
uses: docker/build-push-action@v2
63+
with:
64+
context: "{{defaultContext}}:other/bootstrap_daemon/websocket"
65+
push: ${{ github.event_name == 'push' }}
66+
tags: toxchat/bootstrap-node:latest-websocket
67+
cache-from: type=registry,ref=toxchat/bootstrap-node:latest-websocket
68+
cache-to: type=inline

.hadolint.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
---
22
ignored:
3+
# "cd" is sometimes useful when you want to run one command in one directory
4+
# and then another command in another directory, but they should be executed
5+
# in the same RUN instruction.
6+
- DL3003
37
- DL3007
48
- DL3008
59
- DL3013

heroku.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
build:
3+
docker:
4+
web: other/bootstrap_daemon/websocket/Dockerfile

other/bootstrap_daemon/docker/update-sha256

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/usr/bin/env bash
22

33
set -eux
44

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Stage 1 - Compile websockify.
2+
FROM toxchat/bootstrap-node:latest AS tox
3+
FROM golang:1.17-alpine AS websockify
4+
5+
COPY websockify /work/websockify
6+
RUN cd /work/websockify && go mod download github.com/gorilla/websocket && go install
7+
8+
# Stage 2 - Create the run-time image with bootstrap daemon and websockify.
9+
FROM alpine:latest
10+
11+
RUN addgroup -S tox && adduser -SDH -G tox tox
12+
13+
COPY --from=websockify /go/bin/websockify /usr/local/bin/
14+
COPY --from=tox /usr/local /usr/local/
15+
COPY --from=tox /etc/tox-bootstrapd.conf /etc/
16+
COPY entrypoint.sh /
17+
18+
RUN mkdir -p /var/lib/tox-bootstrapd/ /var/run/tox-bootstrapd/ \
19+
&& chown tox:tox /var/lib/tox-bootstrapd/ /var/run/tox-bootstrapd/
20+
# Public Key: 122837CCDD474DD1183A83152164D51427044B3EAAA52ED683F6BA602568673B
21+
COPY keys /var/lib/tox-bootstrapd/
22+
USER tox
23+
24+
# Use this to generate a keys file:
25+
#RUN /usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf --log-backend stdout \
26+
# && sleep 1
27+
28+
WORKDIR /web
29+
CMD ["/entrypoint.sh"]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
set -eux
4+
5+
/usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf --log-backend stdout
6+
/usr/local/bin/websockify -l "0.0.0.0:$PORT" -t 127.0.0.1:33445
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(7��GM�:�!d�'K>��.փ��`%hg;�G��Y�:�Zy5��S�$�|Cڠ�Ǹ�2� �t
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module github.com/TokTok/c-toxcore/other/bootstrap_daemon/websocket/websockify
2+
3+
go 1.17
4+
5+
require (
6+
github.com/gorilla/websocket master
7+
)
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// A Go version WebSocket to TCP socket proxy
2+
//
3+
// This is a heavily modified version of this file:
4+
// https://github.com/novnc/websockify-other/blob/master/golang/websockify.go
5+
//
6+
// Changes include:
7+
// - Fix infinite loop on error.
8+
// - Proper logging.
9+
// - Proper error handling in general.
10+
// - Support both websocket and regular GET requests on /.
11+
//
12+
// Copyright 2022 The TokTok team.
13+
// Copyright 2021 Michael.liu.
14+
// See LICENSE for licensing conditions.
15+
16+
package main
17+
18+
import (
19+
"encoding/hex"
20+
"flag"
21+
"log"
22+
"net"
23+
"net/http"
24+
25+
"github.com/gorilla/websocket"
26+
)
27+
28+
var (
29+
sourceAddr = flag.String("l", "127.0.0.1:8080", "http service address")
30+
targetAddr = flag.String("t", "127.0.0.1:5900", "tcp service address")
31+
)
32+
33+
var upgrader = websocket.Upgrader{
34+
// Should be enough to fit any Tox TCP packets.
35+
ReadBufferSize: 2048,
36+
WriteBufferSize: 2048,
37+
Subprotocols: []string{"binary"},
38+
CheckOrigin: func(r *http.Request) bool {
39+
return true
40+
},
41+
}
42+
43+
func forwardTCP(wsconn *websocket.Conn, conn net.Conn) {
44+
var tcpbuffer [2048]byte
45+
defer wsconn.Close()
46+
defer conn.Close()
47+
for {
48+
n, err := conn.Read(tcpbuffer[0:])
49+
if err != nil {
50+
log.Println("TCP READ :", err)
51+
break
52+
}
53+
log.Println("TCP READ :", n, hex.EncodeToString(tcpbuffer[0:n]))
54+
55+
if err := wsconn.WriteMessage(websocket.BinaryMessage, tcpbuffer[0:n]); err != nil {
56+
log.Println("WS WRITE :", err)
57+
break
58+
}
59+
log.Println("WS WRITE :", n)
60+
}
61+
}
62+
63+
func forwardWeb(wsconn *websocket.Conn, conn net.Conn) {
64+
defer wsconn.Close()
65+
defer conn.Close()
66+
for {
67+
_, buffer, err := wsconn.ReadMessage()
68+
if err != nil {
69+
log.Println("WS READ :", err)
70+
break
71+
}
72+
log.Println("WS READ :", len(buffer), hex.EncodeToString(buffer))
73+
74+
m, err := conn.Write(buffer)
75+
if err != nil {
76+
log.Println("TCP WRITE:", err)
77+
break
78+
}
79+
log.Println("TCP WRITE:", m)
80+
}
81+
}
82+
83+
func serveWs(w http.ResponseWriter, r *http.Request) {
84+
ws, err := upgrader.Upgrade(w, r, nil)
85+
if err != nil {
86+
log.Println("upgrade:", err)
87+
return
88+
}
89+
vnc, err := net.Dial("tcp", *targetAddr)
90+
if err != nil {
91+
log.Println("dial:", err)
92+
return
93+
}
94+
go forwardTCP(ws, vnc)
95+
go forwardWeb(ws, vnc)
96+
97+
}
98+
99+
func main() {
100+
flag.Parse()
101+
log.Println("Starting up websockify endpoint")
102+
103+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
104+
if r.Header.Get("Upgrade") == "websocket" {
105+
serveWs(w, r)
106+
} else {
107+
http.ServeFile(w, r, r.URL.Path[1:])
108+
}
109+
})
110+
log.Fatal(http.ListenAndServe(*sourceAddr, nil))
111+
}

0 commit comments

Comments
 (0)