Skip to content

Commit

Permalink
feat: 支持更新证书
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinTan committed Jun 21, 2024
1 parent 1977b39 commit 8899bb0
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 29 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea
.vscode

bin
ovpn.db
openvpn-web
50 changes: 30 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,33 +436,43 @@ func main() {
ovpn.POST("/server", func(c *gin.Context) {
a := c.PostForm("action")

if a == "settings" {
switch a {
case "settings":
k := c.PostForm("key")
v := c.PostForm("value")

if k == "auth-user" {
restartCmd := "supervisorctl stop openvpn && sleep 2 && supervisorctl start openvpn"
if v := c.PostForm("value"); v == "true" {
cmd := exec.Command("sh", "-c", fmt.Sprintf("sed -i 's/^#auth-user-pass-verify/auth-user-pass-verify/' $OVPN_DATA/server.conf && %s", restartCmd))

if out, err := cmd.CombinedOutput(); err != nil {
if out == nil {
out = []byte(err.Error())
}
logger.Error(context.Background(), string(out))
c.JSON(http.StatusInternalServerError, gin.H{"message": "启用用户认证失败"})
} else {
c.JSON(http.StatusOK, gin.H{"message": "启用用户认证成功"})
msg := "停用"
if v == "true" {
msg = "启用"
}
cmd := exec.Command("sh", "-c", fmt.Sprintf("/usr/bin/docker-entrypoint.sh auth %s", v))
if out, err := cmd.CombinedOutput(); err != nil {
if out == nil {
out = []byte(err.Error())
}
logger.Error(context.Background(), string(out))
c.JSON(http.StatusInternalServerError, gin.H{"message": fmt.Sprintf("%s用户认证失败", msg)})
} else {
cmd := exec.Command("sh", "-c", fmt.Sprintf("sed -i 's/^auth-user-pass-verify/#&/' $OVPN_DATA/server.conf && %s", restartCmd))
if out, err := cmd.CombinedOutput(); err != nil {
logger.Error(context.Background(), string(out))
c.JSON(http.StatusInternalServerError, gin.H{"message": "停用用户认证失败"})
} else {
c.JSON(http.StatusOK, gin.H{"message": "停用用户认证成功"})
}
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("%s用户认证成功", msg)})
}
}
case "renewcert":
cmd := exec.Command("sh", "-c", "/usr/bin/docker-entrypoint.sh renewcert")
if out, err := cmd.CombinedOutput(); err != nil {
if out == nil {
out = []byte(err.Error())
}
logger.Error(context.Background(), string(out))
c.JSON(http.StatusInternalServerError, gin.H{"message": "更新证书失败"})
return
}

c.JSON(http.StatusOK, gin.H{"message": "更新证书成功"})
default:
c.JSON(http.StatusUnprocessableEntity, gin.H{"message": "未知操作"})
}

})

ovpn.POST("/kill", func(c *gin.Context) {
Expand Down
79 changes: 70 additions & 9 deletions templates/index.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<link rel="stylesheet" type="text/css" href="static/css/buttons.bootstrap5.min.css" />

<style>
.dropdown-menu {
--bs-dropdown-min-width: 8rem;
}

.dataTables_info {
padding-top: 0 !important;
}
Expand Down Expand Up @@ -235,9 +239,6 @@
VPN账号
</span>
</li>
<li>
<hr class="dropdown-divider" />
</li>
<li>
<span class="dropdown-item" role="button" id="showClient">
<svg width="1em" height="1em" fill="currentColor" class="bi" viewBox="0 0 16 16">
Expand All @@ -250,6 +251,24 @@
<li>
<hr class="dropdown-divider" />
</li>
<li>
<h6 class="dropdown-header">系统</h6>
</li>
<li>
<span class="dropdown-item" role="button" id="renewcert">
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill="currentColor"
class="bi bi-opencollective" viewBox="0 0 16 16">
<path fill-opacity=".4"
d="M12.995 8.195c0 .937-.312 1.912-.78 2.693l1.99 1.99c.976-1.327 1.6-2.966 1.6-4.683 0-1.795-.624-3.434-1.561-4.76l-2.068 2.028c.468.781.78 1.679.78 2.732z" />
<path
d="M8 13.151a4.995 4.995 0 1 1 0-9.99c1.015 0 1.951.273 2.732.82l1.95-2.03a7.805 7.805 0 1 0 .04 12.449l-1.951-2.03a5.07 5.07 0 0 1-2.732.781z" />
</svg>
更新证书
</span>
</li>
<li>
<hr class="dropdown-divider" />
</li>
<li>
<a class="dropdown-item" role="button" id="logout" href="/logout">
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill="currentColor"
Expand Down Expand Up @@ -347,7 +366,7 @@
</div>

<div class="modal fade" tabindex="-1" id="addUserModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<form>
<div class="modal-header">
Expand Down Expand Up @@ -387,7 +406,7 @@
</div>

<div class="modal fade" tabindex="-1" id="addClientModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<form>
<div class="modal-header">
Expand Down Expand Up @@ -428,7 +447,7 @@
</div>

<div class="modal fade" tabindex="-1" id="resetPassModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<form>
<div class="modal-header">
Expand Down Expand Up @@ -469,6 +488,32 @@
</div>
</div>

<div class="modal fade" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" id="renewCertModal"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form>
<div class="modal-header">
<h5 class="modal-title">更新证书</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body" style="text-align: center;">
<h4>确认更新openvpn证书?</h4>
<span style="font-size: 14px;color: red;">注: 更新证书会重启服务</span>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
取消
</button>
<button type="submit" class="btn btn-primary" id="renewcertSumbit">
确认
</button>
</div>
</form>
</div>
</div>
</div>

<footer class="fixed-bottom border-top bg-light" style="font-size: 12px">
{{ .server.Version }}
</footer>
Expand Down Expand Up @@ -693,8 +738,11 @@
"<'d-flex justify-content-between align-items-center'lip>",
fnInitComplete: function (oSettings, data) {
$("#user div.toolbar").html(
`<div class="form-check form-switch form-check-reverse"><input class="form-check-input" type="checkbox" role="switch" id="authUser" ${data.authUser ? "checked" : ""
}><label class="form-check-label" for="authUser">账号启用: </label></div>`
`<div class="form-check form-switch form-check-reverse">
<input class="form-check-input" type="checkbox" role="switch" id="authUser" ${data.authUser ? "checked" : ""}>
<label class="form-check-label" for="authUser">账号启用: </label>
</div>
`
);
},
drawCallback: function () {
Expand All @@ -713,7 +761,7 @@
],
},
ajax: function (data, callback, settings) {
request.get("/ovpn/user").then((data) => callback({ data: data?.users }));
request.get("/ovpn/user").then((data) => callback({ data: data?.users, authUser: data?.authUser }));
},
columns: [
{ data: "id" },
Expand Down Expand Up @@ -823,6 +871,19 @@
}
});

$("#renewcert").click(function () {
$("#renewCertModal").modal("show");
});

$("#renewCertModal form").submit(function () {
$("#renewCertModal").modal("hide");
request.post("/ovpn/server", { action: "renewcert" }).then((data) => {
message.success(data.message);
});
return false;
});


$("#addUserModal form").submit(function () {
const name = $('#addUserModal input[name="name"]').val();
const username = $('#addUserModal input[name="username"]').val();
Expand Down

0 comments on commit 8899bb0

Please sign in to comment.