diff --git a/examples/term-and-vnc/.air.toml b/examples/term-and-vnc/.air.toml new file mode 100644 index 0000000..b2a3222 --- /dev/null +++ b/examples/term-and-vnc/.air.toml @@ -0,0 +1,51 @@ +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] + args_bin = [] + bin = "./tmp/main" + cmd = "go build -o ./tmp/main ." + delay = 1000 + exclude_dir = ["assets", "tmp", "vendor", "testdata"] + exclude_file = [] + exclude_regex = ["_test.go"] + exclude_unchanged = false + follow_symlink = false + full_bin = "" + include_dir = [] + include_ext = ["go", "tpl", "tmpl", "html"] + include_file = [] + kill_delay = "0s" + log = "build-errors.log" + poll = false + poll_interval = 0 + post_cmd = [] + pre_cmd = [] + rerun = false + rerun_delay = 500 + send_interrupt = false + stop_on_error = true + +[color] + app = "" + build = "yellow" + main = "magenta" + runner = "green" + watcher = "cyan" + +[log] + main_only = false + time = false + +[misc] + clean_on_exit = false + +[proxy] + app_port = 0 + enabled = false + proxy_port = 0 + +[screen] + clear_on_rebuild = false + keep_scroll = true diff --git a/examples/term-and-vnc/.env.sample b/examples/term-and-vnc/.env.sample new file mode 100644 index 0000000..fcc4937 --- /dev/null +++ b/examples/term-and-vnc/.env.sample @@ -0,0 +1,5 @@ +PROXMOX_USERNAME=root@pam +PROXMOX_PASSWORD=password +PROXMOX_URL=https://192.168.0.200:8006/api2/json +PROXMOX_NODE=proxmox5 +PROXMOX_VM=216 \ No newline at end of file diff --git a/examples/term-and-vnc/.gitignore b/examples/term-and-vnc/.gitignore new file mode 100644 index 0000000..5736aa3 --- /dev/null +++ b/examples/term-and-vnc/.gitignore @@ -0,0 +1,26 @@ +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ +tmp/ + +# Go workspace file +go.work +go.work.sum + +# env file +.env \ No newline at end of file diff --git a/examples/term-and-vnc/README.md b/examples/term-and-vnc/README.md new file mode 100644 index 0000000..a03e15b --- /dev/null +++ b/examples/term-and-vnc/README.md @@ -0,0 +1,78 @@ +# Proxmox VNC and Terminal Proxy + +This project provides a Go-based server for interacting with Proxmox virtual machines via VNC and terminal proxies. The server supports secure WebSocket connections and can be accessed through a simple web client. + +## Getting Started + +### Prerequisites + +- Go 1.18 or higher +- OpenSSL for generating SSL certificates +- Proxmox environment with a valid configuration + +### Installation + +1. **Generate SSL Certificates** + + Create self-signed SSL certificates for secure communication: + + ```bash + openssl req -newkey rsa:2048 -nodes -keyout server.key -x509 -days 365 -out server.crt + ``` + +2. **Create the `.env` File** + + Add the following environment variables to a `.env` file in the root directory: + + ```plaintext + PROXMOX_USERNAME=root@pam + PROXMOX_PASSWORD=password + PROXMOX_URL=https://192.168.0.200:8006/api2/json + PROXMOX_NODE=proxmox5 + PROXMOX_VM=216 + ``` + +3. **Install Dependencies** + + Install the necessary Go modules: + + ```bash + go mod tidy + ``` + +### Running the Server + +Start the server with SSL enabled: + +```bash +go run main.go +``` + +The server will be accessible at `https://localhost:8523`. + +### Endpoints + +- **`/`**: Returns "hello world". +- **`/term`**: WebSocket endpoint for terminal access. +- **`/vnc`**: WebSocket endpoint for VNC access. Requires a valid ticket. +- **`/vnc-ticket`**: Generates and returns a VNC ticket. + +### Code Overview + +- **`main.go`**: The main application file that sets up the server and routes. +- **`impl/term.go`**: Contains the implementation for the terminal WebSocket endpoint. +- **`impl/vnc.go`**: Contains the implementation for the VNC WebSocket endpoint and ticket generation. + +### Code Overview + +- **`main.go`**: The main application file that sets up the server and routes. It configures logging, loads environment variables, and starts the server with SSL. + +- **`impl/term.go`**: Contains the implementation for the terminal WebSocket endpoint. It sets up a WebSocket connection for terminal access and handles communication between the client and the Proxmox virtual machine. + +- **`impl/vnc.go`**: Contains the implementation for the VNC WebSocket endpoint and ticket generation. It manages VNC connections and tickets: + - The `tickets` map is used for learning purposes to store VNC tickets temporarily. In a real-world scenario, you should store tickets in a distributed cache or a persistent storage solution. + - It is also possible to use a password instead of a ticket for authentication. + +### Acknowledgments + +A big thank you to ChatGPT for helping with the documentation and code improvements! diff --git a/examples/term-and-vnc/go.mod b/examples/term-and-vnc/go.mod new file mode 100644 index 0000000..102d422 --- /dev/null +++ b/examples/term-and-vnc/go.mod @@ -0,0 +1,49 @@ +module term-and-vnc + +go 1.22.0 + +replace github.com/luthermonson/go-proxmox => ../../ + +require ( + github.com/gin-contrib/cors v1.7.2 + github.com/gin-gonic/gin v1.10.0 + github.com/gorilla/websocket v1.5.3 + github.com/joho/godotenv v1.5.1 + github.com/luthermonson/go-proxmox v0.1.1 + github.com/rs/zerolog v1.33.0 +) + +require ( + github.com/buger/goterm v1.0.4 // indirect + 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/diskfs/go-diskfs v1.2.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/sse v0.1.0 // 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/jinzhu/copier v0.3.4 // 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/magefile/mage v1.14.0 // indirect + github.com/mattn/go-colorable v0.1.13 // 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/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + golang.org/x/arch v0.8.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/text v0.15.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/djherbis/times.v1 v1.2.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/examples/term-and-vnc/go.sum b/examples/term-and-vnc/go.sum new file mode 100644 index 0000000..a49306e --- /dev/null +++ b/examples/term-and-vnc/go.sum @@ -0,0 +1,171 @@ +4d63.com/gochecknoinits v0.0.0-20200108094044-eb73b47b9fc4/go.mod h1:4o1i5aXtIF5tJFt3UD1knCVmWOXg7fLYdHVu6jeNcnM= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/buger/goterm v1.0.4 h1:Z9YvGmOih81P0FbVtEYTFF6YsSgxSUKEhf/f9bTMXbY= +github.com/buger/goterm v1.0.4/go.mod h1:HiFWV3xnkolgrBV3mY8m0X0Pumt4zg4QhbdOzQtB8tE= +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/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +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/diskfs/go-diskfs v1.2.0 h1:Ow4xorEDw1VNYKbC+SA/qQNwi5gWIwdKUxmUcLFST24= +github.com/diskfs/go-diskfs v1.2.0/go.mod h1:ZTeTbzixuyfnZW5y5qKMtjV2o+GLLHo1KfMhotJI4Rk= +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/cors v1.7.2 h1:oLDHxdg8W/XDoN/8zamqk/Drgt4oVZDvaV0YmvVICQw= +github.com/gin-contrib/cors v1.7.2/go.mod h1:SUJVARKgQ40dmrzgXEVxj2m7Ig1v1qIboQkPDTQ9t2E= +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-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/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +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/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gordonklaus/ineffassign v0.0.0-20190601041439-ed7b1b5ee0f8/go.mod h1:cuNKsD1zp2v6XfE/orVX2QE1LC+i254ceGcVeDT3pTU= +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/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE= +github.com/h2non/gock v1.2.0/go.mod h1:tNhoxHYW2W42cYkYb1WqzdbYIieALC99kpYr7rH/BQk= +github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw= +github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI= +github.com/jgautheron/goconst v0.0.0-20170703170152-9740945f5dcb/go.mod h1:82TxjOpWQiPmywlbIaB2ZkqJoSYJdLGPgAJDvM3PbKc= +github.com/jinzhu/copier v0.3.4 h1:mfU6jI9PtCeUjkjQ322dlff9ELjGDu975C2p/nrubVI= +github.com/jinzhu/copier v0.3.4/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +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/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +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/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +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/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo= +github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +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/mibk/dupl v1.0.0/go.mod h1:pCr4pNxxIbFGvtyCOi0c7LVjmV6duhKWV+ex5vh38ME= +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/pierrec/lz4 v2.3.0+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/xattr v0.4.1/go.mod h1:W2cGD0TBEus7MkUgv0tNZ9JutLtVO3cXu+IBRuHqnFs= +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/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= +github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= +github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +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/stripe/safesql v0.2.0/go.mod h1:q7b2n0JmzM1mVGfcYpanfVb2j23cXZeWFxcILPn3JV4= +github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9/go.mod h1:q+QjxYvZ+fpjMXqs+XEriussHjSYqeXVnAdSV1tkMYk= +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/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8= +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.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +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/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +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/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20181021155630-eda9bb28ed51/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190804053845-51ab0e2deafa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210331175145-43e1dd70ce54/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/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.12.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/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +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/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20200102200121-6de373a2766c/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +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/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/djherbis/times.v1 v1.2.0 h1:UCvDKl1L/fmBygl2Y7hubXCnY7t4Yj46ZrBFNUipFbM= +gopkg.in/djherbis/times.v1 v1.2.0/go.mod h1:AQlg6unIsrsCEdQYhTzERy542dz6SFdQFZFv6mUY0P8= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +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= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= +mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed/go.mod h1:Xkxe497xwlCKkIaQYRfC7CSLworTXY9RMqwhhCm+8Nc= +mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b/go.mod h1:2odslEg/xrtNQqCYg2/jCoyKnw3vv5biOc3JnIcYfL4= +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/examples/term-and-vnc/impl/common.go b/examples/term-and-vnc/impl/common.go new file mode 100644 index 0000000..2f08f9e --- /dev/null +++ b/examples/term-and-vnc/impl/common.go @@ -0,0 +1,51 @@ +package impl + +import ( + "context" + "crypto/tls" + "net/http" + "os" + "strconv" + + "github.com/luthermonson/go-proxmox" +) + +var client *proxmox.Client + +func Init() { + insecureHTTPClient := http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + }, + } + + credentials := proxmox.Credentials{ + Username: os.Getenv("PROXMOX_USERNAME"), + Password: os.Getenv("PROXMOX_PASSWORD"), + } + client = proxmox.NewClient(os.Getenv("PROXMOX_URL"), + proxmox.WithHTTPClient(&insecureHTTPClient), + proxmox.WithCredentials(&credentials), + ) +} + +func GetVm() (*proxmox.VirtualMachine, error) { + node, err := client.Node(context.Background(), os.Getenv("PROXMOX_NODE")) + if err != nil { + return nil, err + } + + vmId, err := strconv.Atoi(os.Getenv("PROXMOX_VM")) + if err != nil { + return nil, err + } + + vm, err := node.VirtualMachine(context.Background(), vmId) + if err != nil { + return nil, err + } + + return vm, nil +} diff --git a/examples/term-and-vnc/impl/term.go b/examples/term-and-vnc/impl/term.go new file mode 100644 index 0000000..af26452 --- /dev/null +++ b/examples/term-and-vnc/impl/term.go @@ -0,0 +1,88 @@ +package impl + +import ( + "context" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/gorilla/websocket" + "github.com/rs/zerolog/log" +) + +func Term(c *gin.Context) { + vm, err := GetVm() + if err != nil { + log.Error().Err(err).Msg("Error getting version") + return + } + + term, err := vm.TermProxy(context.Background()) + if err != nil { + log.Error().Err(err).Msg("Error getting version") + return + } + log.Debug().Any("VNC", term).Send() + + send, recv, errs, close, err := vm.TermWebSocket(term) + if err != nil { + log.Error().Err(err).Msg("Error getting version") + return + } + defer close() + + var upgrader = websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, + CheckOrigin: func(r *http.Request) bool { return true }, + } + + ws, err := upgrader.Upgrade(c.Writer, c.Request, nil) + if err != nil { + return + } + defer ws.Close() + + done := make(chan struct{}) + go reader(ws, send, done) + go writer(ws, recv, errs, done) + + <-done +} + +func reader(ws *websocket.Conn, send chan []byte, done chan struct{}) { + for { + _, msg, err := ws.ReadMessage() + if err != nil { + if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { + log.Error().Err(err).Send() + } + done <- struct{}{} + return + } + send <- msg + } +} + +func writer(ws *websocket.Conn, recv chan []byte, errs chan error, done chan struct{}) { + for { + select { + case msg := <-recv: + err := ws.WriteMessage(websocket.TextMessage, msg) + if err != nil { + done <- struct{}{} + log.Error().Err(err).Send() + return + } + + case err := <-errs: + if err != nil { + log.Error().Err(err).Send() + } + done <- struct{}{} + return + case <-done: + return + } + + } +} diff --git a/examples/term-and-vnc/impl/vnc.go b/examples/term-and-vnc/impl/vnc.go new file mode 100644 index 0000000..50d6d52 --- /dev/null +++ b/examples/term-and-vnc/impl/vnc.go @@ -0,0 +1,129 @@ +package impl + +import ( + "context" + "encoding/base64" + "net/http" + "strings" + + "github.com/gin-gonic/gin" + "github.com/gorilla/websocket" + "github.com/luthermonson/go-proxmox" + "github.com/rs/zerolog/log" +) + +var tickets = make(map[string]*proxmox.VNC) + +func VncTicket(c *gin.Context) { + vm, err := GetVm() + if err != nil { + log.Error().Err(err).Msg("Error getting version") + return + } + + vnc, err := vm.VNCProxy(context.Background(), nil) + if err != nil { + log.Error().Err(err).Msg("Error getting version") + return + } + + // log.Debug().Str("Ticket", vnc.Ticket).Send() + id := base64.StdEncoding.EncodeToString([]byte(vnc.Ticket)) + tickets[id] = vnc + + c.JSON(http.StatusOK, gin.H{"ticket": id}) +} + +func Vnc(c *gin.Context) { + log.Debug().Msg("New VNC connection") + // vnc, err := vm.VNCProxy(context.Background(), nil) + id := c.Query("ticket") + vm, err := GetVm() + if err != nil { + log.Error().Err(err).Msg("Error getting version") + return + } + + // vnc, err := vm.VNCProxy(context.Background(), nil) + // if err != nil { + // log.Error().Err(err).Msg("Error getting version") + // return + // } + vnc := tickets[string(id)] + + if vnc == nil { + log.Error().Msg("VNC ticket not found") + return + } + + // log.Debug().Str("Ticket", vnc.Ticket).Send() + send, recv, errs, close, err := vm.VNCWebSocket(vnc) + if err != nil { + log.Error().Err(err).Msg("Error getting version") + return + } + defer close() + + var upgrader = websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, + CheckOrigin: func(r *http.Request) bool { return true }, + } + + ws, err := upgrader.Upgrade(c.Writer, c.Request, nil) + if err != nil { + return + } + defer ws.Close() + + done := make(chan struct{}) + + go func() { + for { + select { + case <-done: + return + case msg := <-recv: + // log.Debug().Bytes("msg", msg).Msg("proxmox:") + err = ws.WriteMessage(websocket.BinaryMessage, msg) + if err != nil { + done <- struct{}{} + log.Error().Err(err).Send() + return + } + } + } + }() + + go func() { + for { + select { + case <-done: + return + case err := <-errs: + if err != nil { + log.Error().Err(err).Send() + } + done <- struct{}{} + return + default: + _, msg, err := ws.ReadMessage() + if err != nil { + done <- struct{}{} + if strings.Contains(err.Error(), "use of closed network connection") { + return + } + if !websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { + return + } + log.Error().Err(err).Msg("Error reading from websocket") + return + } + // log.Debug().Bytes("msg", msg).Msg("Client:") + send <- msg + } + } + }() + + <-done +} diff --git a/examples/term-and-vnc/main.go b/examples/term-and-vnc/main.go new file mode 100644 index 0000000..7ab5466 --- /dev/null +++ b/examples/term-and-vnc/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "net/http" + "os" + "path/filepath" + "strconv" + "term-and-vnc/impl" + + "github.com/gin-contrib/cors" + "github.com/gin-gonic/gin" + "github.com/joho/godotenv" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" +) + +func main() { + zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string { + return filepath.Base(file) + ":" + strconv.Itoa(line) + } + log.Logger = log. + With(). + Caller(). + Logger(). + Output(zerolog.ConsoleWriter{Out: os.Stderr}) + godotenv.Load() + + impl.Init() + + log.Info().Msg("starting server") + r := gin.Default() + r.Use(cors.Default()) + + r.GET("/", func(c *gin.Context) { + c.String(http.StatusOK, "hello world") + }) + r.GET("/term", impl.Term) + r.GET("/vnc", impl.Vnc) + r.GET("/vnc-ticket", impl.VncTicket) + + r.RunTLS(":8523", "server.crt", "server.key") +} diff --git a/examples/term-and-vnc/server.crt b/examples/term-and-vnc/server.crt new file mode 100644 index 0000000..ea26bd8 --- /dev/null +++ b/examples/term-and-vnc/server.crt @@ -0,0 +1,20 @@ +-----BEGIN CERTIFICATE----- +MIIDWzCCAkOgAwIBAgIUYGRkMwCIazjPiT4W5GC6A4L2Ww4wDQYJKoZIhvcNAQEL +BQAwPTELMAkGA1UEBhMCQlIxCzAJBgNVBAgMAlJKMSEwHwYDVQQKDBhJbnRlcm5l +dCBXaWRnaXRzIFB0eSBMdGQwHhcNMjQwOTEyMDQyNjQ0WhcNMjUwOTEyMDQyNjQ0 +WjA9MQswCQYDVQQGEwJCUjELMAkGA1UECAwCUkoxITAfBgNVBAoMGEludGVybmV0 +IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB +AL5GpQIw8TsB86PnLvJ5MwQjBHE3jVPaVj2f3tsFGaSUpKW1O/te3Icctu3qm+6M +uOXQi4TSl6krDF2Qm/bQo4B14GBTvSlkhowQLAzZRqxswQetEdMOkA+JuErNhOyc +GqMifxkemLSr7DgnuApgw1Y3/mKTelB4UfFJT+j5uLxJYoL9lvdp73VG0QaHPpzO +ybS4N2LCu8GXb7uj4k/7yCXHTEWJxd5GbactPXZyUPbXEv1sLTgd+108oa+0QP+L +b+ZPLsUZavlZy7vAVKfWNXP1GvIw9bghNXoKuopACUb6r4CMDHlsTtuFI/vKmuhY +Lr5jp3gnyOHy1a5J6aqARRUCAwEAAaNTMFEwHQYDVR0OBBYEFASiWaeWhF3HhScq +BYu3M5Tgn2fbMB8GA1UdIwQYMBaAFASiWaeWhF3HhScqBYu3M5Tgn2fbMA8GA1Ud +EwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBALwyCaXwKiXNxoZAQNaDdjlT +hibmruWxS0tG8QrUvk2iqm/wGpkIMl3WB7sMzCFpRXCiCiJw6CupUWWGeptAmISK ++MbmluA2wyLt84gtWoEpClv2GeVUiZqOQA3J85FyrC2uWMoCGE6rEd8gF4dF945Y +F5bh4vc4cBKKHamWRgk0VoE11bzbZD8/ZgShxKcIq7vPLn/OO/CHw3Llm06klnvG +rx+cIHcychCP4BRxm+pJNjqKKvxVJnZgz1QS9EiCcIFEpE3TaxSy0XZwlm8a4OgO +EpceWw7qpH42Rm44hOWScUhELgP026kBimOP4CRhmRhweLiChmk1NfR0s4OrA5E= +-----END CERTIFICATE----- diff --git a/examples/term-and-vnc/server.key b/examples/term-and-vnc/server.key new file mode 100644 index 0000000..2f1f216 --- /dev/null +++ b/examples/term-and-vnc/server.key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC+RqUCMPE7AfOj +5y7yeTMEIwRxN41T2lY9n97bBRmklKSltTv7XtyHHLbt6pvujLjl0IuE0pepKwxd +kJv20KOAdeBgU70pZIaMECwM2UasbMEHrRHTDpAPibhKzYTsnBqjIn8ZHpi0q+w4 +J7gKYMNWN/5ik3pQeFHxSU/o+bi8SWKC/Zb3ae91RtEGhz6czsm0uDdiwrvBl2+7 +o+JP+8glx0xFicXeRm2nLT12clD21xL9bC04HftdPKGvtED/i2/mTy7FGWr5Wcu7 +wFSn1jVz9RryMPW4ITV6CrqKQAlG+q+AjAx5bE7bhSP7yproWC6+Y6d4J8jh8tWu +SemqgEUVAgMBAAECggEABLzODXap/vHevtk5+EQPwt3bVbVQIcMZLBofqpzfJVOl +INIZHvpe68dJf7V8ce1TijdNzf4PVCE0At1foL7Tn1ZwAn37Qv1Tg0fAzQYY5iCk +EryaqcJeUGC5s7UXxuthF2g7uWSjYTY5oLFOegWsP2iP/yTUYg4I3kTYxkB3esqe ++KClmMXKZUUxXk5I3ITr0V9mY/RRZhQUnqYQlN7uxGgEIuqiAp/yt6/+2tfv2pmW +aBHtwsLEbfP2oORC27yjOP0nXjnb0lcuyiDYp6JLIQzRNIJbAMFPAJ/ppfC8WiwT +5ntSlc87gDJm7PdC4nwtIoxqFVk8p2wtfUUtHR5fuQKBgQDiO7VkYsyyHizbh+G2 +nl6A68rRssT6JOxPGTrbgcs1fVIvjjeUIB5jYNtR0LIg2YXcyrzk6W9BWCVmu1rh +LIpPqxzkKwvirtEoevjluHkPX+tn+qctigEgA1Tww3+KjJtP6CXBHu7+oPt9EL6n +IwytVxjljjc0+IYrUX3onxG4OQKBgQDXT8ZMouiY6dDeAEoeUnxPzHZbeTR+XO+7 +/quCrXoDchdJhJCvmqDKDCj3ZmaxOf9a4oCOhg8klwMi0REgwY0nWYjCKxkseyr+ +v2eSAJcdya6WFnmAx4L/8QREaAy8PfjqPBNyl/p/5OSMdte7fPXnqIyXCt/8kcpX +OH2oR2dbvQKBgFBBYEMBbZ5OaBlk5eST3/CaNTQY2BnpGU+iIqgWLlGGaK5oJD1O ++5jDlmo6v8Y5hGxkG4iVaxtiOrpsAnxZAaPrju8+ehPrclWK8lsECyYyH9+eIspb +57REmlBi+g7bfIlnUGVnJN99mlmupAPEwk2H1VQyFFGdTHN3kFQCghW5AoGAXDg4 +8P4drdEHuu5Y0J/3yp4CW6+QQkdADz9G6pOu3Oby/nQfHHeYQrHuofhkJ6h1rNGy +yli1B7D+kN0G/wanlKEOqEuZXqnqgm4syCYkk4eclYBOzk+l1kW+1CXUvpO0l27f +zYc2ray2D0ufnxgoou5YOSFZSm6PQchaWccrh+kCgYBiB5t2e4eW6aPFiarKbkz3 +5kx4WPe4OfjNF0i1sVDcdV+4QNDnjRkmkyhxPpjfK6BkfUUCgi355xYTg0wLzZgR +4G1SPBLaXS4YIda3BDqxy1C91uBTxflwQBX3SlYjBAgMRyjYnLzA2wqKdyUAL8MP +Kk7tm1pZjgRSnl3Q8jSpzQ== +-----END PRIVATE KEY----- diff --git a/examples/term-and-vnc/website/.gitignore b/examples/term-and-vnc/website/.gitignore new file mode 100644 index 0000000..a547bf3 --- /dev/null +++ b/examples/term-and-vnc/website/.gitignore @@ -0,0 +1,24 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +dist +dist-ssr +*.local + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/examples/term-and-vnc/website/index.html b/examples/term-and-vnc/website/index.html new file mode 100644 index 0000000..987320b --- /dev/null +++ b/examples/term-and-vnc/website/index.html @@ -0,0 +1,15 @@ + + + +
+ + +Status: Desconectado
+ + + + + + + \ No newline at end of file diff --git a/examples/term-and-vnc/website/vnc.js b/examples/term-and-vnc/website/vnc.js new file mode 100644 index 0000000..23a4e40 --- /dev/null +++ b/examples/term-and-vnc/website/vnc.js @@ -0,0 +1,56 @@ +import RFB from "@novnc/novnc"; + +const connectButton = document.getElementById("connectButton"); +const disconnectButton = document.getElementById("disconnectButton"); +const statusElement = document.getElementById("status"); +let rfb; + +connectButton.addEventListener("click", () => { + const url = "https://term-and-vnc.toquinha.online/vnc-ticket"; + const wss = "https://term-and-vnc.toquinha.online/vnc"; + + fetch(url) + .then((response) => response.json()) + .then((data) => { + const ticket = data.ticket; + connect(`${wss}?ticket=${ticket}`, atob(ticket)); + }); + + statusElement.textContent = "Conectando ao VNC Parte 1"; +}); + +disconnectButton.addEventListener("click", () => { + if (rfb) { + rfb.disconnect(); + statusElement.textContent = "Desconectado manualmente"; + } +}); + +function connect(url, password) { + statusElement.textContent = "Conectando ao VNC Parte 2"; + // Inicia a conexão VNC + rfb = new RFB(document.getElementById("vncContainer"), url, { + shared: false, + repeaterID: false, + credentials: { + password: password, // Substitua pela senha do VNC + }, + }); + + rfb.addEventListener("connect", () => { + statusElement.textContent = "Conectado ao VNC"; + }); + + rfb.addEventListener("disconnect", () => { + statusElement.textContent = "Desconectado do VNC"; + }); + + rfb.addEventListener("credentialsrequired", () => { + alert("Credenciais necessárias"); + // Você pode solicitar credenciais aqui se ainda não forneceu + }); + + rfb.addEventListener("securityfailure", (e) => { + statusElement.textContent = `Falha de segurança: ${e.detail.reason}`; + }); +}