This repository has been archived on 2019-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mumbledj/web.go

118 lines
2.9 KiB
Go
Raw Normal View History

2015-07-28 15:57:12 +02:00
package main
2015-07-28 15:56:20 +02:00
import (
"fmt"
2015-07-28 22:43:46 +02:00
"html"
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-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 {
siteUrl string
token string
}
2015-07-28 17:31:37 +02:00
var external_ip = ""
2015-07-30 14:48:53 +02:00
func Webserver(port int) *WebServer {
var webserver = WebServer{port, make(map[*gumble.User]string), make(map[string]*gumble.User)}
http.HandleFunc("/", webServer.homepage)
http.HandleFunc("/add", webserver.add)
http.HandleFunc("/volume", webserver.volume)
http.HandleFunc("/skip", webserver.skip)
http.ListenAndServe(":"+port, nil)
2015-07-28 22:38:35 +02:00
rand.Seed(time.Now().UnixNano())
2015-07-30 14:48:53 +02:00
return &webserver
2015-07-28 15:56:20 +02:00
}
2015-07-30 14:48:53 +02:00
func (w WebServer) homepage(w http.ResponseWriter, r *http.Request) {
2015-07-28 22:56:07 +02:00
var uname = 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-07-30 14:48:53 +02:00
t, _ := template.ParseFiles("index.html")
t.Execute(w, Page{"http://" + getIp() + ":" + w.port + "/", r.URL.Path[1:]})
}
}
func (w WebServer) add(w http.ResponseWriter, r *http.Request) {
var uname = token_client[r.FormValue("token")]
if uname == nil {
fmt.Fprintf(w, "Invalid Token")
} else {
add(uname, html.UnescapeString(r.FormValue("value")))
}
}
func (w WebServer) volume(w http.ResponseWriter, r *http.Request) {
var uname = token_client[r.FormValue("token")]
if uname == nil {
fmt.Fprintf(w, "Invalid Token")
} else {
var url = html.UnescapeString(r.FormValue("value"))
add(uname, url)
2015-07-28 15:56:20 +02:00
}
}
2015-07-30 14:48:53 +02:00
func (w WebServer) skip(w http.ResponseWriter, r *http.Request) {
2015-07-28 22:38:35 +02:00
var uname = 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 14:48:53 +02:00
var url = html.UnescapeString(r.FormValue("value"))
2015-07-29 03:04:32 +02:00
add(uname, url)
2015-07-28 17:52:53 +02:00
}
2015-07-28 15:56:20 +02:00
}
2015-07-30 14:48:53 +02:00
func (w WebServer) GetWebAddress(user *gumble.User) {
2015-07-28 22:38:35 +02:00
if client_token[user] != "" {
2015-07-28 22:43:02 +02:00
token_client[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-07-28 22:43:02 +02:00
for firstLoop || token_client[client_token[user]] != nil {
2015-07-28 22:38:35 +02:00
client_token[user] = randSeq(10)
2015-07-28 18:07:21 +02:00
firstLoop = false
}
2015-07-28 22:38:35 +02:00
token_client[client_token[user]] = user
dj.SendPrivateMessage(user, fmt.Sprintf(WEB_ADDRESS, getIP(), client_token[user], getIP(), 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)
}