Improving web authentication
This commit is contained in:
parent
84267809ca
commit
a1f2066532
37
commands.go
37
commands.go
|
@ -36,7 +36,7 @@ func parseCommand(user *gumble.User, username, command string) {
|
|||
// Add command
|
||||
case dj.conf.Aliases.AddAlias:
|
||||
if dj.HasPermission(username, dj.conf.Permissions.AdminAdd) {
|
||||
add(user, username, argument)
|
||||
add(user, argument)
|
||||
} else {
|
||||
dj.SendPrivateMessage(user, NO_PERMISSION_MSG)
|
||||
}
|
||||
|
@ -166,40 +166,15 @@ func parseCommand(user *gumble.User, username, command string) {
|
|||
|
||||
// add performs !add functionality. Checks input URL for service, and adds
|
||||
// the URL to the queue if the format matches.
|
||||
func add(user *gumble.User, username, url string) {
|
||||
func add(user *gumble.User, url string) {
|
||||
if url == "" {
|
||||
dj.SendPrivateMessage(user, NO_ARGUMENT_MSG)
|
||||
} else {
|
||||
var urlService Service
|
||||
|
||||
// Checks all services to see if any can take the URL
|
||||
for _, service := range services {
|
||||
if service.URLRegex(url) {
|
||||
urlService = service
|
||||
}
|
||||
}
|
||||
|
||||
if urlService == nil {
|
||||
dj.SendPrivateMessage(user, INVALID_URL_MSG)
|
||||
title, err := findServiceAndAdd(user, url)
|
||||
if err == nil {
|
||||
dj.client.Self.Channel.Send(fmt.Sprintf(SONG_ADDED_HTML, user.Name, title), false)
|
||||
} else {
|
||||
oldLength := dj.queue.Len()
|
||||
|
||||
if err := urlService.NewRequest(user, url); err == nil {
|
||||
dj.client.Self.Channel.Send(SONG_ADDED_HTML, false)
|
||||
|
||||
// Starts playing the new song if nothing else is playing
|
||||
if oldLength == 0 && dj.queue.Len() != 0 && !dj.audioStream.IsPlaying() {
|
||||
if err := dj.queue.CurrentSong().Download(); err == nil {
|
||||
dj.queue.CurrentSong().Play()
|
||||
} else {
|
||||
dj.SendPrivateMessage(user, AUDIO_FAIL_MSG)
|
||||
dj.queue.CurrentSong().Delete()
|
||||
dj.queue.OnSongFinished()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
dj.SendPrivateMessage(user, err.Error())
|
||||
}
|
||||
dj.SendPrivateMessage(user, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
36
service.go
36
service.go
|
@ -8,14 +8,15 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/layeh/gumble/gumble"
|
||||
)
|
||||
|
||||
// Service interface. Each service should implement these functions
|
||||
type Service interface {
|
||||
ServiceName() string
|
||||
URLRegex(string) bool // Can service deal with URL
|
||||
NewRequest(*gumble.User, string) error // Create song/playlist and add to the queue
|
||||
URLRegex(string) bool // Can service deal with URL
|
||||
NewRequest(*gumble.User, string) (string, error) // Create song/playlist and add to the queue
|
||||
}
|
||||
|
||||
// Song interface. Each service will implement these
|
||||
|
@ -50,3 +51,34 @@ type Playlist interface {
|
|||
}
|
||||
|
||||
var services = []Service{YouTube{}}
|
||||
|
||||
func findServiceAndAdd(user *gumble.User, url string) (string, error) {
|
||||
var urlService Service
|
||||
|
||||
// Checks all services to see if any can take the URL
|
||||
for _, service := range services {
|
||||
if service.URLRegex(url) {
|
||||
urlService = service
|
||||
}
|
||||
}
|
||||
|
||||
if urlService == nil {
|
||||
return nil, errors.New("INVALID_URL")
|
||||
} else {
|
||||
oldLength := dj.queue.Len()
|
||||
if title, err := urlService.NewRequest(user, url); err == nil {
|
||||
|
||||
// Starts playing the new song if nothing else is playing
|
||||
if oldLength == 0 && dj.queue.Len() != 0 && !dj.audioStream.IsPlaying() {
|
||||
if err := dj.queue.CurrentSong().Download(); err == nil {
|
||||
dj.queue.CurrentSong().Play()
|
||||
} else {
|
||||
dj.queue.CurrentSong().Delete()
|
||||
dj.queue.OnSongFinished()
|
||||
return nil, errors.New("FAILED_TO_DOWNLOAD")
|
||||
}
|
||||
}
|
||||
}
|
||||
return title, err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ var youtubeVideoPatterns = []string{
|
|||
// YOUTUBE SERVICE
|
||||
// ---------------
|
||||
|
||||
type YouTube struct {}
|
||||
type YouTube struct{}
|
||||
|
||||
// Name of the service
|
||||
func (y YouTube) ServiceName() string {
|
||||
|
@ -63,16 +63,16 @@ func RegexpFromURL(url string, patterns []string) *regexp.Regexp {
|
|||
}
|
||||
|
||||
// Creates the requested song/playlist and adds to the queue
|
||||
func (y YouTube) NewRequest(user *gumble.User, url string) error {
|
||||
func (y YouTube) NewRequest(user *gumble.User, url string) (string, error) {
|
||||
var shortURL, startOffset = "", ""
|
||||
if re, err := regexp.Compile(youtubePlaylistPattern); err == nil {
|
||||
if re.MatchString(url) {
|
||||
if dj.HasPermission(user.Name, dj.conf.Permissions.AdminAddPlaylists) {
|
||||
shortURL = re.FindStringSubmatch(url)[1]
|
||||
_, err := NewYouTubePlaylist(user.Name, shortURL)
|
||||
return err
|
||||
playlist, err := NewYouTubePlaylist(user.Name, shortURL)
|
||||
return playlist.Title(), err
|
||||
} else {
|
||||
return errors.New("NO_PLAYLIST_PERMISSION")
|
||||
return nil, errors.New("NO_PLAYLIST_PERMISSION")
|
||||
}
|
||||
} else {
|
||||
re = RegexpFromURL(url, youtubeVideoPatterns)
|
||||
|
@ -81,11 +81,11 @@ func (y YouTube) NewRequest(user *gumble.User, url string) error {
|
|||
if len(matches[0]) == 3 {
|
||||
startOffset = matches[0][2]
|
||||
}
|
||||
_, err := NewYouTubeSong(user.Name, shortURL, startOffset, nil)
|
||||
return err
|
||||
song, err := NewYouTubeSong(user.Name, shortURL, startOffset, nil)
|
||||
return song.Title(), err
|
||||
}
|
||||
} else {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
|
|
72
web.go
72
web.go
|
@ -5,64 +5,58 @@ import (
|
|||
"io/ioutil"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"html"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/layeh/gumble/gumble"
|
||||
)
|
||||
|
||||
var client_token = make(map[string]string)
|
||||
var token_client = make(map[string]string)
|
||||
var client_token = make(map[*gumble.User]string)
|
||||
var token_client = make(map[string]*gumble.User)
|
||||
var external_ip = ""
|
||||
|
||||
type Page struct {
|
||||
Title string
|
||||
Body []byte
|
||||
}
|
||||
|
||||
func (p *Page) save() error {
|
||||
filename := p.Title + ".txt"
|
||||
return ioutil.WriteFile(filename, p.Body, 0600)
|
||||
}
|
||||
|
||||
func loadPage(title string) (*Page, error) {
|
||||
filename := title + ".txt"
|
||||
body, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Page{Title: title, Body: body}, nil
|
||||
}
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
var uname = token_client[r.URL.Path[1:]]
|
||||
if uname == "" {
|
||||
fmt.Fprintf(w, "I don't know you")
|
||||
} else {
|
||||
fmt.Fprintf(w, "Hi there, I love %s!", uname)
|
||||
}
|
||||
}
|
||||
|
||||
func Webserver() {
|
||||
http.HandleFunc("/", handler)
|
||||
http.HandleFunc("/", homepage)
|
||||
http.HandleFunc("/add", addSong)
|
||||
http.ListenAndServe(":9563", nil)
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
func homepage(w http.ResponseWriter, r *http.Request) {
|
||||
var uname = token_client[r.FormValue("token")]
|
||||
if uname == "" {
|
||||
fmt.Fprintf(w, "Invalid Token")
|
||||
} else {
|
||||
fmt.Fprintf(w, "Hang in there %s, I haven't made the website yet!", uname.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func addSong(w http.ResponseWriter, r *http.Request) {
|
||||
var uname = token_client[r.FormValue("token")]
|
||||
if uname == "" {
|
||||
fmt.Fprintf(w, "Invalid Token")
|
||||
} else {
|
||||
var url = UnescapeString(r.FormValue("url"))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func GetWebAddress(user *gumble.User) {
|
||||
if client_token[user.Name] != "" {
|
||||
token_client[client_token[user.Name]] = ""
|
||||
if client_token[user] != "" {
|
||||
token_client[client_token[user]] = ""
|
||||
}
|
||||
// dealing with collisions
|
||||
var firstLoop = true
|
||||
for firstLoop || token_client[client_token[user.Name]] != "" {
|
||||
client_token[user.Name] = randSeq(10)
|
||||
for firstLoop || token_client[client_token[user]] != "" {
|
||||
client_token[user] = randSeq(10)
|
||||
firstLoop = false
|
||||
}
|
||||
token_client[client_token[user.Name]] = user.Name
|
||||
dj.SendPrivateMessage(user, fmt.Sprintf(WEB_ADDRESS, getIP(), client_token[user.Name], getIP(), client_token[user.Name]))
|
||||
token_client[client_token[user]] = user
|
||||
dj.SendPrivateMessage(user, fmt.Sprintf(WEB_ADDRESS, getIP(), client_token[user], getIP(), client_token[user]))
|
||||
}
|
||||
|
||||
// Gets the external ip address for the server
|
||||
func getIP() string {
|
||||
if external_ip != "" {
|
||||
return external_ip
|
||||
|
@ -79,9 +73,9 @@ func getIP() string {
|
|||
}
|
||||
}
|
||||
|
||||
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
|
||||
// Generates a pseudorandom string of characters
|
||||
func randSeq(n int) string {
|
||||
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
b := make([]rune, n)
|
||||
for i := range b {
|
||||
b[i] = letters[rand.Intn(len(letters))]
|
||||
|
|
Reference in a new issue