-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (118 loc) · 2.51 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
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"strconv"
"strings"
)
func main() {
flagH := flag.String("host", "", "By default the FQDN is used, if set, overwrite with `string`")
flagO := flag.Bool("o", false, "Serve file only once and then exit")
flagP := flag.Int("p", 12345, "Port `number` used to share file. This and the next 10 ports will be tried")
flagX := flag.Bool("x", false, "Copy url to clipboard (requires xclip)")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s [OPTION]... [FILE|DIR]:\n", os.Args[0])
flag.PrintDefaults()
os.Exit(1)
}
flag.Parse()
fileArg := flag.Args()
if len(fileArg) != 1 {
flag.Usage()
}
var hostName string
if *flagH != "" {
hostName = *flagH
} else {
if h, err := fqdn(); err != nil {
log.Fatal(err)
} else {
hostName = h
}
}
file := fileArg[0]
info, err := os.Stat(file)
if err != nil {
log.Fatal(err)
}
isDir := info.IsDir()
fileBase := "/"
if !isDir {
fileBase += path.Base(file)
http.HandleFunc(fileBase, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, file)
if *flagO {
os.Exit(0)
}
fmt.Println("Done")
})
}
u, err := url.Parse(fileBase)
if err != nil {
log.Fatal(err)
}
for port := *flagP; port <= *flagP+10; port++ {
url := fmt.Sprintf("http://%s:%d%s", hostName, port, u)
fmt.Println("Serving", file, "as", url)
if *flagX {
if err := xclip(url); err != nil {
log.Println(err)
}
}
pStr := strconv.Itoa(port)
if isDir {
log.Println(http.ListenAndServe(":"+pStr, http.FileServer(http.Dir(file))))
} else {
log.Println(http.ListenAndServe(":"+pStr, nil))
}
}
}
func xclip(url string) error {
cmd := exec.Command("xclip", "-i")
in, err := cmd.StdinPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
if _, err := in.Write([]byte(url)); err != nil {
return err
}
if err := in.Close(); err != nil {
return err
}
return cmd.Wait()
}
func fqdn() (string, error) {
hostname, err := os.Hostname()
if err != nil {
return "", err
}
addrs, err := net.LookupIP(hostname)
if err != nil {
return hostname, nil
}
for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
ip, err := ipv4.MarshalText()
if err != nil {
return hostname, nil
}
hosts, err := net.LookupAddr(string(ip))
if err != nil || len(hosts) == 0 {
return hostname, nil
}
fqdn := hosts[0]
return strings.TrimSuffix(fqdn, "."), nil
}
}
return hostname, nil
}