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

85 lines
2 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-28 22:38:35 +02:00
var client_token = make(map[*gumble.User]string)
var token_client = make(map[string]*gumble.User)
2015-07-28 17:31:37 +02:00
var external_ip = ""
2015-07-28 22:38:35 +02:00
func Webserver() {
http.HandleFunc("/", homepage)
http.HandleFunc("/add", addSong)
2015-07-28 22:56:07 +02:00
http.ListenAndServe(":80", nil)
2015-07-28 22:38:35 +02:00
rand.Seed(time.Now().UnixNano())
2015-07-28 15:56:20 +02:00
}
2015-07-28 22:38:35 +02:00
func 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 {
fmt.Fprintf(w, "Hang in there %s, I haven't made the website yet!", uname.Name)
2015-07-28 15:56:20 +02:00
}
}
2015-07-28 22:38:35 +02:00
func addSong(w http.ResponseWriter, r *http.Request) {
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-28 22:47:05 +02:00
var url = html.UnescapeString(r.FormValue("url"))
2015-07-28 22:48:20 +02:00
fmt.Fprintf(w, url)
2015-07-28 17:52:53 +02:00
}
2015-07-28 15:56:20 +02:00
}
2015-07-28 17:31:37 +02:00
func 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)
}