2015-04-16 16:57:48 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2015-04-16 22:40:48 +00:00
|
|
|
//"database/sql"
|
2015-04-16 16:57:48 +00:00
|
|
|
"fmt"
|
2015-04-16 22:40:48 +00:00
|
|
|
//_ "github.com/mattn/go-sqlite3"
|
2015-04-16 16:57:48 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2015-04-16 22:40:48 +00:00
|
|
|
"strings"
|
2015-04-16 16:57:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var output_channel chan string
|
|
|
|
|
|
|
|
func StartServer(ch chan string) {
|
|
|
|
output_channel = ch
|
|
|
|
_, err := os.Stat("mapcrafter/index.html")
|
|
|
|
if err == nil {
|
|
|
|
// Looks like mapcrafter is present
|
|
|
|
output_channel <- "* Mapcrafter Directory is Present, routing to /map\n"
|
|
|
|
fs := http.FileServer(http.Dir("mapcrafter"))
|
|
|
|
http.Handle("/map/", http.StripPrefix("/map/", fs))
|
|
|
|
}
|
|
|
|
http.HandleFunc("/api/", serveAPI)
|
|
|
|
http.HandleFunc("/", serveMcMan)
|
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveMcMan(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, htmlHeader("mc_man - Minecraft Manager"))
|
2015-04-16 22:40:48 +00:00
|
|
|
|
|
|
|
the_path := r.URL.Path
|
|
|
|
output_channel <- the_path
|
|
|
|
|
|
|
|
if strings.HasPrefix(the_path, "/login") || the_path == "/" {
|
|
|
|
fmt.Fprintf(w, loginScreen())
|
|
|
|
} else if strings.HasPrefix(the_path, "/dologin") {
|
|
|
|
fmt.Fprintf(w, doLogin(w, r))
|
|
|
|
}
|
|
|
|
|
2015-04-16 16:57:48 +00:00
|
|
|
fmt.Fprintf(w, htmlFooter())
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:40:48 +00:00
|
|
|
func loginScreen() string {
|
|
|
|
return `
|
|
|
|
<form action="/doLogin" method="POST">
|
|
|
|
<input type="text" name="username">
|
|
|
|
<input type="password" name="password">
|
|
|
|
<input type="submit">
|
|
|
|
</form>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
func doLogin(w http.ResponseWriter, r *http.Request) string {
|
|
|
|
ret := "Do Login<br />"
|
|
|
|
ret = ret + r.FormValue("username")
|
|
|
|
ret = ret + r.FormValue("password")
|
|
|
|
return ret
|
|
|
|
/*
|
|
|
|
//r.F
|
|
|
|
db, err := sql.Open("sqlite3", "mc_man.db")
|
|
|
|
if err == nil {
|
|
|
|
// Error opening the DB, can't log in
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// rows, err := db.Query("SELECT * FROM USERS")
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2015-04-16 16:57:48 +00:00
|
|
|
func serveAPI(w http.ResponseWriter, r *http.Request) {
|
2015-04-16 22:40:48 +00:00
|
|
|
_, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
|
|
|
|
//body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
|
2015-04-16 16:57:48 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := r.Body.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
// What are we doing with this request?
|
|
|
|
output_channel <- fmt.Sprint("HTTP Request: (", r.Method, ") ", r.URL, "\n")
|
2015-04-16 22:40:48 +00:00
|
|
|
the_path := r.URL.Path
|
|
|
|
output_string := ""
|
2015-04-16 16:57:48 +00:00
|
|
|
if strings.HasPrefix(the_path, "/api") {
|
|
|
|
the_path = strings.TrimPrefix(the_path, "/api")
|
|
|
|
if strings.HasPrefix(the_path, "/v1") {
|
|
|
|
the_path = strings.TrimPrefix(the_path, "/v1")
|
|
|
|
if strings.HasPrefix(the_path, "/whitelist") {
|
|
|
|
output_string = handleWhitelist(r)
|
|
|
|
} else if strings.HasPrefix(the_path, "/ops") {
|
|
|
|
output_string = handleOps(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-16 22:40:48 +00:00
|
|
|
fmt.Fprintf(w, output_string)
|
2015-04-16 16:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* JSON Functions */
|
|
|
|
func handleOps(r *http.Request) string {
|
|
|
|
if r.Method == "GET" {
|
|
|
|
return getOps()
|
|
|
|
} else if r.Method == "POST" {
|
|
|
|
// Add posted user to Ops
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWhitelist(r *http.Request) string {
|
|
|
|
if r.Method == "GET" {
|
|
|
|
return getWhitelist()
|
|
|
|
} else if r.Method == "POST" {
|
|
|
|
// Add posted user to whitelist
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func getOps() string {
|
|
|
|
ret := "["
|
|
|
|
num_users := 0
|
|
|
|
for _, op_user := range GetConfig().Ops {
|
|
|
|
if num_users > 0 {
|
|
|
|
ret += ","
|
|
|
|
}
|
|
|
|
ret += fmt.Sprint("\"", op_user, "\"")
|
|
|
|
}
|
|
|
|
ret += "]"
|
|
|
|
return ret
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getWhitelist() string {
|
|
|
|
ret := "["
|
|
|
|
num_users := 0
|
|
|
|
for _, wl_user := range GetConfig().Whitelist {
|
|
|
|
if num_users > 0 {
|
|
|
|
ret += ","
|
|
|
|
}
|
|
|
|
ret += fmt.Sprint("\"", wl_user, "\"")
|
|
|
|
}
|
|
|
|
ret += "]"
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
/* HTML Functions */
|
|
|
|
func htmlHeader(title string) string {
|
|
|
|
head := `
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>`
|
|
|
|
head += title
|
|
|
|
head += `
|
|
|
|
</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
`
|
|
|
|
return head
|
|
|
|
}
|
|
|
|
|
|
|
|
func htmlFooter() string {
|
|
|
|
return `
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
}
|