From 02986a4bfdc8f335732cc0a1d473e062288bbd7e Mon Sep 17 00:00:00 2001 From: Jacky Date: Sun, 28 Mar 2021 22:58:37 +0800 Subject: [PATCH] add acme.sh and login page --- .gitignore | 5 +- nginx-ui-frontend/.env.development | 3 +- nginx-ui-frontend/.env.production | 2 - nginx-ui-frontend/package.json | 2 +- nginx-ui-frontend/public/index.html | 2 +- nginx-ui-frontend/src/api/auth.js | 20 + nginx-ui-frontend/src/api/index.js | 8 +- .../components/VueItextarea/VueItextarea.vue | 83 +++ nginx-ui-frontend/src/lib/http/index.js | 4 + nginx-ui-frontend/src/lib/store/index.js | 7 +- nginx-ui-frontend/src/lib/store/mock.js | 38 -- nginx-ui-frontend/src/lib/store/user.js | 23 - nginx-ui-frontend/src/lib/utils/index.js | 25 +- .../src/lib/utils/scroll-position.js | 2 +- nginx-ui-frontend/src/router/index.js | 14 +- nginx-ui-frontend/src/views/About.vue | 1 + nginx-ui-frontend/src/views/ConfigEdit.vue | 13 +- nginx-ui-frontend/src/views/DashBoard.vue | 6 +- nginx-ui-frontend/src/views/DomainEdit.vue | 99 +++- nginx-ui-frontend/src/views/Login.vue | 106 ++++ nginx-ui-frontend/version.json | 2 +- nginx-ui-frontend/yarn.lock | 493 +----------------- server/api/analytic.go | 9 - server/api/auth.go | 62 +++ server/api/cert.go | 143 +++++ server/app.ini | 3 +- server/go.mod | 6 +- server/go.sum | 32 +- server/model/auth.go | 63 +++ server/model/models.go | 2 + server/router/routers.go | 41 +- server/settings/settings.go | 31 +- server/test/acme_test.go | 55 ++ server/test/cert_test.go | 40 ++ 34 files changed, 792 insertions(+), 653 deletions(-) create mode 100644 nginx-ui-frontend/src/api/auth.js create mode 100644 nginx-ui-frontend/src/components/VueItextarea/VueItextarea.vue delete mode 100644 nginx-ui-frontend/src/lib/store/mock.js create mode 100644 nginx-ui-frontend/src/views/Login.vue create mode 100644 server/api/auth.go create mode 100644 server/api/cert.go create mode 100644 server/model/auth.go create mode 100644 server/test/acme_test.go create mode 100644 server/test/cert_test.go diff --git a/.gitignore b/.gitignore index 385fd9fe..6234c94f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,6 @@ .DS_Store .idea database.db - -server/tmp/main +tmp node_modules dist -nginx-ui-frontend/.env.development -nginx-ui-frontend/.env.production diff --git a/nginx-ui-frontend/.env.development b/nginx-ui-frontend/.env.development index ee020122..2399d472 100644 --- a/nginx-ui-frontend/.env.development +++ b/nginx-ui-frontend/.env.development @@ -1,3 +1,2 @@ VUE_APP_API_ROOT = / -VUE_APP_API_WSS_ROOT = -VUE_APP_API_WSS_TOKEN = +VUE_APP_API_WSS_ROOT = wss://nginx.jackyu.cn/ws diff --git a/nginx-ui-frontend/.env.production b/nginx-ui-frontend/.env.production index 6f7198fb..e5279c3b 100644 --- a/nginx-ui-frontend/.env.production +++ b/nginx-ui-frontend/.env.production @@ -1,3 +1 @@ VUE_APP_API_ROOT = /api -VUE_APP_API_WSS_ROOT = -VUE_APP_API_WSS_TOKEN = diff --git a/nginx-ui-frontend/package.json b/nginx-ui-frontend/package.json index 3b668c19..46f311e3 100644 --- a/nginx-ui-frontend/package.json +++ b/nginx-ui-frontend/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "serve": "vue-cli-service serve", - "build": "vue-cli-service build", + "build": "vue-cli-service build --dest dist --modern", "lint": "vue-cli-service lint" }, "dependencies": { diff --git a/nginx-ui-frontend/public/index.html b/nginx-ui-frontend/public/index.html index 6ba8b205..e137ff0b 100644 --- a/nginx-ui-frontend/public/index.html +++ b/nginx-ui-frontend/public/index.html @@ -3,7 +3,7 @@ - + <%= htmlWebpackPlugin.options.title %> diff --git a/nginx-ui-frontend/src/api/auth.js b/nginx-ui-frontend/src/api/auth.js new file mode 100644 index 00000000..c5e787e1 --- /dev/null +++ b/nginx-ui-frontend/src/api/auth.js @@ -0,0 +1,20 @@ +import http from '@/lib/http' +import store from '@/lib/store' + +const auth = { + async login(name, password) { + return http.post('/login', { + name: name, + password: password + }).then(r => { + store.dispatch('login', r) + }) + }, + logout() { + return http.delete('/logout').then(() => { + store.dispatch('logout').finally() + }) + } +} + +export default auth diff --git a/nginx-ui-frontend/src/api/index.js b/nginx-ui-frontend/src/api/index.js index 500306dc..71832cfd 100644 --- a/nginx-ui-frontend/src/api/index.js +++ b/nginx-ui-frontend/src/api/index.js @@ -1,7 +1,9 @@ -import domain from "./domain" -import config from "./config" +import domain from './domain' +import config from './config' +import auth from './auth' export default { domain, - config + config, + auth } diff --git a/nginx-ui-frontend/src/components/VueItextarea/VueItextarea.vue b/nginx-ui-frontend/src/components/VueItextarea/VueItextarea.vue new file mode 100644 index 00000000..1ed393f3 --- /dev/null +++ b/nginx-ui-frontend/src/components/VueItextarea/VueItextarea.vue @@ -0,0 +1,83 @@ + + + + diff --git a/nginx-ui-frontend/src/lib/http/index.js b/nginx-ui-frontend/src/lib/http/index.js index 9d5ecb43..dc6d430e 100644 --- a/nginx-ui-frontend/src/lib/http/index.js +++ b/nginx-ui-frontend/src/lib/http/index.js @@ -1,4 +1,5 @@ import axios from 'axios' +import store from '../store' /* 创建 axios 实例 */ let http = axios.create({ @@ -18,6 +19,9 @@ let http = axios.create({ /* http request 拦截器 */ http.interceptors.request.use( config => { + if (store.state.user.token) { + config.headers.Authorization = `${store.state.user.token}` + } return config }, err => { diff --git a/nginx-ui-frontend/src/lib/store/index.js b/nginx-ui-frontend/src/lib/store/index.js index 1a797607..4a5608c1 100644 --- a/nginx-ui-frontend/src/lib/store/index.js +++ b/nginx-ui-frontend/src/lib/store/index.js @@ -1,6 +1,7 @@ import Vue from 'vue' import Vuex from 'vuex' import VuexPersistence from 'vuex-persist' +import {user} from './user' Vue.use(Vuex) @@ -8,12 +9,14 @@ const debug = process.env.NODE_ENV !== 'production' const vuexLocal = new VuexPersistence({ storage: window.localStorage, - modules: [] + modules: ['user'] }) export default new Vuex.Store({ // 将各组件分别模块化存入 Store - modules: {}, + modules: { + user + }, plugins: [vuexLocal.plugin], strict: debug }) diff --git a/nginx-ui-frontend/src/lib/store/mock.js b/nginx-ui-frontend/src/lib/store/mock.js deleted file mode 100644 index 66f08528..00000000 --- a/nginx-ui-frontend/src/lib/store/mock.js +++ /dev/null @@ -1,38 +0,0 @@ -export const mock = { - namespace: true, - state: { - user: { - name: 'mock 用户', - school_id: '201904020209', - superuser: true, - // 0学生 1企业 2教师 3学院 - power: 2, - gender: 1, - phone: "10086", - email: 'me@jackyu.cn', - description: '前端、后端、系统架构', - college_id: 1, - college_name: "大数据与互联网学院", - major: 1, - major_name: "物联网工程", - position: 'HR' - } - }, - mutations: { - update_mock_user(state, payload) { - for (const k in payload) { - state.user[k] = payload[k] - } - } - }, - actions: { - async update_mock_user({commit}, data) { - commit('update_mock_user', data) - } - }, - getters: { - user(state) { - return state.user - }, - } -} diff --git a/nginx-ui-frontend/src/lib/store/user.js b/nginx-ui-frontend/src/lib/store/user.js index 842cf45a..e33a334d 100644 --- a/nginx-ui-frontend/src/lib/store/user.js +++ b/nginx-ui-frontend/src/lib/store/user.js @@ -1,16 +1,6 @@ export const user = { namespace: true, state: { - info: { - id: null, - name: null, - power: null, - college_id: null, - college_name: null, - major_id: null, - major_name: null, - position: null - }, token: null }, mutations: { @@ -19,11 +9,7 @@ export const user = { }, logout(state) { sessionStorage.clear() - state.info = {} state.token = null - }, - update_user(state, payload) { - state.info = payload } }, actions: { @@ -32,20 +18,11 @@ export const user = { }, async logout({commit}) { commit('logout') - }, - async update_user({commit}, data) { - commit('update_user', data) } }, getters: { - info(state) { - return state.info - }, token(state) { return state.token - }, - isLogin(state) { - return !!state.token } } } diff --git a/nginx-ui-frontend/src/lib/utils/index.js b/nginx-ui-frontend/src/lib/utils/index.js index 9273eebf..794f6456 100644 --- a/nginx-ui-frontend/src/lib/utils/index.js +++ b/nginx-ui-frontend/src/lib/utils/index.js @@ -29,22 +29,15 @@ export default { return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i] } - Vue.prototype.transformUserType = (power) => { - const type = ['学生', '企业', '教师', '学院'] - return type[power] - } - - Vue.prototype.transformGrade = { - 7: 'A+', - 6: 'A', - 5: 'B+', - 4: 'B', - 3: 'C+', - 2: 'C', - 1: 'D', - 0: 'F' - } - Vue.prototype.scrollPosition = scrollPosition + + Vue.prototype.getWebSocketRoot = () => { + const protocol = location.protocol === "https:" ? "wss://" : "ws://" + if (process.env["VUE_APP_API_WSS_ROOT"]) { + return process.env["VUE_APP_API_WSS_ROOT"] + } + console.log(protocol, document.domain) + return protocol + document.domain + '/ws' + } } } diff --git a/nginx-ui-frontend/src/lib/utils/scroll-position.js b/nginx-ui-frontend/src/lib/utils/scroll-position.js index 6fa421cf..ad7bbd65 100644 --- a/nginx-ui-frontend/src/lib/utils/scroll-position.js +++ b/nginx-ui-frontend/src/lib/utils/scroll-position.js @@ -21,7 +21,7 @@ const scrollPosition = { Vue.prototype.$nextTick(() => { document.documentElement.scrollTop = document.body.scrollTop = 0 }) - } + }, } export default scrollPosition diff --git a/nginx-ui-frontend/src/router/index.js b/nginx-ui-frontend/src/router/index.js index 53dddb5c..93fb0137 100644 --- a/nginx-ui-frontend/src/router/index.js +++ b/nginx-ui-frontend/src/router/index.js @@ -1,6 +1,7 @@ import Vue from 'vue' import VueRouter from 'vue-router' import axios from 'axios' +import store from '@/lib/store' Vue.use(VueRouter) @@ -71,6 +72,12 @@ export const routes = [ }, ] }, + { + path: '/login', + name: '登录', + component: () => import('@/views/Login'), + meta: {noAuth: true} + }, { path: '/404', name: '404 Not Found', @@ -108,7 +115,12 @@ router.beforeEach((to, from, next) => { }) } - next() + if (to.meta.noAuth || store.getters.token) { + next() + } else { + next({path: '/login', query: {next: to.fullPath}}) + } + }) export {router} diff --git a/nginx-ui-frontend/src/views/About.vue b/nginx-ui-frontend/src/views/About.vue index c65862b0..0b211b0b 100644 --- a/nginx-ui-frontend/src/views/About.vue +++ b/nginx-ui-frontend/src/views/About.vue @@ -9,6 +9,7 @@

Go

Gin

Vue

+

Websocket

开源协议

GNU General Public License v2.0

Copyright © 2020 - {{ this_year }} 0xJacky

diff --git a/nginx-ui-frontend/src/views/ConfigEdit.vue b/nginx-ui-frontend/src/views/ConfigEdit.vue index 35cd34f1..8cf90e80 100644 --- a/nginx-ui-frontend/src/views/ConfigEdit.vue +++ b/nginx-ui-frontend/src/views/ConfigEdit.vue @@ -1,6 +1,6 @@