IssueCert.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <script setup lang="ts">
  2. import {useGettext} from 'vue3-gettext'
  3. import {computed, h, nextTick, onMounted, ref, VNode, watch} from 'vue'
  4. import {message} from 'ant-design-vue'
  5. import domain from '@/api/domain'
  6. import websocket from '@/lib/websocket'
  7. import Template from '@/views/template/Template.vue'
  8. const {$gettext, interpolate} = useGettext()
  9. const props = defineProps(['directivesMap', 'current_server_directives', 'enabled'])
  10. const emit = defineEmits(['changeEnabled', 'callback', 'update:enabled'])
  11. const issuing_cert = ref(false)
  12. const modalVisible = ref(false)
  13. function onchange(r: boolean) {
  14. emit('changeEnabled', r)
  15. change_auto_cert(r)
  16. if (r) {
  17. job()
  18. }
  19. }
  20. function job() {
  21. issuing_cert.value = true
  22. if (no_server_name.value) {
  23. message.error($gettext('server_name not found in directives'))
  24. issuing_cert.value = false
  25. return
  26. }
  27. const server_name = props.directivesMap['server_name'][0]
  28. if (!props.directivesMap['ssl_certificate']) {
  29. props.current_server_directives.splice(server_name.idx + 1, 0, {
  30. directive: 'ssl_certificate',
  31. params: ''
  32. })
  33. }
  34. nextTick(() => {
  35. if (!props.directivesMap['ssl_certificate_key']) {
  36. const ssl_certificate = props.directivesMap['ssl_certificate'][0]
  37. props.current_server_directives.splice(ssl_certificate.idx + 1, 0, {
  38. directive: 'ssl_certificate_key',
  39. params: ''
  40. })
  41. }
  42. }).then(() => {
  43. issue_cert(name.value, callback)
  44. })
  45. }
  46. function callback(ssl_certificate: string, ssl_certificate_key: string) {
  47. props.directivesMap['ssl_certificate'][0]['params'] = ssl_certificate
  48. props.directivesMap['ssl_certificate_key'][0]['params'] = ssl_certificate_key
  49. }
  50. function change_auto_cert(r: boolean) {
  51. if (r) {
  52. domain.add_auto_cert(name.value).then(() => {
  53. message.success(interpolate($gettext('Auto-renewal enabled for %{name}'), {name: name.value}))
  54. }).catch(e => {
  55. message.error(e.message ?? interpolate($gettext('Enable auto-renewal failed for %{name}'), {name: name.value}))
  56. })
  57. } else {
  58. domain.remove_auto_cert(name.value).then(() => {
  59. message.success(interpolate($gettext('Auto-renewal disabled for %{name}'), {name: name.value}))
  60. }).catch(e => {
  61. message.error(e.message ?? interpolate($gettext('Disable auto-renewal failed for %{name}'), {name: name.value}))
  62. })
  63. }
  64. }
  65. const logContainer = ref(null)
  66. function log(msg: string) {
  67. const para = document.createElement('p')
  68. para.appendChild(document.createTextNode($gettext(msg)));
  69. (logContainer.value as any as Node).appendChild(para);
  70. (logContainer.value as any as Element).scroll({top: 320, left: 0, behavior: 'smooth'})
  71. }
  72. const issue_cert = async (server_name: string, callback: Function) => {
  73. progressStatus.value = 'active'
  74. modalClosable.value = false
  75. modalVisible.value = true
  76. progressPercent.value = 0;
  77. (logContainer.value as any as Element).innerHTML = ''
  78. log($gettext('Getting the certificate, please wait...'))
  79. const ws = websocket('/api/cert/issue', false)
  80. ws.onopen = () => {
  81. ws.send(JSON.stringify({
  82. server_name: server_name.trim().split(' ')
  83. }))
  84. }
  85. ws.onmessage = m => {
  86. const r = JSON.parse(m.data)
  87. log(r.message)
  88. switch (r.status) {
  89. case 'info':
  90. progressPercent.value += 5
  91. break
  92. default:
  93. modalClosable.value = true
  94. issuing_cert.value = false
  95. if (r.status === 'success' && r.ssl_certificate !== undefined && r.ssl_certificate_key !== undefined) {
  96. progressStatus.value = 'success'
  97. progressPercent.value = 100
  98. callback(r.ssl_certificate, r.ssl_certificate_key)
  99. } else {
  100. progressStatus.value = 'exception'
  101. }
  102. break
  103. }
  104. }
  105. }
  106. const no_server_name = computed(() => {
  107. return props.directivesMap['server_name']?.length === 0
  108. })
  109. const name = computed(() => {
  110. return props.directivesMap['server_name'][0].params.trim()
  111. })
  112. const enabled = computed({
  113. get() {
  114. return props.enabled
  115. },
  116. set(value) {
  117. emit('update:enabled', value)
  118. }
  119. })
  120. watch(no_server_name, () => {
  121. emit('update:enabled', false)
  122. onchange(false)
  123. })
  124. const progressStrokeColor = {
  125. from: '#108ee9',
  126. to: '#87d068'
  127. }
  128. const progressPercent = ref(0)
  129. const progressStatus = ref('active')
  130. const modalClosable = ref(false)
  131. </script>
  132. <template>
  133. <a-modal
  134. :title="$gettext('Obtaining certificate')"
  135. v-model:visible="modalVisible"
  136. :mask-closable="modalClosable"
  137. :footer="null" :closable="modalClosable" force-render>
  138. <a-progress
  139. :stroke-color="progressStrokeColor"
  140. :percent="progressPercent"
  141. :status="progressStatus"
  142. />
  143. <div class="issue-cert-log-container" ref="logContainer"/>
  144. </a-modal>
  145. <div class="issue-cert">
  146. <a-form-item :label="$gettext('Encrypt website with Let\'s Encrypt')">
  147. <a-switch
  148. :loading="issuing_cert"
  149. v-model:checked="enabled"
  150. @change="onchange"
  151. :disabled="no_server_name"
  152. />
  153. <a-alert
  154. v-if="no_server_name"
  155. :message="$gettext('Warning')"
  156. type="warning"
  157. show-icon
  158. >
  159. <template slot="description">
  160. <span v-if="no_server_name" v-translate>
  161. server_name parameter is required
  162. </span>
  163. </template>
  164. </a-alert>
  165. </a-form-item>
  166. <a-alert type="info" closable :message="$gettext('Note')">
  167. <template #description>
  168. <p v-translate>
  169. The server_name in the current configuration must be the domain name
  170. you need to get the certificate.
  171. </p>
  172. <p v-translate>
  173. The certificate for the domain will be checked every hour,
  174. and will be renewed if it has been more than 1 month since it was last issued.
  175. </p>
  176. <p v-translate>
  177. Make sure you have configured a reverse proxy for .well-known
  178. directory to HTTPChallengePort (default: 9180) before getting the certificate.
  179. </p>
  180. </template>
  181. </a-alert>
  182. </div>
  183. </template>
  184. <style lang="less">
  185. .issue-cert-log-container {
  186. height: 320px;
  187. overflow: scroll;
  188. background-color: #f3f3f3;
  189. border-radius: 4px;
  190. margin-top: 15px;
  191. padding: 10px;
  192. p {
  193. font-size: 12px;
  194. line-height: 1.3;
  195. }
  196. }
  197. </style>
  198. <style lang="less" scoped>
  199. .issue-cert {
  200. margin: 15px 0;
  201. }
  202. .switch-wrapper {
  203. position: relative;
  204. .text {
  205. position: absolute;
  206. top: 50%;
  207. transform: translateY(-50%);
  208. margin-left: 10px;
  209. }
  210. }
  211. </style>