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

126 lines
3.1 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-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 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 {
siteUrl string
token string
}
2015-07-28 17:31:37 +02:00
var external_ip = ""
2015-07-30 15:12:13 +02:00
func Webserver(port int) *WebServer {
2015-07-30 15:21:13 +02:00
webserver := new(WebServer)
webserver.port = port
webserver.client_token = make(map[*gumble.User]string)
webserver.token_client = 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(":"+strconv.Itoa(port), nil)
2015-07-30 15:17:01 +02:00
2015-07-28 22:38:35 +02:00
rand.Seed(time.Now().UnixNano())
2015-07-30 15:21:13 +02:00
return webserver
2015-07-28 15:56:20 +02:00
}
2015-07-30 15:07:46 +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-07-30 14:48:53 +02:00
t, _ := template.ParseFiles("index.html")
2015-07-30 15:03:22 +02:00
t.Execute(w, Page{"http://" + getIP() + ":" + strconv.Itoa(web.port) + "/", r.URL.Path[1:]})
2015-07-30 14:48:53 +02:00
}
}
2015-07-30 15:07:46 +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 15:07:46 +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 {
var url = html.UnescapeString(r.FormValue("value"))
add(uname, url)
2015-07-28 15:56:20 +02:00
}
}
2015-07-30 15:07:46 +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 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 15:28:39 +02:00
func (website *WebServer) GetWebAddress(user *gumble.User) {
2015-07-30 15:27:55 +02:00
if website.client_token[user] != "" {
website.token_client[website.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-30 15:27:55 +02:00
for firstLoop || website.token_client[website.client_token[user]] != nil {
website.client_token[user] = randSeq(10)
2015-07-28 18:07:21 +02:00
firstLoop = false
}
2015-07-30 15:27:55 +02:00
website.token_client[website.client_token[user]] = user
dj.SendPrivateMessage(user, fmt.Sprintf(website_ADDRESS, getIP(), website.client_token[user], getIP(), website.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)
}