ws
This commit is contained in:
parent
28dc905e2f
commit
72ecc247d1
@ -225,7 +225,8 @@ function loadWasm() {
|
||||
}
|
||||
|
||||
function callMain() {
|
||||
const fullargs = [ './minetest', ...mtLauncher.args.toArray() ];
|
||||
const fullargs = [ './minetest', '--address', '10.0.0.22', '--port', '30000', '--name', 'user', '--go'];
|
||||
//const fullargs = [ './minetest', ...mtLauncher.args.toArray() ];
|
||||
const [argc, argv] = makeArgv(fullargs);
|
||||
emloop_invoke_main(argc, argv);
|
||||
// Pausing and unpausing here gives the browser time to redraw the DOM
|
||||
|
2
go.mod
2
go.mod
@ -2,4 +2,4 @@ module minetest-web
|
||||
|
||||
go 1.21.1
|
||||
|
||||
require github.com/gorilla/websocket v1.5.0 // indirect
|
||||
require github.com/gorilla/websocket v1.5.0
|
||||
|
34
main.go
34
main.go
@ -1,15 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var upgrader = websocket.Upgrader{}
|
||||
|
||||
func main() {
|
||||
|
||||
fs := http.FileServer(http.FS(os.DirFS(".")))
|
||||
@ -19,34 +14,7 @@ func main() {
|
||||
fs.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
http.HandleFunc("/proxy", func(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// https://github.com/paradust7/webshims/blob/main/src/emsocket/proxy.js
|
||||
t, data, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
fmt.Printf("Err: %v\n", err)
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("RX: type=%d, data=%s\n", t, data)
|
||||
conn.WriteMessage(websocket.BinaryMessage, []byte("PROXY OK"))
|
||||
|
||||
t, data, err = conn.ReadMessage()
|
||||
if err != nil {
|
||||
fmt.Printf("Err: %v\n", err)
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("RX: type=%d, data=%s\n", t, data)
|
||||
|
||||
conn.Close()
|
||||
})
|
||||
http.HandleFunc("/proxy", HandleProxy)
|
||||
|
||||
err := http.ListenAndServe(":8080", nil)
|
||||
if err != nil {
|
||||
|
12
send.go
Normal file
12
send.go
Normal file
@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func SendError(w http.ResponseWriter, status int, err error) {
|
||||
fmt.Printf("HTTP Error %d: '%s'\n", status, err.Error())
|
||||
w.WriteHeader(status)
|
||||
w.Write([]byte(err.Error()))
|
||||
}
|
72
ws.go
Normal file
72
ws.go
Normal file
@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var upgrader = websocket.Upgrader{}
|
||||
|
||||
func HandleProxy(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
SendError(w, 500, err)
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
err = HandleConnection(conn)
|
||||
if err != nil {
|
||||
fmt.Printf("WS Error: %s\n", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func HandleConnection(conn *websocket.Conn) error {
|
||||
_, data, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
parts := strings.Split(string(data), " ")
|
||||
if len(parts) != 5 {
|
||||
return fmt.Errorf("invalid command: '%s'", data)
|
||||
}
|
||||
if parts[0] != "PROXY" {
|
||||
return fmt.Errorf("command not implemented: '%s'", parts[0])
|
||||
}
|
||||
if parts[1] != "IPV4" {
|
||||
return fmt.Errorf("ip version not implemented: '%s'", parts[1])
|
||||
}
|
||||
protocol := parts[2]
|
||||
host := parts[3]
|
||||
port, _ := strconv.ParseInt(parts[4], 10, 32)
|
||||
|
||||
if port < 1 || port >= 65536 {
|
||||
return fmt.Errorf("invalid port: %d", port)
|
||||
}
|
||||
|
||||
switch protocol {
|
||||
case "TCP":
|
||||
case "UDP":
|
||||
default:
|
||||
return fmt.Errorf("protocol not implemented: '%s'", parts[2])
|
||||
}
|
||||
|
||||
fmt.Printf("Connecting to '%s:%d'\n", host, port)
|
||||
|
||||
conn.WriteMessage(websocket.TextMessage, []byte("PROXY OK"))
|
||||
|
||||
_, data, err = conn.ReadMessage()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("RX: data=%s\n", data)
|
||||
return nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user