2024-05-06 07:20:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-05-28 23:30:59 +00:00
|
|
|
"fmt"
|
2024-06-23 13:43:33 +00:00
|
|
|
"net"
|
2024-05-06 07:20:30 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func healthcheck(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte("ok"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func reloadRules(c *Config) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := c.LoadMappingFile("")
|
|
|
|
if err != nil {
|
2024-06-23 13:43:33 +00:00
|
|
|
e := fmt.Errorf("cannot reload rules: %+v", err)
|
2024-05-28 23:30:59 +00:00
|
|
|
http.Error(w, e.Error(), http.StatusInternalServerError)
|
2024-05-06 07:20:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write([]byte("ok"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveRules(c *Config) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-06-23 13:43:33 +00:00
|
|
|
var nameOfPkg string
|
2024-05-06 07:20:30 +00:00
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// if go-get param is absent, return nothing
|
|
|
|
if r.FormValue("go-get") != "1" {
|
2024-06-23 13:43:33 +00:00
|
|
|
data, err := getDefaultHtml()
|
2024-05-28 23:30:59 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(data)
|
2024-05-06 07:20:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-23 13:43:33 +00:00
|
|
|
ipFragments := strings.Split(r.Host, ":")
|
|
|
|
if len(ipFragments) > 1 {
|
|
|
|
ipAddr := net.ParseIP(ipFragments[0])
|
|
|
|
if ipAddr.IsLoopback() {
|
|
|
|
nameOfPkg = ipAddr.String() + r.URL.Path
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nameOfPkg = r.Host + r.URL.Path
|
|
|
|
}
|
2024-05-06 07:20:30 +00:00
|
|
|
|
2024-06-23 13:43:33 +00:00
|
|
|
var found bool
|
2024-05-06 07:20:30 +00:00
|
|
|
var vanityUrl, proto, repoUrl string
|
|
|
|
for _, rule := range c.MappingRules.Mappings {
|
2024-05-28 23:30:59 +00:00
|
|
|
if strings.HasPrefix(strings.ToLower(rule.VanityUrl+"/"), strings.Trim(strings.ToLower(nameOfPkg), " ")) {
|
|
|
|
vanityUrl = rule.VanityUrl
|
|
|
|
repoUrl = rule.RealUrl
|
2024-05-06 07:20:30 +00:00
|
|
|
proto = rule.Protocol
|
2024-06-23 13:43:33 +00:00
|
|
|
found = true
|
2024-05-06 07:20:30 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 13:43:33 +00:00
|
|
|
if !found {
|
|
|
|
data, err := getDefaultHtml()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(data)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-06 07:20:30 +00:00
|
|
|
d := ImportRuleStruct{
|
|
|
|
VanityUrl: vanityUrl,
|
|
|
|
Proto: proto,
|
|
|
|
RepoUrl: repoUrl,
|
|
|
|
}
|
2024-06-23 13:43:33 +00:00
|
|
|
tmpl, err := getServeHtml()
|
2024-05-28 23:30:59 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-05-06 07:20:30 +00:00
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2024-05-28 23:30:59 +00:00
|
|
|
err = tmpl.Execute(&buf, &d)
|
2024-05-06 07:20:30 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=500")
|
|
|
|
w.Write(buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|