2015-07-30 18:28:01 +02:00
|
|
|
package main
|
2015-07-28 15:56:20 +02:00
|
|
|
|
|
|
|
import (
|
2015-08-04 00:06:32 +02:00
|
|
|
//"encoding/json"
|
2015-07-28 15:56:20 +02:00
|
|
|
"fmt"
|
2015-07-28 22:43:46 +02:00
|
|
|
"html"
|
2015-07-30 15:01:55 +02:00
|
|
|
"html/template"
|
2015-07-28 15:56:20 +02:00
|
|
|
"io/ioutil"
|
2015-07-28 17:52:53 +02:00
|
|
|
"math/rand"
|
2015-07-28 15:56:20 +02:00
|
|
|
"net/http"
|
2015-07-30 18:32:56 +02:00
|
|
|
"os"
|
2015-07-30 14:58:31 +02:00
|
|
|
"strconv"
|
2015-07-28 17:52:53 +02:00
|
|
|
"strings"
|
2015-07-28 17:59:55 +02:00
|
|
|
"time"
|
2015-07-28 17:32:36 +02:00
|
|
|
|
|
|
|
"github.com/layeh/gumble/gumble"
|
2015-07-28 15:56:20 +02:00
|
|
|
)
|
|
|
|
|
2015-07-30 14:48:53 +02:00
|
|
|
type WebServer struct {
|
|
|
|
port int
|
|
|
|
client_token map[*gumble.User]string
|
|
|
|
token_client map[string]*gumble.User
|
|
|
|
}
|
|
|
|
|
|
|
|
type Page struct {
|
2015-07-30 18:43:08 +02:00
|
|
|
Site string
|
|
|
|
Token string
|
|
|
|
User string
|
2015-07-30 14:48:53 +02:00
|
|
|
}
|
|
|
|
|
2015-08-03 23:18:49 +02:00
|
|
|
type Status struct {
|
2015-08-02 19:55:51 +02:00
|
|
|
Error bool
|
|
|
|
ErrorMsg string
|
|
|
|
Queue []SongInfo
|
|
|
|
}
|
|
|
|
type SongInfo struct {
|
|
|
|
TitleID string
|
|
|
|
PlaylistID string
|
|
|
|
Title string
|
|
|
|
Playlist string
|
|
|
|
Submitter string
|
|
|
|
Duration string
|
|
|
|
Thumbnail string
|
|
|
|
}
|
|
|
|
|
2015-07-28 17:31:37 +02:00
|
|
|
var external_ip = ""
|
|
|
|
|
2015-07-30 16:34:40 +02:00
|
|
|
func NewWebServer(port int) *WebServer {
|
2015-07-30 18:50:49 +02:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2015-07-30 16:34:40 +02:00
|
|
|
return &WebServer{
|
|
|
|
port: port,
|
|
|
|
client_token: make(map[*gumble.User]string),
|
|
|
|
token_client: make(map[string]*gumble.User),
|
|
|
|
}
|
|
|
|
}
|
2015-07-30 15:17:01 +02:00
|
|
|
|
2015-07-30 16:34:40 +02:00
|
|
|
func (web *WebServer) makeWeb() {
|
|
|
|
http.HandleFunc("/", web.homepage)
|
2015-08-02 19:55:51 +02:00
|
|
|
http.HandleFunc("/api/add", web.add)
|
|
|
|
http.HandleFunc("/api/volume", web.volume)
|
|
|
|
http.HandleFunc("/api/skip", web.skip)
|
2015-08-03 23:59:37 +02:00
|
|
|
//http.HandleFunc("/api/status", web.status)
|
2015-07-30 16:34:40 +02:00
|
|
|
http.ListenAndServe(":"+strconv.Itoa(web.port), nil)
|
2015-07-28 15:56:20 +02:00
|
|
|
}
|
|
|
|
|
2015-07-30 16:28:41 +02:00
|
|
|
func (web *WebServer) homepage(w http.ResponseWriter, r *http.Request) {
|
2015-07-30 14:58:31 +02:00
|
|
|
var uname = web.token_client[r.URL.Path[1:]]
|
2015-07-28 22:43:02 +02:00
|
|
|
if uname == nil {
|
2015-07-28 22:38:35 +02:00
|
|
|
fmt.Fprintf(w, "Invalid Token")
|
|
|
|
} else {
|
2015-08-02 19:55:51 +02:00
|
|
|
var webpage = uname.Name
|
|
|
|
|
|
|
|
// Check to see if user has a custom webpage
|
|
|
|
if _, err := os.Stat(fmt.Sprintf("%s/.mumbledj/web/%s.html", dj.homeDir, uname.Name)); os.IsNotExist(err) {
|
|
|
|
webpage = "index"
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := template.ParseFiles(fmt.Sprintf("%s/.mumbledj/songs/%s.html", dj.homeDir, uname.Name))
|
2015-07-30 16:52:10 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2015-07-30 16:54:46 +02:00
|
|
|
|
2015-07-30 18:50:49 +02:00
|
|
|
err = t.Execute(w, Page{getIP() + ":" + strconv.Itoa(web.port), r.URL.Path[1:], uname.Name})
|
2015-07-30 16:52:10 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2015-07-30 14:48:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 16:28:41 +02:00
|
|
|
func (web *WebServer) add(w http.ResponseWriter, r *http.Request) {
|
2015-07-30 14:58:31 +02:00
|
|
|
var uname = web.token_client[r.FormValue("token")]
|
2015-07-30 14:48:53 +02:00
|
|
|
if uname == nil {
|
|
|
|
fmt.Fprintf(w, "Invalid Token")
|
|
|
|
} else {
|
|
|
|
add(uname, html.UnescapeString(r.FormValue("value")))
|
2015-07-30 19:13:58 +02:00
|
|
|
fmt.Fprintf(w, "Success")
|
2015-07-30 14:48:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 16:28:41 +02:00
|
|
|
func (web *WebServer) volume(w http.ResponseWriter, r *http.Request) {
|
2015-07-30 14:58:31 +02:00
|
|
|
var uname = web.token_client[r.FormValue("token")]
|
2015-07-30 14:48:53 +02:00
|
|
|
if uname == nil {
|
|
|
|
fmt.Fprintf(w, "Invalid Token")
|
|
|
|
} else {
|
2015-07-30 19:13:58 +02:00
|
|
|
var vol = html.UnescapeString(r.FormValue("value"))
|
|
|
|
volume(uname, vol)
|
|
|
|
fmt.Fprintf(w, "Success")
|
2015-07-28 15:56:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 16:28:41 +02:00
|
|
|
func (web *WebServer) skip(w http.ResponseWriter, r *http.Request) {
|
2015-07-30 14:58:31 +02:00
|
|
|
var uname = web.token_client[r.FormValue("token")]
|
2015-07-28 22:43:02 +02:00
|
|
|
if uname == nil {
|
2015-07-28 22:38:35 +02:00
|
|
|
fmt.Fprintf(w, "Invalid Token")
|
2015-07-28 17:52:53 +02:00
|
|
|
} else {
|
2015-07-30 19:14:34 +02:00
|
|
|
value := html.UnescapeString(r.FormValue("value"))
|
|
|
|
playlist, err := strconv.ParseBool(value)
|
2015-07-30 19:13:58 +02:00
|
|
|
if err == nil {
|
|
|
|
skip(uname, false, playlist)
|
|
|
|
fmt.Fprintf(w, "Success")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "Invalid Value")
|
|
|
|
}
|
2015-07-28 17:52:53 +02:00
|
|
|
}
|
2015-07-28 15:56:20 +02:00
|
|
|
}
|
|
|
|
|
2015-08-03 23:59:37 +02:00
|
|
|
//func (web *WebServer) status(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// var uname = web.token_client[r.FormValue("token")]
|
|
|
|
// if uname == nil {
|
|
|
|
// str, ok := json.Marshal(&Status{true, "Invalid Token"}).(string)
|
|
|
|
// fmt.Fprintf(w, str)
|
|
|
|
// } else {
|
|
|
|
// // Generate song queue
|
|
|
|
// queueLength := dj.queue.Len()
|
|
|
|
// var songsInQueue [queueLength]SongInfo
|
|
|
|
// for i := 0; i < dj.queue.Len(); i++ {
|
|
|
|
// songItem := dj.queue.Get(i)
|
|
|
|
// songsInQueue[i] = &SongInfo{
|
|
|
|
// TitleID: songItem.ID(),
|
|
|
|
// Title: songItem.Title(),
|
|
|
|
// Submitter: songItem.Submitter(),
|
|
|
|
// Duration: songItem.Duration(),
|
|
|
|
// Thumbnail: songItem.Thumbnail(),
|
|
|
|
// }
|
|
|
|
// if !isNil(songItem.Playlist()) {
|
|
|
|
// songsInQueue[i].PlaylistID = songItem.Playlist().ID()
|
|
|
|
// songsInQueue[i].Playlist = songItem.Playlist().Title()
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // Output status
|
|
|
|
// fmt.Fprintf(w, string(json.MarshalIndent(&Status{false, "", songsInQueue})))
|
|
|
|
// }
|
|
|
|
//}
|
2015-08-02 19:55:51 +02:00
|
|
|
|
2015-07-30 16:28:41 +02:00
|
|
|
func (website *WebServer) GetWebAddress(user *gumble.User) {
|
2015-07-30 16:12:00 +02:00
|
|
|
Verbose("Port number: " + strconv.Itoa(web.port))
|
2015-07-30 16:07:40 +02:00
|
|
|
if web.client_token[user] != "" {
|
|
|
|
web.token_client[web.client_token[user]] = nil
|
2015-07-28 17:52:53 +02:00
|
|
|
}
|
2015-07-28 18:07:21 +02:00
|
|
|
// dealing with collisions
|
|
|
|
var firstLoop = true
|
2015-08-02 19:55:51 +02:00
|
|
|
for firstLoop || web.token_client[web.client_token[user]] != nil || web.client_token[user] == "api" {
|
2015-07-30 16:07:40 +02:00
|
|
|
web.client_token[user] = randSeq(10)
|
2015-07-28 18:07:21 +02:00
|
|
|
firstLoop = false
|
|
|
|
}
|
2015-07-30 16:07:40 +02:00
|
|
|
web.token_client[web.client_token[user]] = user
|
|
|
|
dj.SendPrivateMessage(user, fmt.Sprintf(WEB_ADDRESS, getIP(), web.client_token[user], getIP(), web.client_token[user]))
|
2015-07-28 17:31:37 +02:00
|
|
|
}
|
|
|
|
|
2015-07-28 22:38:35 +02:00
|
|
|
// Gets the external ip address for the server
|
2015-07-28 17:31:37 +02:00
|
|
|
func getIP() string {
|
|
|
|
if external_ip != "" {
|
|
|
|
return external_ip
|
|
|
|
} else {
|
|
|
|
if response, err := http.Get("http://myexternalip.com/raw"); err == nil {
|
|
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode == 200 {
|
|
|
|
if body, err := ioutil.ReadAll(response.Body); err == nil {
|
2015-07-28 17:52:53 +02:00
|
|
|
external_ip = strings.TrimSpace(string(body))
|
2015-07-28 17:31:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return external_ip
|
|
|
|
}
|
|
|
|
}
|
2015-07-28 17:52:53 +02:00
|
|
|
|
2015-07-28 22:38:35 +02:00
|
|
|
// Generates a pseudorandom string of characters
|
2015-07-28 17:52:53 +02:00
|
|
|
func randSeq(n int) string {
|
2015-07-28 22:38:35 +02:00
|
|
|
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
2015-07-28 17:52:53 +02:00
|
|
|
b := make([]rune, n)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = letters[rand.Intn(len(letters))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|