mirror of
https://github.com/yudai/gotty.git
synced 2026-02-14 12:24:21 +01:00
generate falgs based on struct options instead of defining them externally
This commit is contained in:
parent
c4ed7bc415
commit
d71e2fcfa8
6 changed files with 218 additions and 231 deletions
111
app/app.go
111
app/app.go
|
|
@ -13,7 +13,6 @@ import (
|
|||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
|
@ -22,11 +21,12 @@ import (
|
|||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/yudai/gotty/utils"
|
||||
|
||||
"github.com/braintree/manners"
|
||||
"github.com/elazarl/go-bindata-assetfs"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/kr/pty"
|
||||
"github.com/yudai/hcl"
|
||||
"github.com/yudai/umutex"
|
||||
)
|
||||
|
||||
|
|
@ -53,60 +53,35 @@ type App struct {
|
|||
}
|
||||
|
||||
type Options struct {
|
||||
Address string `hcl:"address"`
|
||||
Port string `hcl:"port"`
|
||||
PermitWrite bool `hcl:"permit_write"`
|
||||
EnableBasicAuth bool `hcl:"enable_basic_auth"`
|
||||
Credential string `hcl:"credential"`
|
||||
EnableRandomUrl bool `hcl:"enable_random_url"`
|
||||
RandomUrlLength int `hcl:"random_url_length"`
|
||||
IndexFile string `hcl:"index_file"`
|
||||
EnableTLS bool `hcl:"enable_tls"`
|
||||
TLSCrtFile string `hcl:"tls_crt_file"`
|
||||
TLSKeyFile string `hcl:"tls_key_file"`
|
||||
EnableTLSClientAuth bool `hcl:"enable_tls_client_auth"`
|
||||
TLSCACrtFile string `hcl:"tls_ca_crt_file"`
|
||||
TitleFormat string `hcl:"title_format"`
|
||||
EnableReconnect bool `hcl:"enable_reconnect"`
|
||||
ReconnectTime int `hcl:"reconnect_time"`
|
||||
MaxConnection int `hcl:"max_connection"`
|
||||
Once bool `hcl:"once"`
|
||||
Timeout int `hcl:"timeout"`
|
||||
PermitArguments bool `hcl:"permit_arguments"`
|
||||
CloseSignal int `hcl:"close_signal"`
|
||||
Address string `hcl:"address" flagName:"address" flagSName:"a" flagDescribe:"IP address to listen" default:""`
|
||||
Port string `hcl:"port" flagName:"port" flagSName:"p" flagDescribe:"Port number to liten" default:"8080"`
|
||||
PermitWrite bool `hcl:"permit_write" flagName:"permit-write" flagSName:"w" flagDescribe:"Permit clients to write to the TTY (BE CAREFUL)" default:"false"`
|
||||
EnableBasicAuth bool `hcl:"enable_basic_auth" default:"false"`
|
||||
Credential string `hcl:"credential" flagName:"credential" flagSName:"c" flagDescribe:"Credential for Basic Authentication (ex: user:pass, default disabled)" default:""`
|
||||
EnableRandomUrl bool `hcl:"enable_random_url flagName:"random-url" flagSName:"r" flagDescribe:"Add a random string to the URL"" default:"false"`
|
||||
RandomUrlLength int `hcl:"random_url_length" flagName:"random-url-length" flagDescribe:"Random URL length" default:"8"`
|
||||
IndexFile string `hcl:"index_file" flagName:"index" flagDescribe:"Custom index.html file" default:""`
|
||||
EnableTLS bool `hcl:"enable_tls" flagName:"tls" flagSName:"t" flagDescribe:"Enable TLS/SSL" default:"false"`
|
||||
TLSCrtFile string `hcl:"tls_crt_file" flagName:"tls-crt" flagDescribe:"TLS/SSL certificate file path" default:"~/.gotty.crt"`
|
||||
TLSKeyFile string `hcl:"tls_key_file" flagName:"tls-key" flagDescribe:"TLS/SSL key file path" default:"~/.gotty.key"`
|
||||
EnableTLSClientAuth bool `hcl:"enable_tls_client_auth" default:"false"`
|
||||
TLSCACrtFile string `hcl:"tls_ca_crt_file" flagName:"tls-ca-crt" flagDescribe:"TLS/SSL CA certificate file for client certifications" default:"~/.gotty.ca.crt"`
|
||||
TitleFormat string `hcl:"title_format" flagName:"title-format" flagDescribe:"Title format of browser window" default:"GoTTY - {{ .Command }} ({{ .Hostname }})"`
|
||||
EnableReconnect bool `hcl:"enable_reconnect" flagName:"reconnect" flagDescribe:"Enable reconnection" default:"false"`
|
||||
ReconnectTime int `hcl:"reconnect_time" flagName:"reconnect-time" flagDescribe:"Time to reconnect" default:"10"`
|
||||
MaxConnection int `hcl:"max_connection" flagName:"max-connection" flagDescribe:"Maximum connection to gotty" default:"0"`
|
||||
Once bool `hcl:"once" flagName:"once" flagDescribe:"Accept only one client and exit on disconnection" default:"false"`
|
||||
Timeout int `hcl:"timeout" flagName:"timeout" flagDescribe:"Timeout seconds for waiting a client(0 to disable)" default:"0"`
|
||||
PermitArguments bool `hcl:"permit_arguments" flagName:"permit-arguments" flagDescribe:"Permit clients to send command line arguments in URL (e.g. http://example.com:8080/?arg=AAA&arg=BBB)" default:"true"`
|
||||
CloseSignal int `hcl:"close_signal" flagName:"close-signal" flagDescribe:"Signal sent to the command process when gotty close it (default: SIGHUP)" default:"1"`
|
||||
Preferences HtermPrefernces `hcl:"preferences"`
|
||||
RawPreferences map[string]interface{} `hcl:"preferences"`
|
||||
Width int `hcl:"width"`
|
||||
Height int `hcl:"height"`
|
||||
Width int `hcl:"width" flagName:"width" flagDescribe:"Static width of the screen, 0(default) means dynamically resize" default:"0"`
|
||||
Height int `hcl:"height" flagName:"height" flagDescribe:"Static height of the screen, 0(default) means dynamically resize" default:"0"`
|
||||
}
|
||||
|
||||
var Version = "1.0.0"
|
||||
|
||||
var DefaultOptions = Options{
|
||||
Address: "",
|
||||
Port: "8080",
|
||||
PermitWrite: false,
|
||||
EnableBasicAuth: false,
|
||||
Credential: "",
|
||||
EnableRandomUrl: false,
|
||||
RandomUrlLength: 8,
|
||||
IndexFile: "",
|
||||
EnableTLS: false,
|
||||
TLSCrtFile: "~/.gotty.crt",
|
||||
TLSKeyFile: "~/.gotty.key",
|
||||
EnableTLSClientAuth: false,
|
||||
TLSCACrtFile: "~/.gotty.ca.crt",
|
||||
TitleFormat: "GoTTY - {{ .Command }} ({{ .Hostname }})",
|
||||
EnableReconnect: false,
|
||||
ReconnectTime: 10,
|
||||
MaxConnection: 0,
|
||||
Once: false,
|
||||
CloseSignal: 1, // syscall.SIGHUP
|
||||
Preferences: HtermPrefernces{},
|
||||
Width: 0,
|
||||
Height: 0,
|
||||
}
|
||||
|
||||
func New(command []string, options *Options) (*App, error) {
|
||||
titleTemplate, err := template.New("title").Parse(options.TitleFormat)
|
||||
if err != nil {
|
||||
|
|
@ -132,26 +107,6 @@ func New(command []string, options *Options) (*App, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func ApplyConfigFile(options *Options, filePath string) error {
|
||||
filePath = ExpandHomeDir(filePath)
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
fileString := []byte{}
|
||||
log.Printf("Loading config file at: %s", filePath)
|
||||
fileString, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := hcl.Decode(options, string(fileString)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CheckConfig(options *Options) error {
|
||||
if options.EnableTLSClientAuth && !options.EnableTLS {
|
||||
return errors.New("TLS client authentication is enabled, but TLS is not enabled")
|
||||
|
|
@ -253,8 +208,8 @@ func (app *App) Run() error {
|
|||
}
|
||||
|
||||
if app.options.EnableTLS {
|
||||
crtFile := ExpandHomeDir(app.options.TLSCrtFile)
|
||||
keyFile := ExpandHomeDir(app.options.TLSKeyFile)
|
||||
crtFile := utils.ExpandHomeDir(app.options.TLSCrtFile)
|
||||
keyFile := utils.ExpandHomeDir(app.options.TLSKeyFile)
|
||||
log.Printf("TLS crt file: " + crtFile)
|
||||
log.Printf("TLS key file: " + keyFile)
|
||||
|
||||
|
|
@ -278,7 +233,7 @@ func (app *App) makeServer(addr string, handler *http.Handler) (*http.Server, er
|
|||
}
|
||||
|
||||
if app.options.EnableTLSClientAuth {
|
||||
caFile := ExpandHomeDir(app.options.TLSCACrtFile)
|
||||
caFile := utils.ExpandHomeDir(app.options.TLSCACrtFile)
|
||||
log.Printf("CA file: " + caFile)
|
||||
caCert, err := ioutil.ReadFile(caFile)
|
||||
if err != nil {
|
||||
|
|
@ -410,7 +365,7 @@ func (app *App) handleWS(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (app *App) handleCustomIndex(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, ExpandHomeDir(app.options.IndexFile))
|
||||
http.ServeFile(w, r, utils.ExpandHomeDir(app.options.IndexFile))
|
||||
}
|
||||
|
||||
func (app *App) handleAuthToken(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -501,11 +456,3 @@ func listAddresses() (addresses []string) {
|
|||
|
||||
return
|
||||
}
|
||||
|
||||
func ExpandHomeDir(path string) string {
|
||||
if path[0:2] == "~/" {
|
||||
return os.Getenv("HOME") + path[1:]
|
||||
} else {
|
||||
return path
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue