Skip to content

Commit

Permalink
feat: validate config close #81
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Feb 5, 2022
1 parent e0a5cbf commit b524b53
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ ehco is a network relay tool and a typo :)

* 面板视频安装教程: [地址](https://youtu.be/BRHcdGeufvY)

* 后端对接视频教程: [地址](https://youtu.be/QNbnya1HHU0)

* 隧道对接视频教程: [地址](https://youtu.be/R4U0NZaMUeY)
* 隧道后端对接视频教程: [地址](https://youtu.be/R4U0NZaMUeY)

## 安装

Expand Down Expand Up @@ -45,6 +43,7 @@ go get -u "github.com/Ehco1996/ehco/cmd/ehco"
* benchmark
* grafana 监控报警
* 热重载配置
* 内嵌了完整版本的 [xray](https://github.com/XTLS/Xray-core) 后端

## 使用说明

Expand Down Expand Up @@ -77,7 +76,7 @@ go get -u "github.com/Ehco1996/ehco/cmd/ehco"

> ehco支持从 `配置文件` / `http接口` 里读取 `json` 格式的配置并启动
配置文件格式要求如下(更多例子可以参考项目里的 `config.json` 文件):
配置文件格式要求如下(更多例子可以参考项目里的 [config.json](https://github.com/Ehco1996/ehco/blob/master/config.json) 文件):

```json
{
Expand Down
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
{
"listen": "127.0.0.1:1235",
"listen_type": "raw",
"transport_type": "ws",
"tcp_remotes": ["ws://0.0.0.0:2443"],
"transport_type": "mws",
"tcp_remotes": ["wxs://0.0.0.0:2443"],
"udp_remotes": ["0.0.0.0:5201"]
},
{
Expand Down
38 changes: 36 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package config

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/Ehco1996/ehco/internal/constant"
"github.com/Ehco1996/ehco/internal/logger"
"github.com/xtls/xray-core/infra/conf"
)
Expand All @@ -20,6 +22,23 @@ type RelayConfig struct {
Label string `json:"label"`
}

func (r *RelayConfig) Validate() error {
if r.ListenType != constant.Listen_RAW &&
r.ListenType != constant.Listen_WS &&
r.ListenType != constant.Listen_WSS &&
r.ListenType != constant.Listen_MWSS {
return fmt.Errorf("invalid listen type:%s", r.ListenType)
}

if r.TransportType != constant.Transport_RAW &&
r.TransportType != constant.Transport_WS &&
r.TransportType != constant.Transport_WSS &&
r.TransportType != constant.Transport_MWSS {
return fmt.Errorf("invalid transport type:%s", r.ListenType)
}
return nil
}

type Config struct {
PATH string

Expand All @@ -42,9 +61,14 @@ func (c *Config) NeedSyncUserFromServer() bool {

func (c *Config) LoadConfig() error {
if c.NeedSyncUserFromServer() {
return c.readFromHttp()
if err := c.readFromHttp(); err != nil {
return err
}
}
return c.readFromFile()
if err := c.readFromFile(); err != nil {
return err
}
return c.Validate()
}

func (c *Config) readFromFile() error {
Expand All @@ -69,3 +93,13 @@ func (c *Config) readFromHttp() error {
logger.Info("[cfg] Load Config From http:", c.PATH)
return json.NewDecoder(r.Body).Decode(&c)
}

func (c *Config) Validate() error {
// validate relay configs
for _, r := range c.RelayConfigs {
if err := r.Validate(); err != nil {
return err
}
}
return nil
}

0 comments on commit b524b53

Please sign in to comment.