Chore: script built

This commit is contained in:
gVisor bot 2021-10-27 23:10:11 +08:00
parent 4f7a0ec129
commit 258f837da3
13 changed files with 131 additions and 97 deletions

View file

@ -27,6 +27,10 @@ jobs:
- name: Get dependencies, run test - name: Get dependencies, run test
run: | run: |
# install python3.9
sudo add-apt-repository -y ppa:deadsnakes/ppa
sudo apt install -y python3.9 python3.9-dev
# fetch python cross compile source files # fetch python cross compile source files
mkdir -p bin/python/ mkdir -p bin/python/
cd bin/python/ cd bin/python/
@ -34,20 +38,21 @@ jobs:
curl -LO https://raw.githubusercontent.com/yaling888/snack/main/python-3.9.7-darwin-arm64.tar.xz curl -LO https://raw.githubusercontent.com/yaling888/snack/main/python-3.9.7-darwin-arm64.tar.xz
curl -LO https://raw.githubusercontent.com/yaling888/snack/main/python-3.9.7-windows-amd64.tar.xz curl -LO https://raw.githubusercontent.com/yaling888/snack/main/python-3.9.7-windows-amd64.tar.xz
curl -LO https://raw.githubusercontent.com/yaling888/snack/main/python-3.9.7-windows-386.tar.xz curl -LO https://raw.githubusercontent.com/yaling888/snack/main/python-3.9.7-windows-386.tar.xz
curl -LO https://raw.githubusercontent.com/yaling888/snack/main/python-3.9.7-linux-amd64.tar.xz #curl -LO https://raw.githubusercontent.com/yaling888/snack/main/python-3.9.7-linux-amd64.tar.xz
curl -LO https://raw.githubusercontent.com/yaling888/snack/main/python-3.9.7-linux-arm64.tar.xz #curl -LO https://raw.githubusercontent.com/yaling888/snack/main/python-3.9.7-linux-arm64.tar.xz
#curl -LO https://raw.githubusercontent.com/yaling888/snack/main/python-3.9.7-linux-386.tar.xz #curl -LO https://raw.githubusercontent.com/yaling888/snack/main/python-3.9.7-linux-386.tar.xz
tar -Jxf python-3.9.7-darwin-amd64.tar.xz tar -Jxf python-3.9.7-darwin-amd64.tar.xz
tar -Jxf python-3.9.7-darwin-arm64.tar.xz tar -Jxf python-3.9.7-darwin-arm64.tar.xz
tar -Jxf python-3.9.7-windows-amd64.tar.xz tar -Jxf python-3.9.7-windows-amd64.tar.xz
tar -Jxf python-3.9.7-windows-386.tar.xz tar -Jxf python-3.9.7-windows-386.tar.xz
tar -Jxf python-3.9.7-linux-amd64.tar.xz #tar -Jxf python-3.9.7-linux-amd64.tar.xz
tar -Jxf python-3.9.7-linux-arm64.tar.xz #tar -Jxf python-3.9.7-linux-arm64.tar.xz
#tar -Jxf python-3.9.7-linux-386.tar.xz #tar -Jxf python-3.9.7-linux-386.tar.xz
rm python-3.9.7-*.tar.xz rm python-3.9.7-*.tar.xz
cd ../../ cd ../../
go test ./... # go test
go test -tags build_local ./...
# init xgo # init xgo
docker pull techknowlogick/xgo:latest docker pull techknowlogick/xgo:latest
@ -66,6 +71,7 @@ jobs:
BINDIR: bin BINDIR: bin
run: | run: |
make -j releases make -j releases
#ls -lahF bin/python/
- name: Prepare upload - name: Prepare upload
if: startsWith(github.ref, 'refs/tags/') == false if: startsWith(github.ref, 'refs/tags/') == false

View file

