2017-04-14 00:45:38 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2018-10-15 19:24:17 +00:00
|
|
|
"time"
|
2017-04-14 00:45:38 +00:00
|
|
|
|
|
|
|
"github.com/Luzifer/expose/ngrok2"
|
2018-09-17 19:23:57 +00:00
|
|
|
http_helper "github.com/Luzifer/go_helpers/http"
|
|
|
|
|
2017-04-14 00:45:38 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-07-31 12:18:31 +00:00
|
|
|
type corsWrap struct {
|
|
|
|
next http.Handler
|
|
|
|
header string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCorsWrap(next http.Handler, header string) *corsWrap { return &corsWrap{next, header} }
|
|
|
|
|
|
|
|
func (c corsWrap) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", c.header)
|
|
|
|
c.next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2017-04-14 00:45:38 +00:00
|
|
|
// serveCmd represents the serve command
|
|
|
|
var serveCmd = &cobra.Command{
|
|
|
|
Use: "serve",
|
|
|
|
Short: "A brief description of your command",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to bind local HTTP server: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, port, err := net.SplitHostPort(listener.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to determine bound port: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := fmt.Sprintf("expose_%s", port)
|
|
|
|
if tun, err := client.GetTunnelByName(name); err == nil {
|
|
|
|
return fmt.Errorf("Address %q already has an active tunnel with URL %s", tun.Config.Addr, tun.PublicURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
tun, err := client.CreateTunnel(ngrok2.CreateTunnelInput{
|
|
|
|
Name: name,
|
|
|
|
Addr: port,
|
|
|
|
Proto: "http",
|
|
|
|
BindTLS: "true",
|
|
|
|
Inspect: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to create tunnel: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-07-31 12:18:31 +00:00
|
|
|
var handler http.Handler
|
|
|
|
handler = http.FileServer(http.Dir("."))
|
|
|
|
handler = http_helper.GzipHandler(handler)
|
|
|
|
|
|
|
|
if c, err := cmd.Flags().GetString("cors"); err == nil && c != "" {
|
|
|
|
handler = newCorsWrap(handler, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if l, err := cmd.Flags().GetBool("http-logs"); err == nil && l {
|
|
|
|
handler = http_helper.NewHTTPLogHandler(handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
go http.Serve(listener, handler)
|
2017-04-14 00:45:38 +00:00
|
|
|
fmt.Printf("Created HTTP server for this directory with URL %s\nPress Ctrl+C to stop server and tunnel", tun.PublicURL)
|
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt)
|
2018-10-15 19:24:17 +00:00
|
|
|
|
|
|
|
if d, err := cmd.Flags().GetDuration("timeout"); err == nil && d > 0 {
|
|
|
|
fmt.Printf(" (automatic close in %s)", d)
|
|
|
|
go func() {
|
|
|
|
<-time.After(d)
|
|
|
|
c <- os.Interrupt
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-07-31 12:18:31 +00:00
|
|
|
fmt.Println()
|
|
|
|
|
2017-04-14 00:45:38 +00:00
|
|
|
for range c {
|
|
|
|
if err := client.StopTunnel(tun.Name); err != nil {
|
|
|
|
return fmt.Errorf("Unable to stop tunnel %q: %s", tun.Name, err)
|
|
|
|
}
|
|
|
|
fmt.Printf("\nSuccessfully stopped server and tunnel.\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(serveCmd)
|
|
|
|
|
2018-10-15 19:24:17 +00:00
|
|
|
serveCmd.Flags().DurationP("timeout", "t", 0, "Automatically close tunnel after timeout")
|
2019-07-31 12:18:31 +00:00
|
|
|
serveCmd.Flags().String("cors", "", "Enable setting CORS header")
|
|
|
|
serveCmd.Flags().Bool("http-logs", true, "Enable HTTP access logs")
|
2018-10-15 19:24:17 +00:00
|
|
|
|
2017-04-14 00:45:38 +00:00
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
|
|
// and all subcommands, e.g.:
|
|
|
|
// serveCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
|
|
// is called directly, e.g.:
|
|
|
|
// serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
|
|
|
|
|
|
}
|