Skip to content

Commit

Permalink
refact: remove repeater
Browse files Browse the repository at this point in the history
Signed-off-by: black-desk <[email protected]>
  • Loading branch information
black-desk committed Jun 17, 2023
1 parent b995b62 commit c695bc1
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 378 deletions.
75 changes: 0 additions & 75 deletions internal/config/alloc.go

This file was deleted.

135 changes: 20 additions & 115 deletions internal/config/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package config

import (
"fmt"
"math/rand"
"regexp"
"strconv"
"strings"

"github.com/black-desk/cgtproxy/internal/consts"
. "github.com/black-desk/cgtproxy/internal/log"
Expand All @@ -24,129 +20,38 @@ func (c *ConfigV1) check() (err error) {
return
}

{
if c.CgroupRoot == "AUTO" {
var cgroupRoot CgroupRoot
cgroupRoot, err = getCgroupRoot()
if err != nil {
return
}

c.CgroupRoot = cgroupRoot

Log.Infow(
"Cgroup mount point auto detection done.",
"cgroup root", cgroupRoot,
)
}
}

{
if c.RouteTable == 0 {
c.RouteTable = rand.Int()
}
}

{
if c.Rules == nil {
Log.Warnw("No rules in config.")
}
}

{
if c.Proxies == nil {
c.Proxies = map[string]*Proxy{}
}

if c.TProxies == nil {
c.TProxies = map[string]*TProxy{}
}

for name := range c.TProxies {
tp := c.TProxies[name]
if tp.Name == "" {
tp.Name = name
}

if strings.HasSuffix(tp.Name, "-MARK") {
err = &ErrBadProxyName{
Actual: tp.Name,
}
Wrap(&err)
return
}
}
}

if c.Repeater != nil {
var (
begin uint64
end uint64
)

begin, end, err = parseRange(c.Repeater.TProxyPorts)
if c.CgroupRoot == "AUTO" {
var cgroupRoot CgroupRoot
cgroupRoot, err = getCgroupRoot()
if err != nil {
return
}

err = c.allocPorts(uint16(begin), uint16(end))
if err != nil {
return
}
}
c.CgroupRoot = cgroupRoot

{
var (
begin uint64
end uint64
Log.Infow(
"Cgroup mount point auto detection done.",
"cgroup root", cgroupRoot,
)

begin, end, err = parseRange(c.Marks)
if err != nil {
return
}

err = c.allocMarks(int(begin), int(end))
if err != nil {
return
}
}

return
}

func parseRange(str string) (begin uint64, end uint64, err error) {
defer Wrap(&err, "Failed to parse range.")

rangeExp := regexp.MustCompile(consts.PortsPattern)

matchs := rangeExp.FindStringSubmatch(str)

if len(matchs) != 3 {
err = &ErrBadRange{
Actual: str,
}
Wrap(&err)

return
if c.Rules == nil {
Log.Warnw("No rules in config.")
}

begin, err = strconv.ParseUint(matchs[1], 10, 16)
if err != nil {
Wrap(&err,
"Failed to parse range begin from %s.",
matchs[0],
)
return
if c.TProxies == nil {
c.TProxies = map[string]*TProxy{}
}

end, err = strconv.ParseUint(matchs[2], 10, 16)
if err != nil {
Wrap(&err,
"Failed to parse range end from %s.",
matchs[1],
)
return
for name := range c.TProxies {
tp := c.TProxies[name]
if tp.Name == "" {
tp.Name = name
}
if tp.DNSHijack != nil && tp.DNSHijack.Addr == nil {
addr := consts.IPv4LocalhostStr
tp.DNSHijack.Addr = &addr
}
}

return
Expand Down
68 changes: 12 additions & 56 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,23 @@ type Config struct {
}

type ConfigV1 struct {
Version uint8 `yaml:"version" validate:"required,eq=1"`
Repeater *Repeater `yaml:"repeater"`
Version uint8 `yaml:"version" validate:"required,eq=1"`

Proxies map[string]*Proxy `yaml:"proxies" validate:"dive"`
TProxies map[string]*TProxy `yaml:"tproxies" validate:"dive"`

Rules []Rule `yaml:"rules" validate:"dive"`
Bypass *Bypass `yaml:"bypass"`
CgroupRoot CgroupRoot `yaml:"cgroup-root" validate:"required,dirpath|eq=AUTO"`
RouteTable int `yaml:"route-table"`
Marks string `yaml:"marks" validate:"required"`
CgroupRoot CgroupRoot `yaml:"cgroup-root" validate:"required,dirpath|eq=AUTO"`
Bypass *Bypass `yaml:"bypass"`
TProxies map[string]*TProxy `yaml:"tproxies" validate:"dive"`
Rules []Rule `yaml:"rules" validate:"dive"`
RouteTable int `yaml:"route-table" validate:"required"`
}

type CgroupRoot string
type RerouteMark uint32
type FireWallMark uint32

// Bypass describes the bypass rules apply to all the TPROXY servers.
// If the destination matched in Bypass, the traffic will not be touched.
type Bypass struct {
IPV4 []string `yaml:"ipv4" validate:"dive,ipv4"`
IPV6 []string `yaml:"ipv6" validate:"dive,ipv6"`
IPV4 []string `yaml:"ipv4" validate:"dive,ipv4|cidrv4"`
IPV6 []string `yaml:"ipv6" validate:"dive,ipv6|cidrv6"`
}

// Rule describes a rule about how to handle traffic comes from a cgroup.
Expand All @@ -37,11 +33,6 @@ type Rule struct {
// TProxy means that the traffic comes from this cgroup
// should be redirected to a TPROXY server.
TProxy string `yaml:"tproxy" validate:"required_without_all=Proxy Drop Direct,excluded_with=Proxy Drop Direct"`
// Proxy means that the traffic comes from this cgroup
// should be redirected to a proxy server.
//
// NOTE: This is not implemented yet.
Proxy string `yaml:"proxy" validate:"required_without_all=TProxy Drop Direct,excluded_with=TProxy Drop Direct"`
// Drop means that the traffic comes from this cgroup will be dropped.
Drop bool `yaml:"drop" validate:"required_without_all=TProxy Proxy Direct,excluded_with=TProxy Proxy Direct"`
// Direct means that the traffic comes from this cgroup will not be touched.
Expand All @@ -54,14 +45,13 @@ type TProxy struct {
NoUDP bool `yaml:"no-udp"`
NoIPv6 bool `yaml:"no-ipv6"`
// NOTE: This field is not used yet.
Addr *string `yaml:"addr" validate:"hostname|ip"`
Port uint16 `yaml:"port" validate:"required"`
// Mark is the fwmark used to identify the TPROXY server.
// It **NOT** means that this TPROXY server
// must send traffic with the fwmark.
// This mark cgtproxy use internally designed to be changeable
// to void fwmark confliction with other program using nftables.
Mark RerouteMark `yaml:"mark"`
Mark FireWallMark `yaml:"mark" validate:"required"`
// DNSHijack will hijack the dns request traffic
// should redirect to this TPROXY server,
// and send them to directory to a dns server described in DNSHijack.
Expand All @@ -70,43 +60,9 @@ type TProxy struct {
}

type DNSHijack struct {
Addr string `yaml:"addr" validate:"ip4_addr"`
Port uint16 `yaml:"port"`
Addr *string `yaml:"addr" validate:"ip4_addr"`
Port uint16 `yaml:"port"`
// If TCP is set to true,
// tcp traffic to any 53 port will be hijacked too.
TCP bool `yaml:"tcp"`
}

// Repeater is configuration for a builtin TPROXY server,
// it is required if you have any entry in Proxies.
//
// NOTE: This is unimplemented yet.
type Repeater struct {
// Listens is a list of ip which this TPROXY server will listen on.
Listens []string `yaml:"listens" validate:"required,dive,ip"`
// TProxyPorts is a string like [20000,21000)
// describe a range of ports which this TPROXY server will use.
TProxyPorts string `yaml:"tproxy-ports" validate:"required"`
}

// Proxy is describes a proxy server.
// If any of Proxy is configurated,
// the repeater is required to be configured too.
//
// NOTE: This is not implemented yet.
type Proxy struct {
Protocol string `yaml:"protocol" validate:"required,eq=http|eq=https|eq=socks|eq=socks4|eq=socks5"`
Addr string `yaml:"addr" validate:"required,hostname|ip"`
Port uint16 `yaml:"port" validate:"required"`
Auth *Auth `yaml:"auth"`
UDP bool `yaml:"udp"`
NoIPv6 bool `yaml:"no-ipv6"`

TProxy *TProxy `yaml:"-"`
}

// Auth describes a proxy server's authentication.
type Auth struct {
User string `yaml:"user" validate:"required"`
Passwd string `yaml:"passwd" validate:"required"`
}
2 changes: 0 additions & 2 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ var _ = Describe("Configuration", func() {
WithFmt("../../test/data/wrong_type.yaml"),
ContextTableEntry("../../test/data/validation_fail.yaml", validator.ValidationErrors{}).
WithFmt("../../test/data/validation_fail.yaml"),
ContextTableEntry("../../test/data/wrong_ports.yaml", new(ErrBadRange)).
WithFmt("../../test/data/wrong_ports.yaml"),
)
})

Expand Down
2 changes: 1 addition & 1 deletion internal/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
const DefaultConfig = `
version: 1
cgroup-root: AUTO
marks: '[3000,3100)'
route-table: 400
rules:
- match: \/.*
direct: true
Expand Down
Loading

0 comments on commit c695bc1

Please sign in to comment.