@ -18,8 +18,8 @@ STATIC_LDFLAGS='-X "github.com/Dreamacro/clash/constant.Version=$(VERSION)" \
PLATFORM_LIST = \ PLATFORM_LIST = \
darwin-amd64 \ darwin-amd64 \
darwin-arm64 \ darwin-arm64 \
linux-amd64 \ linux-amd64
linux-arm64 # linux-arm64
# linux-386 # linux-386
WINDOWS_ARCH_LIST = \ WINDOWS_ARCH_LIST = \
@ -44,7 +44,8 @@ linux-386:
$(XGOCMD) -dest=$(BINDIR) -out=$(NAME) -trimpath=true -ldflags=$(STATIC_LDFLAGS) -targets=linux/386 $(BUILD_PACKAGE) $(XGOCMD) -dest=$(BINDIR) -out=$(NAME) -trimpath=true -ldflags=$(STATIC_LDFLAGS) -targets=linux/386 $(BUILD_PACKAGE)
linux-amd64: linux-amd64:
GOARCH=amd64 GOOS=linux $(GOBUILD) -ldflags $(STATIC_LDFLAGS) -o $(BINDIR)/$(NAME)-$@ $(GOBUILD) -ldflags $(RELEASE_LDFLAGS) -o $(BINDIR)/$(NAME)-$@
#GOARCH=amd64 GOOS=linux $(GOBUILD) -ldflags $(RELEASE_LDFLAGS) -o $(BINDIR)/$(NAME)-$@
#$(XGOCMD) -dest=$(BINDIR) -out=$(NAME) -trimpath=true -ldflags=$(STATIC_LDFLAGS) -targets=linux/amd64 $(BUILD_PACKAGE) #$(XGOCMD) -dest=$(BINDIR) -out=$(NAME) -trimpath=true -ldflags=$(STATIC_LDFLAGS) -targets=linux/amd64 $(BUILD_PACKAGE)
linux-arm64: linux-arm64:
@ -76,6 +77,9 @@ all-arch: $(PLATFORM_LIST) $(WINDOWS_ARCH_LIST)
releases: $(gz_releases) $(zip_releases) releases: $(gz_releases) $(zip_releases)
vet:
$(GOCMD) vet -tags build_local ./...
lint: lint:
golangci-lint run --build-tags=build_local --disable-all -E govet -E gofumpt -E megacheck ./... golangci-lint run --build-tags=build_local --disable-all -E govet -E gofumpt -E megacheck ./...

View file

@ -1,25 +1,25 @@
//go:build !build_local //go:build !build_local && cgo
// +build !build_local // +build !build_local,cgo
package script package script
/* /*
//#cgo linux,amd64 pkg-config: python-3.9-embed #cgo linux,amd64 pkg-config: python3-embed
#cgo darwin,amd64 CFLAGS: -I/build/python/python-3.9.7-darwin-amd64/include/python3.9 #cgo darwin,amd64 CFLAGS: -I/build/python/python-3.9.7-darwin-amd64/include/python3.9
#cgo darwin,arm64 CFLAGS: -I/build/python/python-3.9.7-darwin-arm64/include/python3.9 #cgo darwin,arm64 CFLAGS: -I/build/python/python-3.9.7-darwin-arm64/include/python3.9
#cgo windows,amd64 CFLAGS: -I/build/python/python-3.9.7-windows-amd64/include -DMS_WIN64 #cgo windows,amd64 CFLAGS: -I/build/python/python-3.9.7-windows-amd64/include -DMS_WIN64
#cgo windows,386 CFLAGS: -I/build/python/python-3.9.7-windows-386/include #cgo windows,386 CFLAGS: -I/build/python/python-3.9.7-windows-386/include
#cgo linux,amd64 CFLAGS: -I/home/runner/work/clash/clash/bin/python/python-3.9.7-linux-amd64/include/python3.9 //#cgo linux,amd64 CFLAGS: -I/home/runner/work/clash/clash/bin/python/python-3.9.7-linux-amd64/include/python3.9
#cgo linux,arm64 CFLAGS: -I/build/python/python-3.9.7-linux-arm64/include/python3.9 //#cgo linux,arm64 CFLAGS: -I/build/python/python-3.9.7-linux-arm64/include/python3.9
#cgo linux,386 CFLAGS: -I/build/python/python-3.9.7-linux-386/include/python3.9 //#cgo linux,386 CFLAGS: -I/build/python/python-3.9.7-linux-386/include/python3.9
#cgo darwin,amd64 LDFLAGS: -L/build/python/python-3.9.7-darwin-amd64/lib -lpython3.9 -ldl -framework CoreFoundation #cgo darwin,amd64 LDFLAGS: -L/build/python/python-3.9.7-darwin-amd64/lib -lpython3.9 -ldl -framework CoreFoundation
#cgo darwin,arm64 LDFLAGS: -L/build/python/python-3.9.7-darwin-arm64/lib -lpython3.9 -ldl -framework CoreFoundation #cgo darwin,arm64 LDFLAGS: -L/build/python/python-3.9.7-darwin-arm64/lib -lpython3.9 -ldl -framework CoreFoundation
#cgo windows,amd64 LDFLAGS: -L/build/python/python-3.9.7-windows-amd64/lib -lpython39 -lpthread -lm #cgo windows,amd64 LDFLAGS: -L/build/python/python-3.9.7-windows-amd64/lib -lpython39 -lpthread -lm
#cgo windows,386 LDFLAGS: -L/build/python/python-3.9.7-windows-386/lib -lpython39 -lpthread -lm #cgo windows,386 LDFLAGS: -L/build/python/python-3.9.7-windows-386/lib -lpython39 -lpthread -lm
#cgo linux,amd64 LDFLAGS: -L/home/runner/work/clash/clash/bin/python/python-3.9.7-linux-amd64/lib -lpython3.9 -lpthread -ldl -lutil -lm //#cgo linux,amd64 LDFLAGS: -L/home/runner/work/clash/clash/bin/python/python-3.9.7-linux-amd64/lib -lpython3.9 -lpthread -ldl -lutil -lm
#cgo linux,arm64 LDFLAGS: -L/build/python/python-3.9.7-linux-arm64/lib -lpython3.9 -lpthread -ldl -lutil -lm //#cgo linux,arm64 LDFLAGS: -L/build/python/python-3.9.7-linux-arm64/lib -lpython3.9 -lpthread -ldl -lutil -lm
#cgo linux,386 LDFLAGS: -L/build/python/python-3.9.7-linux-386/lib -lpython3.9 -lcrypt -lpthread -ldl -lutil -lm //#cgo linux,386 LDFLAGS: -L/build/python/python-3.9.7-linux-386/lib -lpython3.9 -lpthread -ldl -lutil -lm
*/ */
import "C" import "C"

