Skip to content

Commit

Permalink
Port to int and set server timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
r4ulcl committed Dec 5, 2024
1 parent dafb914 commit 71802da
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 21 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ The manager requires a configuration file named `manager.conf` to be present in
},
"statusCheckSeconds": 10,
"StatusCheckDown": 360,
"port": "8080",
"httpPort": 8080,
"httpsPort": 8443,
"dbUsername": "your_username",
"dbPassword": "your_password",
"dbHost": "db",
Expand All @@ -155,7 +156,8 @@ The manager requires a configuration file named `manager.conf` to be present in
- `workers`: A map of worker names and their corresponding tokens for authentication. (In this case all workers use the same token called workers)
- `statusCheckSeconds`: The interval in seconds between status check requests from the manager to the workers.
- `StatusCheckDown`: The number of seconds after which a worker is marked as down if the status check request fails.
- `port`: The port on which the manager should listen for incoming connections.
- `httpPort`: The port on which the manager should listen for incoming connections without TLS.
- `httpsPort`: The port on which the manager should listen for incoming connections with TLS.
- `dbUsername`: The username for the database connection.
- `dbPassword`: The password for the database connection.
- `dbHost`: The hostname of the database server.
Expand All @@ -173,7 +175,7 @@ The worker requires a configuration file named `workerouter.conf` to be present
"name": "",
"iddleThreads": 2,
"managerIP": "127.0.0.1",
"managerPort": "8080",
"managerPort": 8443,
"managerOauthToken": "IeH0vpYFz2Yol6RdLvYZz62TFMv5FF",
"CA": "./certs/ca-cert.pem",
"insecureModules": true,
Expand Down
4 changes: 2 additions & 2 deletions manager.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
},
"statusCheckSeconds": 10,
"StatusCheckDown": 360,
"httpPort": "8080",
"httpsPort": "8443",
"httpPort": 8080,
"httpsPort": 8443,
"dbUsername": "your_username",
"dbPassword": "your_password",
"dbHost": "db",
Expand Down
27 changes: 22 additions & 5 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,30 +320,47 @@ func StartManager(swagger bool, configFile, configSSHFile, configCloudFile strin
http.Handle("/", router)

// Start the servers
if config.CertFolder != "" {
if config.CertFolder != "" && config.HttpsPort > 0 && config.HttpsPort > 0 {

// Set string for the HTTPS port
httpsAddr := fmt.Sprintf(":%s", config.HttpsPort)
httpsAddr := fmt.Sprintf(":%d", config.HttpsPort)
if verbose {
log.Println("Starting HTTPS server on port", config.HttpsPort)
}

// Start HTTPS server with timeouts
httpsServer := &http.Server{
Addr: httpsAddr,
Handler: router, // Assuming you have a router defined
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
}
// Start HTTPS server in a goroutine
go func() {
err := http.ListenAndServeTLS(httpsAddr, config.CertFolder+"/cert.pem", config.CertFolder+"/key.pem", router)
err := httpsServer.ListenAndServeTLS(config.CertFolder+"/cert.pem", config.CertFolder+"/key.pem")
if err != nil {
log.Fatalf("Error starting HTTPS server: %v", err)
}
}()
}

// Set string for the HTTP port
httpAddr := fmt.Sprintf(":%s", config.HttpPort)
httpAddr := fmt.Sprintf(":%d", config.HttpPort)
if verbose {
log.Println("Starting HTTP server on port", config.HttpPort)
}

server := &http.Server{
Addr: httpAddr,
Handler: nil, // or your router
ReadTimeout: 10 * time.Second, // Time to read the request
WriteTimeout: 10 * time.Second, // Time to send the response
IdleTimeout: 15 * time.Second, // Time to wait for the next request
}

// Start HTTP server
err = http.ListenAndServe(httpAddr, nil)
err = server.ListenAndServe()
if err != nil {
log.Fatalf("Error starting HTTP server: %v", err)
}
Expand Down
9 changes: 5 additions & 4 deletions manager/sshTunnel/sshTunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net"
"os"
"strconv"
"time"

"github.com/r4ulcl/nTask/manager/utils"
Expand Down Expand Up @@ -39,7 +40,7 @@ func publicKeyFile(file string) (ssh.AuthMethod, error) {
// Maintain a map of active SSH connections
var activeConnections = make(map[string]*ssh.Client)

func StartSSH(config *utils.ManagerSSHConfig, httpPort, httpsPort string, verbose, debug bool) {
func StartSSH(config *utils.ManagerSSHConfig, httpPort, httpsPort int, verbose, debug bool) {
log.Println("SSH StartSSH")
for {
for ip, port := range config.IPPort {
Expand Down Expand Up @@ -91,9 +92,9 @@ func StartSSH(config *utils.ManagerSSHConfig, httpPort, httpsPort string, verbos
activeConnections[connectionKey] = sshClient

// Port forwarding for HTTP and HTTPS
forwardPort := func(localPort, remotePort string) {
remoteAddr := "127.0.0.1:" + remotePort
localAddr := "127.0.0.1:" + localPort
forwardPort := func(localPort, remotePort int) {
remoteAddr := "127.0.0.1:" + strconv.Itoa(remotePort)
localAddr := "127.0.0.1:" + strconv.Itoa(localPort)

if debug {
log.Printf("SSH forwarding remoteAddr: %s to localAddr: %s", remoteAddr, localAddr)
Expand Down
4 changes: 2 additions & 2 deletions manager/utils/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
type ManagerConfig struct {
Users map[string]string `json:"users"`
Workers map[string]string `json:"workers"`
HttpPort string `json:"httpPort"`
HttpsPort string `json:"httpsPort"`
HttpPort int `json:"httpPort"`
HttpsPort int `json:"httpsPort"`
DBUsername string `json:"dbUsername"`
DBPassword string `json:"dbPassword"`
DBHost string `json:"dbHost"`
Expand Down
2 changes: 1 addition & 1 deletion worker.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "",
"defaultThreads": 2,
"managerIP" : "nTask_manager",
"managerPort" : "8443",
"managerPort" : 8443,
"managerOauthToken": "IeH0vpYFz2Yol6RdLvYZz62TFMv5FF",
"CA": "./certs/ca-cert.pem",
"insecureModules": false,
Expand Down
8 changes: 5 additions & 3 deletions worker/managerrequest/managerRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"log"
"net/http"
"strconv"
"sync"

"github.com/gorilla/websocket"
Expand All @@ -18,14 +19,15 @@ func CreateWebsocket(config *utils.WorkerConfig, caCertPath string,
headers.Set("Authorization", config.ManagerOauthToken)

var serverAddr string
portStr := strconv.Itoa(config.ManagerPort)
if transport, ok := config.ClientHTTP.Transport.(*http.Transport); ok {
if transport.TLSClientConfig != nil {
serverAddr = "wss://" + config.ManagerIP + ":" + config.ManagerPort + "/worker/websocket"
serverAddr = "wss://" + config.ManagerIP + ":" + portStr + "/worker/websocket"
} else {
serverAddr = "ws://" + config.ManagerIP + ":" + config.ManagerPort + "/worker/websocket"
serverAddr = "ws://" + config.ManagerIP + ":" + portStr + "/worker/websocket"
}
} else {
serverAddr = "wss://" + config.ManagerIP + ":" + config.ManagerPort + "/worker/websocket"
serverAddr = "wss://" + config.ManagerIP + ":" + portStr + "/worker/websocket"
}

if debug {
Expand Down
2 changes: 1 addition & 1 deletion worker/utils/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type WorkerConfig struct {
Name string `json:"name"`
DefaultThreads int `json:"defaultThreads"`
ManagerIP string `json:"managerIP"`
ManagerPort string `json:"managerPort"`
ManagerPort int `json:"managerPort"`
ManagerOauthToken string `json:"managerOauthToken"`
CA string `json:"ca"`
InsecureModules bool `json:"insecureModules"`
Expand Down

0 comments on commit 71802da

Please sign in to comment.