fix: http latency test would fail when v2ray with tproxy is running

This commit is contained in:
mzz2017 committed 2021-09-08 14:33:30 +08:00
1 parent 159c542272
commit 69b2f8833f
12 files changed
+118 -138

No files matched your search

+22 -8
View File
@@ -1,6 +1,7 @@
package v2ray
import (
"errors"
"fmt"
"github.com/shirou/gopsutil/v3/mem"
"github.com/v2rayA/v2rayA/core/serverObj"
@@ -17,6 +18,8 @@ import (
"time"
)
var NoConnectedServerErr = fmt.Errorf("no selected servers")
// Process is a v2ray-core process
type Process struct {
proc *os.Process
@@ -205,29 +208,40 @@ func getConnectedServerObjs() ([]serverObj.ServerObj, []serverInfo, error) {
return serverObjs, serverInfos, nil
}
func UpdateV2RayConfig() (err error) {
func NewTemplateFromConnectedServers(setting *configure.Setting) (tmpl *Template, err error) {
//read the database and convert to the v2ray-core template
serverObjs, serverInfos, err := getConnectedServerObjs()
if err != nil {
return err
return nil, err
}
if len(serverObjs) == 0 {
//no servers are selected, which means to stop the v2ray-core
ProcessManager.Stop(true)
return nil
return nil, NoConnectedServerErr
}
var pluginPorts map[int]int
if pluginPorts, err = findAvailablePluginPorts(serverObjs); err != nil {
return err
return nil, err
}
for i := range serverInfos {
if port, ok := pluginPorts[i]; ok {
serverInfos[i].PluginPort = port
}
}
tmpl, err := NewTemplate(serverInfos)
tmpl, err = NewTemplate(serverInfos, setting)
if err != nil {
return
return nil, err
}
return tmpl, nil
}
func UpdateV2RayConfig() (err error) {
tmpl, err := NewTemplateFromConnectedServers(nil)
if err != nil {
if errors.Is(err, NoConnectedServerErr) {
//no servers are selected, which means to stop the v2ray-core
ProcessManager.Stop(true)
return nil
}
return err
}
err = ProcessManager.Start(tmpl)
if err != nil {
+2 -3
View File
@@ -49,8 +49,7 @@ func (m *CoreProcessManager) beforeStart(t *Template) (err error) {
return fmt.Errorf("Please sync datetime first. Your datetime is %v, and the correct datetime is %v", time.Now().Local().Format(ntp.DisplayFormat), t.Local().Format(ntp.DisplayFormat))
}
setting := configure.GetSettingNotNil()
if (setting.Transparent == configure.TransparentGfwlist || setting.RulePortMode == configure.GfwlistMode) && !asset.IsGFWListExists() {
if (t.Setting.Transparent == configure.TransparentGfwlist || t.Setting.RulePortMode == configure.GfwlistMode) && !asset.IsGFWListExists() {
return fmt.Errorf("cannot find GFWList files. update GFWList and try again")
}
@@ -61,7 +60,7 @@ func (m *CoreProcessManager) beforeStart(t *Template) (err error) {
}
func (m *CoreProcessManager) afterStart(t *Template) (err error) {
if err = CheckAndSetupTransparentProxy(true); err != nil {
if err = CheckAndSetupTransparentProxy(true, t.Setting); err != nil {
m.Stop(true)
return
}
+6 -2
View File
@@ -63,11 +63,15 @@ func WriteTransparentProxyRules(preprocess func(c *iptables.SetupCommands)) (err
return nil
}
func CheckAndSetupTransparentProxy(checkRunning bool) (err error) {
func CheckAndSetupTransparentProxy(checkRunning bool, setting *configure.Setting) (err error) {
if conf.GetEnvironmentConfig().Lite {
return nil
}
setting := configure.GetSettingNotNil()
if setting != nil {
setting.FillEmpty()
} else {
setting = configure.GetSettingNotNil()
}
if (!checkRunning || ProcessManager.Running()) && setting.Transparent != configure.TransparentClose {
DeleteTransparentProxyRules()
err = WriteTransparentProxyRules(func(c *iptables.SetupCommands) {
+23 -9
View File
@@ -52,6 +52,7 @@ type Template struct {
OutboundTags []string `json:"-"`
ApiCloses []func() `json:"-"`
ApiPort int `json:"-"`
Setting *configure.Setting
}
func (t *Template) Close() error {
@@ -413,7 +414,7 @@ func (t *Template) setDNSRouting(routing []coreObj.RoutingRule, supportUDP map[s
t.Routing.Rules = append(t.Routing.Rules,
coreObj.RoutingRule{Type: "field", InboundTag: []string{"dns"}, OutboundTag: "direct"},
)
setting := configure.GetSettingNotNil()
setting := t.Setting
if setting.AntiPollution != configure.AntipollutionClosed {
dnsOut := coreObj.RoutingRule{ // hijack traffic to port 53
Type: "field",
@@ -759,6 +760,7 @@ func parseRoutingA(t *Template, routingInboundTags []string) error {
})
return nil
}
func (t *Template) setTransparentRouting(setting *configure.Setting) (err error) {
switch setting.Transparent {
case configure.TransparentProxy:
@@ -823,7 +825,7 @@ func (t *Template) setOutboundSockopt(setting *configure.Setting) {
tmp := setting.TcpFastOpen == configure.Yes
t.Outbounds[i].StreamSettings.Sockopt.TCPFastOpen = &tmp
}
checkAndSetMark(&t.Outbounds[i], mark)
t.checkAndSetMark(&t.Outbounds[i], mark)
}
}
func (t *Template) setDualStack(setting *configure.Setting) {
@@ -1363,7 +1365,7 @@ func (t *Template) setVlessGrpcRouting() {
}
}
func NewTemplate(serverInfos []serverInfo) (t *Template, err error) {
func NewTemplate(serverInfos []serverInfo, setting *configure.Setting) (t *Template, err error) {
vmessInfo2ServerInfos, outboundName2VmessInfos := RefineOutboundInfos(serverInfos)
ps2OutboundNames := make(map[string][]string)
for outboundName, vis := range outboundName2VmessInfos {
@@ -1371,7 +1373,12 @@ func NewTemplate(serverInfos []serverInfo) (t *Template, err error) {
ps2OutboundNames[vi.GetName()] = append(ps2OutboundNames[vi.GetName()], outboundName)
}
}
setting := configure.GetSettingNotNil()
if setting != nil {
setting.FillEmpty()
} else {
setting = configure.GetSettingNotNil()
}
var tmplJson Template
// read template json
raw := []byte(TemplateJson)
@@ -1381,6 +1388,7 @@ func NewTemplate(serverInfos []serverInfo) (t *Template, err error) {
}
tmplJson.CoreVersion, _ = where.GetV2rayServiceVersion()
t = &tmplJson
t.Setting = setting
// log
t.Log = new(coreObj.Log)
if logLevel := log.ParseLevel(conf.GetEnvironmentConfig().LogLevel); logLevel >= log.ParseLevel("debug") {
@@ -1601,14 +1609,20 @@ func WriteV2rayConfig(content []byte) (err error) {
return
}
func NewEmptyTemplate() (t *Template) {
func NewEmptyTemplate(setting *configure.Setting) (t *Template) {
t = new(Template)
t.CoreVersion, _ = where.GetV2rayServiceVersion()
if setting != nil {
setting.FillEmpty()
} else {
setting = configure.GetSettingNotNil()
}
t.Setting = setting
return t
}
func checkAndSetMark(o *coreObj.OutboundObject, mark int) {
if configure.GetSettingNotNil().Transparent == configure.TransparentClose {
func (t *Template) checkAndSetMark(o *coreObj.OutboundObject, mark int) {
if t.Setting.Transparent == configure.TransparentClose {
return
}
if o.StreamSettings == nil {
@@ -1620,7 +1634,7 @@ func checkAndSetMark(o *coreObj.OutboundObject, mark int) {
o.StreamSettings.Sockopt.Mark = &mark
}
func (t *Template) AddMappingOutbound(o serverObj.ServerObj, inboundPort string, udpSupport bool, pluginPort int, protocol string) (err error) {
func (t *Template) InsertMappingOutbound(o serverObj.ServerObj, inboundPort string, udpSupport bool, pluginPort int, protocol string) (err error) {
if t.CoreVersion == "" {
t.CoreVersion, _ = where.GetV2rayServiceVersion()
}
@@ -1640,7 +1654,7 @@ func (t *Template) AddMappingOutbound(o serverObj.ServerObj, inboundPort string,
}
}
var mark = 0x80
checkAndSetMark(&c.CoreOutbound, mark)
t.checkAndSetMark(&c.CoreOutbound, mark)
t.Outbounds = append(t.Outbounds, c.CoreOutbound)
iPort, err := strconv.Atoi(inboundPort)
if err != nil || iPort <= 0 {
-13
View File
@@ -20,7 +20,6 @@ type Configure struct {
Setting *Setting `json:"setting"`
Accounts map[string]string `json:"accounts"`
Ports Ports `json:"ports"`
PortWhiteList PortWhiteList `json:"portWhiteList"`
InternalDnsList *string `json:"internalDnsList"`
ExternalDnsList *string `json:"externalDnsList"`
RoutingA *string `json:"routingA"`
@@ -39,7 +38,6 @@ func New() *Configure {
HttpWithPac: 20172,
VlessGrpc: 0,
},
PortWhiteList: PortWhiteList{},
InternalDnsList: nil,
ExternalDnsList: nil,
RoutingA: nil,
@@ -96,9 +94,6 @@ func SetConfigure(cfg *Configure) error {
if err := SetExternalDnsList(cfg.ExternalDnsList); err != nil {
return err
}
if err := SetPortWhiteList(&cfg.PortWhiteList); err != nil {
return err
}
if err := OverwriteConnects(NewWhiches(cfg.ConnectedServers)); err != nil {
return err
}
@@ -132,9 +127,6 @@ func SetSetting(setting *Setting) (err error) {
func SetPorts(ports *Ports) (err error) {
return db.Set("system", "ports", ports)
}
func SetPortWhiteList(portWhiteList *PortWhiteList) (err error) {
return db.Set("system", "portWhiteList", portWhiteList.Compressed())
}
func SetInternalDnsList(dnsList *string) (err error) {
if dnsList == nil {
return db.Set("system", "internalDnsList", nil)
@@ -265,11 +257,6 @@ func GetPortsNotNil() *Ports {
}
return p
}
func GetPortWhiteListNotNil() *PortWhiteList {
r := new(PortWhiteList)
_ = db.Get("system", "portWhiteList", r)
return r
}
func GetExternalDnsListNotNil() (list []string) {
r := new(string)
_ = db.Get("system", "externalDnsList", r)
+10 -1
View File
@@ -1,6 +1,10 @@
package configure
import "github.com/v2rayA/v2rayA/core/ipforward"
import (
"github.com/v2rayA/v2rayA/common"
"github.com/v2rayA/v2rayA/core/ipforward"
"github.com/v2rayA/v2rayA/pkg/util/log"
)
type Setting struct {
RulePortMode RulePortMode `json:"pacMode"`
@@ -36,7 +40,12 @@ func NewSetting() (setting *Setting) {
TransparentType: TransparentRedirect,
AntiPollution: AntipollutionClosed,
}
}
func (s *Setting) FillEmpty() {
if err := common.FillEmpty(s, GetSettingNotNil()); err != nil {
log.Warn("FillEmpty: %v:", err)
}
}
type CustomPac struct {
+19 -2
View File
@@ -1,3 +1,6 @@
//go:build linux || openbsd || freebsd
// +build linux openbsd freebsd
package plugin
import (
@@ -5,13 +8,27 @@ import (
"github.com/v2rayA/v2rayA/pkg/util/log"
"golang.org/x/sys/unix"
"net"
"runtime"
"syscall"
)
var fwmarkIoctl int
func init() {
switch runtime.GOOS {
case "linux", "android":
fwmarkIoctl = 36 /* unix.SO_MARK */
case "freebsd":
fwmarkIoctl = 0x1015 /* unix.SO_USER_COOKIE */
case "openbsd":
fwmarkIoctl = 0x1021 /* unix.SO_RTABLE */
}
}
func SoMarkControl(c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
//TODO: force to set 0xff. any chances to customize this value?
err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, 0x80)
err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, fwmarkIoctl, 0x80)
if err != nil {
return
}
@@ -19,7 +36,7 @@ func SoMarkControl(c syscall.RawConn) error {
}
func BindControl(c syscall.RawConn, laddr string, lport uint32) error {
return c.Control(func(fd uintptr) {
err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, 0x80)
err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, fwmarkIoctl, 0x80)
if err != nil {
log.Warn("control: %s", err)
return
+2 -2
View File
@@ -1,5 +1,5 @@
//go:build !linux
// +build !linux
//go:build !linux && !openbsd && !freebsd
// +build !linux,!openbsd,!freebsd
package plugin
@@ -1,75 +0,0 @@
package controller
import (
"github.com/gin-gonic/gin"
"github.com/v2rayA/v2rayA/common"
"github.com/v2rayA/v2rayA/conf"
"github.com/v2rayA/v2rayA/core/v2ray"
"github.com/v2rayA/v2rayA/db/configure"
"github.com/v2rayA/v2rayA/server/service"
"net"
"strconv"
)
func GetPortWhiteList(ctx *gin.Context) {
common.ResponseSuccess(ctx, configure.GetPortWhiteListNotNil())
}
func PutPortWhiteList(ctx *gin.Context) {
var data configure.PortWhiteList
err := ctx.ShouldBindJSON(&data)
if err != nil {
common.ResponseError(ctx, logError("bad request"))
return
}
if !data.Valid() {
common.ResponseError(ctx, logError("invalid format of port"))
return
}
err = configure.SetPortWhiteList(&data)
if err != nil {
common.ResponseError(ctx, logError(err))
return
}
common.ResponseSuccess(ctx, nil)
}
func PostPortWhiteList(ctx *gin.Context) {
var data struct {
RequestPort string `json:"requestPort"`
}
err := ctx.ShouldBindJSON(&data)
if err != nil {
common.ResponseError(ctx, logError("bad request"))
return
}
p := service.GetPorts()
_, listenPort, _ := net.SplitHostPort(conf.GetEnvironmentConfig().Address)
tcpList := []string{"1:1023", data.RequestPort, listenPort}
udpList := []string{"1:1023"}
if p.Http > 0 {
tcpList = append(tcpList, strconv.Itoa(p.Http))
}
if p.HttpWithPac > 0 {
tcpList = append(tcpList, strconv.Itoa(p.HttpWithPac))
}
if p.VlessGrpc > 0 {
tcpList = append(tcpList, strconv.Itoa(p.VlessGrpc))
}
if p.Socks5 > 0 {
tcpList = append(tcpList, strconv.Itoa(p.Socks5))
udpList = append(udpList, strconv.Itoa(p.Socks5))
}
wpl := configure.PortWhiteList{
TCP: tcpList,
UDP: udpList,
}
err = configure.SetPortWhiteList(&wpl)
if err != nil {
common.ResponseError(ctx, logError(err))
return
}
v2ray.CheckAndStopTransparentProxy()
v2ray.CheckAndSetupTransparentProxy(true)
common.ResponseSuccess(ctx, nil)
}
@@ -1,14 +0,0 @@
package controller
import (
"github.com/v2rayA/v2rayA/db/configure"
"testing"
)
func TestCompressed(t *testing.T) {
pwl := configure.PortWhiteList{
TCP: []string{"2", "2", "5:10", "7", "8", "1", "-5", "64432", "65535", "65536"},
UDP: []string{"3:65534", "65533:65534", "65533", "65534", "65535", "65536"},
}
t.Log(pwl.Compressed())
}
-3
View File
@@ -198,9 +198,6 @@ func Run() error {
auth.GET("ports", controller.GetPorts)
auth.PUT("ports", controller.PutPorts)
//auth.PUT("account", controller.PutAccount)
auth.GET("portWhiteList", controller.GetPortWhiteList)
auth.PUT("portWhiteList", controller.PutPortWhiteList)
auth.POST("portWhiteList", controller.PostPortWhiteList)
auth.GET("dnsList", controller.GetDnsList)
auth.PUT("dnsList", controller.PutDnsList)
auth.GET("routingA", controller.GetRoutingA)
+34 -6
View File
@@ -1,6 +1,7 @@
package service
import (
"errors"
"fmt"
"github.com/v2rayA/v2rayA/common/httpClient"
"github.com/v2rayA/v2rayA/common/resolv"
@@ -27,7 +28,7 @@ func Ping(which []*configure.Which, timeout time.Duration) (_ []*configure.Which
//暂时关闭透明代理
v2ray.CheckAndStopTransparentProxy()
defer func() {
if e := v2ray.CheckAndSetupTransparentProxy(true); e != nil {
if e := v2ray.CheckAndSetupTransparentProxy(true, nil); e != nil {
err = fmt.Errorf("Ping: %v: %v", e, err)
}
}()
@@ -53,8 +54,12 @@ func Ping(which []*configure.Which, timeout time.Duration) (_ []*configure.Which
}
func addHosts(tmpl *v2ray.Template, vms []serverObj.ServerObj) {
tmpl.DNS = new(coreObj.DNS)
tmpl.DNS.Hosts = make(coreObj.Hosts)
if tmpl.DNS == nil {
tmpl.DNS = new(coreObj.DNS)
}
if tmpl.DNS.Hosts == nil {
tmpl.DNS.Hosts = make(coreObj.Hosts)
}
const concurrency = 5
var mu sync.Mutex
var limit = make(chan struct{}, concurrency)
@@ -90,6 +95,8 @@ func addHosts(tmpl *v2ray.Template, vms []serverObj.ServerObj) {
func TestHttpLatency(which []*configure.Which, timeout time.Duration, maxParallel int, showLog bool) ([]*configure.Which, error) {
specialMode.StopDNSSupervisor()
v2ray.CheckAndStopTransparentProxy()
var whiches = configure.NewWhiches(which)
for i := len(which) - 1; i >= 0; i-- {
if which[i].TYPE == configure.SubscriptionType { //去掉subscriptionType
@@ -111,8 +118,29 @@ func TestHttpLatency(which []*configure.Which, timeout time.Duration, maxParalle
vms[i] = sr.ServerObj
}
//modify the template based on current configuration
tmpl := v2ray.NewEmptyTemplate()
tmpl.SetAPI()
var (
tmpl *v2ray.Template
err error
)
if v2rayRunning {
tmpl, err = v2ray.NewTemplateFromConnectedServers(nil)
if err != nil {
if !errors.Is(err, v2ray.NoConnectedServerErr) {
log.Warn("NewTemplateFromConnectedServers: %v", err)
}
}
}
if tmpl == nil {
tmpl = v2ray.NewEmptyTemplate(&configure.Setting{
RulePortMode: configure.WhitelistMode,
TcpFastOpen: configure.Default,
MuxOn: configure.No,
Transparent: configure.TransparentClose,
SpecialMode: configure.SpecialModeNone,
AntiPollution: configure.AntipollutionClosed,
})
tmpl.SetAPI()
}
inboundPortMap := make([]string, len(vms))
pluginPortMap := make(map[int]int)
var toClose []net.Listener
@@ -147,7 +175,7 @@ func TestHttpLatency(which []*configure.Which, timeout time.Duration, maxParalle
pluginPort = port
pluginPortMap[i] = port
}
err := tmpl.AddMappingOutbound(v, v2rayInboundPort, false, pluginPort, "socks")
err := tmpl.InsertMappingOutbound(v, v2rayInboundPort, false, pluginPort, "socks")
if err != nil {
if strings.Contains(err.Error(), "unsupported") {
which[i].Latency = "UNSUPPORTED PROTOCOL"