Skip to content

Commit

Permalink
Merge b34b5ac into optimize_tuic
Browse files Browse the repository at this point in the history
  • Loading branch information
dae-prow[bot] authored Jul 16, 2023
2 parents e0bd35c + b34b5ac commit 8e836ec
Show file tree
Hide file tree
Showing 18 changed files with 423 additions and 286 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,21 @@ jobs:
repository: ${{ github.repository }}
ref: ${{ github.sha }}
fetch-depth: 0
check-runs: '["build", "main-build-passed"]'
secrets: inherit

build:
uses: daeuniverse/dae/.github/workflows/seed-build.yml@main
with:
ref: ${{ github.sha }}
build-type: main-build
secrets: inherit

post-actions:
if: always()
needs: [build]
uses: daeuniverse/ci-seed-jobs/.github/workflows/dae-post-actions.yml@master
with:
check-run-id: "dae-bot[bot]/main-build-passed"
check-run-conclusion: ${{ needs.build.result }}
secrets: inherit
13 changes: 13 additions & 0 deletions .github/workflows/daily-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,20 @@ jobs:
repository: ${{ github.repository }}
ref: ${{ github.sha }}
fetch-depth: 0
check-runs: '["build", "daily-build-passed"]'
secrets: inherit

build:
uses: daeuniverse/dae/.github/workflows/seed-build.yml@main
with:
ref: ${{ github.sha }}
build-type: daily-build

post-actions:
if: always()
needs: [build]
uses: daeuniverse/ci-seed-jobs/.github/workflows/dae-post-actions.yml@master
with:
check-run-id: "dae-bot[bot]/daily-build-passed"
check-run-conclusion: ${{ needs.build.result }}
secrets: inherit
18 changes: 16 additions & 2 deletions .github/workflows/pr-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,30 @@ on:

jobs:
pre-actions:
if: ${{ github.event.pull_request.draft == false }}
uses: daeuniverse/ci-seed-jobs/.github/workflows/pre-actions.yml@master
with:
repository: ${{ github.repository }}
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
check-runs: '["build", "pr-build-passed"]'
secrets: inherit

build:
needs: [pre-actions]
if: ${{ github.event.pull_request.draft == false }}
uses: daeuniverse/dae/.github/workflows/seed-build.yml@main
with:
ref: ${{ github.event.pull_request.head.sha }}
pr_build: true
pr_number: ${{ github.event.pull_request.number }}
pr-number: ${{ github.event.pull_request.number }}
build-type: pr-build
secrets: inherit

post-actions:
if: always()
needs: [build]
uses: daeuniverse/ci-seed-jobs/.github/workflows/dae-post-actions.yml@master
with:
check-run-id: "dae-bot[bot]/pr-build-passed"
check-run-conclusion: ${{ needs.build.result }}
secrets: inherit
21 changes: 14 additions & 7 deletions .github/workflows/seed-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ on:
type: string
required: true
default: ${{ github.ref }}
pr_build:
type: boolean
required: false
default: false
pr_number:
pr-number:
type: number
required: false
build-type:
type: string
description: "[pr-build,main-build,daily-build,release-build]"

