adding frontend authentication, starting file priority code
This commit is contained in:
@@ -29,10 +29,19 @@ var (
|
||||
func GenerateClientConfigFile(config FullClientSettings, authString string) {
|
||||
os.Remove("public/static/js/kickwebsocket-generated.js")
|
||||
var clientFile string
|
||||
var webUIAuth string
|
||||
if config.ClientUsername != "" {
|
||||
webUIAuth = `
|
||||
const LoginRequired = true
|
||||
const ClientUsername = "` + config.ClientUsername + `"
|
||||
const ClientPassword = "` + config.ClientPassword + `"
|
||||
`
|
||||
}
|
||||
|
||||
if config.UseProxy {
|
||||
clientFile = `
|
||||
ClientAuthString = "` + authString + `"
|
||||
|
||||
` + webUIAuth + `
|
||||
var ws = new WebSocket("wss://` + config.BaseURL + `websocket")
|
||||
` + baseFile
|
||||
} else {
|
||||
@@ -40,7 +49,7 @@ func GenerateClientConfigFile(config FullClientSettings, authString string) {
|
||||
IP = "` + config.HTTPAddrIP + `"
|
||||
Port = "` + config.WebsocketClientPort + `"
|
||||
ClientAuthString = "` + authString + `"
|
||||
|
||||
` + webUIAuth + `
|
||||
var ws = new WebSocket(` + "`" + `ws://${IP}:${Port}/websocket` + "`" + `); //creating websocket
|
||||
` + baseFile
|
||||
|
||||
|
@@ -168,7 +168,7 @@ func StartTorrent(clientTorrent *torrent.Torrent, torrentLocalStorage Storage.To
|
||||
var torrentFilePriority = Storage.TorrentFilePriority{}
|
||||
torrentFilePriority.TorrentFilePath = singleFile.DisplayPath()
|
||||
torrentFilePriority.TorrentFilePriority = "Normal"
|
||||
|
||||
torrentFilePriority.TorrentFileSize = singleFile.Length()
|
||||
TorrentFilePriorityArray = append(TorrentFilePriorityArray, torrentFilePriority)
|
||||
|
||||
}
|
||||
@@ -215,8 +215,10 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
|
||||
Logger.WithFields(logrus.Fields{"torrentFile": singleTorrent.Name(), "error": err}).Error("Unable to add infobytes to the torrent!")
|
||||
}
|
||||
//Logger.WithFields(logrus.Fields{"singleTorrent": singleTorrentFromStorage.TorrentName}).Info("Generating infohash")
|
||||
calculatedTotalSize := CalculateDownloadSize(singleTorrentFromStorage, singleTorrent)
|
||||
calculatedCompleteSize := CalculateCompleteSize(singleTorrentFromStorage, singleTorrent)
|
||||
TempHash = singleTorrent.InfoHash()
|
||||
if (singleTorrent.BytesCompleted() == singleTorrentFromStorage.TorrentSize) && (singleTorrentFromStorage.TorrentMoved == false) { //if we are done downloading and haven't moved torrent yet
|
||||
if (calculatedCompleteSize == singleTorrentFromStorage.TorrentSize) && (singleTorrentFromStorage.TorrentMoved == false) { //if we are done downloading and haven't moved torrent yet
|
||||
Logger.WithFields(logrus.Fields{"singleTorrent": singleTorrentFromStorage.TorrentName}).Info("Torrent Completed, moving...")
|
||||
MoveAndLeaveSymlink(config, singleTorrent.InfoHash().String(), db, false, "") //can take some time to move file so running this in another thread TODO make this a goroutine and skip this block if the routine is still running
|
||||
}
|
||||
@@ -226,12 +228,13 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
|
||||
activePeersString := strconv.Itoa(fullStruct.ActivePeers) //converting to strings
|
||||
totalPeersString := fmt.Sprintf("%v", fullStruct.TotalPeers)
|
||||
fullClientDB.StoragePath = singleTorrentFromStorage.StoragePath
|
||||
downloadedSizeHumanized := HumanizeBytes(float32(singleTorrent.BytesCompleted())) //convert size to GB if needed
|
||||
|
||||
totalSizeHumanized := HumanizeBytes(float32(singleTorrentFromStorage.TorrentSize))
|
||||
downloadedSizeHumanized := HumanizeBytes(float32(calculatedCompleteSize)) //convert size to GB if needed
|
||||
totalSizeHumanized := HumanizeBytes(float32(calculatedTotalSize))
|
||||
|
||||
fullClientDB.DownloadedSize = downloadedSizeHumanized
|
||||
fullClientDB.Size = totalSizeHumanized
|
||||
PercentDone := fmt.Sprintf("%.2f", float32(singleTorrent.BytesCompleted())/float32(singleTorrentFromStorage.TorrentSize))
|
||||
PercentDone := fmt.Sprintf("%.2f", float32(calculatedCompleteSize)/float32(calculatedTotalSize))
|
||||
fullClientDB.TorrentHash = TempHash
|
||||
fullClientDB.PercentDone = PercentDone
|
||||
fullClientDB.DataBytesRead = fullStruct.ConnStats.BytesReadData //used for calculations not passed to client calculating up/down speed
|
||||
@@ -241,7 +244,7 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
|
||||
fullClientDB.TorrentName = singleTorrentFromStorage.TorrentName
|
||||
fullClientDB.DateAdded = singleTorrentFromStorage.DateAdded
|
||||
fullClientDB.TorrentLabel = singleTorrentFromStorage.Label
|
||||
fullClientDB.BytesCompleted = singleTorrent.BytesCompleted()
|
||||
fullClientDB.BytesCompleted = calculatedCompleteSize
|
||||
fullClientDB.NumberofFiles = len(singleTorrent.Files())
|
||||
|
||||
if len(PreviousTorrentArray) > 0 { //if we actually have a previous array //ranging over the previous torrent array to calculate the speed for each torrent
|
||||
@@ -253,7 +256,7 @@ func CreateRunningTorrentArray(tclient *torrent.Client, TorrentLocalArray []*Sto
|
||||
}
|
||||
}
|
||||
}
|
||||
CalculateTorrentETA(singleTorrentFromStorage.TorrentSize, singleTorrent.BytesCompleted(), fullClientDB) //needs to be here since we need the speed calculated before we can estimate the eta.
|
||||
CalculateTorrentETA(singleTorrentFromStorage.TorrentSize, calculatedCompleteSize, fullClientDB) //needs to be here since we need the speed calculated before we can estimate the eta.
|
||||
|
||||
fullClientDB.TotalUploadedSize = HumanizeBytes(float32(fullClientDB.TotalUploadedBytes))
|
||||
fullClientDB.UploadRatio = CalculateUploadRatio(singleTorrent, fullClientDB) //calculate the upload ratio
|
||||
|
@@ -40,7 +40,7 @@ func HumanizeBytes(bytes float32) string {
|
||||
}
|
||||
|
||||
//CopyFile takes a source file string and a destination file string and copies the file
|
||||
func CopyFile(srcFile string, destFile string) {
|
||||
func CopyFile(srcFile string, destFile string) { //TODO move this to our imported copy repo
|
||||
fileContents, err := os.Open(srcFile)
|
||||
defer fileContents.Close()
|
||||
if err != nil {
|
||||
@@ -84,8 +84,35 @@ func CalculateTorrentSpeed(t *torrent.Torrent, c *ClientDB, oc ClientDB) {
|
||||
}
|
||||
|
||||
//CalculateDownloadSize will calculate the download size once file priorities are sorted out
|
||||
func CalculateDownloadSize(tFromStorage *Storage.TorrentLocal) {
|
||||
func CalculateDownloadSize(tFromStorage *Storage.TorrentLocal, activeTorrent *torrent.Torrent) int64 {
|
||||
var totalLength int64
|
||||
for _, file := range tFromStorage.TorrentFilePriority {
|
||||
if file.TorrentFilePriority != "Cancel" {
|
||||
totalLength = totalLength + file.TorrentFileSize
|
||||
}
|
||||
}
|
||||
return totalLength
|
||||
}
|
||||
|
||||
//CalculateCompleteSize will be used to calculate how much of the actual torrent we have completed minus the canceled files (even if they have been partially downloaded)
|
||||
func CalculateCompleteSize(tFromStorage *Storage.TorrentLocal, activeTorrent *torrent.Torrent) int64 {
|
||||
var downloadedLength int64
|
||||
var discardByteLength int64
|
||||
for _, storageFile := range tFromStorage.TorrentFilePriority {
|
||||
if storageFile.TorrentFilePriority != "Cancel" { //If the file is canceled
|
||||
for _, activeFile := range activeTorrent.Files() {
|
||||
if activeFile.DisplayPath() == storageFile.TorrentFilePath { //match the file from storage to active
|
||||
for _, piece := range activeFile.State() {
|
||||
if piece.Partial || piece.Complete {
|
||||
discardByteLength = discardByteLength + piece.Bytes
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
downloadedLength = downloadedLength - discardByteLength
|
||||
return downloadedLength
|
||||
}
|
||||
|
||||
//CalculateTorrentETA is used to estimate the remaining dl time of the torrent based on the speed that the MB are being downloaded
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -22,6 +23,8 @@ type FullClientSettings struct {
|
||||
UseProxy bool
|
||||
WebsocketClientPort string
|
||||
BaseURL string
|
||||
ClientUsername string
|
||||
ClientPassword string
|
||||
Version int
|
||||
TorrentConfig torrent.Config
|
||||
TFileUploadFolder string
|
||||
@@ -105,55 +108,10 @@ func FullClientSettingsNew() FullClientSettings {
|
||||
var httpAddr string
|
||||
var baseURL string
|
||||
var websocketClientPort string
|
||||
|
||||
httpAddrIP := viper.GetString("serverConfig.ServerAddr")
|
||||
httpAddrPort := viper.GetString("serverConfig.ServerPort")
|
||||
proxySet := viper.GetBool("reverseProxy.ProxyEnabled")
|
||||
websocketClientPort = strings.TrimLeft(viper.GetString("serverConfig.ServerPort"), ":") //Trimming off the colon in front of the port
|
||||
if proxySet {
|
||||
baseURL = viper.GetString("reverseProxy.BaseURL")
|
||||
fmt.Println("WebsocketClientPort", viper.GetString("serverConfig.ServerPort"))
|
||||
}
|
||||
seedRatioStop := viper.GetFloat64("serverConfig.SeedRatioStop")
|
||||
httpAddr = httpAddrIP + httpAddrPort
|
||||
pushBulletToken := viper.GetString("notifications.PushBulletToken")
|
||||
defaultMoveFolder := filepath.ToSlash(viper.GetString("serverConfig.DefaultMoveFolder")) //Converting the string literal into a filepath
|
||||
defaultMoveFolderAbs, err := filepath.Abs(defaultMoveFolder)
|
||||
if err != nil {
|
||||
fmt.Println("Failed creating absolute path for defaultMoveFolder", err)
|
||||
}
|
||||
torrentWatchFolder := filepath.ToSlash(viper.GetString("serverConfig.TorrentWatchFolder"))
|
||||
torrentWatchFolderAbs, err := filepath.Abs(torrentWatchFolder)
|
||||
if err != nil {
|
||||
fmt.Println("Failed creating absolute path for torrentWatchFolderAbs", err)
|
||||
}
|
||||
|
||||
dataDir := filepath.ToSlash(viper.GetString("torrentClientConfig.DownloadDir")) //Converting the string literal into a filepath
|
||||
dataDirAbs, err := filepath.Abs(dataDir) //Converting to an absolute file path
|
||||
if err != nil {
|
||||
fmt.Println("Failed creating absolute path for dataDir", err)
|
||||
}
|
||||
|
||||
var uploadRateLimiter *rate.Limiter
|
||||
var downloadRateLimiter *rate.Limiter
|
||||
uploadRate := viper.GetString("serverConfig.UploadRateLimit")
|
||||
downloadRate := viper.GetString("serverConfig.DownloadRateLimit")
|
||||
downloadRateLimiter, uploadRateLimiter = calculateRateLimiters(uploadRate, downloadRate)
|
||||
|
||||
listenAddr := viper.GetString("torrentClientConfig.ListenAddr")
|
||||
disablePex := viper.GetBool("torrentClientConfig.DisablePEX")
|
||||
noDHT := viper.GetBool("torrentClientConfig.NoDHT")
|
||||
noUpload := viper.GetBool("torrentClientConfig.NoUpload")
|
||||
seed := viper.GetBool("torrentClientConfig.Seed")
|
||||
|
||||
peerID := viper.GetString("torrentClientConfig.PeerID")
|
||||
disableUTP := viper.GetBool("torrentClientConfig.DisableUTP")
|
||||
disableTCP := viper.GetBool("torrentClientConfig.DisableTCP")
|
||||
disableIPv6 := viper.GetBool("torrentClientConfig.DisableIPv6")
|
||||
debug := viper.GetBool("torrentClientConfig.Debug")
|
||||
var logLevel logrus.Level
|
||||
//logging
|
||||
logLevelString := viper.GetString("serverConfig.LogLevel")
|
||||
logOutput := viper.GetString("serverConfig.LogOutput")
|
||||
var logLevel logrus.Level
|
||||
switch logLevelString { //Options = Debug 5, Info 4, Warn 3, Error 2, Fatal 1, Panic 0
|
||||
case "Panic":
|
||||
logLevel = 0
|
||||
@@ -171,6 +129,65 @@ func FullClientSettingsNew() FullClientSettings {
|
||||
logLevel = 3
|
||||
|
||||
}
|
||||
//HTTP, proxy
|
||||
httpAddrIP := viper.GetString("serverConfig.ServerAddr")
|
||||
httpAddrPort := viper.GetString("serverConfig.ServerPort")
|
||||
httpAddr = httpAddrIP + httpAddrPort
|
||||
proxySet := viper.GetBool("reverseProxy.ProxyEnabled")
|
||||
websocketClientPort = strings.TrimLeft(viper.GetString("serverConfig.ServerPort"), ":") //Trimming off the colon in front of the port
|
||||
if proxySet {
|
||||
baseURL = viper.GetString("reverseProxy.BaseURL")
|
||||
fmt.Println("WebsocketClientPort", viper.GetString("serverConfig.ServerPort"))
|
||||
}
|
||||
//Client Authentication
|
||||
clientAuthEnabled := viper.GetBool("goTorrentWebUI.WebUIAuth")
|
||||
var webUIUser string
|
||||
var webUIPasswordHash string
|
||||
if clientAuthEnabled {
|
||||
webUIUser = viper.GetString("goTorrentWebUI.WebUIUser")
|
||||
webUIPassword := viper.GetString("goTorrentWebUI.WebUIPassword")
|
||||
hash256 := sha256.New()
|
||||
hash256.Write([]byte(webUIPassword)) //Hashing the password
|
||||
webUIPasswordHash = fmt.Sprintf("%x", hash256.Sum(nil)) //Printing the password out as a string
|
||||
}
|
||||
//General Settings
|
||||
seedRatioStop := viper.GetFloat64("serverConfig.SeedRatioStop")
|
||||
defaultMoveFolder := filepath.ToSlash(viper.GetString("serverConfig.DefaultMoveFolder")) //Converting the string literal into a filepath
|
||||
defaultMoveFolderAbs, err := filepath.Abs(defaultMoveFolder)
|
||||
if err != nil {
|
||||
fmt.Println("Failed creating absolute path for defaultMoveFolder", err)
|
||||
}
|
||||
torrentWatchFolder := filepath.ToSlash(viper.GetString("serverConfig.TorrentWatchFolder"))
|
||||
torrentWatchFolderAbs, err := filepath.Abs(torrentWatchFolder)
|
||||
if err != nil {
|
||||
fmt.Println("Failed creating absolute path for torrentWatchFolderAbs", err)
|
||||
}
|
||||
//Notifications
|
||||
pushBulletToken := viper.GetString("notifications.PushBulletToken")
|
||||
|
||||
//Rate Limiters
|
||||
var uploadRateLimiter *rate.Limiter
|
||||
var downloadRateLimiter *rate.Limiter
|
||||
uploadRate := viper.GetString("serverConfig.UploadRateLimit")
|
||||
downloadRate := viper.GetString("serverConfig.DownloadRateLimit")
|
||||
downloadRateLimiter, uploadRateLimiter = calculateRateLimiters(uploadRate, downloadRate)
|
||||
//Internals
|
||||
dataDir := filepath.ToSlash(viper.GetString("torrentClientConfig.DownloadDir")) //Converting the string literal into a filepath
|
||||
dataDirAbs, err := filepath.Abs(dataDir) //Converting to an absolute file path
|
||||
if err != nil {
|
||||
fmt.Println("Failed creating absolute path for dataDir", err)
|
||||
}
|
||||
listenAddr := viper.GetString("torrentClientConfig.ListenAddr")
|
||||
disablePex := viper.GetBool("torrentClientConfig.DisablePEX")
|
||||
noDHT := viper.GetBool("torrentClientConfig.NoDHT")
|
||||
noUpload := viper.GetBool("torrentClientConfig.NoUpload")
|
||||
seed := viper.GetBool("torrentClientConfig.Seed")
|
||||
|
||||
peerID := viper.GetString("torrentClientConfig.PeerID")
|
||||
disableUTP := viper.GetBool("torrentClientConfig.DisableUTP")
|
||||
disableTCP := viper.GetBool("torrentClientConfig.DisableTCP")
|
||||
disableIPv6 := viper.GetBool("torrentClientConfig.DisableIPv6")
|
||||
debug := viper.GetBool("torrentClientConfig.Debug")
|
||||
|
||||
dhtServerConfig := dht.ServerConfig{
|
||||
StartingNodes: dht.GlobalBootstrapAddrs,
|
||||
@@ -212,6 +229,8 @@ func FullClientSettingsNew() FullClientSettings {
|
||||
HTTPAddrIP: httpAddrIP,
|
||||
UseProxy: proxySet,
|
||||
WebsocketClientPort: websocketClientPort,
|
||||
ClientUsername: webUIUser,
|
||||
ClientPassword: webUIPasswordHash,
|
||||
TorrentConfig: tConfig,
|
||||
BaseURL: baseURL,
|
||||
TFileUploadFolder: "uploadedTorrents",
|
||||
|
Reference in New Issue
Block a user