351 lines
		
	
	
	
		
			9.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			351 lines
		
	
	
	
		
			9.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package main
 | |
| 
 | |
| import (
 | |
| 	"encoding/gob"
 | |
| 	"fmt"
 | |
| 	"github.com/delucks/go-subsonic"
 | |
| 	"github.com/diamondburned/gotk4/pkg/core/glib"
 | |
| 	// "io"
 | |
| 	"log"
 | |
| 	"net/http"
 | |
| 	"net/url"
 | |
| 	"os"
 | |
| 	"strconv"
 | |
| 	"strings"
 | |
| 	"time"
 | |
| 	// "github.com/diamondburned/gotk4/pkg/gtk/v4"
 | |
| 	"github.com/djherbis/fscache"
 | |
| 	"github.com/zalando/go-keyring"
 | |
| )
 | |
| 
 | |
| func TestConnButtonClicked() {
 | |
| 	clientt := CreateUser(HttpSettings)
 | |
| 	_, err := clientt.GetGenres()
 | |
| 	if err != nil {
 | |
| 		var up = func() bool {
 | |
| 			SettingsTabUserStatusLabel.SetText("✗  ")
 | |
| 			return false
 | |
| 		}
 | |
| 		glib.IdleAdd(up)
 | |
| 
 | |
| 	} else {
 | |
| 		var up = func() bool {
 | |
| 			SettingsTabUserStatusLabel.SetText("✓  ")
 | |
| 			return false
 | |
| 		}
 | |
| 		glib.IdleAdd(up)
 | |
| 
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func CreateUser(httpsettings *http.Client) subsonic.Client {
 | |
| 	server := SettingsTabUserServerEntry.Text()
 | |
| 	username := SettingsTabUserUsernameEntry.Text()
 | |
| 	device := SettingsTabUserDeviceNameEntry.Text()
 | |
| 	pass := SettingsTabUserPasswordEntry.Text()
 | |
| 
 | |
| 	clientt := subsonic.Client{
 | |
| 		Client:     httpsettings,
 | |
| 		BaseUrl:    server,
 | |
| 		User:       username,
 | |
| 		ClientName: device,
 | |
| 	}
 | |
| 	clientt.Authenticate(pass)
 | |
| 
 | |
| 	return clientt
 | |
| }
 | |
| 
 | |
| type User struct {
 | |
| 	User string
 | |
| 	Url  string
 | |
| }
 | |
| 
 | |
| type Settings struct {
 | |
| 	//User
 | |
| 	Users      [10]User
 | |
| 	PlayerName string
 | |
| 
 | |
| 	//Local
 | |
| 	DeleteOnClose bool
 | |
| 	Coversize     int16
 | |
| 	BlurRadius    int8
 | |
| 	HideHeaderBar bool
 | |
| 
 | |
| 	//Transcoding
 | |
| 	Codec             string
 | |
| 	Bitrate           int16
 | |
| 	DownladBufferSize int16
 | |
| 
 | |
| 	//Net
 | |
| 	UseStreamingProxy  bool
 | |
| 	StreamingProxy     string
 | |
| 	AuthStreamingProxy bool
 | |
| 	StreamingProxyUser string
 | |
| 
 | |
| 	// Metadata
 | |
| 	MetadataType      string
 | |
| 	UseMetadataProxy  bool
 | |
| 	MetadataProxy     string
 | |
| 	AuthMetadataProxy bool
 | |
| 	MetadataProxyUser string
 | |
| }
 | |
| 
 | |
| // type SonicallyStream struct {
 | |
| // 	ReadMaster  fscache.ReadAtCloser
 | |
| // 	WriteMaster io.WriteCloser
 | |
| // }
 | |
| 
 | |
| func Serialize(obj Settings, path string) {
 | |
| 	dataFile, err1 := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0664)
 | |
| 	defer dataFile.Close()
 | |
| 	if err1 == nil {
 | |
| 		os.Remove(path)
 | |
| 	}
 | |
| 	dataFile, _ = os.Create(path)
 | |
| 	e := gob.NewEncoder(dataFile)
 | |
| 	if err := e.Encode(obj); err != nil {
 | |
| 		LogFatalOnError(err)
 | |
| 	}
 | |
| 	dataFile.Close()
 | |
| }
 | |
| 
 | |
| func DeSerialize(path string) Settings {
 | |
| 	dataFile, err := os.Open(path)
 | |
| 	if err != nil {
 | |
| 		PanicOnError(err)
 | |
| 	}
 | |
| 	var sett Settings
 | |
| 	dataDecoder := gob.NewDecoder(dataFile)
 | |
| 	err = dataDecoder.Decode(&sett)
 | |
| 
 | |
| 	if err != nil {
 | |
| 		PanicOnError(err)
 | |
| 	}
 | |
| 	dataFile.Close()
 | |
| 	return sett
 | |
| }
 | |
| 
 | |
| func SerializeArtist(obj subsonic.ArtistID3, path string) {
 | |
| 	dataFile, err1 := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0664)
 | |
| 	defer dataFile.Close()
 | |
| 	if err1 == nil {
 | |
| 		os.Remove(path)
 | |
| 	}
 | |
| 	dataFile, _ = os.Create(path)
 | |
| 	e := gob.NewEncoder(dataFile)
 | |
| 	if err := e.Encode(obj); err != nil {
 | |
| 		LogFatalOnError(err)
 | |
| 	}
 | |
| 	dataFile.Close()
 | |
| }
 | |
