disable https for dlna and fic certs path

This commit is contained in:
nikk gitanes
2023-11-13 01:14:30 +03:00
parent a91e6eb11b
commit 52e1ab9cca
5 changed files with 48 additions and 53 deletions

View File

@@ -236,23 +236,22 @@ func loadTorrent(path, host string) (ret []interface{}) {
func getLink(host, path string) string { func getLink(host, path string) string {
if !strings.HasPrefix(host, "http") { if !strings.HasPrefix(host, "http") {
if settings.Ssl { // if settings.Ssl {
host = "https://" + host // host = "https://" + host
} else { // } else {
host = "http://" + host host = "http://" + host
} // }
} }
pos := strings.LastIndex(host, ":") pos := strings.LastIndex(host, ":")
if pos > 7 { if pos > 7 {
host = host[:pos] host = host[:pos]
} }
if settings.Ssl { // if settings.Ssl {
return host + ":" + settings.SslPort + "/" + path // return host + ":" + settings.SslPort + "/" + path
} else { // } else {
return host + ":" + settings.Port + "/" + path return host + ":" + settings.Port + "/" + path
} // }
} }
func getObjFromTorrent(path, parent, host string, torr *torr.Torrent, file *state.TorrentFileStat) (ret interface{}) { func getObjFromTorrent(path, parent, host string, torr *torr.Torrent, file *state.TorrentFileStat) (ret interface{}) {

View File

@@ -12,19 +12,16 @@ import (
func Start(port, sslport, sslCert, sslKey string, sslEnabled, roSets, searchWA bool) { func Start(port, sslport, sslCert, sslKey string, sslEnabled, roSets, searchWA bool) {
settings.InitSets(roSets, searchWA) settings.InitSets(roSets, searchWA)
// https checks
//// https checks
// check if ssl enabled
settings.Ssl = sslEnabled
settings.BTsets.Ssl = sslEnabled
if sslEnabled { if sslEnabled {
// set settings ssl enabled // set settings ssl enabled
settings.Ssl = sslEnabled
if sslport == "" { if sslport == "" {
if settings.BTsets.SslPort == "" { if settings.BTsets.SslPort != "" {
settings.BTsets.SslPort = "8091" sslport = settings.BTsets.SslPort
} else {
sslport = "8091"
} }
} else {
settings.BTsets.SslPort = sslport
} }
// check if ssl cert and key files exist // check if ssl cert and key files exist
if sslCert != "" && sslKey != "" { if sslCert != "" && sslKey != "" {
@@ -32,17 +29,16 @@ func Start(port, sslport, sslCert, sslKey string, sslEnabled, roSets, searchWA b
settings.BTsets.SslCert = sslCert settings.BTsets.SslCert = sslCert
settings.BTsets.SslKey = sslKey settings.BTsets.SslKey = sslKey
} }
log.TLogln("Check web ssl port", settings.BTsets.SslPort) log.TLogln("Check web ssl port", sslport)
l, err := net.Listen("tcp", ":"+settings.BTsets.SslPort) l, err := net.Listen("tcp", ":"+sslport)
if l != nil { if l != nil {
l.Close() l.Close()
} }
if err != nil { if err != nil {
log.TLogln("Port", settings.BTsets.SslPort, "already in use! Please set different port for HTTP. Abort") log.TLogln("Port", sslport, "already in use! Please set different port for HTTPS. Abort")
os.Exit(1) os.Exit(1)
} }
} }
// http checks // http checks
if port == "" { if port == "" {
port = "8090" port = "8090"
@@ -53,14 +49,14 @@ func Start(port, sslport, sslCert, sslKey string, sslEnabled, roSets, searchWA b
l.Close() l.Close()
} }
if err != nil { if err != nil {
log.TLogln("Port", port, "already in use! Please set different sslport for HTTPS. Abort") log.TLogln("Port", port, "already in use! Please set different sslport for HTTP. Abort")
os.Exit(1) os.Exit(1)
} }
// remove old disk caches
// set settings http and https ports. Start web server.
go cleanCache() go cleanCache()
// set settings http and https ports. Start web server.
settings.Port = port settings.Port = port
settings.SslPort = settings.BTsets.SslPort settings.SslPort = sslport
web.Start() web.Start()
} }

View File

@@ -47,11 +47,10 @@ type BTSets struct {
ConnectionsLimit int ConnectionsLimit int
PeersListenPort int PeersListenPort int
//Https // HTTPS
Ssl bool SslPort string
SslPort string SslCert string
SslCert string SslKey string
SslKey string
} }
func (v *BTSets) String() string { func (v *BTSets) String() string {

View File

@@ -11,8 +11,8 @@ var (
tdb *TDB tdb *TDB
Path string Path string
Port string Port string
Ssl bool
SslPort string SslPort string
Ssl bool
ReadOnly bool ReadOnly bool
HttpAuth bool HttpAuth bool
SearchWA bool SearchWA bool

View File

@@ -14,6 +14,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"server/log" "server/log"
"server/settings"
"time" "time"
) )
@@ -41,7 +42,7 @@ func generateSelfSignedCert(ips []string) ([]byte, []byte, error) {
template := x509.Certificate{ template := x509.Certificate{
SerialNumber: serialNumber, SerialNumber: serialNumber,
Subject: pkix.Name{ Subject: pkix.Name{
Organization: []string{"Torrserver"}, Organization: []string{"TorrServer"},
}, },
NotBefore: notBefore, NotBefore: notBefore,
NotAfter: notAfter, NotAfter: notAfter,
@@ -75,14 +76,14 @@ func MakeCertKeyFiles(ips []string) (string, string) {
log.TLogln("Error generating certificate:", err) log.TLogln("Error generating certificate:", err)
os.Exit(1) os.Exit(1)
} }
certFile, err := os.Create("server.pem") certFile, err := os.Create(filepath.Join(settings.Path, "server.pem"))
if err != nil { if err != nil {
log.TLogln("Error creating certificate file:", err) log.TLogln("Error creating certificate file:", err)
os.Exit(1) os.Exit(1)
} }
defer certFile.Close() defer certFile.Close()
privFile, err := os.Create("server.key") privFile, err := os.Create(filepath.Join(settings.Path, "server.key"))
if err != nil { if err != nil {
log.TLogln("Error creating private key file:", err) log.TLogln("Error creating private key file:", err)
os.Exit(1) os.Exit(1)
@@ -101,7 +102,7 @@ func MakeCertKeyFiles(ips []string) (string, string) {
} }
log.TLogln("Self-signed certificate and private key generated successfully.") log.TLogln("Self-signed certificate and private key generated successfully.")
return getAbsPath("server.pem"), getAbsPath("server.key") return getAbsPath(certFile.Name()), getAbsPath(privFile.Name())
} }
func getAbsPath(fileName string) string { func getAbsPath(fileName string) string {
@@ -119,16 +120,16 @@ func VerifyCertKeyFiles(certFile, keyFile, port string) error {
if err != nil { if err != nil {
return err return err
} }
// Check if the certificate chain is expired // Check if the certificate chain is expired
for _, cert := range cert.Certificate { for _, cert := range cert.Certificate {
x509Cert, err := x509.ParseCertificate(cert) x509Cert, err := x509.ParseCertificate(cert)
if err != nil { if err != nil {
return err return err
} }
if x509Cert.NotAfter.Before(time.Now()) { if x509Cert.NotAfter.Before(time.Now()) {
return errors.New("certificate has expired") return errors.New("certificate has expired")
} }
} }
// Create a TLS configuration // Create a TLS configuration
config := tls.Config{ config := tls.Config{
Certificates: []tls.Certificate{cert}, Certificates: []tls.Certificate{cert},