2015-08-16 09:47:23 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2015-08-19 11:35:04 +00:00
|
|
|
"crypto/rand"
|
2015-08-20 06:40:38 +00:00
|
|
|
"encoding/base64"
|
2015-08-23 13:55:06 +00:00
|
|
|
"encoding/json"
|
2015-08-23 11:40:18 +00:00
|
|
|
"errors"
|
2015-08-23 13:55:06 +00:00
|
|
|
"io/ioutil"
|
2015-08-16 09:47:23 +00:00
|
|
|
"log"
|
2015-08-19 11:35:04 +00:00
|
|
|
"math/big"
|
2015-08-23 05:04:31 +00:00
|
|
|
"net"
|
2015-08-16 09:47:23 +00:00
|
|
|
"net/http"
|
2015-08-23 20:34:56 +00:00
|
|
|
"net/url"
|
2015-08-23 13:55:06 +00:00
|
|
|
"os"
|
2015-08-16 09:47:23 +00:00
|
|
|
"os/exec"
|
2015-08-19 11:35:04 +00:00
|
|
|
"strconv"
|
2015-08-16 09:47:23 +00:00
|
|
|
"strings"
|
2015-08-23 11:40:18 +00:00
|
|
|
"text/template"
|
2015-08-16 09:47:23 +00:00
|
|
|
|
2015-08-24 10:22:25 +00:00
|
|
|
"github.com/braintree/manners"
|
2015-08-16 09:47:23 +00:00
|
|
|
"github.com/elazarl/go-bindata-assetfs"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/kr/pty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type App struct {
|
2015-08-21 03:48:07 +00:00
|
|
|
options Options
|
2015-08-21 09:22:08 +00:00
|
|
|
|
2015-08-23 13:55:06 +00:00
|
|
|
upgrader *websocket.Upgrader
|
2015-08-24 10:22:25 +00:00
|
|
|
server *manners.GracefulServer
|
2015-08-23 13:55:06 +00:00
|
|
|
|
|
|
|
preferences map[string]interface{}
|
2015-08-23 11:40:18 +00:00
|
|
|
titleTemplate *template.Template
|
2015-08-21 03:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Options struct {
|
2015-08-23 22:14:24 +00:00
|
|
|
Address string
|
|
|
|
Port string
|
|
|
|
PermitWrite bool
|
|
|
|
Credential string
|
|
|
|
RandomUrl bool
|
|
|
|
ProfileFile string
|
2015-08-24 07:43:03 +00:00
|
|
|
EnableTLS bool
|
|
|
|
TLSCert string
|
|
|
|
TLSKey string
|
2015-08-23 22:14:24 +00:00
|
|
|
TitleFormat string
|
|
|
|
AutoReconnect int
|
|
|
|
Command []string
|
2015-08-16 09:47:23 +00:00
|
|
|
}
|
|
|
|
|
2015-08-23 13:55:06 +00:00
|
|
|
const DefaultProfileFilePath = "~/.gotty"
|
2015-08-24 07:43:03 +00:00
|
|
|
const DefaultTLSKeyPath = "~/.gotty.key"
|
|
|
|
const DefaultTLSCertPath = "~/.gotty.crt"
|
2015-08-23 13:55:06 +00:00
|
|
|
|
2015-08-23 11:40:18 +00:00
|
|
|
func New(options Options) (*App, error) {
|
|
|
|
titleTemplate, err := template.New("title").Parse(options.TitleFormat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("Title format string syntax error")
|
|
|
|
}
|
|
|
|
|
2015-08-24 07:13:22 +00:00
|
|
|
prefMap, err := loadProfileFile(options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &App{
|
|
|
|
options: options,
|
|
|
|
|
|
|
|
upgrader: &websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
Subprotocols: []string{"gotty"},
|
|
|
|
},
|
|
|
|
|
|
|
|
preferences: prefMap,
|
|
|
|
titleTemplate: titleTemplate,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadProfileFile(options Options) (map[string]interface{}, error) {
|
2015-08-23 13:55:06 +00:00
|
|
|
prefString := []byte{}
|
|
|
|
prefPath := options.ProfileFile
|
|
|
|
if options.ProfileFile == DefaultProfileFilePath {
|
2015-08-23 19:48:49 +00:00
|
|
|
prefPath = os.Getenv("HOME") + "/.gotty"
|
2015-08-23 13:55:06 +00:00
|
|
|
}
|
2015-08-24 07:13:22 +00:00
|
|
|
if _, err := os.Stat(prefPath); os.IsNotExist(err) {
|
2015-08-23 13:55:06 +00:00
|
|
|
if options.ProfileFile != DefaultProfileFilePath {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("Loading profile path: %s", prefPath)
|
|
|
|
prefString, _ = ioutil.ReadFile(prefPath)
|
|
|
|
}
|
|
|
|
if len(prefString) == 0 {
|
|
|
|
prefString = []byte(("{}"))
|
|
|
|
}
|
|
|
|
var prefMap map[string]interface{}
|
2015-08-24 07:13:22 +00:00
|
|
|
err := json.Unmarshal(prefString, &prefMap)
|
2015-08-23 13:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-24 07:13:22 +00:00
|
|
|
return prefMap, nil
|
2015-08-16 09:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) Run() error {
|
2015-08-23 12:00:52 +00:00
|
|
|
if app.options.PermitWrite {
|
|
|
|
log.Printf("Permitting clients to write input to the PTY.")
|
|
|
|
}
|
|
|
|
|
2015-08-23 05:04:31 +00:00
|
|
|
path := ""
|
2015-08-21 03:48:07 +00:00
|
|
|
if app.options.RandomUrl {
|
2015-08-23 05:04:31 +00:00
|
|
|
path += "/" + generateRandomString(8)
|
2015-08-19 11:35:04 +00:00
|
|
|
}
|
|
|
|
|
2015-08-23 20:22:47 +00:00
|
|
|
endpoint := net.JoinHostPort(app.options.Address, app.options.Port)
|
2015-08-22 03:51:37 +00:00
|
|
|
|
2015-08-23 05:04:31 +00:00
|
|
|
wsHandler := http.HandlerFunc(app.handleWS)
|
2015-08-22 03:51:37 +00:00
|
|
|
staticHandler := http.FileServer(
|
2015-08-23 05:04:31 +00:00
|
|
|
&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "static"},
|
2015-08-22 03:51:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var siteMux = http.NewServeMux()
|
2015-08-23 11:40:18 +00:00
|
|
|
siteMux.Handle(path+"/", http.StripPrefix(path+"/", staticHandler))
|
2015-08-23 05:04:31 +00:00
|
|
|
siteMux.Handle(path+"/ws", wsHandler)
|
2015-08-22 03:51:37 +00:00
|
|
|
|
|
|
|
siteHandler := http.Handler(siteMux)
|
|
|
|
|
2015-08-21 03:48:07 +00:00
|
|
|
if app.options.Credential != "" {
|
2015-08-22 03:51:37 +00:00
|
|
|
log.Printf("Using Basic Authentication")
|
|
|
|
siteHandler = wrapBasicAuth(siteHandler, app.options.Credential)
|
2015-08-20 06:40:38 +00:00
|
|
|
}
|
2015-08-22 03:51:37 +00:00
|
|
|
|
|
|
|
siteHandler = wrapLogger(siteHandler)
|
|
|
|
|
2015-08-24 07:43:03 +00:00
|
|
|
scheme := "http"
|
|
|
|
if app.options.EnableTLS {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
2015-08-22 04:10:08 +00:00
|
|
|
log.Printf(
|
|
|
|
"Server is starting with command: %s",
|
|
|
|
strings.Join(app.options.Command, " "),
|
|
|
|
)
|
|
|
|
if app.options.Address != "" {
|
2015-08-23 20:34:56 +00:00
|
|
|
log.Printf(
|
2015-08-24 07:43:03 +00:00
|
|
|
"URL: %s",
|
|
|
|
(&url.URL{Scheme: scheme, Host: endpoint, Path: path + "/"}).String(),
|
2015-08-23 20:34:56 +00:00
|
|
|
)
|
2015-08-22 04:10:08 +00:00
|
|
|
} else {
|
|
|
|
for _, address := range listAddresses() {
|
2015-08-23 20:34:56 +00:00
|
|
|
log.Printf(
|
|
|
|
"URL: %s",
|
|
|
|
(&url.URL{
|
2015-08-24 07:43:03 +00:00
|
|
|
Scheme: scheme,
|
2015-08-23 20:34:56 +00:00
|
|
|
Host: net.JoinHostPort(address, app.options.Port),
|
|
|
|
Path: path + "/",
|
|
|
|
}).String(),
|
|
|
|
)
|
2015-08-22 04:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-24 07:43:03 +00:00
|
|
|
|
|
|
|
var err error
|
2015-08-24 10:22:25 +00:00
|
|
|
app.server = manners.NewWithServer(
|
|
|
|
&http.Server{Addr: endpoint, Handler: siteHandler},
|
|
|
|
)
|
2015-08-24 07:43:03 +00:00
|
|
|
if app.options.EnableTLS {
|
|
|
|
cert, key := app.loadTLSFiles()
|
2015-08-24 10:22:25 +00:00
|
|
|
err = app.server.ListenAndServeTLS(cert, key)
|
2015-08-24 07:43:03 +00:00
|
|
|
} else {
|
2015-08-24 10:22:25 +00:00
|
|
|
err = app.server.ListenAndServe()
|
2015-08-24 07:43:03 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2015-08-16 09:47:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:22:25 +00:00
|
|
|
log.Printf("Exiting...")
|
|
|
|
|
2015-08-16 09:47:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-24 07:43:03 +00:00
|
|
|
func (app *App) loadTLSFiles() (cert string, key string) {
|
|
|
|
cert = app.options.TLSCert
|
|
|
|
if app.options.TLSCert == DefaultTLSCertPath {
|
|
|
|
cert = os.Getenv("HOME") + "/.gotty.crt"
|
|
|
|
}
|
|
|
|
|
|
|
|
key = app.options.TLSKey
|
|
|
|
if app.options.TLSKey == DefaultTLSKeyPath {
|
|
|
|
key = os.Getenv("HOME") + "/.gotty.key"
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-22 03:51:37 +00:00
|
|
|
func (app *App) handleWS(w http.ResponseWriter, r *http.Request) {
|
2015-08-21 09:22:08 +00:00
|
|
|
log.Printf("New client connected: %s", r.RemoteAddr)
|
2015-08-16 09:47:23 +00:00
|
|
|
|
2015-08-21 09:22:08 +00:00
|
|
|
if r.Method != "GET" {
|
|
|
|
http.Error(w, "Method not allowed", 405)
|
|
|
|
return
|
|
|
|
}
|
2015-08-16 09:47:23 +00:00
|
|
|
|
2015-08-21 09:22:08 +00:00
|
|
|
conn, err := app.upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("Failed to upgrade connection")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(app.options.Command[0], app.options.Command[1:]...)
|
|
|
|
ptyIo, err := pty.Start(cmd)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("Failed to execute command")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("Command is running for client %s with PID %d", r.RemoteAddr, cmd.Process.Pid)
|
|
|
|
|
|
|
|
context := &clientContext{
|
2015-08-21 09:51:43 +00:00
|
|
|
app: app,
|
2015-08-21 09:22:08 +00:00
|
|
|
request: r,
|
|
|
|
connection: conn,
|
|
|
|
command: cmd,
|
|
|
|
pty: ptyIo,
|
|
|
|
}
|
|
|
|
|
2015-08-21 09:51:43 +00:00
|
|
|
context.goHandleClient()
|
2015-08-21 09:22:08 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 10:22:25 +00:00
|
|
|
func (app *App) Exit() (firstCall bool) {
|
|
|
|
if app.server != nil {
|
|
|
|
log.Printf("Received Exit command, waiting for all clients to close sessions...")
|
|
|
|
return app.server.Close()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-08-22 03:51:37 +00:00
|
|
|
func wrapLogger(handler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Printf("%s %s", r.Method, r.URL.Path)
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-21 09:22:08 +00:00
|
|
|
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]
|
2015-08-16 09:47:23 +00:00
|
|
|
}
|
2015-08-21 09:22:08 +00:00
|
|
|
return string(n)
|
2015-08-16 09:47:23 +00:00
|
|
|
}
|
2015-08-22 04:10:08 +00:00
|
|
|
|
|
|
|
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:
|
2015-08-23 20:22:47 +00:00
|
|
|
addresses = append(addresses, v.IP.String())
|
2015-08-22 04:10:08 +00:00
|
|
|
case *net.IPAddr:
|
2015-08-23 20:05:58 +00:00
|
|
|
addresses = append(addresses, v.IP.String())
|
2015-08-22 04:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|