github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/cmd/android/options.go (about) 1 package yuhaiin 2 3 import ( 4 "fmt" 5 "syscall" 6 7 "github.com/Asutorufa/yuhaiin/pkg/log" 8 "github.com/Asutorufa/yuhaiin/pkg/net/netapi" 9 "github.com/Asutorufa/yuhaiin/pkg/utils/syncmap" 10 ) 11 12 type Opts struct { 13 Host string `json:"host"` 14 Savepath string `json:"savepath"` 15 Socks5 string `json:"socks5"` 16 Http string `json:"http"` 17 IPv6 bool `json:"ipv6"` 18 Bypass *Bypass `json:"bypass"` 19 DNS *DNSSetting `json:"dns"` 20 TUN *TUN `json:"tun"` 21 Log *Log `json:"log"` 22 CloseFallback Closer 23 } 24 25 type Log struct { 26 SaveLogcat bool `json:"save_logcat"` 27 // 0:verbose, 1:debug, 2:info, 3:warning, 4:error, 5: fatal 28 LogLevel int32 `json:"log_level"` 29 } 30 31 type Bypass struct { 32 // 0: bypass, 1: proxy, 2: direct, 3: block 33 TCP int32 `json:"tcp"` 34 // 0: bypass, 1: proxy, 2: direct, 3: block 35 UDP int32 `json:"udp"` 36 37 Block string `json:"block"` 38 Proxy string `json:"proxy"` 39 Direct string `json:"direct"` 40 } 41 42 type DNSSetting struct { 43 Server string `json:"server"` 44 Fakedns bool `json:"fakedns"` 45 FakednsIpRange string `json:"fakedns_ip_range"` 46 FakednsIpv6Range string `json:"fakedns_ipv6_range"` 47 ResolveRemoteDomain bool `json:"resolve_remote_domain"` 48 Remote *DNS `json:"remote"` 49 Local *DNS `json:"local"` 50 Bootstrap *DNS `json:"bootstrap"` 51 Hosts []byte `json:"hosts"` 52 } 53 54 type DNS struct { 55 Host string `json:"host"` 56 // Type 57 // 0: reserve 58 // 1: udp 59 // 2: tcp 60 // 3: doh 61 // 4: dot 62 // 5: doq 63 // 6: doh3 64 Type int32 `json:"type"` 65 Subnet string `json:"subnet"` 66 TlsServername string `json:"tls_servername"` 67 } 68 69 type TUN struct { 70 FD int32 `json:"fd"` 71 MTU int32 `json:"mtu"` 72 Portal string `json:"portal"` 73 PortalV6 string `json:"portal_v6"` 74 DNSHijacking bool `json:"dns_hijacking"` 75 // Driver 76 // 0: fdbased 77 // 1: channel 78 // 2: tun2socket 79 // 3: tun2socket_gvisor 80 Driver int32 `json:"driver"` 81 UidDumper UidDumper 82 SocketProtect SocketProtect 83 } 84 85 type UidDumper interface { 86 DumpUid(ipProto int32, srcIp string, srcPort int32, destIp string, destPort int32) (int32, error) 87 GetUidInfo(uid int32) (string, error) 88 } 89 90 type SocketProtect interface { 91 Protect(socket int32) bool 92 } 93 94 type Closer interface { 95 Close() error 96 } 97 98 type uidDumper struct { 99 UidDumper 100 cache syncmap.SyncMap[int32, string] 101 } 102 103 func NewUidDumper(ud UidDumper) netapi.ProcessDumper { 104 if ud == nil { 105 return nil 106 } 107 return &uidDumper{UidDumper: ud} 108 } 109 110 func (u *uidDumper) GetUidInfo(uid int32) (string, error) { 111 if r, ok := u.cache.Load(uid); ok { 112 return r, nil 113 } 114 115 r, err := u.UidDumper.GetUidInfo(uid) 116 if err != nil { 117 return "", err 118 } 119 120 u.cache.Store(uid, r) 121 return r, nil 122 } 123 124 func (a *uidDumper) ProcessName(networks string, src, dst netapi.Address) (string, error) { 125 var network int32 126 switch networks { 127 case "tcp": 128 network = syscall.IPPROTO_TCP 129 case "udp": 130 network = syscall.IPPROTO_UDP 131 } 132 133 uid, err := a.UidDumper.DumpUid(network, src.Hostname(), int32(src.Port().Port()), dst.Hostname(), int32(dst.Port().Port())) 134 if err != nil { 135 log.Error("dump uid error", "err", err) 136 } 137 138 var name string 139 if uid != 0 { 140 name, err = a.UidDumper.GetUidInfo(uid) 141 if err != nil { 142 return "", fmt.Errorf("get uid info error: %v", err) 143 } 144 } 145 146 return fmt.Sprintf("%s(%d)", name, uid), nil 147 }