mirror of
https://github.com/sorenisanerd/gotty.git
synced 2024-11-09 23:34:26 +00:00
commit
e81f4e9b7e
17
Godeps/Godeps.json
generated
17
Godeps/Godeps.json
generated
@ -2,15 +2,7 @@
|
|||||||
"ImportPath": "github.com/yudai/gotty",
|
"ImportPath": "github.com/yudai/gotty",
|
||||||
"GoVersion": "go1.7",
|
"GoVersion": "go1.7",
|
||||||
"GodepVersion": "v79",
|
"GodepVersion": "v79",
|
||||||
"Packages": [
|
|
||||||
"./..."
|
|
||||||
],
|
|
||||||
"Deps": [
|
"Deps": [
|
||||||
{
|
|
||||||
"ImportPath": "github.com/braintree/manners",
|
|
||||||
"Comment": "0.4.0-6-g9e2a271",
|
|
||||||
"Rev": "9e2a2714de21eb092ead2ef56d8c7a60d7928819"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/codegangsta/cli",
|
"ImportPath": "github.com/codegangsta/cli",
|
||||||
"Comment": "v1.19.1",
|
"Comment": "v1.19.1",
|
||||||
@ -37,6 +29,11 @@
|
|||||||
"Comment": "release.r56-28-g5cf931e",
|
"Comment": "release.r56-28-g5cf931e",
|
||||||
"Rev": "5cf931ef8f76dccd0910001d74a58a7fca84a83d"
|
"Rev": "5cf931ef8f76dccd0910001d74a58a7fca84a83d"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "github.com/pkg/errors",
|
||||||
|
"Comment": "v0.8.0-2-g248dadf",
|
||||||
|
"Rev": "248dadf4e9068a0b3e79f02ed0a610d935de5302"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/yudai/hcl",
|
"ImportPath": "github.com/yudai/hcl",
|
||||||
"Rev": "5fa2393b3552119bf33a69adb1402a1160cba23d"
|
"Rev": "5fa2393b3552119bf33a69adb1402a1160cba23d"
|
||||||
@ -48,10 +45,6 @@
|
|||||||
{
|
{
|
||||||
"ImportPath": "github.com/yudai/hcl/json",
|
"ImportPath": "github.com/yudai/hcl/json",
|
||||||
"Rev": "5fa2393b3552119bf33a69adb1402a1160cba23d"
|
"Rev": "5fa2393b3552119bf33a69adb1402a1160cba23d"
|
||||||
},
|
|
||||||
{
|
|
||||||
"ImportPath": "github.com/yudai/umutex",
|
|
||||||
"Rev": "18216d265c6bc72c3bb0ad9c8103d47d530b7003"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
10
Makefile
10
Makefile
@ -1,14 +1,14 @@
|
|||||||
OUTPUT_DIR = ./builds
|
OUTPUT_DIR = ./builds
|
||||||
GIT_COMMIT = `git rev-parse HEAD`
|
GIT_COMMIT = `git rev-parse HEAD`
|
||||||
|
|
||||||
gotty: app/resource.go main.go app/*.go
|
gotty: server/asset.go main.go server/*.go webtty/*.go backend/*.go
|
||||||
godep go build
|
godep go build
|
||||||
|
|
||||||
resource: app/resource.go
|
asset: server/asset.go
|
||||||
|
|
||||||
app/resource.go: bindata/static/js/hterm.js bindata/static/js/gotty.js bindata/static/index.html bindata/static/favicon.png
|
server/asset.go: bindata/static/js/hterm.js bindata/static/js/gotty.js bindata/static/index.html bindata/static/favicon.png
|
||||||
go-bindata -prefix bindata -pkg app -ignore=\\.gitkeep -o app/resource.go bindata/...
|
go-bindata -prefix bindata -pkg server -ignore=\\.gitkeep -o server/asset.go bindata/...
|
||||||
gofmt -w app/resource.go
|
gofmt -w server/asset.go
|
||||||
|
|
||||||
bindata:
|
bindata:
|
||||||
mkdir bindata
|
mkdir bindata
|
||||||
|
511
app/app.go
511
app/app.go
@ -1,511 +0,0 @@
|
|||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/rand"
|
|
||||||
"crypto/tls"
|
|
||||||
"crypto/x509"
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"math/big"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
"text/template"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"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"
|
|
||||||
)
|
|
||||||
|
|
||||||
type InitMessage struct {
|
|
||||||
Arguments string `json:"Arguments,omitempty"`
|
|
||||||
AuthToken string `json:"AuthToken,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type App struct {
|
|
||||||
command []string
|
|
||||||
options *Options
|
|
||||||
|
|
||||||
upgrader *websocket.Upgrader
|
|
||||||
server *manners.GracefulServer
|
|
||||||
|
|
||||||
titleTemplate *template.Template
|
|
||||||
|
|
||||||
onceMutex *umutex.UnblockingMutex
|
|
||||||
timer *time.Timer
|
|
||||||
|
|
||||||
// clientContext writes concurrently
|
|
||||||
// Use atomic operations.
|
|
||||||
connections *int64
|
|
||||||
}
|
|
||||||
|
|
||||||
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"`
|
|
||||||
Preferences HtermPrefernces `hcl:"preferences"`
|
|
||||||
RawPreferences map[string]interface{} `hcl:"preferences"`
|
|
||||||
Width int `hcl:"width"`
|
|
||||||
Height int `hcl:"height"`
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
return nil, errors.New("Title format string syntax error")
|
|
||||||
}
|
|
||||||
|
|
||||||
connections := int64(0)
|
|
||||||
|
|
||||||
return &App{
|
|
||||||
command: command,
|
|
||||||
options: options,
|
|
||||||
|
|
||||||
upgrader: &websocket.Upgrader{
|
|
||||||
ReadBufferSize: 1024,
|
|
||||||
WriteBufferSize: 1024,
|
|
||||||
Subprotocols: []string{"gotty"},
|
|
||||||
},
|
|
||||||
|
|
||||||
titleTemplate: titleTemplate,
|
|
||||||
|
|
||||||
onceMutex: umutex.New(),
|
|
||||||
connections: &connections,
|
|
||||||
}, 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")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) Run() error {
|
|
||||||
if app.options.PermitWrite {
|
|
||||||
log.Printf("Permitting clients to write input to the PTY.")
|
|
||||||
}
|
|
||||||
|
|
||||||
if app.options.Once {
|
|
||||||
log.Printf("Once option is provided, accepting only one client")
|
|
||||||
}
|
|
||||||
|
|
||||||
path := ""
|
|
||||||
if app.options.EnableRandomUrl {
|
|
||||||
path += "/" + generateRandomString(app.options.RandomUrlLength)
|
|
||||||
}
|
|
||||||
|
|
||||||
endpoint := net.JoinHostPort(app.options.Address, app.options.Port)
|
|
||||||
|
|
||||||
wsHandler := http.HandlerFunc(app.handleWS)
|
|
||||||
customIndexHandler := http.HandlerFunc(app.handleCustomIndex)
|
|
||||||
authTokenHandler := http.HandlerFunc(app.handleAuthToken)
|
|
||||||
staticHandler := http.FileServer(
|
|
||||||
&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "static"},
|
|
||||||
)
|
|
||||||
|
|
||||||
var siteMux = http.NewServeMux()
|
|
||||||
|
|
||||||
if app.options.IndexFile != "" {
|
|
||||||
log.Printf("Using index file at " + app.options.IndexFile)
|
|
||||||
siteMux.Handle(path+"/", customIndexHandler)
|
|
||||||
} else {
|
|
||||||
siteMux.Handle(path+"/", http.StripPrefix(path+"/", staticHandler))
|
|
||||||
}
|
|
||||||
siteMux.Handle(path+"/auth_token.js", authTokenHandler)
|
|
||||||
siteMux.Handle(path+"/js/", http.StripPrefix(path+"/", staticHandler))
|
|
||||||
siteMux.Handle(path+"/favicon.png", http.StripPrefix(path+"/", staticHandler))
|
|
||||||
|
|
||||||
siteHandler := http.Handler(siteMux)
|
|
||||||
|
|
||||||
if app.options.EnableBasicAuth {
|
|
||||||
log.Printf("Using Basic Authentication")
|
|
||||||
siteHandler = wrapBasicAuth(siteHandler, app.options.Credential)
|
|
||||||
}
|
|
||||||
|
|
||||||
siteHandler = wrapHeaders(siteHandler)
|
|
||||||
|
|
||||||
wsMux := http.NewServeMux()
|
|
||||||
wsMux.Handle("/", siteHandler)
|
|
||||||
wsMux.Handle(path+"/ws", wsHandler)
|
|
||||||
siteHandler = (http.Handler(wsMux))
|
|
||||||
|
|
||||||
siteHandler = wrapLogger(siteHandler)
|
|
||||||
|
|
||||||
scheme := "http"
|
|
||||||
if app.options.EnableTLS {
|
|
||||||
scheme = "https"
|
|
||||||
}
|
|
||||||
log.Printf(
|
|
||||||
"Server is starting with command: %s",
|
|
||||||
strings.Join(app.command, " "),
|
|
||||||
)
|
|
||||||
if app.options.Address != "" {
|
|
||||||
log.Printf(
|
|
||||||
"URL: %s",
|
|
||||||
(&url.URL{Scheme: scheme, Host: endpoint, Path: path + "/"}).String(),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
for _, address := range listAddresses() {
|
|
||||||
log.Printf(
|
|
||||||
"URL: %s",
|
|
||||||
(&url.URL{
|
|
||||||
Scheme: scheme,
|
|
||||||
Host: net.JoinHostPort(address, app.options.Port),
|
|
||||||
Path: path + "/",
|
|
||||||
}).String(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
server, err := app.makeServer(endpoint, &siteHandler)
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("Failed to build server: " + err.Error())
|
|
||||||
}
|
|
||||||
app.server = manners.NewWithServer(
|
|
||||||
server,
|
|
||||||
)
|
|
||||||
|
|
||||||
if app.options.Timeout > 0 {
|
|
||||||
app.timer = time.NewTimer(time.Duration(app.options.Timeout) * time.Second)
|
|
||||||
go func() {
|
|
||||||
<-app.timer.C
|
|
||||||
app.Exit()
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
if app.options.EnableTLS {
|
|
||||||
crtFile := ExpandHomeDir(app.options.TLSCrtFile)
|
|
||||||
keyFile := ExpandHomeDir(app.options.TLSKeyFile)
|
|
||||||
log.Printf("TLS crt file: " + crtFile)
|
|
||||||
log.Printf("TLS key file: " + keyFile)
|
|
||||||
|
|
||||||
err = app.server.ListenAndServeTLS(crtFile, keyFile)
|
|
||||||
} else {
|
|
||||||
err = app.server.ListenAndServe()
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Exiting...")
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) makeServer(addr string, handler *http.Handler) (*http.Server, error) {
|
|
||||||
server := &http.Server{
|
|
||||||
Addr: addr,
|
|
||||||
Handler: *handler,
|
|
||||||
}
|
|
||||||
|
|
||||||
if app.options.EnableTLSClientAuth {
|
|
||||||
caFile := ExpandHomeDir(app.options.TLSCACrtFile)
|
|
||||||
log.Printf("CA file: " + caFile)
|
|
||||||
caCert, err := ioutil.ReadFile(caFile)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("Could not open CA crt file " + caFile)
|
|
||||||
}
|
|
||||||
caCertPool := x509.NewCertPool()
|
|
||||||
if !caCertPool.AppendCertsFromPEM(caCert) {
|
|
||||||
return nil, errors.New("Could not parse CA crt file data in " + caFile)
|
|
||||||
}
|
|
||||||
tlsConfig := &tls.Config{
|
|
||||||
ClientCAs: caCertPool,
|
|
||||||
ClientAuth: tls.RequireAndVerifyClientCert,
|
|
||||||
}
|
|
||||||
server.TLSConfig = tlsConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
return server, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) stopTimer() {
|
|
||||||
if app.options.Timeout > 0 {
|
|
||||||
app.timer.Stop()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) restartTimer() {
|
|
||||||
if app.options.Timeout > 0 {
|
|
||||||
app.timer.Reset(time.Duration(app.options.Timeout) * time.Second)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) handleWS(w http.ResponseWriter, r *http.Request) {
|
|
||||||
app.stopTimer()
|
|
||||||
|
|
||||||
connections := atomic.AddInt64(app.connections, 1)
|
|
||||||
if int64(app.options.MaxConnection) != 0 {
|
|
||||||
if connections > int64(app.options.MaxConnection) {
|
|
||||||
log.Printf("Reached max connection: %d", app.options.MaxConnection)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log.Printf("New client connected: %s", r.RemoteAddr)
|
|
||||||
|
|
||||||
if r.Method != "GET" {
|
|
||||||
http.Error(w, "Method not allowed", 405)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
conn, err := app.upgrader.Upgrade(w, r, nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Print("Failed to upgrade connection: " + err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_, stream, err := conn.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
log.Print("Failed to authenticate websocket connection")
|
|
||||||
conn.Close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var init InitMessage
|
|
||||||
|
|
||||||
err = json.Unmarshal(stream, &init)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to parse init message %v", err)
|
|
||||||
conn.Close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if init.AuthToken != app.options.Credential {
|
|
||||||
log.Print("Failed to authenticate websocket connection")
|
|
||||||
conn.Close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
argv := app.command[1:]
|
|
||||||
if app.options.PermitArguments {
|
|
||||||
if init.Arguments == "" {
|
|
||||||
init.Arguments = "?"
|
|
||||||
}
|
|
||||||
query, err := url.Parse(init.Arguments)
|
|
||||||
if err != nil {
|
|
||||||
log.Print("Failed to parse arguments")
|
|
||||||
conn.Close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
params := query.Query()["arg"]
|
|
||||||
if len(params) != 0 {
|
|
||||||
argv = append(argv, params...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
app.server.StartRoutine()
|
|
||||||
|
|
||||||
if app.options.Once {
|
|
||||||
if app.onceMutex.TryLock() { // no unlock required, it will die soon
|
|
||||||
log.Printf("Last client accepted, closing the listener.")
|
|
||||||
app.server.Close()
|
|
||||||
} else {
|
|
||||||
log.Printf("Server is already closing.")
|
|
||||||
conn.Close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := exec.Command(app.command[0], argv...)
|
|
||||||
ptyIo, err := pty.Start(cmd)
|
|
||||||
if err != nil {
|
|
||||||
log.Print("Failed to execute command")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if app.options.MaxConnection != 0 {
|
|
||||||
log.Printf("Command is running for client %s with PID %d (args=%q), connections: %d/%d",
|
|
||||||
r.RemoteAddr, cmd.Process.Pid, strings.Join(argv, " "), connections, app.options.MaxConnection)
|
|
||||||
} else {
|
|
||||||
log.Printf("Command is running for client %s with PID %d (args=%q), connections: %d",
|
|
||||||
r.RemoteAddr, cmd.Process.Pid, strings.Join(argv, " "), connections)
|
|
||||||
}
|
|
||||||
|
|
||||||
context := &clientContext{
|
|
||||||
app: app,
|
|
||||||
request: r,
|
|
||||||
connection: conn,
|
|
||||||
command: cmd,
|
|
||||||
pty: ptyIo,
|
|
||||||
writeMutex: &sync.Mutex{},
|
|
||||||
}
|
|
||||||
|
|
||||||
context.goHandleClient()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) handleCustomIndex(w http.ResponseWriter, r *http.Request) {
|
|
||||||
http.ServeFile(w, r, ExpandHomeDir(app.options.IndexFile))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) handleAuthToken(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "application/javascript")
|
|
||||||
w.Write([]byte("var gotty_auth_token = '" + app.options.Credential + "';"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) Exit() (firstCall bool) {
|
|
||||||
if app.server != nil {
|
|
||||||
firstCall = app.server.Close()
|
|
||||||
if firstCall {
|
|
||||||
log.Printf("Received Exit command, waiting for all clients to close sessions...")
|
|
||||||
}
|
|
||||||
return firstCall
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func wrapLogger(handler http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
rw := &responseWrapper{w, 200}
|
|
||||||
handler.ServeHTTP(rw, r)
|
|
||||||
log.Printf("%s %d %s %s", r.RemoteAddr, rw.status, r.Method, r.URL.Path)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func wrapHeaders(handler http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Server", "GoTTY/"+Version)
|
|
||||||
handler.ServeHTTP(w, r)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func wrapBasicAuth(handler http.Handler, credential string) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
token := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
|
|
||||||
|
|
||||||
if len(token) != 2 || strings.ToLower(token[0]) != "basic" {
|
|
||||||
w.Header().Set("WWW-Authenticate", `Basic realm="GoTTY"`)
|
|
||||||
http.Error(w, "Bad Request", http.StatusUnauthorized)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
payload, err := base64.StdEncoding.DecodeString(token[1])
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if credential != string(payload) {
|
|
||||||
w.Header().Set("WWW-Authenticate", `Basic realm="GoTTY"`)
|
|
||||||
http.Error(w, "authorization failed", http.StatusUnauthorized)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Basic Authentication Succeeded: %s", r.RemoteAddr)
|
|
||||||
handler.ServeHTTP(w, r)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func generateRandomString(length int) string {
|
|
||||||
const base = 36
|
|
||||||
size := big.NewInt(base)
|
|
||||||
n := make([]byte, length)
|
|
||||||
for i, _ := range n {
|
|
||||||
c, _ := rand.Int(rand.Reader, size)
|
|
||||||
n[i] = strconv.FormatInt(c.Int64(), base)[0]
|
|
||||||
}
|
|
||||||
return string(n)
|
|
||||||
}
|
|
||||||
|
|
||||||
func listAddresses() (addresses []string) {
|
|
||||||
ifaces, _ := net.Interfaces()
|
|
||||||
|
|
||||||
addresses = make([]string, 0, len(ifaces))
|
|
||||||
|
|
||||||
for _, iface := range ifaces {
|
|
||||||
ifAddrs, _ := iface.Addrs()
|
|
||||||
for _, ifAddr := range ifAddrs {
|
|
||||||
switch v := ifAddr.(type) {
|
|
||||||
case *net.IPNet:
|
|
||||||
addresses = append(addresses, v.IP.String())
|
|
||||||
case *net.IPAddr:
|
|
||||||
addresses = append(addresses, v.IP.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExpandHomeDir(path string) string {
|
|
||||||
if path[0:2] == "~/" {
|
|
||||||
return os.Getenv("HOME") + path[1:]
|
|
||||||
} else {
|
|
||||||
return path
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,241 +0,0 @@
|
|||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
"syscall"
|
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
"github.com/fatih/structs"
|
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
)
|
|
||||||
|
|
||||||
type clientContext struct {
|
|
||||||
app *App
|
|
||||||
request *http.Request
|
|
||||||
connection *websocket.Conn
|
|
||||||
command *exec.Cmd
|
|
||||||
pty *os.File
|
|
||||||
writeMutex *sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
Input = '0'
|
|
||||||
Ping = '1'
|
|
||||||
ResizeTerminal = '2'
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
Output = '0'
|
|
||||||
Pong = '1'
|
|
||||||
SetWindowTitle = '2'
|
|
||||||
SetPreferences = '3'
|
|
||||||
SetReconnect = '4'
|
|
||||||
)
|
|
||||||
|
|
||||||
type argResizeTerminal struct {
|
|
||||||
Columns float64
|
|
||||||
Rows float64
|
|
||||||
}
|
|
||||||
|
|
||||||
type ContextVars struct {
|
|
||||||
Command string
|
|
||||||
Pid int
|
|
||||||
Hostname string
|
|
||||||
RemoteAddr string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *clientContext) goHandleClient() {
|
|
||||||
exit := make(chan bool, 2)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer func() { exit <- true }()
|
|
||||||
|
|
||||||
context.processSend()
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer func() { exit <- true }()
|
|
||||||
|
|
||||||
context.processReceive()
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer context.app.server.FinishRoutine()
|
|
||||||
defer func() {
|
|
||||||
connections := atomic.AddInt64(context.app.connections, -1)
|
|
||||||
|
|
||||||
if context.app.options.MaxConnection != 0 {
|
|
||||||
log.Printf("Connection closed: %s, connections: %d/%d",
|
|
||||||
context.request.RemoteAddr, connections, context.app.options.MaxConnection)
|
|
||||||
} else {
|
|
||||||
log.Printf("Connection closed: %s, connections: %d",
|
|
||||||
context.request.RemoteAddr, connections)
|
|
||||||
}
|
|
||||||
|
|
||||||
if connections == 0 {
|
|
||||||
context.app.restartTimer()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
<-exit
|
|
||||||
context.pty.Close()
|
|
||||||
|
|
||||||
// Even if the PTY has been closed,
|
|
||||||
// Read(0 in processSend() keeps blocking and the process doen't exit
|
|
||||||
context.command.Process.Signal(syscall.Signal(context.app.options.CloseSignal))
|
|
||||||
|
|
||||||
context.command.Wait()
|
|
||||||
context.connection.Close()
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *clientContext) processSend() {
|
|
||||||
if err := context.sendInitialize(); err != nil {
|
|
||||||
log.Printf(err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
buf := make([]byte, 1024)
|
|
||||||
|
|
||||||
for {
|
|
||||||
size, err := context.pty.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Command exited for: %s", context.request.RemoteAddr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
safeMessage := base64.StdEncoding.EncodeToString([]byte(buf[:size]))
|
|
||||||
if err = context.write(append([]byte{Output}, []byte(safeMessage)...)); err != nil {
|
|
||||||
log.Printf(err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *clientContext) write(data []byte) error {
|
|
||||||
context.writeMutex.Lock()
|
|
||||||
defer context.writeMutex.Unlock()
|
|
||||||
return context.connection.WriteMessage(websocket.TextMessage, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *clientContext) sendInitialize() error {
|
|
||||||
hostname, _ := os.Hostname()
|
|
||||||
titleVars := ContextVars{
|
|
||||||
Command: strings.Join(context.app.command, " "),
|
|
||||||
Pid: context.command.Process.Pid,
|
|
||||||
Hostname: hostname,
|
|
||||||
RemoteAddr: context.request.RemoteAddr,
|
|
||||||
}
|
|
||||||
|
|
||||||
titleBuffer := new(bytes.Buffer)
|
|
||||||
if err := context.app.titleTemplate.Execute(titleBuffer, titleVars); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := context.write(append([]byte{SetWindowTitle}, titleBuffer.Bytes()...)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
prefStruct := structs.New(context.app.options.Preferences)
|
|
||||||
prefMap := prefStruct.Map()
|
|
||||||
htermPrefs := make(map[string]interface{})
|
|
||||||
for key, value := range prefMap {
|
|
||||||
rawKey := prefStruct.Field(key).Tag("hcl")
|
|
||||||
if _, ok := context.app.options.RawPreferences[rawKey]; ok {
|
|
||||||
htermPrefs[strings.Replace(rawKey, "_", "-", -1)] = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
prefs, err := json.Marshal(htermPrefs)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := context.write(append([]byte{SetPreferences}, prefs...)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if context.app.options.EnableReconnect {
|
|
||||||
reconnect, _ := json.Marshal(context.app.options.ReconnectTime)
|
|
||||||
if err := context.write(append([]byte{SetReconnect}, reconnect...)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *clientContext) processReceive() {
|
|
||||||
for {
|
|
||||||
_, data, err := context.connection.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
log.Print(err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(data) == 0 {
|
|
||||||
log.Print("An error has occured")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch data[0] {
|
|
||||||
case Input:
|
|
||||||
if !context.app.options.PermitWrite {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := context.pty.Write(data[1:])
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
case Ping:
|
|
||||||
if err := context.write([]byte{Pong}); err != nil {
|
|
||||||
log.Print(err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
case ResizeTerminal:
|
|
||||||
var args argResizeTerminal
|
|
||||||
err = json.Unmarshal(data[1:], &args)
|
|
||||||
if err != nil {
|
|
||||||
log.Print("Malformed remote command")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
rows := uint16(context.app.options.Height)
|
|
||||||
if rows == 0 {
|
|
||||||
rows = uint16(args.Rows)
|
|
||||||
}
|
|
||||||
|
|
||||||
columns := uint16(context.app.options.Width)
|
|
||||||
if columns == 0 {
|
|
||||||
columns = uint16(args.Columns)
|
|
||||||
}
|
|
||||||
|
|
||||||
window := struct {
|
|
||||||
row uint16
|
|
||||||
col uint16
|
|
||||||
x uint16
|
|
||||||
y uint16
|
|
||||||
}{
|
|
||||||
rows,
|
|
||||||
columns,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
}
|
|
||||||
syscall.Syscall(
|
|
||||||
syscall.SYS_IOCTL,
|
|
||||||
context.pty.Fd(),
|
|
||||||
syscall.TIOCSWINSZ,
|
|
||||||
uintptr(unsafe.Pointer(&window)),
|
|
||||||
)
|
|
||||||
|
|
||||||
default:
|
|
||||||
log.Print("Unknown message type")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,58 +0,0 @@
|
|||||||
package app
|
|
||||||
|
|
||||||
type HtermPrefernces struct {
|
|
||||||
AltGrMode *string `hcl:"alt_gr_mode"`
|
|
||||||
AltBackspaceIsMetaBackspace bool `hcl:"alt_backspace_is_meta_backspace"`
|
|
||||||
AltIsMeta bool `hcl:"alt_is_meta"`
|
|
||||||
AltSendsWhat string `hcl:"alt_sends_what"`
|
|
||||||
AudibleBellSound string `hcl:"audible_bell_sound"`
|
|
||||||
DesktopNotificationBell bool `hcl:"desktop_notification_bell"`
|
|
||||||
BackgroundColor string `hcl:"background_color"`
|
|
||||||
BackgroundImage string `hcl:"background_image"`
|
|
||||||
BackgroundSize string `hcl:"background_size"`
|
|
||||||
BackgroundPosition string `hcl:"background_position"`
|
|
||||||
BackspaceSendsBackspace bool `hcl:"backspace_sends_backspace"`
|
|
||||||
CharacterMapOverrides map[string]map[string]string `hcl:"character_map_overrides"`
|
|
||||||
CloseOnExit bool `hcl:"close_on_exit"`
|
|
||||||
CursorBlink bool `hcl:"cursor_blink"`
|
|
||||||
CursorBlinkCycle [2]int `hcl:"cursor_blink_cycle"`
|
|
||||||
CursorColor string `hcl:"cursor_color"`
|
|
||||||
ColorPaletteOverrides []*string `hcl:"color_palette_overrides"`
|
|
||||||
CopyOnSelect bool `hcl:"copy_on_select"`
|
|
||||||
UseDefaultWindowCopy bool `hcl:"use_default_window_copy"`
|
|
||||||
ClearSelectionAfterCopy bool `hcl:"clear_selection_after_copy"`
|
|
||||||
CtrlPlusMinusZeroZoom bool `hcl:"ctrl_plus_minus_zero_zoom"`
|
|
||||||
CtrlCCopy bool `hcl:"ctrl_c_copy"`
|
|
||||||
CtrlVPaste bool `hcl:"ctrl_v_paste"`
|
|
||||||
EastAsianAmbiguousAsTwoColumn bool `hcl:"east_asian_ambiguous_as_two_column"`
|
|
||||||
Enable8BitControl *bool `hcl:"enable_8_bit_control"`
|
|
||||||
EnableBold *bool `hcl:"enable_bold"`
|
|
||||||
EnableBoldAsBright bool `hcl:"enable_bold_as_bright"`
|
|
||||||
EnableClipboardNotice bool `hcl:"enable_clipboard_notice"`
|
|
||||||
EnableClipboardWrite bool `hcl:"enable_clipboard_write"`
|
|
||||||
EnableDec12 bool `hcl:"enable_dec12"`
|
|
||||||
Environment map[string]string `hcl:"environment"`
|
|
||||||
FontFamily string `hcl:"font_family"`
|
|
||||||
FontSize int `hcl:"font_size"`
|
|
||||||
FontSmoothing string `hcl:"font_smoothing"`
|
|
||||||
ForegroundColor string `hcl:"foreground_color"`
|
|
||||||
HomeKeysScroll bool `hcl:"home_keys_scroll"`
|
|
||||||
Keybindings map[string]string `hcl:"keybindings"`
|
|
||||||
MaxStringSequence int `hcl:"max_string_sequence"`
|
|
||||||
MediaKeysAreFkeys bool `hcl:"media_keys_are_fkeys"`
|
|
||||||
MetaSendsEscape bool `hcl:"meta_sends_escape"`
|
|
||||||
MousePasteButton *int `hcl:"mouse_paste_button"`
|
|
||||||
PageKeysScroll bool `hcl:"page_keys_scroll"`
|
|
||||||
PassAltNumber *bool `hcl:"pass_alt_number"`
|
|
||||||
PassCtrlNumber *bool `hcl:"pass_ctrl_number"`
|
|
||||||
PassMetaNumber *bool `hcl:"pass_meta_number"`
|
|
||||||
PassMetaV bool `hcl:"pass_meta_v"`
|
|
||||||
ReceiveEncoding string `hcl:"receive_encoding"`
|
|
||||||
ScrollOnKeystroke bool `hcl:"scroll_on_keystroke"`
|
|
||||||
ScrollOnOutput bool `hcl:"scroll_on_output"`
|
|
||||||
ScrollbarVisible bool `hcl:"scrollbar_visible"`
|
|
||||||
ScrollWheelMoveMultiplier int `hcl:"scroll_wheel_move_multiplier"`
|
|
||||||
SendEncoding string `hcl:"send_encoding"`
|
|
||||||
ShiftInsertPaste bool `hcl:"shift_insert_paste"`
|
|
||||||
UserCss string `hcl:"user_css"`
|
|
||||||
}
|
|
1
backend/doc.go
Normal file
1
backend/doc.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package backend
|
3
backend/localcommand/doc.go
Normal file
3
backend/localcommand/doc.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
// Package localcommand provides an implementation of webtty.Slave
|
||||||
|
// that launches a local command with a PTY.
|
||||||
|
package localcommand
|
44
backend/localcommand/factory.go
Normal file
44
backend/localcommand/factory.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package localcommand
|
||||||
|
|
||||||
|
import (
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/yudai/gotty/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
CloseSignal int `hcl:"close_signal" flagName:"close-signal" flagSName:"" flagDescribe:"Signal sent to the command process when gotty close it (default: SIGHUP)" default:"1"`
|
||||||
|
CloseTimeout int `hcl:"close_timeout" flagName:"close-timeout" flagSName:"" flagDescribe:"Time in seconds to force kill process after client is disconnected (default: -1)" default:"-1"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Factory struct {
|
||||||
|
command string
|
||||||
|
argv []string
|
||||||
|
options *Options
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFactory(command string, argv []string, options *Options) (*Factory, error) {
|
||||||
|
return &Factory{
|
||||||
|
command: command,
|
||||||
|
argv: argv,
|
||||||
|
options: options,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (factory *Factory) Name() string {
|
||||||
|
return "local command"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (factory *Factory) New(params map[string][]string) (server.Slave, error) {
|
||||||
|
argv := make([]string, len(factory.argv))
|
||||||
|
copy(argv, factory.argv)
|
||||||
|
if params["arg"] != nil && len(params["arg"]) > 0 {
|
||||||
|
argv = append(argv, params["arg"]...)
|
||||||
|
}
|
||||||
|
return New(
|
||||||
|
factory.command,
|
||||||
|
argv,
|
||||||
|
WithCloseSignal(syscall.Signal(factory.options.CloseSignal)),
|
||||||
|
WithCloseSignal(syscall.Signal(factory.options.CloseTimeout)),
|
||||||
|
)
|
||||||
|
}
|
136
backend/localcommand/local_command.go
Normal file
136
backend/localcommand/local_command.go
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
package localcommand
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/kr/pty"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DefaultCloseSignal = syscall.SIGINT
|
||||||
|
DefaultCloseTimeout = 10 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
type LocalCommand struct {
|
||||||
|
command string
|
||||||
|
argv []string
|
||||||
|
|
||||||
|
closeSignal syscall.Signal
|
||||||
|
closeTimeout time.Duration
|
||||||
|
|
||||||
|
cmd *exec.Cmd
|
||||||
|
pty *os.File
|
||||||
|
ptyClosed chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(command string, argv []string, options ...Option) (*LocalCommand, error) {
|
||||||
|
cmd := exec.Command(command, argv...)
|
||||||
|
|
||||||
|
pty, err := pty.Start(cmd)
|
||||||
|
if err != nil {
|
||||||
|
// todo close cmd?
|
||||||
|
return nil, errors.Wrapf(err, "failed to start command `%s`", command)
|
||||||
|
}
|
||||||
|
ptyClosed := make(chan struct{})
|
||||||
|
|
||||||
|
lcmd := &LocalCommand{
|
||||||
|
command: command,
|
||||||
|
argv: argv,
|
||||||
|
|
||||||
|
closeSignal: DefaultCloseSignal,
|
||||||
|
closeTimeout: DefaultCloseTimeout,
|
||||||
|
|
||||||
|
cmd: cmd,
|
||||||
|
pty: pty,
|
||||||
|
ptyClosed: ptyClosed,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
option(lcmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// When the process is closed by the user,
|
||||||
|
// close pty so that Read() on the pty breaks with an EOF.
|
||||||
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
lcmd.pty.Close()
|
||||||
|
close(lcmd.ptyClosed)
|
||||||
|
}()
|
||||||
|
|
||||||
|
lcmd.cmd.Wait()
|
||||||
|
}()
|
||||||
|
|
||||||
|
return lcmd, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lcmd *LocalCommand) Read(p []byte) (n int, err error) {
|
||||||
|
return lcmd.pty.Read(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lcmd *LocalCommand) Write(p []byte) (n int, err error) {
|
||||||
|
return lcmd.pty.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lcmd *LocalCommand) Close() error {
|
||||||
|
if lcmd.cmd != nil && lcmd.cmd.Process != nil {
|
||||||
|
lcmd.cmd.Process.Signal(lcmd.closeSignal)
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-lcmd.ptyClosed:
|
||||||
|
return nil
|
||||||
|
case <-lcmd.closeTimeoutC():
|
||||||
|
lcmd.cmd.Process.Signal(syscall.SIGKILL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lcmd *LocalCommand) WindowTitleVariables() map[string]interface{} {
|
||||||
|
return map[string]interface{}{
|
||||||
|
"command": lcmd.command,
|
||||||
|
"argv": lcmd.argv,
|
||||||
|
"pid": lcmd.cmd.Process.Pid,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lcmd *LocalCommand) ResizeTerminal(width int, height int) error {
|
||||||
|
window := struct {
|
||||||
|
row uint16
|
||||||
|
col uint16
|
||||||
|
x uint16
|
||||||
|
y uint16
|
||||||
|
}{
|
||||||
|
uint16(height),
|
||||||
|
uint16(width),
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
}
|
||||||
|
_, _, errno := syscall.Syscall(
|
||||||
|
syscall.SYS_IOCTL,
|
||||||
|
lcmd.pty.Fd(),
|
||||||
|
syscall.TIOCSWINSZ,
|
||||||
|
uintptr(unsafe.Pointer(&window)),
|
||||||
|
)
|
||||||
|
if errno != 0 {
|
||||||
|
return errno
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lcmd *LocalCommand) GetTerminalSize() (int, int, error) {
|
||||||
|
return pty.Getsize(lcmd.pty)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lcmd *LocalCommand) closeTimeoutC() <-chan time.Time {
|
||||||
|
if lcmd.closeTimeout >= 0 {
|
||||||
|
return time.After(lcmd.closeTimeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
return make(chan time.Time)
|
||||||
|
}
|
20
backend/localcommand/options.go
Normal file
20
backend/localcommand/options.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package localcommand
|
||||||
|
|
||||||
|
import (
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Option func(*LocalCommand)
|
||||||
|
|
||||||
|
func WithCloseSignal(signal syscall.Signal) Option {
|
||||||
|
return func(lcmd *LocalCommand) {
|
||||||
|
lcmd.closeSignal = signal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithCloseTimeout(timeout time.Duration) Option {
|
||||||
|
return func(lcmd *LocalCommand) {
|
||||||
|
lcmd.closeTimeout = timeout
|
||||||
|
}
|
||||||
|
}
|
101
flags.go
101
flags.go
@ -1,101 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/codegangsta/cli"
|
|
||||||
"github.com/fatih/structs"
|
|
||||||
|
|
||||||
"github.com/yudai/gotty/app"
|
|
||||||
)
|
|
||||||
|
|
||||||
type flag struct {
|
|
||||||
name string
|
|
||||||
shortName string
|
|
||||||
description string
|
|
||||||
}
|
|
||||||
|
|
||||||
func generateFlags(flags []flag, hint map[string]string) ([]cli.Flag, error) {
|
|
||||||
o := structs.New(app.DefaultOptions)
|
|
||||||
|
|
||||||
results := make([]cli.Flag, len(flags))
|
|
||||||
|
|
||||||
for i, flag := range flags {
|
|
||||||
fieldName := fieldName(flag.name, hint)
|
|
||||||
|
|
||||||
field, ok := o.FieldOk(fieldName)
|
|
||||||
if !ok {
|
|
||||||
return nil, errors.New("No such field: " + fieldName)
|
|
||||||
}
|
|
||||||
|
|
||||||
flagName := flag.name
|
|
||||||
if flag.shortName != "" {
|
|
||||||
flagName += ", " + flag.shortName
|
|
||||||
}
|
|
||||||
envName := "GOTTY_" + strings.ToUpper(strings.Join(strings.Split(flag.name, "-"), "_"))
|
|
||||||
|
|
||||||
switch field.Kind() {
|
|
||||||
case reflect.String:
|
|
||||||
results[i] = cli.StringFlag{
|
|
||||||
Name: flagName,
|
|
||||||
Value: field.Value().(string),
|
|
||||||
Usage: flag.description,
|
|
||||||
EnvVar: envName,
|
|
||||||
}
|
|
||||||
case reflect.Bool:
|
|
||||||
results[i] = cli.BoolFlag{
|
|
||||||
Name: flagName,
|
|
||||||
Usage: flag.description,
|
|
||||||
EnvVar: envName,
|
|
||||||
}
|
|
||||||
case reflect.Int:
|
|
||||||
results[i] = cli.IntFlag{
|
|
||||||
Name: flagName,
|
|
||||||
Value: field.Value().(int),
|
|
||||||
Usage: flag.description,
|
|
||||||
EnvVar: envName,
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return nil, errors.New("Unsupported type: " + fieldName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return results, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func applyFlags(
|
|
||||||
options *app.Options,
|
|
||||||
flags []flag,
|
|
||||||
mappingHint map[string]string,
|
|
||||||
c *cli.Context,
|
|
||||||
) {
|
|
||||||
o := structs.New(options)
|
|
||||||
for _, flag := range flags {
|
|
||||||
if c.IsSet(flag.name) {
|
|
||||||
field := o.Field(fieldName(flag.name, mappingHint))
|
|
||||||
var val interface{}
|
|
||||||
switch field.Kind() {
|
|
||||||
case reflect.String:
|
|
||||||
val = c.String(flag.name)
|
|
||||||
case reflect.Bool:
|
|
||||||
val = c.Bool(flag.name)
|
|
||||||
case reflect.Int:
|
|
||||||
val = c.Int(flag.name)
|
|
||||||
}
|
|
||||||
field.Set(val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func fieldName(name string, hint map[string]string) string {
|
|
||||||
if fieldName, ok := hint[name]; ok {
|
|
||||||
return fieldName
|
|
||||||
}
|
|
||||||
nameParts := strings.Split(name, "-")
|
|
||||||
for i, part := range nameParts {
|
|
||||||
nameParts[i] = strings.ToUpper(part[0:1]) + part[1:]
|
|
||||||
}
|
|
||||||
return strings.Join(nameParts, "")
|
|
||||||
}
|
|
147
main.go
147
main.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@ -8,56 +9,35 @@ import (
|
|||||||
|
|
||||||
"github.com/codegangsta/cli"
|
"github.com/codegangsta/cli"
|
||||||
|
|
||||||
"github.com/yudai/gotty/app"
|
"github.com/yudai/gotty/backend/localcommand"
|
||||||
|
"github.com/yudai/gotty/pkg/homedir"
|
||||||
|
"github.com/yudai/gotty/server"
|
||||||
|
"github.com/yudai/gotty/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cmd := cli.NewApp()
|
app := cli.NewApp()
|
||||||
cmd.Version = app.Version
|
app.Name = "gotty"
|
||||||
cmd.Name = "gotty"
|
app.Version = Version
|
||||||
cmd.Usage = "Share your terminal as a web application"
|
app.Usage = "Share your terminal as a web application"
|
||||||
cmd.HideHelp = true
|
app.HideHelp = true
|
||||||
|
cli.AppHelpTemplate = helpTemplate
|
||||||
|
|
||||||
flags := []flag{
|
appOptions := &server.Options{}
|
||||||
flag{"address", "a", "IP address to listen"},
|
if err := utils.ApplyDefaultValues(appOptions); err != nil {
|
||||||
flag{"port", "p", "Port number to listen"},
|
exit(err, 1)
|
||||||
flag{"permit-write", "w", "Permit clients to write to the TTY (BE CAREFUL)"},
|
}
|
||||||
flag{"credential", "c", "Credential for Basic Authentication (ex: user:pass, default disabled)"},
|
backendOptions := &localcommand.Options{}
|
||||||
flag{"random-url", "r", "Add a random string to the URL"},
|
if err := utils.ApplyDefaultValues(backendOptions); err != nil {
|
||||||
flag{"random-url-length", "", "Random URL length"},
|
exit(err, 1)
|
||||||
flag{"tls", "t", "Enable TLS/SSL"},
|
|
||||||
flag{"tls-crt", "", "TLS/SSL certificate file path"},
|
|
||||||
flag{"tls-key", "", "TLS/SSL key file path"},
|
|
||||||
flag{"tls-ca-crt", "", "TLS/SSL CA certificate file for client certifications"},
|
|
||||||
flag{"index", "", "Custom index.html file"},
|
|
||||||
flag{"title-format", "", "Title format of browser window"},
|
|
||||||
flag{"reconnect", "", "Enable reconnection"},
|
|
||||||
flag{"reconnect-time", "", "Time to reconnect"},
|
|
||||||
flag{"timeout", "", "Timeout seconds for waiting a client (0 to disable)"},
|
|
||||||
flag{"max-connection", "", "Maximum connection to gotty, 0(default) means no limit"},
|
|
||||||
flag{"once", "", "Accept only one client and exit on disconnection"},
|
|
||||||
flag{"permit-arguments", "", "Permit clients to send command line arguments in URL (e.g. http://example.com:8080/?arg=AAA&arg=BBB)"},
|
|
||||||
flag{"close-signal", "", "Signal sent to the command process when gotty close it (default: SIGHUP)"},
|
|
||||||
flag{"width", "", "Static width of the screen, 0(default) means dynamically resize"},
|
|
||||||
flag{"height", "", "Static height of the screen, 0(default) means dynamically resize"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mappingHint := map[string]string{
|
cliFlags, flagMappings, err := utils.GenerateFlags(appOptions, backendOptions)
|
||||||
"index": "IndexFile",
|
|
||||||
"tls": "EnableTLS",
|
|
||||||
"tls-crt": "TLSCrtFile",
|
|
||||||
"tls-key": "TLSKeyFile",
|
|
||||||
"tls-ca-crt": "TLSCACrtFile",
|
|
||||||
"random-url": "EnableRandomUrl",
|
|
||||||
"reconnect": "EnableReconnect",
|
|
||||||
}
|
|
||||||
|
|
||||||
cliFlags, err := generateFlags(flags, mappingHint)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exit(err, 3)
|
exit(err, 3)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flags = append(
|
app.Flags = append(
|
||||||
cliFlags,
|
cliFlags,
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "config",
|
Name: "config",
|
||||||
@ -67,52 +47,65 @@ func main() {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
cmd.Action = func(c *cli.Context) {
|
app.Action = func(c *cli.Context) {
|
||||||
if len(c.Args()) == 0 {
|
if len(c.Args()) == 0 {
|
||||||
fmt.Println("Error: No command given.\n")
|
msg := "Error: No command given."
|
||||||
cli.ShowAppHelp(c)
|
cli.ShowAppHelp(c)
|
||||||
exit(err, 1)
|
exit(fmt.Errorf(msg), 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
options := app.DefaultOptions
|
|
||||||
|
|
||||||
configFile := c.String("config")
|
configFile := c.String("config")
|
||||||
_, err := os.Stat(app.ExpandHomeDir(configFile))
|
_, err := os.Stat(homedir.Expand(configFile))
|
||||||
if configFile != "~/.gotty" || !os.IsNotExist(err) {
|
if configFile != "~/.gotty" || !os.IsNotExist(err) {
|
||||||
if err := app.ApplyConfigFile(&options, configFile); err != nil {
|
if err := utils.ApplyConfigFile(configFile, appOptions, backendOptions); err != nil {
|
||||||
exit(err, 2)
|
exit(err, 2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
applyFlags(&options, flags, mappingHint, c)
|
utils.ApplyFlags(cliFlags, flagMappings, c, appOptions, backendOptions)
|
||||||
|
|
||||||
if c.IsSet("credential") {
|
appOptions.EnableBasicAuth = c.IsSet("credential")
|
||||||
options.EnableBasicAuth = true
|
appOptions.EnableTLSClientAuth = c.IsSet("tls-ca-crt")
|
||||||
}
|
|
||||||
if c.IsSet("tls-ca-crt") {
|
|
||||||
options.EnableTLSClientAuth = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := app.CheckConfig(&options); err != nil {
|
err = appOptions.Validate()
|
||||||
|
if err != nil {
|
||||||
exit(err, 6)
|
exit(err, 6)
|
||||||
}
|
}
|
||||||
|
|
||||||
app, err := app.New(c.Args(), &options)
|
args := c.Args()
|
||||||
|
factory, err := localcommand.NewFactory(args[0], args[1:], backendOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exit(err, 3)
|
exit(err, 3)
|
||||||
}
|
}
|
||||||
|
|
||||||
registerSignals(app)
|
hostname, _ := os.Hostname()
|
||||||
|
appOptions.TitleVariables = map[string]interface{}{
|
||||||
|
"command": args[0],
|
||||||
|
"argv": args[1:],
|
||||||
|
"hostname": hostname,
|
||||||
|
}
|
||||||
|
|
||||||
err = app.Run()
|
srv, err := server.New(factory, appOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exit(err, 4)
|
exit(err, 3)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cli.AppHelpTemplate = helpTemplate
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
gCtx, gCancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
cmd.Run(os.Args)
|
errs := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
errs <- srv.Run(ctx, server.WithGracefullContext(gCtx))
|
||||||
|
}()
|
||||||
|
err = waitSignals(errs, cancel, gCancel)
|
||||||
|
|
||||||
|
if err != nil && err != context.Canceled {
|
||||||
|
fmt.Printf("Error: %s\n", err)
|
||||||
|
exit(err, 8)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
app.Run(os.Args)
|
||||||
}
|
}
|
||||||
|
|
||||||
func exit(err error, code int) {
|
func exit(err error, code int) {
|
||||||
@ -122,7 +115,7 @@ func exit(err error, code int) {
|
|||||||
os.Exit(code)
|
os.Exit(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerSignals(app *app.App) {
|
func waitSignals(errs chan error, cancel context.CancelFunc, gracefullCancel context.CancelFunc) error {
|
||||||
sigChan := make(chan os.Signal, 1)
|
sigChan := make(chan os.Signal, 1)
|
||||||
signal.Notify(
|
signal.Notify(
|
||||||
sigChan,
|
sigChan,
|
||||||
@ -130,17 +123,25 @@ func registerSignals(app *app.App) {
|
|||||||
syscall.SIGTERM,
|
syscall.SIGTERM,
|
||||||
)
|
)
|
||||||
|
|
||||||
go func() {
|
select {
|
||||||
for {
|
case err := <-errs:
|
||||||
s := <-sigChan
|
return err
|
||||||
|
|
||||||
|
case s := <-sigChan:
|
||||||
switch s {
|
switch s {
|
||||||
case syscall.SIGINT, syscall.SIGTERM:
|
case syscall.SIGINT:
|
||||||
if app.Exit() {
|
gracefullCancel()
|
||||||
fmt.Println("Send ^C to force exit.")
|
fmt.Println("C-C to force close")
|
||||||
} else {
|
select {
|
||||||
os.Exit(5)
|
case err := <-errs:
|
||||||
|
return err
|
||||||
|
case <-sigChan:
|
||||||
|
cancel()
|
||||||
|
return <-errs
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
cancel()
|
||||||
|
return <-errs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
13
pkg/homedir/expand.go
Normal file
13
pkg/homedir/expand.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package homedir
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Expand(path string) string {
|
||||||
|
if path[0:2] == "~/" {
|
||||||
|
return os.Getenv("HOME") + path[1:]
|
||||||
|
} else {
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
}
|
18
pkg/randomstring/generate.go
Normal file
18
pkg/randomstring/generate.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package randomstring
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"math/big"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Generate(length int) string {
|
||||||
|
const base = 36
|
||||||
|
size := big.NewInt(base)
|
||||||
|
n := make([]byte, length)
|
||||||
|
for i, _ := range n {
|
||||||
|
c, _ := rand.Int(rand.Reader, size)
|
||||||
|
n[i] = strconv.FormatInt(c.Int64(), base)[0]
|
||||||
|
}
|
||||||
|
return string(n)
|
||||||
|
}
|
@ -2,7 +2,7 @@
|
|||||||
var httpsEnabled = window.location.protocol == "https:";
|
var httpsEnabled = window.location.protocol == "https:";
|
||||||
var args = window.location.search;
|
var args = window.location.search;
|
||||||
var url = (httpsEnabled ? 'wss://' : 'ws://') + window.location.host + window.location.pathname + 'ws';
|
var url = (httpsEnabled ? 'wss://' : 'ws://') + window.location.host + window.location.pathname + 'ws';
|
||||||
var protocols = ["gotty"];
|
var protocols = ["webtty"];
|
||||||
var autoReconnect = -1;
|
var autoReconnect = -1;
|
||||||
|
|
||||||
var openWs = function() {
|
var openWs = function() {
|
||||||
@ -26,14 +26,14 @@
|
|||||||
var io = term.io.push();
|
var io = term.io.push();
|
||||||
|
|
||||||
io.onVTKeystroke = function(str) {
|
io.onVTKeystroke = function(str) {
|
||||||
ws.send("0" + str);
|
ws.send("1" + str);
|
||||||
};
|
};
|
||||||
|
|
||||||
io.sendString = io.onVTKeystroke;
|
io.sendString = io.onVTKeystroke;
|
||||||
|
|
||||||
io.onTerminalResize = function(columns, rows) {
|
io.onTerminalResize = function(columns, rows) {
|
||||||
ws.send(
|
ws.send(
|
||||||
"2" + JSON.stringify(
|
"3" + JSON.stringify(
|
||||||
{
|
{
|
||||||
columns: columns,
|
columns: columns,
|
||||||
rows: rows,
|
rows: rows,
|
||||||
@ -51,23 +51,23 @@
|
|||||||
ws.onmessage = function(event) {
|
ws.onmessage = function(event) {
|
||||||
data = event.data.slice(1);
|
data = event.data.slice(1);
|
||||||
switch(event.data[0]) {
|
switch(event.data[0]) {
|
||||||
case '0':
|
case '1':
|
||||||
term.io.writeUTF8(window.atob(data));
|
term.io.writeUTF8(window.atob(data));
|
||||||
break;
|
break;
|
||||||
case '1':
|
case '2':
|
||||||
// pong
|
// pong
|
||||||
break;
|
break;
|
||||||
case '2':
|
case '3':
|
||||||
term.setWindowTitle(data);
|
term.setWindowTitle(data);
|
||||||
break;
|
break;
|
||||||
case '3':
|
case '4':
|
||||||
preferences = JSON.parse(data);
|
preferences = JSON.parse(data);
|
||||||
Object.keys(preferences).forEach(function(key) {
|
Object.keys(preferences).forEach(function(key) {
|
||||||
console.log("Setting " + key + ": " + preferences[key]);
|
console.log("Setting " + key + ": " + preferences[key]);
|
||||||
term.getPrefs().set(key, preferences[key]);
|
term.getPrefs().set(key, preferences[key]);
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case '4':
|
case '5':
|
||||||
autoReconnect = JSON.parse(data);
|
autoReconnect = JSON.parse(data);
|
||||||
console.log("Enabling reconnect: " + autoReconnect + " seconds")
|
console.log("Enabling reconnect: " + autoReconnect + " seconds")
|
||||||
break;
|
break;
|
||||||
@ -88,7 +88,7 @@
|
|||||||
|
|
||||||
|
|
||||||
var sendPing = function(ws) {
|
var sendPing = function(ws) {
|
||||||
ws.send("1");
|
ws.send("2");
|
||||||
}
|
}
|
||||||
|
|
||||||
openWs();
|
openWs();
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>GoTTY</title>
|
<title>{{ .title }}</title>
|
||||||
<style>body, #terminal {position: absolute; height: 100%; width: 100%; margin: 0px;}</style>
|
<style>body, #terminal {position: absolute; height: 100%; width: 100%; margin: 0px;}</style>
|
||||||
<link rel="icon" type="image/png" href="favicon.png">
|
<link rel="icon" type="image/png" href="favicon.png">
|
||||||
</head>
|
</head>
|
||||||
|
File diff suppressed because one or more lines are too long
238
server/handlers.go
Normal file
238
server/handlers.go
Normal file
@ -0,0 +1,238 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"github.com/yudai/gotty/webtty"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (server *Server) generateHandleWS(ctx context.Context, cancel context.CancelFunc) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if server.options.Once {
|
||||||
|
if atomic.LoadInt64(server.once) > 0 {
|
||||||
|
http.Error(w, "Server is shutting down", http.StatusServiceUnavailable)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
atomic.AddInt64(server.once, 1)
|
||||||
|
}
|
||||||
|
connections := atomic.AddInt64(server.connections, 1)
|
||||||
|
server.wsWG.Add(1)
|
||||||
|
server.stopTimer()
|
||||||
|
closeReason := "unknown reason"
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
server.wsWG.Done()
|
||||||
|
|
||||||
|
connections := atomic.AddInt64(server.connections, -1)
|
||||||
|
if connections == 0 {
|
||||||
|
server.resetTimer()
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf(
|
||||||
|
"Connection closed by %s: %s, connections: %d/%d",
|
||||||
|
closeReason, r.RemoteAddr, connections, server.options.MaxConnection,
|
||||||
|
)
|
||||||
|
|
||||||
|
if server.options.Once {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
log.Printf("New client connected: %s", r.RemoteAddr)
|
||||||
|
if int64(server.options.MaxConnection) != 0 {
|
||||||
|
if connections > int64(server.options.MaxConnection) {
|
||||||
|
closeReason = "exceeding max number of connections"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Method != "GET" {
|
||||||
|
http.Error(w, "Method not allowed", 405)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := server.upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Failed to upgrade connection: "+err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
err = server.processWSConn(ctx, conn)
|
||||||
|
|
||||||
|
switch err {
|
||||||
|
case ctx.Err():
|
||||||
|
closeReason = "cancelation"
|
||||||
|
case webtty.ErrSlaveClosed:
|
||||||
|
closeReason = server.factory.Name()
|
||||||
|
case webtty.ErrMasterClosed:
|
||||||
|
closeReason = "client"
|
||||||
|
default:
|
||||||
|
closeReason = fmt.Sprintf("an error: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *Server) processWSConn(ctx context.Context, conn *websocket.Conn) error {
|
||||||
|
typ, initLine, err := conn.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to authenticate websocket connection")
|
||||||
|
}
|
||||||
|
if typ != websocket.TextMessage {
|
||||||
|
return errors.New("failed to authenticate websocket connection: invalid message type")
|
||||||
|
}
|
||||||
|
|
||||||
|
var init InitMessage
|
||||||
|
err = json.Unmarshal(initLine, &init)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to authenticate websocket connection")
|
||||||
|
}
|
||||||
|
if init.AuthToken != server.options.Credential {
|
||||||
|
return errors.New("failed to authenticate websocket connection")
|
||||||
|
}
|
||||||
|
|
||||||
|
queryPath := "?"
|
||||||
|
if server.options.PermitArguments && init.Arguments != "" {
|
||||||
|
queryPath = init.Arguments
|
||||||
|
}
|
||||||
|
|
||||||
|
query, err := url.Parse(queryPath)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to parse arguments")
|
||||||
|
}
|
||||||
|
params := query.Query()
|
||||||
|
var slave Slave
|
||||||
|
slave, err = server.factory.New(params)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to create backend")
|
||||||
|
}
|
||||||
|
defer slave.Close()
|
||||||
|
|
||||||
|
titleVars := server.titleVariables(
|
||||||
|
[]string{"server", "master", "slave"},
|
||||||
|
map[string]map[string]interface{}{
|
||||||
|
"server": server.options.TitleVariables,
|
||||||
|
"master": map[string]interface{}{
|
||||||
|
"remote_addr": conn.RemoteAddr(),
|
||||||
|
},
|
||||||
|
"slave": slave.WindowTitleVariables(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
titleBuf := new(bytes.Buffer)
|
||||||
|
err = server.titleTemplate.Execute(titleBuf, titleVars)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to fill window title template")
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := []webtty.Option{
|
||||||
|
webtty.WithWindowTitle(titleBuf.Bytes()),
|
||||||
|
}
|
||||||
|
if server.options.PermitWrite {
|
||||||
|
opts = append(opts, webtty.WithPermitWrite())
|
||||||
|
}
|
||||||
|
if server.options.EnableReconnect {
|
||||||
|
opts = append(opts, webtty.WithReconnect(server.options.ReconnectTime))
|
||||||
|
}
|
||||||
|
if server.options.Width > 0 || server.options.Height > 0 {
|
||||||
|
width, height, err := slave.GetTerminalSize()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to get default terminal size")
|
||||||
|
}
|
||||||
|
if server.options.Width > 0 {
|
||||||
|
width = server.options.Width
|
||||||
|
}
|
||||||
|
if server.options.Height > 0 {
|
||||||
|
height = server.options.Height
|
||||||
|
}
|
||||||
|
err = slave.ResizeTerminal(width, height)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to resize terminal")
|
||||||
|
}
|
||||||
|
|
||||||
|
opts = append(opts, webtty.WithFixedSize(server.options.Width, server.options.Height))
|
||||||
|
}
|
||||||
|
if server.options.Preferences != nil {
|
||||||
|
opts = append(opts, webtty.WithMasterPreferences(server.options.Preferences))
|
||||||
|
}
|
||||||
|
|
||||||
|
tty, err := webtty.New(conn, slave, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to create webtty")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tty.Run(ctx)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *Server) handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
|
titleVars := server.titleVariables(
|
||||||
|
[]string{"server", "master"},
|
||||||
|
map[string]map[string]interface{}{
|
||||||
|
"server": server.options.TitleVariables,
|
||||||
|
"master": map[string]interface{}{
|
||||||
|
"remote_addr": r.RemoteAddr,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
titleBuf := new(bytes.Buffer)
|
||||||
|
err := server.titleTemplate.Execute(titleBuf, titleVars)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Internal Server Error", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
indexVars := map[string]interface{}{
|
||||||
|
"title": titleBuf.String(),
|
||||||
|
}
|
||||||
|
|
||||||
|
indexBuf := new(bytes.Buffer)
|
||||||
|
err = server.indexTemplate.Execute(indexBuf, indexVars)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Internal Server Error", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write(indexBuf.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *Server) handleAuthToken(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/javascript")
|
||||||
|
// @TODO hashing?
|
||||||
|
w.Write([]byte("var gotty_auth_token = '" + server.options.Credential + "';"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// titleVariables merges maps in a specified order.
|
||||||
|
// varUnits are name-keyed maps, whose names will be iterated using order.
|
||||||
|
func (server *Server) titleVariables(order []string, varUnits map[string]map[string]interface{}) map[string]interface{} {
|
||||||
|
titleVars := map[string]interface{}{}
|
||||||
|
|
||||||
|
for _, name := range order {
|
||||||
|
vars, ok := varUnits[name]
|
||||||
|
if !ok {
|
||||||
|
panic("title variable name error")
|
||||||
|
}
|
||||||
|
for key, val := range vars {
|
||||||
|
titleVars[key] = val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// safe net for conflicted keys
|
||||||
|
for _, name := range order {
|
||||||
|
titleVars[name] = varUnits[name]
|
||||||
|
}
|
||||||
|
|
||||||
|
return titleVars
|
||||||
|
}
|
6
server/init_message.go
Normal file
6
server/init_message.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
type InitMessage struct {
|
||||||
|
Arguments string `json:"Arguments,omitempty"`
|
||||||
|
AuthToken string `json:"AuthToken,omitempty"`
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package app
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
@ -6,17 +6,17 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type responseWrapper struct {
|
type logResponseWriter struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
status int
|
status int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *responseWrapper) WriteHeader(status int) {
|
func (w *logResponseWriter) WriteHeader(status int) {
|
||||||
w.status = status
|
w.status = status
|
||||||
w.ResponseWriter.WriteHeader(status)
|
w.ResponseWriter.WriteHeader(status)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *responseWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
func (w *logResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||||
hj, _ := w.ResponseWriter.(http.Hijacker)
|
hj, _ := w.ResponseWriter.(http.Hijacker)
|
||||||
w.status = http.StatusSwitchingProtocols
|
w.status = http.StatusSwitchingProtocols
|
||||||
return hj.Hijack()
|
return hj.Hijack()
|
51
server/middleware.go
Normal file
51
server/middleware.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (server *Server) wrapLogger(handler http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
rw := &logResponseWriter{w, 200}
|
||||||
|
handler.ServeHTTP(rw, r)
|
||||||
|
log.Printf("%s %d %s %s", r.RemoteAddr, rw.status, r.Method, r.URL.Path)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *Server) wrapHeaders(handler http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// todo add version
|
||||||
|
w.Header().Set("Server", "GoTTY")
|
||||||
|
handler.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *Server) wrapBasicAuth(handler http.Handler, credential string) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
token := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
|
||||||
|
|
||||||
|
if len(token) != 2 || strings.ToLower(token[0]) != "basic" {
|
||||||
|
w.Header().Set("WWW-Authenticate", `Basic realm="GoTTY"`)
|
||||||
|
http.Error(w, "Bad Request", http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
payload, err := base64.StdEncoding.DecodeString(token[1])
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if credential != string(payload) {
|
||||||
|
w.Header().Set("WWW-Authenticate", `Basic realm="GoTTY"`)
|
||||||
|
http.Error(w, "authorization failed", http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Basic Authentication Succeeded: %s", r.RemoteAddr)
|
||||||
|
handler.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
97
server/options.go
Normal file
97
server/options.go
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
Address string `hcl:"address" flagName:"address" flagSName:"a" flagDescribe:"IP address to listen" default:"0.0.0.0"`
|
||||||
|
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"`
|
||||||
|
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"`
|
||||||
|
IndexFile string `hcl:"index_file" flagName:"index" flagDescribe:"Custom index.html file" default:""`
|
||||||
|
TitleFormat string `hcl:"title_format" flagName:"title-format" flagSName:"" flagDescribe:"Title format of browser window" default:"{{ .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"`
|
||||||
|
Preferences *HtermPrefernces `hcl:"preferences"`
|
||||||
|
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"`
|
||||||
|
|
||||||
|
TitleVariables map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (options *Options) Validate() error {
|
||||||
|
if options.EnableTLSClientAuth && !options.EnableTLS {
|
||||||
|
return errors.New("TLS client authentication is enabled, but TLS is not enabled")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type HtermPrefernces struct {
|
||||||
|
AltGrMode *string `hcl:"alt_gr_mode" json:"alt-gr-mode,omitempty"`
|
||||||
|
AltBackspaceIsMetaBackspace bool `hcl:"alt_backspace_is_meta_backspace" json:"alt-backspace-is-meta-backspace,omitempty"`
|
||||||
|
AltIsMeta bool `hcl:"alt_is_meta" json:"alt-is-meta,omitempty"`
|
||||||
|
AltSendsWhat string `hcl:"alt_sends_what" json:"alt-sends-what,omitempty"`
|
||||||
|
AudibleBellSound string `hcl:"audible_bell_sound" json:"audible-bell-sound,omitempty"`
|
||||||
|
DesktopNotificationBell bool `hcl:"desktop_notification_bell" json:"desktop-notification-bell,omitempty"`
|
||||||
|
BackgroundColor string `hcl:"background_color" json:"background-color,omitempty"`
|
||||||
|
BackgroundImage string `hcl:"background_image" json:"background-image,omitempty"`
|
||||||
|
BackgroundSize string `hcl:"background_size" json:"background-size,omitempty"`
|
||||||
|
BackgroundPosition string `hcl:"background_position" json:"background-position,omitempty"`
|
||||||
|
BackspaceSendsBackspace bool `hcl:"backspace_sends_backspace" json:"backspace-sends-backspace,omitempty"`
|
||||||
|
CharacterMapOverrides map[string]map[string]string `hcl:"character_map_overrides" json:"character-map-overrides,omitempty"`
|
||||||
|
CloseOnExit bool `hcl:"close_on_exit" json:"close-on-exit,omitempty"`
|
||||||
|
CursorBlink bool `hcl:"cursor_blink" json:"cursor-blink,omitempty"`
|
||||||
|
CursorBlinkCycle [2]int `hcl:"cursor_blink_cycle" json:"cursor-blink-cycle,omitempty"`
|
||||||
|
CursorColor string `hcl:"cursor_color" json:"cursor-color,omitempty"`
|
||||||
|
ColorPaletteOverrides []*string `hcl:"color_palette_overrides" json:"color-palette-overrides,omitempty"`
|
||||||
|
CopyOnSelect bool `hcl:"copy_on_select" json:"copy-on-select,omitempty"`
|
||||||
|
UseDefaultWindowCopy bool `hcl:"use_default_window_copy" json:"use-default-window-copy,omitempty"`
|
||||||
|
ClearSelectionAfterCopy bool `hcl:"clear_selection_after_copy" json:"clear-selection-after-copy,omitempty"`
|
||||||
|
CtrlPlusMinusZeroZoom bool `hcl:"ctrl_plus_minus_zero_zoom" json:"ctrl-plus-minus-zero-zoom,omitempty"`
|
||||||
|
CtrlCCopy bool `hcl:"ctrl_c_copy" json:"ctrl-c-copy,omitempty"`
|
||||||
|
CtrlVPaste bool `hcl:"ctrl_v_paste" json:"ctrl-v-paste,omitempty"`
|
||||||
|
EastAsianAmbiguousAsTwoColumn bool `hcl:"east_asian_ambiguous_as_two_column" json:"east-asian-ambiguous-as-two-column,omitempty"`
|
||||||
|
Enable8BitControl *bool `hcl:"enable_8_bit_control" json:"enable-8-bit-control,omitempty"`
|
||||||
|
EnableBold *bool `hcl:"enable_bold" json:"enable-bold,omitempty"`
|
||||||
|
EnableBoldAsBright bool `hcl:"enable_bold_as_bright" json:"enable-bold-as-bright,omitempty"`
|
||||||
|
EnableClipboardNotice bool `hcl:"enable_clipboard_notice" json:"enable-clipboard-notice,omitempty"`
|
||||||
|
EnableClipboardWrite bool `hcl:"enable_clipboard_write" json:"enable-clipboard-write,omitempty"`
|
||||||
|
EnableDec12 bool `hcl:"enable_dec12" json:"enable-dec12,omitempty"`
|
||||||
|
Environment map[string]string `hcl:"environment" json:"environment,omitempty"`
|
||||||
|
FontFamily string `hcl:"font_family" json:"font-family,omitempty"`
|
||||||
|
FontSize int `hcl:"font_size" json:"font-size,omitempty"`
|
||||||
|
FontSmoothing string `hcl:"font_smoothing" json:"font-smoothing,omitempty"`
|
||||||
|
ForegroundColor string `hcl:"foreground_color" json:"foreground-color,omitempty"`
|
||||||
|
HomeKeysScroll bool `hcl:"home_keys_scroll" json:"home-keys-scroll,omitempty"`
|
||||||
|
Keybindings map[string]string `hcl:"keybindings" json:"keybindings,omitempty"`
|
||||||
|
MaxStringSequence int `hcl:"max_string_sequence" json:"max-string-sequence,omitempty"`
|
||||||
|
MediaKeysAreFkeys bool `hcl:"media_keys_are_fkeys" json:"media-keys-are-fkeys,omitempty"`
|
||||||
|
MetaSendsEscape bool `hcl:"meta_sends_escape" json:"meta-sends-escape,omitempty"`
|
||||||
|
MousePasteButton *int `hcl:"mouse_paste_button" json:"mouse-paste-button,omitempty"`
|
||||||
|
PageKeysScroll bool `hcl:"page_keys_scroll" json:"page-keys-scroll,omitempty"`
|
||||||
|
PassAltNumber *bool `hcl:"pass_alt_number" json:"pass-alt-number,omitempty"`
|
||||||
|
PassCtrlNumber *bool `hcl:"pass_ctrl_number" json:"pass-ctrl-number,omitempty"`
|
||||||
|
PassMetaNumber *bool `hcl:"pass_meta_number" json:"pass-meta-number,omitempty"`
|
||||||
|
PassMetaV bool `hcl:"pass_meta_v" json:"pass-meta-v,omitempty"`
|
||||||
|
ReceiveEncoding string `hcl:"receive_encoding" json:"receive-encoding,omitempty"`
|
||||||
|
ScrollOnKeystroke bool `hcl:"scroll_on_keystroke" json:"scroll-on-keystroke,omitempty"`
|
||||||
|
ScrollOnOutput bool `hcl:"scroll_on_output" json:"scroll-on-output,omitempty"`
|
||||||
|
ScrollbarVisible bool `hcl:"scrollbar_visible" json:"scrollbar-visible,omitempty"`
|
||||||
|
ScrollWheelMoveMultiplier int `hcl:"scroll_wheel_move_multiplier" json:"scroll-wheel-move-multiplier,omitempty"`
|
||||||
|
SendEncoding string `hcl:"send_encoding" json:"send-encoding,omitempty"`
|
||||||
|
ShiftInsertPaste bool `hcl:"shift_insert_paste" json:"shift-insert-paste,omitempty"`
|
||||||
|
UserCss string `hcl:"user_css" json:"user-css,omitempty"`
|
||||||
|
}
|
21
server/run_option.go
Normal file
21
server/run_option.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RunOptions holds a set of configurations for Server.Run().
|
||||||
|
type RunOptions struct {
|
||||||
|
gracefullCtx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunOption is an option of Server.Run().
|
||||||
|
type RunOption func(*RunOptions)
|
||||||
|
|
||||||
|
// WithGracefullContext accepts a context to shutdown a Server
|
||||||
|
// with care for existing client connections.
|
||||||
|
func WithGracefullContext(ctx context.Context) RunOption {
|
||||||
|
return func(options *RunOptions) {
|
||||||
|
options.gracefullCtx = ctx
|
||||||
|
}
|
||||||
|
}
|
253
server/server.go
Normal file
253
server/server.go
Normal file
@ -0,0 +1,253 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
|
"html/template"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
noesctmpl "text/template"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/elazarl/go-bindata-assetfs"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"github.com/yudai/gotty/pkg/homedir"
|
||||||
|
"github.com/yudai/gotty/pkg/randomstring"
|
||||||
|
"github.com/yudai/gotty/webtty"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Server provides a webtty HTTP endpoint.
|
||||||
|
type Server struct {
|
||||||
|
factory Factory
|
||||||
|
options *Options
|
||||||
|
|
||||||
|
srv *http.Server
|
||||||
|
|
||||||
|
upgrader *websocket.Upgrader
|
||||||
|
|
||||||
|
indexTemplate *template.Template
|
||||||
|
titleTemplate *noesctmpl.Template
|
||||||
|
titleVars map[string]interface{}
|
||||||
|
timer *time.Timer
|
||||||
|
wsWG sync.WaitGroup
|
||||||
|
url *url.URL // use URL()
|
||||||
|
connections *int64 // Use atomic operations
|
||||||
|
once *int64 // use atomic operations
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new instance of Server.
|
||||||
|
// Server will use the New() of the factory provided to handle each request.
|
||||||
|
func New(factory Factory, options *Options) (*Server, error) {
|
||||||
|
indexData, err := Asset("static/index.html")
|
||||||
|
if err != nil {
|
||||||
|
panic("index not found") // must be in bindata
|
||||||
|
}
|
||||||
|
if options.IndexFile != "" {
|
||||||
|
log.Printf("Using index file at " + options.IndexFile)
|
||||||
|
path := homedir.Expand(options.IndexFile)
|
||||||
|
indexData, err = ioutil.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "failed to read custom index file at `%s`", path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
indexTemplate, err := template.New("index").Parse(string(indexData))
|
||||||
|
if err != nil {
|
||||||
|
panic("index template parse failed") // must be valid
|
||||||
|
}
|
||||||
|
|
||||||
|
titleTemplate, err := noesctmpl.New("title").Parse(options.TitleFormat)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "failed to parse window title format `%s`", options.TitleFormat)
|
||||||
|
}
|
||||||
|
|
||||||
|
connections := int64(0)
|
||||||
|
once := int64(0)
|
||||||
|
|
||||||
|
return &Server{
|
||||||
|
factory: factory,
|
||||||
|
options: options,
|
||||||
|
|
||||||
|
upgrader: &websocket.Upgrader{
|
||||||
|
ReadBufferSize: 1024,
|
||||||
|
WriteBufferSize: 1024,
|
||||||
|
Subprotocols: webtty.Protocols,
|
||||||
|
},
|
||||||
|
indexTemplate: indexTemplate,
|
||||||
|
titleTemplate: titleTemplate,
|
||||||
|
connections: &connections,
|
||||||
|
once: &once,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run starts the main process of the Server.
|
||||||
|
// The cancelation of ctx will shutdown the server immediately with aborting
|
||||||
|
// existing connections. Use WithGracefullContext() to support gracefull shutdown.
|
||||||
|
func (server *Server) Run(ctx context.Context, options ...RunOption) error {
|
||||||
|
cctx, cancel := context.WithCancel(ctx)
|
||||||
|
opts := &RunOptions{gracefullCtx: context.Background()}
|
||||||
|
for _, opt := range options {
|
||||||
|
opt(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
handlers := server.setupHandlers(cctx, cancel)
|
||||||
|
srv, err := server.setupHTTPServer(handlers)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to setup an HTTP server")
|
||||||
|
}
|
||||||
|
|
||||||
|
if server.options.PermitWrite {
|
||||||
|
log.Printf("Permitting clients to write input to the PTY.")
|
||||||
|
}
|
||||||
|
|
||||||
|
if server.options.Once {
|
||||||
|
log.Printf("Once option is provided, accepting only one client")
|
||||||
|
}
|
||||||
|
|
||||||
|
server.srv = srv
|
||||||
|
|
||||||
|
if server.options.Timeout > 0 {
|
||||||
|
server.timer = time.NewTimer(time.Duration(server.options.Timeout) * time.Second)
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-server.timer.C:
|
||||||
|
cancel()
|
||||||
|
case <-cctx.Done():
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
listenErr := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
if server.options.EnableTLS {
|
||||||
|
crtFile := homedir.Expand(server.options.TLSCrtFile)
|
||||||
|
keyFile := homedir.Expand(server.options.TLSKeyFile)
|
||||||
|
log.Printf("TLS crt file: " + crtFile)
|
||||||
|
log.Printf("TLS key file: " + keyFile)
|
||||||
|
|
||||||
|
err = srv.ListenAndServeTLS(crtFile, keyFile)
|
||||||
|
} else {
|
||||||
|
err = srv.ListenAndServe()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
listenErr <- err
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-opts.gracefullCtx.Done():
|
||||||
|
srv.Shutdown(context.Background())
|
||||||
|
case <-cctx.Done():
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err = <-listenErr:
|
||||||
|
if err == http.ErrServerClosed { // by gracefull ctx
|
||||||
|
err = nil
|
||||||
|
} else {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
case <-cctx.Done():
|
||||||
|
srv.Close()
|
||||||
|
err = cctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
conn := atomic.LoadInt64(server.connections)
|
||||||
|
if conn > 0 {
|
||||||
|
log.Printf("Waiting for %d connections to be closed", conn)
|
||||||
|
}
|
||||||
|
server.wsWG.Wait()
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *Server) setupHandlers(ctx context.Context, cancel context.CancelFunc) http.Handler {
|
||||||
|
staticFileHandler := http.FileServer(
|
||||||
|
&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "static"},
|
||||||
|
)
|
||||||
|
|
||||||
|
url := server.URL()
|
||||||
|
var siteMux = http.NewServeMux()
|
||||||
|
siteMux.HandleFunc(url.Path, server.handleIndex)
|
||||||
|
siteMux.Handle(url.Path+"js/", http.StripPrefix(url.Path, staticFileHandler))
|
||||||
|
siteMux.Handle(url.Path+"favicon.png", http.StripPrefix(url.Path, staticFileHandler))
|
||||||
|
siteMux.HandleFunc(url.Path+"auth_token.js", server.handleAuthToken)
|
||||||
|
|
||||||
|
siteHandler := http.Handler(siteMux)
|
||||||
|
|
||||||
|
if server.options.EnableBasicAuth {
|
||||||
|
log.Printf("Using Basic Authentication")
|
||||||
|
siteHandler = server.wrapBasicAuth(siteHandler, server.options.Credential)
|
||||||
|
}
|
||||||
|
|
||||||
|
siteHandler = server.wrapHeaders(siteHandler)
|
||||||
|
|
||||||
|
wsMux := http.NewServeMux()
|
||||||
|
wsMux.Handle("/", siteHandler)
|
||||||
|
wsMux.HandleFunc(url.Path+"ws", server.generateHandleWS(ctx, cancel))
|
||||||
|
siteHandler = http.Handler(wsMux)
|
||||||
|
|
||||||
|
return server.wrapLogger(siteHandler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *Server) setupHTTPServer(handler http.Handler) (*http.Server, error) {
|
||||||
|
url := server.URL()
|
||||||
|
log.Printf("URL: %s", url.String())
|
||||||
|
|
||||||
|
srv := &http.Server{
|
||||||
|
Addr: url.Host,
|
||||||
|
Handler: handler,
|
||||||
|
}
|
||||||
|
|
||||||
|
if server.options.EnableTLSClientAuth {
|
||||||
|
tlsConfig, err := server.tlsConfig()
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "failed to setup TLS configuration")
|
||||||
|
}
|
||||||
|
srv.TLSConfig = tlsConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
return srv, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *Server) URL() *url.URL {
|
||||||
|
if server.url == nil {
|
||||||
|
host := net.JoinHostPort(server.options.Address, server.options.Port)
|
||||||
|
path := ""
|
||||||
|
if server.options.EnableRandomUrl {
|
||||||
|
path += "/" + randomstring.Generate(server.options.RandomUrlLength)
|
||||||
|
}
|
||||||
|
scheme := "http"
|
||||||
|
if server.options.EnableTLS {
|
||||||
|
scheme = "https"
|
||||||
|
}
|
||||||
|
server.url = &url.URL{Scheme: scheme, Host: host, Path: path + "/"}
|
||||||
|
}
|
||||||
|
return server.url
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *Server) tlsConfig() (*tls.Config, error) {
|
||||||
|
caFile := homedir.Expand(server.options.TLSCACrtFile)
|
||||||
|
caCert, err := ioutil.ReadFile(caFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("could not open CA crt file " + caFile)
|
||||||
|
}
|
||||||
|
caCertPool := x509.NewCertPool()
|
||||||
|
if !caCertPool.AppendCertsFromPEM(caCert) {
|
||||||
|
return nil, errors.New("could not parse CA crt file data in " + caFile)
|
||||||
|
}
|
||||||
|
tlsConfig := &tls.Config{
|
||||||
|
ClientCAs: caCertPool,
|
||||||
|
ClientAuth: tls.RequireAndVerifyClientCert,
|
||||||
|
}
|
||||||
|
return tlsConfig, nil
|
||||||
|
}
|
17
server/slave.go
Normal file
17
server/slave.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/yudai/gotty/webtty"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Slave is webtty.Slave with some additional methods.
|
||||||
|
type Slave interface {
|
||||||
|
webtty.Slave
|
||||||
|
|
||||||
|
GetTerminalSize() (width int, height int, err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Factory interface {
|
||||||
|
Name() string
|
||||||
|
New(params map[string][]string) (Slave, error)
|
||||||
|
}
|
17
server/timer.go
Normal file
17
server/timer.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (server *Server) stopTimer() {
|
||||||
|
if server.options.Timeout > 0 {
|
||||||
|
server.timer.Stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *Server) resetTimer() {
|
||||||
|
if server.options.Timeout > 0 {
|
||||||
|
server.timer.Reset(time.Duration(server.options.Timeout) * time.Second)
|
||||||
|
}
|
||||||
|
}
|
41
utils/default.go
Normal file
41
utils/default.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/fatih/structs"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ApplyDefaultValues(struct_ interface{}) (err error) {
|
||||||
|
o := structs.New(struct_)
|
||||||
|
|
||||||
|
for _, field := range o.Fields() {
|
||||||
|
defaultValue := field.Tag("default")
|
||||||
|
if defaultValue == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var val interface{}
|
||||||
|
switch field.Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
val = defaultValue
|
||||||
|
case reflect.Bool:
|
||||||
|
if defaultValue == "true" {
|
||||||
|
val = true
|
||||||
|
} else if defaultValue == "false" {
|
||||||
|
val = false
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("invalid bool expression: %v, use true/false", defaultValue)
|
||||||
|
}
|
||||||
|
case reflect.Int:
|
||||||
|
val, err = strconv.Atoi(defaultValue)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
val = field.Value()
|
||||||
|
}
|
||||||
|
field.Set(val)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
124
utils/flags.go
Normal file
124
utils/flags.go
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/codegangsta/cli"
|
||||||
|
"github.com/fatih/structs"
|
||||||
|
"github.com/yudai/hcl"
|
||||||
|
|
||||||
|
"github.com/yudai/gotty/pkg/homedir"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenerateFlags(options ...interface{}) (flags []cli.Flag, mappings map[string]string, err error) {
|
||||||
|
mappings = make(map[string]string)
|
||||||
|
|
||||||
|
for _, struct_ := range options {
|
||||||
|
o := structs.New(struct_)
|
||||||
|
for _, field := range o.Fields() {
|
||||||
|
flagName := field.Tag("flagName")
|
||||||
|
if flagName == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
envName := "GOTTY_" + strings.ToUpper(strings.Join(strings.Split(flagName, "-"), "_"))
|
||||||
|
mappings[flagName] = field.Name()
|
||||||
|
|
||||||
|
flagShortName := field.Tag("flagSName")
|
||||||
|
if flagShortName != "" {
|
||||||
|
flagName += ", " + flagShortName
|
||||||
|
}
|
||||||
|
|
||||||
|
flagDescription := field.Tag("flagDescribe")
|
||||||
|
|
||||||
|
switch field.Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
flags = append(flags, cli.StringFlag{
|
||||||
|
Name: flagName,
|
||||||
|
Value: field.Value().(string),
|
||||||
|
Usage: flagDescription,
|
||||||
|
EnvVar: envName,
|
||||||
|
})
|
||||||
|
case reflect.Bool:
|
||||||
|
flags = append(flags, cli.BoolFlag{
|
||||||
|
Name: flagName,
|
||||||
|
Usage: flagDescription,
|
||||||
|
EnvVar: envName,
|
||||||
|
})
|
||||||
|
case reflect.Int:
|
||||||
|
flags = append(flags, cli.IntFlag{
|
||||||
|
Name: flagName,
|
||||||
|
Value: field.Value().(int),
|
||||||
|
Usage: flagDescription,
|
||||||
|
EnvVar: envName,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ApplyFlags(
|
||||||
|
flags []cli.Flag,
|
||||||
|
mappingHint map[string]string,
|
||||||
|
c *cli.Context,
|
||||||
|
options ...interface{},
|
||||||
|
) {
|
||||||
|
objects := make([]*structs.Struct, len(options))
|
||||||
|
for i, struct_ := range options {
|
||||||
|
objects[i] = structs.New(struct_)
|
||||||
|
}
|
||||||
|
|
||||||
|
for flagName, fieldName := range mappingHint {
|
||||||
|
if !c.IsSet(flagName) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var field *structs.Field
|
||||||
|
var ok bool
|
||||||
|
for _, o := range objects {
|
||||||
|
field, ok = o.FieldOk(fieldName)
|
||||||
|
if ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if field == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var val interface{}
|
||||||
|
switch field.Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
val = c.String(flagName)
|
||||||
|
case reflect.Bool:
|
||||||
|
val = c.Bool(flagName)
|
||||||
|
case reflect.Int:
|
||||||
|
val = c.Int(flagName)
|
||||||
|
}
|
||||||
|
field.Set(val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ApplyConfigFile(filePath string, options ...interface{}) error {
|
||||||
|
filePath = homedir.Expand(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
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, object := range options {
|
||||||
|
if err := hcl.Decode(object, string(fileString)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
19
vendor/github.com/braintree/manners/LICENSE
generated
vendored
19
vendor/github.com/braintree/manners/LICENSE
generated
vendored
@ -1,19 +0,0 @@
|
|||||||
Copyright (c) 2014 Braintree, a division of PayPal, Inc.
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
36
vendor/github.com/braintree/manners/README.md
generated
vendored
36
vendor/github.com/braintree/manners/README.md
generated
vendored
@ -1,36 +0,0 @@
|
|||||||
# Manners
|
|
||||||
|
|
||||||
A *polite* webserver for Go.
|
|
||||||
|
|
||||||
Manners allows you to shut your Go webserver down gracefully, without dropping any requests. It can act as a drop-in replacement for the standard library's http.ListenAndServe function:
|
|
||||||
|
|
||||||
```go
|
|
||||||
func main() {
|
|
||||||
handler := MyHTTPHandler()
|
|
||||||
manners.ListenAndServe(":7000", handler)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Then, when you want to shut the server down:
|
|
||||||
|
|
||||||
```go
|
|
||||||
manners.Close()
|
|
||||||
```
|
|
||||||
|
|
||||||
(Note that this does not block until all the requests are finished. Rather, the call to manners.ListenAndServe will stop blocking when all the requests are finished.)
|
|
||||||
|
|
||||||
Manners ensures that all requests are served by incrementing a WaitGroup when a request comes in and decrementing it when the request finishes.
|
|
||||||
|
|
||||||
If your request handler spawns Goroutines that are not guaranteed to finish with the request, you can ensure they are also completed with the `StartRoutine` and `FinishRoutine` functions on the server.
|
|
||||||
|
|
||||||
### Known Issues
|
|
||||||
|
|
||||||
Manners does not correctly shut down long-lived keepalive connections when issued a shutdown command. Clients on an idle keepalive connection may see a connection reset error rather than a close. See https://github.com/braintree/manners/issues/13 for details.
|
|
||||||
|
|
||||||
### Compatability
|
|
||||||
|
|
||||||
Manners 0.3.0 and above uses standard library functionality introduced in Go 1.3.
|
|
||||||
|
|
||||||
### Installation
|
|
||||||
|
|
||||||
`go get github.com/braintree/manners`
|
|
7
vendor/github.com/braintree/manners/interfaces.go
generated
vendored
7
vendor/github.com/braintree/manners/interfaces.go
generated
vendored
@ -1,7 +0,0 @@
|
|||||||
package manners
|
|
||||||
|
|
||||||
type waitGroup interface {
|
|
||||||
Add(int)
|
|
||||||
Done()
|
|
||||||
Wait()
|
|
||||||
}
|
|
228
vendor/github.com/braintree/manners/server.go
generated
vendored
228
vendor/github.com/braintree/manners/server.go
generated
vendored
@ -1,228 +0,0 @@
|
|||||||
/*
|
|
||||||
Package manners provides a wrapper for a standard net/http server that
|
|
||||||
ensures all active HTTP client have completed their current request
|
|
||||||
before the server shuts down.
|
|
||||||
|
|
||||||
It can be used a drop-in replacement for the standard http package,
|
|
||||||
or can wrap a pre-configured Server.
|
|
||||||
|
|
||||||
eg.
|
|
||||||
|
|
||||||
http.Handle("/hello", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Write([]byte("Hello\n"))
|
|
||||||
})
|
|
||||||
|
|
||||||
log.Fatal(manners.ListenAndServe(":8080", nil))
|
|
||||||
|
|
||||||
or for a customized server:
|
|
||||||
|
|
||||||
s := manners.NewWithServer(&http.Server{
|
|
||||||
Addr: ":8080",
|
|
||||||
Handler: myHandler,
|
|
||||||
ReadTimeout: 10 * time.Second,
|
|
||||||
WriteTimeout: 10 * time.Second,
|
|
||||||
MaxHeaderBytes: 1 << 20,
|
|
||||||
})
|
|
||||||
log.Fatal(s.ListenAndServe())
|
|
||||||
|
|
||||||
The server will shut down cleanly when the Close() method is called:
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
sigchan := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(sigchan, os.Interrupt, os.Kill)
|
|
||||||
<-sigchan
|
|
||||||
log.Info("Shutting down...")
|
|
||||||
manners.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
http.Handle("/hello", myHandler)
|
|
||||||
log.Fatal(manners.ListenAndServe(":8080", nil))
|
|
||||||
*/
|
|
||||||
package manners
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/tls"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A GracefulServer maintains a WaitGroup that counts how many in-flight
|
|
||||||
// requests the server is handling. When it receives a shutdown signal,
|
|
||||||
// it stops accepting new requests but does not actually shut down until
|
|
||||||
// all in-flight requests terminate.
|
|
||||||
//
|
|
||||||
// GracefulServer embeds the underlying net/http.Server making its non-override
|
|
||||||
// methods and properties avaiable.
|
|
||||||
//
|
|
||||||
// It must be initialized by calling NewWithServer.
|
|
||||||
type GracefulServer struct {
|
|
||||||
*http.Server
|
|
||||||
|
|
||||||
shutdown chan bool
|
|
||||||
wg waitGroup
|
|
||||||
|
|
||||||
lcsmu sync.RWMutex
|
|
||||||
lastConnState map[net.Conn]http.ConnState
|
|
||||||
|
|
||||||
up chan net.Listener // Only used by test code.
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewWithServer wraps an existing http.Server object and returns a
|
|
||||||
// GracefulServer that supports all of the original Server operations.
|
|
||||||
func NewWithServer(s *http.Server) *GracefulServer {
|
|
||||||
return &GracefulServer{
|
|
||||||
Server: s,
|
|
||||||
shutdown: make(chan bool),
|
|
||||||
wg: new(sync.WaitGroup),
|
|
||||||
lastConnState: make(map[net.Conn]http.ConnState),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close stops the server from accepting new requets and begins shutting down.
|
|
||||||
// It returns true if it's the first time Close is called.
|
|
||||||
func (s *GracefulServer) Close() bool {
|
|
||||||
return <-s.shutdown
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenAndServe provides a graceful equivalent of net/http.Serve.ListenAndServe.
|
|
||||||
func (s *GracefulServer) ListenAndServe() error {
|
|
||||||
addr := s.Addr
|
|
||||||
if addr == "" {
|
|
||||||
addr = ":http"
|
|
||||||
}
|
|
||||||
listener, err := net.Listen("tcp", addr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.Serve(listener)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenAndServeTLS provides a graceful equivalent of net/http.Serve.ListenAndServeTLS.
|
|
||||||
func (s *GracefulServer) ListenAndServeTLS(certFile, keyFile string) error {
|
|
||||||
// direct lift from net/http/server.go
|
|
||||||
addr := s.Addr
|
|
||||||
if addr == "" {
|
|
||||||
addr = ":https"
|
|
||||||
}
|
|
||||||
config := &tls.Config{}
|
|
||||||
if s.TLSConfig != nil {
|
|
||||||
*config = *s.TLSConfig
|
|
||||||
}
|
|
||||||
if config.NextProtos == nil {
|
|
||||||
config.NextProtos = []string{"http/1.1"}
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
config.Certificates = make([]tls.Certificate, 1)
|
|
||||||
config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
ln, err := net.Listen("tcp", addr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.Serve(tls.NewListener(ln, config))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serve provides a graceful equivalent net/http.Server.Serve.
|
|
||||||
func (s *GracefulServer) Serve(listener net.Listener) error {
|
|
||||||
var closing int32
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
s.shutdown <- true
|
|
||||||
close(s.shutdown)
|
|
||||||
atomic.StoreInt32(&closing, 1)
|
|
||||||
s.Server.SetKeepAlivesEnabled(false)
|
|
||||||
listener.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
originalConnState := s.Server.ConnState
|
|
||||||
|
|
||||||
// s.ConnState is invoked by the net/http.Server every time a connectiion
|
|
||||||
// changes state. It keeps track of each connection's state over time,
|
|
||||||
// enabling manners to handle persisted connections correctly.
|
|
||||||
s.ConnState = func(conn net.Conn, newState http.ConnState) {
|
|
||||||
s.lcsmu.RLock()
|
|
||||||
lastConnState := s.lastConnState[conn]
|
|
||||||
s.lcsmu.RUnlock()
|
|
||||||
|
|
||||||
switch newState {
|
|
||||||
|
|
||||||
// New connection -> StateNew
|
|
||||||
case http.StateNew:
|
|
||||||
s.StartRoutine()
|
|
||||||
|
|
||||||
// (StateNew, StateIdle) -> StateActive
|
|
||||||
case http.StateActive:
|
|
||||||
// The connection transitioned from idle back to active
|
|
||||||
if lastConnState == http.StateIdle {
|
|
||||||
s.StartRoutine()
|
|
||||||
}
|
|
||||||
|
|
||||||
// StateActive -> StateIdle
|
|
||||||
// Immediately close newly idle connections; if not they may make
|
|
||||||
// one more request before SetKeepAliveEnabled(false) takes effect.
|
|
||||||
case http.StateIdle:
|
|
||||||
if atomic.LoadInt32(&closing) == 1 {
|
|
||||||
conn.Close()
|
|
||||||
}
|
|
||||||
s.FinishRoutine()
|
|
||||||
|
|
||||||
// (StateNew, StateActive, StateIdle) -> (StateClosed, StateHiJacked)
|
|
||||||
// If the connection was idle we do not need to decrement the counter.
|
|
||||||
case http.StateClosed, http.StateHijacked:
|
|
||||||
if lastConnState != http.StateIdle {
|
|
||||||
s.FinishRoutine()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
s.lcsmu.Lock()
|
|
||||||
if newState == http.StateClosed || newState == http.StateHijacked {
|
|
||||||
delete(s.lastConnState, conn)
|
|
||||||
} else {
|
|
||||||
s.lastConnState[conn] = newState
|
|
||||||
}
|
|
||||||
s.lcsmu.Unlock()
|
|
||||||
|
|
||||||
if originalConnState != nil {
|
|
||||||
originalConnState(conn, newState)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A hook to allow the server to notify others when it is ready to receive
|
|
||||||
// requests; only used by tests.
|
|
||||||
if s.up != nil {
|
|
||||||
s.up <- listener
|
|
||||||
}
|
|
||||||
|
|
||||||
err := s.Server.Serve(listener)
|
|
||||||
|
|
||||||
// This block is reached when the server has received a shut down command
|
|
||||||
// or a real error happened.
|
|
||||||
if err == nil || atomic.LoadInt32(&closing) == 1 {
|
|
||||||
s.wg.Wait()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// StartRoutine increments the server's WaitGroup. Use this if a web request
|
|
||||||
// starts more goroutines and these goroutines are not guaranteed to finish
|
|
||||||
// before the request.
|
|
||||||
func (s *GracefulServer) StartRoutine() {
|
|
||||||
s.wg.Add(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FinishRoutine decrements the server's WaitGroup. Use this to complement
|
|
||||||
// StartRoutine().
|
|
||||||
func (s *GracefulServer) FinishRoutine() {
|
|
||||||
s.wg.Done()
|
|
||||||
}
|
|
35
vendor/github.com/braintree/manners/static.go
generated
vendored
35
vendor/github.com/braintree/manners/static.go
generated
vendored
@ -1,35 +0,0 @@
|
|||||||
package manners
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
var defaultServer *GracefulServer
|
|
||||||
|
|
||||||
// ListenAndServe provides a graceful version of the function provided by the
|
|
||||||
// net/http package. Call Close() to stop the server.
|
|
||||||
func ListenAndServe(addr string, handler http.Handler) error {
|
|
||||||
defaultServer = NewWithServer(&http.Server{Addr: addr, Handler: handler})
|
|
||||||
return defaultServer.ListenAndServe()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenAndServeTLS provides a graceful version of the function provided by the
|
|
||||||
// net/http package. Call Close() to stop the server.
|
|
||||||
func ListenAndServeTLS(addr string, certFile string, keyFile string, handler http.Handler) error {
|
|
||||||
defaultServer = NewWithServer(&http.Server{Addr: addr, Handler: handler})
|
|
||||||
return defaultServer.ListenAndServeTLS(certFile, keyFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serve provides a graceful version of the function provided by the net/http
|
|
||||||
// package. Call Close() to stop the server.
|
|
||||||
func Serve(l net.Listener, handler http.Handler) error {
|
|
||||||
defaultServer = NewWithServer(&http.Server{Handler: handler})
|
|
||||||
return defaultServer.Serve(l)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Shuts down the default server used by ListenAndServe, ListenAndServeTLS and
|
|
||||||
// Serve. It returns true if it's the first time Close is called.
|
|
||||||
func Close() bool {
|
|
||||||
return defaultServer.Close()
|
|
||||||
}
|
|
24
vendor/github.com/pkg/errors/.gitignore
generated
vendored
Normal file
24
vendor/github.com/pkg/errors/.gitignore
generated
vendored
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||||
|
*.o
|
||||||
|
*.a
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Folders
|
||||||
|
_obj
|
||||||
|
_test
|
||||||
|
|
||||||
|
# Architecture specific extensions/prefixes
|
||||||
|
*.[568vq]
|
||||||
|
[568vq].out
|
||||||
|
|
||||||
|
*.cgo1.go
|
||||||
|
*.cgo2.c
|
||||||
|
_cgo_defun.c
|
||||||
|
_cgo_gotypes.go
|
||||||
|
_cgo_export.*
|
||||||
|
|
||||||
|
_testmain.go
|
||||||
|
|
||||||
|
*.exe
|
||||||
|
*.test
|
||||||
|
*.prof
|
11
vendor/github.com/pkg/errors/.travis.yml
generated
vendored
Normal file
11
vendor/github.com/pkg/errors/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
language: go
|
||||||
|
go_import_path: github.com/pkg/errors
|
||||||
|
go:
|
||||||
|
- 1.4.3
|
||||||
|
- 1.5.4
|
||||||
|
- 1.6.3
|
||||||
|
- 1.7.3
|
||||||
|
- tip
|
||||||
|
|
||||||
|
script:
|
||||||
|
- go test -v ./...
|
23
vendor/github.com/pkg/errors/LICENSE
generated
vendored
Normal file
23
vendor/github.com/pkg/errors/LICENSE
generated
vendored
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
Copyright (c) 2015, Dave Cheney <dave@cheney.net>
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
52
vendor/github.com/pkg/errors/README.md
generated
vendored
Normal file
52
vendor/github.com/pkg/errors/README.md
generated
vendored
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
# errors [![Travis-CI](https://travis-ci.org/pkg/errors.svg)](https://travis-ci.org/pkg/errors) [![AppVeyor](https://ci.appveyor.com/api/projects/status/b98mptawhudj53ep/branch/master?svg=true)](https://ci.appveyor.com/project/davecheney/errors/branch/master) [![GoDoc](https://godoc.org/github.com/pkg/errors?status.svg)](http://godoc.org/github.com/pkg/errors) [![Report card](https://goreportcard.com/badge/github.com/pkg/errors)](https://goreportcard.com/report/github.com/pkg/errors)
|
||||||
|
|
||||||
|
Package errors provides simple error handling primitives.
|
||||||
|
|
||||||
|
`go get github.com/pkg/errors`
|
||||||
|
|
||||||
|
The traditional error handling idiom in Go is roughly akin to
|
||||||
|
```go
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
```
|
||||||
|
which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error.
|
||||||
|
|
||||||
|
## Adding context to an error
|
||||||
|
|
||||||
|
The errors.Wrap function returns a new error that adds context to the original error. For example
|
||||||
|
```go
|
||||||
|
_, err := ioutil.ReadAll(r)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "read failed")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
## Retrieving the cause of an error
|
||||||
|
|
||||||
|
Using `errors.Wrap` constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by `errors.Cause`.
|
||||||
|
```go
|
||||||
|
type causer interface {
|
||||||
|
Cause() error
|
||||||
|
}
|
||||||
|
```
|
||||||
|
`errors.Cause` will recursively retrieve the topmost error which does not implement `causer`, which is assumed to be the original cause. For example:
|
||||||
|
```go
|
||||||
|
switch err := errors.Cause(err).(type) {
|
||||||
|
case *MyError:
|
||||||
|
// handle specifically
|
||||||
|
default:
|
||||||
|
// unknown error
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
[Read the package documentation for more information](https://godoc.org/github.com/pkg/errors).
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
We welcome pull requests, bug fixes and issue reports. With that said, the bar for adding new symbols to this package is intentionally set high.
|
||||||
|
|
||||||
|
Before proposing a change, please discuss your change by raising an issue.
|
||||||
|
|
||||||
|
## Licence
|
||||||
|
|
||||||
|
BSD-2-Clause
|
32
vendor/github.com/pkg/errors/appveyor.yml
generated
vendored
Normal file
32
vendor/github.com/pkg/errors/appveyor.yml
generated
vendored
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
version: build-{build}.{branch}
|
||||||
|
|
||||||
|
clone_folder: C:\gopath\src\github.com\pkg\errors
|
||||||
|
shallow_clone: true # for startup speed
|
||||||
|
|
||||||
|
environment:
|
||||||
|
GOPATH: C:\gopath
|
||||||
|
|
||||||
|
platform:
|
||||||
|
- x64
|
||||||
|
|
||||||
|
# http://www.appveyor.com/docs/installed-software
|
||||||
|
install:
|
||||||
|
# some helpful output for debugging builds
|
||||||
|
- go version
|
||||||
|
- go env
|
||||||
|
# pre-installed MinGW at C:\MinGW is 32bit only
|
||||||
|
# but MSYS2 at C:\msys64 has mingw64
|
||||||
|
- set PATH=C:\msys64\mingw64\bin;%PATH%
|
||||||
|
- gcc --version
|
||||||
|
- g++ --version
|
||||||
|
|
||||||
|
build_script:
|
||||||
|
- go install -v ./...
|
||||||
|
|
||||||
|
test_script:
|
||||||
|
- set PATH=C:\gopath\bin;%PATH%
|
||||||
|
- go test -v ./...
|
||||||
|
|
||||||
|
#artifacts:
|
||||||
|
# - path: '%GOPATH%\bin\*.exe'
|
||||||
|
deploy: off
|
269
vendor/github.com/pkg/errors/errors.go
generated
vendored
Normal file
269
vendor/github.com/pkg/errors/errors.go
generated
vendored
Normal file
@ -0,0 +1,269 @@
|
|||||||
|
// Package errors provides simple error handling primitives.
|
||||||
|
//
|
||||||
|
// The traditional error handling idiom in Go is roughly akin to
|
||||||
|
//
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// which applied recursively up the call stack results in error reports
|
||||||
|
// without context or debugging information. The errors package allows
|
||||||
|
// programmers to add context to the failure path in their code in a way
|
||||||
|
// that does not destroy the original value of the error.
|
||||||
|
//
|
||||||
|
// Adding context to an error
|
||||||
|
//
|
||||||
|
// The errors.Wrap function returns a new error that adds context to the
|
||||||
|
// original error by recording a stack trace at the point Wrap is called,
|
||||||
|
// and the supplied message. For example
|
||||||
|
//
|
||||||
|
// _, err := ioutil.ReadAll(r)
|
||||||
|
// if err != nil {
|
||||||
|
// return errors.Wrap(err, "read failed")
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// If additional control is required the errors.WithStack and errors.WithMessage
|
||||||
|
// functions destructure errors.Wrap into its component operations of annotating
|
||||||
|
// an error with a stack trace and an a message, respectively.
|
||||||
|
//
|
||||||
|
// Retrieving the cause of an error
|
||||||
|
//
|
||||||
|
// Using errors.Wrap constructs a stack of errors, adding context to the
|
||||||
|
// preceding error. Depending on the nature of the error it may be necessary
|
||||||
|
// to reverse the operation of errors.Wrap to retrieve the original error
|
||||||
|
// for inspection. Any error value which implements this interface
|
||||||
|
//
|
||||||
|
// type causer interface {
|
||||||
|
// Cause() error
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// can be inspected by errors.Cause. errors.Cause will recursively retrieve
|
||||||
|
// the topmost error which does not implement causer, which is assumed to be
|
||||||
|
// the original cause. For example:
|
||||||
|
//
|
||||||
|
// switch err := errors.Cause(err).(type) {
|
||||||
|
// case *MyError:
|
||||||
|
// // handle specifically
|
||||||
|
// default:
|
||||||
|
// // unknown error
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// causer interface is not exported by this package, but is considered a part
|
||||||
|
// of stable public API.
|
||||||
|
//
|
||||||
|
// Formatted printing of errors
|
||||||
|
//
|
||||||
|
// All error values returned from this package implement fmt.Formatter and can
|
||||||
|
// be formatted by the fmt package. The following verbs are supported
|
||||||
|
//
|
||||||
|
// %s print the error. If the error has a Cause it will be
|
||||||
|
// printed recursively
|
||||||
|
// %v see %s
|
||||||
|
// %+v extended format. Each Frame of the error's StackTrace will
|
||||||
|
// be printed in detail.
|
||||||
|
//
|
||||||
|
// Retrieving the stack trace of an error or wrapper
|
||||||
|
//
|
||||||
|
// New, Errorf, Wrap, and Wrapf record a stack trace at the point they are
|
||||||
|
// invoked. This information can be retrieved with the following interface.
|
||||||
|
//
|
||||||
|
// type stackTracer interface {
|
||||||
|
// StackTrace() errors.StackTrace
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Where errors.StackTrace is defined as
|
||||||
|
//
|
||||||
|
// type StackTrace []Frame
|
||||||
|
//
|
||||||
|
// The Frame type represents a call site in the stack trace. Frame supports
|
||||||
|
// the fmt.Formatter interface that can be used for printing information about
|
||||||
|
// the stack trace of this error. For example:
|
||||||
|
//
|
||||||
|
// if err, ok := err.(stackTracer); ok {
|
||||||
|
// for _, f := range err.StackTrace() {
|
||||||
|
// fmt.Printf("%+s:%d", f)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// stackTracer interface is not exported by this package, but is considered a part
|
||||||
|
// of stable public API.
|
||||||
|
//
|
||||||
|
// See the documentation for Frame.Format for more details.
|
||||||
|
package errors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// New returns an error with the supplied message.
|
||||||
|
// New also records the stack trace at the point it was called.
|
||||||
|
func New(message string) error {
|
||||||
|
return &fundamental{
|
||||||
|
msg: message,
|
||||||
|
stack: callers(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Errorf formats according to a format specifier and returns the string
|
||||||
|
// as a value that satisfies error.
|
||||||
|
// Errorf also records the stack trace at the point it was called.
|
||||||
|
func Errorf(format string, args ...interface{}) error {
|
||||||
|
return &fundamental{
|
||||||
|
msg: fmt.Sprintf(format, args...),
|
||||||
|
stack: callers(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fundamental is an error that has a message and a stack, but no caller.
|
||||||
|
type fundamental struct {
|
||||||
|
msg string
|
||||||
|
*stack
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fundamental) Error() string { return f.msg }
|
||||||
|
|
||||||
|
func (f *fundamental) Format(s fmt.State, verb rune) {
|
||||||
|
switch verb {
|
||||||
|
case 'v':
|
||||||
|
if s.Flag('+') {
|
||||||
|
io.WriteString(s, f.msg)
|
||||||
|
f.stack.Format(s, verb)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
case 's':
|
||||||
|
io.WriteString(s, f.msg)
|
||||||
|
case 'q':
|
||||||
|
fmt.Fprintf(s, "%q", f.msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithStack annotates err with a stack trace at the point WithStack was called.
|
||||||
|
// If err is nil, WithStack returns nil.
|
||||||
|
func WithStack(err error) error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &withStack{
|
||||||
|
err,
|
||||||
|
callers(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type withStack struct {
|
||||||
|
error
|
||||||
|
*stack
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *withStack) Cause() error { return w.error }
|
||||||
|
|
||||||
|
func (w *withStack) Format(s fmt.State, verb rune) {
|
||||||
|
switch verb {
|
||||||
|
case 'v':
|
||||||
|
if s.Flag('+') {
|
||||||
|
fmt.Fprintf(s, "%+v", w.Cause())
|
||||||
|
w.stack.Format(s, verb)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
case 's':
|
||||||
|
io.WriteString(s, w.Error())
|
||||||
|
case 'q':
|
||||||
|
fmt.Fprintf(s, "%q", w.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrap returns an error annotating err with a stack trace
|
||||||
|
// at the point Wrap is called, and the supplied message.
|
||||||
|
// If err is nil, Wrap returns nil.
|
||||||
|
func Wrap(err error, message string) error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err = &withMessage{
|
||||||
|
cause: err,
|
||||||
|
msg: message,
|
||||||
|
}
|
||||||
|
return &withStack{
|
||||||
|
err,
|
||||||
|
callers(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrapf returns an error annotating err with a stack trace
|
||||||
|
// at the point Wrapf is call, and the format specifier.
|
||||||
|
// If err is nil, Wrapf returns nil.
|
||||||
|
func Wrapf(err error, format string, args ...interface{}) error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err = &withMessage{
|
||||||
|
cause: err,
|
||||||
|
msg: fmt.Sprintf(format, args...),
|
||||||
|
}
|
||||||
|
return &withStack{
|
||||||
|
err,
|
||||||
|
callers(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithMessage annotates err with a new message.
|
||||||
|
// If err is nil, WithMessage returns nil.
|
||||||
|
func WithMessage(err error, message string) error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &withMessage{
|
||||||
|
cause: err,
|
||||||
|
msg: message,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type withMessage struct {
|
||||||
|
cause error
|
||||||
|
msg string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() }
|
||||||
|
func (w *withMessage) Cause() error { return w.cause }
|
||||||
|
|
||||||
|
func (w *withMessage) Format(s fmt.State, verb rune) {
|
||||||
|
switch verb {
|
||||||
|
case 'v':
|
||||||
|
if s.Flag('+') {
|
||||||
|
fmt.Fprintf(s, "%+v\n", w.Cause())
|
||||||
|
io.WriteString(s, w.msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
case 's', 'q':
|
||||||
|
io.WriteString(s, w.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cause returns the underlying cause of the error, if possible.
|
||||||
|
// An error value has a cause if it implements the following
|
||||||
|
// interface:
|
||||||
|
//
|
||||||
|
// type causer interface {
|
||||||
|
// Cause() error
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// If the error does not implement Cause, the original error will
|
||||||
|
// be returned. If the error is nil, nil will be returned without further
|
||||||
|
// investigation.
|
||||||
|
func Cause(err error) error {
|
||||||
|
type causer interface {
|
||||||
|
Cause() error
|
||||||
|
}
|
||||||
|
|
||||||
|
for err != nil {
|
||||||
|
cause, ok := err.(causer)
|
||||||
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
err = cause.Cause()
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
178
vendor/github.com/pkg/errors/stack.go
generated
vendored
Normal file
178
vendor/github.com/pkg/errors/stack.go
generated
vendored
Normal file
@ -0,0 +1,178 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"path"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Frame represents a program counter inside a stack frame.
|
||||||
|
type Frame uintptr
|
||||||
|
|
||||||
|
// pc returns the program counter for this frame;
|
||||||
|
// multiple frames may have the same PC value.
|
||||||
|
func (f Frame) pc() uintptr { return uintptr(f) - 1 }
|
||||||
|
|
||||||
|
// file returns the full path to the file that contains the
|
||||||
|
// function for this Frame's pc.
|
||||||
|
func (f Frame) file() string {
|
||||||
|
fn := runtime.FuncForPC(f.pc())
|
||||||
|
if fn == nil {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
file, _ := fn.FileLine(f.pc())
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
|
// line returns the line number of source code of the
|
||||||
|
// function for this Frame's pc.
|
||||||
|
func (f Frame) line() int {
|
||||||
|
fn := runtime.FuncForPC(f.pc())
|
||||||
|
if fn == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
_, line := fn.FileLine(f.pc())
|
||||||
|
return line
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format formats the frame according to the fmt.Formatter interface.
|
||||||
|
//
|
||||||
|
// %s source file
|
||||||
|
// %d source line
|
||||||
|
// %n function name
|
||||||
|
// %v equivalent to %s:%d
|
||||||
|
//
|
||||||
|
// Format accepts flags that alter the printing of some verbs, as follows:
|
||||||
|
//
|
||||||
|
// %+s path of source file relative to the compile time GOPATH
|
||||||
|
// %+v equivalent to %+s:%d
|
||||||
|
func (f Frame) Format(s fmt.State, verb rune) {
|
||||||
|
switch verb {
|
||||||
|
case 's':
|
||||||
|
switch {
|
||||||
|
case s.Flag('+'):
|
||||||
|
pc := f.pc()
|
||||||
|
fn := runtime.FuncForPC(pc)
|
||||||
|
if fn == nil {
|
||||||
|
io.WriteString(s, "unknown")
|
||||||
|
} else {
|
||||||
|
file, _ := fn.FileLine(pc)
|
||||||
|
fmt.Fprintf(s, "%s\n\t%s", fn.Name(), file)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
io.WriteString(s, path.Base(f.file()))
|
||||||
|
}
|
||||||
|
case 'd':
|
||||||
|
fmt.Fprintf(s, "%d", f.line())
|
||||||
|
case 'n':
|
||||||
|
name := runtime.FuncForPC(f.pc()).Name()
|
||||||
|
io.WriteString(s, funcname(name))
|
||||||
|
case 'v':
|
||||||
|
f.Format(s, 's')
|
||||||
|
io.WriteString(s, ":")
|
||||||
|
f.Format(s, 'd')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
|
||||||
|
type StackTrace []Frame
|
||||||
|
|
||||||
|
func (st StackTrace) Format(s fmt.State, verb rune) {
|
||||||
|
switch verb {
|
||||||
|
case 'v':
|
||||||
|
switch {
|
||||||
|
case s.Flag('+'):
|
||||||
|
for _, f := range st {
|
||||||
|
fmt.Fprintf(s, "\n%+v", f)
|
||||||
|
}
|
||||||
|
case s.Flag('#'):
|
||||||
|
fmt.Fprintf(s, "%#v", []Frame(st))
|
||||||
|
default:
|
||||||
|
fmt.Fprintf(s, "%v", []Frame(st))
|
||||||
|
}
|
||||||
|
case 's':
|
||||||
|
fmt.Fprintf(s, "%s", []Frame(st))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// stack represents a stack of program counters.
|
||||||
|
type stack []uintptr
|
||||||
|
|
||||||
|
func (s *stack) Format(st fmt.State, verb rune) {
|
||||||
|
switch verb {
|
||||||
|
case 'v':
|
||||||
|
switch {
|
||||||
|
case st.Flag('+'):
|
||||||
|
for _, pc := range *s {
|
||||||
|
f := Frame(pc)
|
||||||
|
fmt.Fprintf(st, "\n%+v", f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stack) StackTrace() StackTrace {
|
||||||
|
f := make([]Frame, len(*s))
|
||||||
|
for i := 0; i < len(f); i++ {
|
||||||
|
f[i] = Frame((*s)[i])
|
||||||
|
}
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func callers() *stack {
|
||||||
|
const depth = 32
|
||||||
|
var pcs [depth]uintptr
|
||||||
|
n := runtime.Callers(3, pcs[:])
|
||||||
|
var st stack = pcs[0:n]
|
||||||
|
return &st
|
||||||
|
}
|
||||||
|
|
||||||
|
// funcname removes the path prefix component of a function's name reported by func.Name().
|
||||||
|
func funcname(name string) string {
|
||||||
|
i := strings.LastIndex(name, "/")
|
||||||
|
name = name[i+1:]
|
||||||
|
i = strings.Index(name, ".")
|
||||||
|
return name[i+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimGOPATH(name, file string) string {
|
||||||
|
// Here we want to get the source file path relative to the compile time
|
||||||
|
// GOPATH. As of Go 1.6.x there is no direct way to know the compiled
|
||||||
|
// GOPATH at runtime, but we can infer the number of path segments in the
|
||||||
|
// GOPATH. We note that fn.Name() returns the function name qualified by
|
||||||
|
// the import path, which does not include the GOPATH. Thus we can trim
|
||||||
|
// segments from the beginning of the file path until the number of path
|
||||||
|
// separators remaining is one more than the number of path separators in
|
||||||
|
// the function name. For example, given:
|
||||||
|
//
|
||||||
|
// GOPATH /home/user
|
||||||
|
// file /home/user/src/pkg/sub/file.go
|
||||||
|
// fn.Name() pkg/sub.Type.Method
|
||||||
|
//
|
||||||
|
// We want to produce:
|
||||||
|
//
|
||||||
|
// pkg/sub/file.go
|
||||||
|
//
|
||||||
|
// From this we can easily see that fn.Name() has one less path separator
|
||||||
|
// than our desired output. We count separators from the end of the file
|
||||||
|
// path until it finds two more than in the function name and then move
|
||||||
|
// one character forward to preserve the initial path segment without a
|
||||||
|
// leading separator.
|
||||||
|
const sep = "/"
|
||||||
|
goal := strings.Count(name, sep) + 2
|
||||||
|
i := len(file)
|
||||||
|
for n := 0; n < goal; n++ {
|
||||||
|
i = strings.LastIndex(file[:i], sep)
|
||||||
|
if i == -1 {
|
||||||
|
// not enough separators found, set i so that the slice expression
|
||||||
|
// below leaves file unmodified
|
||||||
|
i = -len(sep)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// get back to 0 or trim the leading separator
|
||||||
|
file = file[i+len(sep):]
|
||||||
|
return file
|
||||||
|
}
|
21
vendor/github.com/yudai/umutex/LICENSE
generated
vendored
21
vendor/github.com/yudai/umutex/LICENSE
generated
vendored
@ -1,21 +0,0 @@
|
|||||||
The MIT License (MIT)
|
|
||||||
|
|
||||||
Copyright (c) 2015 Iwasaki Yudai
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
53
vendor/github.com/yudai/umutex/README.md
generated
vendored
53
vendor/github.com/yudai/umutex/README.md
generated
vendored
@ -1,53 +0,0 @@
|
|||||||
# Unblocking Mutex
|
|
||||||
|
|
||||||
This simple package provides unblocking mutexes for those who don't want to write many `select` clauses or get confused by numerous channels.
|
|
||||||
|
|
||||||
## Usage Example
|
|
||||||
|
|
||||||
```go
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/yudai/umutex"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// Create mutex
|
|
||||||
mutex := umutex.New()
|
|
||||||
|
|
||||||
// First time, try should succeed
|
|
||||||
if mutex.TryLock() {
|
|
||||||
fmt.Println("SUCCESS")
|
|
||||||
} else {
|
|
||||||
fmt.Println("FAILURE")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Second time, try should fail as it's locked
|
|
||||||
if mutex.TryLock() {
|
|
||||||
fmt.Println("SUCCESS")
|
|
||||||
} else {
|
|
||||||
fmt.Println("FAILURE")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unclock mutex
|
|
||||||
mutex.Unlock()
|
|
||||||
|
|
||||||
// Third time, try should succeed again
|
|
||||||
if mutex.TryLock() {
|
|
||||||
fmt.Println("SUCCESS")
|
|
||||||
} else {
|
|
||||||
fmt.Println("FAILURE")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
The output is;
|
|
||||||
|
|
||||||
```sh
|
|
||||||
SUCCESS
|
|
||||||
FAILURE
|
|
||||||
SUCCESS
|
|
||||||
```
|
|
||||||
|
|
||||||
`ForceLock()` method is also availale for normal blocking lock.
|
|
38
vendor/github.com/yudai/umutex/umutex.go
generated
vendored
38
vendor/github.com/yudai/umutex/umutex.go
generated
vendored
@ -1,38 +0,0 @@
|
|||||||
// Package umutex provides unblocking mutex
|
|
||||||
package umutex
|
|
||||||
|
|
||||||
// UnblockingMutex represents an unblocking mutex.
|
|
||||||
type UnblockingMutex struct {
|
|
||||||
// Raw channel
|
|
||||||
C chan bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// New returnes a new unblocking mutex instance.
|
|
||||||
func New() *UnblockingMutex {
|
|
||||||
return &UnblockingMutex{
|
|
||||||
C: make(chan bool, 1),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TryLock tries to lock the mutex.
|
|
||||||
// When the mutex is free at the time, the function locks the mutex and return
|
|
||||||
// true. Otherwise false will be returned. In the both cases, this function
|
|
||||||
// doens't block and return the result immediately.
|
|
||||||
func (m UnblockingMutex) TryLock() (result bool) {
|
|
||||||
select {
|
|
||||||
case m.C <- true:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unlock unclocks the mutex.
|
|
||||||
func (m UnblockingMutex) Unlock() {
|
|
||||||
<-m.C
|
|
||||||
}
|
|
||||||
|
|
||||||
// ForceLock surely locks the mutex, however, this function blocks when the mutex is locked at the time.
|
|
||||||
func (m UnblockingMutex) ForceLock() {
|
|
||||||
m.C <- false
|
|
||||||
}
|
|
3
version.go
Normal file
3
version.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
var Version = "2.0.0-alpha"
|
3
webtty/doc.go
Normal file
3
webtty/doc.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
// Package webtty provides a protocl and an implementation to
|
||||||
|
// controll terminals thorough networks.
|
||||||
|
package webtty
|
10
webtty/errors.go
Normal file
10
webtty/errors.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package webtty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrSlaveClosed = errors.New("slave closed")
|
||||||
|
ErrMasterClosed = errors.New("master closed")
|
||||||
|
)
|
55
webtty/master.go
Normal file
55
webtty/master.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2013 The Gorilla WebSocket Authors. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package webtty
|
||||||
|
|
||||||
|
// Master represents a PTY master, usually it's a websocket connection.
|
||||||
|
type Master interface {
|
||||||
|
WriteMessage(messageType int, data []byte) error
|
||||||
|
ReadMessage() (messageType int, p []byte, err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The message types are defined in RFC 6455, section 11.8.
|
||||||
|
const (
|
||||||
|
// TextMessage denotes a text data message. The text message payload is
|
||||||
|
// interpreted as UTF-8 encoded text data.
|
||||||
|
WSTextMessage = 1
|
||||||
|
|
||||||
|
// BinaryMessage denotes a binary data message.
|
||||||
|
WSBinaryMessage = 2
|
||||||
|
|
||||||
|
// CloseMessage denotes a close control message. The optional message
|
||||||
|
// payload contains a numeric code and text. Use the FormatCloseMessage
|
||||||
|
// function to format a close message payload.
|
||||||
|
WSCloseMessage = 8
|
||||||
|
|
||||||
|
// PingMessage denotes a ping control message. The optional message payload
|
||||||
|
// is UTF-8 encoded text.
|
||||||
|
WSPingMessage = 9
|
||||||
|
|
||||||
|
// PongMessage denotes a ping control message. The optional message payload
|
||||||
|
// is UTF-8 encoded text.
|
||||||
|
WSPongMessage = 10
|
||||||
|
)
|
29
webtty/message_types.go
Normal file
29
webtty/message_types.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package webtty
|
||||||
|
|
||||||
|
var Protocols = []string{"webtty"}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Unknown message type, maybe sent by a bug
|
||||||
|
UnknownInput = '0'
|
||||||
|
// User input typically from a keyboard
|
||||||
|
Input = '1'
|
||||||
|
// Ping to the server
|
||||||
|
Ping = '2'
|
||||||
|
// Notify that the browser size has been changed
|
||||||
|
ResizeTerminal = '3'
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Unknown message type, maybe set by a bug
|
||||||
|
UnknownOutput = '0'
|
||||||
|
// Normal output to the terminal
|
||||||
|
Output = '1'
|
||||||
|
// Pong to the browser
|
||||||
|
Pong = '2'
|
||||||
|
// Set window title of the terminal
|
||||||
|
SetWindowTitle = '3'
|
||||||
|
// Set terminal preference
|
||||||
|
SetPreferences = '4'
|
||||||
|
// Make terminal to reconnect
|
||||||
|
SetReconnect = '5'
|
||||||
|
)
|
55
webtty/option.go
Normal file
55
webtty/option.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package webtty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Option is an option for WebTTY.
|
||||||
|
type Option func(*WebTTY) error
|
||||||
|
|
||||||
|
// WithPermitWrite sets a WebTTY to accept input from slaves.
|
||||||
|
func WithPermitWrite() Option {
|
||||||
|
return func(wt *WebTTY) error {
|
||||||
|
wt.permitWrite = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithFixedSize sets a fixed size to TTY master.
|
||||||
|
func WithFixedSize(width int, height int) Option {
|
||||||
|
return func(wt *WebTTY) error {
|
||||||
|
wt.width = width
|
||||||
|
wt.height = height
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithWindowTitle sets the default window title of the session
|
||||||
|
func WithWindowTitle(windowTitle []byte) Option {
|
||||||
|
return func(wt *WebTTY) error {
|
||||||
|
wt.windowTitle = windowTitle
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithReconnect enables reconnection on the master side.
|
||||||
|
func WithReconnect(timeInSeconds int) Option {
|
||||||
|
return func(wt *WebTTY) error {
|
||||||
|
wt.reconnect = timeInSeconds
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithMasterPreferences sets an optional configuration of master.
|
||||||
|
func WithMasterPreferences(preferences interface{}) Option {
|
||||||
|
return func(wt *WebTTY) error {
|
||||||
|
prefs, err := json.Marshal(preferences)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to marshal preferences as JSON")
|
||||||
|
}
|
||||||
|
wt.masterPrefs = prefs
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
13
webtty/slave.go
Normal file
13
webtty/slave.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package webtty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Slave represents a PTY slave, typically it's a local command.
|
||||||
|
type Slave interface {
|
||||||
|
io.ReadWriteCloser
|
||||||
|
|
||||||
|
WindowTitleVariables() map[string]interface{}
|
||||||
|
ResizeTerminal(columns int, rows int) error
|
||||||
|
}
|
220
webtty/webtty.go
Normal file
220
webtty/webtty.go
Normal file
@ -0,0 +1,220 @@
|
|||||||
|
package webtty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WebTTY bridges sets of a PTY slave and its PTY master.
|
||||||
|
// To support text-based streams and side channel commands such as
|
||||||
|
// terminal resizing, WebTTY uses an original protocol.
|
||||||
|
type WebTTY struct {
|
||||||
|
// PTY Master, which probably a connection to browser
|
||||||
|
masterConn Master
|
||||||
|
// PTY Slave
|
||||||
|
slave Slave
|
||||||
|
|
||||||
|
windowTitle []byte
|
||||||
|
permitWrite bool
|
||||||
|
width int
|
||||||
|
height int
|
||||||
|
reconnect int // in milliseconds
|
||||||
|
masterPrefs []byte
|
||||||
|
|
||||||
|
bufferSize int
|
||||||
|
writeMutex sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new instance of WebTTY.
|
||||||
|
// masterConn is a connection to the PTY master,
|
||||||
|
// typically it's a websocket connection to a client.
|
||||||
|
// slave is a PTY slave such as a local command with a PTY.
|
||||||
|
func New(masterConn Master, slave Slave, options ...Option) (*WebTTY, error) {
|
||||||
|
wt := &WebTTY{
|
||||||
|
masterConn: masterConn,
|
||||||
|
slave: slave,
|
||||||
|
|
||||||
|
permitWrite: false,
|
||||||
|
width: 0,
|
||||||
|
height: 0,
|
||||||
|
|
||||||
|
bufferSize: 1024,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
option(wt)
|
||||||
|
}
|
||||||
|
|
||||||
|
return wt, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run starts the WebTTY.
|
||||||
|
// This method blocks until the context is canceled.
|
||||||
|
// Note that the master and slave are left intact even
|
||||||
|
// after the context is canceled. Closing them is caller's
|
||||||
|
// responsibility.
|
||||||
|
func (wt *WebTTY) Run(ctx context.Context) error {
|
||||||
|
err := wt.sendInitializeMessage()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to send initializing message")
|
||||||
|
}
|
||||||
|
|
||||||
|
errs := make(chan error, 2)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
errs <- func() error {
|
||||||
|
buffer := make([]byte, wt.bufferSize)
|
||||||
|
for {
|
||||||
|
n, err := wt.slave.Read(buffer)
|
||||||
|
if err != nil {
|
||||||
|
return ErrSlaveClosed
|
||||||
|
}
|
||||||
|
|
||||||
|
err = wt.handleSlaveReadEvent(buffer[:n])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
errs <- func() error {
|
||||||
|
for {
|
||||||
|
typ, data, err := wt.masterConn.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
return ErrMasterClosed
|
||||||
|
}
|
||||||
|
if typ != WSTextMessage {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err = wt.handleMasterReadEvent(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
err = ctx.Err()
|
||||||
|
case err = <-errs:
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wt *WebTTY) sendInitializeMessage() error {
|
||||||
|
err := wt.masterWrite(append([]byte{SetWindowTitle}, wt.windowTitle...))
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to send window title")
|
||||||
|
}
|
||||||
|
|
||||||
|
if wt.reconnect > 0 {
|
||||||
|
reconnect, _ := json.Marshal(wt.reconnect)
|
||||||
|
err := wt.masterWrite(append([]byte{SetReconnect}, reconnect...))
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to set reconnect")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if wt.masterPrefs != nil {
|
||||||
|
err := wt.masterWrite(append([]byte{SetPreferences}, wt.masterPrefs...))
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to set preferences")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wt *WebTTY) handleSlaveReadEvent(data []byte) error {
|
||||||
|
safeMessage := base64.StdEncoding.EncodeToString(data)
|
||||||
|
err := wt.masterWrite(append([]byte{Output}, []byte(safeMessage)...))
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to send message to master")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wt *WebTTY) masterWrite(data []byte) error {
|
||||||
|
wt.writeMutex.Lock()
|
||||||
|
defer wt.writeMutex.Unlock()
|
||||||
|
|
||||||
|
err := wt.masterConn.WriteMessage(WSTextMessage, data)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to write to master")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wt *WebTTY) handleMasterReadEvent(data []byte) error {
|
||||||
|
if len(data) == 0 {
|
||||||
|
return errors.New("unexpected zero length read from master")
|
||||||
|
}
|
||||||
|
|
||||||
|
switch data[0] {
|
||||||
|
case Input:
|
||||||
|
if !wt.permitWrite {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(data) <= 1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := wt.slave.Write(data[1:])
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to write received data to slave")
|
||||||
|
}
|
||||||
|
|
||||||
|
case Ping:
|
||||||
|
err := wt.masterWrite([]byte{Pong})
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to return Pong message to master")
|
||||||
|
}
|
||||||
|
|
||||||
|
case ResizeTerminal:
|
||||||
|
if wt.width != 0 && wt.height != 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(data) <= 1 {
|
||||||
|
return errors.New("received malformed remote command for terminal resize: empty payload")
|
||||||
|
}
|
||||||
|
|
||||||
|
var args argResizeTerminal
|
||||||
|
err := json.Unmarshal(data[1:], &args)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "received malformed data for terminal resize")
|
||||||
|
}
|
||||||
|
rows := wt.height
|
||||||
|
if rows == 0 {
|
||||||
|
rows = int(args.Rows)
|
||||||
|
}
|
||||||
|
|
||||||
|
columns := wt.width
|
||||||
|
if columns == 0 {
|
||||||
|
columns = int(args.Columns)
|
||||||
|
}
|
||||||
|
|
||||||
|
wt.slave.ResizeTerminal(columns, rows)
|
||||||
|
default:
|
||||||
|
return errors.Errorf("unknown message type `%c`", data[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type argResizeTerminal struct {
|
||||||
|
Columns float64
|
||||||
|
Rows float64
|
||||||
|
}
|
139
webtty/webtty_test.go
Normal file
139
webtty/webtty_test.go
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
package webtty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
|
"io"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type pipePair struct {
|
||||||
|
*io.PipeReader
|
||||||
|
*io.PipeWriter
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteFromPTY(t *testing.T) {
|
||||||
|
connInPipeReader, connInPipeWriter := io.Pipe() // in to conn
|
||||||
|
connOutPipeReader, _ := io.Pipe() // out from conn
|
||||||
|
|
||||||
|
conn := pipePair{
|
||||||
|
connOutPipeReader,
|
||||||
|
connInPipeWriter,
|
||||||
|
}
|
||||||
|
dt, err := New(conn)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error from New(): %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
wg.Done()
|
||||||
|
err := dt.Run(ctx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error from Run(): %s", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
message := []byte("foobar")
|
||||||
|
n, err := dt.TTY().Write(message)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error from Write(): %s", err)
|
||||||
|
}
|
||||||
|
if n != len(message) {
|
||||||
|
t.Fatalf("Write() accepted `%d` for message `%s`", n, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
n, err = connInPipeReader.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error from Read(): %s", err)
|
||||||
|
}
|
||||||
|
if buf[0] != Output {
|
||||||
|
t.Fatalf("Unexpected message type `%c`", buf[0])
|
||||||
|
}
|
||||||
|
decoded := make([]byte, 1024)
|
||||||
|
n, err = base64.StdEncoding.Decode(decoded, buf[1:n])
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error from Decode(): %s", err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(decoded[:n], message) {
|
||||||
|
t.Fatalf("Unexpected message received: `%s`", decoded[:n])
|
||||||
|
}
|
||||||
|
|
||||||
|
cancel()
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteFromConn(t *testing.T) {
|
||||||
|
connInPipeReader, connInPipeWriter := io.Pipe() // in to conn
|
||||||
|
connOutPipeReader, connOutPipeWriter := io.Pipe() // out from conn
|
||||||
|
|
||||||
|
conn := pipePair{
|
||||||
|
connOutPipeReader,
|
||||||
|
connInPipeWriter,
|
||||||
|
}
|
||||||
|
|
||||||
|
dt, err := New(conn)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error from New(): %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
wg.Done()
|
||||||
|
err := dt.Run(ctx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error from Run(): %s", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
var (
|
||||||
|
message []byte
|
||||||
|
n int
|
||||||
|
)
|
||||||
|
readBuf := make([]byte, 1024)
|
||||||
|
|
||||||
|
// input
|
||||||
|
message = []byte("0hello\n") // line buffered canonical mode
|
||||||
|
n, err = connOutPipeWriter.Write(message)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error from Write(): %s", err)
|
||||||
|
}
|
||||||
|
if n != len(message) {
|
||||||
|
t.Fatalf("Write() accepted `%d` for message `%s`", n, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err = dt.TTY().Read(readBuf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error from Write(): %s", err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(readBuf[:n], message[1:]) {
|
||||||
|
t.Fatalf("Unexpected message received: `%s`", readBuf[:n])
|
||||||
|
}
|
||||||
|
|
||||||
|
// ping
|
||||||
|
message = []byte("1\n") // line buffered canonical mode
|
||||||
|
n, err = connOutPipeWriter.Write(message)
|
||||||
|
if n != len(message) {
|
||||||
|
t.Fatalf("Write() accepted `%d` for message `%s`", n, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err = connInPipeReader.Read(readBuf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error from Read(): %s", err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(readBuf[:n], []byte{'1'}) {
|
||||||
|
t.Fatalf("Unexpected message received: `%s`", readBuf[:n])
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: resize
|
||||||
|
|
||||||
|
cancel()
|
||||||
|
wg.Wait()
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
box: golang:1.7.3
|
box: golang:1.8.3
|
||||||
|
|
||||||
build:
|
build:
|
||||||
steps:
|
steps:
|
||||||
|
Loading…
Reference in New Issue
Block a user