mirror of
https://github.com/sorenisanerd/gotty.git
synced 2024-11-22 04:14:25 +00:00
Merge pull request #27 from shoz/randomurl
Random URL generation(Close #17)
This commit is contained in:
commit
e09d6e0486
39
app/app.go
39
app/app.go
@ -1,11 +1,14 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
@ -20,16 +23,18 @@ type App struct {
|
||||
Address string
|
||||
Port string
|
||||
PermitWrite bool
|
||||
RandomUrl bool
|
||||
Credential string
|
||||
Command []string
|
||||
}
|
||||
|
||||
func New(address string, port string, permitWrite bool, cred string, command []string) *App {
|
||||
func New(address string, port string, permitWrite bool, cred string, randomUrl bool, command []string) *App {
|
||||
return &App{
|
||||
Address: address,
|
||||
Port: port,
|
||||
PermitWrite: permitWrite,
|
||||
Credential: cred,
|
||||
RandomUrl: randomUrl,
|
||||
Command: command,
|
||||
}
|
||||
}
|
||||
@ -65,21 +70,24 @@ func basicAuthHandler(h http.Handler, cred string) http.Handler {
|
||||
}
|
||||
|
||||
func (app *App) Run() error {
|
||||
http.Handle("/",
|
||||
http.FileServer(
|
||||
&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "bindata"},
|
||||
),
|
||||
)
|
||||
http.HandleFunc("/ws", app.generateHandler())
|
||||
path := "/"
|
||||
if app.RandomUrl {
|
||||
randomPath := generateRandomString(8)
|
||||
path = "/" + randomPath + "/"
|
||||
}
|
||||
|
||||
url := app.Address + ":" + app.Port
|
||||
log.Printf("Server is running at %s, command: %s", url, strings.Join(app.Command, " "))
|
||||
fs := http.StripPrefix(path, http.FileServer(&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "bindata"}))
|
||||
http.Handle(path, fs)
|
||||
http.HandleFunc(path+"ws", app.generateHandler())
|
||||
|
||||
endpoint := app.Address + ":" + app.Port
|
||||
log.Printf("Server is running at %s, command: %s", endpoint+path, strings.Join(app.Command, " "))
|
||||
handler := http.Handler(http.DefaultServeMux)
|
||||
handler = loggerHandler(handler)
|
||||
if app.Credential != "" {
|
||||
handler = basicAuthHandler(handler, app.Credential)
|
||||
}
|
||||
err := http.ListenAndServe(url, handler)
|
||||
err := http.ListenAndServe(endpoint, handler)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -227,3 +235,14 @@ type command struct {
|
||||
Name string `json:"name"`
|
||||
Arguments map[string]interface{} `json:"arguments"`
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
7
main.go
7
main.go
@ -37,6 +37,11 @@ func main() {
|
||||
Usage: "Credential for Basic Authentication (ex: user:pass)",
|
||||
EnvVar: "GOTTY_CREDENTIAL",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "random-url, r",
|
||||
Usage: "Add a random string to the URL",
|
||||
EnvVar: "GOTTY_RANDOM_URL",
|
||||
},
|
||||
}
|
||||
cmd.Action = func(c *cli.Context) {
|
||||
if len(c.Args()) == 0 {
|
||||
@ -44,7 +49,7 @@ func main() {
|
||||
cli.ShowAppHelp(c)
|
||||
os.Exit(1)
|
||||
}
|
||||
app := app.New(c.String("addr"), c.String("port"), c.Bool("permit-write"), c.String("credential"), c.Args())
|
||||
app := app.New(c.String("addr"), c.String("port"), c.Bool("permit-write"), c.String("credential"), c.Bool("random-url"), c.Args())
|
||||
err := app.Run()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
Loading…
Reference in New Issue
Block a user