Skip to content

Commit

Permalink
auto generate cert keys
Browse files Browse the repository at this point in the history
  • Loading branch information
alireza0 committed Jun 13, 2024
1 parent d057076 commit b144aec
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 20 deletions.
5 changes: 5 additions & 0 deletions backend/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ func (a *APIHandler) getHandler(c *gin.Context) {
count := c.Query("c")
changes := a.ConfigService.GetChanges(actor, chngKey, count)
jsonObj(c, changes, nil)
case "keypairs":
kType := c.Query("k")
options := c.Query("o")
keypair := a.ServerService.GenKeypair(kType, options)
jsonObj(c, keypair, nil)
default:
jsonMsg(c, "API call", nil)
}
Expand Down
20 changes: 20 additions & 0 deletions backend/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,23 @@ func (s *ServerService) GetLogs(service string, count string, level string) []st

return lines
}

func (s *ServerService) GenKeypair(keyType string, options string) []string {
if len(keyType) == 0 {
return []string{"No keypair to generate"}
}
sbExec := s.GetBinaryPath()
cmdArgs := []string{"generate", keyType + "-keypair"}
if keyType == "tls" || keyType == "ech" {
cmdArgs = append(cmdArgs, options)
}
// Run the command
cmd := exec.Command(sbExec, cmdArgs...)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return []string{"Failed to generate keypair"}
}
return strings.Split(out.String(), "\n")
}
70 changes: 66 additions & 4 deletions frontend/src/components/Ech.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,23 @@
>{{ $t('tls.useText') }}</v-btn>
</v-btn-toggle>
</v-col>
<v-spacer></v-spacer>
<v-col cols="auto">
<v-btn
variant="tonal"
density="compact"
icon="mdi-key-star"
@click="genECH"
:loading="loading">
<v-icon />
<v-tooltip activator="parent" location="top">
{{ $t('actions.generate') }}
</v-tooltip>
</v-btn>
</v-col>
</v-row>
<v-row v-if="useEchPath == 0">
<v-col cols="12" sm="6">
<v-col cols="12">
<v-text-field
:label="$t('tls.keyPath')"
hide-details
Expand All @@ -41,19 +55,21 @@
</v-col>
</v-row>
<v-row v-else>
<v-col cols="12" sm="6">
<v-col cols="12">
<v-textarea
:label="$t('tls.key')"
hide-details
rows="3"
v-model="echKeyText">
</v-textarea>
</v-col>
</v-row>
<v-row>
<v-col cols="12" sm="6">
<v-col cols="12">
<v-textarea
:label="$t('tls.cert')"
hide-details
rows="3"
v-model="echConfigText">
</v-textarea>
</v-col>
Expand All @@ -63,15 +79,61 @@
</template>

