Streams Basic Load Balancing (Allows multiple Forwarding Hosts)

This commit is contained in:
Guillaume Liautard 2022-04-14 01:10:28 +02:00
parent 4db34f5894
commit ec60004c96
9 changed files with 137 additions and 32 deletions

View File

@ -0,0 +1,40 @@
const migrate_name = 'identifier_for_migrate';
const logger = require('../logger').migrate;
/**
* Migrate
*
* @see http://knexjs.org/#Schema
*
* @param {Object} knex
* @param {Promise} Promise
* @returns {Promise}
*/
exports.up = function (knex, Promise) {
logger.info('[' + migrate_name + '] Migrating Up...');
return knex.schema.table('stream', (table) => {
table.renameColumn('forwarding_host', 'forwarding_hosts');
})
.then(function () {
logger.info('[' + migrate_name + '] stream Table altered');
});
};
/**
* Undo Migrate
*
* @param {Object} knex
* @param {Promise} Promise
* @returns {Promise}
*/
exports.down = function (knex, Promise) {
logger.info('[' + migrate_name + '] Migrating Down...');
return knex.schema.table('stream', (table) => {
table.renameColumn('forwarding_hosts', 'forwarding_host');
})
.then(function () {
logger.info('[' + migrate_name + '] stream Table altered');
});
};

View File

