forked from schollz/hostyoself
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (110 loc) · 2.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
//go:generate go get -v github.com/jteeuwen/go-bindata/go-bindata
//go:generate go-bindata -pkg server -o pkg/server/assets.go templates/ static/
import (
"fmt"
"math/rand"
"os"
"runtime"
"strings"
"time"
"github.com/schollz/hostyoself/pkg/client"
"github.com/schollz/hostyoself/pkg/server"
log "github.com/schollz/logger"
"github.com/urfave/cli"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
var Version string
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
app := cli.NewApp()
app.Name = "hostyoself"
if Version == "" {
Version = "v0.0.0"
}
app.Version = Version
app.Compiled = time.Now()
app.Usage = "host your files using websockets from the command line or a browser"
app.UsageText = "use to transfer files or host an impromptu website"
app.Commands = []cli.Command{
{
Name: "relay",
Usage: "start a relay",
Description: "relay is used to transit files",
Flags: []cli.Flag{
cli.StringFlag{Name: "url, u", Value: "localhost", Usage: "public URL to use"},
cli.StringFlag{Name: "port", Value: "8010", Usage: "ports of the local relay"},
},
HelpName: "hostyoself relay",
Action: func(c *cli.Context) error {
return relay(c)
},
},
{
Name: "host",
Usage: "host files from your computer",
Description: "host files from your computer",
HelpName: "hostyoself relay",
Flags: []cli.Flag{
cli.StringFlag{Name: "url, u", Value: "https://hostyoself.com", Usage: "URL of relay to connect"},
cli.StringFlag{Name: "domain, d", Value: "", Usage: "domain to use (default is random)"},
cli.StringFlag{Name: "key, k", Value: "", Usage: "key value to use (default is random)"},
cli.StringFlag{Name: "folder, f", Value: ".", Usage: "folder to serve files"},
},
Action: func(c *cli.Context) error {
return host(c)
},
},
}
app.Flags = []cli.Flag{
cli.BoolFlag{Name: "debug", Usage: "increase verbosity"},
}
app.EnableBashCompletion = true
app.HideHelp = false
app.HideVersion = false
app.BashComplete = func(c *cli.Context) {
fmt.Fprintf(c.App.Writer, "host\nrelay")
}
err := app.Run(os.Args)
if err != nil {
log.Debug(err)
}
}
func host(c *cli.Context) (err error) {
if c.GlobalBool("debug") {
log.SetLevel("debug")
} else {
log.SetLevel("info")
}
cl, err := client.New(c.String("domain"), c.String("key"), c.String("url"), c.String("folder"))
if err != nil {
return
}
for {
log.Info("serving forever")
err = cl.Run()
if err != nil {
log.Debug(err)
}
log.Infof("server disconnected, retrying in 10 seconds")
time.Sleep(10 * time.Second)
}
}
func relay(c *cli.Context) (err error) {
if c.GlobalBool("debug") {
log.SetLevel("debug")
} else {
log.SetLevel("info")
}
flagPublicURL := c.String("url")
if flagPublicURL == "localhost" {
flagPublicURL += ":" + c.String("port")
}
if !strings.HasPrefix(flagPublicURL, "http") {
flagPublicURL = "http://" + flagPublicURL
}
s := server.New(flagPublicURL, c.String("port"))
return s.Run()
}