Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加 fwmark 设置 #531

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ https://github.com/XIU2/CloudflareSpeedTest
-allip
测速全部的IP;对 IP 段中的每个 IP (仅支持 IPv4) 进行测速;(默认 每个 /24 段随机测速一个 IP)

-fwmark
给测速过程中的数据包设置 fwmark 以便过滤流量(默认 0 即不设置,仅 Linux 支持,需要 CAP_NET_ADMIN 或 CAP_NET_RAW 能力)

-v
打印程序版本 + 检查版本更新
-h
Expand Down Expand Up @@ -99,10 +102,14 @@ https://github.com/XIU2/CloudflareSpeedTest
flag.BoolVar(&task.Disable, "dd", false, "禁用下载测速")
flag.BoolVar(&task.TestAll, "allip", false, "测速全部 IP")

flag.IntVar(&task.FWMark, "fwmark", 0, "设置 fwmark")

flag.BoolVar(&printVersion, "v", false, "打印程序版本")
flag.Usage = func() { fmt.Print(help) }
flag.Parse()

fmt.Println("fwmark", task.FWMark)

if task.MinSpeed > 0 && time.Duration(maxDelay)*time.Millisecond == utils.InputMaxDelay {
fmt.Println("[小提示] 在使用 [-sl] 参数时,建议搭配 [-tl] 参数,以避免因凑不够 [-dn] 数量而一直测速...")
}
Expand Down
13 changes: 13 additions & 0 deletions task/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build !linux

package task

import (
"net"
)

var FWMark int

func newDialer() *net.Dialer {
return &net.Dialer{}
}
26 changes: 26 additions & 0 deletions task/common_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package task

import (
"log"
"net"
"syscall"
)

var FWMark int

func newDialer() *net.Dialer {
dialer := &net.Dialer{}
if FWMark != 0 {
dialer.Control = dialerController
}
return dialer
}

func dialerController(network, address string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, FWMark)
if err != nil {
log.Fatalln("failed to set fwmark", err)
}
})
}
2 changes: 1 addition & 1 deletion task/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func getDialContext(ip *net.IPAddr) func(ctx context.Context, network, address s
fakeSourceAddr = fmt.Sprintf("[%s]:%d", ip.String(), TCPPort)
}
return func(ctx context.Context, network, address string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, network, fakeSourceAddr)
return newDialer().DialContext(ctx, network, fakeSourceAddr)
}
}

Expand Down
4 changes: 3 additions & 1 deletion task/tcping.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ func (p *Ping) tcping(ip *net.IPAddr) (bool, time.Duration) {
} else {
fullAddress = fmt.Sprintf("[%s]:%d", ip.String(), TCPPort)
}
conn, err := net.DialTimeout("tcp", fullAddress, tcpConnectTimeout)
dialer := newDialer()
dialer.Timeout = tcpConnectTimeout
conn, err := dialer.Dial("tcp", fullAddress)
if err != nil {
return false, 0
}
Expand Down