| 
 | |
| func DeSerializeArtist(path string) subsonic.ArtistID3 {
 | |
| 	dataFile, err := os.Open(path)
 | |
| 	if err != nil {
 | |
| 		PanicOnError(err)
 | |
| 	}
 | |
| 	var sett subsonic.ArtistID3
 | |
| 	dataDecoder := gob.NewDecoder(dataFile)
 | |
| 	err = dataDecoder.Decode(&sett)
 | |
| 
 | |
| 	if err != nil {
 | |
| 		PanicOnError(err)
 | |
| 	}
 | |
| 	dataFile.Close()
 | |
| 	return sett
 | |
| }
 | |
| 
 | |
| func SerializePlaylist(obj subsonic.Playlist, path string) {
 | |
| 	dataFile, err1 := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0664)
 | |
| 	defer dataFile.Close()
 | |
| 	if err1 == nil {
 | |
| 		os.Remove(path)
 | |
| 	}
 | |
| 	dataFile, _ = os.Create(path)
 | |
| 	e := gob.NewEncoder(dataFile)
 | |
| 	if err := e.Encode(obj); err != nil {
 | |
| 		LogFatalOnError(err)
 | |
| 	}
 | |
| 	dataFile.Close()
 | |
| }
 | |
| 
 | |
| func DeSerializePlaylist(path string) subsonic.Playlist {
 | |
| 	dataFile, err := os.Open(path)
 | |
| 	if err != nil {
 | |
| 		PanicOnError(err)
 | |
| 	}
 | |
| 	var sett subsonic.Playlist
 | |
| 	dataDecoder := gob.NewDecoder(dataFile)
 | |
| 	err = dataDecoder.Decode(&sett)
 | |
| 
 | |
| 	if err != nil {
 | |
| 		PanicOnError(err)
 | |
| 	}
 | |
| 	dataFile.Close()
 | |
| 	return sett
 | |
| }
 | |
| 
 | |