@ -13,6 +13,11 @@ class Stream extends Model {
this.created_on = now(); this.created_on = now();
this.modified_on = now(); this.modified_on = now();
// Default for forwarding_hosts
if (typeof this.forwarding_hosts === 'undefined') {
this.forwarding_hosts = [];
}
// Default for meta // Default for meta
if (typeof this.meta === 'undefined') { if (typeof this.meta === 'undefined') {
this.meta = {}; this.meta = {};
@ -21,6 +26,11 @@ class Stream extends Model {
$beforeUpdate () { $beforeUpdate () {
this.modified_on = now(); this.modified_on = now();
// Sort domain_names
if (typeof this.forwarding_hosts !== 'undefined') {
this.forwarding_hosts.sort();
}
} }
static get name () { static get name () {
@ -32,7 +42,7 @@ class Stream extends Model {
} }
static get jsonAttributes () { static get jsonAttributes () {
return ['meta']; return ['forwarding_hosts', 'meta'];
} }
static get relationMappings () { static get relationMappings () {

View File

@ -20,20 +20,26 @@
"minimum": 1, "minimum": 1,
"maximum": 65535 "maximum": 65535
}, },
"forwarding_host": { "forwarding_hosts": {
"anyOf": [ "type": "array",
{ "minItems": 1,
"$ref": "../definitions.json#/definitions/domain_name" "maxItems": 15,
}, "uniqueItems": true,
{ "items": {
"type": "string", "anyOf": [
"format": "ipv4" {
}, "$ref": "../definitions.json#/definitions/domain_name"
{ },
"type": "string", {
"format": "ipv6" "type": "string",
} "format": "ipv4"
] },
{
"type": "string",
"format": "ipv6"
}
]
}
}, },
"forwarding_port": { "forwarding_port": {
"type": "integer", "type": "integer",
@ -66,8 +72,8 @@
"incoming_port": { "incoming_port": {
"$ref": "#/definitions/incoming_port" "$ref": "#/definitions/incoming_port"
}, },
"forwarding_host": { "forwarding_hosts": {
"$ref": "#/definitions/forwarding_host" "$ref": "#/definitions/forwarding_hosts"
}, },
"forwarding_port": { "forwarding_port": {
"$ref": "#/definitions/forwarding_port" "$ref": "#/definitions/forwarding_port"
@ -118,15 +124,15 @@
"additionalProperties": false, "additionalProperties": false,
"required": [ "required": [
"incoming_port", "incoming_port",
"forwarding_host", "forwarding_hosts",
"forwarding_port" "forwarding_port"
], ],
"properties": { "properties": {
"incoming_port": { "incoming_port": {
"$ref": "#/definitions/incoming_port" "$ref": "#/definitions/incoming_port"
}, },
"forwarding_host": { "forwarding_hosts": {
"$ref": "#/definitions/forwarding_host" "$ref": "#/definitions/forwarding_hosts"
}, },
"forwarding_port": { "forwarding_port": {
"$ref": "#/definitions/forwarding_port" "$ref": "#/definitions/forwarding_port"
@ -165,8 +171,8 @@
"incoming_port": { "incoming_port": {
"$ref": "#/definitions/incoming_port" "$ref": "#/definitions/incoming_port"
}, },
"forwarding_host": { "forwarding_hosts": {
"$ref": "#/definitions/forwarding_host" "$ref": "#/definitions/forwarding_hosts"
}, },
"forwarding_port": { "forwarding_port": {
"$ref": "#/definitions/forwarding_port" "$ref": "#/definitions/forwarding_port"

View File

@ -3,6 +3,13 @@
# ------------------------------------------------------------ # ------------------------------------------------------------
{% if enabled %} {% if enabled %}
upstream stream_{{ incoming_port }}_tcp {
{% for forwarding_host in forwarding_hosts %}
server {{ forwarding_host }}:{{ forwarding_port }};
{%- endfor %}
}
{% if tcp_forwarding == 1 or tcp_forwarding == true -%} {% if tcp_forwarding == 1 or tcp_forwarding == true -%}
server { server {
listen {{ incoming_port }}; listen {{ incoming_port }};
@ -12,7 +19,7 @@ server {
#listen [::]:{{ incoming_port }}; #listen [::]:{{ incoming_port }};
{% endif %} {% endif %}
proxy_pass {{ forwarding_host }}:{{ forwarding_port }}; proxy_pass stream_{{ incoming_port }}_tcp;
# Custom # Custom
include /data/nginx/custom/server_stream[.]conf; include /data/nginx/custom/server_stream[.]conf;
@ -20,18 +27,26 @@ server {
} }
{% endif %} {% endif %}
{% if udp_forwarding == 1 or udp_forwarding == true %} {% if udp_forwarding == 1 or udp_forwarding == true %}
upstream stream_{{ incoming_port }}_udp {
{% for forwarding_host in forwarding_hosts %}
server {{ forwarding_host }}:{{ forwarding_port }};
{%- endfor %}
}
server { server {
listen {{ incoming_port }} udp; listen {{ incoming_port }} udp;
{% if ipv6 -%} {% if ipv6 -%}
listen [::]:{{ incoming_port }} udp; listen [::]:{{ incoming_port }} udp;
{% else -%} {% else -%}
#listen [::]:{{ incoming_port }} udp; #listen [::]:{{ incoming_port }} udp;
{% endif %} {% endif %}
proxy_pass {{ forwarding_host }}:{{ forwarding_port }};
proxy_pass stream_{{ incoming_port }}_udp;
# Custom # Custom
include /data/nginx/custom/server_stream[.]conf; include /data/nginx/custom/server_stream[.]conf;
include /data/nginx/custom/server_stream_udp[.]conf; include /data/nginx/custom/server_stream_udp[.]conf;
} }
{% endif %} {% endif %}
{% endif %} {% endif %}

View File

@ -14,8 +14,8 @@
</div> </div>
<div class="col-sm-8 col-md-8"> <div class="col-sm-8 col-md-8">
<div class="form-group"> <div class="form-group">
<label class="form-label"><%- i18n('streams', 'forwarding-host') %><span class="form-required">*</span></label> <label class="form-label"><%- i18n('streams', 'forwarding-hosts') %><span class="form-required">*</span></label>
<input type="text" name="forwarding_host" class="form-control text-monospace" placeholder="example.com or 10.0.0.1 or 2001:db8:3333:4444:5555:6666:7777:8888" value="<%- forwarding_host %>" autocomplete="off" maxlength="255" required> <input type="text" name="forwarding_hosts" class="form-control" placeholder="example.com or 10.0.0.1 or 2001:db8:3333:4444:5555:6666:7777:8888" id="input-forwarding-hosts" value="<%- forwarding_hosts.join(',') %>" required>
</div> </div>
</div> </div>
<div class="col-sm-4 col-md-4"> <div class="col-sm-4 col-md-4">

View File

@ -6,6 +6,8 @@ const template = require('./form.ejs');
require('jquery-serializejson'); require('jquery-serializejson');
require('jquery-mask-plugin'); require('jquery-mask-plugin');
require('selectize'); require('selectize');
const Helpers = require("../../../lib/helpers");
const certListItemTemplate = require("../certificates-list-item.ejs");
module.exports = Mn.View.extend({ module.exports = Mn.View.extend({
template: template, template: template,
@ -13,7 +15,7 @@ module.exports = Mn.View.extend({
ui: { ui: {
form: 'form', form: 'form',
forwarding_host: 'input[name="forwarding_host"]', forwarding_hosts: 'input[name="forwarding_hosts"]',
type_error: '.forward-type-error', type_error: '.forward-type-error',
buttons: '.modal-footer button', buttons: '.modal-footer button',
switches: '.custom-switch-input', switches: '.custom-switch-input',
@ -48,6 +50,10 @@ module.exports = Mn.View.extend({
data.tcp_forwarding = !!data.tcp_forwarding; data.tcp_forwarding = !!data.tcp_forwarding;
data.udp_forwarding = !!data.udp_forwarding; data.udp_forwarding = !!data.udp_forwarding;
if (typeof data.forwarding_hosts === 'string' && data.forwarding_hosts) {
data.forwarding_hosts = data.forwarding_hosts.split(',');
}
let method = App.Api.Nginx.Streams.create; let method = App.Api.Nginx.Streams.create;
let is_new = true; let is_new = true;
@ -76,6 +82,24 @@ module.exports = Mn.View.extend({
} }
}, },
onRender: function () {
let view = this;
// Domain names
this.ui.forwarding_hosts.selectize({
delimiter: ',',
persist: false,
maxOptions: 15,
create: function (input) {
return {
value: input,
text: input
};
},
createFilter: /^(?:\*\.)?(?:[^.*]+\.?)+[^.]$/
});
},
initialize: function (options) { initialize: function (options) {
if (typeof options.model === 'undefined' || !options.model) { if (typeof options.model === 'undefined' || !options.model) {
this.model = new StreamModel.Model(); this.model = new StreamModel.Model();

View File

@ -12,7 +12,11 @@
</div> </div>
</td> </td>
<td> <td>
<div class="text-monospace"><%- forwarding_host %>:<%- forwarding_port %></div> <div class="wrap">
<% forwarding_hosts.map(function(forwarding_host) { %>
<span class="tag hover-green"><%- forwarding_host %></span>
<% }); %>
</div>
</td> </td>
<td> <td>
<div> <div>
@ -24,6 +28,11 @@
<% } %> <% } %>
</div> </div>
</td> </td>
<td>
<div class="text-monospace">
<%- forwarding_port %>
</div>
</td>
<td> <td>
<% <%
var o = isOnline(); var o = isOnline();
@ -50,4 +59,4 @@
</div> </div>
</div> </div>
</td> </td>
<% } %> <% } %>

View File

@ -3,6 +3,7 @@
<th><%- i18n('streams', 'incoming-port') %></th> <th><%- i18n('streams', 'incoming-port') %></th>
<th><%- i18n('str', 'destination') %></th> <th><%- i18n('str', 'destination') %></th>
<th><%- i18n('streams', 'protocol') %></th> <th><%- i18n('streams', 'protocol') %></th>
<th><%- i18n('streams', 'forwarding-port') %></th>
<th><%- i18n('str', 'status') %></th> <th><%- i18n('str', 'status') %></th>
<% if (canManage) { %> <% if (canManage) { %>
<th>&nbsp;</th> <th>&nbsp;</th>

View File

@ -9,7 +9,7 @@ const model = Backbone.Model.extend({
created_on: null, created_on: null,
modified_on: null, modified_on: null,
incoming_port: null, incoming_port: null,
forwarding_host: null, forwarding_hosts: [],
forwarding_port: null, forwarding_port: null,
tcp_forwarding: true, tcp_forwarding: true,
udp_forwarding: false, udp_forwarding: false,