-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (109 loc) · 2.66 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
package main
import (
"errors"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
)
func isCurl(useragent string) bool {
return regexp.MustCompile(`^curl\/`).MatchString(useragent)
}
func isWget(useragent string) bool {
return regexp.MustCompile(`^Wget\/`).MatchString(useragent)
}
func isPowerShell(useragent string) bool {
return regexp.MustCompile(`PowerShell\/`).MatchString(useragent)
}
func getLatestRelease(owner string, repo string) (string, error) {
var (
err error
)
url := fmt.Sprintf("https://github.com/%s/%s/releases", owner, repo)
res, err := http.Get(url)
if err != nil {
return "", err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
reg := regexp.MustCompile(fmt.Sprintf(`/%s/%s/releases/download/(.*)/.*\.tar.gz`, owner, repo))
// reg := regexp.MustCompile(fmt.Sprintf(`/%s/%s/releases/tag/v([a-z\d\.]+)`, owner, repo))
matchers := reg.FindStringSubmatch(string(body))
if len(matchers) == 0 {
return "", errors.New("not found latest version")
}
version := matchers[1]
return version, nil
}
func handler(w http.ResponseWriter, r *http.Request) error {
var (
err error
version = r.URL.Query().Get("v")
binaryName = r.URL.Query().Get("bin")
)
arr := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
if len(arr) != 2 {
w.WriteHeader(http.StatusNotFound)
return nil
}
owner := arr[0]
repo := strings.Join(arr[1:], "/")
userAgent := r.Header.Get("user-agent")
filename := "install.sh"
if isCurl(userAgent) || isWget(userAgent) {
filename = "install.sh"
} else if isPowerShell(userAgent) {
filename = "install.ps1"
}
// If no version is specified, the latest version is used
if version == "" {
version, err = getLatestRelease(owner, repo)
if err != nil {
return err
}
}
if !strings.HasPrefix(version, "v") {
version = "v" + version
}
if binaryName == "" {
binaryName = repo
}
t := template.New(filename)
w.Header().Set("Content-Type", "text/x-shellscript")
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
t, err = t.Parse(string(b))
if err != nil {
return err
}
err = t.Execute(w, map[string]interface{}{
"Owner": owner,
"Repo": repo,
"Version": version,
"Binary": binaryName,
})
return err
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
err := handler(res, req)
if err != nil {
res.WriteHeader(http.StatusBadRequest)
_, _ = res.Write([]byte(err.Error()))
}
})
fmt.Printf("Listen on port %s\n", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}