diff --git a/.gitignore b/.gitignore index 1ce6283..e000756 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,8 @@ toolchains/ /toolchain/ /test/ /server/web/pages/template/pages/msx +server/config.db +/server/web/pages/template/pages/ +server/web/pages/template/route.go +server/server.pem +server/server.key diff --git a/README.md b/README.md index 7e2c57f..2ac3016 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ TorrServer, stream torrent to http ### Installation Just download server from releases and exec file\ https://github.com/YouROK/TorrServer/releases \ -After open browser link http://127.0.0.1:8090 \ +Then open the browser link http://127.0.0.1:8090 or https://127.0.0.1:8091 if the server was started with --ssl option \ On linux systems you may need to set the environment variable before run \ ***export GODEBUG=madvdontneed=1*** @@ -38,11 +38,19 @@ path/to/Android/sdk/ndk/ver/toolchains/llvm/prebuilt/platform # ### Server args: #### Usage -TorrServer-darwin-arm64 [--port PORT] [--path PATH] [--logpath LOGPATH] [--weblogpath WEBLOGPATH] [--rdb] [--httpauth] [--dontkill] [--ui] [--torrentsdir TORRENTSDIR] [--torrentaddr TORRENTADDR] [--pubipv4 PUBIPV4] [--pubipv6 PUBIPV6] [--searchwa] +TorrServer-darwin-arm64 [--port PORT] [--ssl] [--sslport PORT] [--sslcert PATH] [--sslkey PATH] [--path PATH] [--logpath LOGPATH] [--weblogpath WEBLOGPATH] [--rdb] [--httpauth] [--dontkill] [--ui] [--torrentsdir TORRENTSDIR] [--torrentaddr TORRENTADDR] [--pubipv4 PUBIPV4] [--pubipv6 PUBIPV6] [--searchwa] #### Options * --port PORT, -p PORT * web server port, default 8090 +* --ssl + * enables https +* --sslport PORT + * web server ssl port, If not set, will be set to default 8091 or taken from db(if stored previously). Accepted if --ssl enabled. +* --sslcert PATH + * path to ssl cert file. If not set, will be taken from db(if stored previously) or default self-signed certificate/key will be generated. Accepted if --ssl enabled. +* --sslkey PATH + * path to ssl key file. If not set, will be taken from db(if stored previously) or default self-signed certificate/key will be generated. Accepted if --ssl enabled. * --path PATH, -d PATH * database dir path * --logpath LOGPATH, -l LOGPATH diff --git a/server/cmd/main.go b/server/cmd/main.go index 63260c9..1ef3ec5 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -23,6 +23,10 @@ import ( type args struct { Port string `arg:"-p" help:"web server port, default 8090"` + Ssl bool `help:"enables https"` + SslPort string `help:"web server ssl port, If not set, will be set to default 8091 or taken from db(if stored previously). Accepted if --ssl enabled."` + SslCert string `help:"path to ssl cert file. If not set, will be taken from db(if stored previously) or default self-signed certificate/key will be generated. Accepted if --ssl enabled."` + SslKey string `help:"path to ssl key file. If not set, will be taken from db(if stored previously) or default self-signed certificate/key will be generated. Accepted if --ssl enabled."` Path string `arg:"-d" help:"database dir path"` LogPath string `arg:"-l" help:"server log file path"` WebLogPath string `arg:"-w" help:"web access log file path"` @@ -56,6 +60,10 @@ func main() { params.Port = "8090" } + if params.SslPort == "" { + params.SslPort = "8091" + } + settings.Path = params.Path settings.HttpAuth = params.HttpAuth log.Init(params.LogPath, params.WebLogPath) @@ -71,7 +79,11 @@ func main() { if params.UI { go func() { time.Sleep(time.Second) - browser.OpenURL("http://127.0.0.1:" + params.Port) + if params.Ssl { + browser.OpenURL("https://127.0.0.1:" + params.SslPort) + } else { + browser.OpenURL("http://127.0.0.1:" + params.Port) + } }() } @@ -91,7 +103,7 @@ func main() { go watchTDir(params.TorrentsDir) } - server.Start(params.Port, params.RDB, params.SearchWA) + server.Start(params.Port, params.SslPort, params.SslCert, params.SslKey, params.Ssl, params.RDB, params.SearchWA) log.TLogln(server.WaitServer()) log.Close() time.Sleep(time.Second * 3) diff --git a/server/dlna/list.go b/server/dlna/list.go index d268a13..1310c49 100644 --- a/server/dlna/list.go +++ b/server/dlna/list.go @@ -236,13 +236,23 @@ func loadTorrent(path, host string) (ret []interface{}) { func getLink(host, path string) string { if !strings.HasPrefix(host, "http") { - host = "http://" + host + if settings.Ssl { + host = "https://" + host + } else { + host = "http://" + host + } + } pos := strings.LastIndex(host, ":") if pos > 7 { host = host[:pos] } - return host + ":" + settings.Port + "/" + path + if settings.Ssl { + return host + ":" + settings.SslPort + "/" + path + } else { + return host + ":" + settings.Port + "/" + path + } + } func getObjFromTorrent(path, parent, host string, torr *torr.Torrent, file *state.TorrentFileStat) (ret interface{}) { diff --git a/server/server.go b/server/server.go index 4b3df32..567e2d3 100644 --- a/server/server.go +++ b/server/server.go @@ -10,8 +10,40 @@ import ( "server/web" ) -func Start(port string, roSets, searchWA bool) { +func Start(port, sslport, sslCert, sslKey string, sslEnabled, roSets, searchWA bool) { settings.InitSets(roSets, searchWA) + + //// https checks + // check if ssl enabled + settings.Ssl = sslEnabled + settings.BTsets.Ssl = sslEnabled + if sslEnabled { + // set settings ssl enabled + if sslport == "" { + if settings.BTsets.SslPort == "" { + settings.BTsets.SslPort = "8091" + } + } else { + settings.BTsets.SslPort = sslport + } + // check if ssl cert and key files exist + if sslCert != "" && sslKey != "" { + // set settings ssl cert and key files + settings.BTsets.SslCert = sslCert + settings.BTsets.SslKey = sslKey + } + log.TLogln("Check web ssl port", settings.BTsets.SslPort) + l, err := net.Listen("tcp", ":"+settings.BTsets.SslPort) + if l != nil { + l.Close() + } + if err != nil { + log.TLogln("Port", settings.BTsets.SslPort, "already in use! Please set different port for HTTP. Abort") + os.Exit(1) + } + } + + // http checks if port == "" { port = "8090" } @@ -21,13 +53,16 @@ func Start(port string, roSets, searchWA bool) { l.Close() } if err != nil { - log.TLogln("Port", port, "already in use! Abort") + log.TLogln("Port", port, "already in use! Please set different sslport for HTTPS. Abort") os.Exit(1) - } else { - go cleanCache() - settings.Port = port - web.Start(port) } + + // set settings http and https ports. Start web server. + go cleanCache() + settings.Port = port + settings.SslPort = settings.BTsets.SslPort + web.Start() + } func cleanCache() { diff --git a/server/settings/btsets.go b/server/settings/btsets.go index 73dfc72..7b1f4cc 100644 --- a/server/settings/btsets.go +++ b/server/settings/btsets.go @@ -46,6 +46,12 @@ type BTSets struct { UploadRateLimit int // in kb, 0 - inf ConnectionsLimit int PeersListenPort int + + //Https + Ssl bool + SslPort string + SslCert string + SslKey string } func (v *BTSets) String() string { diff --git a/server/settings/settings.go b/server/settings/settings.go index 09f129e..a6ee420 100644 --- a/server/settings/settings.go +++ b/server/settings/settings.go @@ -11,6 +11,8 @@ var ( tdb *TDB Path string Port string + SslPort string + Ssl bool ReadOnly bool HttpAuth bool SearchWA bool diff --git a/server/torr/preload.go b/server/torr/preload.go index 6e9dadf..ac18d2b 100644 --- a/server/torr/preload.go +++ b/server/torr/preload.go @@ -71,6 +71,9 @@ func (t *Torrent) Preload(index int, size int64) { if ffprobe.Exists() { link := "http://127.0.0.1:" + settings.Port + "/play/" + t.Hash().HexString() + "/" + strconv.Itoa(index) + if settings.Ssl { + link = "https://127.0.0.1:" + settings.SslPort + "/play/" + t.Hash().HexString() + "/" + strconv.Itoa(index) + } if data, err := ffprobe.ProbeUrl(link); err == nil { t.BitRate = data.Format.BitRate t.DurationSeconds = data.Format.DurationSeconds diff --git a/server/web/api/ffprobe.go b/server/web/api/ffprobe.go index 09c5d6f..7af921b 100644 --- a/server/web/api/ffprobe.go +++ b/server/web/api/ffprobe.go @@ -21,6 +21,9 @@ func ffp(c *gin.Context) { } link := "http://127.0.0.1:" + sets.Port + "/play/" + hash + "/" + indexStr + if sets.Ssl { + link = "https://127.0.0.1:" + sets.SslPort + "/play/" + hash + "/" + indexStr + } data, err := ffprobe.ProbeUrl(link) if err != nil { diff --git a/server/web/pages/template/html.go b/server/web/pages/template/html.go index 7baad43..83a90f9 100644 --- a/server/web/pages/template/html.go +++ b/server/web/pages/template/html.go @@ -118,20 +118,20 @@ var Mstile150x150png []byte //go:embed pages/site.webmanifest var Sitewebmanifest []byte -//go:embed pages/static/js/2.84d4a004.chunk.js -var Staticjs284d4a004chunkjs []byte +//go:embed pages/static/js/2.0d7c02d7.chunk.js +var Staticjs20d7c02d7chunkjs []byte -//go:embed pages/static/js/2.84d4a004.chunk.js.LICENSE.txt -var Staticjs284d4a004chunkjsLICENSEtxt []byte +//go:embed pages/static/js/2.0d7c02d7.chunk.js.LICENSE.txt +var Staticjs20d7c02d7chunkjsLICENSEtxt []byte -//go:embed pages/static/js/2.84d4a004.chunk.js.map -var Staticjs284d4a004chunkjsmap []byte +//go:embed pages/static/js/2.0d7c02d7.chunk.js.map +var Staticjs20d7c02d7chunkjsmap []byte -//go:embed pages/static/js/main.cc15501f.chunk.js -var Staticjsmaincc15501fchunkjs []byte +//go:embed pages/static/js/main.7efc8add.chunk.js +var Staticjsmain7efc8addchunkjs []byte -//go:embed pages/static/js/main.cc15501f.chunk.js.map -var Staticjsmaincc15501fchunkjsmap []byte +//go:embed pages/static/js/main.7efc8add.chunk.js.map +var Staticjsmain7efc8addchunkjsmap []byte //go:embed pages/static/js/runtime-main.64d07802.js var Staticjsruntimemain64d07802js []byte diff --git a/server/web/pages/template/pages/asset-manifest.json b/server/web/pages/template/pages/asset-manifest.json index 418133e..4c5c44c 100644 --- a/server/web/pages/template/pages/asset-manifest.json +++ b/server/web/pages/template/pages/asset-manifest.json @@ -1,17 +1,17 @@ { "files": { - "main.js": "/static/js/main.cc15501f.chunk.js", - "main.js.map": "/static/js/main.cc15501f.chunk.js.map", + "main.js": "/static/js/main.7efc8add.chunk.js", + "main.js.map": "/static/js/main.7efc8add.chunk.js.map", "runtime-main.js": "/static/js/runtime-main.64d07802.js", "runtime-main.js.map": "/static/js/runtime-main.64d07802.js.map", - "static/js/2.84d4a004.chunk.js": "/static/js/2.84d4a004.chunk.js", - "static/js/2.84d4a004.chunk.js.map": "/static/js/2.84d4a004.chunk.js.map", + "static/js/2.0d7c02d7.chunk.js": "/static/js/2.0d7c02d7.chunk.js", + "static/js/2.0d7c02d7.chunk.js.map": "/static/js/2.0d7c02d7.chunk.js.map", "index.html": "/index.html", - "static/js/2.84d4a004.chunk.js.LICENSE.txt": "/static/js/2.84d4a004.chunk.js.LICENSE.txt" + "static/js/2.0d7c02d7.chunk.js.LICENSE.txt": "/static/js/2.0d7c02d7.chunk.js.LICENSE.txt" }, "entrypoints": [ "static/js/runtime-main.64d07802.js", - "static/js/2.84d4a004.chunk.js", - "static/js/main.cc15501f.chunk.js" + "static/js/2.0d7c02d7.chunk.js", + "static/js/main.7efc8add.chunk.js" ] } \ No newline at end of file diff --git a/server/web/pages/template/pages/index.html b/server/web/pages/template/pages/index.html index 79c72d9..b087e73 100644 --- a/server/web/pages/template/pages/index.html +++ b/server/web/pages/template/pages/index.html @@ -1 +1 @@ -