View file

@ -8,23 +8,30 @@ PyObject *main_fn;
PyObject *clash_context; PyObject *clash_context;
// init_python // init_python
void init_python(const char *path) { void init_python(const char *program, const char *path) {
// Py_NoSiteFlag = 1;
// Py_FrozenFlag = 1;
// Py_IgnoreEnvironmentFlag = 1;
// Py_IsolatedFlag = 1;
append_inittab(); append_inittab();
Py_Initialize(); wchar_t *programName = Py_DecodeLocale(program, NULL);
if (programName != NULL) {
wchar_t *program = Py_DecodeLocale("clash", NULL); Py_SetProgramName(programName);
if (program != NULL) { PyMem_RawFree(programName);
Py_SetProgramName(program);
PyMem_RawFree(program);
} }
// wchar_t *newPath = Py_DecodeLocale(path, NULL); // wchar_t *newPath = Py_DecodeLocale(path, NULL);
// if (newPath != NULL) { // if (newPath != NULL) {
// Py_SetPath(newPath); // Py_SetPath(newPath);
// PyMem_RawFree(newPath);
// } // }
// Py_Initialize();
Py_InitializeEx(0);
char *pathPrefix = "import sys; sys.path.append('"; char *pathPrefix = "import sys; sys.path.append('";
char *pathSuffix = "')"; char *pathSuffix = "')";
char *newPath = (char *) malloc(strlen(pathPrefix) + strlen(path) + strlen(pathSuffix)); char *newPath = (char *) malloc(strlen(pathPrefix) + strlen(path) + strlen(pathSuffix));

View file

@ -74,7 +74,7 @@ func (pyObject *PyObject) Clear() {
} }
// Py_Initialize initialize Python3 // Py_Initialize initialize Python3
func Py_Initialize(path string) error { func Py_Initialize(program string, path string) error {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
@ -89,8 +89,7 @@ func Py_Initialize(path string) error {
cPath := C.CString(path) cPath := C.CString(path)
//defer C.free(unsafe.Pointer(cPath)) //defer C.free(unsafe.Pointer(cPath))
C.init_python(cPath) C.init_python(C.CString(program), cPath)
//C.Py_Initialize()
err := PyLastError() err := PyLastError()
if err != nil { if err != nil {
@ -129,17 +128,10 @@ func Py_Finalize() {
} }
} }
//Py_GetVersion get
func Py_GetVersion() string { func Py_GetVersion() string {
cversion := C.Py_GetVersion() cversion := C.Py_GetVersion()
return C.GoString(cversion) return strings.Split(C.GoString(cversion), "\n")[0]
}
func PyRun_SimpleString(command string) int {
ccommand := C.CString(command)
defer C.free(unsafe.Pointer(ccommand))
// C.PyRun_SimpleString is a macro, using C.PyRun_SimpleStringFlags instead
return int(C.PyRun_SimpleStringFlags(ccommand, nil))
} }
// loadPyFunc loads a Python function by module and function name // loadPyFunc loads a Python function by module and function name
@ -311,13 +303,19 @@ func initPython3Callback() {
//NewClashPyContext new clash context for python //NewClashPyContext new clash context for python
func NewClashPyContext(ruleProvidersName []string) error { func NewClashPyContext(ruleProvidersName []string) error {
cStringArr := make([]*C.char, len(ruleProvidersName)) length := len(ruleProvidersName)
cStringArr := make([]*C.char, length)
for i, v := range ruleProvidersName { for i, v := range ruleProvidersName {
cStringArr[i] = C.CString(v) cStringArr[i] = C.CString(v)
defer C.free(unsafe.Pointer(cStringArr[i])) defer C.free(unsafe.Pointer(cStringArr[i]))
} }
rs := int(C.new_clash_py_context((**C.char)(unsafe.Pointer(&cStringArr[0])), C.int(len(ruleProvidersName)))) cArrPointer := unsafe.Pointer(nil)
if length > 0 {
cArrPointer = unsafe.Pointer(&cStringArr[0])
}
rs := int(C.new_clash_py_context((**C.char)(cArrPointer), C.int(length)))
if rs == 0 { if rs == 0 {
err := PyLastError() err := PyLastError()
@ -335,5 +333,5 @@ func killSelf() {
return return
} }
p.Signal(syscall.SIGINT) _ = p.Signal(syscall.SIGINT)
} }

View file

@ -29,7 +29,7 @@ void set_log_callback(log_callback cb);
/*---------------------------------------------------------------*/ /*---------------------------------------------------------------*/
void append_inittab(); void append_inittab();
void init_python(const char *path); void init_python(const char *program, const char *path);
void load_main_func(); void load_main_func();
void finalize_Python(); void finalize_Python();
void py_clear(PyObject *obj); void py_clear(PyObject *obj);

View file

@ -85,12 +85,24 @@ func ruleProviderCallbackFn(cProviderName *C.char, cMetadata *C.struct_Metadata)
dstIp := C.GoString(cMetadata.dst_ip) dstIp := C.GoString(cMetadata.dst_ip)
dstPort := strconv.Itoa(int(cMetadata.dst_port)) dstPort := strconv.Itoa(int(cMetadata.dst_port))
dst := net.ParseIP(dstIp)
addrType := constant.AtypDomainName
if dst != nil {
if dst.To4() != nil {
addrType = constant.AtypIPv4
} else {
addrType = constant.AtypIPv6
}
}
metadata := &constant.Metadata{ metadata := &constant.Metadata{
Process: processName, Process: processName,
SrcIP: net.ParseIP(srcIp), SrcIP: net.ParseIP(srcIp),
DstIP: net.ParseIP(dstIp), DstIP: dst,
SrcPort: srcPort, SrcPort: srcPort,
DstPort: dstPort, DstPort: dstPort,
AddrType: addrType,
Host: host, Host: host,
} }

View file

@ -365,10 +365,10 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[
providersMap[name] = pd providersMap[name] = pd
} }
for _, provider := range providersMap { for _, rp := range providersMap {
log.Infoln("Start initial provider %s", provider.Name()) log.Infoln("Start initial provider %s", rp.Name())
if err := provider.Initial(); err != nil { if err := rp.Initial(); err != nil {
return nil, nil, fmt.Errorf("initial proxy provider %s error: %w", provider.Name(), err) return nil, nil, fmt.Errorf("initial proxy provider %s error: %w", rp.Name(), err)
} }
} }
@ -474,7 +474,7 @@ time = ClashTime()
return fmt.Errorf("initialized script module failure, %s", err.Error()) return fmt.Errorf("initialized script module failure, %s", err.Error())
} }
if err = S.Py_Initialize(C.Path.ScriptDir()); err != nil { if err = S.Py_Initialize(C.Path.GetExecutableFullPath(), C.Path.ScriptDir()); err != nil {
return fmt.Errorf("initialized script module failure, %s", err.Error()) return fmt.Errorf("initialized script module failure, %s", err.Error())
} else if mode == T.Script { } else if mode == T.Script {
if err = S.LoadMainFunction(); err != nil { if err = S.LoadMainFunction(); err != nil {
@ -482,7 +482,7 @@ time = ClashTime()
} }
} }
log.Infoln("Start initial script module successful") log.Infoln("Start initial script module successful, version: %s", S.Py_GetVersion())
return nil return nil
} }
@ -561,7 +561,7 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[strin
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} else { } else {
log.Infoln("Start initial script context successful") log.Infoln("Start initial script context successful, provider records: %v", len(providerNames))
} }
} }
@ -582,7 +582,7 @@ func parseHosts(cfg *RawConfig) (*trie.DomainTrie, error) {
if ip == nil { if ip == nil {
return nil, fmt.Errorf("%s is not a valid IP", ipStr) return nil, fmt.Errorf("%s is not a valid IP", ipStr)
} }
tree.Insert(domain, ip) _ = tree.Insert(domain, ip)
} }
} }
@ -740,7 +740,7 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie) (*DNS, error) {
if len(cfg.FakeIPFilter) != 0 { if len(cfg.FakeIPFilter) != 0 {
host = trie.New() host = trie.New()
for _, domain := range cfg.FakeIPFilter { for _, domain := range cfg.FakeIPFilter {
host.Insert(domain, true) _ = host.Insert(domain, true)
} }
} }

View file

@ -80,7 +80,7 @@ func (p *path) ScriptDir() string {
p.scriptDir = dir p.scriptDir = dir
} else { } else {
p.scriptDir = P.Join(os.TempDir(), Name) p.scriptDir = P.Join(os.TempDir(), Name)
os.MkdirAll(p.scriptDir, 0o644) _ = os.MkdirAll(p.scriptDir, 0o644)
} }
return p.scriptDir return p.scriptDir
} }
@ -92,3 +92,12 @@ func (p *path) Script() string {
func (p *path) GetAssetLocation(file string) string { func (p *path) GetAssetLocation(file string) string {
return P.Join(p.homeDir, file) return P.Join(p.homeDir, file)
} }
func (p *path) GetExecutableFullPath() string {
exePath, err := os.Executable()
if err != nil {
return "clash"
}
res, _ := filepath.EvalSymlinks(exePath)
return res
}

View file

@ -22,10 +22,7 @@ func NewTCPHandler(dnsIP net.IP, tcpIn chan<- C.ConnContext) golwip.TCPConnHandl
func (h *tcpHandler) Handle(conn net.Conn, target *net.TCPAddr) error { func (h *tcpHandler) Handle(conn net.Conn, target *net.TCPAddr) error {
if shouldHijackDns(h.dnsIP, target.IP, target.Port) { if shouldHijackDns(h.dnsIP, target.IP, target.Port) {
hijackTCPDns(conn) hijackTCPDns(conn)
if log.Level() == log.DEBUG {
log.Debugln("[TUN] hijack dns tcp: %s:%d", target.IP.String(), target.Port) log.Debugln("[TUN] hijack dns tcp: %s:%d", target.IP.String(), target.Port)
}
return nil return nil
} }
@ -36,10 +33,11 @@ func (h *tcpHandler) Handle(conn net.Conn, target *net.TCPAddr) error {
src, _ := conn.LocalAddr().(*net.TCPAddr) src, _ := conn.LocalAddr().(*net.TCPAddr)
dst, _ := conn.RemoteAddr().(*net.TCPAddr) dst, _ := conn.RemoteAddr().(*net.TCPAddr)
//addrType := C.AtypIPv4
//if dst.IP.To4() == nil { addrType := C.AtypIPv4
// addrType = C.AtypIPv6 if dst.IP.To4() == nil {
//} addrType = C.AtypIPv6
}
metadata := &C.Metadata{ metadata := &C.Metadata{
NetWork: C.TCP, NetWork: C.TCP,
@ -48,10 +46,14 @@ func (h *tcpHandler) Handle(conn net.Conn, target *net.TCPAddr) error {
DstIP: dst.IP, DstIP: dst.IP,
SrcPort: strconv.Itoa(src.Port), SrcPort: strconv.Itoa(src.Port),
DstPort: strconv.Itoa(dst.Port), DstPort: strconv.Itoa(dst.Port),
AddrType: addrType,
Host: "", Host: "",
} }
go func(conn net.Conn, metadata *C.Metadata) { go func(conn net.Conn, metadata *C.Metadata) {
//if c, ok := conn.(*net.TCPConn); ok {
// c.SetKeepAlive(true)
//}
h.tcpIn <- context.NewConnContext(conn, metadata) h.tcpIn <- context.NewConnContext(conn, metadata)
}(conn, metadata) }(conn, metadata)

View file

@ -46,17 +46,14 @@ func NewUDPHandler(dnsIP net.IP, udpIn chan<- *inbound.PacketAdapter) golwip.UDP
return &udpHandler{dnsIP, udpIn} return &udpHandler{dnsIP, udpIn}
} }
func (h *udpHandler) Connect(conn golwip.UDPConn, target *net.UDPAddr) error { func (h *udpHandler) Connect(golwip.UDPConn, *net.UDPAddr) error {
return nil return nil
} }
func (h *udpHandler) ReceiveTo(conn golwip.UDPConn, data []byte, addr *net.UDPAddr) error { func (h *udpHandler) ReceiveTo(conn golwip.UDPConn, data []byte, addr *net.UDPAddr) error {
if shouldHijackDns(h.dnsIP, addr.IP, addr.Port) { if shouldHijackDns(h.dnsIP, addr.IP, addr.Port) {
hijackUDPDns(conn, data, addr) hijackUDPDns(conn, data, addr)
if log.Level() == log.DEBUG {
log.Debugln("[TUN] hijack dns udp: %s:%d", addr.IP.String(), addr.Port) log.Debugln("[TUN] hijack dns udp: %s:%d", addr.IP.String(), addr.Port)
}
return nil return nil
} }

View file

@ -16,16 +16,17 @@ func handleTCP(conn net.Conn, endpoint *binding.Endpoint, tcpIn chan<- C.ConnCon
Port: int(endpoint.Source.Port), Port: int(endpoint.Source.Port),
Zone: "", Zone: "",
} }
dst := &net.TCPAddr{ dst := &net.TCPAddr{
IP: endpoint.Target.IP, IP: endpoint.Target.IP,
Port: int(endpoint.Target.Port), Port: int(endpoint.Target.Port),
Zone: "", Zone: "",
} }
//addrType := C.AtypIPv4 addrType := C.AtypIPv4
//if dst.IP.To4() == nil { if dst.IP.To4() == nil {
// addrType = C.AtypIPv6 addrType = C.AtypIPv6
//} }
metadata := &C.Metadata{ metadata := &C.Metadata{
NetWork: C.TCP, NetWork: C.TCP,
@ -34,8 +35,12 @@ func handleTCP(conn net.Conn, endpoint *binding.Endpoint, tcpIn chan<- C.ConnCon
DstIP: dst.IP, DstIP: dst.IP,
SrcPort: strconv.Itoa(src.Port), SrcPort: strconv.Itoa(src.Port),
DstPort: strconv.Itoa(dst.Port), DstPort: strconv.Itoa(dst.Port),
AddrType: addrType,
Host: "", Host: "",
} }
//if c, ok := conn.(*net.TCPConn); ok {
// c.SetKeepAlive(true)
//}
tcpIn <- context.NewConnContext(conn, metadata) tcpIn <- context.NewConnContext(conn, metadata)
} }

View file

@ -60,10 +60,7 @@ func NewAdapter(device dev.TunDevice, conf config.Tun, mtu int, gateway, mirror
t.SetTCPHandler(func(conn net.Conn, endpoint *binding.Endpoint) { t.SetTCPHandler(func(conn net.Conn, endpoint *binding.Endpoint) {
if shouldHijackDns(dnsAddr, endpoint.Target) { if shouldHijackDns(dnsAddr, endpoint.Target) {
hijackTCPDns(conn) hijackTCPDns(conn)
if log.Level() == log.DEBUG {
log.Debugln("[TUN] hijack dns tcp: %s:%d", endpoint.Target.IP.String(), endpoint.Target.Port) log.Debugln("[TUN] hijack dns tcp: %s:%d", endpoint.Target.IP.String(), endpoint.Target.Port)
}
return return
} }
@ -72,10 +69,7 @@ func NewAdapter(device dev.TunDevice, conf config.Tun, mtu int, gateway, mirror
t.SetUDPHandler(func(payload []byte, endpoint *binding.Endpoint, sender redirect.UDPSender) { t.SetUDPHandler(func(payload []byte, endpoint *binding.Endpoint, sender redirect.UDPSender) {
if shouldHijackDns(dnsAddr, endpoint.Target) { if shouldHijackDns(dnsAddr, endpoint.Target) {
hijackUDPDns(payload, endpoint, sender) hijackUDPDns(payload, endpoint, sender)
if log.Level() == log.DEBUG {
log.Debugln("[TUN] hijack dns udp: %s:%d", endpoint.Target.IP.String(), endpoint.Target.Port) log.Debugln("[TUN] hijack dns udp: %s:%d", endpoint.Target.IP.String(), endpoint.Target.Port)
}
return return
} }