From b524b53454d24c144bdb05e99d6ca70da8eeb0dd Mon Sep 17 00:00:00 2001 From: ehco1996 Date: Sat, 5 Feb 2022 13:43:43 +0800 Subject: [PATCH] feat: validate config close #81 --- README.md | 7 +++---- config.json | 4 ++-- internal/config/config.go | 38 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d37dcef45..383b9bacf 100644 --- a/README.md +++ b/README.md @@ -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) ## 安装 @@ -45,6 +43,7 @@ go get -u "github.com/Ehco1996/ehco/cmd/ehco" * benchmark * grafana 监控报警 * 热重载配置 +* 内嵌了完整版本的 [xray](https://github.com/XTLS/Xray-core) 后端 ## 使用说明 @@ -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 { diff --git a/config.json b/config.json index c19d79165..93b61045a 100644 --- a/config.json +++ b/config.json @@ -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"] }, { diff --git a/internal/config/config.go b/internal/config/config.go index 442268617..c11af2c7e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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" ) @@ -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 @@ -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 { @@ -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 +}