Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vnc and Term Proxy Example #162

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/term-and-vnc/.air.toml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions examples/term-and-vnc/.env.sample
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions examples/term-and-vnc/.gitignore
Original file line number Diff line number Diff line change
@@ -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
78 changes: 78 additions & 0 deletions examples/term-and-vnc/README.md
Original file line number Diff line number Diff line change
@@ -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!
49 changes: 49 additions & 0 deletions examples/term-and-vnc/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
Loading
Loading