Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Installing caprover on custom ports (not 80,443,3000). Tested on synology nas. Changes for https://github.com/caprover/caprover/issues/776 #2220

Merged
merged 7 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions dev-scripts/dev-clean-run-as-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 11,15 @@ sleep 1s
docker secret rm captain-salt
docker build -t captain-debug -f dockerfile-captain.debug .
rm -rf /captain && mkdir /captain
mkdir -p /captain/data/shared-logs
chmod -R 777 /captain

docker run \
-e "CAPTAIN_IS_DEBUG=1" \
-e "MAIN_NODE_IP_ADDRESS=127.0.0.1" \
-e "CAPTAIN_HOST_HTTP_PORT=10083" \
-e "CAPTAIN_HOST_HTTPS_PORT=10443" \
-e "CAPTAIN_HOST_ADMIN_PORT=13000" \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /captain:/captain \
-v $(pwd):/usr/src/app captain-debug
Expand Down
5 changes: 3 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 84,7 @@ app.use(function (req, res, next) {
req.secure || req.get('X-Forwarded-Proto') === 'https'

if (!isRequestSsl) {
const newUrl = `https://${req.get('host')}${req.originalUrl}`
const newUrl = `https://${req.hostname}:${CaptainConstants.configs.nginxPortNumber80}${req.originalUrl}`
res.redirect(302, newUrl)
return
}
Expand Down Expand Up @@ -113,7 113,8 @@ app.use(CaptainConstants.netDataRelativePath, function (req, res, next) {

const newUrl =
(isRequestSsl ? 'https://' : 'http://')
req.get('host')
req.hostname ':'
(isRequestSsl ? CaptainConstants.configs.nginxPortNumber443 : CaptainConstants.configs.nginxPortNumber80)
CaptainConstants.netDataRelativePath
'/'
res.redirect(302, newUrl)
Expand Down
11 changes: 7 additions & 4 deletions src/datastore/DataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 4,8 @@
import Configstore = require('configstore')
import fs = require('fs-extra')
import {
AutomatedCleanupConfigsCleaner,
IAutomatedCleanupConfigs,
AutomatedCleanupConfigsCleaner,
IAutomatedCleanupConfigs,
} from '../models/AutomatedCleanupConfigs'
import CapRoverTheme from '../models/CapRoverTheme'
import { GoAccessInfo } from '../models/GoAccessInfo'
Expand Down Expand Up @@ -249,8 249,11 @@ class DataStore {
const goAccessInfo = (self.data.get(GOACCESS_INFO) ||
{}) as GoAccessInfo
goAccessInfo.isEnabled = goAccessInfo.isEnabled || false
goAccessInfo.data.rotationFrequencyCron =
goAccessInfo.data.rotationFrequencyCron ?? '0 0 1 * *' // monthly
if(!goAccessInfo.data) {
goAccessInfo.data = {
rotationFrequencyCron:'0 0 1 * *' // monthly
}
}
return Promise.resolve(goAccessInfo)
}

Expand Down
21 changes: 1 addition & 20 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 30,7 @@ function startServer() {
* Get port from environment and store in Express.
*/

const port = normalizePort(process.env.PORT || '3000')
const port = 3000
app.set('port', port)

/**
Expand All @@ -47,25 47,6 @@ function startServer() {
server.on('error', onError)
server.on('listening', onListening)

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val: string) {
const port = parseInt(val, 10)

if (isNaN(port)) {
// named pipe
return val
}

if (port >= 0) {
// port number
return port
}

return false
}

/**
* Event listener for HTTP server "error" event.
Expand Down
6 changes: 4 additions & 2 deletions src/user/system/LoadBalancerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 532,7 @@ class LoadBalancerManager {
serviceName: CaptainConstants.captainServiceName,
domain: captainDomain,
serviceExposedPort:
CaptainConstants.captainServiceExposedPort,
CaptainConstants.configs.adminPortNumber3000,
defaultHtmlDir:
CaptainConstants.nginxStaticRootDir
CaptainConstants.nginxDefaultHtmlDir,
Expand Down Expand Up @@ -681,7 681,8 @@ class LoadBalancerManager {
protocol: 'udp',
publishMode: 'host',
containerPort: 443,
hostPort: 443,
hostPort:
CaptainConstants.configs.nginxPortNumber443,
},
],
nodeId,
Expand Down Expand Up @@ -793,6 794,7 @@ class LoadBalancerManager {
0
)
} else {
Logger.d('Captain Nginx is NOT running.. ')
return createNginxServiceOnNode(myNodeId).then(function () {
return myNodeId
})
Expand Down
15 changes: 8 additions & 7 deletions src/utils/CaptainConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 64,12 @@ const configs = {
// this is added in 1.13 just as a safety - remove this after 1.14
disableEncryptedCheck: false,

nginxPortNumber80: 80,

nginxPortNumber443: 443,
// The port can be overridden via env variable CAPTAIN_HOST_HTTP_PORT
nginxPortNumber80: EnvVars.CAPTAIN_HOST_HTTP_PORT,
// The port can be overridden via env variable CAPTAIN_HOST_HTTPS_PORT
nginxPortNumber443: EnvVars.CAPTAIN_HOST_HTTPS_PORT,
// The port can be overridden via env variable CAPTAIN_HOST_ADMIN_PORT
adminPortNumber3000: EnvVars.CAPTAIN_HOST_ADMIN_PORT,
}

export interface CertbotCertCommandRule {
Expand All @@ -89,8 92,6 @@ const data = {

isDebug: EnvVars.CAPTAIN_IS_DEBUG,

captainServiceExposedPort: 3000,

rootNameSpace: 'captain',

// *********************** Disk Paths ************************
Expand Down Expand Up @@ -204,7 205,7 @@ const data = {
// ********************* ETC ************************

disableFirewallCommand:
'ufw allow 80,443,3000,996,7946,4789,2377/tcp; ufw allow 7946,4789,2377/udp; ',
'ufw allow ' configs.nginxPortNumber80 ',' configs.nginxPortNumber443 ',' configs.adminPortNumber3000 ',996,7946,4789,2377/tcp; ufw allow 7946,4789,2377/udp; ',

gitShaEnvVarKey: 'CAPROVER_GIT_COMMIT_SHA',
}
Expand Down Expand Up @@ -246,7 247,7 @@ if (data.isDebug) {

data.debugSourceDirectory = devDirectoryOnLocalMachine
data.configs.publishedNameOnDockerHub = 'captain-debug'
data.configs.nginxPortNumber80 = 80
// data.configs.nginxPortNumber80 = 80
}

export default data
22 changes: 17 additions & 5 deletions src/utils/CaptainInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 280,13 @@ export function install() {
return startServerOnPort_80_443_3000()
})
.then(function () {
return checkPortOrThrow(myIp4, 80)
return checkPortOrThrow(myIp4, CaptainConstants.configs.nginxPortNumber80 as any)
})
.then(function () {
return checkPortOrThrow(myIp4, 443)
return checkPortOrThrow(myIp4, CaptainConstants.configs.nginxPortNumber443 as any)
})
.then(function () {
return checkPortOrThrow(myIp4, 3000)
return checkPortOrThrow(myIp4, CaptainConstants.configs.adminPortNumber3000 as any)
})
.then(function () {
const imageName = CaptainConstants.configs.nginxImageName
Expand Down Expand Up @@ -332,6 332,18 @@ export function install() {
key: EnvVar.keys.IS_CAPTAIN_INSTANCE,
value: '1',
})
env.push({
key: EnvVar.keys.CAPTAIN_HOST_ADMIN_PORT,
value: CaptainConstants.configs.adminPortNumber3000 '',
})
env.push({
key: EnvVar.keys.CAPTAIN_HOST_HTTP_PORT,
value: CaptainConstants.configs.nginxPortNumber80 '',
})
env.push({
key: EnvVar.keys.CAPTAIN_HOST_HTTPS_PORT,
value: CaptainConstants.configs.nginxPortNumber443 '',
})

if (EnvVar.DEFAULT_PASSWORD) {
env.push({
Expand Down Expand Up @@ -386,8 398,8 @@ export function install() {
ports.push({
protocol: 'tcp',
publishMode: 'host',
containerPort: CaptainConstants.captainServiceExposedPort,
hostPort: CaptainConstants.captainServiceExposedPort,
containerPort: 3000,
hostPort: CaptainConstants.configs.adminPortNumber3000,
})

return DockerApi.get().createServiceOnNodeId(
Expand Down
34 changes: 34 additions & 0 deletions src/utils/EnvVars.ts
Original file line number Diff line number Diff line change
@@ -1,3 1,27 @@

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val: string | undefined, defaultPort: number): number {
if (val === undefined) {
return defaultPort
}
const port = parseInt(val, 10)

if (isNaN(port)) {
// named pipe
return defaultPort
}

if (port >= 0) {
// port number
return port
}

return defaultPort
}

export default {
keys: {
CAPTAIN_DOCKER_API: 'CAPTAIN_DOCKER_API',
Expand All @@ -6,6 30,9 @@ export default {
IS_CAPTAIN_INSTANCE: 'IS_CAPTAIN_INSTANCE',
DEMO_MODE_ADMIN_IP: 'DEMO_MODE_ADMIN_IP',
CAPTAIN_BASE_DIRECTORY: 'CAPTAIN_BASE_DIRECTORY',
CAPTAIN_HOST_HTTP_PORT: 'CAPTAIN_HOST_HTTP_PORT',
CAPTAIN_HOST_HTTPS_PORT: 'CAPTAIN_HOST_HTTPS_PORT',
CAPTAIN_HOST_ADMIN_PORT: 'CAPTAIN_HOST_ADMIN_PORT',
},

BY_PASS_PROXY_CHECK: process.env.BY_PASS_PROXY_CHECK,
Expand All @@ -14,6 41,13 @@ export default {

CAPTAIN_IS_DEBUG: !!process.env.CAPTAIN_IS_DEBUG,

// Host ports - external to container. Refer it via CaptainConstants.configs.nginxPortNumber80
CAPTAIN_HOST_HTTP_PORT: normalizePort(process.env.CAPTAIN_HOST_HTTP_PORT, 80),//Tested with 10080
// Host ports - external to container. Refer it via CaptainConstants.configs.nginxPortNumber443
CAPTAIN_HOST_HTTPS_PORT: normalizePort(process.env.CAPTAIN_HOST_HTTPS_PORT, 443),//Tested with 10443
// Host ports - external to container. Refer it via CaptainConstants.configs.adminPortNumber3000
CAPTAIN_HOST_ADMIN_PORT: normalizePort(process.env.CAPTAIN_HOST_ADMIN_PORT, 3000),//Tested with 13000

MAIN_NODE_IP_ADDRESS: process.env.MAIN_NODE_IP_ADDRESS,

ACCEPTED_TERMS: !!process.env.ACCEPTED_TERMS,
Expand Down
Loading