commit 68ba48a3a2490295067b93fe6c0e6dfcac747d12 Author: root Date: Wed Oct 30 13:23:52 2024 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84f1eb4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +test_data/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4b950db --- /dev/null +++ b/Dockerfile @@ -0,0 +1,102 @@ +FROM node:20-alpine as frontend +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME/bin:$PATH" +RUN corepack enable +RUN apk add --update python3 make g++ git && rm -rf /var/cache/apk/* +COPY ./NanoKVM/web /web +RUN cd /web && pnpm install && pnpm build +RUN mv /web/dist /static +#RUN git clone https://github.com/sipeed/nanokvm +#RUN cd nanokvm/web && pnpm install && pnpm build +#RUN mv nanokvm/web/dist /static + + + +FROM golang:alpine as backend +RUN apk add --no-cache git portaudio-dev +COPY . /app +WORKDIR /app +RUN go mod download +RUN go build -o ./bin/ ./cmd/... + +FROM alpine as ffmpeg +RUN apk add --no-cache \ + autoconf \ + automake \ + g++ \ + make \ + gcc \ + libc-dev \ + libtool \ + pkgconfig \ + texinfo \ + zlib-dev \ + yasm \ + coreutils \ + nasm \ + bash \ + cmake \ + git \ + libdrm-dev ninja-build meson v4l-utils-dev wget build-base bsd-compat-headers musl-dev + +RUN mkdir -p ~/dev && cd ~/dev && \ + git clone -b jellyfin-mpp --depth=1 https://github.com/nyanmisaka/mpp.git rkmpp && \ + #git clone --depth=1 https://github.com/rockchip-linux/mpp.git rkmpp && \ + cd rkmpp && mkdir rkmpp_build && cd rkmpp_build && \ + sed -i '/#include /a #include ' ~/dev/rkmpp/mpp/vproc/vdpp/test/hwpq_test.cpp && \ + cmake \ + -DCMAKE_INSTALL_PREFIX=/usr \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_SHARED_LIBS=ON \ + -DBUILD_TEST=OFF \ + -DVDPP_TEST=OFF \ + .. && \ + make -j $(nproc) && \ + make install + +RUN mkdir -p ~/dev && cd ~/dev && \ + git clone -b jellyfin-rga --depth=1 https://github.com/nyanmisaka/rk-mirrors.git rkrga && \ + sed -i '/#include "im2d_api\/im2d.h"/a #define mmap64 mmap' rkrga/core/RockchipRga.cpp && \ + meson setup rkrga rkrga_build \ + --prefix=/usr \ + --libdir=lib \ + --buildtype=release \ + --default-library=shared \ + -Dcpp_args=-fpermissive \ + -Dlibdrm=false \ + -Dlibrga_demo=false && \ + meson configure rkrga_build && \ + ninja -C rkrga_build install + +RUN mkdir -p ~/dev && cd ~/dev && \ + git clone --depth=1 https://github.com/nyanmisaka/ffmpeg-rockchip.git ffmpeg && cd ffmpeg && \ + ./configure --prefix=/usr --enable-indev=v4l2 --enable-gpl --enable-version3 --enable-libdrm --enable-rkmpp --enable-rkrga --enable-protocol=http --enable-protocol=tcp \ + #--enable-libx264 --enable-libvpx \ + && \ + make -j $(nproc) && make install + + +FROM pikvm/ustreamer:latest as ustreamer + +FROM alpine +RUN apk add --no-cache portaudio alsa-utils \ + libevent libjpeg-turbo libgpiod libbsd v4l-utils libdrm + +COPY --from=frontend /static /app/static +COPY --from=backend /app/bin/kvm /app/kvm +COPY --from=backend /app/bin/webrtc /app/webrtc +COPY --from=ustreamer /ustreamer/ustreamer /app/ustreamer +COPY --from=ffmpeg /usr/lib/librga.* /usr/lib +COPY --from=ffmpeg /usr/lib/librockchip_mpp.* /usr/lib +COPY --from=ffmpeg /usr/bin/ffmpeg /app/ffmpeg +COPY ./edid.hex /edid.hex +#COPY ./edid-audio.hex /edid.hex +#COPY ./edid-test.hex /edid.hex +#COPY ./edid-dell.hex /edid.hex NV24 +COPY ./entry.sh /entry.sh +COPY ./hid.sh /hid.sh +RUN chmod +x /entry.sh && chmod +x /hid.sh + +EXPOSE 8080 + +ENTRYPOINT ["/entry.sh"] \ No newline at end of file diff --git a/Dockerfile.ffmpeg b/Dockerfile.ffmpeg new file mode 100644 index 0000000..f482f12 --- /dev/null +++ b/Dockerfile.ffmpeg @@ -0,0 +1,48 @@ +FROM ubuntu:24.04 as ffmpeg + +RUN apt-get update && apt-get install -y \ + build-essential \ + cmake \ + pkg-config \ + libv4l-dev \ + libpthread-stubs0-dev \ + ninja-build \ + git libdrm-dev pkg-config \ +# libx264-dev libvpx-dev \ + meson \ + && rm -rf /var/lib/apt/lists/* + +RUN mkdir -p ~/dev && cd ~/dev && \ +# git clone -b jellyfin-mpp --depth=1 https://github.com/nyanmisaka/mpp.git rkmpp && \ + git clone --depth=1 https://github.com/rockchip-linux/mpp.git rkmpp && \ + cd rkmpp && mkdir rkmpp_build && cd rkmpp_build && \ + cmake \ + -DCMAKE_INSTALL_PREFIX=/usr \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_SHARED_LIBS=ON \ + -DBUILD_STATIC_LIBS=ON \ + -DBUILD_TEST=OFF \ + .. && \ + make -j $(nproc) && \ + make install + +RUN mkdir -p ~/dev && cd ~/dev && \ +git clone -b jellyfin-rga --depth=1 https://github.com/nyanmisaka/rk-mirrors.git rkrga && \ +meson setup rkrga rkrga_build \ + --prefix=/usr \ + --libdir=lib \ + --buildtype=release \ + --default-library=shared \ + -Dcpp_args=-fpermissive \ + -Dlibdrm=false \ + -Dlibrga_demo=false && \ +meson configure rkrga_build && \ +ninja -C rkrga_build install + +RUN mkdir -p ~/dev && cd ~/dev && \ +git clone --depth=1 https://github.com/nyanmisaka/ffmpeg-rockchip.git ffmpeg && \ +cd ffmpeg && \ +./configure --prefix=/usr --enable-indev=v4l2 --enable-gpl --enable-version3 --enable-libdrm --enable-rkmpp --enable-rkrga --enable-protocol=http --enable-protocol=tcp \ +#--enable-libx264 --enable-libvpx \ +&& \ +make -j $(nproc) && make install \ No newline at end of file diff --git a/Dockerfile.ffmpeg-alpine b/Dockerfile.ffmpeg-alpine new file mode 100644 index 0000000..51ebf01 --- /dev/null +++ b/Dockerfile.ffmpeg-alpine @@ -0,0 +1,55 @@ +FROM alpine as ffmpeg +RUN apk add --no-cache \ + autoconf \ + automake \ + g++ \ + make \ + gcc \ + libc-dev \ + libtool \ + pkgconfig \ + texinfo \ + zlib-dev \ + yasm \ + coreutils \ + nasm \ + bash \ + cmake \ + git \ + libdrm-dev ninja-build meson v4l-utils-dev wget build-base bsd-compat-headers musl-dev + +RUN mkdir -p ~/dev && cd ~/dev && \ + git clone -b jellyfin-mpp --depth=1 https://github.com/nyanmisaka/mpp.git rkmpp && \ + #git clone --depth=1 https://github.com/rockchip-linux/mpp.git rkmpp && \ + cd rkmpp && mkdir rkmpp_build && cd rkmpp_build && \ + sed -i '/#include /a #include ' ~/dev/rkmpp/mpp/vproc/vdpp/test/hwpq_test.cpp && \ + cmake \ + -DCMAKE_INSTALL_PREFIX=/usr \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_SHARED_LIBS=ON \ + -DBUILD_TEST=OFF \ + -DVDPP_TEST=OFF \ + .. && \ + make -j $(nproc) && \ + make install + +RUN mkdir -p ~/dev && cd ~/dev && \ + git clone -b jellyfin-rga --depth=1 https://github.com/nyanmisaka/rk-mirrors.git rkrga && \ + sed -i '/#include "im2d_api\/im2d.h"/a #define mmap64 mmap' rkrga/core/RockchipRga.cpp && \ + meson setup rkrga rkrga_build \ + --prefix=/usr \ + --libdir=lib \ + --buildtype=release \ + --default-library=shared \ + -Dcpp_args=-fpermissive \ + -Dlibdrm=false \ + -Dlibrga_demo=false && \ + meson configure rkrga_build && \ + ninja -C rkrga_build install + +RUN mkdir -p ~/dev && cd ~/dev && \ + git clone --depth=1 https://github.com/nyanmisaka/ffmpeg-rockchip.git ffmpeg && cd ffmpeg && \ + ./configure --prefix=/usr --enable-indev=v4l2 --enable-gpl --enable-version3 --enable-libdrm --enable-rkmpp --enable-rkrga --enable-protocol=http --enable-protocol=tcp \ + #--enable-libx264 --enable-libvpx \ + && \ + make -j $(nproc) && make install \ No newline at end of file diff --git a/cmd/kvm/main.go b/cmd/kvm/main.go new file mode 100644 index 0000000..3bc19ce --- /dev/null +++ b/cmd/kvm/main.go @@ -0,0 +1,64 @@ +package main + +import ( + "fmt" + "net/http" + "rkkvm/config" + "rkkvm/http/hw/rtc" + "rkkvm/http/hw/stream" + "rkkvm/http/route" + "strings" + + "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" +) + +func main() { + config.Init() + cfg := config.Get() + lvl, err := log.ParseLevel(cfg.LogLevel) + if err == nil { + log.SetLevel(lvl) + } else { + log.Println("Failed to parse log level, use default level: info") + } + + if cfg.Stream.Source == config.StreamSourceMjpeg { + ustreamer := stream.Init(cfg.UStreamer.Path, strings.Split(cfg.UStreamer.Args, " ")) + ustreamer.Start() + //go ustreamer.Watch() + } else if cfg.Stream.Source == config.StreamSourceH264 { + ffmpeg := stream.InitFFmpeg(cfg.FFmpeg.Path, cfg.FFmpeg.FormatArgs()) + ffmpeg.Start() + //go ffmpeg.Watch() + } else { + log.Fatalf("unsupported stream source type: %v", cfg.Stream.Source) + } + + webrtc, err := rtc.Init(cfg.WebRtc.Host, cfg.WebRtc.Port) + if err != nil { + log.Fatal(err) + } + go webrtc.Read() + go webrtc.Listen() + + r := gin.Default() + r.Use(gin.Recovery()) + r.GET("/ping", func(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "pong", + }) + }) + route.Api(r) + route.Static(r) + + if cfg.NanoKVMUISupport { + r.POST("/api/auth/login", func(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "token": "disabled", + }) + }) + } + + r.Run(fmt.Sprintf(":%d", cfg.Port)) +} diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..1b3f02a --- /dev/null +++ b/config/config.go @@ -0,0 +1,92 @@ +package config + +import ( + "fmt" + "strings" +) + +type Config struct { + LogLevel string `yaml:"log_level"` + Auth bool `yaml:"auth"` + AuthSecret string `yaml:"auth_secret"` + Port int `yaml:"port"` + UStreamer UStreamer `yaml:"ustreamer"` + FFmpeg FFmpeg `yaml:"ffmpeg"` + WebRtc WebRtc `yaml:"webrtc"` + Stream Stream `yaml:"stream"` + + NanoKVMUISupport bool `yaml:"nano_kvm_ui_support"` +} + +type WebRtc struct { + Host string `yaml:"host"` + Port int `yaml:"port"` +} + +type ExtProcess struct { + Path string `yaml:"path"` + Args string `yaml:"args"` +} + +type Stream struct { + Source string `yaml:"source"` // mjpeg or h264 +} + +const ( + StreamSourceMjpeg = "mjpeg" + StreamSourceH264 = "h264" +) + +type FFmpeg struct { + ExtProcess + FPS int `yaml:"fps"` + Bitrate int `yaml:"bitrate"` +} + +func (f FFmpeg) FormatArgs() []string { + return strings.Split(fmt.Sprintf(f.Args, f.Bitrate*1000, f.FPS), " ") +} + +type UStreamer struct { + Host string `yaml:"host"` + Port int `yaml:"port"` + ExtProcess +} + +var c Config + +func Get() Config { + return c +} + +func Init() { + c = Config{ + LogLevel: "debug", + Auth: false, + Port: 8080, + UStreamer: UStreamer{ + Host: "0.0.0.0", + Port: 8888, + ExtProcess: ExtProcess{ + Path: "/app/ustreamer", + Args: "--host 0.0.0.0 --port 8888 -m BGR24 -f 60", + }, + }, + FFmpeg: FFmpeg{ + ExtProcess: ExtProcess{ + Path: "/app/ffmpeg", + Args: "-re -i /dev/video0 -c:v h264_rkmpp -b:v %d -framerate %d -bsf:v h264_mp4toannexb -g 10 -f rtp rtp://127.0.0.1:5004?pkt_size=1200", + }, + FPS: 60, + Bitrate: 6000, + }, + WebRtc: WebRtc{ + Host: "127.0.0.1", + Port: 5004, + }, + Stream: Stream{ + Source: StreamSourceMjpeg, + }, + NanoKVMUISupport: true, + } +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e3ae902 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,25 @@ +version: '3' +services: + rkkvm: + image: rkkvm:latest + container_name: rkkvm + privileged: true + volumes: + - /sys/kernel/config:/sys/kernel/config + - ./test_data:/data + devices: + - /dev/hidg0 + - /dev/hidg1 + - /dev/hidg2 + - /dev/video0 + - /dev/snd + - /dev/dri + - /dev/dma_heap + - /dev/mpp_service + - /dev/mali0 + - /dev/rga + #environment: + # - EDID=1 + ports: + - 8888:8080 + restart: unless-stopped \ No newline at end of file diff --git a/edid-audio.hex b/edid-audio.hex new file mode 100755 index 0000000..661aa82 --- /dev/null +++ b/edid-audio.hex @@ -0,0 +1,16 @@ +00FFFFFFFFFFFF005262888800888888 +1C150103800101780AEE91A3544C9926 +0F505425400001000100010001000100 +010001010101D32C80A070381A403020 +350040442100001E7E1D00A050001940 +3020370080001000001E000000FC0050 +492D4B564D20566964656F0A000000FD +00323D0F2E0F000A20202020202001F8 +02030440DE0D20A03058122030203400 +F0B400000018E01500A0400016303020 +3400000000000018B41400A050D01120 +3020350080D810000018AB22A0A05084 +1A3030203600B00E1100001800000000 +00000000000000000000000000000000 +00000000000000000000000000000000 +00000000000000000000000000000005 diff --git a/edid.hex b/edid.hex new file mode 100644 index 0000000..ea72ff9 --- /dev/null +++ b/edid.hex @@ -0,0 +1,16 @@ +00FFFFFFFFFFFF005262888800888888 +1C150103800000780AEE91A3544C9926 +0F505425400001000100010001000100 +010001010101D32C80A070381A403020 +350040442100001E7E1D00A050001940 +3020370080001000001E000000FC0050 +492D4B564D20566964656F0A000000FD +00323D0F2E0F000000000000000001C4 +02030400DE0D20A03058122030203400 +F0B400000018E01500A0400016303020 +3400000000000018B41400A050D01120 +3020350080D810000018AB22A0A05084 +1A3030203600B00E1100001800000000 +00000000000000000000000000000000 +00000000000000000000000000000000 +00000000000000000000000000000045 \ No newline at end of file diff --git a/entry.sh b/entry.sh new file mode 100644 index 0000000..febdd37 --- /dev/null +++ b/entry.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +set -e + +[ -n "$EDID" ] && { + [ -n "$EDID_HEX" ] && echo "$EDID_HEX" > /edid.hex + while true; do + v4l2-ctl --device=/dev/video0 --set-edid=file=/edid.hex --fix-edid-checksums --info-edid && break + echo 'Failed to set EDID. Retrying...' + sleep 1 + done +} + +#/app/ustreamer --host 0.0.0.0 --port 8888 -m BGR24 -r 1280x720 -f 30 & +/hid.sh create +/app/kvm $@ +/hid.sh delete \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d8e6cbb --- /dev/null +++ b/go.mod @@ -0,0 +1,58 @@ +module rkkvm + +go 1.21 + +require ( + github.com/gin-gonic/gin v1.10.0 + github.com/golang-jwt/jwt/v5 v5.2.1 + github.com/gorilla/websocket v1.5.3 + github.com/sirupsen/logrus v1.9.3 +) + +require ( + github.com/bytedance/sonic v1.11.6 // indirect + github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/gin-contrib/static v1.1.2 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.20.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.7 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/pion/datachannel v1.5.9 // indirect + github.com/pion/dtls/v3 v3.0.3 // indirect + github.com/pion/ice/v4 v4.0.2 // indirect + github.com/pion/interceptor v0.1.37 // indirect + github.com/pion/logging v0.2.2 // indirect + github.com/pion/mdns/v2 v2.0.7 // indirect + github.com/pion/randutil v0.1.0 // indirect + github.com/pion/rtcp v1.2.14 // indirect + github.com/pion/rtp v1.8.9 // indirect + github.com/pion/sctp v1.8.33 // indirect + github.com/pion/sdp/v3 v3.0.9 // indirect + github.com/pion/srtp/v3 v3.0.4 // indirect + github.com/pion/stun/v3 v3.0.0 // indirect + github.com/pion/transport/v3 v3.0.7 // indirect + github.com/pion/turn/v4 v4.0.0 // indirect + github.com/pion/webrtc/v4 v4.0.1 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + github.com/wlynxg/anet v0.0.3 // indirect + golang.org/x/arch v0.8.0 // indirect + golang.org/x/crypto v0.28.0 // indirect + golang.org/x/net v0.29.0 // indirect + golang.org/x/sys v0.26.0 // indirect + golang.org/x/text v0.19.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f0ea0bd --- /dev/null +++ b/go.sum @@ -0,0 +1,143 @@ +github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= +github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= +github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-contrib/static v1.1.2 h1:c3kT4bFkUJn2aoRU3s6XnMjJT8J6nNWJkR0NglqmlZ4= +github.com/gin-contrib/static v1.1.2/go.mod h1:Fw90ozjHCmZBWbgrsqrDvO28YbhKEKzKp8GixhR4yLw= +github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= +github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= +github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= +github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= +github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pion/datachannel v1.5.9 h1:LpIWAOYPyDrXtU+BW7X0Yt/vGtYxtXQ8ql7dFfYUVZA= +github.com/pion/datachannel v1.5.9/go.mod h1:kDUuk4CU4Uxp82NH4LQZbISULkX/HtzKa4P7ldf9izE= +github.com/pion/dtls/v3 v3.0.3 h1:j5ajZbQwff7Z8k3pE3S+rQ4STvKvXUdKsi/07ka+OWM= +github.com/pion/dtls/v3 v3.0.3/go.mod h1:weOTUyIV4z0bQaVzKe8kpaP17+us3yAuiQsEAG1STMU= +github.com/pion/ice/v4 v4.0.2 h1:1JhBRX8iQLi0+TfcavTjPjI6GO41MFn4CeTBX+Y9h5s= +github.com/pion/ice/v4 v4.0.2/go.mod h1:DCdqyzgtsDNYN6/3U8044j3U7qsJ9KFJC92VnOWHvXg= +github.com/pion/interceptor v0.1.37 h1:aRA8Zpab/wE7/c0O3fh1PqY0AJI3fCSEM5lRWJVorwI= +github.com/pion/interceptor v0.1.37/go.mod h1:JzxbJ4umVTlZAf+/utHzNesY8tmRkM2lVmkS82TTj8Y= +github.com/pion/logging v0.2.2 h1:M9+AIj/+pxNsDfAT64+MAVgJO0rsyLnoJKCqf//DoeY= +github.com/pion/logging v0.2.2/go.mod h1:k0/tDVsRCX2Mb2ZEmTqNa7CWsQPc+YYCB7Q+5pahoms= +github.com/pion/mdns/v2 v2.0.7 h1:c9kM8ewCgjslaAmicYMFQIde2H9/lrZpjBkN8VwoVtM= +github.com/pion/mdns/v2 v2.0.7/go.mod h1:vAdSYNAT0Jy3Ru0zl2YiW3Rm/fJCwIeM0nToenfOJKA= +github.com/pion/randutil v0.1.0 h1:CFG1UdESneORglEsnimhUjf33Rwjubwj6xfiOXBa3mA= +github.com/pion/randutil v0.1.0/go.mod h1:XcJrSMMbbMRhASFVOlj/5hQial/Y8oH/HVo7TBZq+j8= +github.com/pion/rtcp v1.2.14 h1:KCkGV3vJ+4DAJmvP0vaQShsb0xkRfWkO540Gy102KyE= +github.com/pion/rtcp v1.2.14/go.mod h1:sn6qjxvnwyAkkPzPULIbVqSKI5Dv54Rv7VG0kNxh9L4= +github.com/pion/rtp v1.8.9 h1:E2HX740TZKaqdcPmf4pw6ZZuG8u5RlMMt+l3dxeu6Wk= +github.com/pion/rtp v1.8.9/go.mod h1:pBGHaFt/yW7bf1jjWAoUjpSNoDnw98KTMg+jWWvziqU= +github.com/pion/sctp v1.8.33 h1:dSE4wX6uTJBcNm8+YlMg7lw1wqyKHggsP5uKbdj+NZw= +github.com/pion/sctp v1.8.33/go.mod h1:beTnqSzewI53KWoG3nqB282oDMGrhNxBdb+JZnkCwRM= +github.com/pion/sdp/v3 v3.0.9 h1:pX++dCHoHUwq43kuwf3PyJfHlwIj4hXA7Vrifiq0IJY= +github.com/pion/sdp/v3 v3.0.9/go.mod h1:B5xmvENq5IXJimIO4zfp6LAe1fD9N+kFv+V/1lOdz8M= +github.com/pion/srtp/v3 v3.0.4 h1:2Z6vDVxzrX3UHEgrUyIGM4rRouoC7v+NiF1IHtp9B5M= +github.com/pion/srtp/v3 v3.0.4/go.mod h1:1Jx3FwDoxpRaTh1oRV8A/6G1BnFL+QI82eK4ms8EEJQ= +github.com/pion/stun/v3 v3.0.0 h1:4h1gwhWLWuZWOJIJR9s2ferRO+W3zA/b6ijOI6mKzUw= +github.com/pion/stun/v3 v3.0.0/go.mod h1:HvCN8txt8mwi4FBvS3EmDghW6aQJ24T+y+1TKjB5jyU= +github.com/pion/transport/v3 v3.0.7 h1:iRbMH05BzSNwhILHoBoAPxoB9xQgOaJk+591KC9P1o0= +github.com/pion/transport/v3 v3.0.7/go.mod h1:YleKiTZ4vqNxVwh77Z0zytYi7rXHl7j6uPLGhhz9rwo= +github.com/pion/turn/v4 v4.0.0 h1:qxplo3Rxa9Yg1xXDxxH8xaqcyGUtbHYw4QSCvmFWvhM= +github.com/pion/turn/v4 v4.0.0/go.mod h1:MuPDkm15nYSklKpN8vWJ9W2M0PlyQZqYt1McGuxG7mA= +github.com/pion/webrtc/v4 v4.0.1 h1:6Unwc6JzoTsjxetcAIoWH81RUM4K5dBc1BbJGcF9WVE= +github.com/pion/webrtc/v4 v4.0.1/go.mod h1:SfNn8CcFxR6OUVjLXVslAQ3a3994JhyE3Hw1jAuqEto= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +github.com/wlynxg/anet v0.0.3 h1:PvR53psxFXstc12jelG6f1Lv4MWqE0tI76/hHGjh9rg= +github.com/wlynxg/anet v0.0.3/go.mod h1:eay5PRQr7fIVAMbTbchTnO9gG65Hg/uYGdc7mguHxoA= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= +golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= +golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= +golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= +golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= +golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/hid.sh b/hid.sh new file mode 100755 index 0000000..df979ec --- /dev/null +++ b/hid.sh @@ -0,0 +1,67 @@ +#!/bin/sh + +set -e +GADGET_DIR=/sys/kernel/config/usb_gadget + +if [ ! -d "$GADGET_DIR" ]; then + echo "$DIRECTORY does not exist." + exit 0 +fi + +if [ "$1" = "create" ] +then + if [ -d "$GADGET_DIR/g0" ]; then + echo "HID devices already exists" + + exit 0 + fi + cd $GADGET_DIR && mkdir g0 && cd g0 + + echo 0x3346 > idVendor + echo 0x1009 > idProduct + mkdir strings/0x409 + echo '0123456789ABCDEF' > strings/0x409/serialnumber + echo 'rockchip' > strings/0x409/manufacturer + echo 'rk3588' > strings/0x409/product + + mkdir configs/c.1 + echo 0xE0 > configs/c.1/bmAttributes + echo 120 > configs/c.1/MaxPower + mkdir configs/c.1/strings/0x409 + echo "rk3588" > configs/c.1/strings/0x409/configuration + + # keyboard + mkdir functions/hid.GS0 + echo 1 > functions/hid.GS0/subclass + #echo 1 > functions/hid.GS0/wakeup_on_write + echo 1 > functions/hid.GS0/protocol + echo 6 > functions/hid.GS0/report_length + echo -ne \\x05\\x01\\x09\\x06\\xa1\\x01\\x05\\x07\\x19\\xe0\\x29\\xe7\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08\\x81\\x02\\x95\\x01\\x75\\x08\\x81\\x03\\x95\\x05\\x75\\x01\\x05\\x08\\x19\\x01\\x29\\x05\\x91\\x02\\x95\\x01\\x75\\x03\\x91\\x03\\x95\\x06\\x75\\x08\\x15\\x00\\x25\\x65\\x05\\x07\\x19\\x00\\x29\\x65\\x81\\x00\\xc0 > functions/hid.GS0/report_desc + ln -s functions/hid.GS0 configs/c.1 + + # mouse + mkdir functions/hid.GS1 + # echo 1 > functions/hid.GS1/subclass + #echo 1 > functions/hid.GS1/wakeup_on_write + echo 2 > functions/hid.GS1/protocol + echo -ne \\x34 > functions/hid.GS1/report_length + echo -ne \\x5\\x1\\x9\\x2\\xa1\\x1\\x9\\x1\\xa1\\x0\\x5\\x9\\x19\\x1\\x29\\x3\\x15\\x0\\x25\\x1\\x95\\x3\\x75\\x1\\x81\\x2\\x95\\x1\\x75\\x5\\x81\\x3\\x5\\x1\\x9\\x30\\x9\\x31\\x9\\x38\\x15\\x81\\x25\\x7f\\x75\\x8\\x95\\x3\\x81\\x6\\xc0\\xc0 > functions/hid.GS1/report_desc + ln -s functions/hid.GS1 configs/c.1 + + # touchpad + mkdir functions/hid.GS2 + # echo 1 > functions/hid.GS2/subclass + #echo 1 > functions/hid.GS2/wakeup_on_write + echo 2 > functions/hid.GS2/protocol + echo 6 > functions/hid.GS2/report_length + echo -ne \\x05\\x01\\x09\\x02\\xa1\\x01\\x09\\x01\\xa1\\x00\\x05\\x09\\x19\\x01\\x29\\x03\\x15\\x00\\x25\\x01\\x95\\x03\\x75\\x01\\x81\\x02\\x95\\x01\\x75\\x05\\x81\\x01\\x05\\x01\\x09\\x30\\x09\\x31\\x15\\x00\\x26\\xff\\x7f\\x35\\x00\\x46\\xff\\x7f\\x75\\x10\\x95\\x02\\x81\\x02\\x05\\x01\\x09\\x38\\x15\\x81\\x25\\x7f\\x35\\x00\\x45\\x00\\x75\\x08\\x95\\x01\\x81\\x06\\xc0\\xc0 > functions/hid.GS2/report_desc + ln -s functions/hid.GS2 configs/c.1 + + ls /sys/class/udc/ | cat > UDC +fi + +if [ "$1" = "delete" ] +then + echo '' > $GADGET_DIR/g0/UDC +fi + diff --git a/http/hw/hid/hid.go b/http/hw/hid/hid.go new file mode 100644 index 0000000..4d4adb1 --- /dev/null +++ b/http/hw/hid/hid.go @@ -0,0 +1,64 @@ +package hid + +import ( + "os" + "sync" +) + +const ( + MouseUp = iota + MouseDown + MouseMoveAbsolute + MouseMoveRelative + MouseScroll +) + +const ( + MouseLeft = 1 + MouseRight = 2 + MouseWheel = 3 +) + +const ( + HidMouseLeft byte = 0x01 + HidMouseRight byte = 0x02 + HidMouseWheel byte = 0x04 +) + +const ( + ModifierLCtrl byte = 0x01 + ModifierLShift byte = 0x02 + ModifierLAlt byte = 0x04 + ModifierLGUI byte = 0x08 + ModifierRAlt byte = 0x40 +) + +var ( + hid *Hid + hidOnce sync.Once +) + +type Hid struct { + g0 *os.File + g1 *os.File + g2 *os.File + kbMutex sync.Mutex + mouseMutex sync.Mutex +} + +func (h *Hid) Lock() { + h.kbMutex.Lock() + h.mouseMutex.Lock() +} + +func (h *Hid) Unlock() { + h.kbMutex.Unlock() + h.mouseMutex.Unlock() +} + +func GetHid() *Hid { + hidOnce.Do(func() { + hid = &Hid{} + }) + return hid +} diff --git a/http/hw/hid/keyboard.go b/http/hw/hid/keyboard.go new file mode 100644 index 0000000..40a377a --- /dev/null +++ b/http/hw/hid/keyboard.go @@ -0,0 +1,47 @@ +package hid + +func (h *Hid) Keyboard(queue <-chan []int) { + for event := range queue { + h.kbMutex.Lock() + h.writeKeyboard(event) + h.kbMutex.Unlock() + } +} + +func (h *Hid) writeKeyboard(event []int) { + var data []byte + + if event[0] > 0 { + code := byte(event[0]) + modifier := getModifier(event) + data = []byte{modifier, 0x00, code, 0x00, 0x00, 0x00, 0x00, 0x00} + } else { + data = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} + } + + h.Write(h.g0, data) +} + +func getModifier(event []int) byte { + var modifier byte = 0x00 + + if event[1] == 1 { + modifier |= ModifierLCtrl + } + + if event[2] == 1 { + modifier |= ModifierLShift + } + + if event[3] == 1 { + modifier |= ModifierLAlt + } else if event[3] == 2 { + modifier |= ModifierRAlt + } + + if event[4] == 1 { + modifier |= ModifierLGUI + } + + return modifier +} diff --git a/http/hw/hid/mouse.go b/http/hw/hid/mouse.go new file mode 100644 index 0000000..9271ca1 --- /dev/null +++ b/http/hw/hid/mouse.go @@ -0,0 +1,102 @@ +package hid + +import ( + "encoding/binary" + "errors" + "os" + "time" + + log "github.com/sirupsen/logrus" +) + +func (h *Hid) Mouse(queue <-chan []int) { + for event := range queue { + h.mouseMutex.Lock() + switch event[0] { + case MouseDown: + h.mouseDown(event) + case MouseUp: + h.mouseUp() + case MouseMoveAbsolute: + h.mouseMoveAbsolute(event) + case MouseMoveRelative: + h.mouseMoveRelative(event) + case MouseScroll: + h.scroll(event) + default: + log.Debugf("invalid mouse event: %+v", event) + } + h.mouseMutex.Unlock() + } +} + +func (h *Hid) mouseDown(event []int) { + var button byte + + switch event[1] { + case MouseLeft: + button = HidMouseLeft + case MouseRight: + button = HidMouseRight + case MouseWheel: + button = HidMouseWheel + default: + log.Debugf("invalid mouse button: %+v", event) + return + } + + data := []byte{button, 0, 0, 0} + h.writeWithTimeout(h.g1, data) +} + +func (h *Hid) mouseUp() { + data := []byte{0, 0, 0, 0} + h.writeWithTimeout(h.g1, data) +} + +func (h *Hid) scroll(event []int) { + direction := 0x01 + if event[3] > 0 { + direction = -0x1 + } + + data := []byte{0, 0, 0, byte(direction)} + h.writeWithTimeout(h.g1, data) +} + +func (h *Hid) mouseMoveAbsolute(event []int) { + x := make([]byte, 2) + y := make([]byte, 2) + binary.LittleEndian.PutUint16(x, uint16(event[2])) + binary.LittleEndian.PutUint16(y, uint16(event[3])) + + data := []byte{0, x[0], x[1], y[0], y[1], 0} + h.writeWithTimeout(h.g2, data) +} + +func (h *Hid) mouseMoveRelative(event []int) { + data := []byte{byte(event[1]), byte(event[2]), byte(event[3]), 0} + h.writeWithTimeout(h.g1, data) +} + +func (h *Hid) writeWithTimeout(file *os.File, data []byte) { + deadline := time.Now().Add(8 * time.Millisecond) + _ = file.SetWriteDeadline(deadline) + + _, err := file.Write(data) + if err != nil { + switch { + case errors.Is(err, os.ErrClosed): + log.Debugf("hid already closed, reopen it...") + h.OpenNoLock() + case errors.Is(err, os.ErrDeadlineExceeded): + log.Debugf("write to hid timeout") + default: + log.Errorf("write to hid failed: %s", err) + } + + return + } + + log.Tracef("write to hid: %+v", data) +} diff --git a/http/hw/hid/op.go b/http/hw/hid/op.go new file mode 100644 index 0000000..f4d5479 --- /dev/null +++ b/http/hw/hid/op.go @@ -0,0 +1,73 @@ +package hid + +import ( + "errors" + "os" + + log "github.com/sirupsen/logrus" +) + +func (h *Hid) OpenNoLock() { + var err error + h.CloseNoLock() + + h.g0, err = os.OpenFile("/dev/hidg0", os.O_WRONLY, 0o666) + if err != nil { + log.Errorf("open /dev/hidg0 failed: %s", err) + } + + h.g1, err = os.OpenFile("/dev/hidg1", os.O_WRONLY, 0o666) + if err != nil { + log.Errorf("open /dev/hidg1 failed: %s", err) + } + + h.g2, err = os.OpenFile("/dev/hidg2", os.O_WRONLY, 0o666) + if err != nil { + log.Errorf("open /dev/hidg2 failed: %s", err) + } +} + +func (h *Hid) Open() { + h.kbMutex.Lock() + defer h.kbMutex.Unlock() + h.mouseMutex.Lock() + defer h.mouseMutex.Unlock() + + h.CloseNoLock() + + h.OpenNoLock() +} + +func (h *Hid) CloseNoLock() { + for _, file := range []*os.File{h.g0, h.g1, h.g2} { + if file != nil { + _ = file.Sync() + _ = file.Close() + } + } +} + +func (h *Hid) Close() { + h.kbMutex.Lock() + defer h.kbMutex.Unlock() + h.mouseMutex.Lock() + defer h.mouseMutex.Unlock() + + h.CloseNoLock() +} + +func (h *Hid) Write(file *os.File, data []byte) { + _, err := file.Write(data) + if err != nil { + if errors.Is(err, os.ErrClosed) { + log.Debugf("hid already closed, reopen it...") + h.OpenNoLock() + } else { + log.Errorf("write to hid failed: %s", err) + } + + return + } + + log.Tracef("write to hid: %+v", data) +} diff --git a/http/hw/rtc/webrtc.go b/http/hw/rtc/webrtc.go new file mode 100644 index 0000000..be75a36 --- /dev/null +++ b/http/hw/rtc/webrtc.go @@ -0,0 +1,184 @@ +package rtc + +import ( + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "io" + "net" + + log "github.com/sirupsen/logrus" + + "github.com/pion/webrtc/v4" +) + +// https://github.com/pion/example-webrtc-applications/blob/master/sfu-ws/main.go + +var rtc *RTC + +func Get() *RTC { + return rtc +} + +var ErrPeerClosedConn = errors.New("webrtc: peer closed conn") + +type RTC struct { + l *net.UDPConn + peer *webrtc.PeerConnection + track *webrtc.TrackLocalStaticRTP + sender *webrtc.RTPSender + localSession string +} + +func (r *RTC) Close() error { + return r.l.Close() +} + +// Read incoming RTCP packets +// Before these packets are returned they are processed by interceptors. For things +// like NACK this needs to be called. +func (r *RTC) Read() { + rtcpBuf := make([]byte, 1500) + for { + if _, _, rtcpErr := r.sender.Read(rtcpBuf); rtcpErr != nil { + log.Errorf("failed to read RTCP packet: %v", rtcpErr) + return + } + } +} + +func Init(host string, port int) (*RTC, error) { + peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{ + ICEServers: []webrtc.ICEServer{ + { + URLs: []string{"stun:stun.l.google.com:19302"}, + }, + }, + }) + if err != nil { + return nil, fmt.Errorf("failed to create peer connection: %v", err) + } + + // Open a UDP Listener for RTP Packets on port 5004 + l, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP(host), Port: port}) + if err != nil { + return nil, fmt.Errorf("failed to init webrtc listener: %v", err) + } + + // Increase the UDP receive buffer size + // Default UDP buffer sizes vary on different operating systems + bufferSize := 300000 // 300KB + err = l.SetReadBuffer(bufferSize) + if err != nil { + return nil, fmt.Errorf("failed to set read buffer: %v", err) + } + + track, err := webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeH264}, "video", "pion") + if err != nil { // it should never happens + panic(fmt.Sprintf("failed to create video track: %v", err)) + } + + rtpSender, err := peerConnection.AddTrack(track) + if err != nil { + return nil, fmt.Errorf("failed to add track to peer connection: %v", err) + } + + r := &RTC{ + peer: peerConnection, + sender: rtpSender, + track: track, + l: l, + } + rtc = r + + return r, nil +} + +func (r *RTC) Handshake(clientSession string) (string, error) { + // Set the handler for ICE connection state + // This will notify you when the peer has connected/disconnected + r.peer.OnICEConnectionStateChange(func(connState webrtc.ICEConnectionState) { + log.Infof("Connection State has changed %s", connState.String()) + + if connState == webrtc.ICEConnectionStateFailed { + if closeErr := r.peer.Close(); closeErr != nil { + panic(closeErr) + } + } + }) + + // Wait for the offer to be pasted + offer := webrtc.SessionDescription{} + decode(clientSession, &offer) + fmt.Printf("Offer: %+v\n", offer) + + // Set the remote SessionDescription + if err := r.peer.SetRemoteDescription(offer); err != nil { + return "", fmt.Errorf("failed to set remote session description: %v", err) + } + + // Create answer + answer, err := r.peer.CreateAnswer(nil) + if err != nil { + return "", fmt.Errorf("failed to create answer: %v", err) + } + + // Create channel that is blocked until ICE Gathering is complete + gatherComplete := webrtc.GatheringCompletePromise(r.peer) + + // Sets the LocalDescription, and starts our UDP listeners + if err = r.peer.SetLocalDescription(answer); err != nil { + return "", fmt.Errorf("failed to set local description: %v", err) + } + + // Block until ICE Gathering is complete, disabling trickle ICE + // we do this because we only can exchange one signaling message + // in a production application you should exchange ICE Candidates via OnICECandidate + <-gatherComplete + + r.localSession = encode(r.peer.LocalDescription()) + return r.localSession, nil +} + +func (r *RTC) Listen() error { + // Read RTP packets forever and send them to the WebRTC Client + inboundRTPPacket := make([]byte, 1600) // UDP MTU + for { + n, _, err := r.l.ReadFrom(inboundRTPPacket) + if err != nil { + return fmt.Errorf("error during read: %v", err) + } + + if _, err = r.track.Write(inboundRTPPacket[:n]); err != nil { + if errors.Is(err, io.ErrClosedPipe) { + // The peerConnection has been closed. + return ErrPeerClosedConn + } + + return fmt.Errorf("failed to send RTP packet to client: %v", err) + } + } +} + +// JSON encode + base64 a SessionDescription +func encode(obj *webrtc.SessionDescription) string { + b, err := json.Marshal(obj) + if err != nil { + panic(err) + } + + return base64.StdEncoding.EncodeToString(b) +} + +// Decode a base64 and unmarshal JSON into a SessionDescription +func decode(in string, obj *webrtc.SessionDescription) { + b, err := base64.StdEncoding.DecodeString(in) + if err != nil { + panic(err) + } + + if err = json.Unmarshal(b, obj); err != nil { + panic(err) + } +} diff --git a/http/hw/stream/audio.go b/http/hw/stream/audio.go new file mode 100644 index 0000000..2c67d4f --- /dev/null +++ b/http/hw/stream/audio.go @@ -0,0 +1,33 @@ +package stream + +import ( + "github.com/gin-gonic/gin" + "io" + "net/http" + "os/exec" +) + +func AudioHandler(c *gin.Context) { + cmd := exec.Command("ffmpeg", + "-f", "alsa", + "-i", "hw:0,0", + "-acodec", "aac", + "-f", "mp4", "-") + // ffmpeg -f alsa -i hw:0,0 -acodec aac -f mp4 - + stdout, err := cmd.StdoutPipe() + if err != nil { + c.String(http.StatusInternalServerError, "Failed to capture audio: %v", err) + return + } + if err := cmd.Start(); err != nil { + c.String(http.StatusInternalServerError, "Failed to start ffmpeg: %v", err) + return + } + defer cmd.Wait() + + c.Header("Content-Type", "audio/mp4") + if _, err := io.Copy(c.Writer, stdout); err != nil { + c.String(http.StatusInternalServerError, "Failed to stream audio: %v", err) + return + } +} diff --git a/http/hw/stream/extprocess.go b/http/hw/stream/extprocess.go new file mode 100644 index 0000000..92347c3 --- /dev/null +++ b/http/hw/stream/extprocess.go @@ -0,0 +1,135 @@ +package stream + +import ( + "io" + "os" + "os/exec" + "sync" + + log "github.com/sirupsen/logrus" +) + +type ExtProcess struct { + path string + cmd *exec.Cmd + mu sync.Mutex + args []string + running bool + stopChan chan struct{} + finished chan struct{} + state State + + stdin io.WriteCloser +} + +func Init(path string, args []string) *ExtProcess { + return &ExtProcess{ + args: args, + path: path, + } +} + +func (u *ExtProcess) Start() { + u.mu.Lock() + defer u.mu.Unlock() + + if u.running { + log.Debug("process is already running.") + return + } + + u.stopChan = make(chan struct{}) + u.finished = make(chan struct{}) + + log.Debug("Starting external process...") + u.cmd = exec.Command(u.path, u.args...) + u.cmd.Stdout = os.Stdout + u.cmd.Stderr = os.Stderr + stdin, err := u.cmd.StdinPipe() + if err != nil { + log.Errorf("Couldn't handle stdin pipe: %v", err) + return + } + u.stdin = stdin + + err = u.cmd.Start() + if err != nil { + log.Errorf("Failed to start process: %v", err) + } + u.running = true + + go func() { + err = u.cmd.Wait() + u.running = false + log.Errorf("process exited with error: %v", err) + + // planned stop + if err == nil { + u.finished <- struct{}{} + close(u.stopChan) + return + } + + u.stopChan <- struct{}{} + close(u.finished) + }() +} + +// Stop the ustreamer application +func (u *ExtProcess) Stop() { + u.mu.Lock() + defer u.mu.Unlock() + + if !u.running { + log.Debug("process is not running.") + return + } + + log.Warn("Stopping process...") + _, err := u.stdin.Write([]byte("q")) // TODO: works for ffmpeg, should be general or move into FFmpeg struct + if err != nil { + log.Errorf("Failed to stop process: %v", err) + + if err := u.cmd.Process.Kill(); err != nil { + log.Errorf("Failed to kill process: %v", err) + } + } + + if err := u.stdin.Close(); err != nil { + log.Errorf("Failed to close stdin: %v", err) + } + + log.Info("waiting for finish") + + select { + case <-u.finished: + log.Info("stopped as expected") + case <-u.stopChan: + log.Info("was killed") + } + + u.running = false +} + +func (u *ExtProcess) ChangeArgs(newArgs []string) { + u.mu.Lock() + defer u.mu.Unlock() + + u.args = newArgs + log.Printf("Updated process arguments: %v", u.args) +} + +func (u *ExtProcess) Watch() { + for { + select { + case <-u.stopChan: + log.Errorf("process stopped unexpectedly. Restarting...") + u.Start() + /*case <-time.After(1 * time.Second): // Adjust the monitoring interval as needed + if !u.running { + log.Errorf("process is not running. Restarting...") + u.Start() + }*/ + } + } +} diff --git a/http/hw/stream/ffmpeg.go b/http/hw/stream/ffmpeg.go new file mode 100644 index 0000000..d3f8734 --- /dev/null +++ b/http/hw/stream/ffmpeg.go @@ -0,0 +1,53 @@ +package stream + +import ( + "rkkvm/config" +) + +//h264 +//ffmpeg -re -i /dev/video0 -c:v h264_rkmpp -b:v 2000000 -bsf:v h264_mp4toannexb -g 10 -f rtp rtp://127.0.0.1:5004?pkt_size=1200 + +//h265 +//ffmpeg -re -i /dev/video0 -c:v hevc_rkmpp -b:v 2000000 -g 10 -f rtp rtp://127.0.0.1:5004?pkt_size=1200 + +// vp8 +//ffmpeg -re -i /dev/video0 -c:v libvpx -crf 10 -b:v 3M -deadline 1 -g 10 -error-resilient 1 -auto-alt-ref 1 -an -f rtp rtp://127.0.0.1:5004?pkt_size=1200 + +// https://jsfiddle.net/z7ms3u5r/ + +var ffmpeg *FFmpeg + +type FFmpeg struct { + *ExtProcess + config.FFmpeg +} + +func (f *FFmpeg) SetBitrate(b int) { + f.Bitrate = b + if f.Bitrate < 0 { + f.Bitrate = 6000 + } + + f.ChangeArgs(f.FormatArgs()) +} + +func (f *FFmpeg) SetFPS(fps int) { + f.FPS = fps + if f.FPS < 0 { + f.FPS = 30 + } + + f.ChangeArgs(f.FormatArgs()) +} + +func InitFFmpeg(path string, args []string) *FFmpeg { + ffmpeg = &FFmpeg{ + ExtProcess: Init(path, args), + FFmpeg: config.Get().FFmpeg, + } + return ffmpeg +} + +func GetFFmpeg() *FFmpeg { + return ffmpeg +} diff --git a/http/hw/stream/ustreamer.go b/http/hw/stream/ustreamer.go new file mode 100644 index 0000000..3c2f735 --- /dev/null +++ b/http/hw/stream/ustreamer.go @@ -0,0 +1,52 @@ +package stream + +import ( + "rkkvm/config" + "time" + + log "github.com/sirupsen/logrus" +) + +var ustreamer *UStreamer + +type UStreamer struct { + *ExtProcess + config.UStreamer +} + +func InitUStreamer(path string, args []string) *UStreamer { + ustreamer = &UStreamer{ + ExtProcess: Init(path, args), + UStreamer: config.Get().UStreamer, + } + return ustreamer +} + +func GetUStreamer() *UStreamer { + return ustreamer +} + +func (u *UStreamer) MonitorState() { + ticker := time.NewTicker(1 * time.Second) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + { + state, err := GetState() + if err != nil { + u.state = State{} + log.Errorf("Failed to get process state: %v", err) + continue + } + + u.state = state + } + } + } +} + +func (u *UStreamer) GetState() State { + return u.state +} diff --git a/http/hw/stream/video.go b/http/hw/stream/video.go new file mode 100644 index 0000000..58d88c9 --- /dev/null +++ b/http/hw/stream/video.go @@ -0,0 +1,189 @@ +package stream + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "rkkvm/config" + "rkkvm/http/hw/rtc" + "strconv" + + "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" +) + +/* +{ + "ok": true, + "result": { + "instance_id": "", + "encoder": { + "type": "CPU", + "quality": 80 + }, + "source": { + "resolution": { + "width": 1920, + "height": 1080 + }, + "online": true, + "desired_fps": 30, + "captured_fps": 50 + }, + "stream": { + "queued_fps": 26, + "clients": 1, + "clients_stat": { + "2d90d210e837a19c": { + "fps": 26, + "extra_headers": false, + "advance_headers": false, + "dual_final_frames": false, + "zero_data": false, + "key": "0" + } + } + } + } +} +*/ + +type State struct { + Ok bool `json:"ok"` + Result struct { + InstanceID string `json:"instance_id"` + Encoder struct { + Type string `json:"type"` + Quality int `json:"quality"` + } `json:"encoder"` + Source struct { + Resolution struct { + Width int `json:"width"` + Height int `json:"height"` + } `json:"resolution"` + Online bool `json:"online"` + DesiredFPS int `json:"desired_fps"` + CapturedFPS int `json:"captured_fps"` + } `json:"source"` + Stream struct { + QueuedFPS int `json:"queued_fps"` + Clients int `json:"clients"` + ClientsStat map[string]struct { + FPS int `json:"fps"` + ExtraHeaders bool `json:"extra_headers"` + AdvanceHeaders bool `json:"advance_headers"` + DualFinalFrames bool `json:"dual_final_frames"` + ZeroData bool `json:"zero_data"` + Key string `json:"key"` + } + } `json:"stream"` + } `json:"result"` +} + +func FormatHttpUrl() string { + cfg := config.Get() + return fmt.Sprintf("http://%s:%d", cfg.UStreamer.Host, cfg.UStreamer.Port) +} + +func GetState() (State, error) { + url := FormatHttpUrl() + "/state" + + rsp, err := http.Get(url) + if err != nil { + log.Errorf("Failed to get state: %v", err) + return State{}, err + } + defer rsp.Body.Close() + + if rsp.StatusCode != http.StatusOK { + log.Errorf("Invalid status code: %d", rsp.StatusCode) + return State{}, err + } + + var state State + if err := json.NewDecoder(rsp.Body).Decode(&state); err != nil { + log.Errorf("Failed to decode state: %v", err) + return State{}, err + } + + return state, nil +} + +func MjpegHandler(c *gin.Context) { + url := FormatHttpUrl() + "/stream" + + rsp, err := http.Get(url) + if err != nil { + c.String(http.StatusInternalServerError, "Failed to get stream") + log.Errorf("Failed to get stream: %v", err) + return + } + defer rsp.Body.Close() + + if rsp.StatusCode != http.StatusOK { + c.String(rsp.StatusCode, "Invalid status code") + return + } + + c.Header("Content-Type", rsp.Header.Get("Content-Type")) + buf := make([]byte, 1024*1024) + _, err = io.CopyBuffer(c.Writer, rsp.Body, buf) + if err != nil { + c.String(http.StatusInternalServerError, "Failed to passthrough stream") + log.Errorf("Failed to passthrough stream: %v", err) + return + } +} + +func WebRTCHandshake(c *gin.Context) { + str, err := c.GetRawData() + if err != nil { + c.String(http.StatusBadRequest, err.Error()) + return + } + + log.Debugf("Client session description: %s", string(str)) + + r := rtc.Get() + localSession, err := r.Handshake(string(str)) + if err != nil { + c.String(http.StatusBadRequest, err.Error()) + return + } + + c.String(http.StatusOK, localSession) +} + +func WebRTCSettings(c *gin.Context) { + bitrateStr := c.Query("bitrate") + fpsStr := c.Query("fps") + + if fpsStr == "" && bitrateStr == "" { + c.String(http.StatusBadGateway, "please specify bitrate or fps parameters") + return + } + + ffmpeg := GetFFmpeg() + + if len(bitrateStr) > 0 { + bitrate, err := strconv.Atoi(bitrateStr) + if err != nil { + c.String(http.StatusBadRequest, "wrong bitrate value") + return + } + ffmpeg.SetBitrate(bitrate) + } + + if len(fpsStr) > 0 { + fps, err := strconv.Atoi(fpsStr) + if err != nil { + c.String(http.StatusBadRequest, "wrong fps value") + return + } + ffmpeg.SetFPS(fps) + } + + ffmpeg.Stop() + ffmpeg.Start() +} diff --git a/http/middleware/jwt.go b/http/middleware/jwt.go new file mode 100644 index 0000000..a7f1c68 --- /dev/null +++ b/http/middleware/jwt.go @@ -0,0 +1,82 @@ +package middleware + +import ( + "github.com/golang-jwt/jwt/v5" + "net/http" + "time" + + "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" + "rkkvm/config" +) + +type Token struct { + Username string `json:"username"` + jwt.RegisteredClaims +} + +const ( + ExpireDuration = 31 * 24 * time.Hour // Month +) + +func CheckToken() gin.HandlerFunc { + return func(c *gin.Context) { + conf := config.Get() + + if !conf.Auth { + c.Next() + return + } + + var cookieName string + if conf.NanoKVMUISupport { + cookieName = "nano-kvm-token" + } else { + cookieName = "auth-token" + } + cookie, err := c.Cookie(cookieName) + if err == nil { + _, err = ParseJWT(cookie) + if err == nil { + c.Next() + return + } + } + + c.JSON(http.StatusUnauthorized, "unauthorized") + c.Abort() + } +} + +func GenerateJWT(username string) (string, error) { + cfg := config.Get() + + claims := Token{ + Username: username, + RegisteredClaims: jwt.RegisteredClaims{ + ExpiresAt: jwt.NewNumericDate(time.Now().Add(ExpireDuration)), + }, + } + + t := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) + + return t.SignedString([]byte(cfg.AuthSecret)) +} + +func ParseJWT(jwtToken string) (*Token, error) { + cfg := config.Get() + + t, err := jwt.ParseWithClaims(jwtToken, &Token{}, func(token *jwt.Token) (interface{}, error) { + return []byte(cfg.AuthSecret), nil + }) + if err != nil { + log.Debugf("parse jwt error: %s", err) + return nil, err + } + + if claims, ok := t.Claims.(*Token); ok && t.Valid { + return claims, nil + } else { + return nil, err + } +} diff --git a/http/route/api.go b/http/route/api.go new file mode 100644 index 0000000..1514671 --- /dev/null +++ b/http/route/api.go @@ -0,0 +1,21 @@ +package route + +import ( + "rkkvm/http/hw/stream" + "rkkvm/http/middleware" + "rkkvm/http/ws" + + "github.com/gin-gonic/gin" +) + +func Api(e *gin.Engine) { + api := e.Group("/api").Use(middleware.CheckToken()) + + api.GET("/stream/mjpeg", stream.MjpegHandler) + api.GET("/stream/audio", stream.AudioHandler) + api.GET("/ws", ws.ConnHandler) + + api.POST("/stream/webrtc", stream.WebRTCHandshake) + + api.GET("/stream/webrtc", stream.WebRTCSettings) +} diff --git a/http/route/static.go b/http/route/static.go new file mode 100644 index 0000000..6fc1773 --- /dev/null +++ b/http/route/static.go @@ -0,0 +1,20 @@ +package route + +import ( + "fmt" + "github.com/gin-contrib/static" + "github.com/gin-gonic/gin" + "os" + "path/filepath" +) + +func Static(e *gin.Engine) { + execPath, err := os.Executable() + if err != nil { + panic("invalid executable path") + } + + execDir := filepath.Dir(execPath) + webPath := fmt.Sprintf("%s/static", execDir) + e.Use(static.Serve("/", static.LocalFile(webPath, true))) +} diff --git a/http/ws/ws.go b/http/ws/ws.go new file mode 100644 index 0000000..60c26c0 --- /dev/null +++ b/http/ws/ws.go @@ -0,0 +1,159 @@ +package ws + +import ( + "encoding/json" + "net/http" + "rkkvm/config" + "rkkvm/http/hw/hid" + + "github.com/gin-gonic/gin" + "github.com/gorilla/websocket" + log "github.com/sirupsen/logrus" + + "time" +) + +type Stream struct { + Type string `json:"type"` + State int `json:"state"` +} + +const ( + KeyboardEvent int = 1 + MouseEvent int = 2 +) + +type client struct { + conn *websocket.Conn + hid *hid.Hid + keyboard chan []int + mouse chan []int + watcher chan struct{} +} + +var upgrader = websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, + CheckOrigin: func(r *http.Request) bool { + return true + }, +} + +func ConnHandler(c *gin.Context) { + conn, err := upgrader.Upgrade(c.Writer, c.Request, nil) + if err != nil { + log.Errorf("Couldn't upgrade websocket: %s", err) + return + } + log.Debugf("websocket connected") + + cl := &client{ + hid: hid.GetHid(), + conn: conn, + keyboard: make(chan []int, 200), + mouse: make(chan []int, 200), + watcher: make(chan struct{}, 1), + } + + go cl.Start() +} + +func (c *client) Start() { + defer c.Clean() + + c.hid.Open() + + go c.hid.Keyboard(c.keyboard) + go c.hid.Mouse(c.mouse) + + if config.Get().NanoKVMUISupport { + go c.Watch() + } + + _ = c.Read() +} + +func (c *client) Read() error { + var zeroTime time.Time + _ = c.conn.SetReadDeadline(zeroTime) + + for { + _, message, err := c.conn.ReadMessage() + if err != nil { + return err + } + + log.Tracef("receive message: %s", message) + + var event []int + err = json.Unmarshal(message, &event) + if err != nil { + log.Debugf("receive invalid message: %s", message) + continue + } + + if event[0] == KeyboardEvent { + c.keyboard <- event[1:] + } else if event[0] == MouseEvent { + c.mouse <- event[1:] + } + } +} + +func (c *client) Write(message []byte) error { + _ = c.conn.SetWriteDeadline(time.Now().Add(10 * time.Second)) + return c.conn.WriteMessage(websocket.TextMessage, message) +} + +func (c *client) Watch() { + ticker := time.NewTicker(1 * time.Second) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + { + //state := stream.Get().GetState() + /*state, err := stream.GetState() + if err != nil { + continue + }*/ + + message, _ := json.Marshal(&Stream{ + Type: "stream", + //State: utils.BoolToInt(state.Result.Source.Online), + State: 1, + }) + + err := c.Write(message) + if err != nil { + return + } + } + + case <-c.watcher: + return + } + } +} + +func (c *client) Clean() { + _ = c.conn.Close() + + go clearQueue(c.keyboard) + close(c.keyboard) + + go clearQueue(c.mouse) + close(c.mouse) + + close(c.watcher) + + c.hid.Close() + + log.Debug("websocket disconnected") +} + +func clearQueue(queue chan []int) { + for range queue { + } +} diff --git a/utils/io.go b/utils/io.go new file mode 100644 index 0000000..c73c99b --- /dev/null +++ b/utils/io.go @@ -0,0 +1,50 @@ +package utils + +import ( + "errors" + "io" + "sync" +) + +var ( + blockSize = 32 +) + +var ( + ErrNoBuf = errors.New("no buffer can be used") + + pool = sync.Pool{ + New: func() interface{} { + return make([]byte, blockSize*1024) + }, + } +) + +func getBuffer() []byte { + if buffer, ok := pool.Get().([]byte); ok { + return buffer + } + return nil +} + +func putBuffer(buffer []byte) { + pool.Put(buffer) +} + +func Copy(dst io.Writer, src io.Reader) (int64, error) { + if wt, ok := src.(io.WriterTo); ok { + return wt.WriteTo(dst) + } + + if rt, ok := dst.(io.ReaderFrom); ok { + return rt.ReadFrom(src) + } + + if buffer := getBuffer(); nil != buffer { + defer putBuffer(buffer) + + return io.CopyBuffer(dst, src, buffer) + } + + return 0, ErrNoBuf +} diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..f7cc553 --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,8 @@ +package utils + +func BoolToInt(b bool) int { + if b { + return 1 + } + return 0 +}