Skip to content

Commit

Permalink
feat: support modifying ports and address
Browse files Browse the repository at this point in the history
  • Loading branch information
mzz2017 committed Nov 26, 2019
1 parent 12375f9 commit 2d3d070
Show file tree
Hide file tree
Showing 17 changed files with 356 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ V2RayA 是 V2Ray 的一个 Web 客户端。
- [x] 以service方式启动
- [x] 检查版本更新
- [x] 登陆与安全
- [x] 服务端端口号配置、前端可指定服务端地址

待开发:

- [ ] 测试节点 HTTP 时延
- [ ] 自定义 PAC 路由规则
- [ ] QUIC、SSR 协议支持
- [ ] 服务端端口号配置、前端可指定服务端地址

## 界面截图

Expand Down
46 changes: 41 additions & 5 deletions gui/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@
</template>
</b-navbar>
<node v-model="runningState" />
<b-modal
:active.sync="showCustomPorts"
has-modal-card
trap-focus
aria-role="dialog"
aria-modal
style="z-index: 1000"
class="modal-custom-address"
>
<ModalCustomAddress @close="showCustomPorts = false"></ModalCustomAddress>
</b-modal>
<div id="login"></div>
</div>
</template>
Expand All @@ -76,9 +87,10 @@ import CONST from "@/assets/js/const";
import ModalSetting from "@/components/modalSetting";
import node from "@/components/node";
import { Base64 } from "js-base64";
import ModalCustomAddress from "./components/modalCustomPorts";
export default {
components: { node },
components: { ModalCustomAddress, node },
data() {
return {
statusMap: {
Expand All @@ -91,7 +103,8 @@ export default {
running: CONST.INSPECTING_RUNNING,
connectedServer: null,
lastConnectedServer: null
}
},
showCustomPorts: false
};
},
computed: {
Expand All @@ -105,6 +118,9 @@ export default {
}
},
created() {
if (!localStorage.getItem("backendAddress")) {
localStorage.setItem("backendAddress", "http://localhost:2017");
}
this.$axios({
url: apiRoot + "/version"
})
Expand Down Expand Up @@ -146,11 +162,25 @@ export default {
.catch(err => {
if (err.message === "Network Error" || err.response === undefined) {
this.$buefy.snackbar.open({
message: "未检测到本地V2RayA服务端,请确保服务端正确监听2017端口",
message: "您是否需要调整服务端地址?",
type: "is-primary",
queue: false,
indefinite: true,
position: "is-top",
actionText: "",
onAction: () => {
this.showCustomPorts = true;
}
});
this.$buefy.snackbar.open({
message: `未在 ${
localStorage["backendAddress"]
} 检测到V2RayA服务端,请确定V2RayA已正确安装且配置正确`,
type: "is-warning",
queue: false,
position: "is-top",
indefinite: true,
actionText: "查看帮助",
duration: 60000,
onAction: () => {
window.open(
"https://github.com/mzz2017/V2RayA#%E4%BD%BF%E7%94%A8",
Expand All @@ -173,11 +203,17 @@ export default {
this.coverStatusText = "";
},
handleClickSetting() {
const that = this;
this.$buefy.modal.open({
parent: this,
component: ModalSetting,
hasModalCard: true,
canCancel: true
canCancel: true,
events: {
clickPorts() {
that.showCustomPorts = true;
}
}
});
},
handleClickAbout() {
Expand Down
146 changes: 146 additions & 0 deletions gui/src/components/modalCustomPorts.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<template>
<div class="modal-card" style="max-width: 450px;margin:auto">
<header class="modal-card-head">
<p class="modal-card-title">
地址与端口
</p>
</header>
<section class="modal-card-body">
<b-field label="服务端地址" label-position="on-border">
<b-input
v-model="table.backendAddress"
placeholder="http://localhost:2017"
required
pattern="https?://.+(:\d+)?"
>
></b-input
>
</b-field>
<template v-show="backendReady && dockerMode === false">
<b-field label="socks5端口" label-position="on-border">
<b-input
v-model="table.socks5"
placeholder="20170"
type="number"
min="1"
required
></b-input>
</b-field>
<b-field label="http端口" label-position="on-border">
<b-input
v-model="table.http"
placeholder="20171"
type="number"
min="1"
required
></b-input>
</b-field>
<b-field label="http端口(with PAC)" label-position="on-border">
<b-input
v-model="table.httpWithPac"
placeholder="20172"
type="number"
min="1"
required
></b-input>
</b-field>
<b-message
v-show="dockerMode"
type="is-warning"
style="font-size:13px"
class="after-line-dot5"
>
<p v-show="!dockerMode">
如需修改后端运行地址(默认0.0.0.0:2017),可在systemd中添加环境变量<code>V2RAYA_ADDRESS</code>或添加启动参数<code>--address</code>。
</p>
<p v-show="dockerMode">
docker模式下如果未使用<code>--privileged --network host</code
>参数启动容器,可通过修改端口映射修改socks5、http端口。
</p>
<p v-show="dockerMode">
docker模式下不能正确判断端口占用,请确保输入的端口未被其他程序占用。
</p>
</b-message>
</template>
</section>
<footer class="modal-card-foot flex-end">
<button class="button" @click="$emit('close')">
取消
</button>
<button class="button is-primary" @click="handleClickSubmit">
确定
</button>
</footer>
</div>
</template>

<script>
import { handleResponse } from "../assets/js/utils";
export default {
name: "ModalCustomPorts",
data: () => ({
table: {
backendAddress: "http://localhost:2017",
socks5: "20170",
http: "20171",
httpWithPac: "20172"
},
backendReady: false
}),
computed: {
dockerMode() {
return window.localStorage["docker"] === "true";
}
},
created() {
this.table.backendAddress = localStorage["backendAddress"];
this.$axios({
url: apiRoot + "/ports"
}).then(res => {
handleResponse(res, this, () => {
this.backendReady = true;
Object.assign(this.table, res.data.data);
});
});
},
methods: {
handleClickSubmit() {
localStorage["backendAddress"] = this.table.backendAddress;
if (this.backendReady) {
this.$axios({
url: apiRoot + "/ports",
method: "put",
data: {
socks5: parseInt(this.table.socks5),
http: parseInt(this.table.http),
httpWithPac: parseInt(this.table.httpWithPac)
}
})
.then(res => {
handleResponse(res, this, () => {
this.$emit("close");
});
})
.catch(() => {
//ERROR
});
} else {
this.$emit("close");
window.location.reload();
}
}
}
};
</script>

<style lang="scss">
.modal-custom-address .modal-background {
background-color: rgba(0, 0, 0, 0.6);
}
.after-line-dot5 {
p {
margin-bottom: 0.5em;
}
}
</style>
7 changes: 7 additions & 0 deletions gui/src/components/modalSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@
<!-- </b-field>-->
</section>
<footer class="modal-card-foot flex-end">
<button class="button footer-absolute-left" type="button" @click="$emit('clickPorts')">
端口与地址
</button>
<button class="button" type="button" @click="$parent.close()">
取消
</button>
Expand Down Expand Up @@ -408,4 +411,8 @@ export default {
.control:first-of-type:not(:last-of-type) .select select {
border-radius: 4px 0 0 4px !important;
}
.footer-absolute-left {
position: absolute;
left: 20px;
}
</style>
28 changes: 8 additions & 20 deletions gui/vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,14 @@ var webpack = require("webpack");

module.exports = {
configureWebpack: config => {
config.resolve.alias["vue$"]='vue/dist/vue.esm.js'
if (process.env.NODE_ENV === "production") {
// 为生产环境修改配置...
return {
plugins: [
new webpack.DefinePlugin({
apiRoot: "'http://localhost:2017/api'"
})
]
};
} else {
// 为开发环境修改配置...
return {
plugins: [
new webpack.DefinePlugin({
apiRoot: "'http://localhost:2017/api'"
})
]
};
}
config.resolve.alias["vue$"] = "vue/dist/vue.esm.js";
return {
plugins: [
new webpack.DefinePlugin({
apiRoot: '`${localStorage["backendAddress"]}/api`'
})
]
};
},

productionSourceMap: false,
Expand Down
28 changes: 28 additions & 0 deletions service/controller/ports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package controller

import (
"V2RayA/persistence/configure"
"V2RayA/service"
"V2RayA/tools"
"errors"
"github.com/gin-gonic/gin"
)

func PutPorts(ctx *gin.Context) {
var data configure.Ports
err := ctx.ShouldBindJSON(&data)
if err != nil {
tools.ResponseError(ctx, errors.New("参数有误"))
return
}
err = service.SetPorts(&data)
if err != nil {
tools.ResponseError(ctx, err)
return
}
tools.ResponseSuccess(ctx, nil)
}

func GetPorts(ctx *gin.Context) {
tools.ResponseSuccess(ctx, service.GetPorts())
}
13 changes: 6 additions & 7 deletions service/global/config.go → service/global/environmentConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import (
"sync"
)

type Param struct {
Address string `id:"address" short:"a" default:"0.0.0.0" desc:"监听地址"`
Port string `id:"port" short:"p" default:"2017" desc:"监听端口"`
type Params struct {
Address string `id:"address" short:"a" default:"0.0.0.0:2017" desc:"监听地址"`
Config string `id:"config" short:"c" default:"/etc/v2ray/v2raya.json" desc:"V2RayA配置文件路径"`
}

var param Param
var params Params

func initFunc() {
err := gonfig.Load(&param, gonfig.Conf{
err := gonfig.Load(&params, gonfig.Conf{
FileDisable: true,
FlagIgnoreUnknown: false,
EnvPrefix: "V2RAYA_",
Expand All @@ -27,8 +26,8 @@ func initFunc() {
}
}

func GetServiceConfig() *Param {
func GetEnvironmentConfig() *Params {
var once sync.Once
once.Do(initFunc)
return &param
return &params
}
Loading

0 comments on commit 2d3d070

Please sign in to comment.