jobs:
build:
Expand Down Expand Up @@ -78,9 +77,9 @@ jobs:
run: |
date=$(git log -1 --format="%cd" --date=short | sed s/-//g)
commit=$(echo ${REF} | cut -c1-6)
if [[ "${{ inputs.pr_build }}" == "true" ]]; then
if [[ "${{ inputs.build-type }}" == "pr-build" ]]; then
count=$(git rev-list --count origin/main..HEAD)
version="unstable-${date}.pr-${{ inputs.pr_number }}.r${count}.${commit}"
version="unstable-${date}.pr-${{ inputs.pr-number }}.r${count}.${commit}"
else
count=$(git rev-list --count HEAD)
version="unstable-${date}.r${count}.${commit}"
Expand Down Expand Up @@ -150,3 +149,11 @@ jobs:
with:
name: dae-${{ steps.get_filename.outputs.ASSET_NAME }}.zip
path: build/*

- name: Report result
uses: daeuniverse/ci-seed-jobs/common/report-check-run@master
if: always() && inputs.build-type == 'pr-build' && startsWith(github.event.pull_request.head.repo.full_name, github.repository_owner)
with:
app_id: ${{ secrets.GH_APP_ID }}
private_key: ${{ secrets.GH_APP_PRIVATE_KEY }}
id: "dae-bot[bot]/build"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dae
outline.json
go-mod/
node_modules/
*.log
36 changes: 28 additions & 8 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/mzz2017/softwind/netproxy"
"github.com/mzz2017/softwind/pkg/fastrand"
"github.com/mzz2017/softwind/protocol/direct"
"gopkg.in/natefinch/lumberjack.v2"

"github.com/daeuniverse/dae/cmd/internal"
"github.com/daeuniverse/dae/common"
Expand Down Expand Up @@ -45,19 +46,25 @@ var (
)

func init() {
runCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file")
runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-timestamp", "", false, "disable timestamp")
runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-pidfile", "", false, "not generate /var/run/dae.pid")
runCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Config file of dae.")
runCmd.PersistentFlags().StringVar(&logFile, "logfile", "", "Log file to write. Empty means writing to stdout and stderr.")
runCmd.PersistentFlags().IntVar(&logFileMaxSize, "logfile-maxsize", 30, "Unit: MB. The maximum size in megabytes of the log file before it gets rotated.")
runCmd.PersistentFlags().IntVar(&logFileMaxBackups, "logfile-maxbackups", 3, "The maximum number of old log files to retain.")
runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-timestamp", "", false, "Disable timestamp.")
runCmd.PersistentFlags().BoolVarP(&disablePidFile, "disable-pidfile", "", false, "Not generate /var/run/dae.pid.")

fastrand.Rand().Shuffle(len(CheckNetworkLinks), func(i, j int) {
CheckNetworkLinks[i], CheckNetworkLinks[j] = CheckNetworkLinks[j], CheckNetworkLinks[i]
})
}

var (
cfgFile string
disableTimestamp bool
disablePidFile bool
cfgFile string
logFile string
logFileMaxSize int
logFileMaxBackups int
disableTimestamp bool
disablePidFile bool

runCmd = &cobra.Command{
Use: "run",
Expand All @@ -78,7 +85,18 @@ var (
}).Fatalln("Failed to read config")
}

log := logger.NewLogger(conf.Global.LogLevel, disableTimestamp)
var logOpts *lumberjack.Logger
if logFile != "" {
logOpts = &lumberjack.Logger{
Filename: logFile,
MaxSize: logFileMaxSize,
MaxAge: 0,
MaxBackups: logFileMaxBackups,
LocalTime: true,
Compress: true,
}
}
log := logger.NewLogger(conf.Global.LogLevel, disableTimestamp, logOpts)
logrus.SetLevel(log.Level)

log.Infof("Include config files: [%v]", strings.Join(includes, ", "))
Expand Down Expand Up @@ -181,7 +199,9 @@ loop:
log.Infof("Include config files: [%v]", strings.Join(includes, ", "))
}
// New logger.
log = logger.NewLogger(newConf.Global.LogLevel, disableTimestamp)
oldLogOutput := log.Out
log = logger.NewLogger(newConf.Global.LogLevel, disableTimestamp, nil)
log.SetOutput(oldLogOutput) // FIXME: THIS IS A HACK.
logrus.SetLevel(log.Level)

// New control plane.
Expand Down
7 changes: 7 additions & 0 deletions common/consts/ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ const (
OutboundDirect OutboundIndex = iota
OutboundBlock

OutboundUserDefinedMin

OutboundMustRules OutboundIndex = 0xFC
OutboundControlPlaneRouting OutboundIndex = 0xFD
OutboundLogicalOr OutboundIndex = 0xFE
Expand Down Expand Up @@ -166,3 +168,8 @@ const (
LanWanFlag_IsLan
LanWanFlag_NotApplicable
)

const (
LinkType_None uint32 = iota
LinkType_Ethernet
)
6 changes: 2 additions & 4 deletions component/outbound/dialer_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ var TestNetworkType = &dialer.NetworkType{
IsDns: false,
}

var log = logger.NewLogger("trace", false, nil)

func TestDialerGroup_Select_Fixed(t *testing.T) {
log := logger.NewLogger("trace", false)
option := &dialer.GlobalOption{
Log: log,
TcpCheckOptionRaw: dialer.TcpCheckOptionRaw{Raw: []string{testTcpCheckUrl}},
Expand Down Expand Up @@ -69,7 +70,6 @@ func TestDialerGroup_Select_Fixed(t *testing.T) {
}

func TestDialerGroup_Select_MinLastLatency(t *testing.T) {
log := logger.NewLogger("trace", false)

option := &dialer.GlobalOption{
Log: log,
Expand Down Expand Up @@ -139,7 +139,6 @@ func TestDialerGroup_Select_MinLastLatency(t *testing.T) {
}

func TestDialerGroup_Select_Random(t *testing.T) {
log := logger.NewLogger("trace", false)

option := &dialer.GlobalOption{
Log: log,
Expand Down Expand Up @@ -179,7 +178,6 @@ func TestDialerGroup_Select_Random(t *testing.T) {
}

func TestDialerGroup_SetAlive(t *testing.T) {
log := logger.NewLogger("trace", false)

option := &dialer.GlobalOption{
Log: log,
Expand Down
20 changes: 14 additions & 6 deletions control/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func NewControlPlane(
// Add clsact qdisc
for _, ifname := range common.Deduplicate(append(append([]string{}, global.LanInterface...), global.WanInterface...)) {
_ = core.addQdisc(ifname)
_ = core.mapLinkType(ifname)
}
// Bind to LAN
if len(global.LanInterface) > 0 {
Expand Down Expand Up @@ -409,11 +410,12 @@ func NewControlPlane(
}
return nil
},
NewCache: func(fqdn string, answers []dnsmessage.RR, deadline time.Time) (cache *DnsCache, err error) {
NewCache: func(fqdn string, answers []dnsmessage.RR, deadline time.Time, originalDeadline time.Time) (cache *DnsCache, err error) {
return &DnsCache{
DomainBitmap: plane.routingMatcher.domainMatcher.MatchDomainBitmap(fqdn),
Answer: answers,
Deadline: deadline,
DomainBitmap: plane.routingMatcher.domainMatcher.MatchDomainBitmap(fqdn),
Answer: answers,
Deadline: deadline,
OriginalDeadline: originalDeadline,
}, nil
},
BestDialerChooser: plane.chooseBestDnsDialer,
Expand All @@ -423,7 +425,9 @@ func NewControlPlane(
return nil, err
}
// Refresh domain routing cache with new routing.
if dnsCache != nil && len(dnsCache) > 0 {
// FIXME: We temperarily disable it because we want to make change of DNS section take effects immediately.
// TODO: Add change detection.
if false && len(dnsCache) > 0 {
for cacheKey, cache := range dnsCache {
// Also refresh out-dated routing because kernel map items have no expiration.
lastDot := strings.LastIndex(cacheKey, ".")
Expand Down Expand Up @@ -556,7 +560,7 @@ func (c *ControlPlane) ChooseDialTarget(outbound consts.OutboundIndex, dst netip
if !outbound.IsReserved() && domain != "" {
switch c.dialMode {
case consts.DialMode_Domain:
if cache := c.dnsController.LookupDnsRespCache(domain, common.AddrToDnsType(dst.Addr())); cache != nil {
if cache := c.dnsController.LookupDnsRespCache(c.dnsController.cacheKey(domain, common.AddrToDnsType(dst.Addr())), true); cache != nil {
// Has A/AAAA records. It is a real domain.
dialMode = consts.DialMode_Domain
} else {
Expand All @@ -565,6 +569,9 @@ func (c *ControlPlane) ChooseDialTarget(outbound consts.OutboundIndex, dst netip
if c.realDomainSet.TestString(domain) {
c.muRealDomainSet.Unlock()
dialMode = consts.DialMode_Domain

// Should use this domain to reroute
shouldReroute = true
} else {
c.muRealDomainSet.Unlock()
// Lookup A/AAAA to make sure it is a real domain.
Expand All @@ -589,6 +596,7 @@ func (c *ControlPlane) ChooseDialTarget(outbound consts.OutboundIndex, dst netip

}
case consts.DialMode_DomainCao:
shouldReroute = true
fallthrough
case consts.DialMode_DomainPlus:
dialMode = consts.DialMode_Domain
Expand Down
16 changes: 16 additions & 0 deletions control/control_plane_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ func getIfParamsFromLink(link netlink.Link) (ifParams bpfIfParams, err error) {
return ifParams, nil
}

func (c *controlPlaneCore) mapLinkType(ifname string) error {
link, err := netlink.LinkByName(ifname)
if err != nil {
return err
}
linkType := uint32(0xffff)
switch link.Attrs().EncapType {
case "none":
linkType = consts.LinkType_None
case "ether":
linkType = consts.LinkType_Ethernet
default:
}
return c.bpf.bpfMaps.LinktypeMap.Update(uint32(link.Attrs().Index), linkType, ebpf.UpdateAny)
}

func (c *controlPlaneCore) addQdisc(ifname string) error {
link, err := netlink.LinkByName(ifname)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions control/dns_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
)

type DnsCache struct {
DomainBitmap []uint32
Answer []dnsmessage.RR
Deadline time.Time
DomainBitmap []uint32
Answer []dnsmessage.RR
Deadline time.Time
OriginalDeadline time.Time // This field is not impacted by `fixed_domain_ttl`.
}

func (c *DnsCache) FillInto(req *dnsmessage.Msg) {
Expand Down
Loading

0 comments on commit 8e836ec

Please sign in to comment.