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