Squashed 'external/toxcore/c-toxcore/' content from commit 67badf69
git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 67badf69416a74e74f6d7eb51dd96f37282b8455
This commit is contained in:
29
other/bootstrap_daemon/websocket/Dockerfile
Normal file
29
other/bootstrap_daemon/websocket/Dockerfile
Normal file
@ -0,0 +1,29 @@
|
||||
# Stage 1 - Compile websockify.
|
||||
FROM toxchat/bootstrap-node:latest AS tox
|
||||
FROM golang:1.17-alpine AS websockify
|
||||
|
||||
COPY websockify /work/websockify
|
||||
RUN cd /work/websockify && go mod download github.com/gorilla/websocket && go install
|
||||
|
||||
# Stage 2 - Create the run-time image with bootstrap daemon and websockify.
|
||||
FROM alpine:latest
|
||||
|
||||
RUN addgroup -S tox && adduser -SDH -G tox tox
|
||||
|
||||
COPY --from=websockify /go/bin/websockify /usr/local/bin/
|
||||
COPY --from=tox /usr/local /usr/local/
|
||||
COPY --from=tox /etc/tox-bootstrapd.conf /etc/
|
||||
COPY entrypoint.sh /
|
||||
|
||||
RUN mkdir -p /var/lib/tox-bootstrapd/ /var/run/tox-bootstrapd/ \
|
||||
&& chown tox:tox /var/lib/tox-bootstrapd/ /var/run/tox-bootstrapd/
|
||||
# Public Key: 122837CCDD474DD1183A83152164D51427044B3EAAA52ED683F6BA602568673B
|
||||
COPY keys /var/lib/tox-bootstrapd/
|
||||
USER tox
|
||||
|
||||
# Use this to generate a keys file:
|
||||
#RUN /usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf --log-backend stdout \
|
||||
# && sleep 1
|
||||
|
||||
WORKDIR /web
|
||||
CMD ["/entrypoint.sh"]
|
6
other/bootstrap_daemon/websocket/entrypoint.sh
Executable file
6
other/bootstrap_daemon/websocket/entrypoint.sh
Executable file
@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eux
|
||||
|
||||
/usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf --log-backend stdout
|
||||
/usr/local/bin/websockify -l "0.0.0.0:$PORT" -t 127.0.0.1:33445
|
1
other/bootstrap_daemon/websocket/keys
Normal file
1
other/bootstrap_daemon/websocket/keys
Normal file
@ -0,0 +1 @@
|
||||
(7<><37>GM<47>:<3A>!d<>'K><3E><>.փ<><D683>`%hg;<3B>G<EFBFBD><47>Y<>:<3A>Zy5<79><35>S<EFBFBD>$<24>|Cڠ<43>Ǹ<EFBFBD>2<10> <09>t
|
17
other/bootstrap_daemon/websocket/websockify/BUILD.bazel
Normal file
17
other/bootstrap_daemon/websocket/websockify/BUILD.bazel
Normal file
@ -0,0 +1,17 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
|
||||
|
||||
package(features = ["-layering_check"])
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["websockify.go"],
|
||||
importpath = "github.com/TokTok/c-toxcore/other/bootstrap_daemon/websocket/websockify",
|
||||
visibility = ["//visibility:private"],
|
||||
deps = ["@com_github_gorilla_websocket//:go_default_library"],
|
||||
)
|
||||
|
||||
go_binary(
|
||||
name = "websockify",
|
||||
embed = [":go_default_library"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
7
other/bootstrap_daemon/websocket/websockify/go.mod
Normal file
7
other/bootstrap_daemon/websocket/websockify/go.mod
Normal file
@ -0,0 +1,7 @@
|
||||
module github.com/TokTok/c-toxcore/other/bootstrap_daemon/websocket/websockify
|
||||
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/gorilla/websocket master
|
||||
)
|
111
other/bootstrap_daemon/websocket/websockify/websockify.go
Normal file
111
other/bootstrap_daemon/websocket/websockify/websockify.go
Normal file
@ -0,0 +1,111 @@
|
||||
// A Go version WebSocket to TCP socket proxy
|
||||
//
|
||||
// This is a heavily modified version of this file:
|
||||
// https://github.com/novnc/websockify-other/blob/master/golang/websockify.go
|
||||
//
|
||||
// Changes include:
|
||||
// - Fix infinite loop on error.
|
||||
// - Proper logging.
|
||||
// - Proper error handling in general.
|
||||
// - Support both websocket and regular GET requests on /.
|
||||
//
|
||||
// Copyright 2022 The TokTok team.
|
||||
// Copyright 2021 Michael.liu.
|
||||
// See LICENSE for licensing conditions.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"flag"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var (
|
||||
sourceAddr = flag.String("l", "127.0.0.1:8080", "http service address")
|
||||
targetAddr = flag.String("t", "127.0.0.1:5900", "tcp service address")
|
||||
)
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
// Should be enough to fit any Tox TCP packets.
|
||||
ReadBufferSize: 2048,
|
||||
WriteBufferSize: 2048,
|
||||
Subprotocols: []string{"binary"},
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
func forwardTCP(wsconn *websocket.Conn, conn net.Conn) {
|
||||
var tcpbuffer [2048]byte
|
||||
defer wsconn.Close()
|
||||
defer conn.Close()
|
||||
for {
|
||||
n, err := conn.Read(tcpbuffer[0:])
|
||||
if err != nil {
|
||||
log.Println("TCP READ :", err)
|
||||
break
|
||||
}
|
||||
log.Println("TCP READ :", n, hex.EncodeToString(tcpbuffer[0:n]))
|
||||
|
||||
if err := wsconn.WriteMessage(websocket.BinaryMessage, tcpbuffer[0:n]); err != nil {
|
||||
log.Println("WS WRITE :", err)
|
||||
break
|
||||
}
|
||||
log.Println("WS WRITE :", n)
|
||||
}
|
||||
}
|
||||
|
||||
func forwardWeb(wsconn *websocket.Conn, conn net.Conn) {
|
||||
defer wsconn.Close()
|
||||
defer conn.Close()
|
||||
for {
|
||||
_, buffer, err := wsconn.ReadMessage()
|
||||
if err != nil {
|
||||
log.Println("WS READ :", err)
|
||||
break
|
||||
}
|
||||
log.Println("WS READ :", len(buffer), hex.EncodeToString(buffer))
|
||||
|
||||
m, err := conn.Write(buffer)
|
||||
if err != nil {
|
||||
log.Println("TCP WRITE:", err)
|
||||
break
|
||||
}
|
||||
log.Println("TCP WRITE:", m)
|
||||
}
|
||||
}
|
||||
|
||||
func serveWs(w http.ResponseWriter, r *http.Request) {
|
||||
ws, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
log.Println("upgrade:", err)
|
||||
return
|
||||
}
|
||||
vnc, err := net.Dial("tcp", *targetAddr)
|
||||
if err != nil {
|
||||
log.Println("dial:", err)
|
||||
return
|
||||
}
|
||||
go forwardTCP(ws, vnc)
|
||||
go forwardWeb(ws, vnc)
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
log.Println("Starting up websockify endpoint")
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Upgrade") == "websocket" {
|
||||
serveWs(w, r)
|
||||
} else {
|
||||
http.ServeFile(w, r, r.URL.Path[1:])
|
||||
}
|
||||
})
|
||||
log.Fatal(http.ListenAndServe(*sourceAddr, nil))
|
||||
}
|
Reference in New Issue
Block a user