Skip to content

Commit

Permalink
Merge pull request #17 from kainhuck/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
kainhuck authored Sep 6, 2021
2 parents 9a5fd7a + 7929ab8 commit 259534c
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 33 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,14 @@ docker run --name yp-proxy --net=host -v <your config path>:/etc/yao-proxy/confi

增加docker部署方式 👌🏻

利用context来管理上下文,控制优雅的退出



## 更新说明

### v2.2.1

- 本地代理更新,可以支持代理多个端口

- 配置文件和之前版本不兼容
4 changes: 1 addition & 3 deletions cmd/local/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ARG BUILDER_BASE=golang:1.16-alpine
FROM ${BUILDER_BASE} AS builder

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN --mount=type=cache,target=/var/cache/apk apk add --update --no-cache make
RUN apk add --update --no-cache make

WORKDIR /yao-proxy

Expand All @@ -12,8 +12,6 @@ RUN make build-local

FROM alpine:3.12

EXPOSE 20808

WORKDIR /

COPY --from=builder /yao-proxy/cmd/local/yp-local /bin/
Expand Down
65 changes: 47 additions & 18 deletions cmd/local/res/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
]
}
2 changes: 1 addition & 1 deletion cmd/remote/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ARG BUILDER_BASE=golang:1.16-alpine
FROM ${BUILDER_BASE} AS builder

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN --mount=type=cache,target=/var/cache/apk apk add --update --no-cache make
RUN apk add --update --no-cache make

WORKDIR /yao-proxy

Expand Down
12 changes: 8 additions & 4 deletions internal/service/local/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
19 changes: 16 additions & 3 deletions internal/service/local/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/kainhuck/yao-proxy/internal/log"
"os"
"sync"
)

func Main() {
Expand All @@ -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()
}
8 changes: 4 additions & 4 deletions internal/service/local/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ func (s *Server) handleConn(conn net.Conn) {
// 4. 和远程建立链接并将目标地址发送给远程
remoteConn := new(YPConn.Conn)
select {
case remoteConn = <- s.remoteChan:
case <- time.After(5 * time.Second):
s.logger.Errorf("time out")
case remoteConn = <-s.remoteChan:
case <-time.After(5 * time.Second):
s.logger.Errorf("dial remote time out")
return
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 259534c

Please sign in to comment.