Skip to content

Commit

Permalink
Added enabled tls switch to NgxConfigEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Jul 28, 2022
1 parent 30a4222 commit d6a5bfc
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 91 deletions.
62 changes: 0 additions & 62 deletions frontend/src/views/domain/DomainAdd.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@

<template v-else-if="current_step===1">

<a-form-item :label="$gettext('Enable TLS')">
<a-switch @change="change_tls"/>
</a-form-item>

<ngx-config-editor
ref="ngx_config"
:ngx_config="ngx_config"
Expand Down Expand Up @@ -132,64 +128,6 @@ export default {
servers: [{}]
}
},
change_tls(r) {
if (r) {
// deep copy servers[0] to servers[1]
const server = JSON.parse(JSON.stringify(this.ngx_config.servers[0]))
this.ngx_config.servers.push(server)
this.$refs.ngx_config.current_server_index = 1
const servers = this.ngx_config.servers
let i = 0
while (i < servers[1].directives.length) {
const v = servers[1].directives[i]
if (v.directive === 'listen') {
servers[1].directives.splice(i, 1)
} else {
i++
}
}
servers[1].directives.splice(0, 0, {
directive: 'listen',
params: '443 ssl http2'
}, {
directive: 'listen',
params: '[::]:443 ssl http2'
})
const directivesMap = this.$refs.ngx_config.directivesMap
const server_name = directivesMap['server_name'][0]
if (!directivesMap['ssl_certificate']) {
servers[1].directives.splice(server_name.idx + 1, 0, {
directive: 'ssl_certificate',
params: ''
})
}
setTimeout(() => {
if (!directivesMap['ssl_certificate_key']) {
servers[1].directives.splice(server_name.idx + 2, 0, {
directive: 'ssl_certificate_key',
params: ''
})
}
}, 100)
} else {
// remove servers[1]
this.$refs.ngx_config.current_server_index = 0
if (this.ngx_config.servers.length === 2) {
this.ngx_config.servers.splice(1, 1)
}
}
}
},
computed: {
has_server_name() {
Expand Down
135 changes: 106 additions & 29 deletions frontend/src/views/domain/ngx_conf/NgxConfigEditor.vue
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
<template>
<a-tabs v-model="current_server_index">
<a-tab-pane :tab="'Server '+(k+1)" v-for="(v,k) in ngx_config.servers" :key="k">

<div class="tab-content">
<template v-if="support_ssl&&enabled">
<cert-info :domain="name" v-if="name"/>
<issue-cert
:current_server_directives="current_server_directives"
:directives-map="directivesMap"
v-model="auto_cert"
/>
<cert-info :current_server_directives="current_server_directives"
:directives-map="directivesMap"
v-model="auto_cert"/>
</template>

<a-form-item :label="$gettext('Comments')" v-if="v.comments">
<p style="white-space: pre-wrap;">{{ v.comments }}</p>
</a-form-item>

<directive-editor :ngx_directives="v.directives" :key="update"/>

<location-editor :locations="v.locations"/>
</div>

</a-tab-pane>
</a-tabs>
<div>
<a-form-item :label="$gettext('Enable TLS')" v-if="!support_ssl">
<a-switch @change="change_tls"/>
</a-form-item>

<a-tabs v-model="current_server_index">
<a-tab-pane :tab="'Server '+(k+1)" v-for="(v,k) in ngx_config.servers" :key="k">

<div class="tab-content">
<template v-if="current_support_ssl&&enabled">
<cert-info :domain="name" v-if="name"/>
<issue-cert
:current_server_directives="current_server_directives"
:directives-map="directivesMap"
v-model="auto_cert"
/>
<cert-info :current_server_directives="current_server_directives"
:directives-map="directivesMap"
v-model="auto_cert"/>
</template>

<a-form-item :label="$gettext('Comments')" v-if="v.comments">
<p style="white-space: pre-wrap;">{{ v.comments }}</p>
</a-form-item>

<directive-editor :ngx_directives="v.directives" :key="update"/>

<location-editor :locations="v.locations"/>
</div>

</a-tab-pane>
</a-tabs>
</div>

</template>

<script>
Expand All @@ -47,6 +54,7 @@ export default {
current_server_index: 0,
update: 0,
name: this.$route.params?.name?.toString() ?? '',
init_ssl_status: false
}
},
model: {
Expand All @@ -58,7 +66,64 @@ export default {
if (this.name && this.$refs['cert-info' + this.current_server_index]) {
this.$refs['cert-info' + this.current_server_index].get()
}
}
},
change_tls(r) {
if (r) {
// deep copy servers[0] to servers[1]
const server = JSON.parse(JSON.stringify(this.ngx_config.servers[0]))
this.ngx_config.servers.push(server)
this.current_server_index = 1
const servers = this.ngx_config.servers
let i = 0
while (i < servers[1].directives.length) {
const v = servers[1].directives[i]
if (v.directive === 'listen') {
servers[1].directives.splice(i, 1)
} else {
i++
}
}
servers[1].directives.splice(0, 0, {
directive: 'listen',
params: '443 ssl http2'
}, {
directive: 'listen',
params: '[::]:443 ssl http2'
})
const directivesMap = this.directivesMap
const server_name = directivesMap['server_name'][0]
if (!directivesMap['ssl_certificate']) {
servers[1].directives.splice(server_name.idx + 1, 0, {
directive: 'ssl_certificate',
params: ''
})
}
setTimeout(() => {
if (!directivesMap['ssl_certificate_key']) {
servers[1].directives.splice(server_name.idx + 2, 0, {
directive: 'ssl_certificate_key',
params: ''
})
}
}, 100)
} else {
// remove servers[1]
this.current_server_index = 0
if (this.ngx_config.servers.length === 2) {
this.ngx_config.servers.splice(1, 1)
}
}
},
},
computed: {
directivesMap: {
Expand All @@ -82,7 +147,19 @@ export default {
return this.ngx_config.servers[this.current_server_index].directives
}
},
support_ssl: {
support_ssl() {
const servers = this.ngx_config.servers
for (const server_key in servers) {
for (const k in servers[server_key].directives) {
const v = servers[server_key].directives[k]
if (v.directive === 'listen' && v.params.indexOf('ssl') > 0) {
return true
}
}
}
return false
},
current_support_ssl: {
get() {
if (this.directivesMap.listen) {
for (const v of this.directivesMap.listen) {
Expand Down

0 comments on commit d6a5bfc

Please sign in to comment.