| func Init() {
 | |
| 	fil, err1 := os.OpenFile(settingsPath, os.O_APPEND|os.O_WRONLY, 0664)
 | |
| 	defer fil.Close()
 | |
| 	if err1 == nil {
 | |
| 		MainSettings = DeSerialize(settingsPath)
 | |
| 		UserList = MainSettings.Users
 | |
| 		UserDeserialized = MainSettings.Users[0]
 | |
| 
 | |
| 		clientt := subsonic.Client{
 | |
| 			Client:     HttpSettings,
 | |
| 			BaseUrl:    UserDeserialized.Url,
 | |
| 			User:       UserDeserialized.User,
 | |
| 			ClientName: MainSettings.PlayerName,
 | |
| 		}
 | |
| 
 | |
| 		sec, err := keyring.Get(UserDeserialized.Url, UserDeserialized.User)
 | |
| 		if err == nil {
 | |
| 			clientt.Authenticate(sec)
 | |
| 			CurrentClient = clientt
 | |
| 			SettingsTabUserUsernameEntry.SetText(UserDeserialized.User)
 | |
| 			SettingsTabUserPasswordEntry.SetText(sec)
 | |
| 			SettingsTabUserDeviceNameEntry.SetText(MainSettings.PlayerName)
 | |
| 			SettingsTabUserServerEntry.SetText(UserDeserialized.Url)
 | |
| 			SettingsTabAppDeleteSwitch.SetActive(MainSettings.DeleteOnClose)
 | |
| 			SettingsTabAppHideHeaderBarSwitch.SetActive(MainSettings.HideHeaderBar)
 | |
| 
 | |
| 			SettingsTabAppCoverSizeEntry.SetText(strconv.Itoa(int(MainSettings.Coversize)))
 | |
| 			SettingsTabAppBlurRadiusEntry.SetText(strconv.Itoa(int(MainSettings.BlurRadius)))
 | |
| 			SettingsTabAppBitrateEntry.SetText(strconv.Itoa(int(MainSettings.Bitrate)))
 | |
| 			SettingsTabAppBufferSizeEntry.SetText(strconv.Itoa(int(MainSettings.DownladBufferSize)))
 | |
| 			SettingsTabAppStreamingProxySwitch.SetActive(MainSettings.UseStreamingProxy)
 | |
| 			if MainSettings.UseStreamingProxy == true {
 | |
| 				SettingsTabAppStreamingProxyEntry.SetText(MainSettings.StreamingProxy)
 | |
| 
 | |
| 				// var temptrans *http.Transport
 | |
| 				var protocol, parsed string
 | |
| 
 | |
| 				if strings.Contains(MainSettings.StreamingProxy, "https") {
 | |
| 					protocol = "https://"
 | |
| 					parsed = MainSettings.StreamingProxy[8:]
 | |
| 				} else if strings.Contains(MainSettings.StreamingProxy, "socks5") {
 | |
| 					protocol = "socks5://"
 | |
| 					parsed = MainSettings.StreamingProxy[9:]
 | |
| 				} else if strings.Contains(MainSettings.StreamingProxy, "http") && !strings.Contains(MainSettings.StreamingProxy, "https") {
 | |
| 					protocol = "http://"
 | |
| 					parsed = MainSettings.StreamingProxy[7:]
 | |
| 				} else {
 | |
| 					log.Fatal("Unsupported protocol")
 | |
| 				}
 | |
| 
 | |
| 				if SettingsTabAppAuthStreamingProxySwitch.Active() == true {
 | |
| 					sec2, err := keyring.Get(MainSettings.StreamingProxy, MainSettings.StreamingProxyUser)
 | |
| 					if err != nil {
 | |
| 						log.Fatal("Error getting from keyring")
 | |
| 					}
 | |
| 					SettingsTabAppStreamingProxyUsernameEntry.SetText(MainSettings.StreamingProxyUser)
 | |
| 					SettingsTabAppStreamingProxyPassEntry.SetText(sec2)
 | |
| 
 | |
| 					fullURL := protocol + MainSettings.StreamingProxyUser + ":" + sec2 + "@" + parsed
 | |
| 					//adding the proxy settings to the Transport object
 | |
| 					parsedurl, _ := url.Parse(fullURL)
 | |
| 
 | |
| 					HttpSettings = &http.Client{Timeout: time.Second * 10, Transport: &http.Transport{Proxy: http.ProxyURL(parsedurl)}}
 | |
| 
 | |
| 				} else {
 | |
| 					// fullURL := protocol + MainSettings.StreamingProxyUser + "@" + parsed
 | |
| 					parsedurl, _ := url.Parse(MainSettings.StreamingProxy)
 | |
| 					// temptrans :=
 | |
| 					HttpSettings = &http.Client{Timeout: time.Second * 10, Transport: &http.Transport{Proxy: http.ProxyURL(parsedurl)}}
 | |
| 				}
 | |
| 
 | |
| 				//adding the Transport object to the http Client
 | |
| 
 | |
| 				// HttpSettings.Transport
 | |
| 
 | |
| 			}
 | |
| 
 | |
| 			SettingsTabAppMetadataProxySwitch.SetActive(MainSettings.UseMetadataProxy)
 | |
| 			if MainSettings.UseMetadataProxy == true {
 | |
| 				SettingsTabAppMetadataProxyEntry.SetText(MainSettings.MetadataProxy)
 | |
| 
 | |
| 				// var temptrans2 *http.Transport
 | |
| 				var protocol, parsed string
 | |
| 
 | |
| 				if strings.Contains(MainSettings.MetadataProxy, "https") {
 | |
| 					protocol = "https://"
 | |
| 					parsed = MainSettings.MetadataProxy[8:]
 | |
| 				} else if strings.Contains(MainSettings.MetadataProxy, "socks5") {
 | |
| 					protocol = "socks5://"
 | |
| 					parsed = MainSettings.MetadataProxy[9:]
 | |
| 				} else if strings.Contains(MainSettings.MetadataProxy, "http") && !strings.Contains(MainSettings.MetadataProxy, "https") {
 | |
| 					protocol = "http://"
 | |
| 					parsed = MainSettings.MetadataProxy[7:]
 | |
| 				} else {
 | |
| 					log.Fatal("Unsupported protocol")
 | |
| 				}
 | |
| 
 | |
| 				if SettingsTabAppAuthMetadataProxySwitch.Active() == true {
 | |
| 					sec3, err := keyring.Get(MainSettings.MetadataProxy, MainSettings.MetadataProxyUser)
 | |
| 					if err != nil {
 | |
| 						log.Fatal("Error getting from keyring")
 | |
| 					}
 | |
| 					SettingsTabAppMetadataProxyUsernameEntry.SetText(MainSettings.MetadataProxyUser)
 | |
| 					SettingsTabAppMetadataProxyPassEntry.SetText(sec3)
 | |
| 
 | |
| 					fullURL := protocol + MainSettings.MetadataProxyUser + ":" + sec3 + "@" + parsed
 | |
| 					//adding the proxy settings to the Transport object
 | |
| 					parsedurl2, err := url.Parse(fullURL)
 | |
| 					MetadataClient = &http.Client{Timeout: time.Second * 10, Transport: &http.Transport{Proxy: http.ProxyURL(parsedurl2)}}
 | |
| 
 | |
| 				} else {
 | |
| 					// fullURL := protocol + MainSettings.StreamingProxyUser + "@" + parsed
 | |
| 					parsedurl2, _ := url.Parse(MainSettings.MetadataProxy)
 | |
| 					MetadataClient = &http.Client{Timeout: time.Second * 10, Transport: &http.Transport{Proxy: http.ProxyURL(parsedurl2)}}
 | |
| 
 | |
| 				}
 | |
| 
 | |
| 				//adding the Transport object to the http Client
 | |
| 
 | |
| 			}
 | |
| 			if MainSettings.Codec == "opus" {
 | |
| 				SettingsTabAppCodecComboBoxTest.SetActive(0)
 | |
| 			} else if MainSettings.Codec == "flac" {
 | |
| 				SettingsTabAppCodecComboBoxTest.SetActive(1)
 | |
| 			} else if MainSettings.Codec == "ogg" {
 | |
| 				SettingsTabAppCodecComboBoxTest.SetActive(2)
 | |
| 			} else if MainSettings.Codec == "mp3" {
 | |
| 				SettingsTabAppCodecComboBoxTest.SetActive(3)
 | |
| 			}
 | |
| 
 | |
| 			// HERE
 | |
| 
 | |
| 			if MainSettings.MetadataType == "None" {
 | |
| 				SettingsTabAppMetadataComboBoxTest.SetActive(0)
 | |
| 			} else if MainSettings.MetadataType == "Server" {
 | |
| 				SettingsTabAppMetadataComboBoxTest.SetActive(1)
 | |
| 			} else if MainSettings.MetadataType == "Sonically" {
 | |
| 				SettingsTabAppMetadataComboBoxTest.SetActive(2)
 | |
| 			}
 | |
| 
 | |
| 			if MainSettings.HideHeaderBar {
 | |
| 				MainWindow.SetDecorated(false)
 | |
| 			}
 | |
| 
 | |
| 			log.Print("Sucessfully set user from deserialzed content")
 | |
| 			cacheDir := GetRunDir() + MainDir + DataDir + "/.cache"
 | |
| 			CurrentCache, err = fscache.New(cacheDir, 0755, time.Hour*24)
 | |
| 			if err != nil {
 | |
| 				log.Fatal(err.Error())
 | |
| 			}
 | |
| 		}
 | |
| 	} else {
 | |
| 		MainDirC := GetRunDir() + MainDir
 | |
| 		if _, err := os.Stat(MainDir); os.IsNotExist(err) {
 | |
| 			os.Mkdir(MainDirC, 0744)
 | |
| 			os.Mkdir(MainDirC+DataDir, 0744)
 | |
| 			os.Mkdir(MainDirC+InfDir, 0744)
 | |
| 			os.Mkdir(MainDirC+SettingsDir, 0744)
 | |
| 			fmt.Println("Directory tree created")
 | |
| 		}
 | |
| 
 | |
| 	}
 | |
| 
 | |
| }
 |