diff --git a/README.md b/README.md index ea4c264..eab9473 100644 --- a/README.md +++ b/README.md @@ -69,3 +69,13 @@ docker run --name yp-proxy --net=host -v :/etc/yao-proxy/confi 增加docker部署方式 👌🏻 利用context来管理上下文,控制优雅的退出 + + + +## 更新说明 + +### v2.2.1 + +- 本地代理更新,可以支持代理多个端口 + +- 配置文件和之前版本不兼容 diff --git a/cmd/local/res/config.json b/cmd/local/res/config.json index 85c6b1f..81f3530 100644 --- a/cmd/local/res/config.json +++ b/cmd/local/res/config.json @@ -1,26 +1,55 @@ { - "port": 20808, "debug": true, - "remote_infos": [ + "server_infos": [ { - "remote_addr": "127.0.0.1:20804", - "method": "aes-128-cfb", - "key": "atuatuatu" + "port": 20808, + "remote_infos": [ + { + "remote_addr": "127.0.0.1:20804", + "method": "aes-128-cfb", + "key": "atuatuatu" + }, + { + "remote_addr": "127.0.0.1:20805", + "method": "aes-128-cfb", + "key": "atuatuatu" + }, + { + "remote_addr": "127.0.0.1:20806", + "method": "aes-128-cfb", + "key": "atuatuatu" + }, + { + "remote_addr": "127.0.0.1:20807", + "method": "aes-128-cfb", + "key": "kainhuck" + } + ] }, { - "remote_addr": "127.0.0.1:20805", - "method": "aes-128-cfb", - "key": "atuatuatu" - }, - { - "remote_addr": "127.0.0.1:20806", - "method": "aes-128-cfb", - "key": "atuatuatu" - }, - { - "remote_addr": "127.0.0.1:20807", - "method": "aes-128-cfb", - "key": "kainhuck" + "port": 20809, + "remote_infos": [ + { + "remote_addr": "127.0.0.1:20804", + "method": "aes-128-cfb", + "key": "atuatuatu" + }, + { + "remote_addr": "127.0.0.1:20805", + "method": "aes-128-cfb", + "key": "atuatuatu" + }, + { + "remote_addr": "127.0.0.1:20806", + "method": "aes-128-cfb", + "key": "atuatuatu" + }, + { + "remote_addr": "127.0.0.1:20807", + "method": "aes-128-cfb", + "key": "kainhuck" + } + ] } ] } \ No newline at end of file diff --git a/internal/service/local/config.go b/internal/service/local/config.go index 2dbfc6a..671dd3b 100644 --- a/internal/service/local/config.go +++ b/internal/service/local/config.go @@ -13,12 +13,16 @@ type RemoteInfo struct { Key string `json:"key"` } -type Config struct { +type ServerInfo struct { Port int `json:"port"` - Debug bool `json:"debug"` RemoteInfos []RemoteInfo `json:"remote_infos"` } +type Config struct { + Debug bool `json:"debug"` + ServerInfos []ServerInfo `json:"server_infos"` +} + func ReadConfig(path string) (*Config, error) { f, err := os.Open(path) if err != nil { @@ -37,8 +41,8 @@ func ReadConfig(path string) (*Config, error) { return nil, err } - if len(cfg.RemoteInfos) == 0 { - return nil, fmt.Errorf("need remote_infos") + if len(cfg.ServerInfos) == 0 { + return nil, fmt.Errorf("need server_infos") } return &cfg, nil diff --git a/internal/service/local/main.go b/internal/service/local/main.go index b364f7a..dd2ba1d 100644 --- a/internal/service/local/main.go +++ b/internal/service/local/main.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/kainhuck/yao-proxy/internal/log" "os" + "sync" ) func Main() { @@ -19,8 +20,20 @@ func Main() { } logger := log.NewLogger(cfg.Debug) - localAddr := fmt.Sprintf(":%d", cfg.Port) + var wg sync.WaitGroup - server := NewServer(localAddr, logger, cfg.RemoteInfos) - server.Run() + for _, info := range cfg.ServerInfos { + localAddr := fmt.Sprintf(":%d", info.Port) + + server := NewServer(localAddr, logger, info.RemoteInfos) + + wg.Add(1) + + go func() { + defer wg.Done() + server.Run() + }() + } + + wg.Wait() } diff --git a/internal/service/local/server.go b/internal/service/local/server.go index 74682f6..69cc79a 100644 --- a/internal/service/local/server.go +++ b/internal/service/local/server.go @@ -165,8 +165,8 @@ func (s *Server) handleConn(conn net.Conn) { // 4. 和远程建立链接并将目标地址发送给远程 remoteConn := new(YPConn.Conn) select { - case remoteConn = <- s.remoteChan: - case <- time.After(5 * time.Second): + case remoteConn = <-s.remoteChan: + case <-time.After(5 * time.Second): s.logger.Errorf("dial remote time out") return } @@ -308,7 +308,7 @@ func (s *Server) getCipherRemote() *CipherRemote { return s.cipherRemotes } -func (s *Server) getRemoteConn() *YPConn.Conn{ +func (s *Server) getRemoteConn() *YPConn.Conn { cr := s.getCipherRemote() conn, err := YPConn.Dial(cr.RemoteAddr, cr.cipher.Copy()) if err != nil {