2017-04-04 21:29:18 +05:30
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-04-04 15:55:00 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/subtle"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
func basicAuth(handler http.Handler, username, password, realm string) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user, pass, ok := r.BasicAuth() // pull creds from the request
|
|
|
|
|
|
|
|
// check user and pass securely
|
|
|
|
if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(password)) != 1 {
|
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
|
|
|
|
w.WriteHeader(401)
|
|
|
|
w.Write([]byte("Unauthorised.\n"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2017-04-05 23:01:34 +05:30
|
|
|
http.Handle("/api/submit", &submitServer{})
|
2017-04-04 15:55:00 +05:30
|
|
|
|
|
|
|
// Make sure bugs directory exists
|
|
|
|
_ = os.Mkdir("bugs", os.ModePerm)
|
|
|
|
|
|
|
|
// serve files under "bugs"
|
2017-04-06 16:50:07 +05:30
|
|
|
ls := &logServer{"bugs"}
|
|
|
|
fs := http.StripPrefix("/api/listing/", ls)
|
2017-04-04 15:55:00 +05:30
|
|
|
|
|
|
|
// set auth if env vars exist
|
|
|
|
usr := os.Getenv("BUGS_USER")
|
|
|
|
pass := os.Getenv("BUGS_PASS")
|
|
|
|
if usr == "" || pass == "" {
|
|
|
|
fmt.Println("BUGS_USER and BUGS_PASS env vars not found. No authentication is running for /api/listing")
|
|
|
|
} else {
|
|
|
|
fs = basicAuth(fs, usr, pass, "Riot bug reports")
|
|
|
|
}
|
|
|
|
http.Handle("/api/listing/", fs)
|
|
|
|
|
|
|
|
port := os.Args[1]
|
2017-04-06 16:50:07 +05:30
|
|
|
log.Println("Listening on port", port)
|
|
|
|
|
2017-04-04 15:55:00 +05:30
|
|
|
log.Fatal(http.ListenAndServe(":"+port, nil))
|
|
|
|
}
|