Skip to content

Commit

Permalink
sub: Add support for assigning free ports to proxies in batch
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Feb 1, 2024
1 parent c918bae commit d99075e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
39 changes: 36 additions & 3 deletions pkg/sub/clash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"sort"
"strconv"
"strings"

relay_cfg "github.com/Ehco1996/ehco/internal/relay/conf"
Expand Down Expand Up @@ -98,10 +99,43 @@ func (c *ClashSub) Refresh() error {

// update current
c.cCfg.Proxies = &tmp

// init group leader for each group
groupProxy := c.cCfg.groupByLongestCommonPrefix()
for _, proxies := range groupProxy {
// only use first proxy will be show in proxy provider, other will be merged into load balance in relay
proxies[0].getOrCreateGroupLeader()
}
return nil
}

func (c *ClashSub) ToRelayConfigs(listenHost string) ([]*relay_cfg.Config, error) {

// assign free port to proxies in batch
needAssign := 0
for _, proxy := range *c.cCfg.Proxies {
if proxy.freePort == "" {
needAssign++
}
if proxy.groupLeader != nil && proxy.groupLeader.freePort == "" {
needAssign++
}
}
freePortList, err := getFreePortInBatch(listenHost, needAssign)
if err != nil {
return nil, err
}
for i, p := range *c.cCfg.Proxies {
if p.freePort == "" {
(*c.cCfg.Proxies)[i].freePort = strconv.Itoa(freePortList[0])
freePortList = freePortList[1:]
}
if p.groupLeader != nil && p.groupLeader.freePort == "" {
(*c.cCfg.Proxies)[i].groupLeader.freePort = strconv.Itoa(freePortList[0])
freePortList = freePortList[1:]
}
}

relayConfigs := []*relay_cfg.Config{}
// generate relay config for each proxy
for _, proxy := range *c.cCfg.Proxies {
Expand All @@ -111,8 +145,7 @@ func (c *ClashSub) ToRelayConfigs(listenHost string) ([]*relay_cfg.Config, error
} else {
newName = fmt.Sprintf("%s-%s", proxy.Name, c.Name)
}

rc, err := proxy.ToRelayConfig(listenHost, newName)
rc, err := proxy.ToRelayConfig(listenHost, proxy.freePort, newName)
if err != nil {
return nil, err
}
Expand All @@ -130,7 +163,7 @@ func (c *ClashSub) ToRelayConfigs(listenHost string) ([]*relay_cfg.Config, error
} else {
newName = fmt.Sprintf("%s-lb", groupName)
}
rc, err := groupLeader.ToRelayConfig(listenHost, newName)
rc, err := groupLeader.ToRelayConfig(listenHost, groupLeader.freePort, newName)
if err != nil {
return nil, err
}
Expand Down
14 changes: 4 additions & 10 deletions pkg/sub/clash_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sub

import (
"net"
"strconv"

"github.com/Ehco1996/ehco/internal/constant"
relay_cfg "github.com/Ehco1996/ehco/internal/relay/conf"
Expand Down Expand Up @@ -89,6 +88,7 @@ type Proxies struct {
rawServer string
rawPort string
relayCfg *relay_cfg.Config
freePort string

groupLeader *Proxies
}
Expand Down Expand Up @@ -127,22 +127,16 @@ func (p *Proxies) Different(new *Proxies) bool {
return false
}

func (p *Proxies) ToRelayConfig(listenHost string, newName string) (*relay_cfg.Config, error) {
func (p *Proxies) ToRelayConfig(listenHost string, listenPort string, newName string) (*relay_cfg.Config, error) {
if p.relayCfg != nil {
return p.relayCfg, nil
}
freePorts, err := getFreePortInBatch(listenHost, 1)
if err != nil {
return nil, err
}
listenPort := freePorts[0]
listenAddr := net.JoinHostPort(listenHost, strconv.Itoa(listenPort))
remoteAddr := net.JoinHostPort(p.Server, p.Port)
r := &relay_cfg.Config{
Label: newName,
ListenType: constant.Listen_RAW,
TransportType: constant.Transport_RAW,
Listen: listenAddr,
Listen: net.JoinHostPort(listenHost, listenPort),
TCPRemotes: []string{remoteAddr},
}
if p.UDP {
Expand All @@ -154,7 +148,7 @@ func (p *Proxies) ToRelayConfig(listenHost string, newName string) (*relay_cfg.C
// overwrite name,port,and server by relay
p.Name = newName
p.Server = listenHost
p.Port = strconv.Itoa(listenPort)
p.Port = listenPort
p.relayCfg = r
return r, nil
}
Expand Down

0 comments on commit d99075e

Please sign in to comment.