-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
190 lines (163 loc) · 4.01 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
"strconv"
"time"
"github.com/skip2/go-qrcode"
)
const (
debug = false
version = "4.3"
defaultPort = "8001"
authPostPath = "/authp"
)
const usages string = `Usage of goshare:
Share specifed directy to the localnetwork.
OPTIONS:
-d <directory_name>
the directory for sharing (default ".")
-p <password>
password (default is no password)
-s
don't show status, be silent
--noqr
don't show qrcode
--noup
don't allow uploads or making directories
--nozip
don't allow zipping
--port <port_number>
port number (default "` + defaultPort + `")
--version
show version number
EXAMPLES
goshare -d "fo/bar/bazz" -p "777"
share "fo/bar/bazz" directory. password would be "777"
`
var (
rootDir, port, password string
dontAllowUploads, dontAllowZipping, dontShowStat, dontShowQr bool
)
func flagParse() {
flag.StringVar(&rootDir, "d", ".", "the directory for sharing")
flag.StringVar(&port, "port", defaultPort, "port number")
flag.StringVar(&password, "p", "", "password")
flag.BoolVar(&dontShowStat, "s", false, "don't show request information. aka silent")
flag.BoolVar(&dontShowQr, "noqr", false, "don't show qr")
flag.BoolVar(&dontAllowUploads, "noup", false, "don't allow uploads")
flag.BoolVar(&dontAllowZipping, "nozip", false, "don't allow zipping")
v := flag.Bool("version", false, "show version number")
flag.Usage = func() { fmt.Print(usages) }
flag.Parse()
if *v {
mode := "release"
if debug {
mode = "debug"
}
fmt.Printf("goshare version v%s %s\n", version, mode)
os.Exit(0)
}
if port == "" {
log.Fatal("port number can't be empty")
}
if rootDir == "" {
log.Fatal("directory name can't be empty")
}
}
func localIp() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "localhost"
}
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
return ipNet.IP.String()
}
}
return "localhost"
}
func main() {
flagParse()
fmt.Println("Starting server")
sv := newServer()
sv.showStat = false
go sv.cleanup()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/browse/", http.StatusMovedPermanently)
})
// don't chage the /browse/ ok it will break suff
http.HandleFunc("/auth", sv.auth)
// work aoround
http.HandleFunc(authPostPath, func(w http.ResponseWriter, r *http.Request) {
r.Method = http.MethodPost
sv.auth(w, r)
})
http.HandleFunc("/browse/", sv.authBrowse)
http.HandleFunc("/zip", sv.authZip)
http.HandleFunc("/downzip/", sv.authDownZip)
http.HandleFunc("/upload", sv.authUpload)
http.HandleFunc("/mkdir", sv.authMkdir)
http.HandleFunc("/static/", sv.authServeStaticFilese)
if debug {
fmt.Printf("Running in debug mode version: %s\n", version)
}
serveMsgStr := "Serving at http://%s:%s"
if password != "" {
fmt.Printf("Password is: %s\n", password)
serveMsgStr += authPostPath + "?password=" + url.QueryEscape(password)
}
// port number
pn := 8001
lIP := localIp()
c := make(chan struct{})
var err error
loop:
for range 10 {
var p string
go func() {
if port != defaultPort {
p = port
} else {
p = strconv.Itoa(pn)
}
err = http.ListenAndServe(":"+p, nil)
c <- struct{}{}
}()
select {
case <-c:
if port == "" {
break loop
}
pn++ // next port
continue loop
case <-time.Tick(1 * time.Second):
m := fmt.Sprintf("http://%s:%s", lIP, p)
fmt.Println(m + "\n")
if !dontShowQr {
var err error
q, err := qrcode.New(m, qrcode.Medium)
if err == nil {
fmt.Println(q.ToSmallString(false))
}
}
fmt.Println()
sv.showStat = !dontShowStat
err = nil
break loop
}
}
if err != nil {
fmt.Printf("\nwhile serving err: %v\n", err)
fmt.Printf("Hint: most likely the issue is, the port is alredy in use\n")
fmt.Printf("use `--port 8002` to specefy another port\n")
os.Exit(1)
}
select {}
}