Skip to content

Commit

Permalink
fix android dump process bug
Browse files Browse the repository at this point in the history
Signed-off-by: Asutorufa <16442314+Asutorufa@users.noreply.github.com>
  • Loading branch information
Asutorufa committed May 25, 2023
1 parent f7d7798 commit b25dce8
Show file tree
Hide file tree
Showing 23 changed files with 702 additions and 75 deletions.
12 changes: 11 additions & 1 deletion pkg/app/statistics/statistic.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/Asutorufa/yuhaiin/pkg/protos/config/listener"
gs "github.com/Asutorufa/yuhaiin/pkg/protos/statistic/grpc"
"github.com/Asutorufa/yuhaiin/pkg/utils/cache"
"github.com/Asutorufa/yuhaiin/pkg/utils/goos"
"github.com/Asutorufa/yuhaiin/pkg/utils/id"
"github.com/Asutorufa/yuhaiin/pkg/utils/syncmap"
"google.golang.org/protobuf/types/known/emptypb"
Expand Down Expand Up @@ -151,7 +152,16 @@ func (c *Connections) DumpProcess(addr proxy.Address) (s string) {
if !ok {
return
}
dst, ok := addr.Value(proxy.DestinationKey{})

var dst any
if goos.IsAndroid == 1 {
dst, ok = addr.Value(proxy.InboundKey{})
if !ok {
dst, ok = addr.Value(proxy.DestinationKey{})
}
} else {
dst, ok = addr.Value(proxy.DestinationKey{})
}
if !ok {
return
}
Expand Down
82 changes: 82 additions & 0 deletions pkg/net/interfaces/proxy/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package proxy

import (
"context"
"net"
"net/netip"

"github.com/Asutorufa/yuhaiin/pkg/net/interfaces/dns"
"github.com/Asutorufa/yuhaiin/pkg/protos/statistic"
)

type Proxy interface {
StreamProxy
PacketProxy
Dispatch(context.Context, Address) (Address, error)
}

type StreamProxy interface {
Conn(context.Context, Address) (net.Conn, error)
}

type PacketProxy interface {
PacketConn(context.Context, Address) (net.PacketConn, error)
}

type Port interface {
Port() uint16
String() string
}

type Type uint8

func (t Type) String() string {
switch t {
case DOMAIN:
return "DOMAIN"
case IP:
return "IP"
case UNIX:
return "UNIX"
case EMPTY:
return "EMPTY"
default:
return "UNKNOWN"
}
}

const (
DOMAIN Type = 1
IP Type = 2
UNIX Type = 3
EMPTY Type = 4
)

type Address interface {
// Hostname return hostname of address, eg: www.example.com, 127.0.0.1, ff::ff
Hostname() string
// IP return net.IP, if address is ip else resolve the domain and return one of ips
IP(context.Context) (net.IP, error)
AddrPort(context.Context) (netip.AddrPort, error)
// Port return port of address
Port() Port
// Type return type of address, domain or ip
Type() Type
NetworkType() statistic.Type

net.Addr

// WithResolver will use call IP(), IPHost(), UDPAddr(), TCPAddr()
// return the current resolver is applied, if can't cover return false
WithResolver(_ dns.DNS, canCover bool) bool
// OverrideHostname clone address(exclude Values) and change hostname
OverrideHostname(string) Address
OverridePort(Port) Address

UDPAddr(context.Context) (*net.UDPAddr, error)
TCPAddr(context.Context) (*net.TCPAddr, error)

WithValue(key, value any)
Value(key any) (any, bool)
RangeValue(func(k, v any) bool)
}
72 changes: 0 additions & 72 deletions pkg/net/interfaces/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,6 @@ import (
"golang.org/x/net/dns/dnsmessage"
)

type Proxy interface {
StreamProxy
PacketProxy
Dispatch(context.Context, Address) (Address, error)
}

type StreamProxy interface {
Conn(context.Context, Address) (net.Conn, error)
}

type PacketProxy interface {
PacketConn(context.Context, Address) (net.PacketConn, error)
}

type EmptyDispatch struct{}

func (EmptyDispatch) Dispatch(_ context.Context, a Address) (Address, error) { return a, nil }
Expand Down Expand Up @@ -88,67 +74,9 @@ func (e errProxy) PacketConn(context.Context, Address) (net.PacketConn, error) {

func PaseNetwork(s string) statistic.Type { return statistic.Type(statistic.Type_value[s]) }

type Type uint8

func (t Type) String() string {
switch t {
case DOMAIN:
return "DOMAIN"
case IP:
return "IP"
case UNIX:
return "UNIX"
case EMPTY:
return "EMPTY"
default:
return "UNKNOWN"
}
}

const (
DOMAIN Type = 1
IP Type = 2
UNIX Type = 3
EMPTY Type = 4
)

type Port interface {
Port() uint16
String() string
}

type resolverKey struct{}
type resolverCanCoverKey struct{}

type Address interface {
// Hostname return hostname of address, eg: www.example.com, 127.0.0.1, ff::ff
Hostname() string
// IP return net.IP, if address is ip else resolve the domain and return one of ips
IP(context.Context) (net.IP, error)
AddrPort(context.Context) (netip.AddrPort, error)
// Port return port of address
Port() Port
// Type return type of address, domain or ip
Type() Type
NetworkType() statistic.Type

net.Addr

// WithResolver will use call IP(), IPHost(), UDPAddr(), TCPAddr()
// return the current resolver is applied, if can't cover return false
WithResolver(_ dns.DNS, canCover bool) bool
// OverrideHostname clone address(exclude Values) and change hostname
OverrideHostname(string) Address
OverridePort(Port) Address

UDPAddr(context.Context) (*net.UDPAddr, error)
TCPAddr(context.Context) (*net.TCPAddr, error)

WithValue(key, value any)
Value(key any) (any, bool)
RangeValue(func(k, v any) bool)
}

func Value[T any](s interface{ Value(any) (any, bool) }, k any, Default T) T {
z, ok := s.Value(k)
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions pkg/net/proxy/tun/tun2socket/tun2socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package tun2socket
import (
"io"
"net/netip"
"runtime"

"github.com/Asutorufa/yuhaiin/pkg/net/proxy/tun/tun2socket/nat"
"github.com/Asutorufa/yuhaiin/pkg/utils/goos"
)

type Tun2Socket struct {
Expand All @@ -28,7 +28,7 @@ func (t *Tun2Socket) Close() error {
_ = t.tcp.Close()
_ = t.udp.Close()

if runtime.GOOS != "android" {
if goos.IsAndroid == 0 {
return t.device.Close()
}

Expand Down
84 changes: 84 additions & 0 deletions pkg/utils/goos/goos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build ignore

package main

import (
"bytes"
"fmt"
"log"
"os"
"strings"
)

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// package goos contains GOOS-specific constants.

// The next line makes 'go generate' write the zgoos*.go files with
// per-OS information, including constants named Is$GOOS for every
// known GOOS. The constant is 1 on the current system, 0 otherwise;
// multiplying by them is useful for defining GOOS-specific constants.
//
//go:generate go run gengoos.go

var gooses []string

func main() {
data, err := os.ReadFile("syslist.go")
if err != nil {
log.Fatal(err)
}
const goosPrefix = `var knownOS = map[string]bool{`
inGOOS := false
for _, line := range strings.Split(string(data), "\n") {
if strings.HasPrefix(line, goosPrefix) {
inGOOS = true
} else if inGOOS && strings.HasPrefix(line, "}") {
break
} else if inGOOS {
goos := strings.Fields(line)[0]
goos = strings.TrimPrefix(goos, `"`)
goos = strings.TrimSuffix(goos, `":`)
gooses = append(gooses, goos)
}
}

for _, target := range gooses {
if target == "nacl" {
continue
}
var tags []string
if target == "linux" {
tags = append(tags, "!android") // must explicitly exclude android for linux
}
if target == "solaris" {
tags = append(tags, "!illumos") // must explicitly exclude illumos for solaris
}
if target == "darwin" {
tags = append(tags, "!ios") // must explicitly exclude ios for darwin
}
tags = append(tags, target) // must explicitly include target for bootstrapping purposes
var buf bytes.Buffer
fmt.Fprintf(&buf, "// Code generated by gengoos.go using 'go generate'. DO NOT EDIT.\n\n")
fmt.Fprintf(&buf, "//go:build %s\n\n", strings.Join(tags, " && "))
fmt.Fprintf(&buf, "package goos\n\n")
fmt.Fprintf(&buf, "const GOOS = `%s`\n\n", target)
for _, goos := range gooses {
value := 0
if goos == target {
value = 1
}
fmt.Fprintf(&buf, "const Is%s = %d\n", strings.Title(goos), value)
}
err := os.WriteFile("zgoos_"+target+".go", buf.Bytes(), 0666)
if err != nil {
log.Fatal(err)
}
}
}
81 changes: 81 additions & 0 deletions pkg/utils/goos/syslist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package goos

// Note that this file is read by internal/goarch/gengoarch.go and by
// internal/goos/gengoos.go. If you change this file, look at those
// files as well.

// knownOS is the list of past, present, and future known GOOS values.
// Do not remove from this list, as it is used for filename matching.
// If you add an entry to this list, look at unixOS, below.
var knownOS = map[string]bool{
"aix": true,
"android": true,
"darwin": true,
"dragonfly": true,
"freebsd": true,
"hurd": true,
"illumos": true,
"ios": true,
"js": true,
"linux": true,
"nacl": true,
"netbsd": true,
"openbsd": true,
"plan9": true,
"solaris": true,
"wasip1": true,
"windows": true,
"zos": true,
}

// unixOS is the set of GOOS values matched by the "unix" build tag.
// This is not used for filename matching.
// This list also appears in cmd/dist/build.go and
// cmd/go/internal/imports/build.go.
var unixOS = map[string]bool{
"aix": true,
"android": true,
"darwin": true,
"dragonfly": true,
"freebsd": true,
"hurd": true,
"illumos": true,
"ios": true,
"linux": true,
"netbsd": true,
"openbsd": true,
"solaris": true,
}

// knownArch is the list of past, present, and future known GOARCH values.
// Do not remove from this list, as it is used for filename matching.
var knownArch = map[string]bool{
"386": true,
"amd64": true,
"amd64p32": true,
"arm": true,
"armbe": true,
"arm64": true,
"arm64be": true,
"loong64": true,
"mips": true,
"mipsle": true,
"mips64": true,
"mips64le": true,
"mips64p32": true,
"mips64p32le": true,
"ppc": true,
"ppc64": true,
"ppc64le": true,
"riscv": true,
"riscv64": true,
"s390": true,
"s390x": true,
"sparc": true,
"sparc64": true,
"wasm": true,
}
Loading

0 comments on commit b25dce8

Please sign in to comment.