<script lang="ts">
import { i18n } from '@/locales'
import HttpUtils from '@/plugins/httputil'
import { ech } from '@/types/inTls'
import { push } from 'notivue'
export default {
props: ['iTls','oTls'],
data() {
return {
useEchPath: 0
useEchPath: 0,
loading: false,
}
},
methods: {
async genECH(){
this.loading = true
const msg = await HttpUtils.get('api/keypairs', { k: "ech", o: this.iTls.server_name?? "''" })
this.loading = false
if (msg.success && this.iTls.ech && this.oTls.ech) {
this.iTls.ech.key_path=undefined
this.useEchPath = 1
if (msg.obj.length>0){
let config = <string[]>[]
let key = <string[]>[]
let isConfig = false
let isKey = false
msg.obj.forEach((line:string) => {
if (line === "-----BEGIN ECH CONFIGS-----") {
isConfig = true
isKey = false
} else if (line === "-----END ECH CONFIGS-----") {
isConfig = false
} else if (line === "-----BEGIN ECH KEYS-----") {
isKey = true
isConfig = false
} else if (line === "-----END ECH KEYS-----") {
isKey = false
} else if (isConfig) {
config.push(line)
} else if (isKey) {
key.push(line)
}
})
this.iTls.ech.key = key?? undefined
this.oTls.ech.config = config?? undefined
} else {
push.error({
message: i18n.global.t('error') + ": " + msg.obj
})
}
}
},
},
computed: {
ech() {
return <ech>this.$props.iTls.ech
Expand Down
114 changes: 108 additions & 6 deletions frontend/src/layouts/modals/Tls.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@
>{{ $t('tls.useText') }}</v-btn>
</v-btn-toggle>
</v-col>
<v-spacer></v-spacer>
<v-col cols="auto">
<v-btn
variant="tonal"
density="compact"
icon="mdi-key-star"
@click="genSelfSigned"
:loading="loading">
<v-icon />
<v-tooltip activator="parent" location="top">
{{ $t('actions.generate') }}
</v-tooltip>
</v-btn>
</v-col>
</v-row>
<v-row v-if="usePath == 0">
<v-col cols="12" sm="6">
Expand All @@ -108,14 +122,14 @@
</v-col>
</v-row>
<v-row v-else>
<v-col cols="12" sm="6">
<v-col cols="12">
<v-textarea
:label="$t('tls.cert')"
hide-details
v-model="certText">
</v-textarea>
</v-col>
<v-col cols="12" sm="6">
<v-col cols="12">
<v-textarea
:label="$t('tls.key')"
hide-details
Expand Down Expand Up @@ -158,26 +172,42 @@
v-model="server_port">
</v-text-field>
</v-col>
<v-spacer></v-spacer>
<v-col cols="auto">
<v-btn
variant="tonal"
density="compact"
icon="mdi-key-star"
@click="genRealityKey"
:loading="loading">
<v-icon />
<v-tooltip activator="parent" location="top">
{{ $t('actions.generate') }}
</v-tooltip>
</v-btn>
</v-col>
</v-row>
<v-row>
<v-col cols="12" md="6">
<v-col cols="12">
<v-text-field
:label="$t('tls.privKey')"
hide-details
v-model="inTls.reality.private_key">
</v-text-field>
</v-col>
<v-col cols="12" md="6">
<v-col cols="12">
<v-text-field
:label="$t('tls.pubKey')"
hide-details
v-model="outTls.reality.public_key">
</v-text-field>
</v-col>
<v-col cols="12" md="4">
<v-col cols="12">
<v-text-field
label="Short IDs"
hide-details
append-icon="mdi-refresh"
@click:append="randomSID"
v-model="short_id">
</v-text-field>
</v-col>
Expand Down Expand Up @@ -261,6 +291,10 @@ import { iTls, defaultInTls } from '@/types/inTls'
import { oTls, defaultOutTls } from '@/types/outTls'
import AcmeVue from '@/components/Acme.vue'
import EchVue from '@/components/Ech.vue'
import HttpUtils from '@/plugins/httputil'
import { push } from 'notivue'
import { i18n } from '@/locales'
import RandomUtil from '@/plugins/randomUtil'
export default {
props: ['visible', 'data', 'index'],
emits: ['close', 'save'],
Expand Down Expand Up @@ -334,7 +368,10 @@ export default {
},
changeTlsType(){
if (this.tlsType) {
this.tls.server = <iTls>{ enabled: true, reality: { enabled: true, handshake: { server_port: 443 } }, server_name: "" }
this.tls.server = <iTls>{
enabled: true,
reality: { enabled: true, handshake: { server_port: 443 }, short_id: RandomUtil.randomShortId() },
server_name: "" }
this.tls.client = <oTls>{ reality: { public_key: "" } }
} else {
this.tls.server = <iTls>{ enabled: true }
Expand All @@ -350,6 +387,71 @@ export default {
this.$emit('save', this.tls)
this.loading = false
},
async genSelfSigned(){
this.loading = true
const msg = await HttpUtils.get('api/keypairs', { k: "tls", o: this.inTls.server_name?? "''" })
this.loading = false
if (msg.success) {
this.inTls.key_path=undefined
this.inTls.certificate_path=undefined
this.usePath = 1
if (msg.obj.length>0){
let privateKey = <string[]>[]
let publicKey = <string[]>[]
let isPrivateKey = false
let isPublicKey = false
msg.obj.forEach((line:string) => {
if (line === "-----BEGIN PRIVATE KEY-----") {
isPrivateKey = true
isPublicKey = false
} else if (line === "-----END PRIVATE KEY-----") {
isPrivateKey = false
} else if (line === "-----BEGIN CERTIFICATE-----") {
isPublicKey = true
isPrivateKey = false
} else if (line === "-----END CERTIFICATE-----") {
isPublicKey = false
} else if (isPrivateKey) {
privateKey.push(line)
} else if (isPublicKey) {
publicKey.push(line)
}
})
this.inTls.key = privateKey?? undefined
this.inTls.certificate = publicKey?? undefined
} else {
push.error({
message: i18n.global.t('error') + ": " + msg.obj
})
}
}
},
async genRealityKey(){
this.loading = true
const msg = await HttpUtils.get('api/keypairs', { k: "reality" })
this.loading = false
if (msg.success) {
msg.obj.forEach((line:string) => {
if (this.inTls.reality && this.outTls.reality){
if (line.startsWith("PrivateKey")){
this.inTls.reality.private_key = line.substring(12)
}
if (line.startsWith("PublicKey")){
this.outTls.reality.public_key = line.substring(11)
}
}
})
} else {
push.error({
message: i18n.global.t('error') + ": " + msg.obj
})
}
},
randomSID(){
this.short_id = RandomUtil.randomShortId().join(',')
}
},
computed: {
inTls(): iTls {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,14 @@ export default {
actions: {
action: "Action",
add: "Add",
new: "Add",
new: "New",
edit: "Edit",
del: "Delete",
save: "Save",
update: "Update",
submit: "Submit",
set: "Set",
generate: "Generate",
disable: "Disable",
close: "Close",
restartApp: "Restart App",
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/locales/fa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ export default {
actions: {
action: "فرمان",
add: "ایجاد",
new: "ایجاد",
new: "جدید",
edit: "ویرایش",
del: "حذف",
save: "ذخیره",
update: "بروزرسانی",
submit: "ارسال",
set: "تنظیم",
generate: "تولید",
disable: "غیرفعال",
close: "بستن",
restartApp: "ریستارت پنل",
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/locales/vi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,14 @@ export default {
actions: {
action: "Hành động",
add: "Thêm",
new: "Thêm",
new: "Mới",
edit: "Chỉnh sửa",
del: "Xóa",
save: "Lưu",
update: "Cập nhật",
submit: "Gửi",
set: "Đặt",
generate: "Tạo ra",
disable: "Vô hiệu hóa",
close: "Đóng",
restartApp: "Khởi động lại ứng dụng",
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/locales/zhcn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,14 @@ export default {
actions: {
action: "操作",
add: "添加",
new: "添加",
new: "新建",
edit: "编辑",
del: "删除",
save: "保存",
update: "更新",
submit: "提交",
set: "设置",
generate: "生成",
disable: "禁用",
close: "关闭",
restartApp: "重启面板",
Expand Down
Loading

0 comments on commit b144aec

Please sign in to comment.