-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhttpfolder.go
75 lines (69 loc) · 1.45 KB
/
httpfolder.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
package main
import (
"errors"
"flag"
"fmt"
"github.com/abbot/go-http-auth"
"log"
"net"
"net/http"
"os"
)
var ipa string
var port string
var usr string
var passwd string
func main() {
flag.StringVar(&ipa, "i", "", "ip addess to serve on")
flag.StringVar(&port, "p", "8080", "port to listen on")
flag.Parse()
usr = flag.Arg(0)
passwd = flag.Arg(1)
cwd, err := os.Getwd()
ip, err := localIP()
fmt.Printf("Serving: %s at http://%s:%s\n", cwd, ip, port)
if err != nil {
log.Fatal(err)
}
authenticator := auth.NewBasicAuthenticator("Please login.", Secret)
http.HandleFunc("/",
authenticator.Wrap(func(res http.ResponseWriter, req *auth.AuthenticatedRequest) {
FileServer(Dir(cwd)).ServeHTTP(res, &req.Request)
}))
err = http.ListenAndServe((ipa + ":" + port), nil)
if err != nil {
log.Fatal(err)
}
}
func Secret(user, realm string) string {
mymd5 := ""
if user == usr {
mymd5 = string(auth.MD5Crypt([]byte(passwd), []byte("mymysalt"), []byte("$apr1$")))
}
return mymd5
}
func localIP() (net.IP, error) {
tt, err := net.Interfaces()
if err != nil {
return nil, err
}
fmt.Println(tt)
for _, t := range tt {
aa, err := t.Addrs()
if err != nil {
return nil, err
}
for _, a := range aa {
ipnet, ok := a.(*net.IPNet)
if !ok {
continue
}
v4 := ipnet.IP.To4()
if v4 == nil || v4[0] == 127 { // loopback address
continue
}
return v4, nil
}
}
return nil, errors.New("cannot find local IP address")
}