GoTTY配置文件详解:打造个性化Web终端体验
【免费下载链接】gotty Share your terminal as a web application 项目地址: https://gitcode.com/gh_mirrors/go/gotty
GoTTY(Go Terminal over Web)是一款能将本地终端命令行工具转换为Web应用的实用工具,通过简单配置即可实现在浏览器中访问和操作终端。本文将详细介绍GoTTY配置文件的各项参数,帮助你打造专属的Web终端体验。
配置文件基础
GoTTY默认会读取用户主目录下的.gotty文件作为配置文件,也可通过--config参数指定自定义配置文件路径。配置文件采用HCL(HashiCorp Configuration Language)格式,主要包含服务器设置和终端显示偏好两部分。
配置文件加载流程
配置文件的加载逻辑在utils/flags.go中实现,系统会按以下优先级读取配置:
- 命令行参数
- 环境变量(以
GOTTY_为前缀) - 配置文件设置
- 默认值
基本结构示例
// 服务器基本设置
port = "9000"
enable_tls = true
credential = "user:pass123"
// 终端显示偏好设置
preferences {
font_size = 14
background_color = "rgb(24, 24, 24)"
cursor_color = "rgb(255, 255, 255)"
}
服务器核心配置
服务器相关配置控制GoTTY的网络行为和访问安全,主要参数定义在server/options.go中。
网络设置
| 参数 | 说明 | 示例值 |
|---|---|---|
address | 监听地址 | "0.0.0.0" |
port | 监听端口 | "8080" |
max_connection | 最大连接数(0表示无限制) | 10 |
timeout | 客户端连接超时时间(秒) | 300 |
安全设置
| 参数 | 说明 | 示例值 |
|---|---|---|
enable_tls | 是否启用TLS加密 | true |
tls_crt_file | TLS证书文件路径 | "~/.gotty.crt" |
tls_key_file | TLS密钥文件路径 | "~/.gotty.key" |
credential | 基础认证凭据(格式:user:pass) | "admin:secret" |
enable_random_url | 是否生成随机访问URL | true |
random_url_length | 随机URL长度 | 8 |
安全配置示例
// 启用TLS加密
enable_tls = true
tls_crt_file = "~/.gotty/server.crt"
tls_key_file = "~/.gotty/server.key"
// 启用基础认证
credential = "admin:SecurePass123!"
// 限制单一客户端连接
once = true
终端显示个性化
GoTTY使用hterm作为Web终端模拟器,可通过preferences块自定义终端的视觉和行为特性,相关配置在server/options.go的HtermPrefernces结构体中定义。
外观设置
以下是常用的外观配置参数:
| 参数 | 说明 | 示例值 |
|---|---|---|
font_size | 字体大小 | 14 |
font_family | 字体家族 | "Consolas, 'Courier New', monospace" |
background_color | 背景颜色 | "rgb(16, 16, 32)" |
foreground_color | 前景文字颜色 | "rgb(255, 255, 255)" |
cursor_color | 光标颜色 | "rgb(255, 255, 0)" |
color_palette_overrides | 颜色方案覆盖 | ["#000000", "#ff0000", ...](16个颜色值) |
行为设置
| 参数 | 说明 | 示例值 |
|---|---|---|
cursor_blink | 光标是否闪烁 | true |
scroll_on_output | 输出时自动滚动到底部 | true |
scrollbar_visible | 是否显示滚动条 | true |
copy_on_select | 选中文本时自动复制 | true |
ctrl_ccopy | 是否启用Ctrl+C复制 | true |
ctrl_vpaste | 是否启用Ctrl+V粘贴 | true |
个性化配置示例
preferences {
// 字体设置
font_size = 14
font_family = "'Fira Code', Consolas, monospace"
// 颜色方案 - 深色主题
background_color = "rgb(15, 15, 25)"
foreground_color = "rgb(240, 240, 240)"
cursor_color = "rgb(255, 165, 0)"
// 行为设置
cursor_blink = true
copy_on_select = true
scroll_on_output = true
// 自定义快捷键
keybindings = {
"Ctrl+Shift+C" = "Copy"
"Ctrl+Shift+V" = "Paste"
}
}
高级功能配置
终端尺寸控制
可通过以下参数固定终端窗口大小:
width = 120 // 宽度(字符数)
height = 40 // 高度(字符数)
当设置为0时,终端会根据浏览器窗口动态调整大小。
标题格式自定义
通过title_format参数自定义浏览器标签页标题,支持模板变量:
title_format = "{{ .command }} - {{ .hostname }}:{{ .port }}"
可用变量:
{{ .command }}: 当前运行的命令{{ .hostname }}: 服务器主机名{{ .port }}: 服务器端口{{ .user }}: 认证用户名
重连功能配置
启用自动重连功能,当网络中断后客户端会自动尝试重新连接:
enable_reconnect = true
reconnect_time = 5 // 重连间隔(秒)
实用配置案例
安全共享终端配置
适合需要临时共享终端给他人查看的场景:
// 安全共享配置
port = "8443"
enable_tls = true
enable_random_url = true
random_url_length = 12
permit_write = false // 禁止写入操作
once = false // 允许多客户端连接
title_format = "Shared Terminal - {{ .random }}"
// 终端外观
preferences {
font_size = 16
background_color = "rgb(24, 24, 24)"
foreground_color = "rgb(200, 200, 200)"
cursor_blink = false
}
个人开发环境配置
适合作为日常开发使用的Web终端:
// 开发环境配置
port = "8080"
enable_tls = false
credential = "dev:dev123"
permit_write = true // 允许写入操作
permit_arguments = true // 允许URL传递命令参数
// 终端外观
preferences {
font_size = 14
font_family = "'Fira Code', 'Courier New', monospace"
background_color = "rgb(30, 30, 30)"
foreground_color = "rgb(255, 255, 255)"
cursor_color = "rgb(0, 255, 0)"
copy_on_select = true
ctrl_ccopy = true
ctrl_vpaste = true
}
配置参数速查表
完整参数列表可参考server/options.go中的Options和HtermPrefernces结构体定义,以下是常用参数速查:
服务器参数
| 分类 | 参数 | 默认值 |
|---|---|---|
| 网络 | address | "0.0.0.0" |
| 网络 | port | "8080" |
| 安全 | enable_tls | false |
| 安全 | credential | "" |
| 连接 | max_connection | 0 |
| 连接 | timeout | 0 |
终端参数
| 分类 | 参数 | 默认值 |
|---|---|---|
| 外观 | font_size | 12 |
| 外观 | background_color | "rgb(0, 0, 0)" |
| 外观 | foreground_color | "rgb(255, 255, 255)" |
| 行为 | cursor_blink | true |
| 行为 | scroll_on_output | true |
| 行为 | copy_on_select | false |
通过灵活配置这些参数,你可以打造既安全又符合个人使用习惯的Web终端环境。更多高级配置选项,请参考项目中的.gotty示例文件和官方文档。
【免费下载链接】gotty Share your terminal as a web application 项目地址: https://gitcode.com/gh_mirrors/go/gotty
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考




